From 7887669758719014744790e4f500ff0b72df22b0 Mon Sep 17 00:00:00 2001 From: Fabian Kozynski Date: Fri, 3 Mar 2023 11:45:10 -0500 Subject: [PATCH] Delay showing the controls ui after dialog In the case that the user clicks Yes on the dialog to change the setting, onComplete should be called after the keyguard is either dismissed (and the setting is changed) or canceled. That way, the correct value of the setting will be sent to the app. Test: atest ControlsSettingsDialogManagerImplTest Test: manual using mock app Fixes: 270913547 Change-Id: I5aae816c457e932e24d4b83ad66549d8b22e0b5b --- .../settings/ControlsSettingsDialogManager.kt | 12 ++++--- .../ControlsSettingsDialogManagerImplTest.kt | 31 +++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/controls/settings/ControlsSettingsDialogManager.kt b/packages/SystemUI/src/com/android/systemui/controls/settings/ControlsSettingsDialogManager.kt index bb2e2d701aa0b..4d446ef760ae9 100644 --- a/packages/SystemUI/src/com/android/systemui/controls/settings/ControlsSettingsDialogManager.kt +++ b/packages/SystemUI/src/com/android/systemui/controls/settings/ControlsSettingsDialogManager.kt @@ -157,17 +157,18 @@ internal constructor( d.show() } - private fun turnOnSettingSecurely(settings: List) { + private fun turnOnSettingSecurely(settings: List, onComplete: () -> Unit) { val action = ActivityStarter.OnDismissAction { settings.forEach { setting -> secureSettings.putIntForUser(setting, 1, userTracker.userId) } + onComplete() true } activityStarter.dismissKeyguardThenExecute( action, - /* cancel */ null, + /* cancel */ onComplete, /* afterKeyguardGone */ true ) } @@ -188,7 +189,11 @@ internal constructor( if (!showDeviceControlsInLockscreen) { settings.add(Settings.Secure.LOCKSCREEN_SHOW_CONTROLS) } - turnOnSettingSecurely(settings) + // If we are toggling the flag, we want to call onComplete after the keyguard is + // dismissed (and the setting is turned on), to pass the correct value. + turnOnSettingSecurely(settings, onComplete) + } else { + onComplete() } if (attempts != MAX_NUMBER_ATTEMPTS_CONTROLS_DIALOG) { prefs @@ -196,7 +201,6 @@ internal constructor( .putInt(PREFS_SETTINGS_DIALOG_ATTEMPTS, MAX_NUMBER_ATTEMPTS_CONTROLS_DIALOG) .apply() } - onComplete() } override fun onCancel(dialog: DialogInterface?) { diff --git a/packages/SystemUI/tests/src/com/android/systemui/controls/settings/ControlsSettingsDialogManagerImplTest.kt b/packages/SystemUI/tests/src/com/android/systemui/controls/settings/ControlsSettingsDialogManagerImplTest.kt index 0c9986d82447c..97a973297fddc 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/controls/settings/ControlsSettingsDialogManagerImplTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/controls/settings/ControlsSettingsDialogManagerImplTest.kt @@ -45,6 +45,7 @@ import org.junit.runner.RunWith import org.mockito.ArgumentMatchers.anyBoolean import org.mockito.Mock import org.mockito.Mockito.anyInt +import org.mockito.Mockito.doAnswer import org.mockito.Mockito.never import org.mockito.Mockito.verify import org.mockito.Mockito.`when` @@ -231,6 +232,36 @@ class ControlsSettingsDialogManagerImplTest : SysuiTestCase() { verify(completedRunnable).invoke() } + @Test + fun dialogPositiveButtonWhenCalledOnCompleteSettingIsTrue() { + sharedPreferences.putAttempts(0) + secureSettings.putBool(SETTING_SHOW, true) + secureSettings.putBool(SETTING_ACTION, false) + + doAnswer { assertThat(secureSettings.getBool(SETTING_ACTION, false)).isTrue() } + .`when`(completedRunnable) + .invoke() + + underTest.maybeShowDialog(context, completedRunnable) + clickButton(DialogInterface.BUTTON_POSITIVE) + + verify(completedRunnable).invoke() + } + + @Test + fun dialogPositiveCancelKeyguardStillCallsOnComplete() { + `when`(activityStarter.dismissKeyguardThenExecute(any(), nullable(), anyBoolean())) + .thenAnswer { (it.arguments[1] as Runnable).run() } + sharedPreferences.putAttempts(0) + secureSettings.putBool(SETTING_SHOW, true) + secureSettings.putBool(SETTING_ACTION, false) + + underTest.maybeShowDialog(context, completedRunnable) + clickButton(DialogInterface.BUTTON_POSITIVE) + + verify(completedRunnable).invoke() + } + @Test fun dialogCancelDoesntChangeSetting() { sharedPreferences.putAttempts(0)