From 9a938093e2b8a2ad2dfa01c391af89ab38589605 Mon Sep 17 00:00:00 2001 From: bopol Date: Tue, 28 Apr 2020 21:12:41 +0200 Subject: [PATCH 1/3] Open in browser button now really opens in browser --- .../org/schabi/newpipe/util/ShareUtils.java | 62 ++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/util/ShareUtils.java b/app/src/main/java/org/schabi/newpipe/util/ShareUtils.java index 8cefa08eb..1bb460520 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ShareUtils.java +++ b/app/src/main/java/org/schabi/newpipe/util/ShareUtils.java @@ -2,19 +2,77 @@ package org.schabi.newpipe.util; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; import android.net.Uri; import org.schabi.newpipe.R; public final class ShareUtils { - private ShareUtils() { } + private ShareUtils() { + } + /** + * Open the url with the system default browser. + *

+ * If no browser is set as default, fallbacks to + * {@link ShareUtils#openUrlBrowserChooser(Context, String)} + * + * @param context the context to use + * @param url the url to browse + */ public static void openUrlInBrowser(final Context context, final String url) { - Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); + final Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setData(Uri.parse(url)); + + final String defaultBrowserPackageName = getDefaultBrowserPackageName(context); + + if (defaultBrowserPackageName.equals("android")) { + // no browser set as default + openUrlBrowserChooser(context, url); + } else { + intent.setPackage(defaultBrowserPackageName); + context.startActivity(intent); + } + } + + /** + * Open the url with application chooser, including browser and apps able to open url. + *

+ * If any app (except browser, typically NewPipe) is set as default, + * it will nor open in browser, neither open the chooser, but just the default app. + * + * @param context the context to use + * @param url the url to browse + */ + private static void openUrlBrowserChooser(final Context context, final String url) { + final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); context.startActivity(Intent.createChooser( intent, context.getString(R.string.share_dialog_title))); } + /** + * Get the default browser package name. + *

+ * If no browser is set as default, it will return "android" + * + * @param context the context to use + * @return the package name of the default browser, or "android" if there's no default + */ + private static String getDefaultBrowserPackageName(final Context context) { + final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://")); + final ResolveInfo resolveInfo = context.getPackageManager().resolveActivity( + intent, PackageManager.MATCH_DEFAULT_ONLY); + return resolveInfo.activityInfo.packageName; + } + + /** + * Open the android share menu to share the current url. + * + * @param context the context to use + * @param subject the url subject, typically the title + * @param url the url to share + */ public static void shareUrl(final Context context, final String subject, final String url) { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); From 97437b8af3ac20580f8d75178ed320691393f796 Mon Sep 17 00:00:00 2001 From: bopol Date: Tue, 19 May 2020 21:51:39 +0200 Subject: [PATCH 2/3] apply @stypox suggestions --- .../java/org/schabi/newpipe/util/ShareUtils.java | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/util/ShareUtils.java b/app/src/main/java/org/schabi/newpipe/util/ShareUtils.java index 1bb460520..a306715e4 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ShareUtils.java +++ b/app/src/main/java/org/schabi/newpipe/util/ShareUtils.java @@ -16,36 +16,33 @@ public final class ShareUtils { * Open the url with the system default browser. *

* If no browser is set as default, fallbacks to - * {@link ShareUtils#openUrlBrowserChooser(Context, String)} + * {@link ShareUtils#openInDefaultApp(Context, String)} * * @param context the context to use * @param url the url to browse */ public static void openUrlInBrowser(final Context context, final String url) { - final Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setData(Uri.parse(url)); - final String defaultBrowserPackageName = getDefaultBrowserPackageName(context); if (defaultBrowserPackageName.equals("android")) { // no browser set as default - openUrlBrowserChooser(context, url); + openInDefaultApp(context, url); } else { + final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); intent.setPackage(defaultBrowserPackageName); context.startActivity(intent); } } /** - * Open the url with application chooser, including browser and apps able to open url. + * Open the url in the default app set to open this type of link *

- * If any app (except browser, typically NewPipe) is set as default, - * it will nor open in browser, neither open the chooser, but just the default app. + * If no app is set as default, it will open a chooser * * @param context the context to use * @param url the url to browse */ - private static void openUrlBrowserChooser(final Context context, final String url) { + private static void openInDefaultApp(final Context context, final String url) { final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); context.startActivity(Intent.createChooser( intent, context.getString(R.string.share_dialog_title))); From 8e13161f64c06f3d47a72ae2c075538998f5887c Mon Sep 17 00:00:00 2001 From: bopol Date: Tue, 19 May 2020 21:57:46 +0200 Subject: [PATCH 3/3] fix checkstyle --- app/src/main/java/org/schabi/newpipe/util/ShareUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/org/schabi/newpipe/util/ShareUtils.java b/app/src/main/java/org/schabi/newpipe/util/ShareUtils.java index a306715e4..0bf731a98 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ShareUtils.java +++ b/app/src/main/java/org/schabi/newpipe/util/ShareUtils.java @@ -35,7 +35,7 @@ public final class ShareUtils { } /** - * Open the url in the default app set to open this type of link + * Open the url in the default app set to open this type of link. *

* If no app is set as default, it will open a chooser *