Skip to content

Commit abe6dfb

Browse files
authored
Merge pull request TeamNewPipe#9671 from Stypox/fix-toast-crash-api33
Fix popup enablement toast crash on API 33
2 parents d08d7cf + b6e6d39 commit abe6dfb

5 files changed

Lines changed: 20 additions & 25 deletions

File tree

app/src/main/java/org/schabi/newpipe/RouterActivity.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,8 +631,7 @@ private void handleChoice(final String selectedChoiceKey) {
631631
}
632632

633633
if (selectedChoiceKey.equals(getString(R.string.popup_player_key))
634-
&& !PermissionHelper.isPopupEnabled(this)) {
635-
PermissionHelper.showPopupEnablementToast(this);
634+
&& !PermissionHelper.isPopupEnabledElseAsk(this)) {
636635
finish();
637636
return;
638637
}

app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,8 +1068,7 @@ private void openBackgroundPlayer(final boolean append) {
10681068
}
10691069

10701070
private void openPopupPlayer(final boolean append) {
1071-
if (!PermissionHelper.isPopupEnabled(activity)) {
1072-
PermissionHelper.showPopupEnablementToast(activity);
1071+
if (!PermissionHelper.isPopupEnabledElseAsk(activity)) {
10731072
return;
10741073
}
10751074

app/src/main/java/org/schabi/newpipe/player/PlayQueueActivity.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,9 @@ public boolean onOptionsItemSelected(final MenuItem item) {
143143
NavigationHelper.playOnMainPlayer(this, player.getPlayQueue(), true);
144144
return true;
145145
case R.id.action_switch_popup:
146-
if (PermissionHelper.isPopupEnabled(this)) {
146+
if (PermissionHelper.isPopupEnabledElseAsk(this)) {
147147
this.player.setRecovery();
148148
NavigationHelper.playOnPopupPlayer(this, player.getPlayQueue(), true);
149-
} else {
150-
PermissionHelper.showPopupEnablementToast(this);
151149
}
152150
return true;
153151
case R.id.action_switch_background:

app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,7 @@ public static void playOnMainPlayer(final Context context,
156156
public static void playOnPopupPlayer(final Context context,
157157
final PlayQueue queue,
158158
final boolean resumePlayback) {
159-
if (!PermissionHelper.isPopupEnabled(context)) {
160-
PermissionHelper.showPopupEnablementToast(context);
159+
if (!PermissionHelper.isPopupEnabledElseAsk(context)) {
161160
return;
162161
}
163162

@@ -183,8 +182,7 @@ public static void playOnBackgroundPlayer(final Context context,
183182
public static void enqueueOnPlayer(final Context context,
184183
final PlayQueue queue,
185184
final PlayerType playerType) {
186-
if ((playerType == PlayerType.POPUP) && !PermissionHelper.isPopupEnabled(context)) {
187-
PermissionHelper.showPopupEnablementToast(context);
185+
if (playerType == PlayerType.POPUP && !PermissionHelper.isPopupEnabledElseAsk(context)) {
188186
return;
189187
}
190188

app/src/main/java/org/schabi/newpipe/util/PermissionHelper.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import android.net.Uri;
1010
import android.os.Build;
1111
import android.provider.Settings;
12-
import android.view.Gravity;
13-
import android.widget.TextView;
1412
import android.widget.Toast;
1513

1614
import androidx.annotation.RequiresApi;
@@ -128,18 +126,21 @@ public static boolean checkSystemAlertWindowPermission(final Context context) {
128126
}
129127
}
130128

131-
public static boolean isPopupEnabled(final Context context) {
132-
return Build.VERSION.SDK_INT < Build.VERSION_CODES.M
133-
|| checkSystemAlertWindowPermission(context);
134-
}
135-
136-
public static void showPopupEnablementToast(final Context context) {
137-
final Toast toast =
138-
Toast.makeText(context, R.string.msg_popup_permission, Toast.LENGTH_LONG);
139-
final TextView messageView = toast.getView().findViewById(android.R.id.message);
140-
if (messageView != null) {
141-
messageView.setGravity(Gravity.CENTER);
129+
/**
130+
* Determines whether the popup is enabled, and if it is not, starts the system activity to
131+
* request the permission with {@link #checkSystemAlertWindowPermission(Context)} and shows a
132+
* toast to the user explaining why the permission is needed.
133+
*
134+
* @param context the Android context
135+
* @return whether the popup is enabled
136+
*/
137+
public static boolean isPopupEnabledElseAsk(final Context context) {
138+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M
139+
|| checkSystemAlertWindowPermission(context)) {
140+
return true;
141+
} else {
142+
Toast.makeText(context, R.string.msg_popup_permission, Toast.LENGTH_LONG).show();
143+
return false;
142144
}
143-
toast.show();
144145
}
145146
}

0 commit comments

Comments
 (0)