diff options
| -rw-r--r-- | AndroidManifest.xml | 10 | ||||
| -rw-r--r-- | java/src/com/android/intentresolver/ChooserPinMigrationReceiver.kt | 55 | ||||
| -rw-r--r-- | java/tests/src/com/android/intentresolver/ChooserPinMigrationReceiverTest.kt | 78 |
3 files changed, 142 insertions, 1 deletions
diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 5228827d..56e8acca 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -22,7 +22,6 @@ android:versionName="2021-11" coreApp="true"> - <uses-permission android:name="android.permission.ACCESS_SHORTCUTS" /> <uses-permission android:name="android.permission.BIND_RESOLVER_RANKER_SERVICE" /> <uses-permission android:name="android.permission.GET_ANY_PROVIDER_TYPE" /> @@ -32,6 +31,7 @@ <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" /> <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" /> <uses-permission android:name="android.permission.READ_DEVICE_CONFIG" /> + <uses-permission android:name="android.permission.RECEIVE_CHOOSER_PIN_MIGRATION" /> <uses-permission android:name="android.permission.SET_CLIP_SOURCE" /> <uses-permission android:name="android.permission.START_ACTIVITY_AS_CALLER" /> <uses-permission android:name="android.permission.UNLIMITED_SHORTCUTS_API_CALLS" /> @@ -72,6 +72,14 @@ </intent-filter> </activity> + <receiver android:name=".ChooserPinMigrationReceiver" + android:exported="true" + android:permission="android.permission.ADD_CHOOSER_PINS"> + <intent-filter> + <action android:name="android.intent.action.CHOOSER_PIN_MIGRATION" /> + </intent-filter> + </receiver> + </application> </manifest> diff --git a/java/src/com/android/intentresolver/ChooserPinMigrationReceiver.kt b/java/src/com/android/intentresolver/ChooserPinMigrationReceiver.kt new file mode 100644 index 00000000..a3ba2192 --- /dev/null +++ b/java/src/com/android/intentresolver/ChooserPinMigrationReceiver.kt @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2023 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.android.intentresolver + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.content.SharedPreferences +import android.util.Log + +/** + * Broadcast receiver for receiving Chooser pin data from the legacy chooser. + * + * Unions the legacy pins with any existing pins. This receiver is protected by the ADD_CHOOSER_PINS + * permission. The receiver is required to have the RECEIVE_CHOOSER_PIN_MIGRATION to receive the + * broadcast. + */ +class ChooserPinMigrationReceiver( + private val pinnedSharedPrefsProvider: (Context) -> SharedPreferences = + { context -> ChooserActivity.getPinnedSharedPrefs(context) }, +) : BroadcastReceiver() { + + override fun onReceive(context: Context, intent: Intent) { + val bundle = intent.extras ?: return + Log.i(TAG, "Starting migration") + + val prefsEditor = pinnedSharedPrefsProvider.invoke(context).edit() + bundle.keySet().forEach { key -> + if(bundle.getBoolean(key)) { + prefsEditor.putBoolean(key, true) + } + } + prefsEditor.apply() + + Log.i(TAG, "Migration complete") + } + + companion object { + private const val TAG = "ChooserPinMigrationReceiver" + } +}
\ No newline at end of file diff --git a/java/tests/src/com/android/intentresolver/ChooserPinMigrationReceiverTest.kt b/java/tests/src/com/android/intentresolver/ChooserPinMigrationReceiverTest.kt new file mode 100644 index 00000000..1daee137 --- /dev/null +++ b/java/tests/src/com/android/intentresolver/ChooserPinMigrationReceiverTest.kt @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2023 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.android.intentresolver + +import android.content.Context +import android.content.Intent +import android.content.SharedPreferences +import androidx.test.ext.junit.runners.AndroidJUnit4 +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mock +import org.mockito.Mockito.verify +import org.mockito.Mockito.verifyNoMoreInteractions +import org.mockito.MockitoAnnotations + +@RunWith(AndroidJUnit4::class) +class ChooserPinMigrationReceiverTest { + + private lateinit var chooserPinMigrationReceiver: ChooserPinMigrationReceiver + + @Mock private lateinit var mockContext: Context + @Mock private lateinit var mockSharedPreferences: SharedPreferences + @Mock private lateinit var mockSharedPreferencesEditor: SharedPreferences.Editor + + @Before + fun setup() { + MockitoAnnotations.initMocks(this) + + whenever(mockSharedPreferences.edit()).thenReturn(mockSharedPreferencesEditor) + + chooserPinMigrationReceiver = ChooserPinMigrationReceiver { mockSharedPreferences } + } + + @Test + fun onReceive_addsReceivedPins() { + // Arrange + val intent = Intent().apply { + putExtra("TestPackage/TestClass", true) + } + + // Act + chooserPinMigrationReceiver.onReceive(mockContext, intent) + + // Assert + verify(mockSharedPreferencesEditor).putBoolean(eq("TestPackage/TestClass"), eq(true)) + verify(mockSharedPreferencesEditor).apply() + } + + @Test + fun onReceive_ignoresUnpinnedEntries() { + // Arrange + val intent = Intent().apply { + putExtra("TestPackage/TestClass", false) + } + + // Act + chooserPinMigrationReceiver.onReceive(mockContext, intent) + + // Assert + verify(mockSharedPreferencesEditor).apply() + verifyNoMoreInteractions(mockSharedPreferencesEditor) + } +}
\ No newline at end of file |