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
diff --git a/src/com/android/settings/wifi/WifiWakeupPreferenceController.java b/src/com/android/settings/wifi/WifiWakeupPreferenceController.java
index 2726de4..11a58af 100644
--- a/src/com/android/settings/wifi/WifiWakeupPreferenceController.java
+++ b/src/com/android/settings/wifi/WifiWakeupPreferenceController.java
@@ -97,15 +97,22 @@
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 @@
}
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 @@
if (requestCode != WIFI_WAKEUP_REQUEST_CODE) {
return;
}
- if (mLocationManager.isLocationEnabled()) {
+ if (mLocationManager.isLocationEnabled() && getWifiScanningEnabled()) {
setWifiWakeupEnabled(true);
}
updateState(mPreference);
@@ -168,6 +173,15 @@
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 ab5f4ea..81ba9a2 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 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;
@@ -96,6 +97,19 @@
}
@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);
Settings.Global.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1);