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

200 lines
5.4 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-12-07 12:25:50 +00:00
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
2011-11-28 09:45:19 +00:00
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;
import android.webkit.WebSettings;
2011-09-09 16:36:09 +00:00
import android.webkit.WebView;
import android.widget.TextView;
2011-09-09 11:58:11 +00:00
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 OnlineServices m_onlineServices;
//private Article m_nextArticle;
//private Article m_prevArticle;
public ArticleFragment() {
super();
}
public ArticleFragment(Article article) {
super();
m_article = article;
}
private View.OnTouchListener m_gestureListener;
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);
Activity activity = (Activity)getActivity();
2011-11-28 09:45:19 +00:00
if (activity != null) {
int orientation = activity.getWindowManager().getDefaultDisplay().getOrientation();
if (!m_onlineServices.isSmallScreen()) {
2011-11-28 09:45:19 +00:00
if (orientation % 2 == 0) {
view.findViewById(R.id.splitter_horizontal).setVisibility(View.GONE);
} else {
view.findViewById(R.id.splitter_vertical).setVisibility(View.GONE);
}
} else {
view.findViewById(R.id.splitter_vertical).setVisibility(View.GONE);
view.findViewById(R.id.splitter_horizontal).setVisibility(View.GONE);
}
} else {
view.findViewById(R.id.splitter_horizontal).setVisibility(View.GONE);
}
2011-09-09 16:36:09 +00:00
if (m_article != null) {
TextView title = (TextView)view.findViewById(R.id.title);
if (title != null) {
2011-11-30 09:02:26 +00:00
String titleStr;
if (m_article.title.length() > 200)
titleStr = m_article.title.substring(0, 200) + "...";
else
titleStr = m_article.title;
title.setMovementMethod(LinkMovementMethod.getInstance());
2011-11-30 09:02:26 +00:00
title.setText(Html.fromHtml("<a href=\""+m_article.link.replace("\"", "\\\"")+"\">" + titleStr + "</a>"));
}
WebView web = (WebView)view.findViewById(R.id.content);
if (web != null) {
String content;
2011-11-29 04:03:38 +00:00
String cssOverride = "";
2011-12-01 09:05:21 +00:00
WebSettings ws = web.getSettings();
ws.setSupportZoom(true);
ws.setBuiltInZoomControls(true);
2011-11-29 04:03:38 +00:00
if (m_prefs.getString("theme", "THEME_DARK").equals("THEME_DARK")) {
cssOverride = "body { background : black; color : #e0e0e0}\n";
web.setBackgroundColor(android.R.color.black);
} else {
cssOverride = "";
}
2011-12-07 12:25:50 +00:00
String articleContent = m_article.content != null ? m_article.content : "";
2011-12-07 12:25:50 +00:00
Document doc = Jsoup.parse(articleContent);
if (doc != null) {
// thanks webview for crashing on <video> tag
Elements videos = doc.select("video");
for (Element video : videos)
video.remove();
articleContent = doc.toString();
}
2011-11-29 04:03:38 +00:00
content =
"<html>" +
"<head>" +
"<meta content=\"text/html; charset=utf-8\" http-equiv=\"content-type\">" +
//"<meta name=\"viewport\" content=\"target-densitydpi=device-dpi\" />" +
2011-11-29 04:03:38 +00:00
"<style type=\"text/css\">" +
cssOverride +
2011-12-01 09:05:21 +00:00
"img { max-width : 98%; height : auto; }" +
2011-11-29 04:03:38 +00:00
"body { text-align : justify; }" +
"</style>" +
"</head>" +
2011-12-07 12:25:50 +00:00
"<body>" + articleContent + "</body></html>";
2011-11-29 04:03:38 +00:00
2011-12-07 12:13:58 +00:00
try {
web.loadDataWithBaseURL(null, content, "text/html", "utf-8", null);
} catch (RuntimeException e) {
e.printStackTrace();
}
if (m_onlineServices.isSmallScreen())
web.setOnTouchListener(m_gestureListener);
}
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));
}
2011-11-28 09:45:19 +00:00
TextView tagv = (TextView)view.findViewById(R.id.tags);
if (tagv != null) {
if (m_article.tags != null) {
String tagsStr = "";
for (String tag : m_article.tags)
tagsStr += tag + ", ";
tagsStr = tagsStr.replaceAll(", $", "");
tagv.setText(tagsStr);
} else {
tagv.setVisibility(View.GONE);
}
}
}
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-12-07 12:25:50 +00:00
out.putParcelable("article", m_article);
2011-09-09 11:58:11 +00:00
}
2011-09-09 11:58:11 +00:00
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
m_onlineServices = (OnlineServices)activity;
//m_article = m_onlineServices.getSelectedArticle();
2011-09-09 11:58:11 +00:00
}
2011-09-09 11:58:11 +00:00
}