Skip to content

Commit 95a65d5

Browse files
Merge pull request #9333 from Isira-Seneviratne/PendingIntent_mutability
Make PendingIntents immutable on Android 6.0 and later.
2 parents 4573407 + 3dc1adb commit 95a65d5

7 files changed

Lines changed: 89 additions & 30 deletions

File tree

app/src/main/java/org/schabi/newpipe/NewVersionWorker.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.schabi.newpipe
22

3-
import android.app.PendingIntent
43
import android.content.Context
54
import android.content.Intent
65
import android.util.Log
@@ -18,6 +17,7 @@ import com.grack.nanojson.JsonParser
1817
import com.grack.nanojson.JsonParserException
1918
import org.schabi.newpipe.extractor.downloader.Response
2019
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException
20+
import org.schabi.newpipe.util.PendingIntentCompat
2121
import org.schabi.newpipe.util.ReleaseVersionUtil.coerceUpdateCheckExpiry
2222
import org.schabi.newpipe.util.ReleaseVersionUtil.isLastUpdateCheckExpired
2323
import org.schabi.newpipe.util.ReleaseVersionUtil.isReleaseApk
@@ -49,7 +49,7 @@ class NewVersionWorker(
4949
// A pending intent to open the apk location url in the browser.
5050
val intent = Intent(Intent.ACTION_VIEW, apkLocationUrl?.toUri())
5151
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
52-
val pendingIntent = PendingIntent.getActivity(app, 0, intent, 0)
52+
val pendingIntent = PendingIntentCompat.getActivity(app, 0, intent, 0)
5353
val channelId = app.getString(R.string.app_update_notification_channel_id)
5454
val notificationBuilder = NotificationCompat.Builder(app, channelId)
5555
.setSmallIcon(R.drawable.ic_newpipe_update)

app/src/main/java/org/schabi/newpipe/error/ErrorUtil.kt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import android.app.PendingIntent
55
import android.content.Context
66
import android.content.Intent
77
import android.graphics.Color
8-
import android.os.Build
98
import android.view.View
109
import android.widget.Toast
1110
import androidx.core.app.NotificationCompat
1211
import androidx.core.app.NotificationManagerCompat
1312
import androidx.fragment.app.Fragment
1413
import com.google.android.material.snackbar.Snackbar
1514
import org.schabi.newpipe.R
15+
import org.schabi.newpipe.util.PendingIntentCompat
1616

1717
/**
1818
* This class contains all of the methods that should be used to let the user know that an error has
@@ -104,11 +104,6 @@ class ErrorUtil {
104104
*/
105105
@JvmStatic
106106
fun createNotification(context: Context, errorInfo: ErrorInfo) {
107-
var pendingIntentFlags = PendingIntent.FLAG_UPDATE_CURRENT
108-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
109-
pendingIntentFlags = pendingIntentFlags or PendingIntent.FLAG_IMMUTABLE
110-
}
111-
112107
val notificationBuilder: NotificationCompat.Builder =
113108
NotificationCompat.Builder(
114109
context,
@@ -119,11 +114,11 @@ class ErrorUtil {
119114
.setContentText(context.getString(errorInfo.messageStringId))
120115
.setAutoCancel(true)
121116
.setContentIntent(
122-
PendingIntent.getActivity(
117+
PendingIntentCompat.getActivity(
123118
context,
124119
0,
125120
getErrorActivityIntent(context, errorInfo),
126-
pendingIntentFlags
121+
PendingIntent.FLAG_UPDATE_CURRENT
127122
)
128123
)
129124

app/src/main/java/org/schabi/newpipe/local/feed/notifications/NotificationHelper.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.schabi.newpipe.local.feed.notifications
22

33
import android.app.NotificationManager
4-
import android.app.PendingIntent
54
import android.content.Context
65
import android.content.Intent
76
import android.graphics.Bitmap
@@ -20,6 +19,7 @@ import org.schabi.newpipe.extractor.stream.StreamInfoItem
2019
import org.schabi.newpipe.local.feed.service.FeedUpdateInfo
2120
import org.schabi.newpipe.util.Localization
2221
import org.schabi.newpipe.util.NavigationHelper
22+
import org.schabi.newpipe.util.PendingIntentCompat
2323
import org.schabi.newpipe.util.PicassoHelper
2424

2525
/**
@@ -70,16 +70,13 @@ class NotificationHelper(val context: Context) {
7070

7171
// open the channel page when clicking on the notification
7272
builder.setContentIntent(
73-
PendingIntent.getActivity(
73+
PendingIntentCompat.getActivity(
7474
context,
7575
data.pseudoId,
7676
NavigationHelper
7777
.getChannelIntent(context, data.listInfo.serviceId, data.listInfo.url)
7878
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
79-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
80-
PendingIntent.FLAG_IMMUTABLE
81-
else
82-
0
79+
0
8380
)
8481
)
8582

app/src/main/java/org/schabi/newpipe/local/feed/service/FeedLoadService.kt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.schabi.newpipe.local.feed.service
2121

22-
import android.app.PendingIntent
2322
import android.app.Service
2423
import android.content.BroadcastReceiver
2524
import android.content.Context
@@ -43,6 +42,7 @@ import org.schabi.newpipe.extractor.ListInfo
4342
import org.schabi.newpipe.extractor.stream.StreamInfoItem
4443
import org.schabi.newpipe.local.feed.service.FeedEventManager.Event.ErrorResultEvent
4544
import org.schabi.newpipe.local.feed.service.FeedEventManager.postEvent
45+
import org.schabi.newpipe.util.PendingIntentCompat
4646
import java.util.concurrent.TimeUnit
4747

4848
class FeedLoadService : Service() {
@@ -152,12 +152,8 @@ class FeedLoadService : Service() {
152152
private lateinit var notificationBuilder: NotificationCompat.Builder
153153

154154
private fun createNotification(): NotificationCompat.Builder {
155-
val cancelActionIntent = PendingIntent.getBroadcast(
156-
this,
157-
NOTIFICATION_ID,
158-
Intent(ACTION_CANCEL),
159-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else 0
160-
)
155+
val cancelActionIntent =
156+
PendingIntentCompat.getBroadcast(this, NOTIFICATION_ID, Intent(ACTION_CANCEL), 0)
161157

162158
return NotificationCompat.Builder(this, getString(R.string.notification_channel_id))
163159
.setOngoing(true)

app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.schabi.newpipe.player.notification;
22

33
import android.annotation.SuppressLint;
4-
import android.app.PendingIntent;
54
import android.content.Intent;
65
import android.content.pm.ServiceInfo;
76
import android.graphics.Bitmap;
@@ -22,6 +21,7 @@
2221
import org.schabi.newpipe.player.Player;
2322
import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi;
2423
import org.schabi.newpipe.util.NavigationHelper;
24+
import org.schabi.newpipe.util.PendingIntentCompat;
2525

2626
import java.util.List;
2727
import java.util.Objects;
@@ -133,8 +133,8 @@ private synchronized NotificationCompat.Builder createNotification() {
133133
R.color.dark_background_color))
134134
.setColorized(player.getPrefs().getBoolean(
135135
player.getContext().getString(R.string.notification_colorize_key), true))
136-
.setDeleteIntent(PendingIntent.getBroadcast(player.getContext(), NOTIFICATION_ID,
137-
new Intent(ACTION_CLOSE), FLAG_UPDATE_CURRENT));
136+
.setDeleteIntent(PendingIntentCompat.getBroadcast(player.getContext(),
137+
NOTIFICATION_ID, new Intent(ACTION_CLOSE), FLAG_UPDATE_CURRENT));
138138

139139
// set the initial value for the video thumbnail, updatable with updateNotificationThumbnail
140140
setLargeIcon(builder);
@@ -151,7 +151,7 @@ private synchronized void updateNotification() {
151151
}
152152

153153
// also update content intent, in case the user switched players
154-
notificationBuilder.setContentIntent(PendingIntent.getActivity(player.getContext(),
154+
notificationBuilder.setContentIntent(PendingIntentCompat.getActivity(player.getContext(),
155155
NOTIFICATION_ID, getIntentForNotification(), FLAG_UPDATE_CURRENT));
156156
notificationBuilder.setContentTitle(player.getVideoTitle());
157157
notificationBuilder.setContentText(player.getUploaderName());
@@ -334,7 +334,7 @@ private NotificationCompat.Action getAction(@DrawableRes final int drawable,
334334
@StringRes final int title,
335335
final String intentAction) {
336336
return new NotificationCompat.Action(drawable, player.getContext().getString(title),
337-
PendingIntent.getBroadcast(player.getContext(), NOTIFICATION_ID,
337+
PendingIntentCompat.getBroadcast(player.getContext(), NOTIFICATION_ID,
338338
new Intent(intentAction), FLAG_UPDATE_CURRENT));
339339
}
340340

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
}

app/src/main/java/us/shandian/giga/service/DownloadManagerService.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.schabi.newpipe.streams.io.StoredDirectoryHelper;
4848
import org.schabi.newpipe.streams.io.StoredFileHelper;
4949
import org.schabi.newpipe.util.Localization;
50+
import org.schabi.newpipe.util.PendingIntentCompat;
5051

5152
import us.shandian.giga.postprocessing.Postprocessing;
5253
import us.shandian.giga.service.DownloadManager.NetworkState;
@@ -142,7 +143,7 @@ public void onCreate() {
142143
Intent openDownloadListIntent = new Intent(this, DownloadActivity.class)
143144
.setAction(Intent.ACTION_MAIN);
144145

145-
mOpenDownloadList = PendingIntent.getActivity(this, 0,
146+
mOpenDownloadList = PendingIntentCompat.getActivity(this, 0,
146147
openDownloadListIntent,
147148
PendingIntent.FLAG_UPDATE_CURRENT);
148149

@@ -484,7 +485,8 @@ public void notifyFailedDownload(DownloadMission mission) {
484485

485486
private PendingIntent makePendingIntent(String action) {
486487
Intent intent = new Intent(this, DownloadManagerService.class).setAction(action);
487-
return PendingIntent.getService(this, intent.hashCode(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
488+
return PendingIntentCompat.getService(this, intent.hashCode(), intent,
489+
PendingIntent.FLAG_UPDATE_CURRENT);
488490
}
489491

490492
private void manageLock(boolean acquire) {

0 commit comments

Comments
 (0)