package org.fox.ttrss.offline;
import java.text.SimpleDateFormat;
import java.util.Date;
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;
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;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.TypedValue;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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;
private OfflineServices m_offlineServices;
public OfflineArticleFragment() {
super();
}
public OfflineArticleFragment(int articleId) {
super();
m_articleId = articleId;
}
@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?
Activity activity = getActivity();
/* if (activity != null) {
int orientation = activity.getWindowManager().getDefaultDisplay().getOrientation();
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);
} */
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("" + titleStr + ""));
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+";}";
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(url)) {
img.attr("src", "file://" + ImageCacheService.getCacheFileName(url));
}
}
}
// thanks webview for crashing on