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

Commit 4288dae2 authored by Alex Shabalin's avatar Alex Shabalin
Browse files

Consolidate the seek bar and title icon rendering.

Flag: EXEMPT refactor
Bug: 387570618
Test: atest SystemUIGoogleRoboRNGTests:MediaOutputAdapterScreenshotTest,
    atest SystemUiRoboTests:MediaOutputAdapterTest
Change-Id: Iff5adb6830c0fa0cea69dfc7d8806ed7f89a9b01
parent 0d977866
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -329,6 +329,7 @@ public class MediaOutputAdapterTest extends SysuiTestCase {
        mMediaOutputAdapter.onBindViewHolder(mViewHolder, 0);

        assertThat(mViewHolder.mProgressBar.getVisibility()).isEqualTo(View.GONE);
        assertThat(mViewHolder.mSeekBar.getVisibility()).isEqualTo(View.GONE);
        assertThat(mViewHolder.mCheckBox.getVisibility()).isEqualTo(View.GONE);
        assertThat(mViewHolder.mTitleText.getVisibility()).isEqualTo(View.VISIBLE);
        assertThat(mViewHolder.mTitleText.getText().toString()).isEqualTo(TEST_DEVICE_NAME_1);
@@ -344,6 +345,7 @@ public class MediaOutputAdapterTest extends SysuiTestCase {
        mMediaOutputAdapter.onBindViewHolder(mViewHolder, 0);

        assertThat(mViewHolder.mProgressBar.getVisibility()).isEqualTo(View.GONE);
        assertThat(mViewHolder.mSeekBar.getVisibility()).isEqualTo(View.GONE);
        assertThat(mViewHolder.mCheckBox.getVisibility()).isEqualTo(View.GONE);
        assertThat(mViewHolder.mTitleText.getVisibility()).isEqualTo(View.VISIBLE);
        assertThat(mViewHolder.mTitleText.getText().toString()).isEqualTo(TEST_DEVICE_NAME_1);
+15 −36
Original line number Diff line number Diff line
@@ -156,7 +156,6 @@ public class MediaOutputAdapter extends MediaOutputBaseAdapter {
            super.onBind(device, position);
            boolean isMutingExpectedDeviceExist = mController.hasMutingExpectedDevice();
            final boolean currentlyConnected = isCurrentlyConnected(device);
            boolean isCurrentSeekbarInvisible = mSeekBar.getVisibility() == View.GONE;
            boolean isSelected = isDeviceIncluded(mController.getSelectedMediaDevice(), device);
            boolean isDeselectable =
                    isDeviceIncluded(mController.getDeselectableMediaDevice(), device);
@@ -189,6 +188,7 @@ public class MediaOutputAdapter extends MediaOutputBaseAdapter {
            GroupStatus groupStatus = null;
            OngoingSessionStatus ongoingSessionStatus = null;
            ConnectionState connectionState = ConnectionState.DISCONNECTED;
            boolean restrictVolumeAdjustment = false;

            if (mCurrentActivePosition == position) {
                mCurrentActivePosition = -1;
@@ -201,10 +201,8 @@ public class MediaOutputAdapter extends MediaOutputBaseAdapter {
                if (device.getState() == MediaDeviceState.STATE_CONNECTING
                        && !mController.hasAdjustVolumeUserRestriction()) {
                    connectionState = ConnectionState.CONNECTING;
                    setUpDeviceIcon(device);
                    setSingleLineLayout(device.getName(), false /* showSeekBar*/);
                    setSingleLineLayout(device.getName());
                } else {
                    setUpDeviceIcon(device);
                    setSingleLineLayout(device.getName());
                }
            } else {
@@ -212,11 +210,10 @@ public class MediaOutputAdapter extends MediaOutputBaseAdapter {
                if (device.isMutingExpectedDevice()
                        && !mController.isCurrentConnectedDeviceRemote()) {
                    connectionState = ConnectionState.CONNECTED;
                    updateUnmutedVolumeIcon(device);
                    restrictVolumeAdjustment = true;
                    mCurrentActivePosition = position;
                    updateFullItemClickListener(v -> onItemClick(v, device));
                    setSingleLineLayout(device.getName());
                    initFakeActiveDevice(device);
                } else if (mShouldGroupSelectedMediaItems
                        && mController.getSelectedMediaDevice().size() > 1
                        && isDeviceIncluded(mController.getSelectedMediaDevice(), device)) {
@@ -226,35 +223,27 @@ public class MediaOutputAdapter extends MediaOutputBaseAdapter {
                    } else {
                        isDeviceGroup = true;
                        String sessionName = mController.getSessionName().toString();
                        updateUnmutedVolumeIcon(null);
                        disableFocusPropertyForView(mContainerLayout);
                        setUpContentDescriptionForView(mSeekBar, mContext.getString(
                                R.string.accessibility_cast_name, sessionName));
                        setSingleLineLayout(sessionName, true /* showSeekBar */);
                        initGroupSeekbar(isCurrentSeekbarInvisible);
                        setSingleLineLayout(sessionName);
                    }
                } else if (device.hasSubtext()) {
                    boolean isActiveWithOngoingSession =
                            device.hasOngoingSession() && (currentlyConnected || isSelected);
                    if (isActiveWithOngoingSession) {
                        mCurrentActivePosition = position;
                        updateUnmutedVolumeIcon(device);
                        mSubTitleText.setText(device.getSubtextString());
                        updateContentAlpha(DEVICE_CONNECTED_ALPHA);
                        ongoingSessionStatus = new OngoingSessionStatus(
                                device.isHostForOngoingSession());
                        setTwoLineLayout(device.getName() /* title */,
                                true /* showSeekBar */,
                                true /* showSubtitle */, false /* showStatus */);
                        connectionState = ConnectionState.CONNECTED;
                        initSeekbar(device, isCurrentSeekbarInvisible);
                    } else {
                        if (currentlyConnected) {
                            mCurrentActivePosition = position;
                            updateUnmutedVolumeIcon(device);
                            connectionState = ConnectionState.CONNECTED;
                        } else {
                            setUpDeviceIcon(device);
                        }
                        mSubTitleText.setText(device.getSubtextString());
                        Drawable deviceStatusIcon =
@@ -270,84 +259,70 @@ public class MediaOutputAdapter extends MediaOutputBaseAdapter {
                                updateClickActionBasedOnSelectionBehavior(device)
                                        ? DEVICE_CONNECTED_ALPHA : DEVICE_DISCONNECTED_ALPHA);
                        setTwoLineLayout(device.getName(),
                                currentlyConnected  /* showSeekBar */,
                                true /* showSubtitle */,
                                deviceStatusIcon != null /* showStatus */);
                    }
                } else if (device.getState() == MediaDeviceState.STATE_CONNECTING_FAILED) {
                    setUpDeviceIcon(device);
                    updateConnectionFailedStatusIcon();
                    mSubTitleText.setText(R.string.media_output_dialog_connect_failed);
                    updateFullItemClickListener(v -> onItemClick(v, device));
                    setTwoLineLayout(device.getName(), false /* showSeekBar */,
                    setTwoLineLayout(device.getName(),
                            true /* showSubtitle */,
                            true /* showStatus */);
                } else if (device.getState() == MediaDeviceState.STATE_GROUPING) {
                    connectionState = ConnectionState.CONNECTING;
                    setUpDeviceIcon(device);
                    setSingleLineLayout(device.getName(), false /* showSeekBar*/);
                    setSingleLineLayout(device.getName());
                } else if (mController.getSelectedMediaDevice().size() > 1 && isSelected) {
                    // selected device in group
                    updateUnmutedVolumeIcon(device);
                    groupStatus = new GroupStatus(
                            true /* selected */,
                            isDeselectable /* deselectable */);
                    disableFocusPropertyForView(mContainerLayout);
                    setUpContentDescriptionForView(mSeekBar, device);
                    setSingleLineLayout(device.getName(), true /* showSeekBar */);
                    setSingleLineLayout(device.getName());
                    connectionState = ConnectionState.CONNECTED;
                    initSeekbar(device, isCurrentSeekbarInvisible);
                } else if (!mController.hasAdjustVolumeUserRestriction()
                        && currentlyConnected) {
                    // single selected device
                    if (isMutingExpectedDeviceExist
                            && !mController.isCurrentConnectedDeviceRemote()) {
                        // mark as disconnected and set special click listener
                        setUpDeviceIcon(device);
                        updateFullItemClickListener(v -> cancelMuteAwaitConnection());
                        setSingleLineLayout(device.getName());
                    } else if (device.hasOngoingSession()) {
                        mCurrentActivePosition = position;
                        updateUnmutedVolumeIcon(device);
                        ongoingSessionStatus = new OngoingSessionStatus(
                                device.isHostForOngoingSession());
                        setSingleLineLayout(device.getName(), true /* showSeekBar */);
                        setSingleLineLayout(device.getName());
                        connectionState = ConnectionState.CONNECTED;
                        initSeekbar(device, isCurrentSeekbarInvisible);
                    } else if (mController.isCurrentConnectedDeviceRemote()
                            && !mController.getSelectableMediaDevice().isEmpty()) {
                        //If device is connected and there's other selectable devices, layout as
                        // one of selected devices.
                        updateUnmutedVolumeIcon(device);
                        groupStatus = new GroupStatus(
                                true /* selected */,
                                isDeselectable /* isDeselectable */);
                        disableFocusPropertyForView(mContainerLayout);
                        setUpContentDescriptionForView(mSeekBar, device);
                        setSingleLineLayout(device.getName(), true /* showSeekBar */);
                        setSingleLineLayout(device.getName());
                        connectionState = ConnectionState.CONNECTED;
                        initSeekbar(device, isCurrentSeekbarInvisible);
                    } else {
                        updateUnmutedVolumeIcon(device);
                        disableFocusPropertyForView(mContainerLayout);
                        setUpContentDescriptionForView(mSeekBar, device);
                        mCurrentActivePosition = position;
                        setSingleLineLayout(device.getName(), true /* showSeekBar */);
                        setSingleLineLayout(device.getName());
                        connectionState = ConnectionState.CONNECTED;
                        initSeekbar(device, isCurrentSeekbarInvisible);
                    }
                } else if (isSelectable) {
                    //groupable device
                    setUpDeviceIcon(device);
                    groupStatus = new GroupStatus(false /* selected */, true /* deselectable */);
                    if (!Flags.disableTransferWhenAppsDoNotSupport()
                            || isTransferable
                            || hasRouteListingPreferenceItem) {
                        updateFullItemClickListener(v -> onItemClick(v, device));
                    }
                    setSingleLineLayout(device.getName(), false /* showSeekBar */);
                    setSingleLineLayout(device.getName());
                } else {
                    setUpDeviceIcon(device);
                    setSingleLineLayout(device.getName());
                    Drawable deviceStatusIcon =
                            device.hasOngoingSession() ? mContext.getDrawable(
@@ -366,9 +341,13 @@ public class MediaOutputAdapter extends MediaOutputBaseAdapter {
            }

            if (isDeviceGroup) {
                updateUnmutedVolumeIcon(null /* device */);
                updateGroupSeekBar();
                updateEndAreaForDeviceGroup();
                updateItemBackground(ConnectionState.CONNECTED);
            } else {
                updateTitleIcon(device, connectionState, restrictVolumeAdjustment);
                updateSeekBar(device, connectionState, restrictVolumeAdjustment);
                updateEndArea(device, connectionState, groupStatus, ongoingSessionStatus);
                updateLoadingIndicator(connectionState);
                updateItemBackground(connectionState);
+40 −27
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.recyclerview.widget.RecyclerView;

@@ -197,31 +198,34 @@ public abstract class MediaOutputBaseAdapter extends
        }

        void setSingleLineLayout(CharSequence title) {
            setSingleLineLayout(title, false);
            mTitleText.setText(title);
        }

        void setSingleLineLayout(CharSequence title, boolean showSeekBar) {
        void updateSeekBar(@NonNull MediaDevice device, ConnectionState connectionState,
                boolean restrictVolumeAdjustment) {
            boolean showSeekBar =
                    connectionState == ConnectionState.CONNECTED && !restrictVolumeAdjustment;
            if (!mCornerAnimator.isRunning()) {
                if (showSeekBar) {
                    updateSeekbarProgressBackground();
                }
            }
            mSeekBar.setAlpha(1);
            boolean isCurrentSeekbarInvisible = mSeekBar.getVisibility() == View.GONE;
            mSeekBar.setVisibility(showSeekBar ? View.VISIBLE : View.GONE);
            if (!showSeekBar) {
                mSeekBar.resetVolume();
            if (showSeekBar) {
                initSeekbar(device, isCurrentSeekbarInvisible);
            }
            mTitleText.setText(title);
        }

        void setTwoLineLayout(CharSequence title,
                boolean showSeekBar, boolean showSubtitle, boolean showStatus) {
            mStatusIcon.setVisibility(showStatus ? View.VISIBLE : View.GONE);
            mSeekBar.setAlpha(1);
            mSeekBar.setVisibility(showSeekBar ? View.VISIBLE : View.GONE);
            if (showSeekBar) {
        void updateGroupSeekBar() {
            updateSeekbarProgressBackground();
            boolean isCurrentSeekbarInvisible = mSeekBar.getVisibility() == View.GONE;
            mSeekBar.setVisibility(View.VISIBLE);
            initGroupSeekbar(isCurrentSeekbarInvisible);
        }

        void setTwoLineLayout(CharSequence title, boolean showSubtitle, boolean showStatus) {
            mStatusIcon.setVisibility(showStatus ? View.VISIBLE : View.GONE);
            mSubTitleText.setVisibility(showSubtitle ? View.VISIBLE : View.GONE);
            mTitleText.setText(title);
        }
@@ -277,7 +281,8 @@ public abstract class MediaOutputBaseAdapter extends
        }

        private void initializeSeekbarVolume(
                MediaDevice device, int currentVolume, boolean isCurrentSeekbarInvisible) {
                @Nullable MediaDevice device, int currentVolume,
                boolean isCurrentSeekbarInvisible) {
            if (!mIsDragging) {
                if (mSeekBar.getVolume() != currentVolume && (mLatestUpdateVolume == -1
                        || currentVolume == mLatestUpdateVolume)) {
@@ -314,7 +319,7 @@ public abstract class MediaOutputBaseAdapter extends
            }
        }

        void initSeekbar(MediaDevice device, boolean isCurrentSeekbarInvisible) {
        void initSeekbar(@NonNull MediaDevice device, boolean isCurrentSeekbarInvisible) {
            SeekBarVolumeControl volumeControl = new SeekBarVolumeControl() {
                @Override
                public int getVolume() {
@@ -385,20 +390,34 @@ public abstract class MediaOutputBaseAdapter extends
            });
        }

        void updateMutedVolumeIcon(MediaDevice device) {
        protected void updateTitleIcon(@NonNull MediaDevice device,
                ConnectionState connectionState, boolean restrictVolumeAdjustment) {
            if (connectionState == ConnectionState.CONNECTED) {
                if (restrictVolumeAdjustment) {
                    // Volume icon without a background that makes it looks like part of a seekbar.
                    updateVolumeIcon(device, false /* isMutedIcon */);
                } else {
                    updateUnmutedVolumeIcon(device);
                }
            } else {
                setUpDeviceIcon(device);
            }
        }

        void updateMutedVolumeIcon(@Nullable MediaDevice device) {
            mIconAreaLayout.setBackground(
                    mContext.getDrawable(R.drawable.media_output_item_background_active));
            updateTitleIcon(device, true /* isMutedVolumeIcon */);
            updateVolumeIcon(device, true /* isMutedVolumeIcon */);
        }

        void updateUnmutedVolumeIcon(MediaDevice device) {
        void updateUnmutedVolumeIcon(@Nullable MediaDevice device) {
            mIconAreaLayout.setBackground(
                    mContext.getDrawable(R.drawable.media_output_title_icon_area)
            );
            updateTitleIcon(device, false /* isMutedVolumeIcon */);
            updateVolumeIcon(device, false /* isMutedVolumeIcon */);
        }

        void updateTitleIcon(MediaDevice device, boolean isMutedVolumeIcon) {
        void updateVolumeIcon(@Nullable MediaDevice device, boolean isMutedVolumeIcon) {
            boolean isInputMediaDevice = device instanceof InputMediaDevice;
            int id = getDrawableId(isInputMediaDevice, isMutedVolumeIcon);
            mTitleIcon.setImageDrawable(mContext.getDrawable(id));
@@ -424,12 +443,6 @@ public abstract class MediaOutputBaseAdapter extends
            mIconAreaLayout.setOnClickListener(listener);
        }

        void initFakeActiveDevice(MediaDevice device) {
            disableSeekBar();
            updateTitleIcon(device, false /* isMutedIcon */);
            mIconAreaLayout.setBackground(null);
        }

        private void initAnimator() {
            mCornerAnimator = ValueAnimator.ofFloat(mInactiveRadius, mActiveRadius);
            mCornerAnimator.setDuration(ANIM_DURATION);
@@ -495,7 +508,7 @@ public abstract class MediaOutputBaseAdapter extends

        }

        protected void setUpDeviceIcon(MediaDevice device) {
        protected void setUpDeviceIcon(@NonNull MediaDevice device) {
            ThreadUtils.postOnBackgroundThread(() -> {
                Icon icon = mController.getDeviceIconCompat(device).toIcon(mContext);
                ThreadUtils.postOnMainThread(() -> {