[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);
}
}
}

View File

@@ -61,7 +61,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
public class AudioSharingDeviceVolumeGroupController extends AudioSharingBasePreferenceController
implements DevicePreferenceCallback {
private static final String TAG = "AudioSharingDeviceVolumeGroupController";
private static final String TAG = "AudioSharingVolCtlr";
private static final String KEY = "audio_sharing_device_volume_group";
@Nullable private final LocalBluetoothManager mBtManager;

View File

@@ -16,27 +16,46 @@
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.NonNull;
import androidx.annotation.Nullable;
import com.android.settings.R;
import com.android.settings.bluetooth.Utils;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.widget.SeekBarPreference;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.VolumeControlProfile;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import com.android.settingslib.utils.ThreadUtils;
public class AudioSharingDeviceVolumePreference extends SeekBarPreference {
private static final String TAG = "AudioSharingVolPref";
public static final int MIN_VOLUME = 0;
public static final int MAX_VOLUME = 255;
private final Context mContext;
private final CachedBluetoothDevice mCachedDevice;
@Nullable protected SeekBar mSeekBar;
private Boolean mTrackingTouch = false;
private MetricsFeatureProvider mMetricsFeatureProvider =
FeatureFactory.getFeatureFactory().getMetricsFeatureProvider();
public AudioSharingDeviceVolumePreference(
Context context, @NonNull CachedBluetoothDevice device) {
super(context);
setLayoutResource(R.layout.preference_volume_slider);
mContext = context;
mCachedDevice = device;
}
@@ -54,4 +73,95 @@ public class AudioSharingDeviceVolumePreference extends SeekBarPreference {
setMax(MAX_VOLUME);
setMin(MIN_VOLUME);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
super.onProgressChanged(seekBar, progress, fromUser);
// When user use talk back swipe up/down or use Switch Access to change the volume bar
// progress, there is no onStopTrackingTouch triggered. So we need to check this scenario
// and update the device volume here.
if (fromUser && !mTrackingTouch) {
Log.d(TAG, "onProgressChanged from user and not in touch, handleProgressChange.");
handleProgressChange(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
mTrackingTouch = true;
super.onStartTrackingTouch(seekBar);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mTrackingTouch = false;
super.onStopTrackingTouch(seekBar);
// When user touch the volume bar to change volume, we only update the device volume when
// user stop touching the bar.
Log.d(TAG, "onStopTrackingTouch, handleProgressChange.");
handleProgressChange(seekBar.getProgress());
}
private void handleProgressChange(int progress) {
var unused =
ThreadUtils.postOnBackgroundThread(
() -> {
int groupId = BluetoothUtils.getGroupId(mCachedDevice);
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(mCachedDevice.getDevice(), progress);
}
});
}
private void setDeviceVolume(@Nullable BluetoothDevice device, int progress) {
if (device == null) {
Log.d(TAG, "Skip set device volume, device is null");
return;
}
LocalBluetoothManager btManager = Utils.getLocalBtManager(mContext);
VolumeControlProfile vc =
btManager == null ? null : btManager.getProfileManager().getVolumeControlProfile();
if (vc != null) {
vc.setDeviceVolume(device, progress, /* isGroupOp= */ true);
mMetricsFeatureProvider.action(
mContext,
SettingsEnums.ACTION_AUDIO_SHARING_CHANGE_MEDIA_DEVICE_VOLUME,
/* isPrimary= */ false);
Log.d(
TAG,
"set device volume, device = "
+ device.getAnonymizedAddress()
+ " volume = "
+ progress);
}
}
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);
Log.d(TAG, "set music stream volume, volume = " + progress);
} catch (RuntimeException e) {
Log.e(TAG, "Fail to setAudioManagerStreamVolumeForFallbackDevice, error = " + e);
}
}
}