Refactor WifiWakeupPreferenceController into TogglePreferenceController
WifiWakeupPreferenceController is essentially reimplementing TogglePreferenceController. Add below 2 changes for test failure of CodeInspectionTest 1. Remove some arguments from constructor of the controller 2. Declare the controller in xml instead of in code Bug: 132391311 Test: manual WifiWakeupPreferenceControllerTest Change-Id: I4aa607f78d5e7de70600a410dfc7267e6bd7d387
This commit is contained in:
@@ -24,7 +24,8 @@
|
||||
android:key="enable_wifi_wakeup"
|
||||
android:title="@string/wifi_wakeup"
|
||||
android:icon="@drawable/ic_auto_wifi"
|
||||
android:summary="@string/wifi_wakeup_summary" />
|
||||
android:summary="@string/wifi_wakeup_summary"
|
||||
settings:controller="com.android.settings.wifi.WifiWakeupPreferenceController"/>
|
||||
|
||||
<SwitchPreference
|
||||
android:key="use_open_wifi_automatically"
|
||||
|
@@ -74,13 +74,10 @@ public class ConfigureWifiSettings extends DashboardFragment {
|
||||
|
||||
@Override
|
||||
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
|
||||
mWifiWakeupPreferenceController = new WifiWakeupPreferenceController(context, this,
|
||||
getSettingsLifecycle());
|
||||
mUseOpenWifiPreferenceController = new UseOpenWifiPreferenceController(context, this,
|
||||
getSettingsLifecycle());
|
||||
final WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
|
||||
final List<AbstractPreferenceController> controllers = new ArrayList<>();
|
||||
controllers.add(mWifiWakeupPreferenceController);
|
||||
controllers.add(new NotifyOpenNetworksPreferenceController(context,
|
||||
getSettingsLifecycle()));
|
||||
controllers.add(mUseOpenWifiPreferenceController);
|
||||
@@ -91,9 +88,17 @@ public class ConfigureWifiSettings extends DashboardFragment {
|
||||
return controllers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
|
||||
mWifiWakeupPreferenceController = use(WifiWakeupPreferenceController.class);
|
||||
mWifiWakeupPreferenceController.setFragment(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (requestCode == WIFI_WAKEUP_REQUEST_CODE && mWifiWakeupPreferenceController != null) {
|
||||
if (requestCode == WIFI_WAKEUP_REQUEST_CODE) {
|
||||
mWifiWakeupPreferenceController.onActivityResult(requestCode, resultCode);
|
||||
return;
|
||||
}
|
||||
|
@@ -25,7 +25,6 @@ import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.location.LocationManager;
|
||||
import android.provider.Settings;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.fragment.app.Fragment;
|
||||
@@ -34,108 +33,94 @@ import androidx.preference.PreferenceScreen;
|
||||
import androidx.preference.SwitchPreference;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.core.TogglePreferenceController;
|
||||
import com.android.settings.utils.AnnotationSpan;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnPause;
|
||||
import com.android.settingslib.core.lifecycle.events.OnResume;
|
||||
|
||||
/**
|
||||
* {@link PreferenceControllerMixin} that controls whether the Wi-Fi Wakeup feature should be
|
||||
* {@link TogglePreferenceController} that controls whether the Wi-Fi Wakeup feature should be
|
||||
* enabled.
|
||||
*/
|
||||
public class WifiWakeupPreferenceController extends AbstractPreferenceController implements
|
||||
public class WifiWakeupPreferenceController extends TogglePreferenceController implements
|
||||
LifecycleObserver, OnPause, OnResume {
|
||||
|
||||
private static final String TAG = "WifiWakeupPrefController";
|
||||
private static final String KEY_ENABLE_WIFI_WAKEUP = "enable_wifi_wakeup";
|
||||
|
||||
private final Fragment mFragment;
|
||||
private Fragment mFragment;
|
||||
|
||||
@VisibleForTesting
|
||||
SwitchPreference mPreference;
|
||||
|
||||
@VisibleForTesting
|
||||
LocationManager mLocationManager;
|
||||
|
||||
private final BroadcastReceiver mLocationReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
updateState(mPreference);
|
||||
}
|
||||
};
|
||||
|
||||
private final IntentFilter mLocationFilter =
|
||||
new IntentFilter(LocationManager.MODE_CHANGED_ACTION);
|
||||
|
||||
public WifiWakeupPreferenceController(Context context, DashboardFragment fragment,
|
||||
Lifecycle lifecycle) {
|
||||
super(context);
|
||||
mFragment = fragment;
|
||||
public WifiWakeupPreferenceController(Context context) {
|
||||
super(context, KEY_ENABLE_WIFI_WAKEUP);
|
||||
mLocationManager = (LocationManager) context.getSystemService(Service.LOCATION_SERVICE);
|
||||
lifecycle.addObserver(this);
|
||||
}
|
||||
|
||||
public void setFragment(Fragment hostFragment) {
|
||||
mFragment = hostFragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mPreference = screen.findPreference(KEY_ENABLE_WIFI_WAKEUP);
|
||||
updateState(mPreference);
|
||||
mPreference = screen.findPreference(getPreferenceKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return true;
|
||||
public int getAvailabilityStatus() {
|
||||
return AVAILABLE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handlePreferenceTreeClick(Preference preference) {
|
||||
if (!TextUtils.equals(preference.getKey(), KEY_ENABLE_WIFI_WAKEUP)) {
|
||||
return false;
|
||||
}
|
||||
if (!(preference instanceof SwitchPreference)) {
|
||||
return false;
|
||||
}
|
||||
public boolean isChecked() {
|
||||
return getWifiWakeupEnabled()
|
||||
&& getWifiScanningEnabled()
|
||||
&& mLocationManager.isLocationEnabled();
|
||||
}
|
||||
|
||||
// TODO(b/132391311): WifiWakeupPreferenceController is essentially reimplementing
|
||||
// TogglePreferenceController. Refactor it into TogglePreferenceController.
|
||||
@Override
|
||||
public boolean setChecked(boolean isChecked) {
|
||||
if (isChecked) {
|
||||
if (mFragment == null) {
|
||||
throw new IllegalStateException("No fragment to start activity");
|
||||
}
|
||||
|
||||
// Toggle wifi-wakeup setting between 1/0 based on its current state, and some other checks.
|
||||
if (isWifiWakeupAvailable()) {
|
||||
setWifiWakeupEnabled(false);
|
||||
} else {
|
||||
if (!mLocationManager.isLocationEnabled()) {
|
||||
final Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
|
||||
mFragment.startActivityForResult(intent, WIFI_WAKEUP_REQUEST_CODE);
|
||||
return true;
|
||||
return false;
|
||||
} else if (!getWifiScanningEnabled()) {
|
||||
showScanningDialog();
|
||||
} else {
|
||||
setWifiWakeupEnabled(true);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
updateState(mPreference);
|
||||
setWifiWakeupEnabled(isChecked);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
return KEY_ENABLE_WIFI_WAKEUP;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
if (!(preference instanceof SwitchPreference)) {
|
||||
return;
|
||||
}
|
||||
final SwitchPreference enableWifiWakeup = (SwitchPreference) preference;
|
||||
|
||||
enableWifiWakeup.setChecked(isWifiWakeupAvailable());
|
||||
public CharSequence getSummary() {
|
||||
if (!mLocationManager.isLocationEnabled()) {
|
||||
preference.setSummary(getNoLocationSummary());
|
||||
return getNoLocationSummary();
|
||||
} else {
|
||||
preference.setSummary(R.string.wifi_wakeup_summary);
|
||||
return mContext.getText(R.string.wifi_wakeup_summary);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,8 +137,8 @@ public class WifiWakeupPreferenceController extends AbstractPreferenceController
|
||||
}
|
||||
if (mLocationManager.isLocationEnabled() && getWifiScanningEnabled()) {
|
||||
setWifiWakeupEnabled(true);
|
||||
updateState(mPreference);
|
||||
}
|
||||
updateState(mPreference);
|
||||
}
|
||||
|
||||
private boolean getWifiScanningEnabled() {
|
||||
@@ -173,15 +158,6 @@ 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);
|
||||
|
@@ -35,7 +35,6 @@ import androidx.preference.SwitchPreference;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -55,14 +54,13 @@ public class WifiWakeupPreferenceControllerTest {
|
||||
private LocationManager mLocationManager;
|
||||
@Mock
|
||||
private SwitchPreference mPreference;
|
||||
@Mock
|
||||
private Lifecycle mLifecycle;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mController = new WifiWakeupPreferenceController(mContext, mFragment, mLifecycle);
|
||||
mController = new WifiWakeupPreferenceController(mContext);
|
||||
mController.setFragment(mFragment);
|
||||
mController.mLocationManager = mLocationManager;
|
||||
mController.mPreference = mPreference;
|
||||
|
||||
@@ -71,42 +69,29 @@ public class WifiWakeupPreferenceControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlePreferenceTreeClick_nonMatchingKey_shouldDoNothing() {
|
||||
final SwitchPreference pref = new SwitchPreference(mContext);
|
||||
public void setChecked_scanEnableLocationEnable_wifiWakeupEnable() {
|
||||
Settings.Global.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 0);
|
||||
Settings.Global.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 1);
|
||||
doReturn(true).when(mLocationManager).isLocationEnabled();
|
||||
|
||||
assertThat(mController.handlePreferenceTreeClick(pref)).isFalse();
|
||||
}
|
||||
mController.setChecked(true);
|
||||
|
||||
@Test
|
||||
public void handlePreferenceTreeClick_nonMatchingType_shouldDoNothing() {
|
||||
final Preference pref = new Preference(mContext);
|
||||
pref.setKey(mController.getPreferenceKey());
|
||||
|
||||
assertThat(mController.handlePreferenceTreeClick(pref)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlePreferenceTreeClick_matchingKeyAndType_shouldUpdateSetting() {
|
||||
final SwitchPreference pref = new SwitchPreference(mContext);
|
||||
pref.setChecked(true);
|
||||
pref.setKey(mController.getPreferenceKey());
|
||||
|
||||
assertThat(mController.handlePreferenceTreeClick(pref)).isTrue();
|
||||
assertThat(Settings.Global.getInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 0))
|
||||
.isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlePreferenceTreeClick_wifiWakeupEnableScanningDisable_wifiWakeupEnable() {
|
||||
public void updateState_wifiWakeupEnableScanningDisable_wifiWakeupDisabled() {
|
||||
final SwitchPreference preference = new SwitchPreference(mContext);
|
||||
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;
|
||||
mController.updateState(preference);
|
||||
|
||||
assertThat(isWifiWakeupEnabled).isTrue();
|
||||
assertThat(preference.isChecked()).isFalse();
|
||||
assertThat(preference.getSummary())
|
||||
.isEqualTo(mContext.getString(R.string.wifi_wakeup_summary));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -114,6 +99,7 @@ public class WifiWakeupPreferenceControllerTest {
|
||||
final SwitchPreference preference = new SwitchPreference(mContext);
|
||||
Settings.Global.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1);
|
||||
Settings.Global.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 1);
|
||||
doReturn(true).when(mLocationManager).isLocationEnabled();
|
||||
|
||||
mController.updateState(preference);
|
||||
|
||||
|
Reference in New Issue
Block a user