From 5e5f1dd7244a7875d4daf1c25ea7c2d6156c3a82 Mon Sep 17 00:00:00 2001 From: Mark Kim Date: Thu, 11 Jan 2024 10:57:03 +0000 Subject: [PATCH] Get and save app label before archiving the app Test: manual Bug: 317125320 Change-Id: I34763106f669e3f2c0f349cdec977ceb57291e01 --- .../android/settings/spa/app/appinfo/AppArchiveButton.kt | 7 ++++--- .../settings/spa/app/appinfo/AppArchiveButtonTest.kt | 5 ++++- .../android/settings/spa/app/appinfo/AppButtonsTest.kt | 9 +++++++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/com/android/settings/spa/app/appinfo/AppArchiveButton.kt b/src/com/android/settings/spa/app/appinfo/AppArchiveButton.kt index 0eef9c39f1b..e4fb1ea47a3 100644 --- a/src/com/android/settings/spa/app/appinfo/AppArchiveButton.kt +++ b/src/com/android/settings/spa/app/appinfo/AppArchiveButton.kt @@ -54,6 +54,7 @@ class AppArchiveButton( private val packageName = packageInfoPresenter.packageName private val userHandle = UserHandle.of(packageInfoPresenter.userId) private var broadcastReceiverIsCreated = false + private lateinit var appLabel: CharSequence @Composable fun getActionButton(app: ApplicationInfo): ActionButton { @@ -61,11 +62,12 @@ class AppArchiveButton( val intentFilter = IntentFilter(INTENT_ACTION) DisposableBroadcastReceiverAsUser(intentFilter, userHandle) { intent -> if (app.packageName == intent.getStringExtra(PackageInstaller.EXTRA_PACKAGE_NAME)) { - onReceive(intent, app) + onReceive(intent) } } broadcastReceiverIsCreated = true } + appLabel = userPackageManager.getApplicationLabel(app) return ActionButton( text = context.getString(R.string.archive), imageVector = Icons.Outlined.CloudUpload, @@ -113,10 +115,9 @@ class AppArchiveButton( } } - private fun onReceive(intent: Intent, app: ApplicationInfo) { + private fun onReceive(intent: Intent) { when (val status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, Int.MIN_VALUE)) { PackageInstaller.STATUS_SUCCESS -> { - val appLabel = userPackageManager.getApplicationLabel(app) Toast.makeText( context, context.getString(R.string.archiving_succeeded, appLabel), diff --git a/tests/spa_unit/src/com/android/settings/spa/app/appinfo/AppArchiveButtonTest.kt b/tests/spa_unit/src/com/android/settings/spa/app/appinfo/AppArchiveButtonTest.kt index df1f15302dd..6b4cc0d4a1f 100644 --- a/tests/spa_unit/src/com/android/settings/spa/app/appinfo/AppArchiveButtonTest.kt +++ b/tests/spa_unit/src/com/android/settings/spa/app/appinfo/AppArchiveButtonTest.kt @@ -63,8 +63,10 @@ class AppArchiveButtonTest { whenever(packageInfoPresenter.context).thenReturn(context) whenever(packageInfoPresenter.userPackageManager).thenReturn(userPackageManager) whenever(userPackageManager.packageInstaller).thenReturn(packageInstaller) + whenever(userPackageManager.getApplicationLabel(any())).thenReturn(APP_LABEL) whenever(packageInfoPresenter.packageName).thenReturn(PACKAGE_NAME) - appArchiveButton = AppArchiveButton(packageInfoPresenter, isHibernationSwitchEnabledStateFlow) + appArchiveButton = + AppArchiveButton(packageInfoPresenter, isHibernationSwitchEnabledStateFlow) } @Test @@ -150,5 +152,6 @@ class AppArchiveButtonTest { private companion object { const val PACKAGE_NAME = "package.name" + const val APP_LABEL = "App label" } } diff --git a/tests/spa_unit/src/com/android/settings/spa/app/appinfo/AppButtonsTest.kt b/tests/spa_unit/src/com/android/settings/spa/app/appinfo/AppButtonsTest.kt index 733e1a4150c..c742bd7a3a8 100644 --- a/tests/spa_unit/src/com/android/settings/spa/app/appinfo/AppButtonsTest.kt +++ b/tests/spa_unit/src/com/android/settings/spa/app/appinfo/AppButtonsTest.kt @@ -45,6 +45,7 @@ import org.junit.runner.RunWith import org.mockito.Mock import org.mockito.MockitoSession import org.mockito.Spy +import org.mockito.kotlin.any import org.mockito.quality.Strictness import org.mockito.Mockito.`when` as whenever @@ -80,6 +81,7 @@ class AppButtonsTest { whenever(packageInfoPresenter.context).thenReturn(context) whenever(packageInfoPresenter.packageName).thenReturn(PACKAGE_NAME) whenever(packageInfoPresenter.userPackageManager).thenReturn(packageManager) + whenever(packageManager.getApplicationLabel(any())).thenReturn(APP_LABEL) whenever(packageManager.packageInstaller).thenReturn(packageInstaller) whenever(packageManager.getPackageInfo(PACKAGE_NAME, 0)).thenReturn(PACKAGE_INFO) whenever(AppUtils.isMainlineModule(packageManager, PACKAGE_NAME)).thenReturn(false) @@ -113,7 +115,8 @@ class AppButtonsTest { featureFlags.setFlag(Flags.FLAG_ARCHIVING, false) setContent() - composeTestRule.onNodeWithText(context.getString(R.string.launch_instant_app)).assertIsDisplayed() + composeTestRule.onNodeWithText(context.getString(R.string.launch_instant_app)) + .assertIsDisplayed() } @Test @@ -122,7 +125,8 @@ class AppButtonsTest { featureFlags.setFlag(Flags.FLAG_ARCHIVING, true) setContent() - composeTestRule.onNodeWithText(context.getString(R.string.launch_instant_app)).assertIsNotDisplayed() + composeTestRule.onNodeWithText(context.getString(R.string.launch_instant_app)) + .assertIsNotDisplayed() } @Test @@ -184,6 +188,7 @@ class AppButtonsTest { private companion object { const val PACKAGE_NAME = "package.name" + const val APP_LABEL = "App label" val PACKAGE_INFO = PackageInfo().apply { applicationInfo = ApplicationInfo().apply { packageName = PACKAGE_NAME