Switch to AndroidX's PendingIntentCompat class.

This commit is contained in:
Isira Seneviratne 2023-04-09 17:36:28 +05:30
parent d3d2549005
commit 2ab3f5c0d8
5 changed files with 24 additions and 75 deletions

View File

@ -1,30 +0,0 @@
package com.github.libretube.compat
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
// TODO: Use AndroidX's PendingIntentCompat class instead once it becomes available.
object PendingIntentCompat {
private fun addImmutabilityFlag(flags: Int): Int {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
flags or PendingIntent.FLAG_IMMUTABLE
} else {
flags
}
}
fun getActivity(context: Context, requestCode: Int, intent: Intent, flags: Int): PendingIntent {
return PendingIntent.getActivity(context, requestCode, intent, addImmutabilityFlag(flags))
}
fun getBroadcast(
context: Context,
requestCode: Int,
intent: Intent,
flags: Int
): PendingIntent {
return PendingIntent.getBroadcast(context, requestCode, intent, addImmutabilityFlag(flags))
}
}

View File

@ -8,13 +8,13 @@ import android.content.pm.ActivityInfo
import android.view.accessibility.CaptioningManager
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.core.app.PendingIntentCompat
import androidx.core.app.RemoteActionCompat
import androidx.core.content.getSystemService
import androidx.core.graphics.drawable.IconCompat
import com.github.libretube.R
import com.github.libretube.api.obj.PipedStream
import com.github.libretube.api.obj.Segment
import com.github.libretube.compat.PendingIntentCompat
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.enums.AudioQuality
import com.github.libretube.enums.PlayerEvent
@ -378,12 +378,8 @@ object PlayerHelper {
}
private fun getPendingIntent(activity: Activity, code: Int): PendingIntent {
return PendingIntentCompat.getBroadcast(
activity,
code,
Intent(getIntentActon(activity)).putExtra(CONTROL_TYPE, code),
0
)
val intent = Intent(getIntentActon(activity)).putExtra(CONTROL_TYPE, code)
return PendingIntentCompat.getBroadcast(activity, code, intent, 0, false)
}
private fun getRemoteAction(

View File

@ -1,12 +1,14 @@
package com.github.libretube.services
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.PendingIntent.FLAG_CANCEL_CURRENT
import android.app.PendingIntent.FLAG_UPDATE_CURRENT
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import android.util.SparseBooleanArray
import androidx.core.app.NotificationCompat
import androidx.core.app.PendingIntentCompat
import androidx.core.app.ServiceCompat
import androidx.core.content.getSystemService
import androidx.core.util.set
@ -16,7 +18,6 @@ import androidx.lifecycle.lifecycleScope
import com.github.libretube.R
import com.github.libretube.api.CronetHelper
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.compat.PendingIntentCompat
import com.github.libretube.constants.DOWNLOAD_CHANNEL_ID
import com.github.libretube.constants.DOWNLOAD_PROGRESS_NOTIFICATION_ID
import com.github.libretube.constants.IntentData
@ -358,13 +359,10 @@ class DownloadService : LifecycleService() {
}
private fun getNotificationBuilder(item: DownloadItem): NotificationCompat.Builder {
val activityIntent = PendingIntentCompat.getActivity(
this@DownloadService,
0,
Intent(this@DownloadService, MainActivity::class.java)
.putExtra("fragmentToOpen", "downloads"),
PendingIntent.FLAG_CANCEL_CURRENT
)
val intent = Intent(this@DownloadService, MainActivity::class.java)
.putExtra("fragmentToOpen", "downloads")
val activityIntent = PendingIntentCompat
.getActivity(this@DownloadService, 0, intent, FLAG_CANCEL_CURRENT, false)
return NotificationCompat
.Builder(this, DOWNLOAD_CHANNEL_ID)
@ -415,15 +413,14 @@ class DownloadService : LifecycleService() {
}
private fun getResumeAction(id: Int): NotificationCompat.Action {
val intent = Intent(this, NotificationReceiver::class.java).apply {
action = ACTION_DOWNLOAD_RESUME
putExtra("id", id)
}
val intent = Intent(this, NotificationReceiver::class.java)
.setAction(ACTION_DOWNLOAD_RESUME)
.putExtra("id", id)
return NotificationCompat.Action.Builder(
R.drawable.ic_play,
getString(R.string.resume),
PendingIntentCompat.getBroadcast(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT)
PendingIntentCompat.getBroadcast(this, id, intent, FLAG_UPDATE_CURRENT, false)
).build()
}
@ -435,7 +432,7 @@ class DownloadService : LifecycleService() {
return NotificationCompat.Action.Builder(
R.drawable.ic_pause,
getString(R.string.pause),
PendingIntentCompat.getBroadcast(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT)
PendingIntentCompat.getBroadcast(this, id, intent, FLAG_UPDATE_CURRENT, false)
).build()
}

View File

@ -1,8 +1,8 @@
package com.github.libretube.util
import android.annotation.SuppressLint
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.PendingIntent.FLAG_UPDATE_CURRENT
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
@ -15,13 +15,13 @@ import android.support.v4.media.session.MediaSessionCompat
import android.support.v4.media.session.PlaybackStateCompat
import androidx.annotation.DrawableRes
import androidx.core.app.NotificationCompat
import androidx.core.app.PendingIntentCompat
import androidx.core.content.getSystemService
import androidx.core.graphics.drawable.toBitmap
import androidx.core.os.bundleOf
import coil.request.ImageRequest
import com.github.libretube.R
import com.github.libretube.api.obj.Streams
import com.github.libretube.compat.PendingIntentCompat
import com.github.libretube.constants.BACKGROUND_CHANNEL_ID
import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PLAYER_NOTIFICATION_ID
@ -74,7 +74,6 @@ class NowPlayingNotification(
/**
* overrides the action when clicking the notification
*/
@SuppressLint("UnspecifiedImmutableFlag")
override fun createCurrentContentIntent(player: Player): PendingIntent {
// starts a new MainActivity Intent when the player notification is clicked
// it doesn't start a completely new MainActivity because the MainActivity's launchMode
@ -86,12 +85,7 @@ class NowPlayingNotification(
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
}
}
return PendingIntentCompat.getActivity(
context,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
)
return PendingIntentCompat.getActivity(context, 0, intent, FLAG_UPDATE_CURRENT, false)
}
/**
@ -163,12 +157,8 @@ class NowPlayingNotification(
private fun createNotificationAction(drawableRes: Int, actionName: String, instanceId: Int): NotificationCompat.Action {
val intent = Intent(actionName).setPackage(context.packageName)
val pendingIntent = PendingIntentCompat.getBroadcast(
context,
instanceId,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
)
val pendingIntent = PendingIntentCompat
.getBroadcast(context, instanceId, intent, PendingIntent.FLAG_CANCEL_CURRENT, false)
return NotificationCompat.Action.Builder(drawableRes, actionName, pendingIntent).build()
}

View File

@ -1,18 +1,18 @@
package com.github.libretube.workers
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.PendingIntent.FLAG_UPDATE_CURRENT
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.PendingIntentCompat
import androidx.core.content.getSystemService
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.github.libretube.R
import com.github.libretube.api.SubscriptionHelper
import com.github.libretube.compat.PendingIntentCompat
import com.github.libretube.constants.DOWNLOAD_PROGRESS_NOTIFICATION_ID
import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PUSH_CHANNEL_ID
@ -163,12 +163,8 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
}
}
val pendingIntent = PendingIntentCompat.getActivity(
applicationContext,
notificationId,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
)
val pendingIntent = PendingIntentCompat
.getActivity(applicationContext, notificationId, intent, FLAG_UPDATE_CURRENT, false)
val builder = NotificationCompat.Builder(applicationContext, PUSH_CHANNEL_ID)
.setContentTitle(title)