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

Commit d9fe8193 authored by Juan Sebastian Martinez's avatar Juan Sebastian Martinez Committed by Android (Google) Code Review
Browse files

Merge "Testing deliverOnProgressChangedHaptics in VolumeRow" into main

parents f16e8ea2 d7d0aa79
Loading
Loading
Loading
Loading
+17 −13
Original line number Diff line number Diff line
@@ -174,9 +174,6 @@ public class VolumeDialogImpl implements VolumeDialog, Dumpable,
    private static final String TYPE_DISMISS = "dismiss";
    /** Volume dialog slider animation. */
    private static final String TYPE_UPDATE = "update";
    static final int PROGRESS_HAPTICS_DISABLED = 0;
    static final int PROGRESS_HAPTICS_EAGER = 1;
    static final int PROGRESS_HAPTICS_ANIMATED = 2;

    /**
     *  TODO(b/290612381): remove lingering animations or tolerate them
@@ -2112,7 +2109,7 @@ public class VolumeDialogImpl implements VolumeDialog, Dumpable,
                    row.anim.setIntValues(progress, newProgress);
                    // The animator can't keep up with the volume changes so haptics need to be
                    // triggered here. This happens when the volume keys are continuously pressed.
                    row.deliverOnProgressChangedHaptics(false, newProgress, PROGRESS_HAPTICS_EAGER);
                    row.deliverOnProgressChangedHaptics(false, newProgress);
                }
                row.animTargetProgress = newProgress;
                row.anim.setDuration(UPDATE_ANIMATION_DURATION);
@@ -2127,13 +2124,14 @@ public class VolumeDialogImpl implements VolumeDialog, Dumpable,
        }
    }

    @VisibleForTesting int progressHapticsForStream(int stream) {
    @VisibleForTesting
    boolean canDeliverProgressHapticsToStream(int stream, boolean fromUser, int progress) {
        for (VolumeRow row: mRows) {
            if (row.stream == stream) {
                return row.mProgressHapticsType;
                return row.deliverOnProgressChangedHaptics(fromUser, progress);
            }
        }
        return PROGRESS_HAPTICS_DISABLED;
        return false;
    }

    private void recheckH(VolumeRow row) {
@@ -2527,8 +2525,7 @@ public class VolumeDialogImpl implements VolumeDialog, Dumpable,
                if (fromUser || mRow.animTargetProgress == progress) {
                    // Deliver user-generated slider haptics immediately, or when the animation
                    // completes
                    mRow.deliverOnProgressChangedHaptics(
                            fromUser, progress, PROGRESS_HAPTICS_ANIMATED);
                    mRow.deliverOnProgressChangedHaptics(fromUser, progress);
                }
            }
            if (D.BUG) Log.d(TAG, AudioSystem.streamToString(mRow.stream)
@@ -2641,7 +2638,6 @@ public class VolumeDialogImpl implements VolumeDialog, Dumpable,
        private int animTargetProgress;
        private int lastAudibleLevel = 1;
        private SeekbarHapticPlugin mHapticPlugin;
        private int mProgressHapticsType = PROGRESS_HAPTICS_DISABLED;

        void setIcon(int iconRes, Resources.Theme theme) {
            if (icon != null) {
@@ -2683,15 +2679,23 @@ public class VolumeDialogImpl implements VolumeDialog, Dumpable,
            slider.setOnTouchListener(null);
        }

        void deliverOnProgressChangedHaptics(boolean fromUser, int progress, int hapticsType) {
            if (mHapticPlugin == null) return;
        /**
         * Deliver haptics when the progress of the slider has changed.
         *
         * @param fromUser True if the progress changed was caused by the user.
         * @param progress The progress value of the slider.
         * @return True if haptics were successfully delivered. False otherwise. This will happen
         *   if mHapticPlugin is null
         */
        boolean deliverOnProgressChangedHaptics(boolean fromUser, int progress) {
            if (mHapticPlugin == null) return false;

            mHapticPlugin.onProgressChanged(slider, progress, fromUser);
            if (!fromUser) {
                // Consider a change from program as the volume key being continuously pressed
                mHapticPlugin.onKeyDown();
            }
            mProgressHapticsType = hapticsType;
            return true;
        }
    }

+16 −41
Original line number Diff line number Diff line
@@ -65,7 +65,6 @@ import android.widget.ImageButton;
import android.widget.SeekBar;

import androidx.test.core.view.MotionEventBuilder;
import androidx.test.filters.FlakyTest;
import androidx.test.filters.SmallTest;

import com.android.internal.jank.InteractionJankMonitor;
@@ -273,54 +272,30 @@ public class VolumeDialogImplTest extends SysuiTestCase {

    @Test
    @DisableFlags(FLAG_HAPTIC_VOLUME_SLIDER)
    public void testVolumeChange_noSliderHaptics_doesNotDeliverOnProgressChangedHaptics() {
        final State shellState = createShellState();
        VolumeDialogController.StreamState musicStreamState =
                shellState.states.get(AudioSystem.STREAM_MUSIC);

        mDialog.show(SHOW_REASON_UNKNOWN);
        mTestableLooper.processMessages(1); //Only the SHOW message
        mDialog.removeDismissMessages(); // Temporarily remove the rescheduled DISMISS

        // Change the volume two times
        musicStreamState.level += 10;
        mDialog.onStateChangedH(shellState);
        musicStreamState.level += 10;
        mDialog.onStateChangedH(shellState);
    public void addSliderHaptics_withHapticsDisabled_doesNotDeliverOnProgressChangedHaptics() {
        // GIVEN that the slider haptics flag is disabled and we try to add haptics to volume rows
        mDialog.addSliderHapticsToRows();

        // expected: the type of the latest progress haptics for the stream should be DISABLED
        int type = mDialog.progressHapticsForStream(AudioSystem.STREAM_MUSIC);
        assertEquals(VolumeDialogImpl.PROGRESS_HAPTICS_DISABLED, type);
        // WHEN haptics try to be delivered to a volume stream
        boolean canDeliverHaptics =
                mDialog.canDeliverProgressHapticsToStream(AudioSystem.STREAM_MUSIC, true, 50);

        mDialog.dismiss(DISMISS_REASON_UNKNOWN); // Dismiss
        mTestableLooper.processAllMessages();
        // THEN the result is that haptics are not successfully delivered
        assertFalse(canDeliverHaptics);
    }

    @Test @FlakyTest(bugId = 329099861)
    @Test
    @EnableFlags(FLAG_HAPTIC_VOLUME_SLIDER)
    public void testVolumeChange_withSliderHaptics_deliversOnProgressChangedHapticsEagerly() {
        // create haptic plugins on the rows with the flag enabled
    public void addSliderHaptics_withHapticsEnabled_canDeliverOnProgressChangedHaptics() {
        // GIVEN that the slider haptics flag is enabled and we try to add haptics to volume rows
        mDialog.addSliderHapticsToRows();
        final State shellState = createShellState();
        VolumeDialogController.StreamState musicStreamState =
                shellState.states.get(AudioSystem.STREAM_MUSIC);

        mDialog.show(SHOW_REASON_UNKNOWN);
        mTestableLooper.processMessages(1); //Only the SHOW message
        mDialog.removeDismissMessages(); // Temporarily remove the rescheduled DISMISS

        // Change the volume two times
        musicStreamState.level += 10;
        mDialog.onStateChangedH(shellState);
        musicStreamState.level += 10;
        mDialog.onStateChangedH(shellState);
        // WHEN haptics try to be delivered to a volume stream
        boolean canDeliverHaptics =
                mDialog.canDeliverProgressHapticsToStream(AudioSystem.STREAM_MUSIC, true, 50);

        // expected: the type of the latest progress haptics for the stream should be EAGER
        int type = mDialog.progressHapticsForStream(AudioSystem.STREAM_MUSIC);
        assertEquals(VolumeDialogImpl.PROGRESS_HAPTICS_EAGER, type);

        mDialog.dismiss(DISMISS_REASON_UNKNOWN); // Dismiss
        mTestableLooper.processAllMessages();
        // THEN the result is that haptics are successfully delivered
        assertTrue(canDeliverHaptics);
    }

    @Test