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

437 lines
12 KiB
Java
Raw Normal View History

2013-04-21 13:01:26 +00:00
package org.fox.ttrss.offline;
import java.net.MalformedURLException;
2013-05-22 18:42:42 +00:00
import java.net.URI;
import java.net.URL;
2013-05-23 10:53:16 +00:00
import java.text.DateFormat;
2013-04-21 13:01:26 +00:00
import java.text.SimpleDateFormat;
import java.util.Date;
import org.fox.ttrss.R;
import org.fox.ttrss.util.ImageCacheService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Color;
2013-04-21 13:01:26 +00:00
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.BaseColumns;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.util.TypedValue;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebView.HitTestResult;
2013-04-21 13:01:26 +00:00
import android.widget.TextView;
public class OfflineArticleFragment extends Fragment implements GestureDetector.OnDoubleTapListener {
private final String TAG = this.getClass().getSimpleName();
private SharedPreferences m_prefs;
private int m_articleId;
private boolean m_isCat = false; // FIXME use
private Cursor m_cursor;
private OfflineActivity m_activity;
private GestureDetector m_detector;
2013-04-26 09:16:53 +00:00
public void initialize(int articleId) {
2013-04-21 13:01:26 +00:00
m_articleId = articleId;
}
@Override
public boolean onContextItemSelected(MenuItem item) {
/* AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo(); */
switch (item.getItemId()) {
case R.id.article_link_share:
m_activity.shareArticle(m_articleId);
return true;
case R.id.article_link_copy:
if (true) {
Cursor article = m_activity.getArticleById(m_articleId);
if (article != null) {
m_activity.copyToClipboard(article.getString(article.getColumnIndex("link")));
article.close();
}
}
return true;
default:
Log.d(TAG, "onContextItemSelected, unhandled id=" + item.getItemId());
return super.onContextItemSelected(item);
}
}
@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")));
String title = m_cursor.getString(m_cursor.getColumnIndex("title"));
if (v.getId() == R.id.content) {
HitTestResult result = ((WebView)v).getHitTestResult();
if (result != null && (result.getType() == HitTestResult.IMAGE_TYPE || result.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE)) {
menu.setHeaderTitle(result.getExtra());
getActivity().getMenuInflater().inflate(R.menu.article_content_img_context_menu, menu);
/* FIXME I have no idea how to do this correctly ;( */
m_activity.setLastContentImageHitTestUrl(result.getExtra());
} else {
menu.setHeaderTitle(title);
getActivity().getMenuInflater().inflate(R.menu.article_link_context_menu, menu);
}
} else {
menu.setHeaderTitle(title);
getActivity().getMenuInflater().inflate(R.menu.article_link_context_menu, menu);
}
2013-04-21 13:01:26 +00:00
super.onCreateContextMenu(menu, v, menuInfo);
}
@SuppressLint("NewApi")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
m_articleId = savedInstanceState.getInt("articleId");
}
boolean useTitleWebView = m_prefs.getBoolean("article_compat_view", false);
View view = inflater.inflate(useTitleWebView ? R.layout.article_fragment_compat : R.layout.article_fragment, container, false);
2013-04-21 13:01:26 +00:00
m_cursor = m_activity.getReadableDb().query("articles LEFT JOIN feeds ON (feed_id = feeds."+BaseColumns._ID+")",
new String[] { "articles.*", "feeds.title AS feed_title" }, "articles." + 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);
final String link = m_cursor.getString(m_cursor.getColumnIndex("link"));
2013-04-21 13:01:26 +00:00
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.setText(titleStr);
//title.setPaintFlags(title.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
2013-05-22 18:42:42 +00:00
URL url = new URL(link.trim());
String uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(),
url.getPort(), url.getPath(), url.getQuery(), url.getRef()).toString();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
2013-04-21 13:01:26 +00:00
} catch (Exception e) {
e.printStackTrace();
m_activity.toast(R.string.error_other_error);
}
}
});
registerForContextMenu(title);
}
TextView comments = (TextView)view.findViewById(R.id.comments);
if (comments != null) {
comments.setVisibility(View.GONE);
}
WebView web = (WebView)view.findViewById(R.id.content);
if (web != null) {
registerForContextMenu(web);
2013-04-21 13:01:26 +00:00
web.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int progress) {
m_activity.setProgress(Math.round(((float)progress / 100f) * 10000));
if (progress == 100) {
m_activity.setProgressBarVisibility(false);
}
}
});
web.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return m_detector.onTouchEvent(event);
}
});
String content;
String cssOverride = "";
WebSettings ws = web.getSettings();
ws.setSupportZoom(true);
ws.setBuiltInZoomControls(true);
if (!m_activity.isCompatMode())
ws.setDisplayZoomControls(false);
2013-04-21 13:01:26 +00:00
web.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
TypedValue tv = new TypedValue();
getActivity().getTheme().resolveAttribute(R.attr.linkColor, tv, true);
// prevent flicker in ics
if (!m_prefs.getBoolean("webview_hardware_accel", true) || useTitleWebView) {
if (android.os.Build.VERSION.SDK_INT >= 11) {
web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
2013-04-21 13:01:26 +00:00
}
String theme = m_prefs.getString("theme", "THEME_DARK");
2013-04-21 13:01:26 +00:00
if ("THEME_DARK".equals(theme) || "THEME_SYSTEM".equals(theme)) {
2013-04-21 13:01:26 +00:00
cssOverride = "body { background : transparent; color : #e0e0e0}";
} else if ("THEME_DARK_GRAY".equals(theme)) {
2013-04-21 13:01:26 +00:00
cssOverride = "body { background : transparent; color : #e0e0e0}";
} else {
cssOverride = "body { background : transparent; }";
}
if (useTitleWebView) {
web.setBackgroundColor(Color.TRANSPARENT);
} else {
// seriously?
web.setBackgroundColor(Color.argb(1, 0, 0, 0));
}
2013-04-21 13:01:26 +00:00
String hexColor = String.format("#%06X", (0xFFFFFF & tv.data));
cssOverride += " a:link {color: "+hexColor+";} a:visited { color: "+hexColor+";}";
2013-05-15 09:02:56 +00:00
cssOverride += " table { width : 100%; }";
2013-04-21 13:01:26 +00:00
String articleContent = m_cursor.getString(m_cursor.getColumnIndex("content"));
Document doc = Jsoup.parse(articleContent);
if (doc != null) {
if (m_prefs.getBoolean("offline_image_cache_enabled", false)) {
Elements images = doc.select("img");
for (Element img : images) {
String url = img.attr("src");
if (ImageCacheService.isUrlCached(m_activity, url)) {
img.attr("src", "file://" + ImageCacheService.getCacheFileName(m_activity, url));
}
}
}
// thanks webview for crashing on <video> tag
Elements videos = doc.select("video");
for (Element video : videos)
video.remove();
articleContent = doc.toString();
}
String align = m_prefs.getBoolean("justify_article_text", true) ? "text-align : justified" : "";
switch (Integer.parseInt(m_prefs.getString("font_size", "0"))) {
case 0:
cssOverride += "body { "+align+"; font-size : 14px; } ";
break;
case 1:
cssOverride += "body { "+align+"; font-size : 18px; } ";
break;
case 2:
cssOverride += "body { "+align+"; font-size : 21px; } ";
break;
}
content =
"<html>" +
"<head>" +
"<meta content=\"text/html; charset=utf-8\" http-equiv=\"content-type\">" +
"<style type=\"text/css\">" +
"body { padding : 0px; margin : 0px; }" +
cssOverride +
"</style>" +
"</head>" +
"<body>" + articleContent;
if (useTitleWebView) {
content += "<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>";
}
content += "</body></html>";
2013-04-21 13:01:26 +00:00
try {
String baseUrl = null;
try {
URL url = new URL(link);
baseUrl = url.getProtocol() + "://" + url.getHost();
} catch (MalformedURLException e) {
//
}
web.loadDataWithBaseURL(baseUrl, content, "text/html", "utf-8", null);
2013-04-21 13:01:26 +00:00
} 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);
2013-05-23 10:53:16 +00:00
DateFormat df = new SimpleDateFormat("MMM dd, HH:mm");
2013-04-21 13:01:26 +00:00
dv.setText(df.format(d));
}
TextView tagv = (TextView)view.findViewById(R.id.tags);
if (tagv != null) {
int feedTitleIndex = m_cursor.getColumnIndex("feed_title");
if (feedTitleIndex != -1 && m_isCat) {
tagv.setText(m_cursor.getString(feedTitleIndex));
} else {
String tagsStr = m_cursor.getString(m_cursor.getColumnIndex("tags"));
tagv.setText(tagsStr);
}
}
TextView author = (TextView)view.findViewById(R.id.author);
if (author != null) {
2013-04-24 14:07:51 +00:00
int authorIndex = m_cursor.getColumnIndex("author");
2013-04-25 15:54:52 +00:00
if (authorIndex >= 0)
2013-04-24 14:07:51 +00:00
author.setText(m_cursor.getString(authorIndex));
else
author.setVisibility(View.GONE);
2013-04-21 13:01:26 +00:00
}
}
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());
m_activity = (OfflineActivity) activity;
m_detector = new GestureDetector(m_activity, new GestureDetector.OnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLongPress(MotionEvent e) {
m_activity.openContextMenu(getView());
2013-04-21 13:01:26 +00:00
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
});
m_detector.setOnDoubleTapListener(this);
}
@Override
public boolean onDoubleTap(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
2013-04-21 13:01:26 +00:00
return false;
}
}