From 2c04ddbbf8d65153dba0baadd86d370097209ce6 Mon Sep 17 00:00:00 2001 From: sictiru Date: Tue, 4 Aug 2020 18:44:57 -0700 Subject: [PATCH] #1319 Built in app browser --- .../com/newsblur/activity/InAppBrowser.java | 14 ++---- .../fragment/ReadingItemFragment.java | 4 +- .../src/com/newsblur/util/UIUtils.java | 40 +++++++++++++++ .../com/newsblur/view/NewsblurWebview.java | 49 ++----------------- 4 files changed, 47 insertions(+), 60 deletions(-) diff --git a/clients/android/NewsBlur/src/com/newsblur/activity/InAppBrowser.java b/clients/android/NewsBlur/src/com/newsblur/activity/InAppBrowser.java index 0cad057ff..a5b28eea0 100644 --- a/clients/android/NewsBlur/src/com/newsblur/activity/InAppBrowser.java +++ b/clients/android/NewsBlur/src/com/newsblur/activity/InAppBrowser.java @@ -2,12 +2,13 @@ package com.newsblur.activity; import android.graphics.Bitmap; import android.os.Bundle; -import androidx.fragment.app.FragmentActivity; import android.view.View; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; +import androidx.fragment.app.FragmentActivity; + import com.newsblur.databinding.ActivityInAppBrowserBinding; import com.newsblur.util.PrefsUtils; @@ -53,13 +54,4 @@ public class InAppBrowser extends FragmentActivity { binding.webView.loadUrl(url); } - - @Override - public void onBackPressed() { - if (binding.webView.canGoBack()) { - binding.webView.goBack(); - } else { - finish(); - } - } -} +} \ No newline at end of file diff --git a/clients/android/NewsBlur/src/com/newsblur/fragment/ReadingItemFragment.java b/clients/android/NewsBlur/src/com/newsblur/fragment/ReadingItemFragment.java index f2e6a261e..8edf8a7a2 100644 --- a/clients/android/NewsBlur/src/com/newsblur/fragment/ReadingItemFragment.java +++ b/clients/android/NewsBlur/src/com/newsblur/fragment/ReadingItemFragment.java @@ -499,10 +499,8 @@ public class ReadingItemFragment extends NbFragment implements PopupMenu.OnMenuI binding.readingItemTitle.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - Intent i = new Intent(Intent.ACTION_VIEW); try { - i.setData(Uri.parse(story.permalink)); - startActivity(i); + UIUtils.handleUri(requireContext(), Uri.parse(story.permalink)); } catch (Throwable t) { // we don't actually know if the user will successfully be able to open whatever string // was in the permalink or if the Intent could throw errors diff --git a/clients/android/NewsBlur/src/com/newsblur/util/UIUtils.java b/clients/android/NewsBlur/src/com/newsblur/util/UIUtils.java index 271dfd688..0970e82a6 100644 --- a/clients/android/NewsBlur/src/com/newsblur/util/UIUtils.java +++ b/clients/android/NewsBlur/src/com/newsblur/util/UIUtils.java @@ -18,6 +18,7 @@ import android.graphics.Color; import android.graphics.Paint; import android.graphics.Shader; import android.graphics.drawable.Drawable; +import android.net.Uri; import android.os.Build; import android.os.Handler; import android.util.Log; @@ -541,4 +542,43 @@ public class UIUtils { return result; } + public static void handleUri(Context context, Uri uri) { + DefaultBrowser defaultBrowser = PrefsUtils.getDefaultBrowser(context); + if (defaultBrowser == DefaultBrowser.SYSTEM_DEFAULT) { + openSystemDefaultBrowser(context, uri); + } else if (defaultBrowser == DefaultBrowser.IN_APP_BROWSER) { + Intent intent = new Intent(context, InAppBrowser.class); + intent.putExtra(InAppBrowser.URI, uri); + context.startActivity(intent); + } else if (defaultBrowser == DefaultBrowser.CHROME) { + openExternalBrowserApp(context, uri, "com.android.chrome"); + } else if (defaultBrowser == DefaultBrowser.FIREFOX) { + openExternalBrowserApp(context, uri, "org.mozilla.firefox"); + } else if (defaultBrowser == DefaultBrowser.OPERA_MINI) { + openExternalBrowserApp(context, uri, "com.opera.mini.native"); + } + } + + public static void openSystemDefaultBrowser(Context context, Uri uri) { + try { + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setData(uri); + context.startActivity(intent); + } catch (Exception e) { + com.newsblur.util.Log.e(context.getClass().getName(), "device cannot open URLs"); + } + } + + public static void openExternalBrowserApp(Context context, Uri uri, String packageName) { + try { + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setData(uri); + intent.setPackage(packageName); + context.startActivity(intent); + } catch (Exception e) { + com.newsblur.util.Log.e(context.getClass().getName(), "apps not available to open URLs"); + // fallback to system default if apps cannot be opened + openSystemDefaultBrowser(context, uri); + } + } } diff --git a/clients/android/NewsBlur/src/com/newsblur/view/NewsblurWebview.java b/clients/android/NewsBlur/src/com/newsblur/view/NewsblurWebview.java index 51b661ca5..9093497f7 100644 --- a/clients/android/NewsBlur/src/com/newsblur/view/NewsblurWebview.java +++ b/clients/android/NewsBlur/src/com/newsblur/view/NewsblurWebview.java @@ -2,7 +2,6 @@ package com.newsblur.view; import android.annotation.TargetApi; import android.content.Context; -import android.content.Intent; import android.net.Uri; import android.os.Build; import android.util.AttributeSet; @@ -17,11 +16,9 @@ import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.FrameLayout; -import com.newsblur.activity.InAppBrowser; import com.newsblur.activity.Reading; import com.newsblur.fragment.ReadingItemFragment; -import com.newsblur.util.DefaultBrowser; -import com.newsblur.util.PrefsUtils; +import com.newsblur.util.UIUtils; public class NewsblurWebview extends WebView { @@ -93,14 +90,14 @@ public class NewsblurWebview extends WebView { class NewsblurWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { - handleUri(Uri.parse(url)); + UIUtils.handleUri(context, Uri.parse(url)); return true; } @TargetApi(Build.VERSION_CODES.N) @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { - handleUri(request.getUrl()); + UIUtils.handleUri(context, request.getUrl()); return true; } @@ -113,46 +110,6 @@ public class NewsblurWebview extends WebView { } } - private void handleUri(Uri uri) { - DefaultBrowser defaultBrowser = PrefsUtils.getDefaultBrowser(context); - if (defaultBrowser == DefaultBrowser.SYSTEM_DEFAULT) { - openSystemDefaultBrowser(uri); - } else if (defaultBrowser == DefaultBrowser.IN_APP_BROWSER) { - Intent intent = new Intent(context, InAppBrowser.class); - intent.putExtra(InAppBrowser.URI, uri); - context.startActivity(intent); - } else if (defaultBrowser == DefaultBrowser.CHROME) { - openExternalBrowserApp(uri, "com.android.chrome"); - } else if (defaultBrowser == DefaultBrowser.FIREFOX) { - openExternalBrowserApp(uri, "org.mozilla.firefox"); - } else if (defaultBrowser == DefaultBrowser.OPERA_MINI) { - openExternalBrowserApp(uri, "com.opera.mini.native"); - } - } - - private void openSystemDefaultBrowser(Uri uri) { - try { - Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setData(uri); - context.startActivity(intent); - } catch (Exception e) { - com.newsblur.util.Log.e(this.getClass().getName(), "device cannot open URLs"); - } - } - - private void openExternalBrowserApp(Uri uri, String packageName) { - try { - Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setData(uri); - intent.setPackage(packageName); - context.startActivity(intent); - } catch (Exception e) { - com.newsblur.util.Log.e(this.getClass().getName(), "apps not available to open URLs"); - // fallback to system default if apps cannot be opened - openSystemDefaultBrowser(uri); - } - } - // this WCC implements the bare minimum callbacks to get HTML5 fullscreen video working class NewsblurWebChromeClient extends WebChromeClient { public View customView;