Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4cbf81ea authored by Yiyi Shen's avatar Yiyi Shen Committed by Android (Google) Code Review
Browse files

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

parents 351fca09 80fab0b8
Loading
Loading
Loading
Loading
+113 −109
Original line number Diff line number Diff line
@@ -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(
            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(
                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.
@@ -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.
@@ -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) {
@@ -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();
@@ -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();
@@ -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
@@ -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);
    }
+26 −3
Original line number Diff line number Diff line
@@ -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
+64 −22

File changed.

Preview size limit exceeded, changes collapsed.

+37 −0
Original line number Diff line number Diff line
@@ -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();
    }
}