implement basic webview context menu for images

This commit is contained in:
Andrew Dolgov 2013-03-31 11:17:59 +04:00
parent 2a268f6c6a
commit cea68d6b54
5 changed files with 83 additions and 7 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.fox.ttrss"
android:versionCode="151"
android:versionCode="152"
android:versionName="1.4.3" >
<uses-sdk

View File

@ -0,0 +1,18 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/article_img_open"
android:showAsAction=""
android:title="@string/article_img_open"/>
<item
android:id="@+id/article_img_share"
android:showAsAction=""
android:title="@string/article_img_share"/>
<!-- <item
android:id="@+id/article_img_save"
android:showAsAction=""
android:title="Save image to file"/> -->
</menu>

View File

@ -186,4 +186,6 @@
<string name="headlines_starred">Starred</string>
<string name="headlines_published">Published</string>
<string name="headlines_unread">Unread</string>
<string name="article_img_open">Open image</string>
<string name="article_img_share">Share image</string>
</resources>

View File

@ -21,11 +21,13 @@ import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.text.Html;
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;
@ -34,7 +36,9 @@ import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebView.HitTestResult;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;
public class ArticleFragment extends Fragment implements GestureDetector.OnDoubleTapListener {
private final String TAG = this.getClass().getSimpleName();
@ -60,8 +64,25 @@ public class ArticleFragment extends Fragment implements GestureDetector.OnDoubl
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
getActivity().getMenuInflater().inflate(R.menu.article_link_context_menu, menu);
if (v.getId() == R.id.content) {
HitTestResult result = ((WebView)v).getHitTestResult();
if (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(m_article.title);
getActivity().getMenuInflater().inflate(R.menu.article_link_context_menu, menu);
}
} else {
menu.setHeaderTitle(m_article.title);
getActivity().getMenuInflater().inflate(R.menu.article_link_context_menu, menu);
}
super.onCreateContextMenu(menu, v, menuInfo);
@ -143,6 +164,8 @@ public class ArticleFragment extends Fragment implements GestureDetector.OnDoubl
WebView web = (WebView)view.findViewById(R.id.content);
if (web != null) {
registerForContextMenu(web);
web.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int progress) {
@ -319,7 +342,6 @@ public class ArticleFragment extends Fragment implements GestureDetector.OnDoubl
out.putParcelable("article", m_article);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
@ -350,8 +372,7 @@ public class ArticleFragment extends Fragment implements GestureDetector.OnDoubl
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
m_activity.openContextMenu(getView());
}
@Override

View File

@ -39,6 +39,8 @@ import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebView.HitTestResult;
import android.widget.EditText;
import android.widget.SearchView;
import android.widget.ShareActionProvider;
@ -61,6 +63,8 @@ public class OnlineActivity extends CommonActivity {
private ActionMode m_headlinesActionMode;
private HeadlinesActionModeCallback m_headlinesActionModeCallback;
private String m_lastImageHitTestUrl;
private BroadcastReceiver m_broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context content, Intent intent) {
@ -536,6 +540,29 @@ public class OnlineActivity extends CommonActivity {
final ArticlePager ap = (ArticlePager)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_link_share:
if (ap != null && ap.getSelectedArticle() != null) {
shareArticle(ap.getSelectedArticle());
@ -1570,4 +1597,12 @@ public class OnlineActivity extends CommonActivity {
public String getViewMode() {
return m_prefs.getString("view_mode", "adaptive");
}
public void setLastContentImageHitTestUrl(String url) {
m_lastImageHitTestUrl = url;
}
public String getLastContentImageHitTestUrl() {
return m_lastImageHitTestUrl;
}
}