From d889d5e44c4922a691181f343a1f2ef3c7520ac8 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Mon, 10 Jun 2013 15:08:10 +0400 Subject: [PATCH] add image context menu in offline mode (closes #711) --- .../fox/ttrss/offline/OfflineActivity.java | 112 ++++++++++++++++-- .../ttrss/offline/OfflineArticleFragment.java | 34 +++++- 2 files changed, 133 insertions(+), 13 deletions(-) diff --git a/src/org/fox/ttrss/offline/OfflineActivity.java b/src/org/fox/ttrss/offline/OfflineActivity.java index a083d7b7..bda9c007 100644 --- a/src/org/fox/ttrss/offline/OfflineActivity.java +++ b/src/org/fox/ttrss/offline/OfflineActivity.java @@ -1,14 +1,12 @@ package org.fox.ttrss.offline; +import org.fox.ttrss.ArticlePager; import org.fox.ttrss.CommonActivity; -import org.fox.ttrss.OnlineActivity; import org.fox.ttrss.PreferencesActivity; import org.fox.ttrss.R; - -import com.actionbarsherlock.view.ActionMode; -import com.actionbarsherlock.view.Menu; -import com.actionbarsherlock.view.MenuInflater; -import com.actionbarsherlock.view.MenuItem; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.select.Elements; import android.annotation.SuppressLint; import android.app.AlertDialog; @@ -20,16 +18,22 @@ import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; import android.database.sqlite.SQLiteStatement; +import android.net.Uri; import android.os.Bundle; import android.preference.PreferenceManager; import android.provider.BaseColumns; import android.util.Log; import android.view.KeyEvent; - import android.view.View; import android.view.Window; import android.widget.EditText; import android.widget.SearchView; +import android.widget.TextView; + +import com.actionbarsherlock.view.ActionMode; +import com.actionbarsherlock.view.Menu; +import com.actionbarsherlock.view.MenuInflater; +import com.actionbarsherlock.view.MenuItem; public class OfflineActivity extends CommonActivity { private final String TAG = this.getClass().getSimpleName(); @@ -40,6 +44,8 @@ public class OfflineActivity extends CommonActivity { private ActionMode m_headlinesActionMode; private HeadlinesActionModeCallback m_headlinesActionModeCallback; + private String m_lastImageHitTestUrl; + @SuppressLint("NewApi") private class HeadlinesActionModeCallback implements ActionMode.Callback { @@ -70,6 +76,90 @@ public class OfflineActivity extends CommonActivity { } }; + @Override + public boolean onContextItemSelected(android.view.MenuItem item) { + /* AdapterContextMenuInfo info = (AdapterContextMenuInfo) item + .getMenuInfo(); */ + + final OfflineArticlePager ap = (OfflineArticlePager)getSupportFragmentManager().findFragmentByTag(FRAG_ARTICLE); + + switch (item.getItemId()) { + case R.id.article_img_open: + if (getLastContentImageHitTestUrl() != null) { + try { + Intent intent = new Intent(Intent.ACTION_VIEW, + Uri.parse(getLastContentImageHitTestUrl())); + startActivity(intent); + } catch (Exception e) { + e.printStackTrace(); + toast(R.string.error_other_error); + } + } + return true; + case R.id.article_img_share: + if (getLastContentImageHitTestUrl() != null) { + Intent intent = new Intent(Intent.ACTION_SEND); + + intent.setType("image/png"); + intent.putExtra(Intent.EXTRA_SUBJECT, getLastContentImageHitTestUrl()); + intent.putExtra(Intent.EXTRA_TEXT, getLastContentImageHitTestUrl()); + + startActivity(Intent.createChooser(intent, getLastContentImageHitTestUrl())); + } + return true; + case R.id.article_img_view_caption: + if (getLastContentImageHitTestUrl() != null) { + + String content = ""; + + Cursor article = getArticleById(ap.getSelectedArticleId()); + + if (article != null) { + content = article.getString(article.getColumnIndex("content")); + article.close(); + } + + // Android doesn't give us an easy way to access title tags; + // we'll use Jsoup on the body text to grab the title text + // from the first image tag with this url. This will show + // the wrong text if an image is used multiple times. + Document doc = Jsoup.parse(content); + Elements es = doc.getElementsByAttributeValue("src", getLastContentImageHitTestUrl()); + if (es.size() > 0){ + if (es.get(0).hasAttr("title")){ + Dialog dia = new Dialog(this); + if (es.get(0).hasAttr("alt")){ + dia.setTitle(es.get(0).attr("alt")); + } else { + dia.setTitle(es.get(0).attr("title")); + } + TextView titleText = new TextView(this); + + if (android.os.Build.VERSION.SDK_INT >= 16) { + titleText.setPaddingRelative(24, 24, 24, 24); + } else { + titleText.setPadding(24, 24, 24, 24); + } + + titleText.setTextSize(16); + titleText.setText(es.get(0).attr("title")); + dia.setContentView(titleText); + dia.show(); + } else { + toast(R.string.no_caption_to_display); + } + } else { + toast(R.string.no_caption_to_display); + } + } + return true; + default: + Log.d(TAG, "onContextItemSelected, unhandled id=" + item.getItemId()); + return super.onContextItemSelected(item); + } + + } + @Override public void onCreate(Bundle savedInstanceState) { m_prefs = PreferenceManager @@ -692,5 +782,11 @@ public class OfflineActivity extends CommonActivity { refresh(); } - + public void setLastContentImageHitTestUrl(String url) { + m_lastImageHitTestUrl = url; + } + + public String getLastContentImageHitTestUrl() { + return m_lastImageHitTestUrl; + } } diff --git a/src/org/fox/ttrss/offline/OfflineArticleFragment.java b/src/org/fox/ttrss/offline/OfflineArticleFragment.java index 8c0b3c60..7010bb34 100644 --- a/src/org/fox/ttrss/offline/OfflineArticleFragment.java +++ b/src/org/fox/ttrss/offline/OfflineArticleFragment.java @@ -38,6 +38,7 @@ import android.view.ViewGroup; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebSettings.LayoutAlgorithm; +import android.webkit.WebView.HitTestResult; import android.webkit.WebView; import android.widget.TextView; @@ -84,8 +85,30 @@ public class OfflineArticleFragment extends Fragment implements GestureDetector. 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"))); + //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); + } super.onCreateContextMenu(menu, v, menuInfo); @@ -153,6 +176,8 @@ public class OfflineArticleFragment extends Fragment implements GestureDetector. if (web != null) { + registerForContextMenu(web); + web.setWebChromeClient(new WebChromeClient() { @Override public void onProgressChanged(WebView view, int progress) { @@ -342,9 +367,8 @@ public class OfflineArticleFragment extends Fragment implements GestureDetector. } @Override - public void onLongPress(MotionEvent e) { - // TODO Auto-generated method stub - + public void onLongPress(MotionEvent e) { + m_activity.openContextMenu(getView()); } @Override