2012-06-19 10:18:00 +00:00
|
|
|
package org.fox.ttrss.offline;
|
2011-12-05 18:32:45 +00:00
|
|
|
|
|
|
|
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;
|
2011-12-05 18:32:45 +00:00
|
|
|
|
|
|
|
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;
|
2011-12-05 18:32:45 +00:00
|
|
|
import android.text.Html;
|
|
|
|
import android.text.method.LinkMovementMethod;
|
2012-02-01 06:57:07 +00:00
|
|
|
import android.util.TypedValue;
|
2012-03-12 08:51:57 +00:00
|
|
|
import android.view.ContextMenu;
|
2011-12-05 18:32:45 +00:00
|
|
|
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;
|
2011-12-19 11:08:40 +00:00
|
|
|
import android.webkit.WebSettings;
|
2011-12-05 18:32:45 +00:00
|
|
|
import android.webkit.WebView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2011-12-09 20:49:55 +00:00
|
|
|
public class OfflineArticleFragment extends Fragment {
|
2011-12-05 18:32:45 +00:00
|
|
|
@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;
|
2011-12-05 18:32:45 +00:00
|
|
|
|
2011-12-10 07:22:27 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-12-05 18:32:45 +00:00
|
|
|
@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);
|
|
|
|
|
2011-12-09 20:49:55 +00:00
|
|
|
|
2011-12-05 18:32:45 +00:00
|
|
|
// TODO change to interface?
|
2011-12-07 08:50:04 +00:00
|
|
|
Activity activity = getActivity();
|
2011-12-05 18:32:45 +00:00
|
|
|
|
2012-06-21 20:35:53 +00:00
|
|
|
/* if (activity != null) {
|
2011-12-05 18:32:45 +00:00
|
|
|
int orientation = activity.getWindowManager().getDefaultDisplay().getOrientation();
|
|
|
|
|
2011-12-07 08:50:04 +00:00
|
|
|
if (!m_offlineServices.isSmallScreen()) {
|
2011-12-05 18:32:45 +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);
|
2012-06-21 20:35:53 +00:00
|
|
|
} */
|
2011-12-05 18:32:45 +00:00
|
|
|
|
2011-12-07 08:50:04 +00:00
|
|
|
m_cursor = m_offlineServices.getReadableDb().query("articles", null, BaseColumns._ID + "=?",
|
2011-12-05 18:32:45 +00:00
|
|
|
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());
|
2012-01-26 10:06:06 +00:00
|
|
|
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);
|
2011-12-05 18:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WebView web = (WebView)view.findViewById(R.id.content);
|
|
|
|
|
|
|
|
if (web != null) {
|
|
|
|
|
|
|
|
String content;
|
|
|
|
String cssOverride = "";
|
|
|
|
|
2011-12-19 11:08:40 +00:00
|
|
|
WebSettings ws = web.getSettings();
|
|
|
|
ws.setSupportZoom(true);
|
|
|
|
ws.setBuiltInZoomControls(true);
|
2011-12-05 18:32:45 +00:00
|
|
|
|
2012-02-01 11:25:35 +00:00
|
|
|
TypedValue tv = new TypedValue();
|
|
|
|
getActivity().getTheme().resolveAttribute(R.attr.linkColor, tv, true);
|
2012-02-03 06:05:42 +00:00
|
|
|
|
|
|
|
// prevent flicker in ics
|
2012-02-03 06:22:15 +00:00
|
|
|
if (android.os.Build.VERSION.SDK_INT >= 11) {
|
|
|
|
web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
|
|
|
|
}
|
2012-02-01 06:57:07 +00:00
|
|
|
|
2012-02-01 11:25:35 +00:00
|
|
|
if (m_prefs.getString("theme", "THEME_DARK").equals("THEME_DARK")) {
|
2012-02-03 06:05:42 +00:00
|
|
|
cssOverride = "body { background : transparent; color : #e0e0e0}";
|
|
|
|
//view.setBackgroundColor(android.R.color.black);
|
2012-05-02 05:54:45 +00:00
|
|
|
web.setBackgroundColor(getResources().getColor(android.R.color.transparent));
|
2011-12-05 18:32:45 +00:00
|
|
|
} else {
|
|
|
|
cssOverride = "";
|
|
|
|
}
|
2012-02-01 06:57:07 +00:00
|
|
|
|
2012-02-01 11:25:35 +00:00
|
|
|
String hexColor = String.format("#%06X", (0xFFFFFF & tv.data));
|
2012-02-01 06:57:07 +00:00
|
|
|
cssOverride += " a:link {color: "+hexColor+";} a:visited { color: "+hexColor+";}";
|
2011-12-05 18:32:45 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-03-12 11:57:57 +00:00
|
|
|
view.findViewById(R.id.attachments_holder).setVisibility(View.GONE);
|
|
|
|
|
2012-03-21 06:18:20 +00:00
|
|
|
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:
|
2012-03-21 06:18:20 +00:00
|
|
|
cssOverride += "body { "+align+"; font-size : 14px; } ";
|
2012-02-25 11:05:37 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2012-03-21 06:18:20 +00:00
|
|
|
cssOverride += "body { "+align+"; font-size : 18px; } ";
|
2012-02-25 11:05:37 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2012-03-21 06:18:20 +00:00
|
|
|
cssOverride += "body { "+align+"; font-size : 21px; } ";
|
2012-02-25 11:05:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-05 18:32:45 +00:00
|
|
|
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\">" +
|
2012-06-21 20:35:53 +00:00
|
|
|
"body { padding : 0px; margin : 0px; }" +
|
2011-12-05 18:32:45 +00:00
|
|
|
cssOverride +
|
|
|
|
"img { max-width : 98%; height : auto; }" +
|
|
|
|
"</style>" +
|
|
|
|
"</head>" +
|
2011-12-07 11:59:30 +00:00
|
|
|
"<body>" + articleContent + "</body></html>";
|
2011-12-05 18:32:45 +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();
|
|
|
|
}
|
2011-12-05 18:32:45 +00:00
|
|
|
|
2011-12-09 20:49:55 +00:00
|
|
|
|
2011-12-05 18:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2011-12-05 18:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|