|
| 1 | +package org.schabi.newpipe.util; |
| 2 | + |
| 3 | +import android.app.PendingIntent; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.Intent; |
| 6 | +import android.os.Build; |
| 7 | + |
| 8 | +import androidx.annotation.NonNull; |
| 9 | + |
| 10 | +public final class PendingIntentCompat { |
| 11 | + private PendingIntentCompat() { |
| 12 | + } |
| 13 | + |
| 14 | + private static int addImmutableFlag(final int flags) { |
| 15 | + return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M |
| 16 | + ? flags | PendingIntent.FLAG_IMMUTABLE : flags; |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Creates a {@link PendingIntent} to start an activity. It is immutable on API level 23 and |
| 21 | + * greater. |
| 22 | + * |
| 23 | + * @param context The context in which the activity should be started. |
| 24 | + * @param requestCode The request code |
| 25 | + * @param intent The Intent of the activity to be launched. |
| 26 | + * @param flags The flags for the intent. |
| 27 | + * @return The pending intent. |
| 28 | + * @see PendingIntent#getActivity(Context, int, Intent, int) |
| 29 | + */ |
| 30 | + @NonNull |
| 31 | + public static PendingIntent getActivity(@NonNull final Context context, final int requestCode, |
| 32 | + @NonNull final Intent intent, final int flags) { |
| 33 | + return PendingIntent.getActivity(context, requestCode, intent, addImmutableFlag(flags)); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Creates a {@link PendingIntent} to start a service. It is immutable on API level 23 and |
| 38 | + * greater. |
| 39 | + * |
| 40 | + * @param context The context in which the service should be started. |
| 41 | + * @param requestCode The request code |
| 42 | + * @param intent The Intent of the service to be launched. |
| 43 | + * @param flags The flags for the intent. |
| 44 | + * @return The pending intent. |
| 45 | + * @see PendingIntent#getService(Context, int, Intent, int) |
| 46 | + */ |
| 47 | + @NonNull |
| 48 | + public static PendingIntent getService(@NonNull final Context context, final int requestCode, |
| 49 | + @NonNull final Intent intent, final int flags) { |
| 50 | + return PendingIntent.getService(context, requestCode, intent, addImmutableFlag(flags)); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Creates a {@link PendingIntent} to perform a broadcast. It is immutable on API level 23 and |
| 55 | + * greater. |
| 56 | + * |
| 57 | + * @param context The context in which the broadcast should be performed. |
| 58 | + * @param requestCode The request code |
| 59 | + * @param intent The Intent to be broadcast. |
| 60 | + * @param flags The flags for the intent. |
| 61 | + * @return The pending intent. |
| 62 | + * @see PendingIntent#getBroadcast(Context, int, Intent, int) |
| 63 | + */ |
| 64 | + @NonNull |
| 65 | + public static PendingIntent getBroadcast(@NonNull final Context context, final int requestCode, |
| 66 | + @NonNull final Intent intent, final int flags) { |
| 67 | + return PendingIntent.getBroadcast(context, requestCode, intent, addImmutableFlag(flags)); |
| 68 | + } |
| 69 | +} |
0 commit comments