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

Commit 88e88efd authored by William Escande's avatar William Escande
Browse files

Use VolumeControlService instead of proxy

LeAudioService will no longer register VolumeControlService but will
directly call VolumeControlService.

Factory seems to be how A2dp talks to avrcpVolumeManager. I reproduced
how it works on LeAudio

Also the `Intent.resolveSystemService` is a hidden API and cannot be
called once bluetooth will be a mainline module, that's why i submit
this patch

Test: atest BluetoothInstrumentationTests
Bug: 200200870
Tag: #refactor
Change-Id: I4119bb794fb7f141a1994f9fa58f87684ddc22d8
parent 0b53f5e2
Loading
Loading
Loading
Loading
+3 −69
Original line number Diff line number Diff line
@@ -133,36 +133,6 @@ public class LeAudioService extends ProfileService {
    private BroadcastReceiver mBondStateChangedReceiver;
    private BroadcastReceiver mConnectionStateChangedReceiver;

    private volatile IBluetoothVolumeControl mVolumeControlProxy;
    VolumeControlService mVolumeControlService = null;
    private final ServiceConnection mVolumeControlProxyConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {
            if (DBG) {
                Log.d(TAG, "mVolumeControlProxyConnection connected");
            }
            synchronized (LeAudioService.this) {

                mVolumeControlProxy = IBluetoothVolumeControl.Stub.asInterface(service);
                mVolumeControlService =
                    VolumeControlService.getVolumeControlService();
                if (mVolumeControlService == null) {
                    Log.e(TAG, "VolumeControlService is null when LeAudioService starts");
                }
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName className) {
            if (DBG) {
                Log.d(TAG, "mVolumeControlProxy disconnected");
            }
            synchronized (LeAudioService.this) {
                mVolumeControlProxy = null;
            }
        }
    };

    @Override
    protected IProfileServiceBinder initBinder() {
        return new BluetoothLeAudioBinder(this);
@@ -209,10 +179,6 @@ public class LeAudioService extends ProfileService {
        mConnectionStateChangedReceiver = new ConnectionStateChangedReceiver();
        registerReceiver(mConnectionStateChangedReceiver, filter);


        /* Bind Volume control service */
        bindVolumeControlService();

        // Mark service as started
        setLeAudioService(this);

@@ -280,7 +246,6 @@ public class LeAudioService extends ProfileService {
        mAdapterService = null;
        mAudioManager = null;

        unbindVolumeControlService();
        return true;
    }

@@ -308,30 +273,6 @@ public class LeAudioService extends ProfileService {
        sLeAudioService = instance;
    }

    private void bindVolumeControlService() {
        synchronized (mVolumeControlProxyConnection) {
            Intent intent = new Intent(IBluetoothVolumeControl.class.getName());
            ComponentName comp = intent.resolveSystemService(getPackageManager(), 0);
            intent.setComponent(comp);
            if (comp == null || !bindService(intent, mVolumeControlProxyConnection, 0)) {
                Log.wtf(TAG, "Could not bind to IBluetoothVolumeControl Service with " +
                        intent);
            }
        }
    }
    private void unbindVolumeControlService() {
        synchronized (mVolumeControlProxyConnection) {
            if (mVolumeControlProxy != null) {
                if (DBG) {
                    Log.d(TAG, "Unbinding mVolumeControlProxyConnection");
                }
                mVolumeControlProxy = null;
                // Synchronization should make sure unbind can be successful
                unbindService(mVolumeControlProxyConnection);
            }
        }
    }

    public boolean connect(BluetoothDevice device) {
        if (DBG) {
            Log.d(TAG, "connect(): " + device);
@@ -1278,16 +1219,9 @@ public class LeAudioService extends ProfileService {
            return;
        }

        if (mVolumeControlService == null) {
            Log.e(TAG, "VolumeControl no available ");
            return;
        }

        try {
            mVolumeControlProxy.setVolumeGroup(mActiveDeviceGroupId, volume,
                                               this.getAttributionSource());
        } catch (RemoteException e) {
            Log.e(TAG, "Set Volume failed: " + e);
        VolumeControlService service = mServiceFactory.getVolumeControlService();
        if (service != null) {
            service.setVolumeGroup(mActiveDeviceGroupId, volume);
        }
    }

+4 −1
Original line number Diff line number Diff line
@@ -413,7 +413,10 @@ public class VolumeControlService extends ProfileService {
        mVolumeControlNativeInterface.setVolume(device, volume);
    }

    void setVolumeGroup(int groupId, int volume) {
    /**
     * {@hide}
     */
    public void setVolumeGroup(int groupId, int volume) {
        mVolumeControlNativeInterface.setVolumeGroup(groupId, volume);
    }