-
Notifications
You must be signed in to change notification settings - Fork 363
Add PendingIntentCompat to enforce mutability flag #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adaext
wants to merge
7
commits into
google:master
Choose a base branch
from
adaext:add_PendingIntentCompat
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
85ae1a5
Implement PendingIntentCompat to enforce mutability flag
71873f8
Add parameter hint comments
c77f0ae
Add unit test for PendingIntentCompat
3a08997
Add a few missing test cases and adjust the code format
4d77286
Fix mistakes in test cases and add annotions in PendingIntentCompat
07d729e
Fix the problem that PendingIntentCompatTest can't run and provide a …
b7d04e5
Convert PendingIntentCompat to Kotlin
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
54 changes: 54 additions & 0 deletions
54
...src/main/java/com/google/android/apps/location/gps/gnsslogger/util/PendingIntentCompat.kt
This file contains hidden or 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,54 @@ | ||
| /* | ||
| * Copyright (C) 2022 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.google.android.apps.location.gps.gnsslogger.util | ||
|
|
||
| import android.content.Intent | ||
| import android.app.PendingIntent | ||
| import android.content.Context | ||
| import com.google.android.apps.location.gps.gnsslogger.util.PendingIntentCompat | ||
| import android.os.Build | ||
|
|
||
| /** Helper for accessing features in [PendingIntent]. */ | ||
| object PendingIntentCompat { | ||
| /** | ||
| * Retrieves a [PendingIntent] with mandatory mutability flag set on supported platform | ||
| * versions. The caller provides the flag as combination of all the other values except mutability | ||
| * flag. This method combines mutability flag when necessary. See [ ][PendingIntent.getBroadcast]. | ||
| */ | ||
| @JvmStatic | ||
| fun getBroadcast( | ||
| context: Context, | ||
| requestCode: Int, | ||
| intent: Intent, | ||
| flags: Int, | ||
| isMutable: Boolean): PendingIntent { | ||
| return PendingIntent.getBroadcast( | ||
| context, requestCode, intent, addMutabilityFlags(isMutable, flags)) | ||
| } | ||
|
|
||
| private fun addMutabilityFlags(isMutable: Boolean, flags: Int): Int { | ||
| var flags = flags | ||
| if (isMutable) { | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { | ||
| flags = flags or PendingIntent.FLAG_MUTABLE | ||
| } | ||
| } else { | ||
| // Minimum support SDK is 24. | ||
| flags = flags or PendingIntent.FLAG_IMMUTABLE | ||
| } | ||
| return flags | ||
| } | ||
| } |
17 changes: 0 additions & 17 deletions
17
...er/app/src/test/java/com/google/android/apps/location/gps/gnsslogger/ExampleUnitTest.java
This file was deleted.
Oops, something went wrong.
121 changes: 121 additions & 0 deletions
121
...st/java/com/google/android/apps/location/gps/gnsslogger/util/PendingIntentCompatTest.java
This file contains hidden or 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,121 @@ | ||
| package com.google.android.apps.location.gps.gnsslogger.util; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
| import static org.robolectric.Shadows.shadowOf; | ||
|
|
||
| import android.app.PendingIntent; | ||
| import android.content.Context; | ||
| import android.content.Intent; | ||
| import android.os.Build; | ||
| import androidx.test.core.app.ApplicationProvider; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.robolectric.RobolectricTestRunner; | ||
| import org.robolectric.annotation.Config; | ||
| import org.robolectric.shadows.ShadowPendingIntent; | ||
|
|
||
| /** Unit test for {@link PendingIntentCompat}. */ | ||
| @RunWith(RobolectricTestRunner.class) | ||
| public class PendingIntentCompatTest { | ||
| private final Context context = ApplicationProvider.getApplicationContext(); | ||
|
|
||
| /** Immutable is introduced in M, but the project's minimum support version is 24 */ | ||
| @Config(sdk = Build.VERSION_CODES.N) | ||
| @Test | ||
| public void addMutabilityFlags_immutableOnMPlus() { | ||
| int requestCode = 7465; | ||
| Intent intent = new Intent(); | ||
| ShadowPendingIntent shadow = | ||
| shadowOf( | ||
| PendingIntentCompat.getBroadcast( | ||
| context, | ||
| requestCode, | ||
| intent, | ||
| PendingIntent.FLAG_UPDATE_CURRENT, | ||
| /* isMutable= */ false)); | ||
| assertThat(shadow.getFlags()) | ||
| .isEqualTo(PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); | ||
| } | ||
|
|
||
| @Config(maxSdk = Build.VERSION_CODES.R) | ||
| @Test | ||
| public void addMutabilityFlags_mutableOnPreS() { | ||
| int requestCode = 7465; | ||
| Intent intent = new Intent(); | ||
| ShadowPendingIntent shadow = | ||
| shadowOf( | ||
| PendingIntentCompat.getBroadcast( | ||
| context, | ||
| requestCode, | ||
| intent, | ||
| PendingIntent.FLAG_UPDATE_CURRENT, | ||
| /* isMutable= */ true)); | ||
| assertThat(shadow.getFlags()).isEqualTo(PendingIntent.FLAG_UPDATE_CURRENT); | ||
| } | ||
|
|
||
| @Config(maxSdk = Build.VERSION_CODES.R) | ||
| @Test | ||
| public void addMutabilityFlags_immutableOnPreS() { | ||
| int requestCode = 7465; | ||
| Intent intent = new Intent(); | ||
| ShadowPendingIntent shadow = | ||
| shadowOf( | ||
| PendingIntentCompat.getBroadcast( | ||
| context, | ||
| requestCode, | ||
| intent, | ||
| PendingIntent.FLAG_UPDATE_CURRENT, | ||
| /* isMutable= */ false)); | ||
| assertThat(shadow.getFlags()) | ||
| .isEqualTo(PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); | ||
| } | ||
|
|
||
| @Config(minSdk = Build.VERSION_CODES.S) | ||
| @Test | ||
| public void addMutabilityFlags_mutableOnSPlus() { | ||
| int requestCode = 7465; | ||
| Intent intent = new Intent(); | ||
| ShadowPendingIntent shadow = | ||
| shadowOf( | ||
| PendingIntentCompat.getBroadcast( | ||
| context, | ||
| requestCode, | ||
| intent, | ||
| PendingIntent.FLAG_UPDATE_CURRENT, | ||
| /* isMutable= */ true)); | ||
| assertThat(shadow.getFlags()) | ||
| .isEqualTo(PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE); | ||
| } | ||
|
|
||
| @Config(minSdk = Build.VERSION_CODES.S) | ||
| @Test | ||
| public void addMutabilityFlags_immutableOnSPlus() { | ||
| int requestCode = 7465; | ||
| Intent intent = new Intent(); | ||
| ShadowPendingIntent shadow = | ||
| shadowOf( | ||
| PendingIntentCompat.getBroadcast( | ||
| context, | ||
| requestCode, | ||
| intent, | ||
| PendingIntent.FLAG_UPDATE_CURRENT, | ||
| /* isMutable= */ false)); | ||
| assertThat(shadow.getFlags()) | ||
| .isEqualTo(PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); | ||
| } | ||
|
|
||
| @Test | ||
| public void getBroadcast() { | ||
| int requestCode = 7465; | ||
| Intent intent = new Intent(); | ||
| ShadowPendingIntent shadow = | ||
| shadowOf( | ||
| PendingIntentCompat.getBroadcast( | ||
| context, | ||
| requestCode, | ||
| intent, | ||
| PendingIntent.FLAG_UPDATE_CURRENT, | ||
| /* isMutable= */ false)); | ||
| assertThat(shadow.isBroadcast()).isTrue(); | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.