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

Commit 0b969b04 authored by Aritra Sen's avatar Aritra Sen Committed by Gerrit Code Review
Browse files

Merge "CSIP Connection state changes not dependent on its broadcasts to be recorded." into main

parents 65cb0ced 8238f43a
Loading
Loading
Loading
Loading
+19 −26
Original line number Diff line number Diff line
@@ -37,7 +37,9 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.ParcelUuid;
import android.os.RemoteException;
import android.sysprop.BluetoothProperties;
@@ -81,6 +83,8 @@ public class CsipSetCoordinatorService extends ProfileService {
    private static final int MAX_CSIS_STATE_MACHINES = 10;
    private static CsipSetCoordinatorService sCsipSetCoordinatorService;

    private Handler mHandler = null;

    private AdapterService mAdapterService;
    private LeAudioService mLeAudioService;
    private DatabaseManager mDatabaseManager;
@@ -107,7 +111,6 @@ public class CsipSetCoordinatorService extends ProfileService {
            new ConcurrentHashMap<>();

    private BroadcastReceiver mBondStateChangedReceiver;
    private BroadcastReceiver mConnectionStateChangedReceiver;

    public static boolean isEnabled() {
        return BluetoothProperties.isProfileCsipSetCoordinatorEnabled().orElse(false);
@@ -145,6 +148,9 @@ public class CsipSetCoordinatorService extends ProfileService {
                "CsipSetCoordinatorNativeInterface cannot be null when"
                .concat("CsipSetCoordinatorService starts"));

        // Setup Handler.
        mHandler = new Handler(Looper.getMainLooper());

        // Get LE Audio service (can be null)
        mLeAudioService = mServiceFactory.getLeAudioService();

@@ -159,11 +165,6 @@ public class CsipSetCoordinatorService extends ProfileService {
        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        mBondStateChangedReceiver = new BondStateChangedReceiver();
        registerReceiver(mBondStateChangedReceiver, filter);
        filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(BluetoothCsipSetCoordinator.ACTION_CSIS_CONNECTION_STATE_CHANGED);
        mConnectionStateChangedReceiver = new ConnectionStateChangedReceiver();
        registerReceiver(mConnectionStateChangedReceiver, filter);

        // Mark service as started
        setCsipSetCoordinatorService(this);
@@ -198,8 +199,6 @@ public class CsipSetCoordinatorService extends ProfileService {
        // Unregister broadcast receivers
        unregisterReceiver(mBondStateChangedReceiver);
        mBondStateChangedReceiver = null;
        unregisterReceiver(mConnectionStateChangedReceiver);
        mConnectionStateChangedReceiver = null;

        // Destroy state machines and stop handler thread
        synchronized (mStateMachines) {
@@ -274,7 +273,8 @@ public class CsipSetCoordinatorService extends ProfileService {
     * @return true if connection is successful, false otherwise.
     */
    public boolean connect(BluetoothDevice device) {
        enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED, "Need BLUETOOTH_PRIVILEGED permission");
        enforceCallingOrSelfPermission(
                BLUETOOTH_PRIVILEGED, "Need BLUETOOTH_PRIVILEGED permission");
        if (DBG) {
            Log.d(TAG, "connect(): " + device);
        }
@@ -309,7 +309,8 @@ public class CsipSetCoordinatorService extends ProfileService {
     * @return true if disconnect is successful, false otherwise.
     */
    public boolean disconnect(BluetoothDevice device) {
        enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED, "Need BLUETOOTH_PRIVILEGED permission");
        enforceCallingOrSelfPermission(
                BLUETOOTH_PRIVILEGED, "Need BLUETOOTH_PRIVILEGED permission");
        if (DBG) {
            Log.d(TAG, "disconnect(): " + device);
        }
@@ -480,7 +481,8 @@ public class CsipSetCoordinatorService extends ProfileService {
     * @return true on success, otherwise false
     */
    public boolean setConnectionPolicy(BluetoothDevice device, int connectionPolicy) {
        enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED, "Need BLUETOOTH_PRIVILEGED permission");
        enforceCallingOrSelfPermission(
                BLUETOOTH_PRIVILEGED, "Need BLUETOOTH_PRIVILEGED permission");
        if (DBG) {
            Log.d(TAG, "Saved connectionPolicy " + device + " = " + connectionPolicy);
        }
@@ -501,7 +503,8 @@ public class CsipSetCoordinatorService extends ProfileService {
     * @return connection policy of the specified device
     */
    public int getConnectionPolicy(BluetoothDevice device) {
        enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED, "Need BLUETOOTH_PRIVILEGED permission");
        enforceCallingOrSelfPermission(
                BLUETOOTH_PRIVILEGED, "Need BLUETOOTH_PRIVILEGED permission");
        return mDatabaseManager.getProfileConnectionPolicy(
                device, BluetoothProfile.CSIP_SET_COORDINATOR);
    }
@@ -1018,6 +1021,10 @@ public class CsipSetCoordinatorService extends ProfileService {
        }
    }

    void handleConnectionStateChanged(BluetoothDevice device, int fromState, int toState) {
        mHandler.post(() -> connectionStateChanged(device, fromState, toState));
    }

    @VisibleForTesting
    synchronized void connectionStateChanged(BluetoothDevice device, int fromState, int toState) {
        if ((device == null) || (fromState == toState)) {
@@ -1051,20 +1058,6 @@ public class CsipSetCoordinatorService extends ProfileService {
        }
    }

    private class ConnectionStateChangedReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (!BluetoothCsipSetCoordinator.ACTION_CSIS_CONNECTION_STATE_CHANGED.equals(
                        intent.getAction())) {
                return;
            }
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            int toState = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1);
            int fromState = intent.getIntExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, -1);
            connectionStateChanged(device, fromState, toState);
        }
    }

    /**
     * Binder object: must be a static class or memory leak may occur
     */
+1 −0
Original line number Diff line number Diff line
@@ -516,6 +516,7 @@ public class CsipSetCoordinatorStateMachine extends StateMachine {
    private void csipConnectionState(int newState, int prevState) {
        log("Connection state " + mDevice + ": " + profileStateToString(prevState) + "->"
                + profileStateToString(newState));
        mService.handleConnectionStateChanged(mDevice, prevState, newState);

        Intent intent =
                new Intent(BluetoothCsipSetCoordinator.ACTION_CSIS_CONNECTION_STATE_CHANGED);