Skip to content

Commit

Permalink
For mozilla-mobile#11666 - Use PendingUtils.defaultFlags to specify m…
Browse files Browse the repository at this point in the history
…utability
  • Loading branch information
Mugurell committed Feb 4, 2022
1 parent 618bae7 commit 0eff6a2
Show file tree
Hide file tree
Showing 20 changed files with 72 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import mozilla.components.support.base.log.logger.Logger
import mozilla.components.support.ktx.android.content.appName
import mozilla.components.support.ktx.android.notification.ChannelData
import mozilla.components.support.ktx.android.notification.ensureNotificationChannelExists
import mozilla.components.support.utils.PendingIntentUtils
import java.util.concurrent.TimeUnit

/**
Expand Down Expand Up @@ -250,7 +251,10 @@ internal class SupportedAddonsWorker(

private fun createContentIntent(): PendingIntent {
return PendingIntent.getActivity(
context, 0, onNotificationClickIntent, PendingIntent.FLAG_UPDATE_CURRENT
context,
0,
onNotificationClickIntent,
PendingIntentUtils.defaultFlags or PendingIntent.FLAG_UPDATE_CURRENT
)
}
@Suppress("MaxLineLength")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import mozilla.components.support.base.ids.SharedIdsHelper
import mozilla.components.support.base.log.logger.Logger
import mozilla.components.support.ktx.android.notification.ChannelData
import mozilla.components.support.ktx.android.notification.ensureNotificationChannelExists
import mozilla.components.support.utils.PendingIntentUtils
import mozilla.components.support.webextensions.WebExtensionSupport
import java.lang.Exception
import java.util.Date
Expand Down Expand Up @@ -333,7 +334,7 @@ class DefaultAddonUpdater(
flags = Intent.FLAG_ACTIVITY_NEW_TASK
} ?: throw IllegalStateException("Package has no launcher intent")
return PendingIntent.getActivity(
applicationContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT
applicationContext, 0, intent, PendingIntentUtils.defaultFlags or PendingIntent.FLAG_UPDATE_CURRENT
)
}

Expand All @@ -348,7 +349,12 @@ class DefaultAddonUpdater(
@VisibleForTesting
internal fun createAllowAction(ext: WebExtension, requestCode: Int): NotificationCompat.Action {
val allowIntent = createNotificationIntent(ext.id, NOTIFICATION_ACTION_ALLOW)
val allowPendingIntent = PendingIntent.getService(applicationContext, requestCode, allowIntent, 0)
val allowPendingIntent = PendingIntent.getService(
applicationContext,
requestCode,
allowIntent,
PendingIntentUtils.defaultFlags
)

val allowText =
applicationContext.getString(R.string.mozac_feature_addons_updater_notification_allow_button)
Expand All @@ -363,7 +369,12 @@ class DefaultAddonUpdater(
@VisibleForTesting
internal fun createDenyAction(ext: WebExtension, requestCode: Int): NotificationCompat.Action {
val denyIntent = createNotificationIntent(ext.id, NOTIFICATION_ACTION_DENY)
val denyPendingIntent = PendingIntent.getService(applicationContext, requestCode, denyIntent, 0)
val denyPendingIntent = PendingIntent.getService(
applicationContext,
requestCode,
denyIntent,
PendingIntentUtils.defaultFlags
)

val denyText =
applicationContext.getString(R.string.mozac_feature_addons_updater_notification_deny_button)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import mozilla.components.concept.storage.Login
import mozilla.components.feature.autofill.AutofillConfiguration
import mozilla.components.feature.autofill.handler.EXTRA_LOGIN_ID
import mozilla.components.feature.autofill.structure.ParsedStructure
import mozilla.components.support.utils.PendingIntentUtils

@RequiresApi(Build.VERSION_CODES.O)
internal data class LoginDatasetBuilder(
Expand All @@ -48,7 +49,7 @@ internal data class LoginDatasetBuilder(
context,
0,
Intent(),
PendingIntent.FLAG_CANCEL_CURRENT
PendingIntentUtils.defaultFlags or PendingIntent.FLAG_CANCEL_CURRENT
)

val usernameText = login.usernamePresentationOrFallback(context)
Expand Down Expand Up @@ -86,7 +87,7 @@ internal data class LoginDatasetBuilder(
context,
configuration.activityRequestCode + requestOffset,
confirmIntent,
PendingIntent.FLAG_CANCEL_CURRENT
PendingIntentUtils.defaultFlags or PendingIntent.FLAG_CANCEL_CURRENT
).intentSender

dataset.setAuthentication(intentSender)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import mozilla.components.feature.autofill.AutofillConfiguration
import mozilla.components.feature.autofill.R
import mozilla.components.feature.autofill.handler.MAX_LOGINS
import mozilla.components.feature.autofill.structure.ParsedStructure
import mozilla.components.support.utils.PendingIntentUtils

@RequiresApi(Build.VERSION_CODES.O)
internal data class SearchDatasetBuilder(
Expand All @@ -36,7 +37,7 @@ internal data class SearchDatasetBuilder(
context,
configuration.activityRequestCode + MAX_LOGINS,
searchIntent,
PendingIntent.FLAG_CANCEL_CURRENT
PendingIntentUtils.defaultFlags or PendingIntent.FLAG_CANCEL_CURRENT
)
val intentSender: IntentSender = searchPendingIntent.intentSender

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import mozilla.components.feature.downloads.AbstractFetchDownloadService.Compani
import mozilla.components.feature.downloads.AbstractFetchDownloadService.Companion.ACTION_RESUME
import mozilla.components.feature.downloads.AbstractFetchDownloadService.Companion.ACTION_TRY_AGAIN
import mozilla.components.feature.downloads.AbstractFetchDownloadService.DownloadJobState
import mozilla.components.support.utils.PendingIntentUtils
import kotlin.random.Random

@Suppress("LargeClass")
Expand Down Expand Up @@ -289,7 +290,12 @@ internal object DownloadNotification {

// We generate a random requestCode in order to generate a distinct PendingIntent:
// https://developer.android.com/reference/android/app/PendingIntent.html
return PendingIntent.getBroadcast(context.applicationContext, Random.nextInt(), intent, 0)
return PendingIntent.getBroadcast(
context.applicationContext,
Random.nextInt(),
intent,
PendingIntentUtils.defaultFlags
)
}
}

Expand Down
1 change: 1 addition & 0 deletions components/feature/media/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {
implementation project(':ui-icons')
implementation project(':support-ktx')
implementation project(':support-base')
implementation project(':support-utils')

implementation Dependencies.kotlin_stdlib
implementation Dependencies.kotlin_coroutines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import mozilla.components.feature.media.notification.MediaNotificationChannel
import mozilla.components.lib.state.Middleware
import mozilla.components.lib.state.MiddlewareContext
import mozilla.components.support.base.ids.SharedIdsHelper
import mozilla.components.support.utils.PendingIntentUtils

private const val NOTIFICATION_TAG = "mozac.feature.media.recordingDevices"
private const val NOTIFICATION_ID = 1
Expand Down Expand Up @@ -93,7 +94,9 @@ class RecordingDevicesMiddleware(

val pendingIntent = PendingIntent.getActivity(
context,
SharedIdsHelper.getIdForTag(context, PENDING_INTENT_TAG), intent, PendingIntent.FLAG_UPDATE_CURRENT
SharedIdsHelper.getIdForTag(context, PENDING_INTENT_TAG),
intent,
PendingIntentUtils.defaultFlags or PendingIntent.FLAG_UPDATE_CURRENT
)

val notification = NotificationCompat.Builder(context, channelId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package mozilla.components.feature.media.notification

import android.app.Notification
import android.app.PendingIntent
import android.app.PendingIntent.FLAG_IMMUTABLE
import android.app.PendingIntent.FLAG_UPDATE_CURRENT
import android.content.Context
import android.graphics.Bitmap
Expand All @@ -24,6 +23,7 @@ import mozilla.components.feature.media.ext.getTitleOrUrl
import mozilla.components.feature.media.ext.nonPrivateUrl
import mozilla.components.feature.media.service.AbstractMediaSessionService
import mozilla.components.support.base.ids.SharedIdsHelper
import mozilla.components.support.utils.PendingIntentUtils
import java.util.Locale

/**
Expand Down Expand Up @@ -155,14 +155,6 @@ private data class NotificationData(
val contentIntent: PendingIntent? = null
)

private fun getNotificationFlag() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
FLAG_IMMUTABLE
} else {
0
}
private fun getNotificationFlag() = PendingIntentUtils.defaultFlags

private fun getUpdateNotificationFlag() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
FLAG_IMMUTABLE or FLAG_UPDATE_CURRENT
} else {
FLAG_UPDATE_CURRENT
}
private fun getUpdateNotificationFlag() = PendingIntentUtils.defaultFlags or FLAG_UPDATE_CURRENT
1 change: 1 addition & 0 deletions components/feature/privatemode/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies {
implementation project(':concept-engine')
implementation project(':support-base')
implementation project(':support-ktx')
implementation project(':support-utils')

implementation Dependencies.androidx_core_ktx
implementation Dependencies.kotlin_coroutines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import mozilla.components.support.base.ids.SharedIdsHelper
import mozilla.components.support.ktx.android.notification.ChannelData
import mozilla.components.support.ktx.android.notification.ensureNotificationChannelExists
import mozilla.components.support.ktx.kotlinx.coroutines.flow.ifChanged
import mozilla.components.support.utils.PendingIntentUtils
import java.util.Locale

/**
Expand Down Expand Up @@ -149,7 +150,7 @@ abstract class AbstractPrivateNotificationService : Service() {
.setContentIntent(
Intent(ACTION_ERASE).let {
it.setClass(this, this::class.java)
PendingIntent.getService(this, 0, it, FLAG_ONE_SHOT)
PendingIntent.getService(this, 0, it, PendingIntentUtils.defaultFlags or FLAG_ONE_SHOT)
}
)
.apply { buildNotification() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import mozilla.components.feature.pwa.WebAppLauncherActivity.Companion.ACTION_PW
import mozilla.components.feature.pwa.ext.hasLargeIcons
import mozilla.components.feature.pwa.ext.installableManifest
import mozilla.components.support.images.decoder.AndroidImageDecoder
import mozilla.components.support.utils.PendingIntentUtils

private val pwaIconMemoryCache = IconMemoryCache()

Expand Down Expand Up @@ -90,7 +91,12 @@ class WebAppShortcutManager(
addCategory(CATEGORY_HOME)
flags = FLAG_ACTIVITY_NEW_TASK
}
val pendingIntent = PendingIntent.getActivity(context, 0, intent, FLAG_UPDATE_CURRENT)
val pendingIntent = PendingIntent.getActivity(
context,
0,
intent,
PendingIntentUtils.defaultFlags or FLAG_UPDATE_CURRENT
)
val intentSender = pendingIntent.intentSender

ShortcutManagerCompat.requestPinShortcut(context, shortcut, intentSender)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.core.content.getSystemService
import mozilla.components.browser.state.state.CustomTabSessionState
import mozilla.components.feature.pwa.R
import mozilla.components.feature.session.SessionUseCases
import mozilla.components.support.utils.PendingIntentUtils

/**
* Callback for [WebAppSiteControlsFeature] that lets the displayed notification be customized.
Expand Down Expand Up @@ -76,7 +77,7 @@ interface SiteControlsBuilder {
protected fun createPendingIntent(context: Context, action: String, requestCode: Int): PendingIntent {
val intent = Intent(action)
intent.setPackage(context.packageName)
return PendingIntent.getBroadcast(context, requestCode, intent, 0)
return PendingIntent.getBroadcast(context, requestCode, intent, PendingIntentUtils.defaultFlags)
}

companion object {
Expand Down
1 change: 1 addition & 0 deletions components/feature/webnotifications/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies {
implementation project(':concept-engine')
implementation project(':feature-sitepermissions')
implementation project(':support-ktx')
implementation project(':support-utils')

implementation Dependencies.androidx_core_ktx
implementation Dependencies.kotlin_stdlib
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import mozilla.components.browser.icons.IconRequest
import mozilla.components.browser.icons.IconRequest.Size
import mozilla.components.concept.engine.webnotifications.WebNotification
import mozilla.components.support.ktx.kotlin.tryGetHostFromUrl
import mozilla.components.support.utils.PendingIntentUtils

internal class NativeNotificationBridge(
private val icons: BrowserIcons,
Expand Down Expand Up @@ -52,7 +53,7 @@ internal class NativeNotificationBridge(
putExtra(EXTRA_ON_CLICK, tag)
}

PendingIntent.getActivity(context, requestId, intent, 0).apply {
PendingIntent.getActivity(context, requestId, intent, PendingIntentUtils.defaultFlags).apply {
builder.setContentIntent(this)
}
}
Expand Down
3 changes: 2 additions & 1 deletion components/lib/crash/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ val pendingIntent = PendingIntent.getActivity(
Intent(this, MyActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
},
0)
PendingIntentUtils.defaultFlags
)

CrashReporter(
shouldPrompt = CrashReporter.Prompt.ALWAYS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import mozilla.components.lib.crash.R
import mozilla.components.lib.crash.prompt.CrashPrompt
import mozilla.components.lib.crash.service.SendCrashReportService
import mozilla.components.support.base.ids.SharedIdsHelper
import mozilla.components.support.utils.PendingIntentUtils
import mozilla.components.support.utils.asForegroundServicePendingIntent

private const val NOTIFICATION_SDK_LEVEL = 29 // On Android Q+ we show a notification instead of a prompt
Expand Down Expand Up @@ -104,9 +105,5 @@ internal class CrashNotification(
}
}

private fun getNotificationFlag() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
PendingIntent.FLAG_IMMUTABLE
} else {
0
}
private fun getNotificationFlag() = PendingIntentUtils.defaultFlags
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import mozilla.components.support.base.ids.SharedIdsHelper
import mozilla.components.support.base.log.logger.Logger
import mozilla.components.support.migration.state.MigrationAction
import mozilla.components.support.migration.state.MigrationStore
import mozilla.components.support.utils.PendingIntentUtils

private const val NOTIFICATION_CHANNEL_ID = "mozac.support.migration.generic"

Expand Down Expand Up @@ -125,8 +126,10 @@ abstract class AbstractMigrationService : Service() {
.setContentText(getString(contentRes))
.setContentIntent(
PendingIntent.getActivity(
this, 0,
Intent(this, migrationDecisionActivity).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP), 0
this,
0,
Intent(this, migrationDecisionActivity).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP),
PendingIntentUtils.defaultFlags
)
)
.setPriority(NotificationCompat.PRIORITY_LOW)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import android.os.Build
fun Intent.asForegroundServicePendingIntent(
context: Context,
requestCode: Int,
flags: Int = PendingIntent.FLAG_UPDATE_CURRENT
flags: Int = PendingIntentUtils.defaultFlags or PendingIntent.FLAG_UPDATE_CURRENT
): PendingIntent =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
PendingIntent.getForegroundService(context, requestCode, this, flags)
Expand Down
1 change: 1 addition & 0 deletions samples/crash/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies {
implementation project(':lib-fetch-httpurlconnection')
implementation project(':service-glean')
implementation project(':support-base')
implementation project(':support-utils')

implementation Dependencies.kotlin_stdlib
implementation Dependencies.kotlin_coroutines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import mozilla.components.service.glean.config.Configuration
import mozilla.components.service.glean.net.ConceptFetchHttpUploader
import mozilla.components.support.base.log.Log
import mozilla.components.support.base.log.sink.AndroidLogSink
import mozilla.components.support.utils.PendingIntentUtils
import java.util.Calendar
import java.util.TimeZone
import java.util.UUID
Expand Down Expand Up @@ -128,7 +129,12 @@ private fun createNonFatalPendingIntent(context: Context): PendingIntent {
// The PendingIntent can launch whatever you want - an activity, a service... Here we pick a broadcast. Our main
// activity will listener for the broadcast and show an in-app snackbar to ask the user whether we should send
// this crash report.
return PendingIntent.getBroadcast(context, 0, Intent(CrashApplication.NON_FATAL_CRASH_BROADCAST), 0)
return PendingIntent.getBroadcast(
context,
0,
Intent(CrashApplication.NON_FATAL_CRASH_BROADCAST),
PendingIntentUtils.defaultFlags
)
}

val Context.crashReporter: CrashReporter
Expand Down

0 comments on commit 0eff6a2

Please sign in to comment.