From b6e6d39985b34ac318258ce5e894ae06abf51208 Mon Sep 17 00:00:00 2001 From: Stypox Date: Thu, 12 Jan 2023 11:39:25 +0100 Subject: [PATCH] Fix toast crash on API 33 You shouldn't call getView() on toasts. Also simplified some duplicate code. --- .../org/schabi/newpipe/RouterActivity.java | 3 +- .../fragments/detail/VideoDetailFragment.java | 3 +- .../newpipe/player/PlayQueueActivity.java | 4 +-- .../schabi/newpipe/util/NavigationHelper.java | 6 ++-- .../schabi/newpipe/util/PermissionHelper.java | 29 ++++++++++--------- 5 files changed, 20 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/RouterActivity.java b/app/src/main/java/org/schabi/newpipe/RouterActivity.java index 2567df993..e9c19a22d 100644 --- a/app/src/main/java/org/schabi/newpipe/RouterActivity.java +++ b/app/src/main/java/org/schabi/newpipe/RouterActivity.java @@ -631,8 +631,7 @@ public class RouterActivity extends AppCompatActivity { } if (selectedChoiceKey.equals(getString(R.string.popup_player_key)) - && !PermissionHelper.isPopupEnabled(this)) { - PermissionHelper.showPopupEnablementToast(this); + && !PermissionHelper.isPopupEnabledElseAsk(this)) { finish(); return; } diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java index 601135cbb..6f4439ee9 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java @@ -1066,8 +1066,7 @@ public final class VideoDetailFragment } private void openPopupPlayer(final boolean append) { - if (!PermissionHelper.isPopupEnabled(activity)) { - PermissionHelper.showPopupEnablementToast(activity); + if (!PermissionHelper.isPopupEnabledElseAsk(activity)) { return; } diff --git a/app/src/main/java/org/schabi/newpipe/player/PlayQueueActivity.java b/app/src/main/java/org/schabi/newpipe/player/PlayQueueActivity.java index 94de7fef3..9ce99c15b 100644 --- a/app/src/main/java/org/schabi/newpipe/player/PlayQueueActivity.java +++ b/app/src/main/java/org/schabi/newpipe/player/PlayQueueActivity.java @@ -143,11 +143,9 @@ public final class PlayQueueActivity extends AppCompatActivity NavigationHelper.playOnMainPlayer(this, player.getPlayQueue(), true); return true; case R.id.action_switch_popup: - if (PermissionHelper.isPopupEnabled(this)) { + if (PermissionHelper.isPopupEnabledElseAsk(this)) { this.player.setRecovery(); NavigationHelper.playOnPopupPlayer(this, player.getPlayQueue(), true); - } else { - PermissionHelper.showPopupEnablementToast(this); } return true; case R.id.action_switch_background: 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 483f5067a..b4556507c 100644 --- a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java @@ -156,8 +156,7 @@ public final class NavigationHelper { public static void playOnPopupPlayer(final Context context, final PlayQueue queue, final boolean resumePlayback) { - if (!PermissionHelper.isPopupEnabled(context)) { - PermissionHelper.showPopupEnablementToast(context); + if (!PermissionHelper.isPopupEnabledElseAsk(context)) { return; } @@ -183,8 +182,7 @@ public final class NavigationHelper { public static void enqueueOnPlayer(final Context context, final PlayQueue queue, final PlayerType playerType) { - if ((playerType == PlayerType.POPUP) && !PermissionHelper.isPopupEnabled(context)) { - PermissionHelper.showPopupEnablementToast(context); + if (playerType == PlayerType.POPUP && !PermissionHelper.isPopupEnabledElseAsk(context)) { return; } diff --git a/app/src/main/java/org/schabi/newpipe/util/PermissionHelper.java b/app/src/main/java/org/schabi/newpipe/util/PermissionHelper.java index f47494770..55193599e 100644 --- a/app/src/main/java/org/schabi/newpipe/util/PermissionHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/PermissionHelper.java @@ -9,8 +9,6 @@ import android.content.pm.PackageManager; import android.net.Uri; import android.os.Build; import android.provider.Settings; -import android.view.Gravity; -import android.widget.TextView; import android.widget.Toast; import androidx.annotation.RequiresApi; @@ -128,18 +126,21 @@ public final class PermissionHelper { } } - public static boolean isPopupEnabled(final Context context) { - return Build.VERSION.SDK_INT < Build.VERSION_CODES.M - || checkSystemAlertWindowPermission(context); - } - - public static void showPopupEnablementToast(final Context context) { - final Toast toast = - Toast.makeText(context, R.string.msg_popup_permission, Toast.LENGTH_LONG); - final TextView messageView = toast.getView().findViewById(android.R.id.message); - if (messageView != null) { - messageView.setGravity(Gravity.CENTER); + /** + * Determines whether the popup is enabled, and if it is not, starts the system activity to + * request the permission with {@link #checkSystemAlertWindowPermission(Context)} and shows a + * toast to the user explaining why the permission is needed. + * + * @param context the Android context + * @return whether the popup is enabled + */ + public static boolean isPopupEnabledElseAsk(final Context context) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M + || checkSystemAlertWindowPermission(context)) { + return true; + } else { + Toast.makeText(context, R.string.msg_popup_permission, Toast.LENGTH_LONG).show(); + return false; } - toast.show(); } }