[Audiosharing] Avoid audio sharing dialogs in call

Test: atest
Bug: 362714470
Flag: com.android.settingslib.flags.enable_le_audio_sharing
Change-Id: I972d65727865793454de3eb0d9f07926d236afd7
This commit is contained in:
Yiyi Shen
2024-08-29 18:48:03 +08:00
parent 840495eb5c
commit abd3889b5c
4 changed files with 54 additions and 2 deletions

View File

@@ -26,6 +26,7 @@ import com.android.settings.connecteddevice.DevicePreferenceCallback;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.utils.ThreadUtils;
/** Controller to maintain available media Bluetooth devices */
public class AvailableMediaBluetoothDeviceUpdater extends BluetoothDeviceUpdater
@@ -135,7 +136,9 @@ public class AvailableMediaBluetoothDeviceUpdater extends BluetoothDeviceUpdater
@Override
public boolean onPreferenceClick(Preference preference) {
mMetricsFeatureProvider.logClickedPreference(preference, mMetricsCategory);
mDevicePreferenceCallback.onDeviceClick(preference);
var unused =
ThreadUtils.postOnBackgroundThread(
() -> mDevicePreferenceCallback.onDeviceClick(preference));
return true;
}

View File

@@ -59,6 +59,7 @@ import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import com.android.settingslib.utils.ThreadUtils;
import java.util.Locale;
import java.util.concurrent.Executor;
@@ -287,7 +288,8 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
if (AudioSharingUtils.isAudioSharingProfileReady(mProfileManager)) {
if (!mIntentHandled.get()) {
Log.d(TAG, "displayPreference: profile ready, handleDeviceClickFromIntent");
handleDeviceClickFromIntent();
var unused =
ThreadUtils.postOnBackgroundThread(() -> handleDeviceClickFromIntent());
mIntentHandled.set(true);
}
}

View File

@@ -25,6 +25,7 @@ import android.bluetooth.BluetoothLeBroadcast;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.bluetooth.BluetoothLeBroadcastReceiveState;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.util.Pair;
@@ -64,6 +65,7 @@ public class AudioSharingDialogHandler {
@Nullable private final CachedBluetoothDeviceManager mDeviceManager;
@Nullable private final LocalBluetoothLeBroadcast mBroadcast;
@Nullable private final LocalBluetoothLeBroadcastAssistant mAssistant;
@Nullable private final AudioManager mAudioManager;
private final MetricsFeatureProvider mMetricsFeatureProvider;
private boolean mIsStoppingBroadcast = false;
@@ -157,6 +159,7 @@ public class AudioSharingDialogHandler {
mLocalBtManager != null
? mLocalBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile()
: null;
mAudioManager = context.getSystemService(AudioManager.class);
mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider();
}
@@ -178,6 +181,22 @@ public class AudioSharingDialogHandler {
public void handleDeviceConnected(
@NonNull CachedBluetoothDevice cachedDevice, boolean userTriggered) {
String anonymizedAddress = cachedDevice.getDevice().getAnonymizedAddress();
if (mAudioManager != null) {
int audioMode = mAudioManager.getMode();
if (audioMode == AudioManager.MODE_RINGTONE
|| audioMode == AudioManager.MODE_IN_CALL
|| audioMode == AudioManager.MODE_IN_COMMUNICATION) {
Log.d(TAG, "Skip handleDeviceConnected, audio mode = " + audioMode);
// TODO: add metric for this case
if (userTriggered) {
// If this method is called with user triggered, e.g. manual click on the
// "Connected devices" page, we need call setActive for the device, since user
// intend to switch active device for the call.
cachedDevice.setActive();
}
return;
}
}
boolean isBroadcasting = isBroadcasting();
boolean isLeAudioSupported = AudioSharingUtils.isLeAudioSupported(cachedDevice);
if (!isLeAudioSupported) {

View File

@@ -39,6 +39,7 @@ import android.bluetooth.BluetoothLeBroadcastReceiveState;
import android.bluetooth.BluetoothStatusCodes;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Looper;
import android.platform.test.flag.junit.SetFlagsRule;
@@ -113,6 +114,7 @@ public class AudioSharingDialogHandlerTest {
@Mock private CachedBluetoothDeviceManager mCacheManager;
@Mock private LocalBluetoothLeBroadcast mBroadcast;
@Mock private LocalBluetoothLeBroadcastAssistant mAssistant;
@Mock private AudioManager mAudioManager;
@Mock private CachedBluetoothDevice mCachedDevice1;
@Mock private CachedBluetoothDevice mCachedDevice2;
@Mock private CachedBluetoothDevice mCachedDevice3;
@@ -152,6 +154,8 @@ public class AudioSharingDialogHandlerTest {
when(mLocalBtManager.getProfileManager()).thenReturn(mLocalBtProfileManager);
when(mLocalBtProfileManager.getLeAudioBroadcastProfile()).thenReturn(mBroadcast);
when(mLocalBtProfileManager.getLeAudioBroadcastAssistantProfile()).thenReturn(mAssistant);
when(mContext.getSystemService(AudioManager.class)).thenReturn(mAudioManager);
when(mAudioManager.getMode()).thenReturn(AudioManager.MODE_NORMAL);
List<Long> bisSyncState = new ArrayList<>();
bisSyncState.add(1L);
when(mState.getBisSyncState()).thenReturn(bisSyncState);
@@ -187,6 +191,18 @@ public class AudioSharingDialogHandlerTest {
ShadowBluetoothUtils.reset();
}
@Test
public void handleUserTriggeredDeviceConnected_inCall_setActive() {
when(mAudioManager.getMode()).thenReturn(AudioManager.MODE_IN_CALL);
setUpBroadcast(true);
ImmutableList<BluetoothDevice> deviceList = ImmutableList.of(mDevice1);
when(mAssistant.getAllConnectedDevices()).thenReturn(deviceList);
when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of());
mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */ true);
shadowOf(Looper.getMainLooper()).idle();
verify(mCachedDevice1).setActive();
}
@Test
public void handleUserTriggeredNonLeaDeviceConnected_noSharing_setActive() {
setUpBroadcast(false);
@@ -403,6 +419,18 @@ public class AudioSharingDialogHandlerTest {
verify(mAssistant).addSource(mDevice1, mMetadata, /* isGroupOp= */ false);
}
@Test
public void handleDeviceConnected_inCall_doNothing() {
when(mAudioManager.getMode()).thenReturn(AudioManager.MODE_IN_CALL);
setUpBroadcast(true);
when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of());
mHandler.handleDeviceConnected(mCachedDevice2, /* userTriggered= */ false);
shadowOf(Looper.getMainLooper()).idle();
verify(mCachedDevice2, never()).setActive();
List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
assertThat(childFragments).isEmpty();
}
@Test
public void handleNonLeaDeviceConnected_noSharing_doNothing() {
setUpBroadcast(false);