This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For #11666 - Add PendingUtils.defaultFlags helper
- Loading branch information
1 parent
a788718
commit 39f8dd6
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
...onents/support/utils/src/main/java/mozilla/components/support/utils/PendingIntentUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...ts/support/utils/src/test/java/mozilla/components/support/utils/PendingIntentUtilsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters