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

Commit 707e47e9 authored by Ted Wang's avatar Ted Wang Committed by android-build-merger
Browse files

Merge "Refine absolute volume control when multi connections" am: f35912b1

am: df97b6e3

Change-Id: I38a4bb4c89040fb384b51231bdbaeedf7c99623e
parents 471d67f8 df97b6e3
Loading
Loading
Loading
Loading
+13 −4
Original line number Diff line number Diff line
@@ -785,10 +785,18 @@ static void volumeDeviceDisconnected(const RawAddress& address) {
                               j_bdaddr);
}

static void sendVolumeChangedNative(JNIEnv* env, jobject object, jint volume) {
static void sendVolumeChangedNative(JNIEnv* env, jobject object,
                                    jstring address, jint volume) {
  const char* tmp_addr = env->GetStringUTFChars(address, 0);
  RawAddress bdaddr;
  bool success = RawAddress::FromString(tmp_addr, bdaddr);
  env->ReleaseStringUTFChars(address, tmp_addr);

  if (!success) return;

  ALOGD("%s", __func__);
  for (const auto& cb : volumeCallbackMap) {
    cb.second.Run(volume & 0x7F);
  if (volumeCallbackMap.find(bdaddr) != volumeCallbackMap.end()) {
    volumeCallbackMap.find(bdaddr)->second.Run(volume & 0x7F);
  }
}

@@ -815,7 +823,8 @@ static JNINativeMethod sMethods[] = {
     (void*)connectDeviceNative},
    {"disconnectDeviceNative", "(Ljava/lang/String;)Z",
     (void*)disconnectDeviceNative},
    {"sendVolumeChangedNative", "(I)V", (void*)sendVolumeChangedNative},
    {"sendVolumeChangedNative", "(Ljava/lang/String;I)V",
     (void*)sendVolumeChangedNative},
};

int register_com_android_bluetooth_avrcp_target(JNIEnv* env) {
+3 −3
Original line number Diff line number Diff line
@@ -218,9 +218,9 @@ public class AvrcpNativeInterface {
        mAvrcpService.deviceDisconnected(device);
    }

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

    void setVolume(int volume) {
@@ -245,7 +245,7 @@ public class AvrcpNativeInterface {
    private native void cleanupNative();
    private native boolean connectDeviceNative(String bdaddr);
    private native boolean disconnectDeviceNative(String bdaddr);
    private native void sendVolumeChangedNative(int volume);
    private native void sendVolumeChangedNative(String bdaddr, int volume);

    private static void d(String msg) {
        if (DEBUG) {
+12 −16
Original line number Diff line number Diff line
@@ -277,15 +277,13 @@ public class AvrcpTargetService extends ProfileService {

    // TODO (apanicke): Add checks to blacklist Absolute Volume devices if they behave poorly.
    void setVolume(int avrcpVolume) {
        int deviceVolume =
                (int) Math.floor((double) avrcpVolume * sDeviceMaxVolume / AVRCP_MAX_VOL);
        if (DEBUG) {
            Log.d(TAG, "SendVolumeChanged: avrcpVolume=" + avrcpVolume
                    + " deviceVolume=" + deviceVolume
                    + " sDeviceMaxVolume=" + sDeviceMaxVolume);
        BluetoothDevice activeDevice = mFactory.getA2dpService().getActiveDevice();
        if (activeDevice == null) {
            Log.d(TAG, "setVolume: no active device");
            return;
        }
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, deviceVolume,
                AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_BLUETOOTH_ABS_VOLUME);

        mVolumeManager.setVolume(activeDevice, avrcpVolume);
    }

    /**
@@ -293,15 +291,13 @@ public class AvrcpTargetService extends ProfileService {
     * volume.
     */
    public void sendVolumeChanged(int deviceVolume) {
        int avrcpVolume =
                (int) Math.floor((double) deviceVolume * AVRCP_MAX_VOL / sDeviceMaxVolume);
        if (avrcpVolume > 127) avrcpVolume = 127;
        if (DEBUG) {
            Log.d(TAG, "SendVolumeChanged: avrcpVolume=" + avrcpVolume
                    + " deviceVolume=" + deviceVolume
                    + " sDeviceMaxVolume=" + sDeviceMaxVolume);
        BluetoothDevice activeDevice = mFactory.getA2dpService().getActiveDevice();
        if (activeDevice == null) {
            Log.d(TAG, "sendVolumeChanged: no active device");
            return;
        }
        mNativeInterface.sendVolumeChanged(avrcpVolume);

        mVolumeManager.sendVolumeChanged(activeDevice, deviceVolume);
    }

    Metadata getCurrentSongInfo() {
+27 −1
Original line number Diff line number Diff line
@@ -81,7 +81,7 @@ class AvrcpVolumeManager extends AudioDeviceCallback {
        if (mDeviceMap.get(device)) {
            int avrcpVolume = systemToAvrcpVolume(savedVolume);
            Log.i(TAG, "switchVolumeDevice: Updating device volume: avrcpVolume=" + avrcpVolume);
            mNativeInterface.sendVolumeChanged(avrcpVolume);
            mNativeInterface.sendVolumeChanged(device.getAddress(), avrcpVolume);
        }
    }

@@ -157,6 +157,32 @@ class AvrcpVolumeManager extends AudioDeviceCallback {
        return sNewDeviceVolume;
    }

    void setVolume(@NonNull BluetoothDevice device, int avrcpVolume) {
        int deviceVolume =
                (int) Math.floor((double) avrcpVolume * sDeviceMaxVolume / AVRCP_MAX_VOL);
        if (DEBUG) {
            Log.d(TAG, "setVolume: avrcpVolume=" + avrcpVolume
                    + " deviceVolume=" + deviceVolume
                    + " sDeviceMaxVolume=" + sDeviceMaxVolume);
        }
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, deviceVolume,
                AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_BLUETOOTH_ABS_VOLUME);
        storeVolumeForDevice(device);
    }

    void sendVolumeChanged(@NonNull BluetoothDevice device, int deviceVolume) {
        int avrcpVolume =
                (int) Math.floor((double) deviceVolume * AVRCP_MAX_VOL / sDeviceMaxVolume);
        if (avrcpVolume > 127) avrcpVolume = 127;
        if (DEBUG) {
            Log.d(TAG, "sendVolumeChanged: avrcpVolume=" + avrcpVolume
                    + " deviceVolume=" + deviceVolume
                    + " sDeviceMaxVolume=" + sDeviceMaxVolume);
        }
        mNativeInterface.sendVolumeChanged(device.getAddress(), avrcpVolume);
        storeVolumeForDevice(device);
    }

    @Override
    public synchronized void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
        if (mCurrentDevice == null) {