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

View File

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