Add a new version of the disable API.

This allows for the setting to be persisted or not.
Also turn on Bluetooth in System Server if needed.
It won't work currently because the service wouldn't have
started.

Change-Id: I15fa2bff93aa32134c1b565fcbe90ba68614b6a1
diff --git a/core/java/android/bluetooth/BluetoothAdapter.java b/core/java/android/bluetooth/BluetoothAdapter.java
index 755884c..8d4c3af 100644
--- a/core/java/android/bluetooth/BluetoothAdapter.java
+++ b/core/java/android/bluetooth/BluetoothAdapter.java
@@ -518,7 +518,24 @@
      */
     public boolean disable() {
         try {
-            return mService.disable();
+            return mService.disable(true);
+        } catch (RemoteException e) {Log.e(TAG, "", e);}
+        return false;
+    }
+
+    /**
+     * Turn off the local Bluetooth adapter and don't persist the setting.
+     *
+     * <p>Requires the {@link android.Manifest.permission#BLUETOOTH_ADMIN}
+     * permission
+     *
+     * @return true to indicate adapter shutdown has begun, or false on
+     *         immediate error
+     * @hide
+     */
+    public boolean disable(boolean persist) {
+        try {
+            return mService.disable(persist);
         } catch (RemoteException e) {Log.e(TAG, "", e);}
         return false;
     }
diff --git a/core/java/android/bluetooth/IBluetooth.aidl b/core/java/android/bluetooth/IBluetooth.aidl
index 21f2ae3..c59c62c 100644
--- a/core/java/android/bluetooth/IBluetooth.aidl
+++ b/core/java/android/bluetooth/IBluetooth.aidl
@@ -34,7 +34,7 @@
     boolean isEnabled();
     int getState();
     boolean enable();
-    boolean disable();
+    boolean disable(boolean persist);
 
     String getAddress();
     ParcelUuid[] getUuids();
diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java
index f49c4e6..2393957 100644
--- a/services/java/com/android/server/SystemServer.java
+++ b/services/java/com/android/server/SystemServer.java
@@ -241,11 +241,17 @@
             } else if (factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {
                 Slog.i(TAG, "No Bluetooth Service (factory test)");
             } else {
-                //TODO(BT): Start BT services and turn on if needed.
                 int airplaneModeOn = Settings.System.getInt(mContentResolver,
                         Settings.System.AIRPLANE_MODE_ON, 0);
                 int bluetoothOn = Settings.Secure.getInt(mContentResolver,
                     Settings.Secure.BLUETOOTH_ON, 0);
+                BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
+                // TODO(BT): This will not work as the Bluetooth process is not
+                // up. Depending on the process architecture, BluetoothAdapter
+                // will have to bind to the service.
+                if (adapter != null && airplaneModeOn == 0 &&  bluetoothOn != 0) {
+                    adapter.enable();
+                }
             }
 
         } catch (RuntimeException e) {
diff --git a/services/java/com/android/server/power/ShutdownThread.java b/services/java/com/android/server/power/ShutdownThread.java
index 82f72f7..d5b266a 100644
--- a/services/java/com/android/server/power/ShutdownThread.java
+++ b/services/java/com/android/server/power/ShutdownThread.java
@@ -340,8 +340,7 @@
                            bluetooth.getState() == BluetoothAdapter.STATE_OFF;
             if (!bluetoothOff) {
                 Log.w(TAG, "Disabling Bluetooth...");
-                //TODO(BT)
-                bluetooth.disable();  // disable but don't persist new state
+                bluetooth.disable(false);  // disable but don't persist new state
             }
         } catch (RemoteException ex) {
             Log.e(TAG, "RemoteException during bluetooth shutdown", ex);