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

Commit 7d2ddd33 authored by Alexandr Shabalin's avatar Alexandr Shabalin Committed by Android (Google) Code Review
Browse files

Merge changes I87d7aae8,I459c8155,I7f074194,Icbbe049e into main

* changes:
  Extract rendering of device item and device group in the list.
  Extract generation of ongoingSession and groupStatus device objects.
  Remove the special logic for the `device.hasSubtext()` condition.
  Consolidate the logic for "Connected" and "Selected" states.
parents f3e3b1c1 b6db16d4
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -240,6 +240,8 @@ public class MediaOutputAdapterTest extends SysuiTestCase {
    @DisableFlags(Flags.FLAG_ENABLE_OUTPUT_SWITCHER_DEVICE_GROUPING)
    @Test
    public void onBindViewHolder_bindConnectedRemoteDevice_verifyView() {
        when(mMediaSwitchingController.getSelectedMediaDevice())
                .thenReturn(ImmutableList.of(mMediaDevice1));
        when(mMediaSwitchingController.getSelectableMediaDevice())
                .thenReturn(ImmutableList.of(mMediaDevice2));
        when(mMediaSwitchingController.isCurrentConnectedDeviceRemote()).thenReturn(true);
@@ -854,6 +856,8 @@ public class MediaOutputAdapterTest extends SysuiTestCase {
                .thenReturn(ImmutableList.of(mMediaDevice2));
        when(mMediaSwitchingController.getDeselectableMediaDevice())
                .thenReturn(ImmutableList.of(mMediaDevice1));
        when(mMediaSwitchingController.getSelectedMediaDevice())
                .thenReturn(ImmutableList.of(mMediaDevice1));
        when(mMediaSwitchingController.isCurrentConnectedDeviceRemote()).thenReturn(true);
        mViewHolder = (MediaOutputAdapter.MediaDeviceViewHolder) mMediaOutputAdapter
                .onCreateViewHolder(new LinearLayout(mContext), 0);
+88 −74
Original line number Diff line number Diff line
@@ -185,6 +185,7 @@ public class MediaOutputAdapter extends MediaOutputBaseAdapter {
            }

            boolean isDeviceGroup = false;
            boolean hideGroupItem = false;
            GroupStatus groupStatus = null;
            OngoingSessionStatus ongoingSessionStatus = null;
            ConnectionState connectionState = ConnectionState.DISCONNECTED;
@@ -216,84 +217,63 @@ public class MediaOutputAdapter extends MediaOutputBaseAdapter {
                    clickListener = v -> cancelMuteAwaitConnection();
                } else if (device.getState() == MediaDeviceState.STATE_GROUPING) {
                    connectionState = ConnectionState.CONNECTING;
                } else if (mShouldGroupSelectedMediaItems
                        && mController.getSelectedMediaDevice().size() > 1
                        && isDeviceIncluded(mController.getSelectedMediaDevice(), device)) {
                    if (!mediaItem.isFirstDeviceInGroup()) {
                        mItemLayout.setVisibility(View.GONE);
                        return;
                    } else {
                } else if (mShouldGroupSelectedMediaItems && hasMultipleSelectedDevices()
                        && isSelected) {
                    if (mediaItem.isFirstDeviceInGroup()) {
                        isDeviceGroup = true;
                    }
                } else if (device.hasSubtext()) {
                    subtitle = device.getSubtextString();
                    boolean isActiveWithOngoingSession =
                            device.hasOngoingSession() && (currentlyConnected || isSelected);
                    if (isActiveWithOngoingSession) {
                        ongoingSessionStatus = new OngoingSessionStatus(
                                device.isHostForOngoingSession());
                        connectionState = ConnectionState.CONNECTED;
                    } else {
                        if (currentlyConnected) {
                            connectionState = ConnectionState.CONNECTED;
                        hideGroupItem = true;
                    }
                        clickListener = getClickListenerBasedOnSelectionBehavior(device);
                        deviceDisabled = clickListener == null;
                        deviceStatusIcon = getDeviceStatusIcon(device, device.hasOngoingSession());
                    }
                } else if (device.getState() == MediaDeviceState.STATE_CONNECTING_FAILED) {
                    deviceStatusIcon = mContext.getDrawable(R.drawable.media_output_status_failed);
                } else { // A connected or disconnected device.
                    subtitle = device.hasSubtext() ? device.getSubtextString() : null;
                    ongoingSessionStatus = getOngoingSessionStatus(device);
                    groupStatus = getGroupStatus(isSelected, isSelectable, isDeselectable);

                    if (device.getState() == MediaDeviceState.STATE_CONNECTING_FAILED) {
                        deviceStatusIcon = mContext.getDrawable(
                                R.drawable.media_output_status_failed);
                        subtitle = mContext.getString(R.string.media_output_dialog_connect_failed);
                        clickListener = v -> onItemClick(v, device);
                } else if (mController.getSelectedMediaDevice().size() > 1 && isSelected) {
                    // selected device in group
                    groupStatus = new GroupStatus(
                            true /* selected */,
                            isDeselectable /* deselectable */);
                    connectionState = ConnectionState.CONNECTED;
                } else if (currentlyConnected) {
                    } else if (currentlyConnected || isSelected) {
                        connectionState = ConnectionState.CONNECTED;
                    // single selected device
                    if (device.hasOngoingSession()) {
                        ongoingSessionStatus = new OngoingSessionStatus(
                                device.isHostForOngoingSession());
                    } else if (mController.isCurrentConnectedDeviceRemote()
                            && !mController.getSelectableMediaDevice().isEmpty()) {
                        //If device is connected and there's other selectable devices, layout as
                        // one of selected devices.
                        groupStatus = new GroupStatus(
                                true /* selected */,
                                isDeselectable /* isDeselectable */);
                    }
                } else if (isSelectable) {
                    //groupable device
                    groupStatus = new GroupStatus(false /* selected */, true /* deselectable */);
                    if (!Flags.disableTransferWhenAppsDoNotSupport()
                            || isTransferable
                    } else { // disconnected
                        if (isSelectable) { // groupable device
                            if (!Flags.disableTransferWhenAppsDoNotSupport() || isTransferable
                                    || hasRouteListingPreferenceItem) {
                                clickListener = v -> onItemClick(v, device);
                            }
                    deviceDisabled = clickListener == null;
                        } else {
                    deviceStatusIcon = getDeviceStatusIcon(device, device.hasOngoingSession());
                            deviceStatusIcon = getDeviceStatusIcon(device,
                                    device.hasOngoingSession());
                            clickListener = getClickListenerBasedOnSelectionBehavior(device);
                        }
                        deviceDisabled = clickListener == null;
                    }
                }
            }

            if (connectionState == ConnectionState.CONNECTED || isDeviceGroup) {
                mCurrentActivePosition = position;
            }

            if (isDeviceGroup) {
                String sessionName = mController.getSessionName() == null ? ""
                        : mController.getSessionName().toString();
                updateTitle(sessionName);
                updateUnmutedVolumeIcon(null /* device */);
                updateGroupSeekBar(getGroupItemContentDescription(sessionName));
                updateEndAreaForDeviceGroup();
                updateItemBackground(ConnectionState.CONNECTED);
                renderDeviceGroupItem();
            } else {
                renderDeviceItem(hideGroupItem, device, connectionState, restrictVolumeAdjustment,
                        groupStatus, ongoingSessionStatus, clickListener, deviceDisabled, subtitle,
                        deviceStatusIcon);
            }
        }

        private void renderDeviceItem(boolean hideGroupItem, MediaDevice device,
                ConnectionState connectionState, boolean restrictVolumeAdjustment,
                GroupStatus groupStatus, OngoingSessionStatus ongoingSessionStatus,
                View.OnClickListener clickListener, boolean deviceDisabled, String subtitle,
                Drawable deviceStatusIcon) {
            if (hideGroupItem) {
                mItemLayout.setVisibility(View.GONE);
                return;
            }
            updateTitle(device.getName());
            updateTitleIcon(device, connectionState, restrictVolumeAdjustment);
            updateSeekBar(device, connectionState, restrictVolumeAdjustment,
@@ -306,6 +286,40 @@ public class MediaOutputAdapter extends MediaOutputBaseAdapter {
            updateDeviceStatusIcon(deviceStatusIcon);
            updateItemBackground(connectionState);
        }

        private void renderDeviceGroupItem() {
            String sessionName = mController.getSessionName() == null ? ""
                    : mController.getSessionName().toString();
            updateTitle(sessionName);
            updateUnmutedVolumeIcon(null /* device */);
            updateGroupSeekBar(getGroupItemContentDescription(sessionName));
            updateEndAreaForDeviceGroup();
            updateItemBackground(ConnectionState.CONNECTED);
        }

        private OngoingSessionStatus getOngoingSessionStatus(MediaDevice device) {
            return device.hasOngoingSession() ? new OngoingSessionStatus(
                    device.isHostForOngoingSession()) : null;
        }

        private GroupStatus getGroupStatus(boolean isSelected, boolean isSelectable,
                boolean isDeselectable) {
            // A device should either be selectable or, when the device selected, the list should
            // have other selectable or selected devices.
            boolean selectedWithOtherGroupDevices =
                    isSelected && (hasMultipleSelectedDevices() || hasSelectableDevices());
            if (isSelectable || selectedWithOtherGroupDevices) {
                return new GroupStatus(isSelected, isDeselectable);
            }
            return null;
        }

        private boolean hasMultipleSelectedDevices() {
            return mController.getSelectedMediaDevice().size() > 1;
        }

        private boolean hasSelectableDevices() {
            return !mController.getSelectableMediaDevice().isEmpty();
        }

        /** Renders the right side round pill button / checkbox. */