Loading src/com/android/settings/connecteddevice/audiosharing/AudioSharingDialogHandler.java +113 −109 Original line number Diff line number Diff line Loading @@ -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) { Loading @@ -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) { Loading Loading @@ -249,16 +254,13 @@ public class AudioSharingDialogHandler { userTriggered, deviceItemsInSharingSession.size(), /* candidateDeviceCount= */ 0); postOnMainThread( () -> { closeOpeningDialogsOtherThan(AudioSharingStopDialogFragment.tag()); AudioSharingStopDialogFragment.show( return AudioSharingStopDialogFragment.show( mHostFragment, deviceItemsInSharingSession, cachedDevice, listener, eventData); }); } else { if (userTriggered) { cachedDevice.setActive(); Loading @@ -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) { Loading @@ -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 Loading @@ -324,18 +324,15 @@ public class AudioSharingDialogHandler { userTriggered, deviceItemsInSharingSession.size(), /* candidateDeviceCount= */ 1); postOnMainThread( () -> { closeOpeningDialogsOtherThan( AudioSharingDisconnectDialogFragment.tag()); AudioSharingDisconnectDialogFragment.show( Log.d(TAG, "Show disconnect dialog, device = " + deviceAddress); return AudioSharingDisconnectDialogFragment.show( mHostFragment, deviceItemsInSharingSession, cachedDevice, listener, eventData); Log.d(TAG, "Show disconnect dialog, device = " + deviceAddress); }); } else { // Show audio sharing join dialog when the first or second eligible (LE audio) // remote device connected during a sharing session. Loading @@ -356,17 +353,14 @@ public class AudioSharingDialogHandler { userTriggered, deviceItemsInSharingSession.size(), /* candidateDeviceCount= */ 1); postOnMainThread( () -> { closeOpeningDialogsOtherThan(AudioSharingJoinDialogFragment.tag()); AudioSharingJoinDialogFragment.show( Log.d(TAG, "Show join dialog, device = " + deviceAddress); return AudioSharingJoinDialogFragment.show( mHostFragment, deviceItemsInSharingSession, cachedDevice, listener, eventData); Log.d(TAG, "Show join dialog, device = " + deviceAddress); }); } } else { // Build a list of AudioSharingDeviceItem for connected devices other than cachedDevice. Loading Loading @@ -419,29 +413,33 @@ 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); }); 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; AudioSharingUtils.postOnMainThread( mContext, () -> { List<Fragment> fragments; try { fragments = mHostFragment.getChildFragmentManager().getFragments(); } catch (IllegalStateException e) { Log.d(TAG, "Fail to closeOpeningDialogsOtherThan " + tag + ": " + e.getMessage()); Log.d(TAG, "Fail to closeOpeningDialogsOtherThan " + tag + ": " + e.getMessage()); return; } for (Fragment fragment : fragments) { Loading @@ -449,16 +447,20 @@ public class AudioSharingDialogHandler { && fragment.getTag() != null && !fragment.getTag().equals(tag)) { Log.d(TAG, "Remove staled opening dialog " + fragment.getTag()); ((DialogFragment) fragment).dismiss(); ((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); AudioSharingUtils.postOnMainThread( mContext, () -> { List<Fragment> fragments; try { fragments = mHostFragment.getChildFragmentManager().getFragments(); Loading @@ -472,16 +474,20 @@ public class AudioSharingDialogHandler { && groupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID && BluetoothUtils.getGroupId(device) == groupId) { Log.d(TAG, "Remove staled opening dialog for group " + groupId); ((DialogFragment) fragment).dismiss(); ((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(); AudioSharingUtils.postOnMainThread( mContext, () -> { List<Fragment> fragments; try { fragments = mHostFragment.getChildFragmentManager().getFragments(); Loading @@ -491,15 +497,17 @@ public class AudioSharingDialogHandler { } for (Fragment fragment : fragments) { CachedBluetoothDevice device = getCachedBluetoothDeviceFromDialog(fragment); if (device != null && address != null && address.equals(device.getAddress())) { if (device != null && address != null && address.equals( device.getAddress())) { Log.d( TAG, "Remove staled opening dialog for device " + cachedDevice.getDevice().getAnonymizedAddress()); ((DialogFragment) fragment).dismiss(); ((DialogFragment) fragment).dismissAllowingStateLoss(); logDialogDismissEvent(fragment); } } }); } @Nullable Loading Loading @@ -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); } Loading src/com/android/settings/connecteddevice/audiosharing/AudioSharingJoinHandlerController.java +26 −3 Original line number Diff line number Diff line Loading @@ -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) { Loading @@ -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 Loading tests/robotests/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDialogHandlerTest.java +64 −22 File changed.Preview size limit exceeded, changes collapsed. Show changes tests/robotests/src/com/android/settings/connecteddevice/audiosharing/AudioSharingJoinHandlerControllerTest.java +37 −0 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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 Loading Loading @@ -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(); } } Loading
src/com/android/settings/connecteddevice/audiosharing/AudioSharingDialogHandler.java +113 −109 Original line number Diff line number Diff line Loading @@ -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) { Loading @@ -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) { Loading Loading @@ -249,16 +254,13 @@ public class AudioSharingDialogHandler { userTriggered, deviceItemsInSharingSession.size(), /* candidateDeviceCount= */ 0); postOnMainThread( () -> { closeOpeningDialogsOtherThan(AudioSharingStopDialogFragment.tag()); AudioSharingStopDialogFragment.show( return AudioSharingStopDialogFragment.show( mHostFragment, deviceItemsInSharingSession, cachedDevice, listener, eventData); }); } else { if (userTriggered) { cachedDevice.setActive(); Loading @@ -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) { Loading @@ -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 Loading @@ -324,18 +324,15 @@ public class AudioSharingDialogHandler { userTriggered, deviceItemsInSharingSession.size(), /* candidateDeviceCount= */ 1); postOnMainThread( () -> { closeOpeningDialogsOtherThan( AudioSharingDisconnectDialogFragment.tag()); AudioSharingDisconnectDialogFragment.show( Log.d(TAG, "Show disconnect dialog, device = " + deviceAddress); return AudioSharingDisconnectDialogFragment.show( mHostFragment, deviceItemsInSharingSession, cachedDevice, listener, eventData); Log.d(TAG, "Show disconnect dialog, device = " + deviceAddress); }); } else { // Show audio sharing join dialog when the first or second eligible (LE audio) // remote device connected during a sharing session. Loading @@ -356,17 +353,14 @@ public class AudioSharingDialogHandler { userTriggered, deviceItemsInSharingSession.size(), /* candidateDeviceCount= */ 1); postOnMainThread( () -> { closeOpeningDialogsOtherThan(AudioSharingJoinDialogFragment.tag()); AudioSharingJoinDialogFragment.show( Log.d(TAG, "Show join dialog, device = " + deviceAddress); return AudioSharingJoinDialogFragment.show( mHostFragment, deviceItemsInSharingSession, cachedDevice, listener, eventData); Log.d(TAG, "Show join dialog, device = " + deviceAddress); }); } } else { // Build a list of AudioSharingDeviceItem for connected devices other than cachedDevice. Loading Loading @@ -419,29 +413,33 @@ 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); }); 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; AudioSharingUtils.postOnMainThread( mContext, () -> { List<Fragment> fragments; try { fragments = mHostFragment.getChildFragmentManager().getFragments(); } catch (IllegalStateException e) { Log.d(TAG, "Fail to closeOpeningDialogsOtherThan " + tag + ": " + e.getMessage()); Log.d(TAG, "Fail to closeOpeningDialogsOtherThan " + tag + ": " + e.getMessage()); return; } for (Fragment fragment : fragments) { Loading @@ -449,16 +447,20 @@ public class AudioSharingDialogHandler { && fragment.getTag() != null && !fragment.getTag().equals(tag)) { Log.d(TAG, "Remove staled opening dialog " + fragment.getTag()); ((DialogFragment) fragment).dismiss(); ((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); AudioSharingUtils.postOnMainThread( mContext, () -> { List<Fragment> fragments; try { fragments = mHostFragment.getChildFragmentManager().getFragments(); Loading @@ -472,16 +474,20 @@ public class AudioSharingDialogHandler { && groupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID && BluetoothUtils.getGroupId(device) == groupId) { Log.d(TAG, "Remove staled opening dialog for group " + groupId); ((DialogFragment) fragment).dismiss(); ((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(); AudioSharingUtils.postOnMainThread( mContext, () -> { List<Fragment> fragments; try { fragments = mHostFragment.getChildFragmentManager().getFragments(); Loading @@ -491,15 +497,17 @@ public class AudioSharingDialogHandler { } for (Fragment fragment : fragments) { CachedBluetoothDevice device = getCachedBluetoothDeviceFromDialog(fragment); if (device != null && address != null && address.equals(device.getAddress())) { if (device != null && address != null && address.equals( device.getAddress())) { Log.d( TAG, "Remove staled opening dialog for device " + cachedDevice.getDevice().getAnonymizedAddress()); ((DialogFragment) fragment).dismiss(); ((DialogFragment) fragment).dismissAllowingStateLoss(); logDialogDismissEvent(fragment); } } }); } @Nullable Loading Loading @@ -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); } Loading
src/com/android/settings/connecteddevice/audiosharing/AudioSharingJoinHandlerController.java +26 −3 Original line number Diff line number Diff line Loading @@ -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) { Loading @@ -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 Loading
tests/robotests/src/com/android/settings/connecteddevice/audiosharing/AudioSharingDialogHandlerTest.java +64 −22 File changed.Preview size limit exceeded, changes collapsed. Show changes
tests/robotests/src/com/android/settings/connecteddevice/audiosharing/AudioSharingJoinHandlerControllerTest.java +37 −0 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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 Loading Loading @@ -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(); } }