Bluetooth: fix issues re-enabling after crash

When Bluetooth crashes, sometimes an LE app restarts it before
ActivityManager gets a chance.  In this case, the Bluetooth manager
would assume the state should be LE only and did not continue to fully
enabled.

Luckily in this case the persisted system state is also ON.  Use the
persisted state as information about whether we should be fully enabled.

Test: basic sanity check, forced crash of BT
Bug: 33632976
Change-Id: I546d7abccb82a26fcca2eb70d6d7c76e9510404e
diff --git a/services/core/java/com/android/server/BluetoothManagerService.java b/services/core/java/com/android/server/BluetoothManagerService.java
index b2e38d8..9b21208 100644
--- a/services/core/java/com/android/server/BluetoothManagerService.java
+++ b/services/core/java/com/android/server/BluetoothManagerService.java
@@ -317,13 +317,15 @@
 
     /**
      *  Save the Bluetooth on/off state
-     *
      */
     private void persistBluetoothSetting(int value) {
         if (DBG) Slog.d(TAG, "Persisting Bluetooth Setting: " + value);
+        // waive WRITE_SECURE_SETTINGS permission check
+        long callingIdentity = Binder.clearCallingIdentity();
         Settings.Global.putInt(mContext.getContentResolver(),
                                Settings.Global.BLUETOOTH_ON,
                                value);
+        Binder.restoreCallingIdentity(callingIdentity);
     }
 
     /**
@@ -588,20 +590,26 @@
     }
 
     /**
-     * Action taken when GattService is turned off
+     * Action taken when GattService is turned on
      */
     private void onBluetoothGattServiceUp() {
         if (DBG) Slog.d(TAG,"BluetoothGatt Service is Up");
         try {
             mBluetoothLock.readLock().lock();
-            if (isBleAppPresent() == false && mBluetooth != null
-                  && mBluetooth.getState() == BluetoothAdapter.STATE_BLE_ON) {
+            if (mBluetooth == null) {
+                if (DBG) Slog.w(TAG, "onBluetoothServiceUp: mBluetooth is null!");
+                return;
+            }
+            int st = mBluetooth.getState();
+            if (st != BluetoothAdapter.STATE_BLE_ON) {
+                if (DBG) Slog.v(TAG, "onBluetoothServiceUp: state isn't BLE_ON: " +
+                        BluetoothAdapter.nameForState(st));
+                return;
+            }
+            if (isBluetoothPersistedStateOnBluetooth() || !isBleAppPresent()) {
+                // This triggers transition to STATE_ON
                 mBluetooth.onLeServiceUp();
-
-                // waive WRITE_SECURE_SETTINGS permission check
-                long callingIdentity = Binder.clearCallingIdentity();
                 persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH);
-                Binder.restoreCallingIdentity(callingIdentity);
             }
         } catch (RemoteException e) {
             Slog.e(TAG,"Unable to call onServiceUp", e);
@@ -727,10 +735,7 @@
 
         synchronized(mReceiver) {
             if (persist) {
-                // waive WRITE_SECURE_SETTINGS permission check
-                long callingIdentity = Binder.clearCallingIdentity();
                 persistBluetoothSetting(BLUETOOTH_OFF);
-                Binder.restoreCallingIdentity(callingIdentity);
             }
             mEnableExternal = false;
             sendDisableMsg();