Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
For #11666 - Add PendingUtils.defaultFlags helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Mugurell authored and mergify[bot] committed Feb 7, 2022
1 parent a788718 commit 39f8dd6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.components.support.utils

import android.app.PendingIntent
import android.os.Build

/**
* Helper methods for when working with [PendingIntent]s.
*/
object PendingIntentUtils {
/**
* Android 6 introduced FLAG_IMMUTABLE to prevents apps that receive a PendingIntent from
* filling in unpopulated properties. Android 12 requires mutability explicitly.
*
* This property will return:
* - PendingIntent.FLAG_IMMUTABLE - for Android API 23+
* - 0 (framework default flags) - for Android APIs lower than 23.
*/
val defaultFlags
get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PendingIntent.FLAG_IMMUTABLE
} else {
0 // No flags. Default behavior.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.components.support.utils

import android.app.PendingIntent
import android.os.Build
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.annotation.Config

@RunWith(AndroidJUnit4::class)
class PendingIntentUtilsTest {
@Test
@Config(sdk = [Build.VERSION_CODES.LOLLIPOP_MR1]) // highest API level for which to return 0
fun `GIVEN an Android L device WHEN defaultFlags is called THEN return 0`() {
assertEquals(0, PendingIntentUtils.defaultFlags)
}

@Test
@Config(sdk = [Build.VERSION_CODES.M]) // first API level for which to return FLAG_IMMUTABLE
fun `GIVEN an Android M device WHEN defaultFlags is called THEN return FLAG_IMMUTABLE`() {
assertEquals(PendingIntent.FLAG_IMMUTABLE, PendingIntentUtils.defaultFlags)
}
}
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ permalink: /changelog/
* [Gecko](https://github.com/mozilla-mobile/android-components/blob/main/buildSrc/src/main/java/Gecko.kt)
* [Configuration](https://github.com/mozilla-mobile/android-components/blob/main/.config.yml)

* **support-utils**
* 🌟️️ **Add a `PendingUtils.defaultFlags` property to ease setting PendingIntent mutability as required for Android 31+.

* **feature-prompts**:
* More prompts are dismissable.
* **Breaking change:** Success / dismiss callbacks are now consistently
Expand Down

0 comments on commit 39f8dd6

Please sign in to comment.