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

Commit 1ad92562 authored by Shaowei Shen's avatar Shaowei Shen Committed by Android (Google) Code Review
Browse files

Merge "[Output Switcher] Handle muting expected device" into tm-dev

parents 76386f47 0b289fe7
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -439,7 +439,7 @@ public abstract class MediaDevice implements Comparable<MediaDevice> {
     * Check if it is muting expected device
     * @return {@code true} if it is muting expected device, otherwise return {@code false}
     */
    protected boolean isMutingExpectedDevice() {
    public boolean isMutingExpectedDevice() {
        return false;
    }

+39 −12
Original line number Diff line number Diff line
@@ -107,6 +107,7 @@ public class MediaOutputAdapter extends MediaOutputBaseAdapter {
        @Override
        void onBind(MediaDevice device, boolean topMargin, boolean bottomMargin, int position) {
            super.onBind(device, topMargin, bottomMargin, position);
            boolean isMutingExpectedDeviceExist = mController.hasMutingExpectedDevice();
            final boolean currentlyConnected = !mIncludeDynamicGroup
                    && isCurrentlyConnected(device);
            boolean isCurrentSeekbarInvisible = mSeekBar.getVisibility() == View.GONE;
@@ -146,7 +147,19 @@ public class MediaOutputAdapter extends MediaOutputBaseAdapter {
                }
            } else {
                // Set different layout for each device
                if (device.getState() == MediaDeviceState.STATE_CONNECTING_FAILED) {
                if (device.isMutingExpectedDevice()
                        && !mController.isCurrentConnectedDeviceRemote()) {
                    mTitleIcon.setImageDrawable(
                            mContext.getDrawable(R.drawable.media_output_icon_volume));
                    mTitleIcon.setColorFilter(mController.getColorItemContent());
                    mTitleText.setTextColor(mController.getColorItemContent());
                    setSingleLineLayout(getItemTitle(device), true /* bFocused */,
                            false /* showSeekBar */,
                            false /* showProgressBar */, false /* showStatus */);
                    initMutingExpectedDevice();
                    mCurrentActivePosition = position;
                    mContainerLayout.setOnClickListener(v -> onItemClick(v, device));
                } else if (device.getState() == MediaDeviceState.STATE_CONNECTING_FAILED) {
                    setUpDeviceIcon(device);
                    mStatusIcon.setImageDrawable(
                            mContext.getDrawable(R.drawable.media_output_status_failed));
@@ -193,7 +206,15 @@ public class MediaOutputAdapter extends MediaOutputBaseAdapter {
                    mEndTouchArea.setImportantForAccessibility(
                            View.IMPORTANT_FOR_ACCESSIBILITY_YES);
                    setUpContentDescriptionForView(mEndTouchArea, true, device);
                } else if (!mController.hasAdjustVolumeUserRestriction() && currentlyConnected) {
                } else if (!mController.hasAdjustVolumeUserRestriction()
                        && currentlyConnected) {
                    if (isMutingExpectedDeviceExist
                            && !mController.isCurrentConnectedDeviceRemote()) {
                        // mark as disconnected and set special click listener
                        setUpDeviceIcon(device);
                        setSingleLineLayout(getItemTitle(device), false /* bFocused */);
                        mContainerLayout.setOnClickListener(v -> cancelMuteAwaitConnection());
                    } else {
                        mTitleIcon.setImageDrawable(
                                mContext.getDrawable(R.drawable.media_output_icon_volume));
                        mTitleIcon.setColorFilter(mController.getColorItemContent());
@@ -204,6 +225,7 @@ public class MediaOutputAdapter extends MediaOutputBaseAdapter {
                        initSeekbar(device, isCurrentSeekbarInvisible);
                        setUpContentDescriptionForView(mContainerLayout, false, device);
                        mCurrentActivePosition = position;
                    }
                } else if (isDeviceIncluded(mController.getSelectableMediaDevice(), device)) {
                    setUpDeviceIcon(device);
                    mCheckBox.setOnCheckedChangeListener(null);
@@ -269,6 +291,11 @@ public class MediaOutputAdapter extends MediaOutputBaseAdapter {
            notifyDataSetChanged();
        }

        private void cancelMuteAwaitConnection() {
            mController.cancelMuteAwaitConnection();
            notifyDataSetChanged();
        }

        private void setUpContentDescriptionForView(View view, boolean clickable,
                MediaDevice device) {
            view.setClickable(clickable);
+11 −0
Original line number Diff line number Diff line
@@ -309,6 +309,17 @@ public abstract class MediaOutputBaseAdapter extends
            });
        }

        void initMutingExpectedDevice() {
            disableSeekBar();
            final Drawable backgroundDrawable = mContext.getDrawable(
                                    R.drawable.media_output_item_background_active)
                            .mutate();
            backgroundDrawable.setColorFilter(
                    new PorterDuffColorFilter(mController.getColorConnectedItemBackground(),
                            PorterDuff.Mode.SRC_IN));
            mItemLayout.setBackground(backgroundDrawable);
        }

        void initSessionSeekbar() {
            disableSeekBar();
            mSeekBar.setMax(mController.getSessionVolumeMax());
+4 −2
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.systemui.media.dialog

import android.content.Context
import android.media.AudioManager
import android.media.session.MediaSessionManager
import android.view.View
import com.android.internal.logging.UiEventLogger
@@ -41,7 +42,8 @@ class MediaOutputBroadcastDialogFactory @Inject constructor(
    private val notifCollection: CommonNotifCollection,
    private val uiEventLogger: UiEventLogger,
    private val dialogLaunchAnimator: DialogLaunchAnimator,
    private val nearbyMediaDevicesManagerOptional: Optional<NearbyMediaDevicesManager>
    private val nearbyMediaDevicesManagerOptional: Optional<NearbyMediaDevicesManager>,
    private val audioManager: AudioManager
) {
    var mediaOutputBroadcastDialog: MediaOutputBroadcastDialog? = null

@@ -52,7 +54,7 @@ class MediaOutputBroadcastDialogFactory @Inject constructor(

        val controller = MediaOutputController(context, packageName,
                mediaSessionManager, lbm, starter, notifCollection,
                dialogLaunchAnimator, nearbyMediaDevicesManagerOptional)
                dialogLaunchAnimator, nearbyMediaDevicesManagerOptional, audioManager)
        val dialog =
                MediaOutputBroadcastDialog(context, aboveStatusBar, broadcastSender, controller)
        mediaOutputBroadcastDialog = dialog
+55 −10
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.media.AudioManager;
import android.media.INearbyMediaDevicesUpdateCallback;
import android.media.MediaMetadata;
import android.media.MediaRoute2Info;
@@ -112,6 +113,7 @@ public class MediaOutputController implements LocalMediaManager.DeviceCallback,
    @VisibleForTesting
    final List<MediaDevice> mMediaDevices = new CopyOnWriteArrayList<>();
    final List<MediaDevice> mCachedMediaDevices = new CopyOnWriteArrayList<>();
    private final AudioManager mAudioManager;
    private final NearbyMediaDevicesManager mNearbyMediaDevicesManager;
    private final Map<String, Integer> mNearbyDeviceInfoMap = new ConcurrentHashMap<>();

@@ -122,7 +124,6 @@ public class MediaOutputController implements LocalMediaManager.DeviceCallback,
    Callback mCallback;
    @VisibleForTesting
    LocalMediaManager mLocalMediaManager;

    private MediaOutputMetricLogger mMetricLogger;

    private int mColorItemContent;
@@ -145,13 +146,15 @@ public class MediaOutputController implements LocalMediaManager.DeviceCallback,
            lbm, ActivityStarter starter,
            CommonNotifCollection notifCollection,
            DialogLaunchAnimator dialogLaunchAnimator,
            Optional<NearbyMediaDevicesManager> nearbyMediaDevicesManagerOptional) {
            Optional<NearbyMediaDevicesManager> nearbyMediaDevicesManagerOptional,
            AudioManager audioManager) {
        mContext = context;
        mPackageName = packageName;
        mMediaSessionManager = mediaSessionManager;
        mLocalBluetoothManager = lbm;
        mActivityStarter = starter;
        mNotifCollection = notifCollection;
        mAudioManager = audioManager;
        InfoMediaManager imm = new InfoMediaManager(mContext, packageName, null, lbm);
        mLocalMediaManager = new LocalMediaManager(mContext, lbm, imm, packageName);
        mMetricLogger = new MediaOutputMetricLogger(mContext, mPackageName);
@@ -217,14 +220,14 @@ public class MediaOutputController implements LocalMediaManager.DeviceCallback,
        return false;
    }

    void setRefreshing(boolean refreshing) {
        mIsRefreshing = refreshing;
    }

    boolean isRefreshing() {
        return mIsRefreshing;
    }

    void setRefreshing(boolean refreshing) {
        mIsRefreshing = refreshing;
    }

    void stop() {
        if (mMediaController != null) {
            mMediaController.unregisterCallback(mCb);
@@ -275,6 +278,27 @@ public class MediaOutputController implements LocalMediaManager.DeviceCallback,
        mMetricLogger.logOutputFailure(new ArrayList<>(mMediaDevices), reason);
    }

    /**
     * Checks if there's any muting expected device exist
     */
    public boolean hasMutingExpectedDevice() {
        return mAudioManager.getMutingExpectedDevice() != null;
    }

    /**
     * Cancels mute await connection action in follow up request
     */
    public void cancelMuteAwaitConnection() {
        if (mAudioManager.getMutingExpectedDevice() == null) {
            return;
        }
        try {
            mAudioManager.cancelMuteAwaitConnection(mAudioManager.getMutingExpectedDevice());
        } catch (Exception e) {
            Log.d(TAG, "Unable to cancel mute await connection");
        }
    }

    Drawable getAppSourceIcon() {
        if (mPackageName.isEmpty()) {
            return null;
@@ -471,12 +495,26 @@ public class MediaOutputController implements LocalMediaManager.DeviceCallback,
            // For the first time building list, to make sure the top device is the connected
            // device.
            if (mMediaDevices.isEmpty()) {
                final MediaDevice connectedMediaDevice = getCurrentConnectedMediaDevice();
                boolean needToHandleMutingExpectedDevice =
                        hasMutingExpectedDevice() && !isCurrentConnectedDeviceRemote();
                final MediaDevice connectedMediaDevice =
                        needToHandleMutingExpectedDevice ? null
                                : getCurrentConnectedMediaDevice();
                if (connectedMediaDevice == null) {
                    if (DEBUG) {
                        Log.d(TAG, "No connected media device.");
                        Log.d(TAG, "No connected media device or muting expected device exist.");
                    }
                    if (needToHandleMutingExpectedDevice) {
                        for (MediaDevice device : devices) {
                            if (device.isMutingExpectedDevice()) {
                                mMediaDevices.add(0, device);
                            } else {
                                mMediaDevices.add(device);
                            }
                        }
                    } else {
                        mMediaDevices.addAll(devices);
                    }
                    return;
                }
                for (MediaDevice device : devices) {
@@ -516,6 +554,12 @@ public class MediaOutputController implements LocalMediaManager.DeviceCallback,

    }

    boolean isCurrentConnectedDeviceRemote() {
        MediaDevice currentConnectedMediaDevice = getCurrentConnectedMediaDevice();
        return currentConnectedMediaDevice != null && isActiveRemoteDevice(
                currentConnectedMediaDevice);
    }

    List<MediaDevice> getGroupMediaDevices() {
        final List<MediaDevice> selectedDevices = getSelectedMediaDevice();
        final List<MediaDevice> selectableDevices = getSelectableMediaDevice();
@@ -731,7 +775,8 @@ public class MediaOutputController implements LocalMediaManager.DeviceCallback,
    void launchMediaOutputBroadcastDialog(View mediaOutputDialog, BroadcastSender broadcastSender) {
        MediaOutputController controller = new MediaOutputController(mContext, mPackageName,
                mMediaSessionManager, mLocalBluetoothManager, mActivityStarter,
                mNotifCollection, mDialogLaunchAnimator, Optional.of(mNearbyMediaDevicesManager));
                mNotifCollection, mDialogLaunchAnimator, Optional.of(mNearbyMediaDevicesManager),
                mAudioManager);
        MediaOutputBroadcastDialog dialog = new MediaOutputBroadcastDialog(mContext, true,
                broadcastSender, controller);
        mDialogLaunchAnimator.showFromView(dialog, mediaOutputDialog);
Loading