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
diff --git a/src/com/android/settings/wifi/tether/WifiTetherSettings.java b/src/com/android/settings/wifi/tether/WifiTetherSettings.java
index 647ba5a..c98fbdb 100644
--- a/src/com/android/settings/wifi/tether/WifiTetherSettings.java
+++ b/src/com/android/settings/wifi/tether/WifiTetherSettings.java
@@ -71,6 +71,7 @@
 
     private WifiManager mWifiManager;
     private boolean mRestartWifiApAfterConfigChange;
+    private boolean mUnavailable;
 
     @VisibleForTesting
     TetherChangeReceiver mTetherChangeReceiver;
@@ -95,6 +96,15 @@
     }
 
     @Override
+    public void onCreate(Bundle icicle) {
+        super.onCreate(icicle);
+        setIfOnlyAvailableForAdmins(true);
+        if (isUiRestricted()) {
+            mUnavailable = true;
+        }
+    }
+
+    @Override
     public void onAttach(Context context) {
         super.onAttach(context);
         mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
@@ -109,6 +119,9 @@
     @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 @@
     @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 @@
     @Override
     public void onStop() {
         super.onStop();
+        if (mUnavailable) {
+            return;
+        }
         final Context context = getContext();
         if (context != null) {
             context.unregisterReceiver(mTetherChangeReceiver);
diff --git a/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherSettingsTest.java b/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherSettingsTest.java
index 4b765e8..ef1a3b6 100644
--- a/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherSettingsTest.java
+++ b/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherSettingsTest.java
@@ -18,15 +18,24 @@
 
 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.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 @@
                 .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);