tt-rss-android/src/org/fox/ttrss/offline/OfflineArticleFragment.java

256 lines
7.6 KiB
Java
Raw Normal View History

2012-06-19 10:18:00 +00:00
package org.fox.ttrss.offline;
import java.text.SimpleDateFormat;
import java.util.Date;
2012-06-19 10:18:00 +00:00
import org.fox.ttrss.R;
import org.fox.ttrss.R.attr;
import org.fox.ttrss.R.id;
import org.fox.ttrss.R.layout;
import org.fox.ttrss.R.menu;
import org.fox.ttrss.util.ImageCacheService;
2011-12-07 11:59:30 +00:00
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.BaseColumns;
2012-06-19 14:24:22 +00:00
import android.support.v4.app.Fragment;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.TypedValue;
2012-03-12 08:51:57 +00:00
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
2012-06-19 14:24:22 +00:00
import android.view.ContextMenu.ContextMenuInfo;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;
public class OfflineArticleFragment extends Fragment {
@SuppressWarnings("unused")
private final String TAG = this.getClass().getSimpleName();
private SharedPreferences m_prefs;
private int m_articleId;
private Cursor m_cursor;
2011-12-07 08:50:04 +00:00
private OfflineServices m_offlineServices;
public OfflineArticleFragment() {
super();
}
public OfflineArticleFragment(int articleId) {
super();
m_articleId = articleId;
}
2012-03-12 08:51:57 +00:00
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
getActivity().getMenuInflater().inflate(R.menu.article_link_context_menu, menu);
menu.setHeaderTitle(m_cursor.getString(m_cursor.getColumnIndex("title")));
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
m_articleId = savedInstanceState.getInt("articleId");
}
View view = inflater.inflate(R.layout.article_fragment, container, false);
// TODO change to interface?
2011-12-07 08:50:04 +00:00
Activity activity = getActivity();
/* if (activity != null) {
int orientation = activity.getWindowManager().getDefaultDisplay().getOrientation();
2011-12-07 08:50:04 +00:00
if (!m_offlineServices.isSmallScreen()) {
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-12-07 08:50:04 +00:00
m_cursor = m_offlineServices.getReadableDb().query("articles", null, BaseColumns._ID + "=?",
new String[] { String.valueOf(m_articleId) }, null, null, null);
m_cursor.moveToFirst();
if (m_cursor.isFirst()) {
TextView title = (TextView)view.findViewById(R.id.title);
if (title != null) {
String titleStr;
if (m_cursor.getString(m_cursor.getColumnIndex("title")).length() > 200)
titleStr = m_cursor.getString(m_cursor.getColumnIndex("title")).substring(0, 200) + "...";
else
titleStr = m_cursor.getString(m_cursor.getColumnIndex("title"));
title.setMovementMethod(LinkMovementMethod.getInstance());
title.setText(Html.fromHtml("<a href=\""+m_cursor.getString(m_cursor.getColumnIndex("link")).trim().replace("\"", "\\\"")+"\">" + titleStr + "</a>"));
2012-03-12 08:51:57 +00:00
registerForContextMenu(title);
}
WebView web = (WebView)view.findViewById(R.id.content);
if (web != null) {
String content;
String cssOverride = "";
WebSettings ws = web.getSettings();
ws.setSupportZoom(true);
ws.setBuiltInZoomControls(true);
TypedValue tv = new TypedValue();
getActivity().getTheme().resolveAttribute(R.attr.linkColor, tv, true);
// prevent flicker in ics
if (android.os.Build.VERSION.SDK_INT >= 11) {
web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
if (m_prefs.getString("theme", "THEME_DARK").equals("THEME_DARK")) {
cssOverride = "body { background : transparent; color : #e0e0e0}";
//view.setBackgroundColor(android.R.color.black);
web.setBackgroundColor(getResources().getColor(android.R.color.transparent));
} else {
cssOverride = "";
}
String hexColor = String.format("#%06X", (0xFFFFFF & tv.data));
cssOverride += " a:link {color: "+hexColor+";} a:visited { color: "+hexColor+";}";
2011-12-07 11:59:30 +00:00
String articleContent = m_cursor.getString(m_cursor.getColumnIndex("content"));
2011-12-07 12:25:50 +00:00
Document doc = Jsoup.parse(articleContent);
2011-12-07 11:59:30 +00:00
2011-12-07 12:25:50 +00:00
if (doc != null) {
if (m_prefs.getBoolean("offline_image_cache_enabled", false)) {
2011-12-07 11:59:30 +00:00
Elements images = doc.select("img");
for (Element img : images) {
String url = img.attr("src");
if (ImageCacheService.isUrlCached(url)) {
img.attr("src", "file://" + ImageCacheService.getCacheFileName(url));
}
}
}
2011-12-07 12:25:50 +00:00
// thanks webview for crashing on <video> tag
Elements videos = doc.select("video");
for (Element video : videos)
video.remove();
articleContent = doc.toString();
2011-12-07 11:59:30 +00:00
}
view.findViewById(R.id.attachments_holder).setVisibility(View.GONE);
String align = m_prefs.getBoolean("justify_article_text", true) ? "text-align : justified" : "";
2012-02-25 11:05:37 +00:00
switch (Integer.parseInt(m_prefs.getString("font_size", "0"))) {
case 0:
cssOverride += "body { "+align+"; font-size : 14px; } ";
2012-02-25 11:05:37 +00:00
break;
case 1:
cssOverride += "body { "+align+"; font-size : 18px; } ";
2012-02-25 11:05:37 +00:00
break;
case 2:
cssOverride += "body { "+align+"; font-size : 21px; } ";
2012-02-25 11:05:37 +00:00
break;
}
content =
"<html>" +
"<head>" +
"<meta content=\"text/html; charset=utf-8\" http-equiv=\"content-type\">" +
//"<meta name=\"viewport\" content=\"target-densitydpi=device-dpi\" />" +
"<style type=\"text/css\">" +
"body { padding : 0px; margin : 0px; }" +
cssOverride +
"img { max-width : 98%; height : auto; }" +
"</style>" +
"</head>" +
2011-12-07 11:59:30 +00:00
"<body>" + articleContent + "</body></html>";
2011-12-07 12:13:58 +00:00
try {
web.loadDataWithBaseURL(null, content, "text/html", "utf-8", null);
} catch (RuntimeException e) {
e.printStackTrace();
}
}
TextView dv = (TextView)view.findViewById(R.id.date);
if (dv != null) {
Date d = new Date(m_cursor.getInt(m_cursor.getColumnIndex("updated")) * 1000L);
SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy, HH:mm");
dv.setText(df.format(d));
}
TextView tagv = (TextView)view.findViewById(R.id.tags);
if (tagv != null) {
String tagsStr = m_cursor.getString(m_cursor.getColumnIndex("tags"));
tagv.setText(tagsStr);
}
}
return view;
}
@Override
public void onDestroy() {
super.onDestroy();
m_cursor.close();
}
@Override
public void onSaveInstanceState (Bundle out) {
super.onSaveInstanceState(out);
out.putInt("articleId", m_articleId);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
2011-12-07 08:50:04 +00:00
m_offlineServices = (OfflineServices)activity;
}
}