[Audiosharing] Handle device volume when volume bar is changed without touch

Fix volume control when user use talk back swipe up/down or use Switch
Access to change the volume bar progress

Fix: 357028436
Fix: 357028435
Test: atest
Flag: com.android.settingslib.flags.enable_le_audio_sharing
Change-Id: Iab7d48ab8713a3a809cc8f7b28381a2b86b9be5f
This commit is contained in:
Yiyi Shen
2024-08-06 18:20:02 +08:00
parent 7038ee8d24
commit adbdc16113
5 changed files with 287 additions and 156 deletions

View File

@@ -16,13 +16,9 @@
package com.android.settings.connecteddevice.audiosharing;
import android.app.settings.SettingsEnums;
import android.bluetooth.BluetoothCsipSetCoordinator;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.media.AudioManager;
import android.util.Log;
import android.widget.SeekBar;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
@@ -40,7 +36,7 @@ import com.android.settingslib.bluetooth.VolumeControlProfile;
public class AudioSharingDeviceVolumeControlUpdater extends BluetoothDeviceUpdater
implements Preference.OnPreferenceClickListener {
private static final String TAG = "AudioSharingDeviceVolumeControlUpdater";
private static final String TAG = "AudioSharingVolUpdater";
@VisibleForTesting
static final String PREF_KEY_PREFIX = "audio_sharing_volume_control_";
@@ -91,36 +87,9 @@ public class AudioSharingDeviceVolumeControlUpdater extends BluetoothDeviceUpdat
if (cachedDevice == null) return;
final BluetoothDevice device = cachedDevice.getDevice();
if (!mPreferenceMap.containsKey(device)) {
SeekBar.OnSeekBarChangeListener listener =
new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(
SeekBar seekBar, int progress, boolean fromUser) {}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress = seekBar.getProgress();
int groupId = BluetoothUtils.getGroupId(cachedDevice);
if (groupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID
&& groupId
== BluetoothUtils.getPrimaryGroupIdForBroadcast(
mContext.getContentResolver())) {
// Set media stream volume for primary buds, audio manager will
// update all buds volume in the audio sharing.
setAudioManagerStreamVolume(progress);
} else {
// Set buds volume for other buds.
setDeviceVolume(cachedDevice, progress);
}
}
};
AudioSharingDeviceVolumePreference vPreference =
new AudioSharingDeviceVolumePreference(mPrefContext, cachedDevice);
vPreference.initialize();
vPreference.setOnSeekBarChangeListener(listener);
vPreference.setKey(getPreferenceKeyPrefix() + cachedDevice.hashCode());
vPreference.setIcon(com.android.settingslib.R.drawable.ic_bt_untethered_earbuds);
vPreference.setTitle(cachedDevice.getName());
@@ -154,35 +123,4 @@ public class AudioSharingDeviceVolumeControlUpdater extends BluetoothDeviceUpdat
@Override
public void refreshPreference() {}
private void setDeviceVolume(CachedBluetoothDevice cachedDevice, int progress) {
if (mVolumeControl != null) {
mVolumeControl.setDeviceVolume(
cachedDevice.getDevice(), progress, /* isGroupOp= */ true);
mMetricsFeatureProvider.action(
mContext,
SettingsEnums.ACTION_AUDIO_SHARING_CHANGE_MEDIA_DEVICE_VOLUME,
/* isPrimary= */ false);
}
}
private void setAudioManagerStreamVolume(int progress) {
int seekbarRange =
AudioSharingDeviceVolumePreference.MAX_VOLUME
- AudioSharingDeviceVolumePreference.MIN_VOLUME;
try {
AudioManager audioManager = mContext.getSystemService(AudioManager.class);
int streamVolumeRange =
audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
- audioManager.getStreamMinVolume(AudioManager.STREAM_MUSIC);
int volume = Math.round((float) progress * streamVolumeRange / seekbarRange);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
mMetricsFeatureProvider.action(
mContext,
SettingsEnums.ACTION_AUDIO_SHARING_CHANGE_MEDIA_DEVICE_VOLUME,
/* isPrimary= */ true);
} catch (RuntimeException e) {
Log.e(TAG, "Fail to setAudioManagerStreamVolumeForFallbackDevice, error = " + e);
}
}
}