diff options
author | 2023-10-30 13:53:48 +0900 | |
---|---|---|
committer | 2023-10-30 14:25:05 +0900 | |
commit | 592a712a734e2bdfcd209162bce7c3dda446bf14 (patch) | |
tree | 55e1df80662f3a8cf33f64535d5bdc32099560d0 | |
parent | a4cad3be5a66e63079c02004179a929cf0d80a3b (diff) |
Close dialogs on power button press
Checking for screen off when we get ACTION_CLOSE_SYSTEM_DIALOGS isn't
reliable for differentiating whether the action came from the home
button or the power button. For simplicity and consistency, close on
all ACTION_CLOSE_SYSTEM_DIALOGS including the power button press.
Also add an extra check for EXTRA_CLOSE_SYSTEM_DIALOGS_EXCEPT_WIFI,
which was erroneously removed previously.
Bug: 297981585
Test: atest WifiDialogManagerTest, manually verify "adb shell cmd wifi
launch-dialog-simple -t Title" and then pressing power button will close
the dialog.
Change-Id: Ibc6fdaed570760ecaf12d453fe9d40219fc30adc
3 files changed, 69 insertions, 79 deletions
diff --git a/WifiDialog/src/com/android/wifi/dialog/WifiDialogActivity.java b/WifiDialog/src/com/android/wifi/dialog/WifiDialogActivity.java index 198e07d532..7bbfed3b8a 100644 --- a/WifiDialog/src/com/android/wifi/dialog/WifiDialogActivity.java +++ b/WifiDialog/src/com/android/wifi/dialog/WifiDialogActivity.java @@ -35,7 +35,6 @@ import android.os.Bundle; import android.os.CountDownTimer; import android.os.Handler; import android.os.Looper; -import android.os.PowerManager; import android.os.SystemClock; import android.os.Vibrator; import android.text.Editable; @@ -144,26 +143,24 @@ public class WifiDialogActivity extends Activity { return mWifiManager; } - private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - if (intent.getAction() != Intent.ACTION_CLOSE_SYSTEM_DIALOGS) { - return; - } - PowerManager powerManager = context.getSystemService(PowerManager.class); - if (powerManager == null) { - return; - } - if (!powerManager.isInteractive()) { - // Ignore screen off case. - return; - } - // Cancel all dialogs for ACTION_CLOSE_SYSTEM_DIALOGS (e.g. Home button pressed). - for (int i = 0; i < mActiveDialogsPerId.size(); i++) { - mActiveDialogsPerId.valueAt(i).cancel(); - } - } - }; + private final BroadcastReceiver mBroadcastReceiver = + new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + if (!Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) { + return; + } + if (intent.getBooleanExtra( + WifiManager.EXTRA_CLOSE_SYSTEM_DIALOGS_EXCEPT_WIFI, false)) { + return; + } + // Cancel all dialogs for ACTION_CLOSE_SYSTEM_DIALOGS (e.g. Home button + // pressed). + for (int i = 0; i < mActiveDialogsPerId.size(); i++) { + mActiveDialogsPerId.valueAt(i).cancel(); + } + } + }; @Override protected void onCreate(Bundle savedInstanceState) { diff --git a/service/java/com/android/server/wifi/WifiDialogManager.java b/service/java/com/android/server/wifi/WifiDialogManager.java index 6c35e60dc0..b7a0c40a48 100644 --- a/service/java/com/android/server/wifi/WifiDialogManager.java +++ b/service/java/com/android/server/wifi/WifiDialogManager.java @@ -26,7 +26,6 @@ import android.content.IntentFilter; import android.net.Uri; import android.net.wifi.WifiContext; import android.net.wifi.WifiManager; -import android.os.PowerManager; import android.os.UserHandle; import android.provider.Browser; import android.text.SpannableString; @@ -81,48 +80,54 @@ public class WifiDialogManager { private final @NonNull WifiThreadRunner mWifiThreadRunner; private final @NonNull FrameworkFacade mFrameworkFacade; - private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - mWifiThreadRunner.post(() -> { - String action = intent.getAction(); - if (mVerboseLoggingEnabled) { - Log.v(TAG, "Received action: " + action); + private final BroadcastReceiver mBroadcastReceiver = + new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + mWifiThreadRunner.post( + () -> { + String action = intent.getAction(); + if (mVerboseLoggingEnabled) { + Log.v(TAG, "Received action: " + action); + } + if (Intent.ACTION_SCREEN_OFF.equals(action)) { + // Change all window types to TYPE_APPLICATION_OVERLAY to + // prevent the dialogs from appearing over the lock screen when + // the screen turns on again. + for (LegacySimpleDialogHandle dialogHandle : + mActiveLegacySimpleDialogs) { + dialogHandle.changeWindowType( + WindowManager.LayoutParams + .TYPE_APPLICATION_OVERLAY); + } + } else if (Intent.ACTION_USER_PRESENT.equals(action)) { + // Change all window types to TYPE_KEYGUARD_DIALOG to show the + // dialogs over the QuickSettings after the screen is unlocked. + for (LegacySimpleDialogHandle dialogHandle : + mActiveLegacySimpleDialogs) { + dialogHandle.changeWindowType( + WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); + } + } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) { + if (intent.getBooleanExtra( + WifiManager.EXTRA_CLOSE_SYSTEM_DIALOGS_EXCEPT_WIFI, + false)) { + return; + } + if (mVerboseLoggingEnabled) { + Log.v( + TAG, + "ACTION_CLOSE_SYSTEM_DIALOGS received, cancelling" + + " all legacy dialogs."); + } + for (LegacySimpleDialogHandle dialogHandle : + mActiveLegacySimpleDialogs) { + dialogHandle.cancelDialog(); + } + } + }); } - if (Intent.ACTION_SCREEN_OFF.equals(action)) { - // Change all window types to TYPE_APPLICATION_OVERLAY to prevent the dialogs - // from appearing over the lock screen when the screen turns on again. - for (LegacySimpleDialogHandle dialogHandle : mActiveLegacySimpleDialogs) { - dialogHandle.changeWindowType( - WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY); - } - } else if (Intent.ACTION_USER_PRESENT.equals(action)) { - // Change all window types to TYPE_KEYGUARD_DIALOG to show the dialogs over the - // QuickSettings after the screen is unlocked. - for (LegacySimpleDialogHandle dialogHandle : mActiveLegacySimpleDialogs) { - dialogHandle.changeWindowType( - WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); - } - } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) { - if (intent.getBooleanExtra( - WifiManager.EXTRA_CLOSE_SYSTEM_DIALOGS_EXCEPT_WIFI, false)) { - return; - } - if (!context.getSystemService(PowerManager.class).isInteractive()) { - // Do not cancel dialogs for ACTION_CLOSE_SYSTEM_DIALOGS due to screen off. - return; - } - if (mVerboseLoggingEnabled) { - Log.v(TAG, "ACTION_CLOSE_SYSTEM_DIALOGS received while screen on," - + " cancelling all legacy dialogs."); - } - for (LegacySimpleDialogHandle dialogHandle : mActiveLegacySimpleDialogs) { - dialogHandle.cancelDialog(); - } - } - }); - } - }; + }; /** * Constructs a WifiDialogManager diff --git a/service/tests/wifitests/src/com/android/server/wifi/WifiDialogManagerTest.java b/service/tests/wifitests/src/com/android/server/wifi/WifiDialogManagerTest.java index dc837ebe2d..bf347639f7 100644 --- a/service/tests/wifitests/src/com/android/server/wifi/WifiDialogManagerTest.java +++ b/service/tests/wifitests/src/com/android/server/wifi/WifiDialogManagerTest.java @@ -42,7 +42,6 @@ import android.content.res.Resources; import android.net.wifi.WifiContext; import android.net.wifi.WifiManager; import android.os.Bundle; -import android.os.PowerManager; import android.os.UserHandle; import android.view.Display; import android.view.Window; @@ -82,7 +81,6 @@ public class WifiDialogManagerTest extends WifiBaseTest { @Mock WifiThreadRunner mWifiThreadRunner; @Mock FrameworkFacade mFrameworkFacade; @Mock Resources mResources; - @Mock PowerManager mPowerManager; @Mock ActivityManager mActivityManager; private WifiDialogManager mDialogManager; @@ -90,10 +88,8 @@ public class WifiDialogManagerTest extends WifiBaseTest { public void setUp() throws Exception { MockitoAnnotations.initMocks(this); when(mWifiContext.getWifiDialogApkPkgName()).thenReturn(WIFI_DIALOG_APK_PKG_NAME); - when(mWifiContext.getSystemService(PowerManager.class)).thenReturn(mPowerManager); when(mWifiContext.getSystemService(ActivityManager.class)).thenReturn(mActivityManager); when(mWifiContext.getResources()).thenReturn(mResources); - when(mPowerManager.isInteractive()).thenReturn(true); doThrow(SecurityException.class).when(mWifiContext).startActivityAsUser(any(), any(), any()); mDialogManager = @@ -765,18 +761,10 @@ public class WifiDialogManagerTest extends WifiBaseTest { verify(dialog, never()).cancel(); - // ACTION_CLOSE_SYSTEM_DIALOGS due to screen off should be ignored. - when(mPowerManager.isInteractive()).thenReturn(false); - broadcastReceiverCaptor.getValue().onReceive(mWifiContext, - new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); - dispatchMockWifiThreadRunner(mWifiThreadRunner); - - verify(dialog, never()).cancel(); - - // ACTION_CLOSE_SYSTEM_DIALOGS while screen on should cancel the dialog. - when(mPowerManager.isInteractive()).thenReturn(true); - broadcastReceiverCaptor.getValue().onReceive(mWifiContext, - new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); + // ACTION_CLOSE_SYSTEM_DIALOGS without the extra should cancel the dialog. + broadcastReceiverCaptor + .getValue() + .onReceive(mWifiContext, new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); dispatchMockWifiThreadRunner(mWifiThreadRunner); verify(dialog).cancel(); |