Settings: Use new API for wifi wakeup feature toggle

Bug: 148514485
Test: m RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.wifi
Test: Verified wifi wakeup feature toggle in wifi settings.
Change-Id: I1e7c0b3ca053a9b7d6105cad16362df38517b51e
This commit is contained in:
Roshan Pius
2020-02-07 09:21:24 -08:00
parent c1313b41a5
commit 65291297a9
6 changed files with 33 additions and 23 deletions

View File

@@ -1020,8 +1020,7 @@ public class WifiSettings extends RestrictedSettingsFragment
final Context context = getContext();
final PowerManager powerManager = context.getSystemService(PowerManager.class);
final ContentResolver contentResolver = context.getContentResolver();
return Settings.Global.getInt(contentResolver,
Settings.Global.WIFI_WAKEUP_ENABLED, 0) == 1
return mWifiManager.isAutoWakeupEnabled()
&& Settings.Global.getInt(contentResolver,
Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0) == 1
&& Settings.Global.getInt(contentResolver,

View File

@@ -851,8 +851,7 @@ public class WifiSettings2 extends RestrictedSettingsFragment
final Context context = getContext();
final PowerManager powerManager = context.getSystemService(PowerManager.class);
final ContentResolver contentResolver = context.getContentResolver();
return Settings.Global.getInt(contentResolver,
Settings.Global.WIFI_WAKEUP_ENABLED, 0) == 1
return mWifiManager.isAutoWakeupEnabled()
&& Settings.Global.getInt(contentResolver,
Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0) == 1
&& Settings.Global.getInt(contentResolver,

View File

@@ -24,6 +24,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.LocationManager;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import androidx.annotation.VisibleForTesting;
@@ -57,6 +58,9 @@ public class WifiWakeupPreferenceController extends TogglePreferenceController i
@VisibleForTesting
LocationManager mLocationManager;
@VisibleForTesting
WifiManager mWifiManager;
private final BroadcastReceiver mLocationReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
@@ -70,6 +74,7 @@ public class WifiWakeupPreferenceController extends TogglePreferenceController i
public WifiWakeupPreferenceController(Context context) {
super(context, KEY_ENABLE_WIFI_WAKEUP);
mLocationManager = (LocationManager) context.getSystemService(Service.LOCATION_SERVICE);
mWifiManager = context.getSystemService(WifiManager.class);
}
public void setFragment(Fragment hostFragment) {
@@ -160,13 +165,11 @@ public class WifiWakeupPreferenceController extends TogglePreferenceController i
}
private boolean getWifiWakeupEnabled() {
return Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.WIFI_WAKEUP_ENABLED, 0) == 1;
return mWifiManager.isAutoWakeupEnabled();
}
private void setWifiWakeupEnabled(boolean enabled) {
Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.WIFI_WAKEUP_ENABLED,
enabled ? 1 : 0);
mWifiManager.setAutoWakeupEnabled(enabled);
}
@Override

View File

@@ -33,6 +33,7 @@ import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.UserManager;
@@ -71,6 +72,8 @@ public class WifiSettings2Test {
@Mock
private PowerManager mPowerManager;
@Mock
private WifiManager mWifiManager;
@Mock
private DataUsagePreference mDataUsagePreference;
private Context mContext;
private WifiSettings2 mWifiSettings2;
@@ -85,10 +88,12 @@ public class WifiSettings2Test {
mWifiSettings2 = spy(new WifiSettings2());
doReturn(mContext).when(mWifiSettings2).getContext();
doReturn(mPowerManager).when(mContext).getSystemService(PowerManager.class);
doReturn(mWifiManager).when(mContext).getSystemService(WifiManager.class);
mWifiSettings2.mAddWifiNetworkPreference = new AddWifiNetworkPreference(mContext);
mWifiSettings2.mSavedNetworksPreference = new Preference(mContext);
mWifiSettings2.mConfigureWifiSettingsPreference = new Preference(mContext);
mWifiSettings2.mWifiPickerTracker = mMockWifiPickerTracker;
mWifiSettings2.mWifiManager = mWifiManager;
}
@Test
@@ -157,7 +162,7 @@ public class WifiSettings2Test {
@Test
public void setAdditionalSettingsSummaries_wifiWakeupEnabled_displayOn() {
final ContentResolver contentResolver = mContext.getContentResolver();
Settings.Global.putInt(contentResolver, Settings.Global.WIFI_WAKEUP_ENABLED, 1);
when(mWifiManager.isAutoWakeupEnabled()).thenReturn(true);
Settings.Global.putInt(contentResolver, Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 1);
Settings.Global.putInt(contentResolver, Settings.Global.AIRPLANE_MODE_ON, 0);
when(mPowerManager.isPowerSaveMode()).thenReturn(false);
@@ -171,7 +176,7 @@ public class WifiSettings2Test {
@Test
public void setAdditionalSettingsSummaries_wifiWakeupDisabled_displayOff() {
final ContentResolver contentResolver = mContext.getContentResolver();
Settings.Global.putInt(contentResolver, Settings.Global.WIFI_WAKEUP_ENABLED, 0);
when(mWifiManager.isAutoWakeupEnabled()).thenReturn(false);
mWifiSettings2.setAdditionalSettingsSummaries();

View File

@@ -186,7 +186,7 @@ public class WifiSettingsTest {
@Test
public void setAdditionalSettingsSummaries_wifiWakeupEnabled_displayOn() {
final ContentResolver contentResolver = mContext.getContentResolver();
Settings.Global.putInt(contentResolver, Settings.Global.WIFI_WAKEUP_ENABLED, 1);
when(mWifiManager.isAutoWakeupEnabled()).thenReturn(true);
Settings.Global.putInt(contentResolver, Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 1);
Settings.Global.putInt(contentResolver, Settings.Global.AIRPLANE_MODE_ON, 0);
when(mPowerManager.isPowerSaveMode()).thenReturn(false);
@@ -200,7 +200,7 @@ public class WifiSettingsTest {
@Test
public void setAdditionalSettingsSummaries_wifiWakeupDisabled_displayOff() {
final ContentResolver contentResolver = mContext.getContentResolver();
Settings.Global.putInt(contentResolver, Settings.Global.WIFI_WAKEUP_ENABLED, 0);
when(mWifiManager.isAutoWakeupEnabled()).thenReturn(false);
mWifiSettings.setAdditionalSettingsSummaries();

View File

@@ -17,15 +17,17 @@
package com.android.settings.wifi;
import static android.provider.Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE;
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 static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.location.LocationManager;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.text.TextUtils;
@@ -53,6 +55,8 @@ public class WifiWakeupPreferenceControllerTest {
@Mock
private LocationManager mLocationManager;
@Mock
private WifiManager mWifiManager;
@Mock
private SwitchPreference mPreference;
@Before
@@ -63,6 +67,7 @@ public class WifiWakeupPreferenceControllerTest {
mController.setFragment(mFragment);
mController.mLocationManager = mLocationManager;
mController.mPreference = mPreference;
mController.mWifiManager = mWifiManager;
Settings.Global.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 1);
doReturn(true).when(mLocationManager).isLocationEnabled();
@@ -70,20 +75,19 @@ public class WifiWakeupPreferenceControllerTest {
@Test
public void setChecked_scanEnableLocationEnable_wifiWakeupEnable() {
Settings.Global.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 0);
when(mWifiManager.isAutoWakeupEnabled()).thenReturn(false);
Settings.Global.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 1);
doReturn(true).when(mLocationManager).isLocationEnabled();
mController.setChecked(true);
assertThat(Settings.Global.getInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 0))
.isEqualTo(1);
verify(mWifiManager).setAutoWakeupEnabled(true);
}
@Test
public void updateState_wifiWakeupEnableScanningDisable_wifiWakeupDisabled() {
final SwitchPreference preference = new SwitchPreference(mContext);
Settings.Global.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1);
when(mWifiManager.isAutoWakeupEnabled()).thenReturn(true);
Settings.Global.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 0);
doReturn(true).when(mLocationManager).isLocationEnabled();
@@ -97,7 +101,7 @@ public class WifiWakeupPreferenceControllerTest {
@Test
public void updateState_preferenceSetCheckedWhenWakeupSettingEnabled() {
final SwitchPreference preference = new SwitchPreference(mContext);
Settings.Global.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1);
when(mWifiManager.isAutoWakeupEnabled()).thenReturn(true);
Settings.Global.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 1);
doReturn(true).when(mLocationManager).isLocationEnabled();
@@ -111,7 +115,7 @@ public class WifiWakeupPreferenceControllerTest {
@Test
public void updateState_preferenceSetUncheckedWhenWakeupSettingDisabled() {
final SwitchPreference preference = new SwitchPreference(mContext);
Settings.Global.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 0);
when(mWifiManager.isAutoWakeupEnabled()).thenReturn(false);
mController.updateState(preference);
@@ -123,7 +127,7 @@ public class WifiWakeupPreferenceControllerTest {
@Test
public void updateState_preferenceSetUncheckedWhenWifiScanningDisabled() {
final SwitchPreference preference = new SwitchPreference(mContext);
Settings.Global.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1);
when(mWifiManager.isAutoWakeupEnabled()).thenReturn(true);
Settings.Global.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 0);
mController.updateState(preference);
@@ -134,7 +138,7 @@ public class WifiWakeupPreferenceControllerTest {
@Test
public void updateState_preferenceSetUncheckedWhenWakeupSettingEnabledNoLocation() {
final SwitchPreference preference = new SwitchPreference(mContext);
Settings.Global.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1);
when(mWifiManager.isAutoWakeupEnabled()).thenReturn(true);
doReturn(false).when(mLocationManager).isLocationEnabled();
mController.updateState(preference);
@@ -147,7 +151,7 @@ public class WifiWakeupPreferenceControllerTest {
@Test
public void updateState_preferenceSetUncheckedWhenWakeupSettingDisabledLocationEnabled() {
final SwitchPreference preference = new SwitchPreference(mContext);
Settings.Global.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 0);
when(mWifiManager.isAutoWakeupEnabled()).thenReturn(false);
doReturn(false).when(mLocationManager).isLocationEnabled();
mController.updateState(preference);
@@ -160,7 +164,7 @@ public class WifiWakeupPreferenceControllerTest {
@Test
public void updateState_preferenceSetUncheckedWhenWifiScanningDisabledLocationEnabled() {
final SwitchPreference preference = new SwitchPreference(mContext);
Settings.Global.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1);
when(mWifiManager.isAutoWakeupEnabled()).thenReturn(true);
Settings.Global.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 0);
doReturn(false).when(mLocationManager).isLocationEnabled();