Skip to content

Commit

Permalink
Use PendingIntentCompat for mutable flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mar-v-in committed Mar 21, 2024
1 parent ae30bab commit 5528a0f
Show file tree
Hide file tree
Showing 16 changed files with 34 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import android.telephony.SmsMessage
import android.text.TextUtils
import android.util.Base64
import android.util.Log
import androidx.core.app.PendingIntentCompat
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import androidx.lifecycle.DefaultLifecycleObserver
Expand Down Expand Up @@ -160,7 +161,7 @@ class SmsRetrieverCore(private val context: Context, override val lifecycle: Lif
private fun getTimeoutPendingIntent(context: Context, packageName: String): PendingIntent {
val intent = Intent(ACTION_SMS_RETRIEVE_TIMEOUT)
intent.setPackage(packageName)
return PendingIntent.getBroadcast(context, ++requestCode, intent, PendingIntent.FLAG_IMMUTABLE)
return PendingIntentCompat.getBroadcast(context, ++requestCode, intent, 0, false)!!
}

private fun tryHandleIncomingMessageAsRetrieverMessage(message: SmsMessage): Boolean {
Expand Down
1 change: 1 addition & 0 deletions play-services-base/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies {

implementation "androidx.annotation:annotation:$annotationVersion"
implementation "androidx.appcompat:appcompat:$appcompatVersion"
implementation "androidx.core:core-ktx:$coreVersion"
implementation "androidx.lifecycle:lifecycle-service:$lifecycleVersion"
implementation "androidx.navigation:navigation-fragment-ktx:$navigationVersion"
implementation "androidx.navigation:navigation-ui-ktx:$navigationVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ package org.microg.gms.utils

import android.app.AlarmManager
import android.app.AlarmManager.ELAPSED_REALTIME_WAKEUP
import android.app.PendingIntent
import android.app.PendingIntent.FLAG_NO_CREATE
import android.app.PendingIntent.FLAG_MUTABLE
import android.app.PendingIntent.FLAG_UPDATE_CURRENT
import android.app.Service
import android.content.Context
Expand All @@ -18,6 +16,7 @@ import android.os.Build.VERSION.SDK_INT
import android.os.Parcelable
import android.os.SystemClock
import android.util.Log
import androidx.core.app.PendingIntentCompat
import androidx.core.content.getSystemService
import java.util.UUID

Expand All @@ -29,7 +28,7 @@ class IntentCacheManager<S : Service, T : Parcelable>(private val context: Conte
private val pendingActions: MutableList<() -> Unit> = arrayListOf()

init {
val pendingIntent = PendingIntent.getService(context, type, getIntent(), if (SDK_INT >= 31) FLAG_MUTABLE else 0)
val pendingIntent = PendingIntentCompat.getService(context, type, getIntent(), 0, true)!!
val alarmManager = context.getSystemService<AlarmManager>()
if (SDK_INT >= 19) {
alarmManager?.setWindow(ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + TEN_YEARS, -1, pendingIntent)
Expand Down Expand Up @@ -116,7 +115,7 @@ class IntentCacheManager<S : Service, T : Parcelable>(private val context: Conte
putExtra(EXTRA_ID, id)
putParcelableArrayListExtra(EXTRA_DATA, content)
}
val pendingIntent = PendingIntent.getService(context, type, intent, FLAG_NO_CREATE or FLAG_UPDATE_CURRENT or if (SDK_INT >= 31) FLAG_MUTABLE else 0)
val pendingIntent = PendingIntentCompat.getService(context, type, intent, FLAG_NO_CREATE or FLAG_UPDATE_CURRENT, true)
if (pendingIntent == null) {
Log.w(TAG, "Failed to update existing pending intent, will likely have a loss of information")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.content.Context
import android.content.Intent
import android.os.Parcel
import android.util.Log
import androidx.core.app.PendingIntentCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
Expand Down Expand Up @@ -51,10 +52,10 @@ class GamesConnectServiceImpl(val context: Context, override val lifecycle: Life
override fun signIn(callback: IGamesConnectCallbacks?, request: GamesSignInRequest?) {
Log.d(TAG, "signIn($request)")
fun sendSignInRequired() {
val resolution = PendingIntent.getActivity(context, packageName.hashCode(), Intent(context, GamesSignInActivity::class.java).apply {
val resolution = PendingIntentCompat.getActivity(context, packageName.hashCode(), Intent(context, GamesSignInActivity::class.java).apply {
putExtra(EXTRA_GAME_PACKAGE_NAME, packageName)
putExtra(EXTRA_SCOPES, arrayOf(Scopes.GAMES_LITE))
}, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
}, PendingIntent.FLAG_UPDATE_CURRENT, false)
when (request?.signInType) {
0 -> { // Manual sign in, provide resolution
callback?.onSignIn(Status(CommonStatusCodes.SIGN_IN_REQUIRED, null, resolution), null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import android.os.Bundle
import android.os.IBinder
import android.os.Parcel
import android.util.Log
import androidx.core.app.PendingIntentCompat
import androidx.core.os.bundleOf
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
Expand Down Expand Up @@ -55,15 +56,16 @@ class GamesService : BaseService(TAG, GmsService.GAMES) {
Log.d(TAG, "Sending SIGN_IN_REQUIRED to $packageName")
callback.onPostInitCompleteWithConnectionInfo(ConnectionResult.SIGN_IN_REQUIRED, null, ConnectionInfo().apply {
params = bundleOf(
"pendingIntent" to PendingIntent.getActivity(
"pendingIntent" to PendingIntentCompat.getActivity(
this@GamesService,
packageName.hashCode(),
Intent(this@GamesService, GamesSignInActivity::class.java).apply {
putExtra(EXTRA_GAME_PACKAGE_NAME, request.packageName)
putExtra(EXTRA_ACCOUNT, request.account)
putExtra(EXTRA_SCOPES, request.scopes)
},
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
PendingIntent.FLAG_UPDATE_CURRENT,
false
)
)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
package org.microg.gms.fido.core.privileged

import android.app.KeyguardManager
import android.app.PendingIntent
import android.app.PendingIntent.FLAG_IMMUTABLE
import android.app.PendingIntent.FLAG_UPDATE_CURRENT
import android.content.Context
import android.content.Context.KEYGUARD_SERVICE
import android.content.Intent
import android.os.Build.VERSION.SDK_INT
import android.os.Parcel
import androidx.core.app.PendingIntentCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
Expand Down Expand Up @@ -70,7 +69,7 @@ class Fido2PrivilegedServiceImpl(private val context: Context, override val life
.putExtra(KEY_OPTIONS, options.serializeToBytes())

val pendingIntent =
PendingIntent.getActivity(context, options.hashCode(), intent, FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE)
PendingIntentCompat.getActivity(context, options.hashCode(), intent, FLAG_UPDATE_CURRENT, false)
callbacks.onPendingIntent(Status.SUCCESS, pendingIntent)
}
}
Expand All @@ -84,7 +83,7 @@ class Fido2PrivilegedServiceImpl(private val context: Context, override val life
.putExtra(KEY_OPTIONS, options.serializeToBytes())

val pendingIntent =
PendingIntent.getActivity(context, options.hashCode(), intent, FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE)
PendingIntentCompat.getActivity(context, options.hashCode(), intent, FLAG_UPDATE_CURRENT, false)
callbacks.onPendingIntent(Status.SUCCESS, pendingIntent)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import android.nfc.Tag
import android.nfc.tech.IsoDep
import android.util.Log
import androidx.core.app.OnNewIntentProvider
import androidx.core.app.PendingIntentCompat
import androidx.core.util.Consumer
import com.google.android.gms.fido.fido2.api.common.AuthenticatorAssertionResponse
import com.google.android.gms.fido.fido2.api.common.AuthenticatorAttestationResponse
Expand All @@ -36,7 +37,7 @@ class NfcTransportHandler(private val activity: Activity, callback: TransportHan

private suspend fun waitForNewNfcTag(adapter: NfcAdapter): Tag {
val intent = Intent(activity, activity.javaClass).apply { addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP) }
val pendingIntent: PendingIntent = PendingIntent.getActivity(activity, 0, intent, PendingIntent.FLAG_MUTABLE)
val pendingIntent: PendingIntent = PendingIntentCompat.getActivity(activity, 0, intent, 0, true)!!
adapter.enableForegroundDispatch(
activity,
pendingIntent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.content.IntentFilter
import android.hardware.usb.UsbDevice
import android.hardware.usb.UsbManager
import android.os.Build.VERSION.SDK_INT
import androidx.core.app.PendingIntentCompat
import kotlinx.coroutines.CompletableDeferred

private val Context.usbPermissionCallbackAction
Expand Down Expand Up @@ -75,7 +76,7 @@ class UsbDevicePermissionManager(private val context: Context) {
val res = CompletableDeferred<Boolean>()
if (UsbDevicePermissionReceiver.addDeferred(device, res)) {
UsbDevicePermissionReceiver.register(context)
val intent = PendingIntent.getBroadcast(context, 0, Intent(context.usbPermissionCallbackAction).apply { `package` = context.packageName }, if (SDK_INT >= 31) PendingIntent.FLAG_MUTABLE else 0)
val intent = PendingIntentCompat.getBroadcast(context, 0, Intent(context.usbPermissionCallbackAction).apply { `package` = context.packageName }, 0, true)
context.usbManager?.requestPermission(device, intent)
}
return res.await()
Expand Down
1 change: 1 addition & 0 deletions play-services-location/core/provider/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies {
implementation project(':play-services-base-core')
implementation project(':play-services-location-core-base')

implementation "androidx.core:core-ktx:$coreVersion"
implementation "androidx.lifecycle:lifecycle-service:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycleVersion"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package org.microg.gms.location.provider

import android.app.PendingIntent
import android.app.PendingIntent.FLAG_MUTABLE
import android.app.PendingIntent.FLAG_UPDATE_CURRENT
import android.content.Context
import android.content.Intent
Expand All @@ -15,6 +14,7 @@ import android.location.Location
import android.os.Build.VERSION.SDK_INT
import android.os.WorkSource
import androidx.annotation.RequiresApi
import androidx.core.app.PendingIntentCompat
import com.android.location.provider.ProviderPropertiesUnbundled
import com.android.location.provider.ProviderRequestUnbundled
import org.microg.gms.location.*
Expand Down Expand Up @@ -86,7 +86,7 @@ class NetworkLocationProviderPreTiramisu : AbstractLocationProviderPreTiramisu {
if (enabled) throw IllegalStateException()
val intent = Intent(context, NetworkLocationProviderService::class.java)
intent.action = ACTION_REPORT_LOCATION
pendingIntent = PendingIntent.getService(context, 0, intent, (if (SDK_INT >= 31) FLAG_MUTABLE else 0) or FLAG_UPDATE_CURRENT)
pendingIntent = PendingIntentCompat.getService(context, 0, intent, FLAG_UPDATE_CURRENT, true)
currentRequest = null
enabled = true
when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ package org.microg.gms.nearby.exposurenotification
import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.app.AlarmManager
import android.app.PendingIntent
import android.app.PendingIntent.FLAG_IMMUTABLE
import android.app.PendingIntent.FLAG_ONE_SHOT
import android.app.PendingIntent.FLAG_UPDATE_CURRENT
import android.bluetooth.BluetoothAdapter.*
Expand All @@ -24,6 +22,7 @@ import android.os.Handler
import android.os.Looper
import android.os.SystemClock
import android.util.Log
import androidx.core.app.PendingIntentCompat
import androidx.lifecycle.LifecycleService
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -199,7 +198,7 @@ class AdvertiserService : LifecycleService() {

private fun scheduleRestartAdvertising(nextSend: Long) {
val intent = Intent(this, AdvertiserService::class.java).apply { action = ACTION_RESTART_ADVERTISING }
val pendingIntent = PendingIntent.getService(this, ACTION_RESTART_ADVERTISING.hashCode(), intent, FLAG_ONE_SHOT or FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE)
val pendingIntent = PendingIntentCompat.getService(this, ACTION_RESTART_ADVERTISING.hashCode(), intent, FLAG_ONE_SHOT or FLAG_UPDATE_CURRENT, false)!!
when {
SDK_INT >= 23 ->
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + nextSend, pendingIntent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
package org.microg.gms.nearby.exposurenotification

import android.app.AlarmManager
import android.app.PendingIntent
import android.app.PendingIntent.FLAG_IMMUTABLE
import android.app.PendingIntent.FLAG_ONE_SHOT
import android.app.PendingIntent.FLAG_UPDATE_CURRENT
import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.core.app.PendingIntentCompat
import androidx.lifecycle.LifecycleService
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -50,7 +49,7 @@ class CleanupService : LifecycleService() {

fun stop() {
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val pendingIntent = PendingIntent.getService(applicationContext, CleanupService::class.java.name.hashCode(), Intent(applicationContext, CleanupService::class.java), FLAG_ONE_SHOT or FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE)
val pendingIntent = PendingIntentCompat.getService(applicationContext, CleanupService::class.java.name.hashCode(), Intent(applicationContext, CleanupService::class.java), FLAG_ONE_SHOT or FLAG_UPDATE_CURRENT, false)!!
alarmManager.set(AlarmManager.RTC, ExposurePreferences(this).lastCleanup + CLEANUP_INTERVAL, pendingIntent)
stopSelf()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import android.location.LocationManager
import android.os.*
import android.util.Base64
import android.util.Log
import androidx.core.app.PendingIntentCompat
import androidx.core.location.LocationManagerCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleCoroutineScope
Expand Down Expand Up @@ -113,7 +114,7 @@ class ExposureNotificationServiceImpl(private val context: Context, override val
Log.w(TAG, e)
}
Log.d(TAG, "Pending: $intent")
val pi = PendingIntent.getActivity(context, permission.hashCode(), intent, PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE)
val pi = PendingIntentCompat.getActivity(context, permission.hashCode(), intent, PendingIntent.FLAG_ONE_SHOT, false)!!
Log.d(TAG, "Pending: $pi")
return pi
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package org.microg.gms.nearby.exposurenotification

import android.annotation.TargetApi
import android.app.*
import android.app.PendingIntent.FLAG_IMMUTABLE
import android.app.PendingIntent.FLAG_UPDATE_CURRENT
import android.bluetooth.BluetoothAdapter
import android.content.BroadcastReceiver
Expand All @@ -22,6 +21,7 @@ import android.util.Log
import android.util.TypedValue
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.app.PendingIntentCompat
import androidx.core.content.ContextCompat
import androidx.core.location.LocationManagerCompat
import androidx.lifecycle.LifecycleService
Expand Down Expand Up @@ -99,7 +99,7 @@ class NotifyService : LifecycleService() {
try {
val intent = Intent(ExposureNotificationClient.ACTION_EXPOSURE_NOTIFICATION_SETTINGS).apply { `package` = packageName }
intent.resolveActivity(packageManager)
setContentIntent(PendingIntent.getActivity(this@NotifyService, notificationId, Intent(ExposureNotificationClient.ACTION_EXPOSURE_NOTIFICATION_SETTINGS).apply { `package` = packageName }, FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE))
setContentIntent(PendingIntentCompat.getActivity(this@NotifyService, notificationId, Intent(ExposureNotificationClient.ACTION_EXPOSURE_NOTIFICATION_SETTINGS).apply { `package` = packageName }, FLAG_UPDATE_CURRENT, false))
} catch (e: Exception) {
// Ignore
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ package org.microg.gms.nearby.exposurenotification
import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.app.AlarmManager
import android.app.PendingIntent
import android.app.PendingIntent.FLAG_IMMUTABLE
import android.app.PendingIntent.FLAG_ONE_SHOT
import android.app.PendingIntent.FLAG_UPDATE_CURRENT
import android.bluetooth.BluetoothAdapter.*
Expand All @@ -20,6 +18,7 @@ import android.content.Intent
import android.content.IntentFilter
import android.os.*
import android.util.Log
import androidx.core.app.PendingIntentCompat
import androidx.lifecycle.LifecycleService
import androidx.lifecycle.lifecycleScope
import org.microg.gms.common.ForegroundServiceContext
Expand Down Expand Up @@ -161,7 +160,7 @@ class ScannerService : LifecycleService() {

private fun scheduleStartScan(nextScan: Long) {
val intent = Intent(this, ScannerService::class.java)
val pendingIntent = PendingIntent.getService(this, ScannerService::class.java.hashCode(), intent, FLAG_ONE_SHOT or FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE)
val pendingIntent = PendingIntentCompat.getService(this, ScannerService::class.java.hashCode(), intent, FLAG_ONE_SHOT or FLAG_UPDATE_CURRENT, false)!!
if (Build.VERSION.SDK_INT >= 23) {
// Note: there is no setWindowAndAllowWhileIdle()
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + nextScan, pendingIntent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public Task<TResult> addOnCompleteListener(Executor executor, OnCompleteListener
public abstract Task<TResult> addOnFailureListener(Activity activity, OnFailureListener listener);

/**
* Adds an Activity-scoped listener that is called if the Task fails.
* Adds a listener that is called if the Task fails.
* <p/>
* The listener will be called on main application thread. If the Task has already failed, a
* call to the listener will be immediately scheduled. If multiple listeners are added, they
Expand Down

0 comments on commit 5528a0f

Please sign in to comment.