package org.fox.ttrss.offline; import java.net.URI; import java.net.URL; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import org.fox.ttrss.CommonActivity; 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.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.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; public void initialize(int articleId) { 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"))); 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"); } View view = inflater.inflate(R.layout.article_fragment, container, false); 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); 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")); final String link = m_cursor.getString(m_cursor.getColumnIndex("link")); title.setText(titleStr); //title.setPaintFlags(title.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); title.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { 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); } 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) { 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); web.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); 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}"; } else if (m_prefs.getString("theme", "THEME_DARK").equals("THEME_DARK_GRAY")) { cssOverride = "body { background : transparent; color : #e0e0e0}"; } else { cssOverride = "body { background : transparent; }"; } web.setBackgroundColor(getResources().getColor(android.R.color.transparent)); String hexColor = String.format("#%06X", (0xFFFFFF & tv.data)); cssOverride += " a:link {color: "+hexColor+";} a:visited { color: "+hexColor+";}"; cssOverride += " table { width : 100%; }"; 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