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

Commit 73451128 authored by Abhijoy Saha's avatar Abhijoy Saha
Browse files

Switch CarVolumeItems when group volume changes.

If volume dialog is not expanded, then we just replace the old default
CarVolumeItem with the new group.

Bug: 141752233
Fixes: 141752233
Test: Manual
Change-Id: Idfa6ad3a1129e069a297f1c1cfd47b40a61773cc
parent fc07949b
Loading
Loading
Loading
Loading
+59 −51
Original line number Original line Diff line number Diff line
@@ -62,7 +62,6 @@ import org.xmlpull.v1.XmlPullParserException;


import java.io.IOException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.List;


/**
/**
@@ -91,12 +90,21 @@ public class CarVolumeDialogImpl implements VolumeDialog {
    // Volume items in the RecyclerView.
    // Volume items in the RecyclerView.
    private final List<CarVolumeItem> mCarVolumeLineItems = new ArrayList<>();
    private final List<CarVolumeItem> mCarVolumeLineItems = new ArrayList<>();
    private final KeyguardManager mKeyguard;
    private final KeyguardManager mKeyguard;

    private Window mWindow;
    private Window mWindow;
    private CustomDialog mDialog;
    private CustomDialog mDialog;
    private RecyclerView mListView;
    private RecyclerView mListView;
    private CarVolumeItemAdapter mVolumeItemsAdapter;
    private CarVolumeItemAdapter mVolumeItemsAdapter;
    private Car mCar;
    private Car mCar;
    private CarAudioManager mCarAudioManager;
    private CarAudioManager mCarAudioManager;
    private boolean mHovering;
    private int mCurrentlyDisplayingGroupId;
    private int mPreviouslyDisplayingGroupId;
    private boolean mShowing;
    private boolean mDismissing;
    private boolean mExpanded;
    private View mExpandIcon;

    private final CarAudioManager.CarVolumeCallback mVolumeChangeCallback =
    private final CarAudioManager.CarVolumeCallback mVolumeChangeCallback =
            new CarAudioManager.CarVolumeCallback() {
            new CarAudioManager.CarVolumeCallback() {
                @Override
                @Override
@@ -126,6 +134,7 @@ public class CarVolumeDialogImpl implements VolumeDialog {
                        volumeItem.progress = value;
                        volumeItem.progress = value;
                    }
                    }
                    if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
                    if ((flags & AudioManager.FLAG_SHOW_UI) != 0) {
                        mPreviouslyDisplayingGroupId = mCurrentlyDisplayingGroupId;
                        mCurrentlyDisplayingGroupId = groupId;
                        mCurrentlyDisplayingGroupId = groupId;
                        mHandler.obtainMessage(H.SHOW,
                        mHandler.obtainMessage(H.SHOW,
                                Events.SHOW_REASON_VOLUME_CHANGED).sendToTarget();
                                Events.SHOW_REASON_VOLUME_CHANGED).sendToTarget();
@@ -137,12 +146,6 @@ public class CarVolumeDialogImpl implements VolumeDialog {
                    // ignored
                    // ignored
                }
                }
            };
            };
    private boolean mHovering;
    private int mCurrentlyDisplayingGroupId;
    private boolean mShowing;
    private boolean mDismissing;
    private boolean mExpanded;
    private View mExpandIcon;


    private final CarServiceLifecycleListener mCarServiceLifecycleListener = (car, ready) -> {
    private final CarServiceLifecycleListener mCarServiceLifecycleListener = (car, ready) -> {
        if (!ready) {
        if (!ready) {
@@ -158,7 +161,7 @@ public class CarVolumeDialogImpl implements VolumeDialog {
            mAvailableVolumeItems.add(volumeItem);
            mAvailableVolumeItems.add(volumeItem);
            // The first one is the default item.
            // The first one is the default item.
            if (groupId == 0) {
            if (groupId == 0) {
                setuptListItem(0);
                clearAllAndSetupDefaultCarVolumeLineItem(0);
            }
            }
        }
        }


@@ -169,15 +172,6 @@ public class CarVolumeDialogImpl implements VolumeDialog {
        mCarAudioManager.registerCarVolumeCallback(mVolumeChangeCallback);
        mCarAudioManager.registerCarVolumeCallback(mVolumeChangeCallback);
    };
    };


    private void setuptListItem(int groupId) {
        mCarVolumeLineItems.clear();
        VolumeItem volumeItem = mAvailableVolumeItems.get(groupId);
        volumeItem.defaultItem = true;
        addCarVolumeListItem(volumeItem, /* volumeGroupId = */ groupId,
                R.drawable.car_ic_keyboard_arrow_down, new ExpandIconListener()
        );
    }

    public CarVolumeDialogImpl(Context context) {
    public CarVolumeDialogImpl(Context context) {
        mContext = context;
        mContext = context;
        mKeyguard = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
        mKeyguard = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
@@ -204,7 +198,7 @@ public class CarVolumeDialogImpl implements VolumeDialog {


    @Override
    @Override
    public void destroy() {
    public void destroy() {
        mHandler.removeCallbacksAndMessages(null);
        mHandler.removeCallbacksAndMessages(/* token= */ null);


        cleanupAudioManager();
        cleanupAudioManager();
        // unregisterVolumeCallback is not being called when disconnect car, so we manually cleanup
        // unregisterVolumeCallback is not being called when disconnect car, so we manually cleanup
@@ -280,19 +274,36 @@ public class CarVolumeDialogImpl implements VolumeDialog {


        mHandler.removeMessages(H.SHOW);
        mHandler.removeMessages(H.SHOW);
        mHandler.removeMessages(H.DISMISS);
        mHandler.removeMessages(H.DISMISS);

        rescheduleTimeoutH();
        rescheduleTimeoutH();

        // Refresh the data set before showing.
        // Refresh the data set before showing.
        mVolumeItemsAdapter.notifyDataSetChanged();
        mVolumeItemsAdapter.notifyDataSetChanged();

        if (mShowing) {
        if (mShowing) {
            if (mPreviouslyDisplayingGroupId == mCurrentlyDisplayingGroupId || mExpanded) {
                return;
            }

            clearAllAndSetupDefaultCarVolumeLineItem(mCurrentlyDisplayingGroupId);
            return;
            return;
        }
        }

        mShowing = true;
        mShowing = true;
        setuptListItem(mCurrentlyDisplayingGroupId);
        clearAllAndSetupDefaultCarVolumeLineItem(mCurrentlyDisplayingGroupId);
        mDialog.show();
        mDialog.show();
        Events.writeEvent(mContext, Events.EVENT_SHOW_DIALOG, reason, mKeyguard.isKeyguardLocked());
        Events.writeEvent(mContext, Events.EVENT_SHOW_DIALOG, reason, mKeyguard.isKeyguardLocked());
    }
    }


    private void rescheduleTimeoutH() {
    private void clearAllAndSetupDefaultCarVolumeLineItem(int groupId) {
        mCarVolumeLineItems.clear();
        VolumeItem volumeItem = mAvailableVolumeItems.get(groupId);
        volumeItem.defaultItem = true;
        addCarVolumeListItem(volumeItem, /* volumeGroupId = */ groupId,
                R.drawable.car_ic_keyboard_arrow_down, new ExpandIconListener());
    }

    protected void rescheduleTimeoutH() {
        mHandler.removeMessages(H.DISMISS);
        mHandler.removeMessages(H.DISMISS);
        final int timeout = computeTimeoutH();
        final int timeout = computeTimeoutH();
        mHandler.sendMessageDelayed(mHandler
        mHandler.sendMessageDelayed(mHandler
@@ -366,12 +377,13 @@ public class CarVolumeDialogImpl implements VolumeDialog {
                if (XML_TAG_VOLUME_ITEM.equals(parser.getName())) {
                if (XML_TAG_VOLUME_ITEM.equals(parser.getName())) {
                    TypedArray item = mContext.getResources().obtainAttributes(
                    TypedArray item = mContext.getResources().obtainAttributes(
                            attrs, R.styleable.carVolumeItems_item);
                            attrs, R.styleable.carVolumeItems_item);
                    int usage = item.getInt(R.styleable.carVolumeItems_item_usage, -1);
                    int usage = item.getInt(R.styleable.carVolumeItems_item_usage,
                            /* defValue= */ -1);
                    if (usage >= 0) {
                    if (usage >= 0) {
                        VolumeItem volumeItem = new VolumeItem();
                        VolumeItem volumeItem = new VolumeItem();
                        volumeItem.rank = rank;
                        volumeItem.rank = rank;
                        volumeItem.icon = item.getResourceId(R.styleable.carVolumeItems_item_icon,
                        volumeItem.icon = item.getResourceId(
                                0);
                                R.styleable.carVolumeItems_item_icon, /* defValue= */ 0);
                        mVolumeItems.put(usage, volumeItem);
                        mVolumeItems.put(usage, volumeItem);
                        rank++;
                        rank++;
                    }
                    }
@@ -396,22 +408,22 @@ public class CarVolumeDialogImpl implements VolumeDialog {
        return result;
        return result;
    }
    }


    private CarVolumeItem addCarVolumeListItem(VolumeItem volumeItem, int volumeGroupId,
    private CarVolumeItem createCarVolumeListItem(VolumeItem volumeItem, int volumeGroupId,
            int supplementalIconId,
            Drawable supplementalIcon, int seekbarProgressValue,
            @Nullable View.OnClickListener supplementalIconOnClickListener) {
            @Nullable View.OnClickListener supplementalIconOnClickListener) {
        CarVolumeItem carVolumeItem = new CarVolumeItem();
        CarVolumeItem carVolumeItem = new CarVolumeItem();
        carVolumeItem.setMax(getMaxSeekbarValue(mCarAudioManager, volumeGroupId));
        carVolumeItem.setMax(getMaxSeekbarValue(mCarAudioManager, volumeGroupId));
        int color = mContext.getResources().getColor(R.color.car_volume_dialog_tint);
        carVolumeItem.setProgress(seekbarProgressValue);
        int progress = getSeekbarValue(mCarAudioManager, volumeGroupId);
        carVolumeItem.setProgress(progress);
        carVolumeItem.setOnSeekBarChangeListener(
        carVolumeItem.setOnSeekBarChangeListener(
                new CarVolumeDialogImpl.VolumeSeekBarChangeListener(volumeGroupId,
                new CarVolumeDialogImpl.VolumeSeekBarChangeListener(volumeGroupId,
                        mCarAudioManager));
                        mCarAudioManager));
        Drawable primaryIcon = mContext.getResources().getDrawable(volumeItem.icon);
        carVolumeItem.setGroupId(volumeGroupId);

        int color = mContext.getColor(R.color.car_volume_dialog_tint);
        Drawable primaryIcon = mContext.getDrawable(volumeItem.icon);
        primaryIcon.mutate().setTint(color);
        primaryIcon.mutate().setTint(color);
        carVolumeItem.setPrimaryIcon(primaryIcon);
        carVolumeItem.setPrimaryIcon(primaryIcon);
        if (supplementalIconId != 0) {
        if (supplementalIcon != null) {
            Drawable supplementalIcon = mContext.getResources().getDrawable(supplementalIconId);
            supplementalIcon.mutate().setTint(color);
            supplementalIcon.mutate().setTint(color);
            carVolumeItem.setSupplementalIcon(supplementalIcon,
            carVolumeItem.setSupplementalIcon(supplementalIcon,
                    /* showSupplementalIconDivider= */ true);
                    /* showSupplementalIconDivider= */ true);
@@ -420,21 +432,23 @@ public class CarVolumeDialogImpl implements VolumeDialog {
            carVolumeItem.setSupplementalIcon(/* drawable= */ null,
            carVolumeItem.setSupplementalIcon(/* drawable= */ null,
                    /* showSupplementalIconDivider= */ false);
                    /* showSupplementalIconDivider= */ false);
        }
        }
        carVolumeItem.setGroupId(volumeGroupId);

        mCarVolumeLineItems.add(carVolumeItem);
        volumeItem.carVolumeItem = carVolumeItem;
        volumeItem.carVolumeItem = carVolumeItem;
        volumeItem.progress = progress;
        volumeItem.progress = seekbarProgressValue;

        return carVolumeItem;
        return carVolumeItem;
    }
    }


    private VolumeItem findVolumeItem(CarVolumeItem targetItem) {
    private CarVolumeItem addCarVolumeListItem(VolumeItem volumeItem, int volumeGroupId,
        for (int i = 0; i < mVolumeItems.size(); ++i) {
            int supplementalIconId,
            VolumeItem volumeItem = mVolumeItems.valueAt(i);
            @Nullable View.OnClickListener supplementalIconOnClickListener) {
            if (volumeItem.carVolumeItem == targetItem) {
        int seekbarProgressValue = getSeekbarValue(mCarAudioManager, volumeGroupId);
                return volumeItem;
        Drawable supplementalIcon = supplementalIconId == 0 ? null : mContext.getDrawable(
            }
                supplementalIconId);
        }
        CarVolumeItem carVolumeItem = createCarVolumeListItem(volumeItem, volumeGroupId,
        return null;
                supplementalIcon, seekbarProgressValue, supplementalIconOnClickListener);
        mCarVolumeLineItems.add(carVolumeItem);
        return carVolumeItem;
    }
    }


    private void cleanupAudioManager() {
    private void cleanupAudioManager() {
@@ -530,21 +544,15 @@ public class CarVolumeDialogImpl implements VolumeDialog {
            for (int groupId = 0; groupId < mAvailableVolumeItems.size(); ++groupId) {
            for (int groupId = 0; groupId < mAvailableVolumeItems.size(); ++groupId) {
                if (groupId != mCurrentlyDisplayingGroupId) {
                if (groupId != mCurrentlyDisplayingGroupId) {
                    VolumeItem volumeItem = mAvailableVolumeItems.get(groupId);
                    VolumeItem volumeItem = mAvailableVolumeItems.get(groupId);
                    addCarVolumeListItem(volumeItem, groupId, 0, null);
                    addCarVolumeListItem(volumeItem, groupId, /* supplementalIconId= */ 0,
                            /* supplementalIconOnClickListener= */ null);
                }
                }
            }
            }
            inAnimator = AnimatorInflater.loadAnimator(
            inAnimator = AnimatorInflater.loadAnimator(
                    mContext, R.anim.car_arrow_fade_in_rotate_up);
                    mContext, R.anim.car_arrow_fade_in_rotate_up);


        } else {
        } else {
            // Only keeping the default stream if it is not expended.
            clearAllAndSetupDefaultCarVolumeLineItem(mCurrentlyDisplayingGroupId);
            Iterator itr = mCarVolumeLineItems.iterator();
            while (itr.hasNext()) {
                CarVolumeItem carVolumeItem = (CarVolumeItem) itr.next();
                if (carVolumeItem.getGroupId() != mCurrentlyDisplayingGroupId) {
                    itr.remove();
                }
            }
            inAnimator = AnimatorInflater.loadAnimator(
            inAnimator = AnimatorInflater.loadAnimator(
                    mContext, R.anim.car_arrow_fade_in_rotate_down);
                    mContext, R.anim.car_arrow_fade_in_rotate_down);
        }
        }