From 138d8a45d2b32d5dd60cfd91a6a4c59c8e1a5ef7 Mon Sep 17 00:00:00 2001 From: Mugurell Date: Fri, 4 Feb 2022 10:48:57 +0200 Subject: [PATCH] [components] For https://github.com/mozilla-mobile/android-components/issues/11666 - Add PendingUtils.defaultFlags helper --- .../components/support/utils/PendingIntentUtils.kt | 28 ++++++++++++++++++++++ .../support/utils/PendingIntentUtilsTest.kt | 28 ++++++++++++++++++++++ .../android/android-components/docs/changelog.md | 3 +++ 3 files changed, 59 insertions(+) create mode 100644 mobile/android/android-components/components/support/utils/src/main/java/mozilla/components/support/utils/PendingIntentUtils.kt create mode 100644 mobile/android/android-components/components/support/utils/src/test/java/mozilla/components/support/utils/PendingIntentUtilsTest.kt diff --git a/mobile/android/android-components/components/support/utils/src/main/java/mozilla/components/support/utils/PendingIntentUtils.kt b/mobile/android/android-components/components/support/utils/src/main/java/mozilla/components/support/utils/PendingIntentUtils.kt new file mode 100644 index 000000000000..93491db04e03 --- /dev/null +++ b/mobile/android/android-components/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/mobile/android/android-components/components/support/utils/src/test/java/mozilla/components/support/utils/PendingIntentUtilsTest.kt b/mobile/android/android-components/components/support/utils/src/test/java/mozilla/components/support/utils/PendingIntentUtilsTest.kt new file mode 100644 index 000000000000..328bb79811d8 --- /dev/null +++ b/mobile/android/android-components/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/mobile/android/android-components/docs/changelog.md b/mobile/android/android-components/docs/changelog.md index c29146534f98..895fa9d69f0f 100644 --- a/mobile/android/android-components/docs/changelog.md +++ b/mobile/android/android-components/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 -- 2.11.4.GIT