From 9e9d1a04e46fba9bf5c597dac5d39a924d1be308 Mon Sep 17 00:00:00 2001 From: TiA4f8R <74829229+TiA4f8R@users.noreply.github.com> Date: Sat, 13 Mar 2021 12:37:54 +0100 Subject: [PATCH] Fix toast shown when falling back to Google Play Store URL and the action 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. --- .../schabi/newpipe/error/ErrorActivity.java | 5 +- .../schabi/newpipe/util/NavigationHelper.java | 2 +- .../org/schabi/newpipe/util/ShareUtils.java | 65 ++++++++++++------- .../giga/ui/adapter/MissionAdapter.java | 18 ++--- 4 files changed, 47 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/error/ErrorActivity.java b/app/src/main/java/org/schabi/newpipe/error/ErrorActivity.java index 106a86cfad3..5fc9a72ca70 100644 --- a/app/src/main/java/org/schabi/newpipe/error/ErrorActivity.java +++ b/app/src/main/java/org/schabi/newpipe/error/ErrorActivity.java @@ -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 diff --git a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java index 106399735ab..2f1851efe3b 100644 --- a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java @@ -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) 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 45ec1d01557..18b2aa5d0ef 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ShareUtils.java +++ b/app/src/main/java/org/schabi/newpipe/util/ShareUtils.java @@ -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. *
- * 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 @@ -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, @@ -48,7 +48,7 @@ public static void installApp(final Context context, final String packageId) { * Open the url with the system default browser. *
* 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 @@ -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 @@ -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); } } } @@ -96,7 +96,7 @@ public static boolean openUrlInBrowser(final Context context, final String url, * Open the url with the system default browser. *
* If no browser is set as default, fallbacks to - * {@link ShareUtils#openAppChooser(Context, Intent, String)} + * {@link ShareUtils#openAppChooser(Context, Intent, boolean)} *
* This calls {@link ShareUtils#openUrlInBrowser(Context, String, boolean)} with true * for the boolean parameter @@ -116,22 +116,29 @@ public static boolean openUrlInBrowser(final Context context, final String url) * {@link ShareUtils#openUrlInBrowser(Context, String, boolean)} should be used. *
* If no app 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 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 { @@ -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); } } } @@ -152,18 +159,25 @@ public static boolean openIntentInApp(final Context context, final Intent intent * Open the system chooser to launch an intent. *
* 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); } @@ -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); } /** diff --git a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java index 45ee290f6f9..b31933dfdd2 100644 --- a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java +++ b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java @@ -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) { @@ -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(); } @@ -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); } /**