mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 16:00:31 +05:30
Merge pull request #2749 from Isira-Seneviratne/PendingIntentCompat_helpers
Add PendingIntentCompat helper methods.
This commit is contained in:
commit
7151361fdb
@ -1,20 +1,30 @@
|
||||
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 {
|
||||
val updateCurrentFlags: Int
|
||||
get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
(PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
|
||||
private fun addImmutabilityFlag(flags: Int): Int {
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
flags or PendingIntent.FLAG_IMMUTABLE
|
||||
} else {
|
||||
PendingIntent.FLAG_UPDATE_CURRENT
|
||||
flags
|
||||
}
|
||||
}
|
||||
|
||||
val cancelCurrentFlags: Int
|
||||
get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_CANCEL_CURRENT
|
||||
} else {
|
||||
PendingIntent.FLAG_CANCEL_CURRENT
|
||||
}
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
@ -367,15 +367,13 @@ class DownloadService : Service() {
|
||||
}
|
||||
|
||||
private fun getNotificationBuilder(item: DownloadItem): NotificationCompat.Builder {
|
||||
val activityIntent =
|
||||
PendingIntent.getActivity(
|
||||
this@DownloadService,
|
||||
0,
|
||||
Intent(this@DownloadService, MainActivity::class.java).apply {
|
||||
putExtra("fragmentToOpen", "downloads")
|
||||
},
|
||||
PendingIntentCompat.cancelCurrentFlags
|
||||
)
|
||||
val activityIntent = PendingIntentCompat.getActivity(
|
||||
this@DownloadService,
|
||||
0,
|
||||
Intent(this@DownloadService, MainActivity::class.java)
|
||||
.putExtra("fragmentToOpen", "downloads"),
|
||||
PendingIntent.FLAG_CANCEL_CURRENT
|
||||
)
|
||||
|
||||
return NotificationCompat
|
||||
.Builder(this, DOWNLOAD_CHANNEL_ID)
|
||||
@ -434,20 +432,19 @@ class DownloadService : Service() {
|
||||
return NotificationCompat.Action.Builder(
|
||||
R.drawable.ic_play,
|
||||
getString(R.string.resume),
|
||||
PendingIntent.getBroadcast(this, id, intent, PendingIntentCompat.updateCurrentFlags)
|
||||
PendingIntentCompat.getBroadcast(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
).build()
|
||||
}
|
||||
|
||||
private fun getPauseAction(id: Int): NotificationCompat.Action {
|
||||
val intent = Intent(this, NotificationReceiver::class.java).apply {
|
||||
action = ACTION_DOWNLOAD_PAUSE
|
||||
putExtra("id", id)
|
||||
}
|
||||
val intent = Intent(this, NotificationReceiver::class.java)
|
||||
.setAction(ACTION_DOWNLOAD_PAUSE)
|
||||
.putExtra("id", id)
|
||||
|
||||
return NotificationCompat.Action.Builder(
|
||||
R.drawable.ic_pause,
|
||||
getString(R.string.pause),
|
||||
PendingIntent.getBroadcast(this, id, intent, PendingIntentCompat.updateCurrentFlags)
|
||||
PendingIntentCompat.getBroadcast(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
).build()
|
||||
}
|
||||
|
||||
|
@ -70,7 +70,7 @@ class NowPlayingNotification(
|
||||
* overrides the action when clicking the notification
|
||||
*/
|
||||
@SuppressLint("UnspecifiedImmutableFlag")
|
||||
override fun createCurrentContentIntent(player: Player): PendingIntent? {
|
||||
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
|
||||
// is set to "singleTop" in the AndroidManifest (important!!!)
|
||||
@ -81,11 +81,11 @@ class NowPlayingNotification(
|
||||
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
|
||||
}
|
||||
}
|
||||
return PendingIntent.getActivity(
|
||||
return PendingIntentCompat.getActivity(
|
||||
context,
|
||||
0,
|
||||
intent,
|
||||
PendingIntentCompat.updateCurrentFlags
|
||||
PendingIntent.FLAG_UPDATE_CURRENT
|
||||
)
|
||||
}
|
||||
|
||||
@ -155,12 +155,12 @@ class NowPlayingNotification(
|
||||
}
|
||||
|
||||
private fun createNotificationAction(drawableRes: Int, actionName: String, instanceId: Int): NotificationCompat.Action {
|
||||
val intent: Intent = Intent(actionName).setPackage(context.packageName)
|
||||
val pendingIntent = PendingIntent.getBroadcast(
|
||||
val intent = Intent(actionName).setPackage(context.packageName)
|
||||
val pendingIntent = PendingIntentCompat.getBroadcast(
|
||||
context,
|
||||
instanceId,
|
||||
intent,
|
||||
PendingIntentCompat.cancelCurrentFlags
|
||||
PendingIntent.FLAG_CANCEL_CURRENT
|
||||
)
|
||||
return NotificationCompat.Action.Builder(drawableRes, actionName, pendingIntent).build()
|
||||
}
|
||||
|
@ -358,22 +358,14 @@ object PlayerHelper {
|
||||
return context.packageName + "." + ACTION_MEDIA_CONTROL
|
||||
}
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.O)
|
||||
private fun getPendingIntent(activity: Activity, code: Int): PendingIntent {
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
PendingIntent.getBroadcast(
|
||||
activity,
|
||||
code,
|
||||
Intent(getIntentActon(activity)).putExtra(CONTROL_TYPE, code),
|
||||
PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
} else {
|
||||
PendingIntent.getBroadcast(
|
||||
activity,
|
||||
code,
|
||||
Intent(getIntentActon(activity)).putExtra(CONTROL_TYPE, code),
|
||||
0
|
||||
)
|
||||
}
|
||||
return PendingIntent.getBroadcast(
|
||||
activity,
|
||||
code,
|
||||
Intent(getIntentActon(activity)).putExtra(CONTROL_TYPE, code),
|
||||
PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
}
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.O)
|
||||
|
@ -12,6 +12,7 @@ import androidx.work.Worker
|
||||
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
|
||||
@ -173,11 +174,11 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) :
|
||||
}
|
||||
}
|
||||
|
||||
val pendingIntent: PendingIntent = PendingIntent.getActivity(
|
||||
val pendingIntent = PendingIntentCompat.getActivity(
|
||||
applicationContext,
|
||||
notificationId,
|
||||
intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
PendingIntent.FLAG_UPDATE_CURRENT
|
||||
)
|
||||
|
||||
val builder = NotificationCompat.Builder(applicationContext, PUSH_CHANNEL_ID)
|
||||
|
Loading…
x
Reference in New Issue
Block a user