Merge "To disable Wi-Fi tethering when user restriction is set"
This commit is contained in:
@@ -36,6 +36,7 @@ import com.android.settingslib.core.lifecycle.Lifecycle;
|
|||||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||||
import com.android.settingslib.core.lifecycle.events.OnStart;
|
import com.android.settingslib.core.lifecycle.events.OnStart;
|
||||||
import com.android.settingslib.core.lifecycle.events.OnStop;
|
import com.android.settingslib.core.lifecycle.events.OnStop;
|
||||||
|
import com.android.settingslib.wifi.WifiEnterpriseRestrictionUtils;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -48,6 +49,8 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
|
|||||||
private final String[] mWifiRegexs;
|
private final String[] mWifiRegexs;
|
||||||
private final WifiManager mWifiManager;
|
private final WifiManager mWifiManager;
|
||||||
private final Lifecycle mLifecycle;
|
private final Lifecycle mLifecycle;
|
||||||
|
@VisibleForTesting
|
||||||
|
boolean mIsWifiTetheringAllow;
|
||||||
private int mSoftApState;
|
private int mSoftApState;
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
Preference mPreference;
|
Preference mPreference;
|
||||||
@@ -65,6 +68,7 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
|
|||||||
mTetheringManager = context.getSystemService(TetheringManager.class);
|
mTetheringManager = context.getSystemService(TetheringManager.class);
|
||||||
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
|
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
|
||||||
mWifiRegexs = mTetheringManager.getTetherableWifiRegexs();
|
mWifiRegexs = mTetheringManager.getTetherableWifiRegexs();
|
||||||
|
mIsWifiTetheringAllow = WifiEnterpriseRestrictionUtils.isWifiTetheringAllowed(context);
|
||||||
mLifecycle = lifecycle;
|
mLifecycle = lifecycle;
|
||||||
if (lifecycle != null) {
|
if (lifecycle != null) {
|
||||||
lifecycle.addObserver(this);
|
lifecycle.addObserver(this);
|
||||||
@@ -89,6 +93,7 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
|
|||||||
// unavailable
|
// unavailable
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
mPreference.setEnabled(mIsWifiTetheringAllow);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -26,6 +26,8 @@ import android.content.Context;
|
|||||||
import android.net.TetheringManager;
|
import android.net.TetheringManager;
|
||||||
import android.net.wifi.SoftApConfiguration;
|
import android.net.wifi.SoftApConfiguration;
|
||||||
import android.net.wifi.WifiManager;
|
import android.net.wifi.WifiManager;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.os.UserManager;
|
||||||
|
|
||||||
import androidx.lifecycle.LifecycleOwner;
|
import androidx.lifecycle.LifecycleOwner;
|
||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
@@ -60,6 +62,10 @@ public class WifiTetherPreferenceControllerTest {
|
|||||||
@Mock
|
@Mock
|
||||||
private WifiManager mWifiManager;
|
private WifiManager mWifiManager;
|
||||||
@Mock
|
@Mock
|
||||||
|
private UserManager mUserManager;
|
||||||
|
@Mock
|
||||||
|
private Bundle mBundle;
|
||||||
|
@Mock
|
||||||
private PreferenceScreen mScreen;
|
private PreferenceScreen mScreen;
|
||||||
private SoftApConfiguration mSoftApConfiguration;
|
private SoftApConfiguration mSoftApConfiguration;
|
||||||
|
|
||||||
@@ -79,6 +85,8 @@ public class WifiTetherPreferenceControllerTest {
|
|||||||
mPreference = new PrimarySwitchPreference(RuntimeEnvironment.application);
|
mPreference = new PrimarySwitchPreference(RuntimeEnvironment.application);
|
||||||
when(mContext.getSystemService(Context.TETHERING_SERVICE)).thenReturn(mTetheringManager);
|
when(mContext.getSystemService(Context.TETHERING_SERVICE)).thenReturn(mTetheringManager);
|
||||||
when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
|
when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
|
||||||
|
when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
|
||||||
|
when(mUserManager.getUserRestrictions()).thenReturn(mBundle);
|
||||||
when(mScreen.findPreference(anyString())).thenReturn(mPreference);
|
when(mScreen.findPreference(anyString())).thenReturn(mPreference);
|
||||||
mSoftApConfiguration = new SoftApConfiguration.Builder().setSsid(SSID).build();
|
mSoftApConfiguration = new SoftApConfiguration.Builder().setSsid(SSID).build();
|
||||||
when(mWifiManager.getSoftApConfiguration()).thenReturn(mSoftApConfiguration);
|
when(mWifiManager.getSoftApConfiguration()).thenReturn(mSoftApConfiguration);
|
||||||
@@ -86,6 +94,7 @@ public class WifiTetherPreferenceControllerTest {
|
|||||||
when(mTetheringManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"});
|
when(mTetheringManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"});
|
||||||
mController = new WifiTetherPreferenceController(mContext, mLifecycle,
|
mController = new WifiTetherPreferenceController(mContext, mLifecycle,
|
||||||
false /* initSoftApManager */);
|
false /* initSoftApManager */);
|
||||||
|
mController.mIsWifiTetheringAllow = true;
|
||||||
mController.displayPreference(mScreen);
|
mController.displayPreference(mScreen);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,6 +112,24 @@ public class WifiTetherPreferenceControllerTest {
|
|||||||
assertThat(mController.isAvailable()).isTrue();
|
assertThat(mController.isAvailable()).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void displayPreference_wifiTetheringNotAllowed_shouldDisable() {
|
||||||
|
mController.mIsWifiTetheringAllow = false;
|
||||||
|
|
||||||
|
mController.displayPreference(mScreen);
|
||||||
|
|
||||||
|
assertThat(mPreference.isEnabled()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void displayPreference_wifiTetheringAllowed_shouldEnable() {
|
||||||
|
mController.mIsWifiTetheringAllow = true;
|
||||||
|
|
||||||
|
mController.displayPreference(mScreen);
|
||||||
|
|
||||||
|
assertThat(mPreference.isEnabled()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHandleWifiApStateChanged_stateEnabling_showEnablingSummary() {
|
public void testHandleWifiApStateChanged_stateEnabling_showEnablingSummary() {
|
||||||
mController.handleWifiApStateChanged(WifiManager.WIFI_AP_STATE_ENABLING, 0 /* reason */);
|
mController.handleWifiApStateChanged(WifiManager.WIFI_AP_STATE_ENABLING, 0 /* reason */);
|
||||||
|
Reference in New Issue
Block a user