Merge "[Audiosharing] Finish handler activity if no dialog to show" into main

This commit is contained in:
Yiyi Shen
2025-03-03 01:27:26 -08:00
committed by Android (Google) Code Review
4 changed files with 240 additions and 134 deletions

View File

@@ -179,8 +179,13 @@ public class AudioSharingDialogHandler {
}
}
/** Handle dialog pop-up logic when device is connected. */
public void handleDeviceConnected(
/**
* Handle dialog pop-up logic when device is connected.
* @param cachedDevice The target {@link CachedBluetoothDevice} to handle for
* @param userTriggered If the device is connected by user
* @return If a dialog is popped up
*/
public boolean handleDeviceConnected(
@NonNull CachedBluetoothDevice cachedDevice, boolean userTriggered) {
String anonymizedAddress = cachedDevice.getDevice().getAnonymizedAddress();
if (mAudioManager != null) {
@@ -197,23 +202,23 @@ public class AudioSharingDialogHandler {
cachedDevice.setActive();
AudioSharingUtils.setUserPreferredPrimary(mContext, cachedDevice);
}
return;
return false;
}
}
boolean isBroadcasting = isBroadcasting();
boolean isLeAudioSupported = AudioSharingUtils.isLeAudioSupported(cachedDevice);
boolean isLeAudioSupported = BluetoothUtils.isLeAudioSupported(cachedDevice);
if (!isLeAudioSupported) {
Log.d(TAG, "Handle non LE audio device connected, device = " + anonymizedAddress);
// Handle connected ineligible (non LE audio) remote device
handleNonLeAudioDeviceConnected(cachedDevice, isBroadcasting, userTriggered);
return handleNonLeAudioDeviceConnected(cachedDevice, isBroadcasting, userTriggered);
} else {
Log.d(TAG, "Handle LE audio device connected, device = " + anonymizedAddress);
// Handle connected eligible (LE audio) remote device
handleLeAudioDeviceConnected(cachedDevice, isBroadcasting, userTriggered);
return handleLeAudioDeviceConnected(cachedDevice, isBroadcasting, userTriggered);
}
}
private void handleNonLeAudioDeviceConnected(
private boolean handleNonLeAudioDeviceConnected(
@NonNull CachedBluetoothDevice cachedDevice,
boolean isBroadcasting,
boolean userTriggered) {
@@ -249,16 +254,13 @@ public class AudioSharingDialogHandler {
userTriggered,
deviceItemsInSharingSession.size(),
/* candidateDeviceCount= */ 0);
postOnMainThread(
() -> {
closeOpeningDialogsOtherThan(AudioSharingStopDialogFragment.tag());
AudioSharingStopDialogFragment.show(
mHostFragment,
deviceItemsInSharingSession,
cachedDevice,
listener,
eventData);
});
closeOpeningDialogsOtherThan(AudioSharingStopDialogFragment.tag());
return AudioSharingStopDialogFragment.show(
mHostFragment,
deviceItemsInSharingSession,
cachedDevice,
listener,
eventData);
} else {
if (userTriggered) {
cachedDevice.setActive();
@@ -268,10 +270,11 @@ public class AudioSharingDialogHandler {
TAG,
"Ignore onProfileConnectionStateChanged for non LE audio without"
+ " sharing session");
return false;
}
}
private void handleLeAudioDeviceConnected(
private boolean handleLeAudioDeviceConnected(
@NonNull CachedBluetoothDevice cachedDevice,
boolean isBroadcasting,
boolean userTriggered) {
@@ -289,17 +292,14 @@ public class AudioSharingDialogHandler {
device ->
BluetoothUtils.hasConnectedBroadcastSourceForBtDevice(
device, mLocalBtManager))) {
Log.d(
TAG,
"Automatically add another device within the same group to the sharing: "
+ deviceAddress);
Log.d(TAG, "Auto add sink with the same group to the sharing: " + deviceAddress);
if (mAssistant != null && mBroadcast != null) {
mAssistant.addSource(
btDevice,
mBroadcast.getLatestBluetoothLeBroadcastMetadata(),
/* isGroupOp= */ false);
}
return;
return false;
}
// Show audio sharing switch or join dialog according to device count in the sharing
@@ -324,18 +324,15 @@ public class AudioSharingDialogHandler {
userTriggered,
deviceItemsInSharingSession.size(),
/* candidateDeviceCount= */ 1);
postOnMainThread(
() -> {
closeOpeningDialogsOtherThan(
AudioSharingDisconnectDialogFragment.tag());
AudioSharingDisconnectDialogFragment.show(
mHostFragment,
deviceItemsInSharingSession,
cachedDevice,
listener,
eventData);
Log.d(TAG, "Show disconnect dialog, device = " + deviceAddress);
});
closeOpeningDialogsOtherThan(
AudioSharingDisconnectDialogFragment.tag());
Log.d(TAG, "Show disconnect dialog, device = " + deviceAddress);
return AudioSharingDisconnectDialogFragment.show(
mHostFragment,
deviceItemsInSharingSession,
cachedDevice,
listener,
eventData);
} else {
// Show audio sharing join dialog when the first or second eligible (LE audio)
// remote device connected during a sharing session.
@@ -356,17 +353,14 @@ public class AudioSharingDialogHandler {
userTriggered,
deviceItemsInSharingSession.size(),
/* candidateDeviceCount= */ 1);
postOnMainThread(
() -> {
closeOpeningDialogsOtherThan(AudioSharingJoinDialogFragment.tag());
AudioSharingJoinDialogFragment.show(
mHostFragment,
deviceItemsInSharingSession,
cachedDevice,
listener,
eventData);
Log.d(TAG, "Show join dialog, device = " + deviceAddress);
});
closeOpeningDialogsOtherThan(AudioSharingJoinDialogFragment.tag());
Log.d(TAG, "Show join dialog, device = " + deviceAddress);
return AudioSharingJoinDialogFragment.show(
mHostFragment,
deviceItemsInSharingSession,
cachedDevice,
listener,
eventData);
}
} else {
// Build a list of AudioSharingDeviceItem for connected devices other than cachedDevice.
@@ -419,87 +413,101 @@ public class AudioSharingDialogHandler {
userTriggered,
/* deviceCountInSharing= */ 0,
/* candidateDeviceCount= */ 2);
postOnMainThread(
() -> {
closeOpeningDialogsOtherThan(AudioSharingJoinDialogFragment.tag());
AudioSharingJoinDialogFragment.show(
mHostFragment, deviceItems, cachedDevice, listener, eventData);
Log.d(TAG, "Show start dialog, device = " + deviceAddress);
});
closeOpeningDialogsOtherThan(AudioSharingJoinDialogFragment.tag());
Log.d(TAG, "Show start dialog, device = " + deviceAddress);
return AudioSharingJoinDialogFragment.show(
mHostFragment, deviceItems, cachedDevice, listener, eventData);
} else if (userTriggered) {
cachedDevice.setActive();
Log.d(TAG, "Set active device = " + deviceAddress);
return false;
} else {
Log.d(TAG, "Fail to handle LE audio device connected, device = " + deviceAddress);
return false;
}
}
}
private void closeOpeningDialogsOtherThan(String tag) {
/** Close opening dialogs other than the given tag */
public void closeOpeningDialogsOtherThan(String tag) {
if (mHostFragment == null) return;
List<Fragment> fragments;
try {
fragments = mHostFragment.getChildFragmentManager().getFragments();
} catch (IllegalStateException e) {
Log.d(TAG, "Fail to closeOpeningDialogsOtherThan " + tag + ": " + e.getMessage());
return;
}
for (Fragment fragment : fragments) {
if (fragment instanceof DialogFragment
&& fragment.getTag() != null
&& !fragment.getTag().equals(tag)) {
Log.d(TAG, "Remove staled opening dialog " + fragment.getTag());
((DialogFragment) fragment).dismiss();
logDialogDismissEvent(fragment);
}
}
AudioSharingUtils.postOnMainThread(
mContext,
() -> {
List<Fragment> fragments;
try {
fragments = mHostFragment.getChildFragmentManager().getFragments();
} catch (IllegalStateException e) {
Log.d(TAG, "Fail to closeOpeningDialogsOtherThan " + tag + ": "
+ e.getMessage());
return;
}
for (Fragment fragment : fragments) {
if (fragment instanceof DialogFragment
&& fragment.getTag() != null
&& !fragment.getTag().equals(tag)) {
Log.d(TAG, "Remove staled opening dialog " + fragment.getTag());
((DialogFragment) fragment).dismissAllowingStateLoss();
logDialogDismissEvent(fragment);
}
}
});
}
/** Close opening dialogs for le audio device */
public void closeOpeningDialogsForLeaDevice(@NonNull CachedBluetoothDevice cachedDevice) {
if (mHostFragment == null) return;
int groupId = BluetoothUtils.getGroupId(cachedDevice);
List<Fragment> fragments;
try {
fragments = mHostFragment.getChildFragmentManager().getFragments();
} catch (IllegalStateException e) {
Log.d(TAG, "Fail to closeOpeningDialogsForLeaDevice: " + e.getMessage());
return;
}
for (Fragment fragment : fragments) {
CachedBluetoothDevice device = getCachedBluetoothDeviceFromDialog(fragment);
if (device != null
&& groupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID
&& BluetoothUtils.getGroupId(device) == groupId) {
Log.d(TAG, "Remove staled opening dialog for group " + groupId);
((DialogFragment) fragment).dismiss();
logDialogDismissEvent(fragment);
}
}
AudioSharingUtils.postOnMainThread(
mContext,
() -> {
List<Fragment> fragments;
try {
fragments = mHostFragment.getChildFragmentManager().getFragments();
} catch (IllegalStateException e) {
Log.d(TAG, "Fail to closeOpeningDialogsForLeaDevice: " + e.getMessage());
return;
}
for (Fragment fragment : fragments) {
CachedBluetoothDevice device = getCachedBluetoothDeviceFromDialog(fragment);
if (device != null
&& groupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID
&& BluetoothUtils.getGroupId(device) == groupId) {
Log.d(TAG, "Remove staled opening dialog for group " + groupId);
((DialogFragment) fragment).dismissAllowingStateLoss();
logDialogDismissEvent(fragment);
}
}
});
}
/** Close opening dialogs for non le audio device */
public void closeOpeningDialogsForNonLeaDevice(@NonNull CachedBluetoothDevice cachedDevice) {
if (mHostFragment == null) return;
String address = cachedDevice.getAddress();
List<Fragment> fragments;
try {
fragments = mHostFragment.getChildFragmentManager().getFragments();
} catch (IllegalStateException e) {
Log.d(TAG, "Fail to closeOpeningDialogsForNonLeaDevice: " + e.getMessage());
return;
}
for (Fragment fragment : fragments) {
CachedBluetoothDevice device = getCachedBluetoothDeviceFromDialog(fragment);
if (device != null && address != null && address.equals(device.getAddress())) {
Log.d(
TAG,
"Remove staled opening dialog for device "
+ cachedDevice.getDevice().getAnonymizedAddress());
((DialogFragment) fragment).dismiss();
logDialogDismissEvent(fragment);
}
}
AudioSharingUtils.postOnMainThread(
mContext,
() -> {
List<Fragment> fragments;
try {
fragments = mHostFragment.getChildFragmentManager().getFragments();
} catch (IllegalStateException e) {
Log.d(TAG, "Fail to closeOpeningDialogsForNonLeaDevice: " + e.getMessage());
return;
}
for (Fragment fragment : fragments) {
CachedBluetoothDevice device = getCachedBluetoothDeviceFromDialog(fragment);
if (device != null && address != null && address.equals(
device.getAddress())) {
Log.d(
TAG,
"Remove staled opening dialog for device "
+ cachedDevice.getDevice().getAnonymizedAddress());
((DialogFragment) fragment).dismissAllowingStateLoss();
logDialogDismissEvent(fragment);
}
}
});
}
@Nullable
@@ -554,10 +562,6 @@ public class AudioSharingDialogHandler {
/* isGroupOp= */ false));
}
private void postOnMainThread(@NonNull Runnable runnable) {
mContext.getMainExecutor().execute(runnable);
}
private boolean isBroadcasting() {
return mBroadcast != null && mBroadcast.isEnabled(null);
}

View File

@@ -239,13 +239,22 @@ public class AudioSharingJoinHandlerController extends BasePreferenceController
boolean isLeAudioSupported = BluetoothUtils.isLeAudioSupported(cachedDevice);
if (isLeAudioSupported
&& bluetoothProfile == BluetoothProfile.LE_AUDIO_BROADCAST_ASSISTANT) {
Log.d(TAG, "closeOpeningDialogsForLeaDevice");
mDialogHandler.closeOpeningDialogsForLeaDevice(cachedDevice);
} else if (!isLeAudioSupported && !cachedDevice.isConnected()) {
Log.d(TAG, "closeOpeningDialogsForNonLeaDevice");
mDialogHandler.closeOpeningDialogsForNonLeaDevice(cachedDevice);
}
}
}
@Override
public void onBluetoothStateChanged(@AdapterState int bluetoothState) {
if (bluetoothState == BluetoothAdapter.STATE_OFF) {
finishActivity();
}
}
/** Handle just connected device via intent. */
@WorkerThread
public void handleDeviceConnectedFromIntent(@NonNull Intent intent) {
@@ -256,15 +265,29 @@ public class AudioSharingJoinHandlerController extends BasePreferenceController
? null
: mDeviceManager.findDevice(device);
if (cachedDevice == null) {
Log.d(TAG, "Skip handleDeviceConnectedFromIntent, device is null");
Log.d(TAG, "Skip handleDeviceConnectedFromIntent and finish activity, device is null");
finishActivity();
return;
}
if (mDialogHandler == null) {
Log.d(TAG, "Skip handleDeviceConnectedFromIntent, handler is null");
Log.d(TAG, "Skip handleDeviceConnectedFromIntent and finish activity, handler is null");
finishActivity();
return;
}
Log.d(TAG, "handleDeviceConnectedFromIntent, device = " + device.getAnonymizedAddress());
mDialogHandler.handleDeviceConnected(cachedDevice, /* userTriggered= */ false);
if (!mDialogHandler.handleDeviceConnected(cachedDevice, /* userTriggered= */ false)) {
Log.d(TAG, "handleDeviceConnectedFromIntent, finish activity");
finishActivity();
}
}
private void finishActivity() {
AudioSharingUtils.postOnMainThread(mContext, () -> {
if (mFragment != null && mFragment.getActivity() != null) {
Log.d(TAG, "Finish activity");
mFragment.getActivity().finish();
}
});
}
@VisibleForTesting

View File

@@ -45,6 +45,8 @@ import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Looper;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
import android.util.Pair;
@@ -196,9 +198,9 @@ public class AudioSharingDialogHandlerTest {
}
@Test
@EnableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API)
@DisableFlags(Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX)
public void handleUserTriggeredDeviceConnected_inCall_setActive() {
mSetFlagsRule.disableFlags(Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX);
mSetFlagsRule.enableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API);
Settings.Secure.putInt(mContext.getContentResolver(),
BLUETOOTH_LE_BROADCAST_PRIMARY_DEVICE_GROUP_ID,
BluetoothCsipSetCoordinator.GROUP_ID_INVALID);
@@ -207,19 +209,21 @@ public class AudioSharingDialogHandlerTest {
ImmutableList<BluetoothDevice> deviceList = ImmutableList.of(mDevice1);
when(mAssistant.getAllConnectedDevices()).thenReturn(deviceList);
when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of());
mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */ true);
boolean showDialog = mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */
true);
shadowOf(Looper.getMainLooper()).idle();
verify(mCachedDevice1).setActive();
assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
BLUETOOTH_LE_BROADCAST_PRIMARY_DEVICE_GROUP_ID,
BluetoothCsipSetCoordinator.GROUP_ID_INVALID)).isEqualTo(
BluetoothCsipSetCoordinator.GROUP_ID_INVALID);
assertThat(showDialog).isFalse();
}
@Test
@EnableFlags({Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX,
Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API})
public void handleUserTriggeredDeviceConnected_inCall_enableHysteresisFix_setAndSaveActive() {
mSetFlagsRule.enableFlags(Flags.FLAG_AUDIO_SHARING_HYSTERESIS_MODE_FIX);
mSetFlagsRule.enableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API);
Settings.Secure.putInt(mContext.getContentResolver(),
BLUETOOTH_LE_BROADCAST_PRIMARY_DEVICE_GROUP_ID,
BluetoothCsipSetCoordinator.GROUP_ID_INVALID);
@@ -228,12 +232,14 @@ public class AudioSharingDialogHandlerTest {
ImmutableList<BluetoothDevice> deviceList = ImmutableList.of(mDevice1);
when(mAssistant.getAllConnectedDevices()).thenReturn(deviceList);
when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of());
mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */ true);
boolean showDialog = mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */
true);
shadowOf(Looper.getMainLooper()).idle();
verify(mCachedDevice1).setActive();
assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
BLUETOOTH_LE_BROADCAST_PRIMARY_DEVICE_GROUP_ID,
BluetoothCsipSetCoordinator.GROUP_ID_INVALID)).isEqualTo(1);
assertThat(showDialog).isFalse();
}
@Test
@@ -242,9 +248,11 @@ public class AudioSharingDialogHandlerTest {
ImmutableList<BluetoothDevice> deviceList = ImmutableList.of(mDevice2);
when(mAssistant.getAllConnectedDevices()).thenReturn(deviceList);
when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of());
mHandler.handleDeviceConnected(mCachedDevice2, /* userTriggered= */ true);
boolean showDialog = mHandler.handleDeviceConnected(mCachedDevice2, /* userTriggered= */
true);
shadowOf(Looper.getMainLooper()).idle();
verify(mCachedDevice2).setActive();
assertThat(showDialog).isFalse();
}
@Test
@@ -253,7 +261,8 @@ public class AudioSharingDialogHandlerTest {
ImmutableList<BluetoothDevice> deviceList = ImmutableList.of(mDevice2);
when(mAssistant.getAllConnectedDevices()).thenReturn(deviceList);
when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(mState));
mHandler.handleDeviceConnected(mCachedDevice2, /* userTriggered= */ true);
boolean showDialog = mHandler.handleDeviceConnected(mCachedDevice2, /* userTriggered= */
true);
shadowOf(Looper.getMainLooper()).idle();
List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
assertThat(childFragments)
@@ -282,6 +291,7 @@ public class AudioSharingDialogHandlerTest {
AudioSharingUtils.MetricKey.METRIC_KEY_CANDIDATE_DEVICE_COUNT
.ordinal(),
0));
assertThat(showDialog).isTrue();
}
@Test
@@ -290,9 +300,11 @@ public class AudioSharingDialogHandlerTest {
ImmutableList<BluetoothDevice> deviceList = ImmutableList.of(mDevice1);
when(mAssistant.getAllConnectedDevices()).thenReturn(deviceList);
when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of());
mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */ true);
boolean showDialog = mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */
true);
shadowOf(Looper.getMainLooper()).idle();
verify(mCachedDevice1).setActive();
assertThat(showDialog).isFalse();
}
@Test
@@ -303,11 +315,13 @@ public class AudioSharingDialogHandlerTest {
ImmutableList<BluetoothDevice> deviceList = ImmutableList.of(mDevice1, mDevice3);
when(mAssistant.getAllConnectedDevices()).thenReturn(deviceList);
when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of());
mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */ true);
boolean showDialog = mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */
true);
shadowOf(Looper.getMainLooper()).idle();
List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
assertThat(childFragments).isEmpty();
verify(mCachedDevice1).setActive();
assertThat(showDialog).isFalse();
}
@Test
@@ -316,7 +330,8 @@ public class AudioSharingDialogHandlerTest {
ImmutableList<BluetoothDevice> deviceList = ImmutableList.of(mDevice1, mDevice3);
when(mAssistant.getAllConnectedDevices()).thenReturn(deviceList);
when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of());
mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */ true);
boolean showDialog = mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */
true);
shadowOf(Looper.getMainLooper()).idle();
List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
assertThat(childFragments)
@@ -345,6 +360,8 @@ public class AudioSharingDialogHandlerTest {
AudioSharingUtils.MetricKey.METRIC_KEY_CANDIDATE_DEVICE_COUNT
.ordinal(),
2));
assertThat(showDialog).isTrue();
AudioSharingJoinDialogFragment.DialogEventListener listener = fragment.getListener();
assertThat(listener).isNotNull();
listener.onShareClick();
@@ -370,7 +387,8 @@ public class AudioSharingDialogHandlerTest {
when(mAssistant.getAllConnectedDevices()).thenReturn(deviceList);
when(mAssistant.getAllSources(mDevice1)).thenReturn(ImmutableList.of());
when(mAssistant.getAllSources(mDevice3)).thenReturn(ImmutableList.of(mState));
mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */ true);
boolean showDialog = mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */
true);
shadowOf(Looper.getMainLooper()).idle();
List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
assertThat(childFragments)
@@ -399,6 +417,8 @@ public class AudioSharingDialogHandlerTest {
AudioSharingUtils.MetricKey.METRIC_KEY_CANDIDATE_DEVICE_COUNT
.ordinal(),
1));
assertThat(showDialog).isTrue();
AudioSharingJoinDialogFragment.DialogEventListener listener = fragment.getListener();
assertThat(listener).isNotNull();
listener.onCancelClick();
@@ -416,7 +436,8 @@ public class AudioSharingDialogHandlerTest {
when(mAssistant.getAllSources(mDevice1)).thenReturn(ImmutableList.of());
when(mAssistant.getAllSources(mDevice3)).thenReturn(ImmutableList.of(mState));
when(mAssistant.getAllSources(mDevice4)).thenReturn(ImmutableList.of(mState));
mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */ true);
boolean showDialog = mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */
true);
shadowOf(Looper.getMainLooper()).idle();
List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
assertThat(childFragments)
@@ -445,6 +466,8 @@ public class AudioSharingDialogHandlerTest {
AudioSharingUtils.MetricKey.METRIC_KEY_CANDIDATE_DEVICE_COUNT
.ordinal(),
1));
assertThat(showDialog).isTrue();
AudioSharingDisconnectDialogFragment.DialogEventListener listener = fragment.getListener();
assertThat(listener).isNotNull();
listener.onItemClick(AudioSharingUtils.buildAudioSharingDeviceItem(mCachedDevice3));
@@ -453,16 +476,18 @@ public class AudioSharingDialogHandlerTest {
}
@Test
@EnableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API)
public void handleDeviceConnected_inCall_doNothing() {
mSetFlagsRule.enableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API);
when(mAudioManager.getMode()).thenReturn(AudioManager.MODE_IN_CALL);
setUpBroadcast(true);
when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of());
mHandler.handleDeviceConnected(mCachedDevice2, /* userTriggered= */ false);
boolean showDialog = mHandler.handleDeviceConnected(mCachedDevice2, /* userTriggered= */
false);
shadowOf(Looper.getMainLooper()).idle();
verify(mCachedDevice2, never()).setActive();
List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
assertThat(childFragments).isEmpty();
assertThat(showDialog).isFalse();
}
@Test
@@ -471,9 +496,11 @@ public class AudioSharingDialogHandlerTest {
ImmutableList<BluetoothDevice> deviceList = ImmutableList.of(mDevice2);
when(mAssistant.getAllConnectedDevices()).thenReturn(deviceList);
when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of());
mHandler.handleDeviceConnected(mCachedDevice2, /* userTriggered= */ false);
boolean showDialog = mHandler.handleDeviceConnected(mCachedDevice2, /* userTriggered= */
false);
shadowOf(Looper.getMainLooper()).idle();
verify(mCachedDevice2, never()).setActive();
assertThat(showDialog).isFalse();
}
@Test
@@ -482,7 +509,8 @@ public class AudioSharingDialogHandlerTest {
ImmutableList<BluetoothDevice> deviceList = ImmutableList.of(mDevice1);
when(mAssistant.getAllConnectedDevices()).thenReturn(deviceList);
when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(mState));
mHandler.handleDeviceConnected(mCachedDevice2, /* userTriggered= */ false);
boolean showDialog = mHandler.handleDeviceConnected(mCachedDevice2, /* userTriggered= */
false);
shadowOf(Looper.getMainLooper()).idle();
List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
assertThat(childFragments)
@@ -511,6 +539,7 @@ public class AudioSharingDialogHandlerTest {
AudioSharingUtils.MetricKey.METRIC_KEY_CANDIDATE_DEVICE_COUNT
.ordinal(),
0));
assertThat(showDialog).isTrue();
}
@Test
@@ -519,9 +548,11 @@ public class AudioSharingDialogHandlerTest {
ImmutableList<BluetoothDevice> deviceList = ImmutableList.of(mDevice1);
when(mAssistant.getAllConnectedDevices()).thenReturn(deviceList);
when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of());
mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */ false);
boolean showDialog = mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */
false);
shadowOf(Looper.getMainLooper()).idle();
verify(mCachedDevice1, never()).setActive();
assertThat(showDialog).isFalse();
}
@Test
@@ -532,11 +563,13 @@ public class AudioSharingDialogHandlerTest {
ImmutableList<BluetoothDevice> deviceList = ImmutableList.of(mDevice1, mDevice3);
when(mAssistant.getAllConnectedDevices()).thenReturn(deviceList);
when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of());
mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */ false);
boolean showDialog = mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */
false);
shadowOf(Looper.getMainLooper()).idle();
List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
assertThat(childFragments).isEmpty();
verify(mCachedDevice1, never()).setActive();
assertThat(showDialog).isFalse();
}
@Test
@@ -545,7 +578,8 @@ public class AudioSharingDialogHandlerTest {
ImmutableList<BluetoothDevice> deviceList = ImmutableList.of(mDevice1, mDevice3);
when(mAssistant.getAllConnectedDevices()).thenReturn(deviceList);
when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of());
mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */ false);
boolean showDialog = mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */
false);
shadowOf(Looper.getMainLooper()).idle();
List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
assertThat(childFragments)
@@ -574,6 +608,8 @@ public class AudioSharingDialogHandlerTest {
AudioSharingUtils.MetricKey.METRIC_KEY_CANDIDATE_DEVICE_COUNT
.ordinal(),
2));
assertThat(showDialog).isTrue();
AudioSharingJoinDialogFragment.DialogEventListener listener = fragment.getListener();
assertThat(listener).isNotNull();
listener.onShareClick();
@@ -599,7 +635,8 @@ public class AudioSharingDialogHandlerTest {
when(mAssistant.getAllConnectedDevices()).thenReturn(deviceList);
when(mAssistant.getAllSources(mDevice1)).thenReturn(ImmutableList.of());
when(mAssistant.getAllSources(mDevice3)).thenReturn(ImmutableList.of(mState));
mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */ false);
boolean showDialog = mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */
false);
shadowOf(Looper.getMainLooper()).idle();
List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
assertThat(childFragments)
@@ -628,6 +665,8 @@ public class AudioSharingDialogHandlerTest {
AudioSharingUtils.MetricKey.METRIC_KEY_CANDIDATE_DEVICE_COUNT
.ordinal(),
1));
assertThat(showDialog).isTrue();
AudioSharingJoinDialogFragment.DialogEventListener listener = fragment.getListener();
assertThat(listener).isNotNull();
listener.onCancelClick();
@@ -644,7 +683,8 @@ public class AudioSharingDialogHandlerTest {
when(mAssistant.getAllSources(mDevice1)).thenReturn(ImmutableList.of());
when(mAssistant.getAllSources(mDevice3)).thenReturn(ImmutableList.of(mState));
when(mAssistant.getAllSources(mDevice4)).thenReturn(ImmutableList.of(mState));
mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */ false);
boolean showDialog = mHandler.handleDeviceConnected(mCachedDevice1, /* userTriggered= */
false);
shadowOf(Looper.getMainLooper()).idle();
List<Fragment> childFragments = mParentFragment.getChildFragmentManager().getFragments();
assertThat(childFragments)
@@ -673,6 +713,8 @@ public class AudioSharingDialogHandlerTest {
AudioSharingUtils.MetricKey.METRIC_KEY_CANDIDATE_DEVICE_COUNT
.ordinal(),
1));
assertThat(showDialog).isTrue();
AudioSharingDisconnectDialogFragment.DialogEventListener listener = fragment.getListener();
assertThat(listener).isNotNull();
listener.onItemClick(AudioSharingUtils.buildAudioSharingDeviceItem(mCachedDevice3));

View File

@@ -273,6 +273,7 @@ public class AudioSharingJoinHandlerControllerTest {
verify(mDeviceManager, never()).findDevice(any(BluetoothDevice.class));
verify(mDialogHandler, never())
.handleDeviceConnected(any(CachedBluetoothDevice.class), anyBoolean());
verify(mActivity).finish();
}
@Test
@@ -285,10 +286,30 @@ public class AudioSharingJoinHandlerControllerTest {
Intent intent = new Intent();
intent.putExtra(EXTRA_BLUETOOTH_DEVICE, device);
doReturn(intent).when(mActivity).getIntent();
when(mDialogHandler.handleDeviceConnected(any(), anyBoolean())).thenReturn(true);
mController.displayPreference(mScreen);
shadowOf(Looper.getMainLooper()).idle();
verify(mDialogHandler).handleDeviceConnected(cachedDevice, /* userTriggered = */ false);
verify(mActivity, never()).finish();
}
@Test
@EnableFlags({Flags.FLAG_ENABLE_LE_AUDIO_SHARING,
Flags.FLAG_PROMOTE_AUDIO_SHARING_FOR_SECOND_AUTO_CONNECTED_LEA_DEVICE})
public void handleDeviceClickFromIntent_noDialogToShow_finish() {
CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class);
BluetoothDevice device = mock(BluetoothDevice.class);
when(mDeviceManager.findDevice(device)).thenReturn(cachedDevice);
Intent intent = new Intent();
intent.putExtra(EXTRA_BLUETOOTH_DEVICE, device);
doReturn(intent).when(mActivity).getIntent();
when(mDialogHandler.handleDeviceConnected(any(), anyBoolean())).thenReturn(false);
mController.displayPreference(mScreen);
shadowOf(Looper.getMainLooper()).idle();
verify(mDialogHandler).handleDeviceConnected(cachedDevice, /* userTriggered = */ false);
verify(mActivity).finish();
}
@Test
@@ -323,4 +344,20 @@ public class AudioSharingJoinHandlerControllerTest {
// Above callbacks won't dismiss stale dialogs
verifyNoInteractions(mDialogHandler);
}
@Test
public void onBluetoothStateChanged_stateOn_doNothing() {
mController.onBluetoothStateChanged(BluetoothAdapter.STATE_ON);
shadowOf(Looper.getMainLooper()).idle();
verify(mActivity, never()).finish();
}
@Test
public void onBluetoothStateChanged_stateOff_finish() {
mController.onBluetoothStateChanged(BluetoothAdapter.STATE_OFF);
shadowOf(Looper.getMainLooper()).idle();
verify(mActivity).finish();
}
}