Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 57e4bf7a authored by Gurpreet Ghai's avatar Gurpreet Ghai Committed by Rashed Abdel-Tawab
Browse files

BT: Update wait function in ManagerService to handle BLE states

-> The wairforOnOff method while waiting for on/off event to
   occur doesn't takes BLE states into account. Since Bluetooth
   Manager Service needs to takes action to completely turn On
   or completely turn off Bluetooth when BLE turns ON, this
   intermediate state to handle BLE On State need to be handled
   at Manager Service to avoid waitforOnOff timeout.

-> Replaced the waitforOnOff method with waitForMonitoredOnOff
   which handles state changes while waiting.

-> In an another change, after receiving Enable Message, added
   condition to wait for off event only when Bluetooth is being
   disabled, no need to wait for Off while enabling Bluetooth
   because this serialization is handled at Adapter State.

Change-Id: I83dd6c8b28b819b1f305ba45853d56a685eccc4e
CRs-Fixed: 1102756
parent e5b82e0b
Loading
Loading
Loading
Loading
+31 −16
Original line number Diff line number Diff line
@@ -1317,7 +1317,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                        // the previous Bluetooth process has exited. The
                        // waiting period has three components:
                        // (a) Wait until the local state is STATE_OFF. This
                        //     is accomplished by "waitForOnOff(false, true)".
                        //     is accomplished by "waitForMonitoredOnOff(false, true)".
                        // (b) Wait until the STATE_OFF state is updated to
                        //     all components.
                        // (c) Wait until the Bluetooth process exits, and
@@ -1327,7 +1327,17 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                        // message. On slower devices, that delay needs to be
                        // on the order of (2 * SERVICE_RESTART_TIME_MS).
                        //
                        waitForOnOff(false, true);
                        // Wait for (a) is required only when Bluetooth is being
                        // turned off.
                        int state;
                        try {
                            state = mBluetooth.getState();
                        } catch (RemoteException e) {
                            Slog.e(TAG, "getState()", e);
                            break;
                        }
                        if(state == BluetoothAdapter.STATE_TURNING_OFF || state == BluetoothAdapter.STATE_BLE_TURNING_OFF)
                            waitForMonitoredOnOff(false, true);
                        Message restartMsg = mHandler.obtainMessage(
                                MESSAGE_RESTART_BLUETOOTH_SERVICE);
                        mHandler.sendMessageDelayed(restartMsg,
@@ -1339,10 +1349,10 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                    if (DBG) Slog.d(TAG, "MESSAGE_DISABLE: mBluetooth = " + mBluetooth);
                    mHandler.removeMessages(MESSAGE_RESTART_BLUETOOTH_SERVICE);
                    if (mEnable && mBluetooth != null) {
                        waitForOnOff(true, false);
                        waitForMonitoredOnOff(true, false);
                        mEnable = false;
                        handleDisable();
                        waitForOnOff(false, false);
                        waitForMonitoredOnOff(false, false);
                    } else {
                        mEnable = false;
                        handleDisable();
@@ -1459,9 +1469,9 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                    }

                    if (!mEnable) {
                        waitForOnOff(true, false);
                        waitForMonitoredOnOff(true, false);
                        handleDisable();
                        waitForOnOff(false, false);
                        waitForMonitoredOnOff(false, false);
                    }
                    break;
                }
@@ -1499,7 +1509,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                            (newState == BluetoothAdapter.STATE_OFF)) {
                        if (mEnable) {
                            Slog.d(TAG, "Entering STATE_OFF but mEnabled is true; restarting.");
                            waitForOnOff(false, true);
                            waitForMonitoredOnOff(false, true);
                            Message restartMsg = mHandler.obtainMessage(
                                    MESSAGE_RESTART_BLUETOOTH_SERVICE);
                            mHandler.sendMessageDelayed(restartMsg, 2 * SERVICE_RESTART_TIME_MS);
@@ -1878,7 +1888,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
     */
    private boolean waitForOnOff(boolean on, boolean off) {
        int i = 0;
        while (i < 10) {
        while (i < 16) {
            try {
                mBluetoothLock.readLock().lock();
                if (mBluetooth == null) break;
@@ -1896,9 +1906,9 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                mBluetoothLock.readLock().unlock();
            }
            if (on || off) {
                SystemClock.sleep(300);
                SystemClock.sleep(500);
            } else {
                SystemClock.sleep(50);
                SystemClock.sleep(30);
            }
            i++;
        }
@@ -1913,7 +1923,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
     */
    private boolean waitForMonitoredOnOff(boolean on, boolean off) {
        int i = 0;
        while (i < 16) {
        while (i < 10) {
            synchronized(mConnection) {
                try {
                    if (mBluetooth == null) break;
@@ -1922,12 +1932,17 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                        if (mBluetooth.getState() == BluetoothAdapter.STATE_BLE_ON) {
                            bluetoothStateChangeHandler(BluetoothAdapter.STATE_BLE_TURNING_ON,
                                                        BluetoothAdapter.STATE_BLE_ON);
                            boolean ret = waitForOnOff(on, off);
                            return ret;
                        }
                    } else if (off) {
                        if (mBluetooth.getState() == BluetoothAdapter.STATE_OFF) return true;
                        if (mBluetooth.getState() == BluetoothAdapter.STATE_BLE_ON)
                        if (mBluetooth.getState() == BluetoothAdapter.STATE_BLE_ON) {
                            bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,
                                                        BluetoothAdapter.STATE_BLE_ON);
                            boolean ret = waitForOnOff(on, off);
                            return ret;
                        }
                    } else {
                        if (mBluetooth.getState() != BluetoothAdapter.STATE_ON) return true;
                    }
@@ -1937,13 +1952,13 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                }
            }
            if (on || off) {
                SystemClock.sleep(500);
                SystemClock.sleep(300);
            } else {
                SystemClock.sleep(30);
                SystemClock.sleep(50);
            }
            i++;
        }
        Slog.e(TAG,"waitForOnOff time out");
        Slog.e(TAG,"waitForMonitoredOnOff time out");
        return false;
    }

@@ -1981,7 +1996,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
            mBluetoothLock.readLock().unlock();
        }

        waitForOnOff(false, true);
        waitForMonitoredOnOff(false, true);

        sendBluetoothServiceDownCallback();