From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: lucaslin <lucaslin@google.com>
Date: Wed, 9 Mar 2022 10:52:43 +0800
Subject: [PATCH] Hide private DNS settings UI in Guest mode

Hide private DNS settings UI in Guest mode to prevent guest
users modifying global private DNS settings.

Bug: 206987762
Test: 1. make RunSettingsRoboTests \
         ROBOTEST_FILTER=PrivateDnsPreferenceControllerTest
      2. Switch to Guest user and check if the private DNS UI is
         hidden or not.
Change-Id: Iebfb8684da3be32110decd9e8447dd07b1c40387
(cherry picked from commit 52e863b5a212889d4f8cb89a4028c42af59c9327)
Merged-In: Iebfb8684da3be32110decd9e8447dd07b1c40387
---
 .../PrivateDnsPreferenceController.java        |  3 +++
 .../PrivateDnsPreferenceControllerTest.java    | 18 ++++++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/src/com/android/settings/network/PrivateDnsPreferenceController.java b/src/com/android/settings/network/PrivateDnsPreferenceController.java
index 6f385696733..825ffd66f1a 100644
--- a/src/com/android/settings/network/PrivateDnsPreferenceController.java
+++ b/src/com/android/settings/network/PrivateDnsPreferenceController.java
@@ -34,6 +34,7 @@ import android.net.Network;
 import android.net.Uri;
 import android.os.Handler;
 import android.os.Looper;
+import android.os.UserManager;
 import android.provider.Settings;
 import android.support.v7.preference.Preference;
 import android.support.v7.preference.PreferenceScreen;
@@ -79,6 +80,8 @@ public class PrivateDnsPreferenceController extends BasePreferenceController
 
     @Override
     public int getAvailabilityStatus() {
+        final UserManager userManager = mContext.getSystemService(UserManager.class);
+        if (userManager.isGuestUser()) return DISABLED_FOR_USER;
         return AVAILABLE;
     }
 
diff --git a/tests/robotests/src/com/android/settings/network/PrivateDnsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/network/PrivateDnsPreferenceControllerTest.java
index a63645ba059..ca1dcaaa6e2 100644
--- a/tests/robotests/src/com/android/settings/network/PrivateDnsPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/network/PrivateDnsPreferenceControllerTest.java
@@ -24,6 +24,9 @@ import static android.net.ConnectivityManager.PRIVATE_DNS_MODE_PROVIDER_HOSTNAME
 import static android.provider.Settings.Global.PRIVATE_DNS_DEFAULT_MODE;
 import static android.provider.Settings.Global.PRIVATE_DNS_MODE;
 import static android.provider.Settings.Global.PRIVATE_DNS_SPECIFIER;
+import static com.android.settings.core.BasePreferenceController.AVAILABLE;
+import static com.android.settings.core.BasePreferenceController.DISABLED_FOR_USER;
+
 import static com.google.common.truth.Truth.assertThat;
 import static org.mockito.ArgumentMatchers.nullable;
 import static org.mockito.Matchers.any;
@@ -31,6 +34,7 @@ import static org.mockito.Matchers.anyString;
 import static org.mockito.Mockito.CALLS_REAL_METHODS;
 import static org.mockito.Mockito.atLeastOnce;
 import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.reset;
 import static org.mockito.Mockito.spy;
@@ -96,6 +100,8 @@ public class PrivateDnsPreferenceControllerTest {
     private Network mNetwork;
     @Mock
     private Preference mPreference;
+    @Mock
+    private UserManager mUserManager;
     @Captor
     private ArgumentCaptor<NetworkCallback> mCallbackCaptor;
     private PrivateDnsPreferenceController mController;
@@ -113,6 +119,7 @@ public class PrivateDnsPreferenceControllerTest {
         mShadowContentResolver = Shadow.extract(mContentResolver);
         when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
                 .thenReturn(mConnectivityManager);
+        when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
         doNothing().when(mConnectivityManager).registerDefaultNetworkCallback(
                 mCallbackCaptor.capture(), nullable(Handler.class));
 
@@ -146,6 +153,17 @@ public class PrivateDnsPreferenceControllerTest {
         nc.onLinkPropertiesChanged(mNetwork, lp);
     }
 
+    @Test
+    public void getAvailibilityStatus_availableByDefault() {
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
+    }
+
+    @Test
+    public void getAvailabilityStatus_disabledForGuestUser() {
+        doReturn(true).when(mUserManager).isGuestUser();
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_FOR_USER);
+    }
+
     @Test
     public void goThroughLifecycle_shouldRegisterUnregisterSettingsObserver() {
         mLifecycle.handleLifecycleEvent(ON_START);
