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

Commit a8f02f91 authored by Chienyuan's avatar Chienyuan
Browse files

AVRCP: Choose proper address for native command/callback

Tag: #refactor
Bug: 197044261
Test: manual
BYPASS_LONG_LINES_REASON: Bluetooth likes 120 lines

Change-Id: I1a5d0f2b6002e1e51799f9d37cbdebefed65927a
parent ee8e0569
Loading
Loading
Loading
Loading
+22 −12
Original line number Diff line number Diff line
@@ -21,12 +21,15 @@ import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.util.Log;

import com.android.bluetooth.Utils;
import com.android.bluetooth.audio_util.ListItem;
import com.android.bluetooth.audio_util.Metadata;
import com.android.bluetooth.audio_util.PlayStatus;
import com.android.bluetooth.audio_util.PlayerInfo;
import com.android.bluetooth.btservice.AdapterService;

import java.util.List;
import java.util.Objects;

/**
 * Native Interface to communicate with the JNI layer. This class should never be passed null
@@ -38,11 +41,17 @@ public class AvrcpNativeInterface {

    private static AvrcpNativeInterface sInstance;
    private AvrcpTargetService mAvrcpService;
    private AdapterService mAdapterService;

    static {
        classInitNative();
    }

    private AvrcpNativeInterface() {
        mAdapterService = Objects.requireNonNull(AdapterService.getAdapterService(),
                "AdapterService cannot be null when AvrcpNativeInterface init");
    }

    static AvrcpNativeInterface getInterface() {
        if (sInstance == null) {
            sInstance = new AvrcpNativeInterface();
@@ -74,7 +83,8 @@ public class AvrcpNativeInterface {
    }

    void setBipClientStatus(String bdaddr, boolean connected) {
        setBipClientStatusNative(bdaddr, connected);
        String identityAddress = mAdapterService.getIdentityAddress(bdaddr);
        setBipClientStatusNative(identityAddress, connected);
    }

    Metadata getCurrentSongInfo() {
@@ -198,25 +208,25 @@ public class AvrcpNativeInterface {
    }

    boolean connectDevice(String bdaddr) {
        d("connectDevice: bdaddr=" + bdaddr);
        return connectDeviceNative(bdaddr);
        String identityAddress = mAdapterService.getIdentityAddress(bdaddr);
        d("connectDevice: identityAddress=" + identityAddress);
        return connectDeviceNative(identityAddress);
    }

    boolean disconnectDevice(String bdaddr) {
        d("disconnectDevice: bdaddr=" + bdaddr);
        return disconnectDeviceNative(bdaddr);
        String identityAddress = mAdapterService.getIdentityAddress(bdaddr);
        d("disconnectDevice: identityAddress=" + identityAddress);
        return disconnectDeviceNative(identityAddress);
    }

    void setActiveDevice(String bdaddr) {
        BluetoothDevice device =
                BluetoothAdapter.getDefaultAdapter().getRemoteDevice(bdaddr.toUpperCase());
        BluetoothDevice device = mAdapterService.getDeviceFromByte(Utils.getBytesFromAddress(bdaddr));
        d("setActiveDevice: device=" + device);
        mAvrcpService.setActiveDevice(device);
    }

    void deviceConnected(String bdaddr, boolean absoluteVolume) {
        BluetoothDevice device =
                BluetoothAdapter.getDefaultAdapter().getRemoteDevice(bdaddr.toUpperCase());
        BluetoothDevice device = mAdapterService.getDeviceFromByte(Utils.getBytesFromAddress(bdaddr));
        d("deviceConnected: device=" + device + " absoluteVolume=" + absoluteVolume);
        if (mAvrcpService == null) {
            Log.w(TAG, "deviceConnected: AvrcpTargetService is null");
@@ -227,8 +237,7 @@ public class AvrcpNativeInterface {
    }

    void deviceDisconnected(String bdaddr) {
        BluetoothDevice device =
                BluetoothAdapter.getDefaultAdapter().getRemoteDevice(bdaddr.toUpperCase());
        BluetoothDevice device = mAdapterService.getDeviceFromByte(Utils.getBytesFromAddress(bdaddr));
        d("deviceDisconnected: device=" + device);
        if (mAvrcpService == null) {
            Log.w(TAG, "deviceDisconnected: AvrcpTargetService is null");
@@ -240,7 +249,8 @@ public class AvrcpNativeInterface {

    void sendVolumeChanged(String bdaddr, int volume) {
        d("sendVolumeChanged: volume=" + volume);
        sendVolumeChangedNative(bdaddr, volume);
        String identityAddress = mAdapterService.getIdentityAddress(bdaddr);
        sendVolumeChangedNative(identityAddress, volume);
    }

    void setVolume(int volume) {
+10 −0
Original line number Diff line number Diff line
@@ -2751,6 +2751,16 @@ public class AdapterService extends Service {
        return device;
    }

    public String getIdentityAddress(String address) {
        BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(address.toUpperCase());
        DeviceProperties deviceProp = mRemoteDevices.getDeviceProperties(device);
        if (deviceProp.isConsolidated()) {
            return deviceProp.getIdentityAddress();
        } else {
            return address;
        }
    }

    private class CallerInfo {
        public String callerPackageName;
        public UserHandle user;