Merge "Restrict Wi-Fi toggle in Internet Settings" into tm-dev am: 59c849da4d

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/17628094

Change-Id: I2b91d4ac1320ce79037c073f913800f0f0ca785c
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
TreeHugger Robot
2022-04-07 11:54:52 +00:00
committed by Automerger Merge Worker
2 changed files with 124 additions and 22 deletions

View File

@@ -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();
}
}