Restrict guest user to configure WiFi access points

- If the user is not allowed to configure Wi-Fi access points, close WifiDialogActivity.

Bug: 226133034
Test: manual test
make RunSettingsRoboTests ROBOTEST_FILTER=WifiDialogActivityTest

Change-Id: I32d0c5beceb8a140a98636a5a0af97790495620d
diff --git a/src/com/android/settings/wifi/WifiDialogActivity.java b/src/com/android/settings/wifi/WifiDialogActivity.java
index 67f291d..8a46c9d 100644
--- a/src/com/android/settings/wifi/WifiDialogActivity.java
+++ b/src/com/android/settings/wifi/WifiDialogActivity.java
@@ -17,6 +17,7 @@
 package com.android.settings.wifi;
 
 import static android.Manifest.permission.ACCESS_FINE_LOCATION;
+import static android.os.UserManager.DISALLOW_CONFIG_WIFI;
 
 import android.content.DialogInterface;
 import android.content.Intent;
@@ -31,6 +32,7 @@
 import android.os.Process;
 import android.os.SimpleClock;
 import android.os.SystemClock;
+import android.os.UserManager;
 import android.text.TextUtils;
 import android.util.EventLog;
 import android.util.Log;
@@ -115,6 +117,10 @@
         }
 
         super.onCreate(savedInstanceState);
+        if (!isConfigWifiAllowed()) {
+            finish();
+            return;
+        }
 
         mIsWifiTrackerLib = !TextUtils.isEmpty(mIntent.getStringExtra(KEY_CHOSEN_WIFIENTRY_KEY));
 
@@ -361,6 +367,19 @@
         }
     }
 
+    @VisibleForTesting
+    boolean isConfigWifiAllowed() {
+        UserManager userManager = getSystemService(UserManager.class);
+        if (userManager == null) return true;
+        final boolean isConfigWifiAllowed = !userManager.hasUserRestriction(DISALLOW_CONFIG_WIFI);
+        if (!isConfigWifiAllowed) {
+            Log.e(TAG, "The user is not allowed to configure Wi-Fi.");
+            EventLog.writeEvent(0x534e4554, "226133034", getApplicationContext().getUserId(),
+                    "The user is not allowed to configure Wi-Fi.");
+        }
+        return isConfigWifiAllowed;
+    }
+
     private boolean hasWifiManager() {
         if (mWifiManager != null) return true;
         mWifiManager = getSystemService(WifiManager.class);
diff --git a/tests/robotests/src/com/android/settings/wifi/WifiDialogActivityTest.java b/tests/robotests/src/com/android/settings/wifi/WifiDialogActivityTest.java
index 4ceb1e3..28b7ecb 100644
--- a/tests/robotests/src/com/android/settings/wifi/WifiDialogActivityTest.java
+++ b/tests/robotests/src/com/android/settings/wifi/WifiDialogActivityTest.java
@@ -18,6 +18,7 @@
 
 import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
 import static android.Manifest.permission.ACCESS_FINE_LOCATION;
+import static android.os.UserManager.DISALLOW_CONFIG_WIFI;
 
 import static com.android.settings.wifi.WifiDialogActivity.REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER;
 import static com.android.settings.wifi.WifiDialogActivity.RESULT_CONNECTED;
@@ -36,6 +37,7 @@
 import android.content.pm.PackageManager;
 import android.net.wifi.WifiConfiguration;
 import android.net.wifi.WifiManager;
+import android.os.UserManager;
 
 import com.android.settings.testutils.FakeFeatureFactory;
 import com.android.settingslib.wifi.AccessPoint;
@@ -44,6 +46,7 @@
 import com.google.android.setupcompat.util.WizardManagerHelper;
 
 import org.junit.Before;
+import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
@@ -58,6 +61,8 @@
     static final int REQUEST_CODE = REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER;
 
     @Mock
+    UserManager mUserManager;
+    @Mock
     PackageManager mPackageManager;
     @Mock
     WifiManager mWifiManager;
@@ -92,6 +97,7 @@
         FakeFeatureFactory.setupForTest();
 
         mActivity = spy(Robolectric.setupActivity(WifiDialogActivity.class));
+        when(mActivity.getSystemService(UserManager.class)).thenReturn(mUserManager);
         when(mActivity.getSystemService(WifiManager.class)).thenReturn(mWifiManager);
     }
 
@@ -212,6 +218,20 @@
     }
 
     @Test
+    public void isConfigWifiAllowed_hasNoUserRestriction_returnTrue() {
+        when(mUserManager.hasUserRestriction(DISALLOW_CONFIG_WIFI)).thenReturn(false);
+
+        assertThat(mActivity.isConfigWifiAllowed()).isTrue();
+    }
+
+    @Test
+    public void isConfigWifiAllowed_hasUserRestriction_returnFalse() {
+        when(mUserManager.hasUserRestriction(DISALLOW_CONFIG_WIFI)).thenReturn(true);
+
+        assertThat(mActivity.isConfigWifiAllowed()).isFalse();
+    }
+
+    @Test
     public void hasPermissionForResult_noCallingPackage_returnFalse() {
         when(mActivity.getCallingPackage()).thenReturn(null);