Attempt to workaround Android 11 / Xiaomi pending intent bug

This commit is contained in:
M66B 2022-03-31 18:49:00 +02:00
parent 7bcb15220d
commit a32e118ed3
1 changed files with 5 additions and 3 deletions

View File

@ -30,22 +30,24 @@ public class PendingIntentCompat {
private PendingIntentCompat() { private PendingIntentCompat() {
} }
// https://developer.android.com/about/versions/12/behavior-changes-12#pending-intent-mutability
public static PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags) { public static PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || (flags & PendingIntent.FLAG_MUTABLE) != 0) if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S || (flags & PendingIntent.FLAG_MUTABLE) != 0)
return PendingIntent.getActivity(context, requestCode, intent, flags); return PendingIntent.getActivity(context, requestCode, intent, flags);
else else
return PendingIntent.getActivity(context, requestCode, intent, flags | PendingIntent.FLAG_IMMUTABLE); return PendingIntent.getActivity(context, requestCode, intent, flags | PendingIntent.FLAG_IMMUTABLE);
} }
public static PendingIntent getService(Context context, int requestCode, @NonNull Intent intent, int flags) { public static PendingIntent getService(Context context, int requestCode, @NonNull Intent intent, int flags) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || (flags & PendingIntent.FLAG_MUTABLE) != 0) if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S || (flags & PendingIntent.FLAG_MUTABLE) != 0)
return PendingIntent.getService(context, requestCode, intent, flags); return PendingIntent.getService(context, requestCode, intent, flags);
else else
return PendingIntent.getService(context, requestCode, intent, flags | PendingIntent.FLAG_IMMUTABLE); return PendingIntent.getService(context, requestCode, intent, flags | PendingIntent.FLAG_IMMUTABLE);
} }
static PendingIntent getForegroundService(Context context, int requestCode, @NonNull Intent intent, int flags) { static PendingIntent getForegroundService(Context context, int requestCode, @NonNull Intent intent, int flags) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O || (flags & PendingIntent.FLAG_MUTABLE) != 0) if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S || (flags & PendingIntent.FLAG_MUTABLE) != 0)
return PendingIntent.getService(context, requestCode, intent, flags); return PendingIntent.getService(context, requestCode, intent, flags);
else else
return PendingIntent.getForegroundService(context, requestCode, intent, flags | PendingIntent.FLAG_IMMUTABLE); return PendingIntent.getForegroundService(context, requestCode, intent, flags | PendingIntent.FLAG_IMMUTABLE);