Merge pull request #9333 from Isira-Seneviratne/PendingIntent_mutability

Make PendingIntents immutable on Android 6.0 and later.
This commit is contained in:
Isira Seneviratne 2022-11-09 08:58:16 +05:30 committed by GitHub
commit 95a65d5704
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 89 additions and 30 deletions

View File

@ -1,6 +1,5 @@
package org.schabi.newpipe package org.schabi.newpipe
import android.app.PendingIntent
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.util.Log import android.util.Log
@ -18,6 +17,7 @@ import com.grack.nanojson.JsonParser
import com.grack.nanojson.JsonParserException import com.grack.nanojson.JsonParserException
import org.schabi.newpipe.extractor.downloader.Response import org.schabi.newpipe.extractor.downloader.Response
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException import org.schabi.newpipe.extractor.exceptions.ReCaptchaException
import org.schabi.newpipe.util.PendingIntentCompat
import org.schabi.newpipe.util.ReleaseVersionUtil.coerceUpdateCheckExpiry import org.schabi.newpipe.util.ReleaseVersionUtil.coerceUpdateCheckExpiry
import org.schabi.newpipe.util.ReleaseVersionUtil.isLastUpdateCheckExpired import org.schabi.newpipe.util.ReleaseVersionUtil.isLastUpdateCheckExpired
import org.schabi.newpipe.util.ReleaseVersionUtil.isReleaseApk import org.schabi.newpipe.util.ReleaseVersionUtil.isReleaseApk
@ -49,7 +49,7 @@ class NewVersionWorker(
// A pending intent to open the apk location url in the browser. // A pending intent to open the apk location url in the browser.
val intent = Intent(Intent.ACTION_VIEW, apkLocationUrl?.toUri()) val intent = Intent(Intent.ACTION_VIEW, apkLocationUrl?.toUri())
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val pendingIntent = PendingIntent.getActivity(app, 0, intent, 0) val pendingIntent = PendingIntentCompat.getActivity(app, 0, intent, 0)
val channelId = app.getString(R.string.app_update_notification_channel_id) val channelId = app.getString(R.string.app_update_notification_channel_id)
val notificationBuilder = NotificationCompat.Builder(app, channelId) val notificationBuilder = NotificationCompat.Builder(app, channelId)
.setSmallIcon(R.drawable.ic_newpipe_update) .setSmallIcon(R.drawable.ic_newpipe_update)

View File

@ -5,7 +5,6 @@ import android.app.PendingIntent
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.graphics.Color import android.graphics.Color
import android.os.Build
import android.view.View import android.view.View
import android.widget.Toast import android.widget.Toast
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
@ -13,6 +12,7 @@ import androidx.core.app.NotificationManagerCompat
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import org.schabi.newpipe.R import org.schabi.newpipe.R
import org.schabi.newpipe.util.PendingIntentCompat
/** /**
* This class contains all of the methods that should be used to let the user know that an error has * 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 {
*/ */
@JvmStatic @JvmStatic
fun createNotification(context: Context, errorInfo: ErrorInfo) { fun createNotification(context: Context, errorInfo: ErrorInfo) {
var pendingIntentFlags = PendingIntent.FLAG_UPDATE_CURRENT
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
pendingIntentFlags = pendingIntentFlags or PendingIntent.FLAG_IMMUTABLE
}
val notificationBuilder: NotificationCompat.Builder = val notificationBuilder: NotificationCompat.Builder =
NotificationCompat.Builder( NotificationCompat.Builder(
context, context,
@ -119,11 +114,11 @@ class ErrorUtil {
.setContentText(context.getString(errorInfo.messageStringId)) .setContentText(context.getString(errorInfo.messageStringId))
.setAutoCancel(true) .setAutoCancel(true)
.setContentIntent( .setContentIntent(
PendingIntent.getActivity( PendingIntentCompat.getActivity(
context, context,
0, 0,
getErrorActivityIntent(context, errorInfo), getErrorActivityIntent(context, errorInfo),
pendingIntentFlags PendingIntent.FLAG_UPDATE_CURRENT
) )
) )

View File

@ -1,7 +1,6 @@
package org.schabi.newpipe.local.feed.notifications package org.schabi.newpipe.local.feed.notifications
import android.app.NotificationManager import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.graphics.Bitmap import android.graphics.Bitmap
@ -20,6 +19,7 @@ import org.schabi.newpipe.extractor.stream.StreamInfoItem
import org.schabi.newpipe.local.feed.service.FeedUpdateInfo import org.schabi.newpipe.local.feed.service.FeedUpdateInfo
import org.schabi.newpipe.util.Localization import org.schabi.newpipe.util.Localization
import org.schabi.newpipe.util.NavigationHelper import org.schabi.newpipe.util.NavigationHelper
import org.schabi.newpipe.util.PendingIntentCompat
import org.schabi.newpipe.util.PicassoHelper import org.schabi.newpipe.util.PicassoHelper
/** /**
@ -70,16 +70,13 @@ class NotificationHelper(val context: Context) {
// open the channel page when clicking on the notification // open the channel page when clicking on the notification
builder.setContentIntent( builder.setContentIntent(
PendingIntent.getActivity( PendingIntentCompat.getActivity(
context, context,
data.pseudoId, data.pseudoId,
NavigationHelper NavigationHelper
.getChannelIntent(context, data.listInfo.serviceId, data.listInfo.url) .getChannelIntent(context, data.listInfo.serviceId, data.listInfo.url)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 0
PendingIntent.FLAG_IMMUTABLE
else
0
) )
) )

View File

@ -19,7 +19,6 @@
package org.schabi.newpipe.local.feed.service package org.schabi.newpipe.local.feed.service
import android.app.PendingIntent
import android.app.Service import android.app.Service
import android.content.BroadcastReceiver import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
@ -43,6 +42,7 @@ import org.schabi.newpipe.extractor.ListInfo
import org.schabi.newpipe.extractor.stream.StreamInfoItem import org.schabi.newpipe.extractor.stream.StreamInfoItem
import org.schabi.newpipe.local.feed.service.FeedEventManager.Event.ErrorResultEvent import org.schabi.newpipe.local.feed.service.FeedEventManager.Event.ErrorResultEvent
import org.schabi.newpipe.local.feed.service.FeedEventManager.postEvent import org.schabi.newpipe.local.feed.service.FeedEventManager.postEvent
import org.schabi.newpipe.util.PendingIntentCompat
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
class FeedLoadService : Service() { class FeedLoadService : Service() {
@ -152,12 +152,8 @@ class FeedLoadService : Service() {
private lateinit var notificationBuilder: NotificationCompat.Builder private lateinit var notificationBuilder: NotificationCompat.Builder
private fun createNotification(): NotificationCompat.Builder { private fun createNotification(): NotificationCompat.Builder {
val cancelActionIntent = PendingIntent.getBroadcast( val cancelActionIntent =
this, PendingIntentCompat.getBroadcast(this, NOTIFICATION_ID, Intent(ACTION_CANCEL), 0)
NOTIFICATION_ID,
Intent(ACTION_CANCEL),
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else 0
)
return NotificationCompat.Builder(this, getString(R.string.notification_channel_id)) return NotificationCompat.Builder(this, getString(R.string.notification_channel_id))
.setOngoing(true) .setOngoing(true)

View File

@ -1,7 +1,6 @@
package org.schabi.newpipe.player.notification; package org.schabi.newpipe.player.notification;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.content.Intent; import android.content.Intent;
import android.content.pm.ServiceInfo; import android.content.pm.ServiceInfo;
import android.graphics.Bitmap; import android.graphics.Bitmap;
@ -22,6 +21,7 @@ import org.schabi.newpipe.R;
import org.schabi.newpipe.player.Player; import org.schabi.newpipe.player.Player;
import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi; import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi;
import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.PendingIntentCompat;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -133,8 +133,8 @@ public final class NotificationUtil {
R.color.dark_background_color)) R.color.dark_background_color))
.setColorized(player.getPrefs().getBoolean( .setColorized(player.getPrefs().getBoolean(
player.getContext().getString(R.string.notification_colorize_key), true)) player.getContext().getString(R.string.notification_colorize_key), true))
.setDeleteIntent(PendingIntent.getBroadcast(player.getContext(), NOTIFICATION_ID, .setDeleteIntent(PendingIntentCompat.getBroadcast(player.getContext(),
new Intent(ACTION_CLOSE), FLAG_UPDATE_CURRENT)); NOTIFICATION_ID, new Intent(ACTION_CLOSE), FLAG_UPDATE_CURRENT));
// set the initial value for the video thumbnail, updatable with updateNotificationThumbnail // set the initial value for the video thumbnail, updatable with updateNotificationThumbnail
setLargeIcon(builder); setLargeIcon(builder);
@ -151,7 +151,7 @@ public final class NotificationUtil {
} }
// also update content intent, in case the user switched players // also update content intent, in case the user switched players
notificationBuilder.setContentIntent(PendingIntent.getActivity(player.getContext(), notificationBuilder.setContentIntent(PendingIntentCompat.getActivity(player.getContext(),
NOTIFICATION_ID, getIntentForNotification(), FLAG_UPDATE_CURRENT)); NOTIFICATION_ID, getIntentForNotification(), FLAG_UPDATE_CURRENT));
notificationBuilder.setContentTitle(player.getVideoTitle()); notificationBuilder.setContentTitle(player.getVideoTitle());
notificationBuilder.setContentText(player.getUploaderName()); notificationBuilder.setContentText(player.getUploaderName());
@ -334,7 +334,7 @@ public final class NotificationUtil {
@StringRes final int title, @StringRes final int title,
final String intentAction) { final String intentAction) {
return new NotificationCompat.Action(drawable, player.getContext().getString(title), return new NotificationCompat.Action(drawable, player.getContext().getString(title),
PendingIntent.getBroadcast(player.getContext(), NOTIFICATION_ID, PendingIntentCompat.getBroadcast(player.getContext(), NOTIFICATION_ID,
new Intent(intentAction), FLAG_UPDATE_CURRENT)); new Intent(intentAction), FLAG_UPDATE_CURRENT));
} }

View File

@ -0,0 +1,69 @@
package org.schabi.newpipe.util;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import androidx.annotation.NonNull;
public final class PendingIntentCompat {
private PendingIntentCompat() {
}
private static int addImmutableFlag(final int flags) {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
? flags | PendingIntent.FLAG_IMMUTABLE : flags;
}
/**
* Creates a {@link PendingIntent} to start an activity. It is immutable on API level 23 and
* greater.
*
* @param context The context in which the activity should be started.
* @param requestCode The request code
* @param intent The Intent of the activity to be launched.
* @param flags The flags for the intent.
* @return The pending intent.
* @see PendingIntent#getActivity(Context, int, Intent, int)
*/
@NonNull
public static PendingIntent getActivity(@NonNull final Context context, final int requestCode,
@NonNull final Intent intent, final int flags) {
return PendingIntent.getActivity(context, requestCode, intent, addImmutableFlag(flags));
}
/**
* Creates a {@link PendingIntent} to start a service. It is immutable on API level 23 and
* greater.
*
* @param context The context in which the service should be started.
* @param requestCode The request code
* @param intent The Intent of the service to be launched.
* @param flags The flags for the intent.
* @return The pending intent.
* @see PendingIntent#getService(Context, int, Intent, int)
*/
@NonNull
public static PendingIntent getService(@NonNull final Context context, final int requestCode,
@NonNull final Intent intent, final int flags) {
return PendingIntent.getService(context, requestCode, intent, addImmutableFlag(flags));
}
/**
* Creates a {@link PendingIntent} to perform a broadcast. It is immutable on API level 23 and
* greater.
*
* @param context The context in which the broadcast should be performed.
* @param requestCode The request code
* @param intent The Intent to be broadcast.
* @param flags The flags for the intent.
* @return The pending intent.
* @see PendingIntent#getBroadcast(Context, int, Intent, int)
*/
@NonNull
public static PendingIntent getBroadcast(@NonNull final Context context, final int requestCode,
@NonNull final Intent intent, final int flags) {
return PendingIntent.getBroadcast(context, requestCode, intent, addImmutableFlag(flags));
}
}

View File

@ -47,6 +47,7 @@ import us.shandian.giga.get.MissionRecoveryInfo;
import org.schabi.newpipe.streams.io.StoredDirectoryHelper; import org.schabi.newpipe.streams.io.StoredDirectoryHelper;
import org.schabi.newpipe.streams.io.StoredFileHelper; import org.schabi.newpipe.streams.io.StoredFileHelper;
import org.schabi.newpipe.util.Localization; import org.schabi.newpipe.util.Localization;
import org.schabi.newpipe.util.PendingIntentCompat;
import us.shandian.giga.postprocessing.Postprocessing; import us.shandian.giga.postprocessing.Postprocessing;
import us.shandian.giga.service.DownloadManager.NetworkState; import us.shandian.giga.service.DownloadManager.NetworkState;
@ -142,7 +143,7 @@ public class DownloadManagerService extends Service {
Intent openDownloadListIntent = new Intent(this, DownloadActivity.class) Intent openDownloadListIntent = new Intent(this, DownloadActivity.class)
.setAction(Intent.ACTION_MAIN); .setAction(Intent.ACTION_MAIN);
mOpenDownloadList = PendingIntent.getActivity(this, 0, mOpenDownloadList = PendingIntentCompat.getActivity(this, 0,
openDownloadListIntent, openDownloadListIntent,
PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent.FLAG_UPDATE_CURRENT);
@ -484,7 +485,8 @@ public class DownloadManagerService extends Service {
private PendingIntent makePendingIntent(String action) { private PendingIntent makePendingIntent(String action) {
Intent intent = new Intent(this, DownloadManagerService.class).setAction(action); Intent intent = new Intent(this, DownloadManagerService.class).setAction(action);
return PendingIntent.getService(this, intent.hashCode(), intent, PendingIntent.FLAG_UPDATE_CURRENT); return PendingIntentCompat.getService(this, intent.hashCode(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);
} }
private void manageLock(boolean acquire) { private void manageLock(boolean acquire) {