From 39f8dd61f95ddc0551b69b404a19cecc1395abe2 Mon Sep 17 00:00:00 2001 From: Mugurell Date: Fri, 4 Feb 2022 10:48:57 +0200 Subject: [PATCH] For #11666 - Add PendingUtils.defaultFlags helper --- .../support/utils/PendingIntentUtils.kt | 28 +++++++++++++++++++ .../support/utils/PendingIntentUtilsTest.kt | 28 +++++++++++++++++++ docs/changelog.md | 3 ++ 3 files changed, 59 insertions(+) create mode 100644 components/support/utils/src/main/java/mozilla/components/support/utils/PendingIntentUtils.kt create mode 100644 components/support/utils/src/test/java/mozilla/components/support/utils/PendingIntentUtilsTest.kt diff --git a/components/support/utils/src/main/java/mozilla/components/support/utils/PendingIntentUtils.kt b/components/support/utils/src/main/java/mozilla/components/support/utils/PendingIntentUtils.kt new file mode 100644 index 00000000000..93491db04e0 --- /dev/null +++ b/components/support/utils/src/main/java/mozilla/components/support/utils/PendingIntentUtils.kt @@ -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. + } +} diff --git a/components/support/utils/src/test/java/mozilla/components/support/utils/PendingIntentUtilsTest.kt b/components/support/utils/src/test/java/mozilla/components/support/utils/PendingIntentUtilsTest.kt new file mode 100644 index 00000000000..328bb79811d --- /dev/null +++ b/components/support/utils/src/test/java/mozilla/components/support/utils/PendingIntentUtilsTest.kt @@ -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) + } +} diff --git a/docs/changelog.md b/docs/changelog.md index c29146534f9..895fa9d69f0 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -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