Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,13 @@ private static void compareAppVersionAndShowNotification(@NonNull final Applicat

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);

Expand Down
79 changes: 52 additions & 27 deletions app/src/main/java/org/schabi/newpipe/util/ShareUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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;

Expand Down Expand Up @@ -64,13 +65,18 @@ public static void openUrlInBrowser(final Context context, final String url,
// 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);
}
}
}
}
Expand Down Expand Up @@ -104,19 +110,24 @@ public static void openUrlInBrowser(final Context context, final String url) {
* @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);
}
}
}
}
Expand All @@ -140,33 +151,47 @@ private static void openInDefaultApp(final Context context, final Intent viewInt
/**
* Get the default app package name.
* <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>
* 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 null.
AudricV marked this conversation as resolved.
Show resolved Hide resolved
*
* @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, an empty string if there's no app installed to
* handle the intent 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.
* <p>
* 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).
* <p>
* If no app is installed on user's device to handle the intent, it will return null.
* @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;
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -658,4 +658,5 @@
<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_title">Afficher les méta-infos</string>
<string name="no_app_to_open_intent">Aucune application installée sur votre appareil ne peut ouvrir ceci</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -699,4 +699,5 @@
<string name="show_thumbnail_summary">Use thumbnail for both lock screen background and notifications</string>
<string name="recent">Recent</string>
<string name="chapters">Chapters</string>
<string name="no_app_to_open_intent">No app installed on your device can open this</string>
TobiGr marked this conversation as resolved.
Show resolved Hide resolved
</resources>