Restrict Wi-Fi toggle in Internet Settings
- Disable Wi-Fi toggle when user is not allowed to change Wi-Fi state. - Show restriction message in Wi-Fi toggle summary. - See the result screenshot in b/203168097#comment24 Bug: 203168097 Test: manual test atest -c WifiSwitchPreferenceControllerTest Change-Id: I3cfe2f4f0e855dde91a82babe3a03005c3985d59
This commit is contained in:
@@ -16,19 +16,25 @@
|
||||
|
||||
package com.android.settings.network;
|
||||
|
||||
import static com.android.settingslib.wifi.WifiEnterpriseRestrictionUtils.isChangeWifiStateAllowed;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.wifi.WifiManager;
|
||||
|
||||
import androidx.lifecycle.Lifecycle;
|
||||
import androidx.lifecycle.LifecycleObserver;
|
||||
import androidx.lifecycle.OnLifecycleEvent;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settings.widget.GenericSwitchController;
|
||||
import com.android.settings.wifi.WifiEnabler;
|
||||
import com.android.settingslib.RestrictedSwitchPreference;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
|
||||
/**
|
||||
* This controller helps to manage the state of wifi switch preference.
|
||||
*/
|
||||
@@ -37,9 +43,12 @@ public class WifiSwitchPreferenceController extends AbstractPreferenceController
|
||||
|
||||
public static final String KEY = "main_toggle_wifi";
|
||||
|
||||
private RestrictedSwitchPreference mPreference;
|
||||
@VisibleForTesting
|
||||
boolean mIsChangeWifiStateAllowed;
|
||||
@VisibleForTesting
|
||||
WifiEnabler mWifiEnabler;
|
||||
|
||||
private WifiEnabler mWifiEnabler;
|
||||
private RestrictedSwitchPreference mPreference;
|
||||
|
||||
public WifiSwitchPreferenceController(Context context, Lifecycle lifecycle) {
|
||||
super(context);
|
||||
@@ -48,6 +57,7 @@ public class WifiSwitchPreferenceController extends AbstractPreferenceController
|
||||
}
|
||||
|
||||
lifecycle.addObserver(this);
|
||||
mIsChangeWifiStateAllowed = isChangeWifiStateAllowed(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -64,12 +74,22 @@ public class WifiSwitchPreferenceController extends AbstractPreferenceController
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mPreference = screen.findPreference(getPreferenceKey());
|
||||
if (mPreference == null) return;
|
||||
|
||||
mPreference.setChecked(isWifiEnabled());
|
||||
if (!mIsChangeWifiStateAllowed) {
|
||||
mPreference.setEnabled(false);
|
||||
mPreference.setSummary(R.string.not_allowed_by_ent);
|
||||
}
|
||||
}
|
||||
|
||||
/** Lifecycle.Event.ON_START */
|
||||
@OnLifecycleEvent(Lifecycle.Event.ON_START)
|
||||
public void onStart() {
|
||||
if (mPreference != null) {
|
||||
// Don't use WifiEnabler when user is not allowed to change Wi-Fi state,
|
||||
// Because the preference needs to be disabled when the user is not allowed to change the
|
||||
// Wi-Fi state, but WifiEnabler will enable the preference when the Wi-Fi state changes.
|
||||
if (mPreference != null && mIsChangeWifiStateAllowed) {
|
||||
mWifiEnabler = new WifiEnabler(mContext, new GenericSwitchController(mPreference),
|
||||
FeatureFactory.getFactory(mContext).getMetricsFeatureProvider());
|
||||
}
|
||||
@@ -98,4 +118,10 @@ public class WifiSwitchPreferenceController extends AbstractPreferenceController
|
||||
mWifiEnabler.pause();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isWifiEnabled() {
|
||||
WifiManager wifiManager = mContext.getSystemService(WifiManager.class);
|
||||
if (wifiManager == null) return false;
|
||||
return wifiManager.isWifiEnabled();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user