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 Context context = getContext();
final PowerManager powerManager = context.getSystemService(PowerManager.class); final PowerManager powerManager = context.getSystemService(PowerManager.class);
final ContentResolver contentResolver = context.getContentResolver(); final ContentResolver contentResolver = context.getContentResolver();
return Settings.Global.getInt(contentResolver, return mWifiManager.isAutoWakeupEnabled()
Settings.Global.WIFI_WAKEUP_ENABLED, 0) == 1
&& Settings.Global.getInt(contentResolver, && Settings.Global.getInt(contentResolver,
Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0) == 1 Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0) == 1
&& Settings.Global.getInt(contentResolver, && Settings.Global.getInt(contentResolver,

View File

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

View File

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

View File

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

View File

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

View File

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