Merge "Restrict WifiScanModeActivity when user restriction applies" into main
diff --git a/src/com/android/settings/wifi/WifiScanModeActivity.java b/src/com/android/settings/wifi/WifiScanModeActivity.java
index c10ee27..446891a 100644
--- a/src/com/android/settings/wifi/WifiScanModeActivity.java
+++ b/src/com/android/settings/wifi/WifiScanModeActivity.java
@@ -39,26 +39,24 @@
 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
 import com.android.settingslib.wifi.WifiPermissionChecker;
 
-/**
- * This activity requests users permission to allow scanning even when Wi-Fi is turned off
- */
+/** This activity requests users permission to allow scanning even when Wi-Fi is turned off */
 public class WifiScanModeActivity extends FragmentActivity {
     private static final String TAG = "WifiScanModeActivity";
     private DialogFragment mDialog;
-    @VisibleForTesting
-    String mApp;
-    @VisibleForTesting
-    WifiPermissionChecker mWifiPermissionChecker;
+    @VisibleForTesting String mApp;
+    @VisibleForTesting WifiPermissionChecker mWifiPermissionChecker;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        getWindow().addSystemFlags(
-                WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
+        getWindow()
+                .addSystemFlags(
+                        WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
         Intent intent = getIntent();
         if (savedInstanceState == null) {
-            if (intent != null && WifiManager.ACTION_REQUEST_SCAN_ALWAYS_AVAILABLE
-                    .equals(intent.getAction())) {
+            if (intent != null
+                    && WifiManager.ACTION_REQUEST_SCAN_ALWAYS_AVAILABLE.equals(
+                            intent.getAction())) {
                 refreshAppLabel();
             } else {
                 finish();
@@ -92,6 +90,12 @@
             return;
         }
 
+        if (!isWifiScanModeConfigAllowed(getApplicationContext())) {
+            Log.e(TAG, "This user is not allowed to configure Wi-Fi Scan Mode!");
+            finish();
+            return;
+        }
+
         if (mDialog == null) {
             mDialog = AlertDialogFragment.newInstance(mApp);
             mDialog.show(getSupportFragmentManager(), "dialog");
@@ -140,6 +144,7 @@
         }
 
         private final String mApp;
+
         public AlertDialogFragment(String app) {
             super();
             mApp = app;
@@ -158,25 +163,27 @@
         @Override
         public Dialog onCreateDialog(Bundle savedInstanceState) {
             return new AlertDialog.Builder(getActivity())
-                    .setMessage(TextUtils.isEmpty(mApp) ?
-                        getString(R.string.wifi_scan_always_turn_on_message_unknown) :
-                        getString(R.string.wifi_scan_always_turnon_message, mApp))
-                    .setPositiveButton(R.string.wifi_scan_always_confirm_allow,
+                    .setMessage(
+                            TextUtils.isEmpty(mApp)
+                                    ? getString(R.string.wifi_scan_always_turn_on_message_unknown)
+                                    : getString(R.string.wifi_scan_always_turnon_message, mApp))
+                    .setPositiveButton(
+                            R.string.wifi_scan_always_confirm_allow,
                             new DialogInterface.OnClickListener() {
                                 public void onClick(DialogInterface dialog, int whichButton) {
                                     ((WifiScanModeActivity) getActivity()).doPositiveClick();
                                 }
-                            }
-                    )
-                    .setNegativeButton(R.string.wifi_scan_always_confirm_deny,
+                            })
+                    .setNegativeButton(
+                            R.string.wifi_scan_always_confirm_deny,
                             new DialogInterface.OnClickListener() {
                                 public void onClick(DialogInterface dialog, int whichButton) {
                                     ((WifiScanModeActivity) getActivity()).doNegativeClick();
                                 }
-                            }
-                    )
+                            })
                     .create();
         }
+
         @Override
         public void onCancel(DialogInterface dialog) {
             ((WifiScanModeActivity) getActivity()).doNegativeClick();
@@ -184,9 +191,14 @@
     }
 
     private static boolean isGuestUser(Context context) {
-        if (context == null) return false;
         final UserManager userManager = context.getSystemService(UserManager.class);
         if (userManager == null) return false;
         return userManager.isGuestUser();
     }
+
+    private static boolean isWifiScanModeConfigAllowed(Context context) {
+        final UserManager userManager = context.getSystemService(UserManager.class);
+        if (userManager == null) return true;
+        return !userManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_LOCATION);
+    }
 }