From 96c6ed59a63c01513dce7c33ef4341b68971b639 Mon Sep 17 00:00:00 2001 From: Azhara Assanova Date: Tue, 11 Mar 2025 16:35:22 +0000 Subject: [PATCH] Change the resource ID of advanced protection help URI Change-Id I3c6d24e3e9a4358ab1adb342dbee8fc56ac16794 added advanced protection help URI to frameworks/base/core/res/res/values/config.xml, so this change removes the duplicate resource from Settings and updates ActionDisabledByAdvancedProtectionDialog to use the framework resource. Bug: 401233918 Test: manual Test: atest ActionDisabledByAdvancedProtectionDialog Flag: EXEMPT bug fix Change-Id: Ic84909f4c16d3449bd9981be6659987b1963a26a --- res/values/strings.xml | 3 - ...ctionDisabledByAdvancedProtectionDialog.kt | 14 ++- tests/spa_unit/AndroidManifest.xml | 8 ++ ...nDisabledByAdvancedProtectionDialogTest.kt | 113 +++++++++++++++++- 4 files changed, 129 insertions(+), 9 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index db9c302edca..9a506b64c4d 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -12221,9 +12221,6 @@ Data usage charges may apply. - - - Update Do Not Disturb diff --git a/src/com/android/settings/security/ActionDisabledByAdvancedProtectionDialog.kt b/src/com/android/settings/security/ActionDisabledByAdvancedProtectionDialog.kt index cf29c3d707e..255bc985fcb 100644 --- a/src/com/android/settings/security/ActionDisabledByAdvancedProtectionDialog.kt +++ b/src/com/android/settings/security/ActionDisabledByAdvancedProtectionDialog.kt @@ -14,21 +14,23 @@ * limitations under the License. */ -package com.android.settings.security; +package com.android.settings.security import android.content.Intent +import android.content.pm.PackageManager +import android.security.advancedprotection.AdvancedProtectionManager import android.security.advancedprotection.AdvancedProtectionManager.EXTRA_SUPPORT_DIALOG_FEATURE import android.security.advancedprotection.AdvancedProtectionManager.EXTRA_SUPPORT_DIALOG_TYPE import android.security.advancedprotection.AdvancedProtectionManager.FEATURE_ID_DISALLOW_CELLULAR_2G import android.security.advancedprotection.AdvancedProtectionManager.FEATURE_ID_DISALLOW_INSTALL_UNKNOWN_SOURCES import android.security.advancedprotection.AdvancedProtectionManager.FEATURE_ID_DISALLOW_WEP -import android.content.pm.PackageManager import android.security.advancedprotection.AdvancedProtectionManager.FEATURE_ID_ENABLE_MTE import android.security.advancedprotection.AdvancedProtectionManager.SUPPORT_DIALOG_TYPE_BLOCKED_INTERACTION import android.security.advancedprotection.AdvancedProtectionManager.SUPPORT_DIALOG_TYPE_DISABLED_SETTING import android.security.advancedprotection.AdvancedProtectionManager.SUPPORT_DIALOG_TYPE_UNKNOWN import android.util.Log import android.view.WindowManager +import androidx.annotation.VisibleForTesting import androidx.compose.material3.Icon import androidx.compose.material3.Text import androidx.compose.runtime.Composable @@ -38,7 +40,6 @@ import com.android.settingslib.spa.SpaDialogWindowTypeActivity import com.android.settingslib.spa.widget.dialog.AlertDialogButton import com.android.settingslib.spa.widget.dialog.SettingsAlertDialogContent import com.android.settingslib.wifi.WifiUtils.Companion.DIALOG_WINDOW_TYPE -import android.security.advancedprotection.AdvancedProtectionManager class ActionDisabledByAdvancedProtectionDialog : SpaDialogWindowTypeActivity() { @@ -85,9 +86,12 @@ class ActionDisabledByAdvancedProtectionDialog : SpaDialogWindowTypeActivity() { return getString(messageId) } - private fun getSupportButtonIfExists(): AlertDialogButton? { + @VisibleForTesting + fun getSupportButtonIfExists(): AlertDialogButton? { try { - val helpIntentUri = getString(R.string.help_url_action_disabled_by_advanced_protection) + val helpIntentUri = getString( + com.android.internal.R.string.config_help_url_action_disabled_by_advanced_protection + ) val helpIntent = Intent.parseUri(helpIntentUri, Intent.URI_INTENT_SCHEME) if (helpIntent == null) return null val helpActivityInfo = packageManager.resolveActivity(helpIntent, /* flags */ 0) diff --git a/tests/spa_unit/AndroidManifest.xml b/tests/spa_unit/AndroidManifest.xml index 1950f209868..d225a03061a 100644 --- a/tests/spa_unit/AndroidManifest.xml +++ b/tests/spa_unit/AndroidManifest.xml @@ -30,6 +30,14 @@ + + + + + + + () + + private val context: Context = ApplicationProvider.getApplicationContext() @Test fun blockedInteractionDialog_showsCorrectTitleAndMessage() { @@ -159,6 +178,85 @@ class ActionDisabledByAdvancedProtectionDialogTest { } } + @Test + fun helpIntentDoesNotExist_getSupportButtonIfExists_returnsNull() { + launchDialogActivity(defaultIntent) { scenario -> + scenario.onActivity { activity -> + val spyActivity = spyOnActivityHelpIntentUri(activity, /* uriToReturn */ null) + + val button = spyActivity.getSupportButtonIfExists() + assertNull(button) + } + } + } + + @Test + fun helpIntentExistsAndDoesNotResolveToActivity_getSupportButtonIfExists_returnsNull() { + launchDialogActivity(defaultIntent) { scenario -> + scenario.onActivity { activity -> + val spyActivity = spyOnActivityHelpIntentUri(activity, helpIntentUri) + mockResolveActivity(spyActivity, /* resolveInfoToReturn */ null) + + val button = spyActivity.getSupportButtonIfExists() + assertNull(button) + } + } + } + + @Test + fun helpIntentExistsAndResolvesToActivity_getSupportButtonIfExists_returnsButton() { + launchDialogActivity(defaultIntent) { scenario -> + scenario.onActivity { activity -> + val spyActivity = spyOnActivityHelpIntentUri(activity, helpIntentUri) + val resolveInfoToReturn = ResolveInfo().apply { + activityInfo = ActivityInfo().apply { + packageName = HELP_INTENT_PKG_NAME + } + } + mockResolveActivity(spyActivity, resolveInfoToReturn) + + // 1. Check the button is returned. + val button = spyActivity.getSupportButtonIfExists() + assertNotNull(button) + + // 2. Check the button has correct text. + assertEquals(context.getString( + R.string.disabled_by_advanced_protection_help_button_title), button!!.text + ) + + // 3. Check the button's onClick launches the help activity and finishes the dialog. + button.onClick() + + val intentCaptor = ArgumentCaptor.forClass(Intent::class.java) + verify(spyActivity).startActivity(intentCaptor.capture()) + val launchedIntent = intentCaptor.value + assertEquals(HELP_INTENT_ACTION, launchedIntent.action) + assertEquals(HELP_INTENT_PKG_NAME, launchedIntent.`package`) + + assertTrue(spyActivity.isFinishing) + } + } + } + + private fun spyOnActivityHelpIntentUri( + activity: ActionDisabledByAdvancedProtectionDialog, + uriToReturn: String? + ): ActionDisabledByAdvancedProtectionDialog { + val spyActivity = spy(activity) + val spyResources = spy(spyActivity.resources) + doReturn(spyResources).whenever(spyActivity).resources + doReturn(uriToReturn).whenever(spyResources).getString(helpUriResourceId) + return spyActivity + } + + private fun mockResolveActivity( + spyActivity: ActionDisabledByAdvancedProtectionDialog, + resolveInfoToReturn: ResolveInfo? + ) { + doReturn(mockPackageManager).whenever(spyActivity).packageManager + doReturn(resolveInfoToReturn).whenever(mockPackageManager).resolveActivity(any(), anyInt()) + } + private fun launchDialogActivity( intent: Intent, onScenario: (ActivityScenario) -> Unit @@ -172,10 +270,23 @@ class ActionDisabledByAdvancedProtectionDialogTest { launch(intent).use(onScenario) } + class HelpTestActivity : Activity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + finish() + } + } + private companion object { val defaultIntent = AdvancedProtectionManager.createSupportIntent( FEATURE_ID_DISALLOW_CELLULAR_2G, SUPPORT_DIALOG_TYPE_BLOCKED_INTERACTION ) + const val HELP_INTENT_PKG_NAME = "com.android.settings.tests.spa_unit" + const val HELP_INTENT_ACTION = "$HELP_INTENT_PKG_NAME.HELP_ACTION" + val helpIntent = Intent(HELP_INTENT_ACTION).setPackage(HELP_INTENT_PKG_NAME) + val helpIntentUri = helpIntent.toUri(Intent.URI_INTENT_SCHEME) + val helpUriResourceId = + com.android.internal.R.string.config_help_url_action_disabled_by_advanced_protection } }