Show restricted message in Wi-Fi hotspot summary

- Refine WifiTetherPreferenceController

- See the result screenshot in b/203168953#comment8

Bug: 203168953
Test: manual test
make RunSettingsRoboTests \
    ROBOTEST_FILTER=WifiTetherPreferenceControllerTest

Change-Id: If094178eb0cd9ccf20ff3899dc4b087b45c66f6b
This commit is contained in:
Weng Su
2022-04-29 02:22:05 +08:00
parent 27aa5f9d94
commit 4e34e65228
2 changed files with 51 additions and 46 deletions

View File

@@ -46,12 +46,9 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
private static final String WIFI_TETHER_SETTINGS = "wifi_tether";
private final TetheringManager mTetheringManager;
private final String[] mWifiRegexs;
private final WifiManager mWifiManager;
private final Lifecycle mLifecycle;
@VisibleForTesting
boolean mIsWifiTetheringAllow;
private boolean mIsWifiTetherable;
private WifiManager mWifiManager;
private boolean mIsWifiTetheringAllow;
private int mSoftApState;
@VisibleForTesting
Preference mPreference;
@@ -59,18 +56,32 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
WifiTetherSoftApManager mWifiTetherSoftApManager;
public WifiTetherPreferenceController(Context context, Lifecycle lifecycle) {
this(context, lifecycle, true /* initSoftApManager */);
this(context, lifecycle,
context.getSystemService(WifiManager.class),
context.getSystemService(TetheringManager.class),
true /* initSoftApManager */,
WifiEnterpriseRestrictionUtils.isWifiTetheringAllowed(context));
}
@VisibleForTesting
WifiTetherPreferenceController(Context context, Lifecycle lifecycle,
boolean initSoftApManager) {
WifiTetherPreferenceController(
Context context,
Lifecycle lifecycle,
WifiManager wifiManager,
TetheringManager tetheringManager,
boolean initSoftApManager,
boolean isWifiTetheringAllow) {
super(context);
mTetheringManager = context.getSystemService(TetheringManager.class);
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
mWifiRegexs = mTetheringManager.getTetherableWifiRegexs();
mIsWifiTetheringAllow = WifiEnterpriseRestrictionUtils.isWifiTetheringAllowed(context);
mLifecycle = lifecycle;
final String[] wifiRegexs = tetheringManager.getTetherableWifiRegexs();
if (wifiRegexs != null && wifiRegexs.length != 0) {
mIsWifiTetherable = true;
}
mIsWifiTetheringAllow = isWifiTetheringAllow;
if (!isWifiTetheringAllow) return;
mWifiManager = wifiManager;
if (lifecycle != null) {
lifecycle.addObserver(this);
}
@@ -81,9 +92,7 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
@Override
public boolean isAvailable() {
return mWifiRegexs != null
&& mWifiRegexs.length != 0
&& !Utils.isMonkeyRunning();
return mIsWifiTetherable && !Utils.isMonkeyRunning();
}
@Override
@@ -94,7 +103,10 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
// unavailable
return;
}
mPreference.setEnabled(mIsWifiTetheringAllow);
if (!mIsWifiTetheringAllow && mPreference.isEnabled()) {
mPreference.setEnabled(false);
mPreference.setSummary(R.string.not_allowed_by_ent);
}
}
@Override

View File

@@ -19,30 +19,29 @@ package com.android.settings.wifi.tether;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.net.TetheringManager;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.UserManager;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.PrimarySwitchPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
@@ -56,53 +55,44 @@ public class WifiTetherPreferenceControllerTest {
private static final String SSID = "Pixel";
private Context mContext;
@Rule
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Spy
Context mContext = ApplicationProvider.getApplicationContext();
@Mock
private Lifecycle mLifecycle;
@Mock
private TetheringManager mTetheringManager;
@Mock
private WifiManager mWifiManager;
@Mock
private UserManager mUserManager;
@Mock
private Bundle mBundle;
@Mock
private PreferenceScreen mScreen;
private SoftApConfiguration mSoftApConfiguration;
private WifiTetherPreferenceController mController;
private Lifecycle mLifecycle;
private LifecycleOwner mLifecycleOwner;
private PrimarySwitchPreference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner);
FakeFeatureFactory.setupForTest();
mPreference = new PrimarySwitchPreference(RuntimeEnvironment.application);
mPreference = new PrimarySwitchPreference(mContext);
when(mContext.getSystemService(Context.TETHERING_SERVICE)).thenReturn(mTetheringManager);
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);
mSoftApConfiguration = new SoftApConfiguration.Builder().setSsid(SSID).build();
when(mWifiManager.getSoftApConfiguration()).thenReturn(mSoftApConfiguration);
when(mTetheringManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"});
mController = new WifiTetherPreferenceController(mContext, mLifecycle,
false /* initSoftApManager */);
mController.mIsWifiTetheringAllow = true;
mController = new WifiTetherPreferenceController(mContext, mLifecycle, mWifiManager,
mTetheringManager, false /* initSoftApManager */, true /* isWifiTetheringAllow */);
mController.displayPreference(mScreen);
}
@Test
public void isAvailable_noTetherRegex_shouldReturnFalse() {
when(mTetheringManager.getTetherableWifiRegexs()).thenReturn(new String[]{});
mController = new WifiTetherPreferenceController(mContext, mLifecycle,
false /* initSoftApManager */);
mController = new WifiTetherPreferenceController(mContext, mLifecycle, mWifiManager,
mTetheringManager, false /* initSoftApManager */, true /* isWifiTetheringAllow */);
assertThat(mController.isAvailable()).isFalse();
}
@@ -114,16 +104,19 @@ public class WifiTetherPreferenceControllerTest {
@Test
public void displayPreference_wifiTetheringNotAllowed_shouldDisable() {
mController.mIsWifiTetheringAllow = false;
mController = new WifiTetherPreferenceController(mContext, mLifecycle, mWifiManager,
mTetheringManager, false /* initSoftApManager */, false /* isWifiTetheringAllow */);
mController.displayPreference(mScreen);
assertThat(mPreference.isEnabled()).isFalse();
assertThat(mPreference.getSummary()).isEqualTo("Not allowed by your organization");
}
@Test
public void displayPreference_wifiTetheringAllowed_shouldEnable() {
mController.mIsWifiTetheringAllow = true;
mController = new WifiTetherPreferenceController(mContext, mLifecycle, mWifiManager,
mTetheringManager, false /* initSoftApManager */, true /* isWifiTetheringAllow */);
mController.displayPreference(mScreen);