tt-rss-android/src/org/fox/ttrss/ArticleFragment.java

136 lines
3.7 KiB
Java
Raw Normal View History

2011-09-09 11:58:11 +00:00
package org.fox.ttrss;
import java.text.SimpleDateFormat;
import java.util.Date;
2011-09-09 11:58:11 +00:00
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.text.method.LinkMovementMethod;
2011-09-09 11:58:11 +00:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
2011-09-09 16:36:09 +00:00
import android.webkit.WebView;
import android.widget.TextView;
2011-09-09 11:58:11 +00:00
2011-11-27 16:04:25 +00:00
import com.google.ads.AdRequest;
import com.google.ads.AdView;
public class ArticleFragment extends Fragment {
@SuppressWarnings("unused")
2011-09-09 16:36:09 +00:00
private final String TAG = this.getClass().getSimpleName();
private SharedPreferences m_prefs;
private Article m_article;
private ArticleOps m_articleOps;
2011-09-09 11:58:11 +00:00
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
m_article = savedInstanceState.getParcelable("article");
2011-09-09 11:58:11 +00:00
}
View view = inflater.inflate(R.layout.article_fragment, container, false);
2011-09-09 16:36:09 +00:00
if (m_article != null) {
TextView title = (TextView)view.findViewById(R.id.title);
if (title != null) {
title.setMovementMethod(LinkMovementMethod.getInstance());
title.setText(Html.fromHtml("<a href=\""+m_article.link.replace("\"", "\\\"")+"\">" + m_article.title + "</a>"));
}
WebView web = (WebView)view.findViewById(R.id.content);
if (web != null) {
// this is ridiculous
// TODO white on black style for dark theme
String content;
try {
String cssOverride = "";
if (m_prefs.getString("theme", "THEME_DARK").equals("THEME_DARK")) {
cssOverride = "body { background : black; color : #f0f0f0}\n";
}
content =
"<html>" +
"<head>" +
"<meta content=\"text/html; charset=utf-8\" http-equiv=\"content-type\">" +
"<style type=\"text/css\">" +
cssOverride +
"img { max-width : 90%; }" +
"body { text-align : justify; }" +
"</style>" +
"</head>" +
"<body>" + m_article.content + "</body></html>";
} catch (Exception e) {
content = getString(R.string.could_not_decode_content);
e.printStackTrace();
}
web.loadDataWithBaseURL(null, content, "text/html", "utf-8", null);
}
2011-11-24 12:08:02 +00:00
TextView dv = (TextView)view.findViewById(R.id.date);
if (dv != null) {
Date d = new Date(m_article.updated * 1000L);
SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy, HH:mm");
dv.setText(df.format(d));
}
TextView cv = (TextView)view.findViewById(R.id.comments);
// comments are not currently returned by the API
if (cv != null) {
cv.setVisibility(View.GONE);
}
2011-11-27 16:04:25 +00:00
AdView av = (AdView)view.findViewById(R.id.ad);
2011-11-28 05:20:29 +00:00
boolean enableAds = m_prefs.getBoolean("enable_ads", false);
2011-11-27 16:04:25 +00:00
if (av != null) {
2011-11-28 05:20:29 +00:00
if (enableAds) {
AdRequest request = new AdRequest();
request.addTestDevice(AdRequest.TEST_EMULATOR);
av.loadAd(request);
} else {
av.setVisibility(View.GONE);
}
2011-11-27 16:04:25 +00:00
}
}
2011-09-09 11:58:11 +00:00
return view;
}
@Override
public void onDestroy() {
2011-09-11 09:04:48 +00:00
super.onDestroy();
2011-09-09 11:58:11 +00:00
}
@Override
2011-09-09 16:36:09 +00:00
public void onSaveInstanceState (Bundle out) {
2011-09-09 11:58:11 +00:00
super.onSaveInstanceState(out);
2011-09-09 16:36:09 +00:00
out.putParcelable("article", m_article);
2011-09-09 11:58:11 +00:00
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
m_articleOps = (ArticleOps)activity;
m_article = m_articleOps.getSelectedArticle();
2011-09-09 11:58:11 +00:00
}
}