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
This commit is contained in:
Arc Wang
2019-05-06 18:29:06 +08:00
parent 30a9392b21
commit 454553ed52
2 changed files with 39 additions and 11 deletions

View File

@@ -97,15 +97,22 @@ public class WifiWakeupPreferenceController extends AbstractPreferenceController
return false; return false;
} }
if (!mLocationManager.isLocationEnabled()) { // TODO(b/132391311): WifiWakeupPreferenceController is essentially reimplementing
final Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); // TogglePreferenceController. Refactor it into TogglePreferenceController.
mFragment.startActivity(intent);
} else if (getWifiWakeupEnabled()) { // Toggle wifi-wakeup setting between 1/0 based on its current state, and some other checks.
if (isWifiWakeupAvailable()) {
setWifiWakeupEnabled(false); setWifiWakeupEnabled(false);
} else if (!getWifiScanningEnabled()) {
showScanningDialog();
} else { } 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); updateState(mPreference);
@@ -124,9 +131,7 @@ public class WifiWakeupPreferenceController extends AbstractPreferenceController
} }
final SwitchPreference enableWifiWakeup = (SwitchPreference) preference; final SwitchPreference enableWifiWakeup = (SwitchPreference) preference;
enableWifiWakeup.setChecked(getWifiWakeupEnabled() enableWifiWakeup.setChecked(isWifiWakeupAvailable());
&& getWifiScanningEnabled()
&& mLocationManager.isLocationEnabled());
if (!mLocationManager.isLocationEnabled()) { if (!mLocationManager.isLocationEnabled()) {
preference.setSummary(getNoLocationSummary()); preference.setSummary(getNoLocationSummary());
} else { } else {
@@ -145,7 +150,7 @@ public class WifiWakeupPreferenceController extends AbstractPreferenceController
if (requestCode != WIFI_WAKEUP_REQUEST_CODE) { if (requestCode != WIFI_WAKEUP_REQUEST_CODE) {
return; return;
} }
if (mLocationManager.isLocationEnabled()) { if (mLocationManager.isLocationEnabled() && getWifiScanningEnabled()) {
setWifiWakeupEnabled(true); setWifiWakeupEnabled(true);
} }
updateState(mPreference); updateState(mPreference);
@@ -168,6 +173,15 @@ public class WifiWakeupPreferenceController extends AbstractPreferenceController
Settings.Global.WIFI_WAKEUP_ENABLED, 0) == 1; 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) { private void setWifiWakeupEnabled(boolean enabled) {
Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.WIFI_WAKEUP_ENABLED, Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.WIFI_WAKEUP_ENABLED,
enabled ? 1 : 0); enabled ? 1 : 0);

View File

@@ -22,6 +22,7 @@ import static android.provider.Settings.Global.WIFI_WAKEUP_ENABLED;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.times;
import android.content.Context; import android.content.Context;
import android.location.LocationManager; import android.location.LocationManager;
@@ -95,6 +96,19 @@ public class WifiWakeupPreferenceControllerTest {
.isEqualTo(1); .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 @Test
public void updateState_preferenceSetCheckedWhenWakeupSettingEnabled() { public void updateState_preferenceSetCheckedWhenWakeupSettingEnabled() {
final SwitchPreference preference = new SwitchPreference(mContext); final SwitchPreference preference = new SwitchPreference(mContext);