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

Commit f5c0cf9d authored by Chienyuan Huang's avatar Chienyuan Huang Committed by Gerrit Code Review
Browse files

Merge changes I1a5d0f2b,I5fe90070,Id31a0f9a

* changes:
  AVRCP: Choose proper address for native command/callback
  A2DP: Choose proper address for native command/callback
  HFP: Choose proper address for native command/callback
parents 9edb3f08 a8f02f91
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -29,10 +29,12 @@ import android.bluetooth.BluetoothDevice;
import android.util.Log;

import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;

import java.util.Arrays;
import java.util.Objects;

/**
 * A2DP Native Interface to/from JNI.
@@ -41,6 +43,7 @@ public class A2dpNativeInterface {
    private static final String TAG = "A2dpNativeInterface";
    private static final boolean DBG = true;
    private BluetoothAdapter mAdapter;
    private AdapterService mAdapterService;

    @GuardedBy("INSTANCE_LOCK")
    private static A2dpNativeInterface sInstance;
@@ -56,6 +59,8 @@ public class A2dpNativeInterface {
        if (mAdapter == null) {
            Log.wtf(TAG, "No Bluetooth Adapter Available");
        }
        mAdapterService = Objects.requireNonNull(AdapterService.getAdapterService(),
                "AdapterService cannot be null when A2dpNativeInterface init");
    }

    /**
@@ -145,14 +150,14 @@ public class A2dpNativeInterface {
    }

    private BluetoothDevice getDevice(byte[] address) {
        return mAdapter.getRemoteDevice(address);
        return mAdapterService.getDeviceFromByte(address);
    }

    private byte[] getByteAddress(BluetoothDevice device) {
        if (device == null) {
            return Utils.getBytesFromAddress("00:00:00:00:00:00");
        }
        return Utils.getBytesFromAddress(device.getAddress());
        return mAdapterService.getByteIdentityAddress(device);
    }

    private void sendMessageToService(A2dpStackEvent event) {
+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
@@ -2758,6 +2758,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;
+31 −21
Original line number Diff line number Diff line
@@ -20,9 +20,11 @@ import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.util.Log;

import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.internal.annotations.VisibleForTesting;

import java.util.Objects;

/**
 * Defines native calls that are used by state machine/service to either send or receive
 * messages to/from the native stack. This file is registered for the native methods in
@@ -39,8 +41,12 @@ public class HeadsetNativeInterface {

    private static HeadsetNativeInterface sInterface;
    private static final Object INSTANCE_LOCK = new Object();
    private AdapterService mAdapterService;

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

    /**
     * This class is a singleton because native library should only be loaded once
@@ -68,7 +74,11 @@ public class HeadsetNativeInterface {
    }

    private BluetoothDevice getDevice(byte[] address) {
        return mAdapter.getRemoteDevice(Utils.getAddressStringFromByte(address));
        return mAdapterService.getDeviceFromByte(address);
    }

    private byte[] getByteAddress(BluetoothDevice device) {
        return mAdapterService.getByteIdentityAddress(device);
    }

    void onConnectionStateChanged(int state, byte[] address) {
@@ -238,7 +248,7 @@ public class HeadsetNativeInterface {
     */
    @VisibleForTesting
    public boolean atResponseCode(BluetoothDevice device, int responseCode, int errorCode) {
        return atResponseCodeNative(responseCode, errorCode, Utils.getByteAddress(device));
        return atResponseCodeNative(responseCode, errorCode, getByteAddress(device));
    }

    /**
@@ -250,7 +260,7 @@ public class HeadsetNativeInterface {
     */
    @VisibleForTesting
    public boolean atResponseString(BluetoothDevice device, String responseString) {
        return atResponseStringNative(responseString, Utils.getByteAddress(device));
        return atResponseStringNative(responseString, getByteAddress(device));
    }

    /**
@@ -261,7 +271,7 @@ public class HeadsetNativeInterface {
     */
    @VisibleForTesting
    public boolean connectHfp(BluetoothDevice device) {
        return connectHfpNative(Utils.getByteAddress(device));
        return connectHfpNative(getByteAddress(device));
    }

    /**
@@ -272,7 +282,7 @@ public class HeadsetNativeInterface {
     */
    @VisibleForTesting
    public boolean disconnectHfp(BluetoothDevice device) {
        return disconnectHfpNative(Utils.getByteAddress(device));
        return disconnectHfpNative(getByteAddress(device));
    }

    /**
@@ -283,7 +293,7 @@ public class HeadsetNativeInterface {
     */
    @VisibleForTesting
    public boolean connectAudio(BluetoothDevice device) {
        return connectAudioNative(Utils.getByteAddress(device));
        return connectAudioNative(getByteAddress(device));
    }

    /**
@@ -294,7 +304,7 @@ public class HeadsetNativeInterface {
     */
    @VisibleForTesting
    public boolean disconnectAudio(BluetoothDevice device) {
        return disconnectAudioNative(Utils.getByteAddress(device));
        return disconnectAudioNative(getByteAddress(device));
    }

    /**
@@ -305,7 +315,7 @@ public class HeadsetNativeInterface {
     * @return true if the device support echo cancellation or noise reduction, false otherwise
     */
    public boolean isNoiseReductionSupported(BluetoothDevice device) {
        return isNoiseReductionSupportedNative(Utils.getByteAddress(device));
        return isNoiseReductionSupportedNative(getByteAddress(device));
    }

    /**
@@ -315,7 +325,7 @@ public class HeadsetNativeInterface {
     * @return true if the device supports voice recognition, false otherwise
     */
    public boolean isVoiceRecognitionSupported(BluetoothDevice device) {
        return isVoiceRecognitionSupportedNative(Utils.getByteAddress(device));
        return isVoiceRecognitionSupportedNative(getByteAddress(device));
    }

    /**
@@ -326,7 +336,7 @@ public class HeadsetNativeInterface {
     */
    @VisibleForTesting
    public boolean startVoiceRecognition(BluetoothDevice device) {
        return startVoiceRecognitionNative(Utils.getByteAddress(device));
        return startVoiceRecognitionNative(getByteAddress(device));
    }


@@ -338,7 +348,7 @@ public class HeadsetNativeInterface {
     */
    @VisibleForTesting
    public boolean stopVoiceRecognition(BluetoothDevice device) {
        return stopVoiceRecognitionNative(Utils.getByteAddress(device));
        return stopVoiceRecognitionNative(getByteAddress(device));
    }

    /**
@@ -351,7 +361,7 @@ public class HeadsetNativeInterface {
     */
    @VisibleForTesting
    public boolean setVolume(BluetoothDevice device, int volumeType, int volume) {
        return setVolumeNative(volumeType, volume, Utils.getByteAddress(device));
        return setVolumeNative(volumeType, volume, getByteAddress(device));
    }

    /**
@@ -371,7 +381,7 @@ public class HeadsetNativeInterface {
    public boolean cindResponse(BluetoothDevice device, int service, int numActive, int numHeld,
            int callState, int signal, int roam, int batteryCharge) {
        return cindResponseNative(service, numActive, numHeld, callState, signal, roam,
                batteryCharge, Utils.getByteAddress(device));
                batteryCharge, getByteAddress(device));
    }

    /**
@@ -384,7 +394,7 @@ public class HeadsetNativeInterface {
    @VisibleForTesting
    public boolean notifyDeviceStatus(BluetoothDevice device, HeadsetDeviceState deviceState) {
        return notifyDeviceStatusNative(deviceState.mService, deviceState.mRoam,
                deviceState.mSignal, deviceState.mBatteryCharge, Utils.getByteAddress(device));
                deviceState.mSignal, deviceState.mBatteryCharge, getByteAddress(device));
    }

    /**
@@ -410,7 +420,7 @@ public class HeadsetNativeInterface {
    public boolean clccResponse(BluetoothDevice device, int index, int dir, int status, int mode,
            boolean mpty, String number, int type) {
        return clccResponseNative(index, dir, status, mode, mpty, number, type,
                Utils.getByteAddress(device));
                getByteAddress(device));
    }

    /**
@@ -422,7 +432,7 @@ public class HeadsetNativeInterface {
     */
    @VisibleForTesting
    public boolean copsResponse(BluetoothDevice device, String operatorName) {
        return copsResponseNative(operatorName, Utils.getByteAddress(device));
        return copsResponseNative(operatorName, getByteAddress(device));
    }

    /**
@@ -441,7 +451,7 @@ public class HeadsetNativeInterface {
    public boolean phoneStateChange(BluetoothDevice device, HeadsetCallState callState) {
        return phoneStateChangeNative(callState.mNumActive, callState.mNumHeld,
                callState.mCallState, callState.mNumber, callState.mType, callState.mName,
                Utils.getByteAddress(device));
                getByteAddress(device));
    }

    /**
@@ -464,7 +474,7 @@ public class HeadsetNativeInterface {
     */
    @VisibleForTesting
    public boolean sendBsir(BluetoothDevice device, boolean value) {
        return sendBsirNative(value, Utils.getByteAddress(device));
        return sendBsirNative(value, getByteAddress(device));
    }

    /**
@@ -474,7 +484,7 @@ public class HeadsetNativeInterface {
     */
    @VisibleForTesting
    public boolean setActiveDevice(BluetoothDevice device) {
        return setActiveDeviceNative(Utils.getByteAddress(device));
        return setActiveDeviceNative(getByteAddress(device));
    }

    /* Native methods */
+1 −1
Original line number Diff line number Diff line
@@ -124,7 +124,7 @@ public class PanService extends ProfileService {
    @Override
    protected boolean start() {
        mAdapterService = Objects.requireNonNull(AdapterService.getAdapterService(),
                "AdapterService cannot be null when HeadsetService starts");
                "AdapterService cannot be null when PanService starts");
        mDatabaseManager = Objects.requireNonNull(AdapterService.getAdapterService().getDatabase(),
                "DatabaseManager cannot be null when PanService starts");