Declare intent mutability for Android 12 compatibility

This commit is contained in:
ByteHamster 2021-10-28 23:20:23 +02:00
parent 345aad4148
commit da50f43232
10 changed files with 48 additions and 27 deletions

View File

@ -9,11 +9,11 @@ jobs:
runs-on: macOS-latest runs-on: macOS-latest
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: Set up JDK 8 - name: Set up JDK 11
uses: actions/setup-java@v2 uses: actions/setup-java@v2
with: with:
distribution: 'adopt' distribution: 'adopt'
java-version: '8' java-version: '11'
- name: Wrapper validation - name: Wrapper validation
uses: gradle/wrapper-validation-action@v1 uses: gradle/wrapper-validation-action@v1
- name: Build with Gradle - name: Build with Gradle

View File

@ -3,6 +3,7 @@ package de.danoeh.antennapod.config;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
@ -24,7 +25,8 @@ public class DownloadServiceCallbacksImpl implements DownloadServiceCallbacks {
args.putInt(DownloadsFragment.ARG_SELECTED_TAB, DownloadsFragment.POS_LOG); args.putInt(DownloadsFragment.ARG_SELECTED_TAB, DownloadsFragment.POS_LOG);
intent.putExtra(MainActivity.EXTRA_FRAGMENT_ARGS, args); intent.putExtra(MainActivity.EXTRA_FRAGMENT_ARGS, args);
return PendingIntent.getActivity(context, return PendingIntent.getActivity(context,
R.id.pending_intent_download_service_notification, intent, PendingIntent.FLAG_UPDATE_CURRENT); R.id.pending_intent_download_service_notification, intent,
PendingIntent.FLAG_UPDATE_CURRENT | (Build.VERSION.SDK_INT >= 23 ? PendingIntent.FLAG_IMMUTABLE : 0));
} }
@Override @Override
@ -33,7 +35,8 @@ public class DownloadServiceCallbacksImpl implements DownloadServiceCallbacks {
activityIntent.setAction("request" + request.getFeedfileId()); activityIntent.setAction("request" + request.getFeedfileId());
activityIntent.putExtra(DownloadAuthenticationActivity.ARG_DOWNLOAD_REQUEST, request); activityIntent.putExtra(DownloadAuthenticationActivity.ARG_DOWNLOAD_REQUEST, request);
return PendingIntent.getActivity(context.getApplicationContext(), return PendingIntent.getActivity(context.getApplicationContext(),
R.id.pending_intent_download_service_auth, activityIntent, PendingIntent.FLAG_ONE_SHOT); R.id.pending_intent_download_service_auth, activityIntent,
PendingIntent.FLAG_ONE_SHOT | (Build.VERSION.SDK_INT >= 23 ? PendingIntent.FLAG_IMMUTABLE : 0));
} }
@Override @Override
@ -43,15 +46,15 @@ public class DownloadServiceCallbacksImpl implements DownloadServiceCallbacks {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putInt(DownloadsFragment.ARG_SELECTED_TAB, DownloadsFragment.POS_LOG); args.putInt(DownloadsFragment.ARG_SELECTED_TAB, DownloadsFragment.POS_LOG);
intent.putExtra(MainActivity.EXTRA_FRAGMENT_ARGS, args); intent.putExtra(MainActivity.EXTRA_FRAGMENT_ARGS, args);
return PendingIntent.getActivity(context, R.id.pending_intent_download_service_report, return PendingIntent.getActivity(context, R.id.pending_intent_download_service_report, intent,
intent, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent.FLAG_UPDATE_CURRENT | (Build.VERSION.SDK_INT >= 23 ? PendingIntent.FLAG_IMMUTABLE : 0));
} }
@Override @Override
public PendingIntent getAutoDownloadReportNotificationContentIntent(Context context) { public PendingIntent getAutoDownloadReportNotificationContentIntent(Context context) {
Intent intent = new Intent(context, MainActivity.class); Intent intent = new Intent(context, MainActivity.class);
intent.putExtra(MainActivity.EXTRA_FRAGMENT_TAG, QueueFragment.TAG); intent.putExtra(MainActivity.EXTRA_FRAGMENT_TAG, QueueFragment.TAG);
return PendingIntent.getActivity(context, R.id.pending_intent_download_service_autodownload_report, return PendingIntent.getActivity(context, R.id.pending_intent_download_service_autodownload_report, intent,
intent, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent.FLAG_UPDATE_CURRENT | (Build.VERSION.SDK_INT >= 23 ? PendingIntent.FLAG_IMMUTABLE : 0));
} }
} }

View File

@ -1,5 +1,5 @@
android { android {
compileSdkVersion 30 compileSdkVersion 31
defaultConfig { defaultConfig {
minSdkVersion 16 minSdkVersion 16

View File

@ -8,6 +8,7 @@ import android.content.Intent;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.os.Build;
import android.util.Log; import android.util.Log;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat; import androidx.core.app.NotificationManagerCompat;
@ -68,7 +69,8 @@ public class NewEpisodesNotification {
intent.setComponent(new ComponentName(context, "de.danoeh.antennapod.activity.MainActivity")); intent.setComponent(new ComponentName(context, "de.danoeh.antennapod.activity.MainActivity"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra("fragment_feed_id", feed.getId()); intent.putExtra("fragment_feed_id", feed.getId());
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
(Build.VERSION.SDK_INT >= 23 ? PendingIntent.FLAG_IMMUTABLE : 0));
Notification notification = new NotificationCompat.Builder( Notification notification = new NotificationCompat.Builder(
context, NotificationUtils.CHANNEL_ID_EPISODE_NOTIFICATIONS) context, NotificationUtils.CHANNEL_ID_EPISODE_NOTIFICATIONS)
@ -93,7 +95,8 @@ public class NewEpisodesNotification {
intent.setComponent(new ComponentName(context, "de.danoeh.antennapod.activity.MainActivity")); intent.setComponent(new ComponentName(context, "de.danoeh.antennapod.activity.MainActivity"));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra("fragment_tag", "EpisodesFragment"); intent.putExtra("fragment_tag", "EpisodesFragment");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
(Build.VERSION.SDK_INT >= 23 ? PendingIntent.FLAG_IMMUTABLE : 0));
Notification notificationGroupSummary = new NotificationCompat.Builder( Notification notificationGroupSummary = new NotificationCompat.Builder(
context, NotificationUtils.CHANNEL_ID_EPISODE_NOTIFICATIONS) context, NotificationUtils.CHANNEL_ID_EPISODE_NOTIFICATIONS)

View File

@ -286,7 +286,8 @@ public class PlaybackService extends MediaBrowserServiceCompat {
ComponentName eventReceiver = new ComponentName(getApplicationContext(), MediaButtonReceiver.class); ComponentName eventReceiver = new ComponentName(getApplicationContext(), MediaButtonReceiver.class);
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(eventReceiver); mediaButtonIntent.setComponent(eventReceiver);
PendingIntent buttonReceiverIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent buttonReceiverIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent,
PendingIntent.FLAG_UPDATE_CURRENT | (Build.VERSION.SDK_INT >= 31 ? PendingIntent.FLAG_MUTABLE : 0));
mediaSession = new MediaSessionCompat(getApplicationContext(), TAG, eventReceiver, buttonReceiverIntent); mediaSession = new MediaSessionCompat(getApplicationContext(), TAG, eventReceiver, buttonReceiverIntent);
setSessionToken(mediaSession.getSessionToken()); setSessionToken(mediaSession.getSessionToken());
@ -598,10 +599,12 @@ public class PlaybackService extends MediaBrowserServiceCompat {
PendingIntent pendingIntentAllowThisTime; PendingIntent pendingIntentAllowThisTime;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
pendingIntentAllowThisTime = PendingIntent.getForegroundService(this, pendingIntentAllowThisTime = PendingIntent.getForegroundService(this,
R.id.pending_intent_allow_stream_this_time, intentAllowThisTime, PendingIntent.FLAG_UPDATE_CURRENT); R.id.pending_intent_allow_stream_this_time, intentAllowThisTime,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
} else { } else {
pendingIntentAllowThisTime = PendingIntent.getService(this, pendingIntentAllowThisTime = PendingIntent.getService(this,
R.id.pending_intent_allow_stream_this_time, intentAllowThisTime, PendingIntent.FLAG_UPDATE_CURRENT); R.id.pending_intent_allow_stream_this_time, intentAllowThisTime, PendingIntent.FLAG_UPDATE_CURRENT
| (Build.VERSION.SDK_INT >= 23 ? PendingIntent.FLAG_IMMUTABLE : 0));
} }
Intent intentAlwaysAllow = new Intent(intentAllowThisTime); Intent intentAlwaysAllow = new Intent(intentAllowThisTime);
@ -610,10 +613,12 @@ public class PlaybackService extends MediaBrowserServiceCompat {
PendingIntent pendingIntentAlwaysAllow; PendingIntent pendingIntentAlwaysAllow;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
pendingIntentAlwaysAllow = PendingIntent.getForegroundService(this, pendingIntentAlwaysAllow = PendingIntent.getForegroundService(this,
R.id.pending_intent_allow_stream_always, intentAlwaysAllow, PendingIntent.FLAG_UPDATE_CURRENT); R.id.pending_intent_allow_stream_always, intentAlwaysAllow,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
} else { } else {
pendingIntentAlwaysAllow = PendingIntent.getService(this, pendingIntentAlwaysAllow = PendingIntent.getService(this,
R.id.pending_intent_allow_stream_always, intentAlwaysAllow, PendingIntent.FLAG_UPDATE_CURRENT); R.id.pending_intent_allow_stream_always, intentAlwaysAllow, PendingIntent.FLAG_UPDATE_CURRENT
| (Build.VERSION.SDK_INT >= 23 ? PendingIntent.FLAG_IMMUTABLE : 0));
} }
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NotificationCompat.Builder builder = new NotificationCompat.Builder(this,
@ -1302,7 +1307,8 @@ public class PlaybackService extends MediaBrowserServiceCompat {
if (stateManager.hasReceivedValidStartCommand()) { if (stateManager.hasReceivedValidStartCommand()) {
mediaSession.setSessionActivity(PendingIntent.getActivity(this, R.id.pending_intent_player_activity, mediaSession.setSessionActivity(PendingIntent.getActivity(this, R.id.pending_intent_player_activity,
PlaybackService.getPlayerActivityIntent(this), PendingIntent.FLAG_UPDATE_CURRENT)); PlaybackService.getPlayerActivityIntent(this), PendingIntent.FLAG_UPDATE_CURRENT
| (Build.VERSION.SDK_INT >= 31 ? PendingIntent.FLAG_MUTABLE : 0)));
try { try {
mediaSession.setMetadata(builder.build()); mediaSession.setMetadata(builder.build());
} catch (OutOfMemoryError e) { } catch (OutOfMemoryError e) {

View File

@ -170,7 +170,8 @@ public class PlaybackServiceNotificationBuilder {
private PendingIntent getPlayerActivityPendingIntent() { private PendingIntent getPlayerActivityPendingIntent() {
return PendingIntent.getActivity(context, R.id.pending_intent_player_activity, return PendingIntent.getActivity(context, R.id.pending_intent_player_activity,
PlaybackService.getPlayerActivityIntent(context), PendingIntent.FLAG_UPDATE_CURRENT); PlaybackService.getPlayerActivityIntent(context), PendingIntent.FLAG_UPDATE_CURRENT
| (Build.VERSION.SDK_INT >= 23 ? PendingIntent.FLAG_IMMUTABLE : 0));
} }
private void addActions(NotificationCompat.Builder notification, MediaSessionCompat.Token mediaSessionToken, private void addActions(NotificationCompat.Builder notification, MediaSessionCompat.Token mediaSessionToken,
@ -183,7 +184,8 @@ public class PlaybackServiceNotificationBuilder {
Intent stopCastingIntent = new Intent(context, PlaybackService.class); Intent stopCastingIntent = new Intent(context, PlaybackService.class);
stopCastingIntent.putExtra(PlaybackService.EXTRA_CAST_DISCONNECT, true); stopCastingIntent.putExtra(PlaybackService.EXTRA_CAST_DISCONNECT, true);
PendingIntent stopCastingPendingIntent = PendingIntent.getService(context, PendingIntent stopCastingPendingIntent = PendingIntent.getService(context,
numActions, stopCastingIntent, PendingIntent.FLAG_UPDATE_CURRENT); numActions, stopCastingIntent, PendingIntent.FLAG_UPDATE_CURRENT
| (Build.VERSION.SDK_INT >= 23 ? PendingIntent.FLAG_IMMUTABLE : 0));
notification.addAction(R.drawable.ic_notification_cast_off, notification.addAction(R.drawable.ic_notification_cast_off,
context.getString(R.string.cast_disconnect_label), context.getString(R.string.cast_disconnect_label),
stopCastingPendingIntent); stopCastingPendingIntent);
@ -252,9 +254,11 @@ public class PlaybackServiceNotificationBuilder {
intent.putExtra(MediaButtonReceiver.EXTRA_KEYCODE, keycodeValue); intent.putExtra(MediaButtonReceiver.EXTRA_KEYCODE, keycodeValue);
if (Build.VERSION.SDK_INT >= 26) { if (Build.VERSION.SDK_INT >= 26) {
return PendingIntent.getForegroundService(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT); return PendingIntent.getForegroundService(context, requestCode, intent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
} else { } else {
return PendingIntent.getService(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT); return PendingIntent.getService(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT
| (Build.VERSION.SDK_INT >= 23 ? PendingIntent.FLAG_IMMUTABLE : 0));
} }
} }

View File

@ -5,6 +5,7 @@ import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Build;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -302,7 +303,8 @@ public class SyncService extends Worker {
Intent intent = getApplicationContext().getPackageManager().getLaunchIntentForPackage( Intent intent = getApplicationContext().getPackageManager().getLaunchIntentForPackage(
getApplicationContext().getPackageName()); getApplicationContext().getPackageName());
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
R.id.pending_intent_sync_error, intent, PendingIntent.FLAG_UPDATE_CURRENT); R.id.pending_intent_sync_error, intent, PendingIntent.FLAG_UPDATE_CURRENT
| (Build.VERSION.SDK_INT >= 23 ? PendingIntent.FLAG_IMMUTABLE : 0));
Notification notification = new NotificationCompat.Builder(getApplicationContext(), Notification notification = new NotificationCompat.Builder(getApplicationContext(),
NotificationUtils.CHANNEL_ID_SYNC_ERROR) NotificationUtils.CHANNEL_ID_SYNC_ERROR)
.setContentTitle(getApplicationContext().getString(R.string.gpodnetsync_error_title)) .setContentTitle(getApplicationContext().getString(R.string.gpodnetsync_error_title))

View File

@ -7,6 +7,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.KeyEvent; import android.view.KeyEvent;
@ -212,7 +213,8 @@ public abstract class WidgetUpdater {
startingIntent.setAction(MediaButtonReceiver.NOTIFY_BUTTON_RECEIVER); startingIntent.setAction(MediaButtonReceiver.NOTIFY_BUTTON_RECEIVER);
startingIntent.putExtra(Intent.EXTRA_KEY_EVENT, event); startingIntent.putExtra(Intent.EXTRA_KEY_EVENT, event);
return PendingIntent.getBroadcast(context, eventCode, startingIntent, 0); return PendingIntent.getBroadcast(context, eventCode, startingIntent,
(Build.VERSION.SDK_INT >= 23 ? PendingIntent.FLAG_IMMUTABLE : 0));
} }
private static String getProgressString(int position, int duration, float speed) { private static String getProgressString(int position, int duration, float speed) {

View File

@ -3,6 +3,7 @@ package de.danoeh.antennapod.ui.appstartintent;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Build;
/** /**
* Launches the main activity of the app with specific arguments. * Launches the main activity of the app with specific arguments.
@ -26,8 +27,8 @@ public class MainActivityStarter {
} }
public PendingIntent getPendingIntent() { public PendingIntent getPendingIntent() {
return PendingIntent.getActivity(context, R.id.pending_intent_player_activity, return PendingIntent.getActivity(context, R.id.pending_intent_player_activity, getIntent(),
getIntent(), PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent.FLAG_UPDATE_CURRENT | (Build.VERSION.SDK_INT >= 23 ? PendingIntent.FLAG_IMMUTABLE : 0));
} }
public void start() { public void start() {

View File

@ -28,8 +28,8 @@ public class VideoPlayerActivityStarter {
} }
public PendingIntent getPendingIntent() { public PendingIntent getPendingIntent() {
return PendingIntent.getActivity(context, R.id.pending_intent_video_player, return PendingIntent.getActivity(context, R.id.pending_intent_video_player, getIntent(),
getIntent(), PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent.FLAG_UPDATE_CURRENT | (Build.VERSION.SDK_INT >= 23 ? PendingIntent.FLAG_IMMUTABLE : 0));
} }
public void start() { public void start() {