Merge "Disable toggling wifi tethering in secondary user."
This commit is contained in:
@@ -71,6 +71,7 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
|
||||
|
||||
private WifiManager mWifiManager;
|
||||
private boolean mRestartWifiApAfterConfigChange;
|
||||
private boolean mUnavailable;
|
||||
|
||||
@VisibleForTesting
|
||||
TetherChangeReceiver mTetherChangeReceiver;
|
||||
@@ -94,6 +95,15 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
|
||||
return "WifiTetherSettings";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
setIfOnlyAvailableForAdmins(true);
|
||||
if (isUiRestricted()) {
|
||||
mUnavailable = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
@@ -109,6 +119,9 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
if (mUnavailable) {
|
||||
return;
|
||||
}
|
||||
// Assume we are in a SettingsActivity. This is only safe because we currently use
|
||||
// SettingsActivity as base for all preference fragments.
|
||||
final SettingsActivity activity = (SettingsActivity) getActivity();
|
||||
@@ -122,6 +135,13 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
if (mUnavailable) {
|
||||
if (!isUiRestrictedByOnlyAdmin()) {
|
||||
getEmptyTextView().setText(R.string.tethering_settings_not_available);
|
||||
}
|
||||
getPreferenceScreen().removeAll();
|
||||
return;
|
||||
}
|
||||
final Context context = getContext();
|
||||
if (context != null) {
|
||||
context.registerReceiver(mTetherChangeReceiver, TETHER_STATE_CHANGE_FILTER);
|
||||
@@ -131,6 +151,9 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
|
||||
@Override
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
if (mUnavailable) {
|
||||
return;
|
||||
}
|
||||
final Context context = getContext();
|
||||
if (context != null) {
|
||||
context.unregisterReceiver(mTetherChangeReceiver);
|
||||
|
@@ -18,15 +18,24 @@ package com.android.settings.wifi.tether;
|
||||
|
||||
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.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserHandle;
|
||||
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.shadow.ShadowWifiManager;
|
||||
|
||||
@@ -37,10 +46,14 @@ import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(shadows = {ShadowWifiManager.class})
|
||||
public class WifiTetherSettingsTest {
|
||||
@@ -98,6 +111,31 @@ public class WifiTetherSettingsTest {
|
||||
.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) {
|
||||
when(mConnectivityManager.isTetheringSupported()).thenReturn(true);
|
||||
|
||||
|
Reference in New Issue
Block a user