diff --git a/app/src/main/java/org/schabi/newpipe/CheckForNewAppVersion.java b/app/src/main/java/org/schabi/newpipe/CheckForNewAppVersion.java index 0fecc3f96..f8497ea27 100644 --- a/app/src/main/java/org/schabi/newpipe/CheckForNewAppVersion.java +++ b/app/src/main/java/org/schabi/newpipe/CheckForNewAppVersion.java @@ -132,7 +132,13 @@ public final class CheckForNewAppVersion { if (BuildConfig.VERSION_CODE < versionCode) { // A pending intent to open the apk location url in the browser. - final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(apkLocationUrl)); + final Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(apkLocationUrl)); + + final Intent intent = new Intent(Intent.ACTION_CHOOSER); + intent.putExtra(Intent.EXTRA_INTENT, viewIntent); + intent.putExtra(Intent.EXTRA_TITLE, R.string.open_with); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + final PendingIntent pendingIntent = PendingIntent.getActivity(application, 0, intent, 0); 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 ffb965ae7..32157e43e 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ShareUtils.java +++ b/app/src/main/java/org/schabi/newpipe/util/ShareUtils.java @@ -6,6 +6,7 @@ import android.content.ClipboardManager; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; import android.net.Uri; import android.widget.Toast; @@ -64,13 +65,18 @@ public final class ShareUtils { // No browser set as default (doesn't work on some devices) openInDefaultApp(context, intent); } else { - try { - intent.setPackage(defaultPackageName); - context.startActivity(intent); - } catch (final ActivityNotFoundException e) { - // Not a browser but an app chooser because of OEMs changes - intent.setPackage(null); - openInDefaultApp(context, intent); + if (defaultPackageName.isEmpty()) { + // No app installed to open a web url + Toast.makeText(context, R.string.no_app_to_open_intent, Toast.LENGTH_LONG).show(); + } else { + try { + intent.setPackage(defaultPackageName); + context.startActivity(intent); + } catch (final ActivityNotFoundException e) { + // Not a browser but an app chooser because of OEMs changes + intent.setPackage(null); + openInDefaultApp(context, intent); + } } } } @@ -104,19 +110,24 @@ public final class ShareUtils { * @param intent the intent to open */ public static void openIntentInApp(final Context context, final Intent intent) { - final String defaultAppPackageName = getDefaultAppPackageName(context, intent); + final String defaultPackageName = getDefaultAppPackageName(context, intent); - if (defaultAppPackageName.equals("android")) { + if (defaultPackageName.equals("android")) { // No app set as default (doesn't work on some devices) openInDefaultApp(context, intent); } else { - try { - intent.setPackage(defaultAppPackageName); - context.startActivity(intent); - } catch (final ActivityNotFoundException e) { - // Not an app to open the intent but an app chooser because of OEMs changes - intent.setPackage(null); - openInDefaultApp(context, intent); + if (defaultPackageName.isEmpty()) { + // No app installed to open the intent + Toast.makeText(context, R.string.no_app_to_open_intent, Toast.LENGTH_LONG).show(); + } else { + try { + intent.setPackage(defaultPackageName); + context.startActivity(intent); + } catch (final ActivityNotFoundException e) { + // Not an app to open the intent but an app chooser because of OEMs changes + intent.setPackage(null); + openInDefaultApp(context, intent); + } } } } @@ -140,33 +151,47 @@ public final class ShareUtils { /** * Get the default app package name. *

- * If no app is set as default, it will return "android". + * If no app is set as default, it will return "android" (not on some devices because some + * OEMs changed the app chooser). *

- * Note: it doesn't return "android" on some devices because some OEMs changed the app chooser. + * If no app is installed on user's device to handle the intent, it will return an empty string. * * @param context the context to use * @param intent the intent to get default app - * @return the package name of the default app, or the app chooser if there's no default + * @return the package name of the default app to open the intent, an empty string if there's no + * app installed to handle it or the app chooser if there's no default */ private static String getDefaultAppPackageName(final Context context, final Intent intent) { - return context.getPackageManager().resolveActivity(intent, - PackageManager.MATCH_DEFAULT_ONLY).activityInfo.packageName; + final ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent, + PackageManager.MATCH_DEFAULT_ONLY); + if (resolveInfo == null) { + return ""; + } else { + return resolveInfo.activityInfo.packageName; + } } /** * Get the default browser package name. *

- * If no browser is set as default, it will return "android" - * Note: it doesn't return "android" on some devices because some OEMs changed the app chooser. - * + * If no browser is set as default, it will return "android" (not on some devices because some + * OEMs changed the app chooser). + *

+ * If no browser is installed on user's device, it will return an empty string. * @param context the context to use - * @return the package name of the default browser, or "android" if there's no default + * @return the package name of the default browser, an empty string if there's no browser + * installed or the app chooser if there's no default */ private static String getDefaultBrowserPackageName(final Context context) { final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://")) .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - return context.getPackageManager().resolveActivity(intent, - PackageManager.MATCH_DEFAULT_ONLY).activityInfo.packageName; + final ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent, + PackageManager.MATCH_DEFAULT_ONLY); + if (resolveInfo == null) { + return ""; + } else { + return resolveInfo.activityInfo.packageName; + } } /** diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 521228e7c..d8c53ce78 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -658,4 +658,5 @@ Notification de hachage vidéo Désactivez cette option pour masquer les zones de méta-informations contenant des informations supplémentaires sur le créateur du flux, le contenu du flux ou une demande de recherche. Afficher les méta-infos - \ No newline at end of file + Aucune application sur votre appareil ne peut ouvrir ceci + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 702dcafbf..ba3093618 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -699,4 +699,5 @@ Use thumbnail for both lock screen background and notifications Recent Chapters - \ No newline at end of file + No app on your device can open this +