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