fix: set required (im)mutable flag when creating pending intent

When targeting SDK S+ (version 31 and above) a (im)mutable flags is required when creating a PendingInent.
This commit is contained in:
Dave Heineman 2022-02-21 15:19:34 +01:00
parent 6a6405ccdc
commit 0a48cb5016
No known key found for this signature in database
GPG Key ID: A84D988313CEEB29
2 changed files with 12 additions and 5 deletions

View File

@ -54,9 +54,10 @@ class VideoPlayActivity : CommonActivity() {
val videoPlayerFragment = val videoPlayerFragment =
fragmentManager.findFragmentById(R.id.video_player_fragment) as VideoPlayerFragment? fragmentManager.findFragmentById(R.id.video_player_fragment) as VideoPlayerFragment?
val actions = ArrayList<RemoteAction>() val actions = ArrayList<RemoteAction>()
val flags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else 0
var actionIntent = Intent(getString(R.string.app_background_audio)) var actionIntent = Intent(getString(R.string.app_background_audio))
var pendingIntent = var pendingIntent =
PendingIntent.getBroadcast(applicationContext, REQUEST_CODE, actionIntent, 0) PendingIntent.getBroadcast(applicationContext, REQUEST_CODE, actionIntent, flags)
@SuppressLint("NewApi", "LocalSuppress") var icon = Icon.createWithResource( @SuppressLint("NewApi", "LocalSuppress") var icon = Icon.createWithResource(
applicationContext, android.R.drawable.stat_sys_speakerphone applicationContext, android.R.drawable.stat_sys_speakerphone
) )
@ -65,7 +66,7 @@ class VideoPlayActivity : CommonActivity() {
actions.add(remoteAction) actions.add(remoteAction)
actionIntent = Intent(PlayerNotificationManager.ACTION_STOP) actionIntent = Intent(PlayerNotificationManager.ACTION_STOP)
pendingIntent = pendingIntent =
PendingIntent.getBroadcast(applicationContext, REQUEST_CODE, actionIntent, 0) PendingIntent.getBroadcast(applicationContext, REQUEST_CODE, actionIntent, flags)
icon = Icon.createWithResource( icon = Icon.createWithResource(
applicationContext, applicationContext,
com.google.android.exoplayer2.ui.R.drawable.exo_notification_stop com.google.android.exoplayer2.ui.R.drawable.exo_notification_stop
@ -77,7 +78,7 @@ class VideoPlayActivity : CommonActivity() {
Log.e(TAG, "setting actions with play button") Log.e(TAG, "setting actions with play button")
actionIntent = Intent(PlayerNotificationManager.ACTION_PLAY) actionIntent = Intent(PlayerNotificationManager.ACTION_PLAY)
pendingIntent = pendingIntent =
PendingIntent.getBroadcast(applicationContext, REQUEST_CODE, actionIntent, 0) PendingIntent.getBroadcast(applicationContext, REQUEST_CODE, actionIntent, flags)
icon = Icon.createWithResource( icon = Icon.createWithResource(
applicationContext, applicationContext,
com.google.android.exoplayer2.ui.R.drawable.exo_notification_play com.google.android.exoplayer2.ui.R.drawable.exo_notification_play
@ -87,7 +88,7 @@ class VideoPlayActivity : CommonActivity() {
Log.e(TAG, "setting actions with pause button") Log.e(TAG, "setting actions with pause button")
actionIntent = Intent(PlayerNotificationManager.ACTION_PAUSE) actionIntent = Intent(PlayerNotificationManager.ACTION_PAUSE)
pendingIntent = pendingIntent =
PendingIntent.getBroadcast(applicationContext, REQUEST_CODE, actionIntent, 0) PendingIntent.getBroadcast(applicationContext, REQUEST_CODE, actionIntent, flags)
icon = Icon.createWithResource( icon = Icon.createWithResource(
applicationContext, applicationContext,
com.google.android.exoplayer2.ui.R.drawable.exo_notification_pause com.google.android.exoplayer2.ui.R.drawable.exo_notification_pause

View File

@ -16,6 +16,7 @@
*/ */
package net.schueller.peertube.service package net.schueller.peertube.service
import android.annotation.SuppressLint
import android.app.Notification import android.app.Notification
import net.schueller.peertube.helper.MetaDataHelper.getMetaString import net.schueller.peertube.helper.MetaDataHelper.getMetaString
import net.schueller.peertube.model.Video.Companion.getMediaDescription import net.schueller.peertube.model.Video.Companion.getMediaDescription
@ -46,6 +47,7 @@ import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.net.Uri import android.net.Uri
import android.os.Binder import android.os.Binder
import android.os.Build
import android.util.Log import android.util.Log
import android.webkit.URLUtil import android.webkit.URLUtil
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
@ -217,11 +219,15 @@ class VideoPlayerService : Service() {
return currentVideo!!.name return currentVideo!!.name
} }
@SuppressLint("UnspecifiedImmutableFlag")
override fun createCurrentContentIntent(player: Player): PendingIntent? { override fun createCurrentContentIntent(player: Player): PendingIntent? {
val intent = Intent(context, VideoPlayActivity::class.java) val intent = Intent(context, VideoPlayActivity::class.java)
intent.putExtra(VideoListActivity.EXTRA_VIDEOID, currentVideo!!.uuid) intent.putExtra(VideoListActivity.EXTRA_VIDEOID, currentVideo!!.uuid)
return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
else
PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
} }
override fun getCurrentContentText(player: Player): CharSequence { override fun getCurrentContentText(player: Player): CharSequence {