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

Commit ea36f956 authored by Aritra Sen's avatar Aritra Sen
Browse files

Remove dependency on BT broadcasts inside the Bluetooth stack for Volume...

Remove dependency on BT broadcasts inside the Bluetooth stack for Volume control connection changes.

Bug: 296932947
Test: atest BluetoothInstrumentationTests
Tag: #refactor
Change-Id: Ifbb5e29d5a24a9582695487b1865a3950a5053c5
parent c5d7951c
Loading
Loading
Loading
Loading
+8 −22
Original line number Diff line number Diff line
@@ -25,7 +25,6 @@ import android.annotation.RequiresPermission;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothUuid;
import android.bluetooth.BluetoothVolumeControl;
import android.bluetooth.IBluetoothCsipSetCoordinator;
import android.bluetooth.IBluetoothLeAudio;
import android.bluetooth.IBluetoothVolumeControl;
@@ -36,7 +35,9 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.ParcelUuid;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
@@ -77,6 +78,7 @@ public class VolumeControlService extends ProfileService {
    private DatabaseManager mDatabaseManager;
    private HandlerThread mStateMachinesThread;
    private BluetoothDevice mPreviousAudioDevice;
    private Handler mHandler = null;

    @VisibleForTesting
    RemoteCallbackList<IBluetoothVolumeControlCallback> mCallbacks;
@@ -193,7 +195,6 @@ public class VolumeControlService extends ProfileService {
    private final Map<Integer, Boolean> mGroupMuteCache = new HashMap<>();

    private BroadcastReceiver mBondStateChangedReceiver;
    private BroadcastReceiver mConnectionStateChangedReceiver;

    @VisibleForTesting
    ServiceFactory mFactory = new ServiceFactory();
@@ -237,6 +238,7 @@ public class VolumeControlService extends ProfileService {
                "AudioManager cannot be null when VolumeControlService starts");

        // Start handler thread for state machines
        mHandler = new Handler(Looper.getMainLooper());
        mStateMachines.clear();
        mStateMachinesThread = new HandlerThread("VolumeControlService.StateMachines");
        mStateMachinesThread.start();
@@ -247,11 +249,6 @@ public class VolumeControlService 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(BluetoothVolumeControl.ACTION_CONNECTION_STATE_CHANGED);
        mConnectionStateChangedReceiver = new ConnectionStateChangedReceiver();
        registerReceiver(mConnectionStateChangedReceiver, filter);

        mAudioOffsets.clear();
        mGroupVolumeCache.clear();
@@ -283,8 +280,6 @@ public class VolumeControlService extends ProfileService {
        // Unregister broadcast receivers
        unregisterReceiver(mBondStateChangedReceiver);
        mBondStateChangedReceiver = null;
        unregisterReceiver(mConnectionStateChangedReceiver);
        mConnectionStateChangedReceiver = null;

        // Destroy state machines and stop handler thread
        synchronized (mStateMachines) {
@@ -1076,6 +1071,10 @@ public class VolumeControlService 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) {
@@ -1115,19 +1114,6 @@ public class VolumeControlService extends ProfileService {
        }
    }

    private class ConnectionStateChangedReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (!BluetoothVolumeControl.ACTION_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
@@ -506,6 +506,7 @@ public class VolumeControlStateMachine extends StateMachine {
        log("Connection state " + mDevice + ": " + profileStateToString(prevState)
                + "->" + profileStateToString(newState));

        mService.handleConnectionStateChanged(mDevice, prevState, newState);
        Intent intent = new Intent(BluetoothVolumeControl.ACTION_CONNECTION_STATE_CHANGED);
        intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, prevState);
        intent.putExtra(BluetoothProfile.EXTRA_STATE, newState);