Merge "Refactor setCurrentFunction and setUsbDataUnlocked into one method"
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 48e8c03..0391bad 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -2025,10 +2025,11 @@
                   android:label="@string/bluetooth_permission_request"
                   android:excludeFromRecents="true"
                   android:permission="android.permission.BLUETOOTH"
-                  android:theme="@*android:style/Theme.Material.Light.Dialog.Alert">
+                  android:theme="@style/BluetoothPermission">
             <intent-filter android:priority="1">
                 <action android:name="android.bluetooth.adapter.action.REQUEST_DISCOVERABLE" />
                 <action android:name="android.bluetooth.adapter.action.REQUEST_ENABLE" />
+                <action android:name="android.bluetooth.adapter.action.REQUEST_DISABLE" />
                 <category android:name="android.intent.category.DEFAULT" />
             </intent-filter>
         </activity>
@@ -2770,6 +2771,17 @@
             android:excludeFromRecents="true">
         </activity>
 
+        <activity android:name=".wifi.RequestToggleWiFiActivity"
+                  android:theme="@android:style/Theme.DeviceDefault.Light.Dialog.Alert"
+            android:excludeFromRecents="true"
+            android:permission="android.permission.CHANGE_WIFI_STATE">
+            <intent-filter>
+                <action android:name="android.net.wifi.action.REQUEST_ENABLE" />
+                <action android:name="android.net.wifi.action.REQUEST_DISABLE" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </activity>
+
         <activity android:name=".wifi.WifiDialogActivity"
             android:label=""
             android:theme="@style/Transparent"
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 3a37705..3ee8228 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -263,6 +263,9 @@
     <!-- This string asks the user whether or not to allow an app to enable bluetooth. [CHAR LIMIT=100] -->
     <string name="bluetooth_ask_enablement">An app wants to turn Bluetooth ON for this device.</string>
 
+    <!-- This string asks the user whether or not to allow an app to disable bluetooth. [CHAR LIMIT=100] -->
+    <string name="bluetooth_ask_disablement">An app wants to turn Bluetooth OFF for this device.</string>
+
     <!-- Strings for asking to the user whether to allow an app to enable discovery mode -->
     <string name="bluetooth_ask_discovery" product="tablet">An app wants to make your tablet visible to other Bluetooth devices for <xliff:g id="timeout">%1$d</xliff:g> seconds.</string>
     <!-- Strings for asking to the user whether to allow an app to enable discovery mode -->
@@ -1482,6 +1485,11 @@
     <!-- Link speed on Wifi Status screen -->
     <string name="link_speed">%1$d Mbps</string>
 
+    <!-- This string asks the user whether or not to allow an app to enable WiFi. [CHAR LIMIT=NONE] -->
+    <string name="wifi_ask_enable"><xliff:g id="requester" example="FancyApp">%s</xliff:g> wants to turn WiFi ON for this device.</string>
+    <!-- This string asks the user whether or not to allow an app to disable WiFi. [CHAR LIMIT=NONE] -->
+    <string name="wifi_ask_disable"><xliff:g id="requester" example="FancyApp">%s</xliff:g> wants to turn WiFi OFF for this device.</string>
+
     <!-- NFC settings -->
     <!-- Used in the 1st-level settings screen to turn on NFC -->
     <string name="nfc_quick_toggle_title">NFC</string>
diff --git a/res/values/themes.xml b/res/values/themes.xml
index ef9e1e0..4e3c774 100644
--- a/res/values/themes.xml
+++ b/res/values/themes.xml
@@ -314,4 +314,8 @@
         <item name="android:navigationBarColor">#00000000</item>
     </style>
 
+    <style name="BluetoothPermission" parent="@android:style/Theme.Material.Light.Dialog.Alert">
+        <item name="android:windowNoTitle">true</item>
+    </style>
+
 </resources>
diff --git a/src/com/android/settings/bluetooth/RequestPermissionActivity.java b/src/com/android/settings/bluetooth/RequestPermissionActivity.java
index 3148aed..9cfcc6b 100644
--- a/src/com/android/settings/bluetooth/RequestPermissionActivity.java
+++ b/src/com/android/settings/bluetooth/RequestPermissionActivity.java
@@ -42,61 +42,32 @@
     // Command line to test this
     // adb shell am start -a android.bluetooth.adapter.action.REQUEST_ENABLE
     // adb shell am start -a android.bluetooth.adapter.action.REQUEST_DISCOVERABLE
+    // adb shell am start -a android.bluetooth.adapter.action.REQUEST_DISABLE
 
     private static final String TAG = "RequestPermissionActivity";
 
     private static final int MAX_DISCOVERABLE_TIMEOUT = 3600; // 1 hr
 
-    // Non-error return code: BT is starting or has started successfully. Used
-    // by this Activity and RequestPermissionHelperActivity
-    /* package */ static final int RESULT_BT_STARTING_OR_STARTED = -1000;
-
-    private static final int REQUEST_CODE_START_BT = 1;
+    static final int REQUEST_ENABLE = 1;
+    static final int REQUEST_ENABLE_DISCOVERABLE = 2;
+    static final int REQUEST_DISABLE = 3;
 
     private LocalBluetoothAdapter mLocalAdapter;
 
     private int mTimeout = BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT;
 
-    /*
-     * True if bluetooth wasn't enabled and RequestPermissionHelperActivity was
-     * started to ask the user and start bt.
-     *
-     * If/when that activity returns successfully, display please wait msg then
-     * go away when bt has started and discovery mode has been enabled.
-     */
-    private boolean mNeededToEnableBluetooth;
-
-    // True if requesting BT to be turned on
-    // False if requesting BT to be turned on + discoverable mode
-    private boolean mEnableOnly;
-
-    private boolean mUserConfirmed;
+    private int mRequest;
 
     private AlertDialog mDialog;
 
-    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
-
-        @Override
-        public void onReceive(Context context, Intent intent) {
-            if (intent == null) {
-                return;
-            }
-            if (mNeededToEnableBluetooth
-                    && BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
-                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothDevice.ERROR);
-                if (state == BluetoothAdapter.STATE_ON) {
-                    if (mUserConfirmed) {
-                        proceedAndFinish();
-                    }
-                }
-            }
-        }
-    };
+    private BroadcastReceiver mReceiver;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
+        setResult(Activity.RESULT_CANCELED);
+
         // Note: initializes mLocalAdapter and returns true on error
         if (parseIntent()) {
             finish();
@@ -105,68 +76,98 @@
 
         int btState = mLocalAdapter.getState();
 
-        switch (btState) {
-            case BluetoothAdapter.STATE_OFF:
-            case BluetoothAdapter.STATE_TURNING_OFF:
-            case BluetoothAdapter.STATE_TURNING_ON:
-                /*
-                 * Strictly speaking STATE_TURNING_ON belong with STATE_ON;
-                 * however, BT may not be ready when the user clicks yes and we
-                 * would fail to turn on discovery mode. By kicking this to the
-                 * RequestPermissionHelperActivity, this class will handle that
-                 * case via the broadcast receiver.
-                 */
-
-                /*
-                 * Start the helper activity to:
-                 * 1) ask the user about enabling bt AND discovery
-                 * 2) enable BT upon confirmation
-                 */
-                registerReceiver(mReceiver,
-                        new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
-                Intent intent = new Intent();
-                intent.setClass(this, RequestPermissionHelperActivity.class);
-                if (mEnableOnly) {
-                    intent.setAction(RequestPermissionHelperActivity.ACTION_INTERNAL_REQUEST_BT_ON);
-                } else {
-                    intent.setAction(RequestPermissionHelperActivity.
-                            ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE);
-                    intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, mTimeout);
-                }
-                startActivityForResult(intent, REQUEST_CODE_START_BT);
-                mNeededToEnableBluetooth = true;
-                break;
-            case BluetoothAdapter.STATE_ON:
-                if (mEnableOnly) {
-                    // Nothing to do. Already enabled.
+        if (mRequest == REQUEST_DISABLE) {
+            switch (btState) {
+                case BluetoothAdapter.STATE_OFF:
+                case BluetoothAdapter.STATE_TURNING_OFF: {
                     proceedAndFinish();
-                } else {
-                    // Ask the user about enabling discovery mode
-                    createDialog();
-                }
-                break;
-            default:
-                Log.e(TAG, "Unknown adapter state: " + btState);
+                } break;
+
+                case BluetoothAdapter.STATE_ON:
+                case BluetoothAdapter.STATE_TURNING_ON: {
+                    Intent intent = new Intent(this, RequestPermissionHelperActivity.class);
+                    intent.setAction(RequestPermissionHelperActivity
+                                .ACTION_INTERNAL_REQUEST_BT_OFF);
+                    startActivityForResult(intent, 0);
+                } break;
+
+                default: {
+                    Log.e(TAG, "Unknown adapter state: " + btState);
+                    cancelAndFinish();
+                } break;
+            }
+        } else {
+            switch (btState) {
+                case BluetoothAdapter.STATE_OFF:
+                case BluetoothAdapter.STATE_TURNING_OFF:
+                case BluetoothAdapter.STATE_TURNING_ON: {
+                    /*
+                     * Strictly speaking STATE_TURNING_ON belong with STATE_ON;
+                     * however, BT may not be ready when the user clicks yes and we
+                     * would fail to turn on discovery mode. By kicking this to the
+                     * RequestPermissionHelperActivity, this class will handle that
+                     * case via the broadcast receiver.
+                     */
+
+                    /*
+                     * Start the helper activity to:
+                     * 1) ask the user about enabling bt AND discovery
+                     * 2) enable BT upon confirmation
+                     */
+                    Intent intent = new Intent(this, RequestPermissionHelperActivity.class);
+                    intent.setAction(RequestPermissionHelperActivity.ACTION_INTERNAL_REQUEST_BT_ON);
+                    if (mRequest == REQUEST_ENABLE_DISCOVERABLE) {
+                        intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, mTimeout);
+                    }
+                    startActivityForResult(intent, 0);
+                } break;
+
+                case BluetoothAdapter.STATE_ON: {
+                    if (mRequest == REQUEST_ENABLE) {
+                        // Nothing to do. Already enabled.
+                        proceedAndFinish();
+                    } else {
+                        // Ask the user about enabling discovery mode
+                        createDialog();
+                    }
+                } break;
+
+                default: {
+                    Log.e(TAG, "Unknown adapter state: " + btState);
+                    cancelAndFinish();
+                } break;
+            }
         }
     }
 
     private void createDialog() {
+        if (getResources().getBoolean(R.bool.auto_confirm_bluetooth_activation_dialog)) {
+            onClick(null, DialogInterface.BUTTON_POSITIVE);
+            return;
+        }
+
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
 
-        if (mNeededToEnableBluetooth) {
-            // RequestPermissionHelperActivity has gotten confirmation from user
-            // to turn on BT
-            builder.setMessage(getString(R.string.bluetooth_turning_on));
+        // Non-null receiver means we are toggling
+        if (mReceiver != null) {
+            switch (mRequest) {
+                case REQUEST_ENABLE:
+                case REQUEST_ENABLE_DISCOVERABLE: {
+                    builder.setMessage(getString(R.string.bluetooth_turning_on));
+                } break;
+
+                default: {
+                    builder.setMessage(getString(R.string.bluetooth_turning_off));
+                } break;
+            }
             builder.setCancelable(false);
         } else {
             // Ask the user whether to turn on discovery mode or not
             // For lasting discoverable mode there is a different message
             if (mTimeout == BluetoothDiscoverableEnabler.DISCOVERABLE_TIMEOUT_NEVER) {
-                builder.setMessage(
-                        getString(R.string.bluetooth_ask_lasting_discovery));
+                builder.setMessage(getString(R.string.bluetooth_ask_lasting_discovery));
             } else {
-                builder.setMessage(
-                        getString(R.string.bluetooth_ask_discovery, mTimeout));
+                builder.setMessage(getString(R.string.bluetooth_ask_discovery, mTimeout));
             }
             builder.setPositiveButton(getString(R.string.allow), this);
             builder.setNegativeButton(getString(R.string.deny), this);
@@ -174,36 +175,44 @@
 
         mDialog = builder.create();
         mDialog.show();
-
-        if (getResources().getBoolean(R.bool.auto_confirm_bluetooth_activation_dialog) == true) {
-            // dismiss dialog immediately if settings say so
-            onClick(null, DialogInterface.BUTTON_POSITIVE);
-        }
     }
 
     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
-        if (requestCode != REQUEST_CODE_START_BT) {
-            Log.e(TAG, "Unexpected onActivityResult " + requestCode + ' ' + resultCode);
-            setResult(RESULT_CANCELED);
-            finish();
-            return;
-        }
-        if (resultCode != RESULT_BT_STARTING_OR_STARTED) {
-            setResult(resultCode);
-            finish();
+        if (resultCode != Activity.RESULT_OK) {
+            cancelAndFinish();
             return;
         }
 
-        // Back from RequestPermissionHelperActivity. User confirmed to enable
-        // BT and discoverable mode.
-        mUserConfirmed = true;
+        switch (mRequest) {
+            case REQUEST_ENABLE:
+            case REQUEST_ENABLE_DISCOVERABLE: {
+                if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) {
+                    proceedAndFinish();
+                } else {
+                    // If BT is not up yet, show "Turning on Bluetooth..."
+                    mReceiver = new StateChangeReceiver();
+                    registerReceiver(mReceiver, new IntentFilter(
+                            BluetoothAdapter.ACTION_STATE_CHANGED));
+                    createDialog();
+                }
+            } break;
 
-        if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) {
-            proceedAndFinish();
-        } else {
-            // If BT is not up yet, show "Turning on Bluetooth..."
-            createDialog();
+            case REQUEST_DISABLE: {
+                if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_OFF) {
+                    proceedAndFinish();
+                } else {
+                    // If BT is not up yet, show "Turning off Bluetooth..."
+                    mReceiver = new StateChangeReceiver();
+                    registerReceiver(mReceiver, new IntentFilter(
+                            BluetoothAdapter.ACTION_STATE_CHANGED));
+                    createDialog();
+                }
+            } break;
+
+            default: {
+                cancelAndFinish();
+            } break;
         }
     }
 
@@ -223,8 +232,8 @@
     private void proceedAndFinish() {
         int returnCode;
 
-        if (mEnableOnly) {
-            // BT enabled. Done
+        if (mRequest == REQUEST_ENABLE || mRequest == REQUEST_DISABLE) {
+            // BT toggled. Done
             returnCode = RESULT_OK;
         } else if (mLocalAdapter.setScanMode(
                 BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, mTimeout)) {
@@ -252,16 +261,26 @@
         finish();
     }
 
+    private void cancelAndFinish() {
+        setResult(Activity.RESULT_CANCELED);
+        finish();
+    }
+
     /**
      * Parse the received Intent and initialize mLocalBluetoothAdapter.
      * @return true if an error occurred; false otherwise
      */
     private boolean parseIntent() {
         Intent intent = getIntent();
-        if (intent != null && intent.getAction().equals(BluetoothAdapter.ACTION_REQUEST_ENABLE)) {
-            mEnableOnly = true;
-        } else if (intent != null
-                && intent.getAction().equals(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE)) {
+        if (intent == null) {
+            return true;
+        }
+        if (intent.getAction().equals(BluetoothAdapter.ACTION_REQUEST_ENABLE)) {
+            mRequest = REQUEST_ENABLE;
+        } else if (intent.getAction().equals(BluetoothAdapter.ACTION_REQUEST_DISABLE)) {
+            mRequest = REQUEST_DISABLE;
+        } else if (intent.getAction().equals(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE)) {
+            mRequest = REQUEST_ENABLE_DISCOVERABLE;
             mTimeout = intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
                     BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT);
 
@@ -292,8 +311,9 @@
     @Override
     protected void onDestroy() {
         super.onDestroy();
-        if (mNeededToEnableBluetooth) {
+        if (mReceiver != null) {
             unregisterReceiver(mReceiver);
+            mReceiver = null;
         }
     }
 
@@ -302,4 +322,38 @@
         setResult(RESULT_CANCELED);
         super.onBackPressed();
     }
+
+    private final class StateChangeReceiver extends BroadcastReceiver {
+        private static final long TOGGLE_TIMEOUT_MILLIS = 10000; // 10 sec
+
+        public StateChangeReceiver() {
+            getWindow().getDecorView().postDelayed(() -> {
+                if (!isFinishing() && !isDestroyed()) {
+                    cancelAndFinish();
+                }
+            }, TOGGLE_TIMEOUT_MILLIS);
+        }
+
+        public void onReceive(Context context, Intent intent) {
+            if (intent == null) {
+                return;
+            }
+            final int currentState = intent.getIntExtra(
+                    BluetoothAdapter.EXTRA_STATE, BluetoothDevice.ERROR);
+            switch (mRequest) {
+                case REQUEST_ENABLE:
+                case REQUEST_ENABLE_DISCOVERABLE: {
+                    if (currentState == BluetoothAdapter.STATE_ON) {
+                        proceedAndFinish();
+                    }
+                } break;
+
+                case REQUEST_DISABLE: {
+                    if (currentState == BluetoothAdapter.STATE_OFF) {
+                        proceedAndFinish();
+                    }
+                } break;
+            }
+        }
+    }
 }
diff --git a/src/com/android/settings/bluetooth/RequestPermissionHelperActivity.java b/src/com/android/settings/bluetooth/RequestPermissionHelperActivity.java
index fafa284..d70a63f 100644
--- a/src/com/android/settings/bluetooth/RequestPermissionHelperActivity.java
+++ b/src/com/android/settings/bluetooth/RequestPermissionHelperActivity.java
@@ -16,6 +16,7 @@
 
 package com.android.settings.bluetooth;
 
+import android.app.Activity;
 import android.bluetooth.BluetoothAdapter;
 import android.content.DialogInterface;
 import android.content.Intent;
@@ -24,106 +25,94 @@
 
 import com.android.internal.app.AlertActivity;
 import com.android.internal.app.AlertController;
+import com.android.internal.util.ArrayUtils;
 import com.android.settings.R;
 import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
 import com.android.settingslib.bluetooth.LocalBluetoothManager;
 
 /**
- * RequestPermissionHelperActivity asks the user whether to enable discovery.
- * This is usually started by RequestPermissionActivity.
+ * RequestPermissionHelperActivity asks the user whether to toggle Bluetooth.
+ *
+ * TODO: This activity isn't needed - this should be folded in RequestPermissionActivity
  */
 public class RequestPermissionHelperActivity extends AlertActivity implements
         DialogInterface.OnClickListener {
     private static final String TAG = "RequestPermissionHelperActivity";
 
     public static final String ACTION_INTERNAL_REQUEST_BT_ON =
-        "com.android.settings.bluetooth.ACTION_INTERNAL_REQUEST_BT_ON";
+            "com.android.settings.bluetooth.ACTION_INTERNAL_REQUEST_BT_ON";
 
-    public static final String ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE =
-        "com.android.settings.bluetooth.ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE";
+    public static final String ACTION_INTERNAL_REQUEST_BT_OFF =
+            "com.android.settings.bluetooth.ACTION_INTERNAL_REQUEST_BT_OFF";
 
     private LocalBluetoothAdapter mLocalAdapter;
 
-    private int mTimeout;
+    private int mTimeout = -1;
 
-    // True if requesting BT to be turned on
-    // False if requesting BT to be turned on + discoverable mode
-    private boolean mEnableOnly;
+    private int mRequest;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
+        setResult(RESULT_CANCELED);
+
         // Note: initializes mLocalAdapter and returns true on error
-        if (parseIntent()) {
+        if (!parseIntent()) {
             finish();
             return;
         }
 
-        createDialog();
-
-        if (getResources().getBoolean(R.bool.auto_confirm_bluetooth_activation_dialog) == true) {
-            // dismiss dialog immediately if settings say so
+        if (getResources().getBoolean(R.bool.auto_confirm_bluetooth_activation_dialog)) {
+            // Don't even show the dialog if configured this way
             onClick(null, BUTTON_POSITIVE);
             dismiss();
         }
+
+        createDialog();
     }
 
     void createDialog() {
         final AlertController.AlertParams p = mAlertParams;
 
-        if (mEnableOnly) {
-            p.mMessage = getString(R.string.bluetooth_ask_enablement);
-        } else {
-            if (mTimeout == BluetoothDiscoverableEnabler.DISCOVERABLE_TIMEOUT_NEVER) {
-                p.mMessage = getString(R.string.bluetooth_ask_enablement_and_lasting_discovery);
-            } else {
-                p.mMessage = getString(R.string.bluetooth_ask_enablement_and_discovery, mTimeout);
-            }
+        switch (mRequest) {
+            case RequestPermissionActivity.REQUEST_ENABLE: {
+                if (mTimeout < 0) {
+                    p.mMessage = getString(R.string.bluetooth_ask_enablement);
+                } else if (mTimeout == BluetoothDiscoverableEnabler.DISCOVERABLE_TIMEOUT_NEVER) {
+                    p.mMessage = getString(
+                            R.string.bluetooth_ask_enablement_and_lasting_discovery);
+                } else {
+                    p.mMessage = getString(
+                            R.string.bluetooth_ask_enablement_and_discovery, mTimeout);
+                }
+            } break;
+
+            case RequestPermissionActivity.REQUEST_DISABLE: {
+                p.mMessage = getString(R.string.bluetooth_ask_disablement);
+            } break;
         }
 
         p.mPositiveButtonText = getString(R.string.allow);
         p.mPositiveButtonListener = this;
         p.mNegativeButtonText = getString(R.string.deny);
-        p.mNegativeButtonListener = this;
 
         setupAlert();
     }
 
     public void onClick(DialogInterface dialog, int which) {
-        int returnCode;
-        // FIXME: fix this ugly switch logic!
-        switch (which) {
-            case BUTTON_POSITIVE:
-                int btState = 0;
+        switch (mRequest) {
+            case RequestPermissionActivity.REQUEST_ENABLE:
+            case RequestPermissionActivity.REQUEST_ENABLE_DISCOVERABLE: {
+                mLocalAdapter.enable();
+                setResult(Activity.RESULT_OK);
+            } break;
 
-                try {
-                    // TODO There's a better way.
-                    int retryCount = 30;
-                    do {
-                        btState = mLocalAdapter.getBluetoothState();
-                        Thread.sleep(100);
-                    } while (btState == BluetoothAdapter.STATE_TURNING_OFF && --retryCount > 0);
-                } catch (InterruptedException ignored) {
-                    // don't care
-                }
-
-                if (btState == BluetoothAdapter.STATE_TURNING_ON
-                        || btState == BluetoothAdapter.STATE_ON
-                        || mLocalAdapter.enable()) {
-                    returnCode = RequestPermissionActivity.RESULT_BT_STARTING_OR_STARTED;
-                } else {
-                    returnCode = RESULT_CANCELED;
-                }
-                break;
-
-            case BUTTON_NEGATIVE:
-                returnCode = RESULT_CANCELED;
-                break;
-            default:
-                return;
+            case RequestPermissionActivity.REQUEST_DISABLE: {
+                mLocalAdapter.disable();
+                setResult(Activity.RESULT_OK);
+            } break;
         }
-        setResult(returnCode);
     }
 
     /**
@@ -132,33 +121,32 @@
      */
     private boolean parseIntent() {
         Intent intent = getIntent();
-        if (intent != null && intent.getAction().equals(ACTION_INTERNAL_REQUEST_BT_ON)) {
-            mEnableOnly = true;
-        } else if (intent != null
-                && intent.getAction().equals(ACTION_INTERNAL_REQUEST_BT_ON_AND_DISCOVERABLE)) {
-            mEnableOnly = false;
-            // Value used for display purposes. Not range checking.
-            mTimeout = intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
-                    BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT);
+        if (intent == null) {
+            return false;
+        }
+
+        String action = intent.getAction();
+        if (ACTION_INTERNAL_REQUEST_BT_ON.equals(action)) {
+            mRequest = RequestPermissionActivity.REQUEST_ENABLE;
+            if (intent.hasExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION)) {
+                // Value used for display purposes. Not range checking.
+                mTimeout = intent.getIntExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
+                        BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT);
+            }
+        } else if (ACTION_INTERNAL_REQUEST_BT_OFF.equals(action)) {
+            mRequest = RequestPermissionActivity.REQUEST_DISABLE;
         } else {
-            setResult(RESULT_CANCELED);
-            return true;
+            return false;
         }
 
         LocalBluetoothManager manager = Utils.getLocalBtManager(this);
         if (manager == null) {
             Log.e(TAG, "Error: there's a problem starting Bluetooth");
-            setResult(RESULT_CANCELED);
-            return true;
+            return false;
         }
+
         mLocalAdapter = manager.getBluetoothAdapter();
 
-        return false;
-    }
-
-    @Override
-    public void onBackPressed() {
-        setResult(RESULT_CANCELED);
-        super.onBackPressed();
+        return true;
     }
 }
diff --git a/src/com/android/settings/dashboard/SearchResultsSummary.java b/src/com/android/settings/dashboard/SearchResultsSummary.java
index 38f17e3..6f476a2 100644
--- a/src/com/android/settings/dashboard/SearchResultsSummary.java
+++ b/src/com/android/settings/dashboard/SearchResultsSummary.java
@@ -323,6 +323,7 @@
             mUpdateSuggestionsTask = null;
         }
         setSuggestionsCursor(null);
+        setSuggestionsVisibility(false);
     }
 
     private void setSuggestionsCursor(Cursor cursor) {
@@ -341,6 +342,7 @@
             mUpdateSearchResultsTask = null;
         }
         setResultsCursor(null);
+        setResultsVisibility(false);
     }
 
     private void setResultsCursor(Cursor cursor) {
diff --git a/src/com/android/settings/deviceinfo/Status.java b/src/com/android/settings/deviceinfo/Status.java
index 1ba3978..42e3d55 100644
--- a/src/com/android/settings/deviceinfo/Status.java
+++ b/src/com/android/settings/deviceinfo/Status.java
@@ -280,7 +280,8 @@
 
     private void setWifiStatus() {
         WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
-        String macAddress = wifiInfo == null ? null : wifiInfo.getMacAddress();
+        boolean hasMacAddress = wifiInfo != null && wifiInfo.hasRealMacAddress();
+        String macAddress = hasMacAddress ? wifiInfo.getMacAddress() : null;
         mWifiMacAddress.setSummary(!TextUtils.isEmpty(macAddress) ? macAddress : mUnavailable);
     }
 
diff --git a/src/com/android/settings/wifi/RequestToggleWiFiActivity.java b/src/com/android/settings/wifi/RequestToggleWiFiActivity.java
new file mode 100644
index 0000000..58bc4cd
--- /dev/null
+++ b/src/com/android/settings/wifi/RequestToggleWiFiActivity.java
@@ -0,0 +1,303 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.wifi;
+
+import android.app.Activity;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
+import android.net.wifi.WifiManager;
+import android.os.Bundle;
+import android.support.annotation.NonNull;
+import android.text.TextUtils;
+import android.util.Log;
+import android.widget.Toast;
+import com.android.internal.app.AlertActivity;
+import com.android.settings.R;
+
+/**
+ * This activity handles requests to toggle WiFi by collecting user
+ * consent and waiting until the state change is completed.
+ */
+public class RequestToggleWiFiActivity extends AlertActivity
+        implements DialogInterface.OnClickListener {
+    private static final String LOG_TAG = "RequestToggleWiFiActivity";
+
+    private static final long TOGGLE_TIMEOUT_MILLIS = 10000; // 10 sec
+
+    private static final int STATE_UNKNOWN = -1;
+    private static final int STATE_ENABLE = 1;
+    private static final int STATE_ENABLING = 2;
+    private static final int STATE_DISABLE = 3;
+    private static final int STATE_DISABLING = 4;
+
+    private final StateChangeReceiver mReceiver = new StateChangeReceiver();
+
+    private final Runnable mTimeoutCommand = () -> {
+        if (!isFinishing() && !isDestroyed()) {
+            finish();
+        }
+    };
+
+    private @NonNull WifiManager mWiFiManager;
+    private @NonNull CharSequence mAppLabel;
+
+    private int mState = STATE_UNKNOWN;
+    private int mLastUpdateState = STATE_UNKNOWN;
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        mWiFiManager = getSystemService(WifiManager.class);
+
+        setResult(Activity.RESULT_CANCELED);
+
+        String packageName = getIntent().getStringExtra(Intent.EXTRA_PACKAGE_NAME);
+        if (TextUtils.isEmpty(packageName)) {
+            finish();
+            return;
+        }
+
+        try {
+            ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo(
+                    packageName, 0);
+            mAppLabel = applicationInfo.loadSafeLabel(getPackageManager());
+        } catch (PackageManager.NameNotFoundException e) {
+            Log.e(LOG_TAG, "Couldn't find app with package name " + packageName);
+            finish();
+            return;
+        }
+
+        String action = getIntent().getAction();
+        switch (action) {
+            case WifiManager.ACTION_REQUEST_ENABLE: {
+                mState = STATE_ENABLE;
+            } break;
+
+            case WifiManager.ACTION_REQUEST_DISABLE: {
+                mState = STATE_DISABLE;
+            } break;
+
+            default: {
+                finish();
+            }
+        }
+    }
+
+    @Override
+    public void onClick(DialogInterface dialog, int which) {
+        if (which != DialogInterface.BUTTON_POSITIVE) {
+            return;
+        }
+        switch (mState) {
+            case STATE_ENABLE: {
+                mWiFiManager.setWifiEnabled(true);
+                mState = STATE_ENABLING;
+                scheduleToggleTimeout();
+                updateUi();
+            } break;
+
+            case STATE_DISABLE: {
+                mWiFiManager.setWifiEnabled(false);
+                mState = STATE_DISABLING;
+                scheduleToggleTimeout();
+                updateUi();
+            } break;
+        }
+    }
+
+    @Override
+    protected void onStart() {
+        super.onStart();
+
+        mReceiver.register();
+
+        final int wifiState = mWiFiManager.getWifiState();
+
+        switch (mState) {
+            case STATE_ENABLE: {
+                switch (wifiState) {
+                    case WifiManager.WIFI_STATE_ENABLED: {
+                        setResult(RESULT_OK);
+                        finish();
+                    } return;
+
+                    case WifiManager.WIFI_STATE_ENABLING: {
+                        mState = STATE_ENABLING;
+                        scheduleToggleTimeout();
+                    } break;
+                }
+            } break;
+
+            case STATE_DISABLE: {
+                switch (wifiState) {
+                    case WifiManager.WIFI_STATE_DISABLED: {
+                        setResult(RESULT_OK);
+                        finish();
+                    }
+                    return;
+
+                    case WifiManager.WIFI_STATE_ENABLING: {
+                        mState = STATE_DISABLING;
+                        scheduleToggleTimeout();
+                    }
+                    break;
+                }
+            } break;
+
+            case STATE_ENABLING: {
+                switch (wifiState) {
+                    case WifiManager.WIFI_STATE_ENABLED: {
+                        setResult(RESULT_OK);
+                        finish();
+                    } return;
+
+                    case WifiManager.WIFI_STATE_ENABLING: {
+                        scheduleToggleTimeout();
+                    } break;
+
+                    case WifiManager.WIFI_STATE_DISABLED:
+                    case WifiManager.WIFI_STATE_DISABLING: {
+                        mState = STATE_ENABLE;
+                    } break;
+                }
+            } break;
+
+            case STATE_DISABLING: {
+                switch (wifiState) {
+                    case WifiManager.WIFI_STATE_DISABLED: {
+                        setResult(RESULT_OK);
+                        finish();
+                    } return;
+
+                    case WifiManager.WIFI_STATE_DISABLING: {
+                        scheduleToggleTimeout();
+                    } break;
+
+                    case WifiManager.WIFI_STATE_ENABLED:
+                    case WifiManager.WIFI_STATE_ENABLING: {
+                        mState = STATE_DISABLE;
+                    } break;
+                }
+            } break;
+        }
+
+        updateUi();
+    }
+
+    @Override
+    protected void onStop() {
+        mReceiver.unregister();
+        unscheduleToggleTimeout();
+        super.onStop();
+    }
+
+    private void updateUi() {
+        if (mLastUpdateState == mState) {
+            return;
+        }
+        mLastUpdateState = mState;
+
+        switch (mState) {
+            case STATE_ENABLE: {
+                mAlertParams.mPositiveButtonText = getString(android.R.string.ok);
+                mAlertParams.mPositiveButtonListener = this;
+                mAlertParams.mMessage = getString(R.string.wifi_ask_enable, mAppLabel);
+            } break;
+
+            case STATE_ENABLING: {
+                // Params set button text only if non-null, but we want a null
+                // button text to hide the button, so reset the controller directly.
+                mAlert.setButton(DialogInterface.BUTTON_POSITIVE, null, null, null);
+                mAlertParams.mPositiveButtonText = null;
+                mAlertParams.mPositiveButtonListener = null;
+                mAlertParams.mMessage = getString(R.string.wifi_starting);
+            } break;
+
+            case STATE_DISABLE: {
+                mAlertParams.mPositiveButtonText = getString(android.R.string.ok);
+                mAlertParams.mPositiveButtonListener = this;
+                mAlertParams.mMessage = getString(R.string.wifi_ask_disable, mAppLabel);
+            } break;
+
+            case STATE_DISABLING: {
+                // Params set button text only if non-null, but we want a null
+                // button text to hide the button, so reset the controller directly.
+                mAlert.setButton(DialogInterface.BUTTON_POSITIVE, null, null, null);
+                mAlertParams.mPositiveButtonText = null;
+                mAlertParams.mPositiveButtonListener = null;
+                mAlertParams.mMessage = getString(R.string.wifi_stopping);
+            } break;
+        }
+
+        setupAlert();
+    }
+
+    @Override
+    public void dismiss() {
+        // Clicking on the dialog buttons dismisses the dialog and finishes
+        // the activity but we want to finish after the WiFi state changed.
+    }
+
+    private void scheduleToggleTimeout() {
+        getWindow().getDecorView().postDelayed(mTimeoutCommand, TOGGLE_TIMEOUT_MILLIS);
+    }
+
+    private void unscheduleToggleTimeout() {
+        getWindow().getDecorView().removeCallbacks(mTimeoutCommand);
+    }
+
+    private final class StateChangeReceiver extends BroadcastReceiver {
+        private final IntentFilter mFilter = new IntentFilter(
+                WifiManager.WIFI_STATE_CHANGED_ACTION);
+
+        public void register() {
+            registerReceiver(this, mFilter);
+        }
+
+        public void unregister() {
+            unregisterReceiver(this);
+        }
+
+        public void onReceive(Context context, Intent intent) {
+            Activity activity = RequestToggleWiFiActivity.this;
+            if (activity.isFinishing() || activity.isDestroyed()) {
+                return;
+            }
+            final int currentState = mWiFiManager.getWifiState();
+            switch (currentState) {
+                case WifiManager.WIFI_STATE_ENABLED:
+                case WifiManager.WIFI_STATE_DISABLED: {
+                    if (mState == STATE_ENABLING || mState == STATE_DISABLING) {
+                        RequestToggleWiFiActivity.this.setResult(Activity.RESULT_OK);
+                        finish();
+                    }
+                } break;
+
+                case WifiManager.ERROR: {
+                    Toast.makeText(activity, R.string.wifi_error, Toast.LENGTH_SHORT).show();
+                    finish();
+                } break;
+            }
+        }
+    }
+}