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

Commit 39f87d89 authored by Nitin Arora's avatar Nitin Arora Committed by Gerrit - the friendly Code Review server
Browse files

Bluetooth: Handle user switch with new states

When user switch occurs, BT is toggled. However since the state
change callbacks are disabled, state never moves forward from
STATE_BLE_ON. This causes toggle timeout, causing recovery.
This change handles the case by manually causing the state
change handling during the wait period.

CRs-Fixed: 918585
Change-Id: I8fd8dcbc88d9f8b41d4f81957ccbade4b0fd8507
parent 9611ba2c
Loading
Loading
Loading
Loading
+44 −2
Original line number Diff line number Diff line
@@ -1483,7 +1483,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                            mState = BluetoothAdapter.STATE_TURNING_ON;
                        }

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

                        if (mState == BluetoothAdapter.STATE_TURNING_ON) {
                            bluetoothStateChangeHandler(mState, BluetoothAdapter.STATE_ON);
@@ -1496,7 +1496,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
                        bluetoothStateChangeHandler(BluetoothAdapter.STATE_ON,
                                                    BluetoothAdapter.STATE_TURNING_OFF);

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

                        bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,
                                                    BluetoothAdapter.STATE_OFF);
@@ -1776,6 +1776,48 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
        return false;
    }

    /**
     *  if on is true, wait for state become ON
     *  if off is true, wait for state become OFF
     *  if both on and off are false, wait for state not ON
     */
    private boolean waitForMonitoredOnOff(boolean on, boolean off) {
        int i = 0;
        while (i < 10) {
            synchronized(mConnection) {
                try {
                    if (mBluetooth == null) break;
                    if (on) {
                        if (mBluetooth.getState() == BluetoothAdapter.STATE_ON) return true;
                        if (mBluetooth.getState() == BluetoothAdapter.STATE_BLE_ON) {
                            bluetoothStateChangeHandler(BluetoothAdapter.STATE_BLE_TURNING_ON,
                                                        BluetoothAdapter.STATE_BLE_ON);
                        }
                    } else if (off) {
                        if (mBluetooth.getState() == BluetoothAdapter.STATE_OFF) return true;
                        if (mBluetooth.getState() == BluetoothAdapter.STATE_BLE_ON) {
                            bluetoothStateChangeHandler(BluetoothAdapter.STATE_TURNING_OFF,
                                                        BluetoothAdapter.STATE_BLE_ON);
                        }
                    } else {
                        if (mBluetooth.getState() != BluetoothAdapter.STATE_ON) return true;
                    }
                } catch (RemoteException e) {
                    Log.e(TAG, "getState()", e);
                    break;
                }
            }
            if (on || off) {
                SystemClock.sleep(800);
            } else {
                SystemClock.sleep(50);
            }
            i++;
        }
        Log.e(TAG,"waitForOnOff time out");
        return false;
    }

    private void sendDisableMsg() {
        mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISABLE));
    }