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

Commit a46f2fb1 authored by Jaikumar Ganesh's avatar Jaikumar Ganesh
Browse files

Implement ACTION_CONNECTION_STATE_CHANGED intent.

Change-Id: I6b5783c189c9796ebd85d9d54bdcb07949bef27e
parent 7c7ff8c8
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -92,4 +92,6 @@ interface IBluetooth
    List<BluetoothDevice> getConnectedPanDevices();
    boolean connectPanDevice(in BluetoothDevice device);
    boolean disconnectPanDevice(in BluetoothDevice device);

    void sendConnectionStateChange(in BluetoothDevice device, int state, int prevState);
}
+2 −0
Original line number Diff line number Diff line
@@ -534,6 +534,8 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub {
            mContext.sendBroadcast(intent, BLUETOOTH_PERM);

            if (DBG) log("A2DP state : device: " + device + " State:" + prevState + "->" + state);

            mBluetoothService.sendConnectionStateChange(device, state, prevState);
        }
    }

+76 −1
Original line number Diff line number Diff line
@@ -162,6 +162,7 @@ public class BluetoothService extends IBluetooth.Stub {
    private final HashMap<BluetoothDevice, Pair<Integer, String>> mPanDevices;
    private final HashMap<String, Pair<byte[], byte[]>> mDeviceOobData;

    private int mProfilesConnected = 0, mProfilesConnecting = 0, mProfilesDisconnecting = 0;

    private static String mDockAddress;
    private String mDockPin;
@@ -411,6 +412,10 @@ public class BluetoothService extends IBluetooth.Stub {
        mAdapterProperties.clear();
        mServiceRecordToPid.clear();

        mProfilesConnected = 0;
        mProfilesConnecting = 0;
        mProfilesDisconnecting = 0;

        if (saveSetting) {
            persistBluetoothOnSetting(false);
        }
@@ -1563,6 +1568,7 @@ public class BluetoothService extends IBluetooth.Stub {
        mContext.sendBroadcast(intent, BLUETOOTH_PERM);

        if (DBG) log("Pan Device state : device: " + device + " State:" + prevState + "->" + state);
        sendConnectionStateChange(device, state, prevState);
    }

    /*package*/ synchronized void handlePanDeviceStateChange(BluetoothDevice device,
@@ -1790,7 +1796,7 @@ public class BluetoothService extends IBluetooth.Stub {
        mContext.sendBroadcast(intent, BLUETOOTH_PERM);

        if (DBG) log("InputDevice state : device: " + device + " State:" + prevState + "->" + state);

        sendConnectionStateChange(device, state, prevState);
    }

    /*package*/ void handleInputDevicePropertyChange(String address, boolean connected) {
@@ -2731,6 +2737,75 @@ public class BluetoothService extends IBluetooth.Stub {
        }
    }

    public synchronized void sendConnectionStateChange(BluetoothDevice device, int state,
                                                        int prevState) {
        if (updateCountersAndCheckForConnectionStateChange(device, state, prevState)) {
            state = getAdapterConnectionState(state);
            prevState = getAdapterConnectionState(prevState);
            Intent intent = new Intent(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
            intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
            intent.putExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, state);
            intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_CONNECTION_STATE, prevState);
            mContext.sendBroadcast(intent, BLUETOOTH_PERM);
            log("CONNECTION_STATE_CHANGE: " + device + ": " + prevState + "-> " + state);
        }
    }

    private int getAdapterConnectionState(int state) {
        switch (state) {
            case BluetoothProfile.STATE_CONNECTING:
                return BluetoothAdapter.STATE_CONNECTING;
            case BluetoothProfile.STATE_CONNECTED:
              return BluetoothAdapter.STATE_CONNECTED;
            case BluetoothProfile.STATE_DISCONNECTING:
              return BluetoothAdapter.STATE_DISCONNECTING;
            case BluetoothProfile.STATE_DISCONNECTED:
              return BluetoothAdapter.STATE_DISCONNECTED;
            default:
              Log.e(TAG, "Error in getAdapterConnectionState");
              return -1;
        }
    }

    private boolean updateCountersAndCheckForConnectionStateChange(BluetoothDevice device,
                                                                   int state,
                                                                   int prevState) {
        switch (state) {
            case BluetoothProfile.STATE_CONNECTING:
                mProfilesConnecting++;
                if (prevState == BluetoothAdapter.STATE_DISCONNECTING) mProfilesDisconnecting--;
                if (mProfilesConnected > 0 || mProfilesConnecting > 1) return false;

                break;
            case BluetoothProfile.STATE_CONNECTED:
                if (prevState == BluetoothAdapter.STATE_CONNECTING) mProfilesConnecting--;
                if (prevState == BluetoothAdapter.STATE_DISCONNECTING) mProfilesDisconnecting--;

                mProfilesConnected++;

                if (mProfilesConnected > 1) return false;
                break;
            case BluetoothProfile.STATE_DISCONNECTING:
                mProfilesDisconnecting++;
                mProfilesConnected--;

                if (mProfilesConnected > 0 || mProfilesDisconnecting > 1) return false;

                break;
            case BluetoothProfile.STATE_DISCONNECTED:
                if (prevState == BluetoothAdapter.STATE_CONNECTING) mProfilesConnecting--;
                if (prevState == BluetoothAdapter.STATE_DISCONNECTING) mProfilesDisconnecting--;

                if (prevState == BluetoothAdapter.STATE_CONNECTED) {
                    mProfilesConnected--;
                }

                if (mProfilesConnected  > 0 || mProfilesConnecting > 0) return false;
                break;
        }
        return true;
    }

    private static void log(String msg) {
        Log.d(TAG, msg);
    }