Skip to content

Commit

Permalink
Fix toast shown when falling back to Google Play Store URL and the ac…
Browse files Browse the repository at this point in the history
…tion of Open with Kodi button in the player

Add a boolean param, showToast, in ShareUtils.openIntentInApp and only show toast "No app on your device can open this" if this boolean is true.
Fix the action of play with Kodi button by applying the fix provided in #5599 (adding the flag Intent.FLAG_ACTIVITY_NEW_TASK to the intent in NavigationHelper.playWithKore method).
Do also some cleanup in viewWithFileProvider and shareFile methods of MissionAdapter class.
  • Loading branch information
AudricV committed Jun 11, 2021
1 parent ae9349e commit 9e9d1a0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,10 @@ private void openPrivacyPolicyDialog(final Context context, final String action)
+ getString(R.string.app_name) + " "
+ BuildConfig.VERSION_NAME)
.putExtra(Intent.EXTRA_TEXT, buildJson());
if (i.resolveActivity(getPackageManager()) != null) {
ShareUtils.openIntentInApp(context, i);
}
ShareUtils.openIntentInApp(context, i, true);
} else if (action.equals("GITHUB")) { // open the NewPipe issue page on GitHub
ShareUtils.openUrlInBrowser(this, ERROR_GITHUB_ISSUE_URL, false);
}

})
.setNegativeButton(R.string.decline, (dialog, which) -> {
// do nothing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public static void playOnExternalPlayer(final Context context, final String name

public static void resolveActivityOrAskToInstall(final Context context, final Intent intent) {
if (intent.resolveActivity(context.getPackageManager()) != null) {
ShareUtils.openIntentInApp(context, intent);
ShareUtils.openIntentInApp(context, intent, false);
} else {
if (context instanceof Activity) {
new AlertDialog.Builder(context)
Expand Down
65 changes: 41 additions & 24 deletions app/src/main/java/org/schabi/newpipe/util/ShareUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ private ShareUtils() {
* second param (a system chooser will be opened if there are multiple markets and no default)
* and falls back to Google Play Store web URL if no app to handle the market scheme was found.
* <p>
* It uses {@link ShareUtils#openIntentInApp(Context, Intent)} to open market scheme and
* {@link ShareUtils#openUrlInBrowser(Context, String, boolean)} to open Google Play Store web
* URL with false for the boolean param.
* It uses {@link ShareUtils#openIntentInApp(Context, Intent, boolean)} to open market scheme
* and {@link ShareUtils#openUrlInBrowser(Context, String, boolean)} to open Google Play Store
* web URL with false for the boolean param.
*
* @param context the context to use
* @param packageId the package id of the app to be installed
Expand All @@ -36,7 +36,7 @@ public static void installApp(final Context context, final String packageId) {
// Try market:// scheme
final boolean marketSchemeResult = openIntentInApp(context, new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=" + packageId))
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), false);
if (!marketSchemeResult) {
// Fall back to Google Play Store Web URL (F-Droid can handle it)
openUrlInBrowser(context,
Expand All @@ -48,7 +48,7 @@ public static void installApp(final Context context, final String packageId) {
* Open the url with the system default browser.
* <p>
* If no browser is set as default, fallbacks to
* {@link ShareUtils#openAppChooser(Context, Intent, String)}
* {@link ShareUtils#openAppChooser(Context, Intent, boolean)}
*
* @param context the context to use
* @param url the url to browse
Expand All @@ -71,7 +71,7 @@ public static boolean openUrlInBrowser(final Context context, final String url,

if (defaultPackageName.equals("android")) {
// No browser set as default (doesn't work on some devices)
openAppChooser(context, intent, context.getString(R.string.open_with));
openAppChooser(context, intent, true);
} else {
if (defaultPackageName.isEmpty()) {
// No app installed to open a web url
Expand All @@ -84,7 +84,7 @@ public static boolean openUrlInBrowser(final Context context, final String url,
} catch (final ActivityNotFoundException e) {
// Not a browser but an app chooser because of OEMs changes
intent.setPackage(null);
openAppChooser(context, intent, context.getString(R.string.open_with));
openAppChooser(context, intent, true);
}
}
}
Expand All @@ -96,7 +96,7 @@ public static boolean openUrlInBrowser(final Context context, final String url,
* Open the url with the system default browser.
* <p>
* If no browser is set as default, fallbacks to
* {@link ShareUtils#openAppChooser(Context, Intent, String)}
* {@link ShareUtils#openAppChooser(Context, Intent, boolean)}
* <p>
* This calls {@link ShareUtils#openUrlInBrowser(Context, String, boolean)} with true
* for the boolean parameter
Expand All @@ -116,22 +116,29 @@ public static boolean openUrlInBrowser(final Context context, final String url)
* {@link ShareUtils#openUrlInBrowser(Context, String, boolean)} should be used.
* <p>
* If no app is set as default, fallbacks to
* {@link ShareUtils#openAppChooser(Context, Intent, String)}
* {@link ShareUtils#openAppChooser(Context, Intent, boolean)}.
* <p>
*
* @param context the context to use
* @param intent the intent to open
* @param context the context to use
* @param intent the intent to open
* @param showToast the boolean to set if a toast is displayed to user when no app is installed
* to open the intent (true) or not (false)
* @return true if the intent can be opened or false if it cannot be
*/
public static boolean openIntentInApp(final Context context, final Intent intent) {
public static boolean openIntentInApp(final Context context, final Intent intent,
final boolean showToast) {
final String defaultPackageName = getDefaultAppPackageName(context, intent);

if (defaultPackageName.equals("android")) {
// No app set as default (doesn't work on some devices)
openAppChooser(context, intent, context.getString(R.string.open_with));
openAppChooser(context, intent, true);
} else {
if (defaultPackageName.isEmpty()) {
// No app installed to open the intent
Toast.makeText(context, R.string.no_app_to_open_intent, Toast.LENGTH_LONG).show();
if (showToast) {
Toast.makeText(context, R.string.no_app_to_open_intent, Toast.LENGTH_LONG)
.show();
}
return false;
} else {
try {
Expand All @@ -140,7 +147,7 @@ public static boolean openIntentInApp(final Context context, final Intent intent
} catch (final ActivityNotFoundException e) {
// Not an app to open the intent but an app chooser because of OEMs changes
intent.setPackage(null);
openAppChooser(context, intent, context.getString(R.string.open_with));
openAppChooser(context, intent, true);
}
}
}
Expand All @@ -152,18 +159,25 @@ public static boolean openIntentInApp(final Context context, final Intent intent
* Open the system chooser to launch an intent.
* <p>
* This method opens an {@link android.content.Intent#ACTION_CHOOSER} of the intent putted
* as the viewIntent param. A string for the chooser's title must be passed as the last param.
* as the intent param. If the setTitleChooser boolean is true, the string "Open with" will be
* set as the title of the system chooser.
* For Android P and higher, title for {@link android.content.Intent#ACTION_SEND} system
* choosers must be set on this intent, not on the
* {@link android.content.Intent#ACTION_CHOOSER} intent.
*
* @param context the context to use
* @param intent the intent to open
* @param chooserStringTitle the string of chooser's title
* @param context the context to use
* @param intent the intent to open
* @param setTitleChooser set the title "Open with" to the chooser if true, else not
*/
private static void openAppChooser(final Context context, final Intent intent,
final String chooserStringTitle) {
private static void openAppChooser(final Context context,
final Intent intent,
final boolean setTitleChooser) {
final Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, intent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, chooserStringTitle);
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (setTitleChooser) {
chooserIntent.putExtra(Intent.EXTRA_TITLE, context.getString(R.string.open_with));
}
context.startActivity(chooserIntent);
}

Expand Down Expand Up @@ -201,10 +215,13 @@ private static String getDefaultAppPackageName(final Context context, final Inte
public static void shareText(final Context context, final String subject, final String url) {
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
if (!subject.isEmpty()) {
shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
}
shareIntent.putExtra(Intent.EXTRA_TEXT, url);
shareIntent.putExtra(Intent.EXTRA_TITLE, context.getString(R.string.share_dialog_title));

openAppChooser(context, shareIntent, context.getString(R.string.share_dialog_title));
openAppChooser(context, shareIntent, false);
}

/**
Expand Down
18 changes: 4 additions & 14 deletions app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,8 @@ private void viewWithFileProvider(Mission mission) {
if (BuildConfig.DEBUG)
Log.v(TAG, "Mime: " + mimeType + " package: " + BuildConfig.APPLICATION_ID + ".provider");

final Uri uri = resolveShareableUri(mission);

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, mimeType);
intent.setDataAndType(resolveShareableUri(mission), mimeType);
intent.addFlags(FLAG_GRANT_READ_URI_PERMISSION);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Expand All @@ -361,10 +359,8 @@ private void viewWithFileProvider(Mission mission) {
intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
}

//mContext.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);

if (intent.resolveActivity(mContext.getPackageManager()) != null) {
ShareUtils.openIntentInApp(mContext, intent);
ShareUtils.openIntentInApp(mContext, intent, false);
} else {
Toast.makeText(mContext, R.string.toast_no_player, Toast.LENGTH_LONG).show();
}
Expand All @@ -377,19 +373,13 @@ private void shareFile(Mission mission) {
shareIntent.setType(resolveMimeType(mission));
shareIntent.putExtra(Intent.EXTRA_STREAM, resolveShareableUri(mission));
shareIntent.addFlags(FLAG_GRANT_READ_URI_PERMISSION);

final Intent intent = new Intent(Intent.ACTION_CHOOSER);
intent.putExtra(Intent.EXTRA_INTENT, shareIntent);
intent.putExtra(Intent.EXTRA_TITLE, mContext.getString(R.string.share_dialog_title));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

try {
intent.setPackage("android");
mContext.startActivity(intent);
} catch (final ActivityNotFoundException e) {
// falling back to OEM chooser if Android's system chooser was removed by the OEM
intent.setPackage(null);
mContext.startActivity(intent);
}
mContext.startActivity(intent);
}

/**
Expand Down

0 comments on commit 9e9d1a0

Please sign in to comment.