From 7698f7b724e03f8915c40d60abeb35457640efd4 Mon Sep 17 00:00:00 2001 From: Dmitriy Bogdanov Date: Mon, 3 Oct 2016 13:05:56 +0400 Subject: [PATCH] Add an option to open context menu for post links --- .../dfa/diaspora_android/data/AppSettings.java | 4 ++++ .../diaspora_android/ui/ContextMenuWebView.java | 14 ++++++++++++++ .../diaspora_android/util/DiasporaUrlHelper.java | 15 +++++++++++++++ .../webview/CustomWebViewClient.java | 5 +++++ app/src/main/res/values/strings-preferences.xml | 4 ++++ app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/preferences.xml | 6 ++++++ 7 files changed, 49 insertions(+) diff --git a/app/src/main/java/com/github/dfa/diaspora_android/data/AppSettings.java b/app/src/main/java/com/github/dfa/diaspora_android/data/AppSettings.java index da5839c9..ef92580d 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/data/AppSettings.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/data/AppSettings.java @@ -260,6 +260,10 @@ public class AppSettings { return getBoolean(prefApp, R.string.pref_key__chrome_custom_tabs_enabled, true); } + public boolean isPostContextMenuEnabled() { + return getBoolean(prefApp, R.string.pref_key__post_link_context_menu_enabled, false); + } + public boolean isLoggingEnabled() { return getBoolean(prefApp, R.string.pref_key__logging_enabled, true); } diff --git a/app/src/main/java/com/github/dfa/diaspora_android/ui/ContextMenuWebView.java b/app/src/main/java/com/github/dfa/diaspora_android/ui/ContextMenuWebView.java index 3be4ce63..34486672 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/ui/ContextMenuWebView.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/ui/ContextMenuWebView.java @@ -37,9 +37,11 @@ import android.view.ContextMenu; import android.view.MenuItem; import android.widget.Toast; +import com.github.dfa.diaspora_android.App; import com.github.dfa.diaspora_android.R; import com.github.dfa.diaspora_android.activity.MainActivity; import com.github.dfa.diaspora_android.task.ImageDownloadTask; +import com.github.dfa.diaspora_android.util.DiasporaUrlHelper; import java.io.File; @@ -55,6 +57,7 @@ public class ContextMenuWebView extends NestedWebView { public static final int ID_COPY_LINK = 12; public static final int ID_SHARE_LINK = 13; public static final int ID_SHARE_IMAGE = 14; + public static final int ID_OPEN_HERE = 15; private final Context context; private Activity parentActivity; @@ -195,6 +198,13 @@ public class ContextMenuWebView extends NestedWebView { .getText(R.string.context_menu_share_link))); } break; + + //Open link in the webview + case ID_OPEN_HERE: + if (url != null) { + loadUrlNew(url); + } + break; } return true; } @@ -212,6 +222,10 @@ public class ContextMenuWebView extends NestedWebView { result.getType() == HitTestResult.SRC_ANCHOR_TYPE) { // Menu options for a hyperlink. menu.setHeaderTitle(result.getExtra()); + String url = result.getExtra(); + if (url != null && new DiasporaUrlHelper(((App)parentActivity.getApplication()).getSettings()).isPostUrl(url)) { + menu.add(0, ID_OPEN_HERE, 0, context.getString(R.string.context_menu_open_here)).setOnMenuItemClickListener(handler); + } menu.add(0, ID_COPY_LINK, 0, context.getString(R.string.context_menu_copy_link)).setOnMenuItemClickListener(handler); menu.add(0, ID_SHARE_LINK, 0, context.getString(R.string.context_menu_share_link)).setOnMenuItemClickListener(handler); } diff --git a/app/src/main/java/com/github/dfa/diaspora_android/util/DiasporaUrlHelper.java b/app/src/main/java/com/github/dfa/diaspora_android/util/DiasporaUrlHelper.java index b3c99805..52c08817 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/util/DiasporaUrlHelper.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/util/DiasporaUrlHelper.java @@ -23,6 +23,8 @@ import com.github.dfa.diaspora_android.R; import com.github.dfa.diaspora_android.data.AppSettings; import com.github.dfa.diaspora_android.data.PodAspect; +import java.util.regex.Pattern; + /** * Helper class that provides easy access to specific urls related to diaspora * Created by vanitasvitae on 10.08.16. @@ -51,6 +53,8 @@ public class DiasporaUrlHelper { public static final String SUBURL_STATISTICS = "/statistics"; public static final String URL_BLANK = "about:blank"; + public static final Pattern NUMBERS_ONLY_PATTERN = Pattern.compile("^\\d+$"); + public DiasporaUrlHelper(AppSettings settings) { this.settings = settings; } @@ -237,4 +241,15 @@ public class DiasporaUrlHelper { } return app.getString(R.string.aspects); } + + public boolean isPostUrl(String url) { + if (url.startsWith(getPodUrl() + SUBURL_POSTS)) { + String path = url.substring((getPodUrl() + SUBURL_POSTS).length()); + if (NUMBERS_ONLY_PATTERN.matcher(path).matches()) { + return true; + } + } + + return false; + } } diff --git a/app/src/main/java/com/github/dfa/diaspora_android/webview/CustomWebViewClient.java b/app/src/main/java/com/github/dfa/diaspora_android/webview/CustomWebViewClient.java index 914abcbb..e94aa9c1 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/webview/CustomWebViewClient.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/webview/CustomWebViewClient.java @@ -27,6 +27,7 @@ import android.webkit.WebViewClient; import com.github.dfa.diaspora_android.App; import com.github.dfa.diaspora_android.activity.MainActivity; +import com.github.dfa.diaspora_android.util.DiasporaUrlHelper; public class CustomWebViewClient extends WebViewClient { private final App app; @@ -44,6 +45,10 @@ public class CustomWebViewClient extends WebViewClient { i.putExtra(MainActivity.EXTRA_URL, url); LocalBroadcastManager.getInstance(app.getApplicationContext()).sendBroadcast(i); return true; + } else if (app.getSettings().isPostContextMenuEnabled() + && new DiasporaUrlHelper(app.getSettings()).isPostUrl(url)) { + view.showContextMenu(); + return true; } return false; } diff --git a/app/src/main/res/values/strings-preferences.xml b/app/src/main/res/values/strings-preferences.xml index fe4e728b..145fce0d 100644 --- a/app/src/main/res/values/strings-preferences.xml +++ b/app/src/main/res/values/strings-preferences.xml @@ -46,6 +46,7 @@ pref_catkey__category_more + pref_key__post_link_context_menu_enabled pref_key__logging_spam_enabled pref_key__logging_enabled @@ -118,6 +119,9 @@ Append a reference to this app ("shared by…") to shared texts + Context menu for post links + Open context menu for post links with short press + @string/about_activity__title_debug_info @string/fragment_debug__section_log diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index aa23ef1f..faa4c319 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -85,6 +85,7 @@ Share image Open in external browser… Copy link address to clipboard + Open link diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 4bbc7b4c..36facc03 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -152,6 +152,12 @@ android:key="@string/pref_catkey__category_more" android:title="@string/pref_cat__more"> + +