Disable toggling wifi tethering in secondary user.
- in previous release, the shortcut widget was for the top level Hotspot & Tethering settings page. The top level settings has logic to check whether the page is restricted and remove all preferences and show a message to tell the user that tethering settings are not available, and the user will not be able to launch the wifi hotspot settings page. - the updated shortcut now launches the wifi hotspot page directly. The settings does not check for restriction. Copy the logic from the top level settings to check for restriction and remove all preferences accordingly. Change-Id: I76fb7838e2db379f6ffbce7bf14003bccc1b10d3 Fixes: 116642428 Test: make RunSettingsRoboTests
This commit is contained in:
@@ -71,6 +71,7 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
|
|||||||
|
|
||||||
private WifiManager mWifiManager;
|
private WifiManager mWifiManager;
|
||||||
private boolean mRestartWifiApAfterConfigChange;
|
private boolean mRestartWifiApAfterConfigChange;
|
||||||
|
private boolean mUnavailable;
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
TetherChangeReceiver mTetherChangeReceiver;
|
TetherChangeReceiver mTetherChangeReceiver;
|
||||||
@@ -94,6 +95,15 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
|
|||||||
return "WifiTetherSettings";
|
return "WifiTetherSettings";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate(Bundle icicle) {
|
||||||
|
super.onCreate(icicle);
|
||||||
|
setIfOnlyAvailableForAdmins(true);
|
||||||
|
if (isUiRestricted()) {
|
||||||
|
mUnavailable = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAttach(Context context) {
|
public void onAttach(Context context) {
|
||||||
super.onAttach(context);
|
super.onAttach(context);
|
||||||
@@ -109,6 +119,9 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
|
|||||||
@Override
|
@Override
|
||||||
public void onActivityCreated(Bundle savedInstanceState) {
|
public void onActivityCreated(Bundle savedInstanceState) {
|
||||||
super.onActivityCreated(savedInstanceState);
|
super.onActivityCreated(savedInstanceState);
|
||||||
|
if (mUnavailable) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
// Assume we are in a SettingsActivity. This is only safe because we currently use
|
// Assume we are in a SettingsActivity. This is only safe because we currently use
|
||||||
// SettingsActivity as base for all preference fragments.
|
// SettingsActivity as base for all preference fragments.
|
||||||
final SettingsActivity activity = (SettingsActivity) getActivity();
|
final SettingsActivity activity = (SettingsActivity) getActivity();
|
||||||
@@ -122,6 +135,13 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
|
|||||||
@Override
|
@Override
|
||||||
public void onStart() {
|
public void onStart() {
|
||||||
super.onStart();
|
super.onStart();
|
||||||
|
if (mUnavailable) {
|
||||||
|
if (!isUiRestrictedByOnlyAdmin()) {
|
||||||
|
getEmptyTextView().setText(R.string.tethering_settings_not_available);
|
||||||
|
}
|
||||||
|
getPreferenceScreen().removeAll();
|
||||||
|
return;
|
||||||
|
}
|
||||||
final Context context = getContext();
|
final Context context = getContext();
|
||||||
if (context != null) {
|
if (context != null) {
|
||||||
context.registerReceiver(mTetherChangeReceiver, TETHER_STATE_CHANGE_FILTER);
|
context.registerReceiver(mTetherChangeReceiver, TETHER_STATE_CHANGE_FILTER);
|
||||||
@@ -131,6 +151,9 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
|
|||||||
@Override
|
@Override
|
||||||
public void onStop() {
|
public void onStop() {
|
||||||
super.onStop();
|
super.onStop();
|
||||||
|
if (mUnavailable) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
final Context context = getContext();
|
final Context context = getContext();
|
||||||
if (context != null) {
|
if (context != null) {
|
||||||
context.unregisterReceiver(mTetherChangeReceiver);
|
context.unregisterReceiver(mTetherChangeReceiver);
|
||||||
|
@@ -18,15 +18,24 @@ 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.Matchers.any;
|
||||||
|
import static org.mockito.Matchers.nullable;
|
||||||
|
import static org.mockito.Mockito.doNothing;
|
||||||
import static org.mockito.Mockito.doReturn;
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.spy;
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.res.Resources;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
|
import android.os.Bundle;
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import com.android.settings.testutils.FakeFeatureFactory;
|
||||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
import com.android.settings.testutils.shadow.ShadowWifiManager;
|
import com.android.settings.testutils.shadow.ShadowWifiManager;
|
||||||
|
|
||||||
@@ -37,10 +46,14 @@ import org.mockito.Mock;
|
|||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
|
import org.robolectric.util.ReflectionHelpers;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import androidx.fragment.app.FragmentActivity;
|
||||||
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
@Config(shadows = {ShadowWifiManager.class})
|
@Config(shadows = {ShadowWifiManager.class})
|
||||||
public class WifiTetherSettingsTest {
|
public class WifiTetherSettingsTest {
|
||||||
@@ -98,6 +111,31 @@ public class WifiTetherSettingsTest {
|
|||||||
.isNotEmpty();
|
.isNotEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void startFragment_notAdminUser_shouldRemoveAllPreferences() {
|
||||||
|
final WifiTetherSettings settings = spy(new WifiTetherSettings());
|
||||||
|
final FragmentActivity activity = mock(FragmentActivity.class);
|
||||||
|
when(settings.getActivity()).thenReturn(activity);
|
||||||
|
when(settings.getContext()).thenReturn(mContext);
|
||||||
|
final Resources.Theme theme = mContext.getTheme();
|
||||||
|
when(activity.getTheme()).thenReturn(theme);
|
||||||
|
when(activity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
|
||||||
|
doNothing().when(settings)
|
||||||
|
.onCreatePreferences(any(Bundle.class), nullable(String.class));
|
||||||
|
final FakeFeatureFactory fakeFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||||
|
ReflectionHelpers.setField(settings, "mDashboardFeatureProvider",
|
||||||
|
fakeFeatureFactory.dashboardFeatureProvider);
|
||||||
|
final TextView emptyTextView = mock(TextView.class);
|
||||||
|
ReflectionHelpers.setField(settings, "mEmptyTextView", emptyTextView);
|
||||||
|
final PreferenceScreen screen = mock(PreferenceScreen.class);
|
||||||
|
doReturn(screen).when(settings).getPreferenceScreen();
|
||||||
|
settings.onCreate(Bundle.EMPTY);
|
||||||
|
|
||||||
|
settings.onStart();
|
||||||
|
|
||||||
|
verify(screen).removeAll();
|
||||||
|
}
|
||||||
|
|
||||||
private void setupIsTetherAvailable(boolean returnValue) {
|
private void setupIsTetherAvailable(boolean returnValue) {
|
||||||
when(mConnectivityManager.isTetheringSupported()).thenReturn(true);
|
when(mConnectivityManager.isTetheringSupported()).thenReturn(true);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user