Fix crash when no browser is present and use an ACTION_CHOOSER intent in the app update notification (#5429)

Fix crash when no browser is present and use an ACTION_CHOOSER intent for app update notification
Show a Toast when no app is present on user's device to open a content in an app and in a browser and use an ACTION_CHOOSER intent with the ACTION_VIEW intent put as an extra intent in the update notification.
This commit is contained in:
TiA4f8R 2021-01-18 21:45:36 +01:00 committed by GitHub
parent d1fc9c5880
commit 9a65f02d5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 30 deletions

View File

@ -132,7 +132,13 @@ public final class CheckForNewAppVersion {
if (BuildConfig.VERSION_CODE < versionCode) { if (BuildConfig.VERSION_CODE < versionCode) {
// A pending intent to open the apk location url in the browser. // 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 final PendingIntent pendingIntent
= PendingIntent.getActivity(application, 0, intent, 0); = PendingIntent.getActivity(application, 0, intent, 0);

View File

@ -6,6 +6,7 @@ import android.content.ClipboardManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri; import android.net.Uri;
import android.widget.Toast; import android.widget.Toast;
@ -64,13 +65,18 @@ public final class ShareUtils {
// No browser set as default (doesn't work on some devices) // No browser set as default (doesn't work on some devices)
openInDefaultApp(context, intent); openInDefaultApp(context, intent);
} else { } else {
try { if (defaultPackageName.isEmpty()) {
intent.setPackage(defaultPackageName); // No app installed to open a web url
context.startActivity(intent); Toast.makeText(context, R.string.no_app_to_open_intent, Toast.LENGTH_LONG).show();
} catch (final ActivityNotFoundException e) { } else {
// Not a browser but an app chooser because of OEMs changes try {
intent.setPackage(null); intent.setPackage(defaultPackageName);
openInDefaultApp(context, intent); 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 * @param intent the intent to open
*/ */
public static void openIntentInApp(final Context context, final Intent intent) { 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) // No app set as default (doesn't work on some devices)
openInDefaultApp(context, intent); openInDefaultApp(context, intent);
} else { } else {
try { if (defaultPackageName.isEmpty()) {
intent.setPackage(defaultAppPackageName); // No app installed to open the intent
context.startActivity(intent); Toast.makeText(context, R.string.no_app_to_open_intent, Toast.LENGTH_LONG).show();
} catch (final ActivityNotFoundException e) { } else {
// Not an app to open the intent but an app chooser because of OEMs changes try {
intent.setPackage(null); intent.setPackage(defaultPackageName);
openInDefaultApp(context, intent); 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. * Get the default app package name.
* <p> * <p>
* 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).
* <p> * <p>
* 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 context the context to use
* @param intent the intent to get default app * @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) { private static String getDefaultAppPackageName(final Context context, final Intent intent) {
return context.getPackageManager().resolveActivity(intent, final ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent,
PackageManager.MATCH_DEFAULT_ONLY).activityInfo.packageName; PackageManager.MATCH_DEFAULT_ONLY);
if (resolveInfo == null) {
return "";
} else {
return resolveInfo.activityInfo.packageName;
}
} }
/** /**
* Get the default browser package name. * Get the default browser package name.
* <p> * <p>
* If no browser is set as default, it will return "android" * If no browser is set as default, it will return "android" (not on some devices because some
* Note: it doesn't return "android" on some devices because some OEMs changed the app chooser. * OEMs changed the app chooser).
* * <p>
* If no browser is installed on user's device, it will return an empty string.
* @param context the context to use * @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) { private static String getDefaultBrowserPackageName(final Context context) {
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://")) final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"))
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return context.getPackageManager().resolveActivity(intent, final ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent,
PackageManager.MATCH_DEFAULT_ONLY).activityInfo.packageName; PackageManager.MATCH_DEFAULT_ONLY);
if (resolveInfo == null) {
return "";
} else {
return resolveInfo.activityInfo.packageName;
}
} }
/** /**

View File

@ -658,4 +658,5 @@
<string name="hash_channel_name">Notification de hachage vidéo</string> <string name="hash_channel_name">Notification de hachage vidéo</string>
<string name="show_meta_info_summary">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.</string> <string name="show_meta_info_summary">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.</string>
<string name="show_meta_info_title">Afficher les méta-infos</string> <string name="show_meta_info_title">Afficher les méta-infos</string>
</resources> <string name="no_app_to_open_intent">Aucune application sur votre appareil ne peut ouvrir ceci</string>
</resources>

View File

@ -699,4 +699,5 @@
<string name="show_thumbnail_summary">Use thumbnail for both lock screen background and notifications</string> <string name="show_thumbnail_summary">Use thumbnail for both lock screen background and notifications</string>
<string name="recent">Recent</string> <string name="recent">Recent</string>
<string name="chapters">Chapters</string> <string name="chapters">Chapters</string>
</resources> <string name="no_app_to_open_intent">No app on your device can open this</string>
</resources>