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

Commit 816ceadd authored by jonerlin's avatar jonerlin
Browse files

HearingAid: Check the value of mConnectionState in isConnected

* In current HearingAid state machine design, there could be potential
critical timing to cause java.lang.ArrayIndexOutOfBoundsException:
index=-1, when HearingAid state machine exit one state to enter another
state. at this timing, the mStateStackTopIndex value of mStateStack be set to -1.
* Checking mConnectionState value which maintained in
HearingAidStateMachine to avoid this AndroidRuntime exception.

Bug: 154663576
Test: Enable Bluetooth -> Connect Bluetooth headset -> Disconnect
Bluetooth headset -> Disable Bluetooth then check HearingAid state
machine log.

Change-Id: I21826a3e80d93b0cab7210d40975fa461485dcda
parent b7e0734d
Loading
Loading
Loading
Loading
+7 −15
Original line number Diff line number Diff line
@@ -81,6 +81,7 @@ final class HearingAidStateMachine extends StateMachine {
    private Connecting mConnecting;
    private Disconnecting mDisconnecting;
    private Connected mConnected;
    private int mConnectionState = BluetoothProfile.STATE_DISCONNECTED;
    private int mLastConnectionState = -1;

    private HearingAidService mService;
@@ -132,6 +133,7 @@ final class HearingAidStateMachine extends StateMachine {
        public void enter() {
            Log.i(TAG, "Enter Disconnected(" + mDevice + "): " + messageWhatToString(
                    getCurrentMessage().what));
            mConnectionState = BluetoothProfile.STATE_DISCONNECTED;

            removeDeferredMessages(DISCONNECT);

@@ -239,6 +241,7 @@ final class HearingAidStateMachine extends StateMachine {
            Log.i(TAG, "Enter Connecting(" + mDevice + "): "
                    + messageWhatToString(getCurrentMessage().what));
            sendMessageDelayed(CONNECT_TIMEOUT, sConnectTimeoutMs);
            mConnectionState = BluetoothProfile.STATE_CONNECTING;
            broadcastConnectionState(BluetoothProfile.STATE_CONNECTING, mLastConnectionState);
        }

@@ -329,6 +332,7 @@ final class HearingAidStateMachine extends StateMachine {
            Log.i(TAG, "Enter Disconnecting(" + mDevice + "): "
                    + messageWhatToString(getCurrentMessage().what));
            sendMessageDelayed(CONNECT_TIMEOUT, sConnectTimeoutMs);
            mConnectionState = BluetoothProfile.STATE_DISCONNECTING;
            broadcastConnectionState(BluetoothProfile.STATE_DISCONNECTING, mLastConnectionState);
        }

@@ -426,6 +430,7 @@ final class HearingAidStateMachine extends StateMachine {
        public void enter() {
            Log.i(TAG, "Enter Connected(" + mDevice + "): "
                    + messageWhatToString(getCurrentMessage().what));
            mConnectionState = BluetoothProfile.STATE_CONNECTED;
            removeDeferredMessages(CONNECT);
            broadcastConnectionState(BluetoothProfile.STATE_CONNECTED, mLastConnectionState);
        }
@@ -496,20 +501,7 @@ final class HearingAidStateMachine extends StateMachine {
    }

    int getConnectionState() {
        String currentState = getCurrentState().getName();
        switch (currentState) {
            case "Disconnected":
                return BluetoothProfile.STATE_DISCONNECTED;
            case "Connecting":
                return BluetoothProfile.STATE_CONNECTING;
            case "Connected":
                return BluetoothProfile.STATE_CONNECTED;
            case "Disconnecting":
                return BluetoothProfile.STATE_DISCONNECTING;
            default:
                Log.e(TAG, "Bad currentState: " + currentState);
                return BluetoothProfile.STATE_DISCONNECTED;
        }
        return mConnectionState;
    }

    BluetoothDevice getDevice() {
@@ -517,7 +509,7 @@ final class HearingAidStateMachine extends StateMachine {
    }

    synchronized boolean isConnected() {
        return getCurrentState() == mConnected;
        return (getConnectionState() == BluetoothProfile.STATE_CONNECTED);
    }

    // This method does not check for error condition (newState == prevState)