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

Commit a7d92b6f authored by Jason Monk's avatar Jason Monk
Browse files

Cache bluetooth connection state from callback

Bug: 21444506
Change-Id: Idebcc1974fdd7551645d10eea4e30d9e1133556b
parent 373816ef
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -149,9 +149,10 @@ public class BluetoothTile extends QSTile<QSTile.BooleanState> {

    private final BluetoothController.Callback mCallback = new BluetoothController.Callback() {
        @Override
        public void onBluetoothStateChange(boolean enabled, boolean connecting) {
        public void onBluetoothStateChange(boolean enabled) {
            refreshState();
        }

        @Override
        public void onBluetoothDevicesChanged() {
            mUiHandler.post(new Runnable() {
+1 −1
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@ public interface BluetoothController {
    void disconnect(CachedBluetoothDevice device);

    public interface Callback {
        void onBluetoothStateChange(boolean enabled, boolean connecting);
        void onBluetoothStateChange(boolean enabled);
        void onBluetoothDevicesChanged();
    }
}
+21 −8
Original line number Diff line number Diff line
@@ -41,7 +41,7 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa
    private final LocalBluetoothManager mLocalBluetoothManager;

    private boolean mEnabled;
    private boolean mConnecting;
    private int mConnectionState = BluetoothAdapter.STATE_DISCONNECTED;
    private CachedBluetoothDevice mLastDevice;

    private final H mHandler = new H();
@@ -63,7 +63,7 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa
            return;
        }
        pw.print("  mEnabled="); pw.println(mEnabled);
        pw.print("  mConnecting="); pw.println(mConnecting);
        pw.print("  mConnectionState="); pw.println(stateToString(mConnectionState));
        pw.print("  mLastDevice="); pw.println(mLastDevice);
        pw.print("  mCallbacks.size="); pw.println(mCallbacks.size());
        pw.println("  Bluetooth Devices:");
@@ -73,10 +73,25 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa
        }
    }

    private static String stateToString(int state) {
        switch (state) {
            case BluetoothAdapter.STATE_CONNECTED:
                return "CONNECTED";
            case BluetoothAdapter.STATE_CONNECTING:
                return "CONNECTING";
            case BluetoothAdapter.STATE_DISCONNECTED:
                return "DISCONNECTED";
            case BluetoothAdapter.STATE_DISCONNECTING:
                return "DISCONNECTING";
        }
        return "UNKNOWN(" + state + ")";
    }

    private String getDeviceString(CachedBluetoothDevice device) {
        return device.getName() + " " + device.getBondState() + " " + device.isConnected();
    }

    @Override
    public void addStateChangedCallback(Callback cb) {
        mCallbacks.add(cb);
        mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED);
@@ -94,14 +109,12 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa

    @Override
    public boolean isBluetoothConnected() {
        return mLocalBluetoothManager != null
                && mLocalBluetoothManager.getBluetoothAdapter().getConnectionState()
                == BluetoothAdapter.STATE_CONNECTED;
        return mConnectionState == BluetoothAdapter.STATE_CONNECTED;
    }

    @Override
    public boolean isBluetoothConnecting() {
        return mConnecting;
        return mConnectionState == BluetoothAdapter.STATE_CONNECTING;
    }

    @Override
@@ -190,7 +203,7 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa

    @Override
    public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) {
        mConnecting = state == BluetoothAdapter.STATE_CONNECTING;
        mConnectionState = state;
        mLastDevice = cachedDevice;
        updateConnected();
        mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED);
@@ -225,7 +238,7 @@ public class BluetoothControllerImpl implements BluetoothController, BluetoothCa
        }

        private void fireStateChange(BluetoothController.Callback cb) {
            cb.onBluetoothStateChange(mEnabled, mConnecting);
            cb.onBluetoothStateChange(mEnabled);
        }
    }
}