From 454553ed52380b1eeac9fa517fecbcf6b5efee4d Mon Sep 17 00:00:00 2001 From: Arc Wang Date: Mon, 6 May 2019 18:29:06 +0800 Subject: [PATCH] Fix "Turn on Wi-Fi scanning?" does not pop up when clicking "Turn on Wi-Fi automatically" We should make sure both 'Use location' & 'Wi-Fi scanning' are enabled before calling setWifiWakeupEnabled. Bug: 131777439 Test: manual Change-Id: I602917cfa7c5581ecb414e8c44b4e20c8f9ea78d --- .../wifi/WifiWakeupPreferenceController.java | 36 +++++++++++++------ .../WifiWakeupPreferenceControllerTest.java | 14 ++++++++ 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/com/android/settings/wifi/WifiWakeupPreferenceController.java b/src/com/android/settings/wifi/WifiWakeupPreferenceController.java index 2726de49df2..11a58af4c4a 100644 --- a/src/com/android/settings/wifi/WifiWakeupPreferenceController.java +++ b/src/com/android/settings/wifi/WifiWakeupPreferenceController.java @@ -97,15 +97,22 @@ public class WifiWakeupPreferenceController extends AbstractPreferenceController return false; } - if (!mLocationManager.isLocationEnabled()) { - final Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); - mFragment.startActivity(intent); - } else if (getWifiWakeupEnabled()) { + // TODO(b/132391311): WifiWakeupPreferenceController is essentially reimplementing + // TogglePreferenceController. Refactor it into TogglePreferenceController. + + // Toggle wifi-wakeup setting between 1/0 based on its current state, and some other checks. + if (isWifiWakeupAvailable()) { setWifiWakeupEnabled(false); - } else if (!getWifiScanningEnabled()) { - showScanningDialog(); } else { - setWifiWakeupEnabled(true); + if (!mLocationManager.isLocationEnabled()) { + final Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); + mFragment.startActivityForResult(intent, WIFI_WAKEUP_REQUEST_CODE); + return true; + } else if (!getWifiScanningEnabled()) { + showScanningDialog(); + } else { + setWifiWakeupEnabled(true); + } } updateState(mPreference); @@ -124,9 +131,7 @@ public class WifiWakeupPreferenceController extends AbstractPreferenceController } final SwitchPreference enableWifiWakeup = (SwitchPreference) preference; - enableWifiWakeup.setChecked(getWifiWakeupEnabled() - && getWifiScanningEnabled() - && mLocationManager.isLocationEnabled()); + enableWifiWakeup.setChecked(isWifiWakeupAvailable()); if (!mLocationManager.isLocationEnabled()) { preference.setSummary(getNoLocationSummary()); } else { @@ -145,7 +150,7 @@ public class WifiWakeupPreferenceController extends AbstractPreferenceController if (requestCode != WIFI_WAKEUP_REQUEST_CODE) { return; } - if (mLocationManager.isLocationEnabled()) { + if (mLocationManager.isLocationEnabled() && getWifiScanningEnabled()) { setWifiWakeupEnabled(true); } updateState(mPreference); @@ -168,6 +173,15 @@ public class WifiWakeupPreferenceController extends AbstractPreferenceController Settings.Global.WIFI_WAKEUP_ENABLED, 0) == 1; } + /** + * Wifi wakeup is available only when both location and Wi-Fi scanning are enabled. + */ + private boolean isWifiWakeupAvailable() { + return getWifiWakeupEnabled() + && getWifiScanningEnabled() + && mLocationManager.isLocationEnabled(); + } + private void setWifiWakeupEnabled(boolean enabled) { Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.WIFI_WAKEUP_ENABLED, enabled ? 1 : 0); diff --git a/tests/robotests/src/com/android/settings/wifi/WifiWakeupPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/wifi/WifiWakeupPreferenceControllerTest.java index ab5f4ea42bb..81ba9a23c94 100644 --- a/tests/robotests/src/com/android/settings/wifi/WifiWakeupPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/wifi/WifiWakeupPreferenceControllerTest.java @@ -22,6 +22,7 @@ import static android.provider.Settings.Global.WIFI_WAKEUP_ENABLED; import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.times; import android.content.Context; import android.location.LocationManager; @@ -95,6 +96,19 @@ public class WifiWakeupPreferenceControllerTest { .isEqualTo(1); } + @Test + public void handlePreferenceTreeClick_wifiWakeupEnableScanningDisable_wifiWakeupEnable() { + Settings.Global.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1); + Settings.Global.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 0); + doReturn(true).when(mLocationManager).isLocationEnabled(); + + mController.handlePreferenceTreeClick(mPreference); + final boolean isWifiWakeupEnabled = Settings.Global.getInt(mContext.getContentResolver(), + Settings.Global.WIFI_WAKEUP_ENABLED, 0) == 1; + + assertThat(isWifiWakeupEnabled).isTrue(); + } + @Test public void updateState_preferenceSetCheckedWhenWakeupSettingEnabled() { final SwitchPreference preference = new SwitchPreference(mContext);