Align the Talkback slider percentage announcement with the UI.

- Revert ag/21912040. That CL doesn't fix b/264841237 anyway. The
problem is that `INITIAL_PROGRESS` was not used in all the conversions.
- Call `mSeekBar.setStateDescription` with the same value as
`mVolumeValueText.setText` This ensures consistency between the UI and
the Talkback announcement.

Flag: EXEMPT bugfix
Bug: 374860822
Test: locally on a physical device.
Change-Id: Ib2904d8ed4f067ff50a28ac682a6f260c428d641
diff --git a/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputBaseAdapter.java b/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputBaseAdapter.java
index 0c81966..9b24c69 100644
--- a/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputBaseAdapter.java
+++ b/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputBaseAdapter.java
@@ -16,8 +16,6 @@
 
 package com.android.systemui.media.dialog;
 
-import static com.android.systemui.media.dialog.MediaOutputSeekbar.VOLUME_PERCENTAGE_SCALE_SIZE;
-
 import android.animation.Animator;
 import android.animation.ValueAnimator;
 import android.app.WallpaperColors;
@@ -289,10 +287,7 @@
                         }
                     } else {
                         if (!mVolumeAnimator.isStarted()) {
-                            int percentage =
-                                    (int) ((double) currentVolume * VOLUME_PERCENTAGE_SCALE_SIZE
-                                            / (double) mSeekBar.getMax());
-                            if (percentage == 0) {
+                            if (currentVolume == 0) {
                                 updateMutedVolumeIcon(device);
                             } else {
                                 updateUnmutedVolumeIcon(device);
@@ -319,20 +314,20 @@
                     if (device == null || !fromUser) {
                         return;
                     }
-                    int progressToVolume = MediaOutputSeekbar.scaleProgressToVolume(progress);
-                    int deviceVolume = device.getCurrentVolume();
-                    int percentage =
-                            (int) ((double) progressToVolume * VOLUME_PERCENTAGE_SCALE_SIZE
-                                    / (double) seekBar.getMax());
-                    mVolumeValueText.setText(mContext.getResources().getString(
-                            R.string.media_output_dialog_volume_percentage, percentage));
+
+                    final String percentageString = mContext.getResources().getString(
+                            R.string.media_output_dialog_volume_percentage,
+                            mSeekBar.getPercentage());
+                    mVolumeValueText.setText(percentageString);
+
                     if (mStartFromMute) {
                         updateUnmutedVolumeIcon(device);
                         mStartFromMute = false;
                     }
-                    if (progressToVolume != deviceVolume) {
-                        mLatestUpdateVolume = progressToVolume;
-                        mController.adjustVolume(device, progressToVolume);
+                    int seekBarVolume = MediaOutputSeekbar.scaleProgressToVolume(progress);
+                    if (seekBarVolume != device.getCurrentVolume()) {
+                        mLatestUpdateVolume = seekBarVolume;
+                        mController.adjustVolume(device, seekBarVolume);
                     }
                 }
 
diff --git a/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputSeekbar.java b/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputSeekbar.java
index be5d607..b7381da 100644
--- a/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputSeekbar.java
+++ b/packages/SystemUI/src/com/android/systemui/media/dialog/MediaOutputSeekbar.java
@@ -16,22 +16,62 @@
 
 package com.android.systemui.media.dialog;
 
+import android.annotation.Nullable;
 import android.content.Context;
 import android.util.AttributeSet;
 import android.widget.SeekBar;
 
+import com.android.systemui.res.R;
+
 /**
  * Customized SeekBar for MediaOutputDialog, apply scale between device volume and progress, to make
  * adjustment smoother.
  */
 public class MediaOutputSeekbar extends SeekBar {
+    // The scale is added to make slider value change smooth.
     private static final int SCALE_SIZE = 1000;
-    private static final int INITIAL_PROGRESS = 500;
-    public static final int VOLUME_PERCENTAGE_SCALE_SIZE = 100000;
+
+    @Nullable
+    private SeekBar.OnSeekBarChangeListener mOnSeekBarChangeListener = null;
 
     public MediaOutputSeekbar(Context context, AttributeSet attrs) {
         super(context, attrs);
         setMin(0);
+        super.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
+            @Override
+            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
+                final String percentageString = context.getResources().getString(
+                        R.string.media_output_dialog_volume_percentage,
+                        getPercentage());
+                // Override the default TTS for the seekbar. The percentage should correspond to
+                // the volume value, not the progress value. I.e. for the volume range 0 - 25, the
+                // percentage should be 0%, 4%, 8%, etc. It should never be 6% since 6% doesn't map
+                // to an integer volume value.
+                setStateDescription(percentageString);
+                if (mOnSeekBarChangeListener != null) {
+                    mOnSeekBarChangeListener.onProgressChanged(seekBar, progress, fromUser);
+                }
+            }
+
+            @Override
+            public void onStartTrackingTouch(SeekBar seekBar) {
+                if (mOnSeekBarChangeListener != null) {
+                    mOnSeekBarChangeListener.onStartTrackingTouch(seekBar);
+                }
+            }
+
+            @Override
+            public void onStopTrackingTouch(SeekBar seekBar) {
+                if (mOnSeekBarChangeListener != null) {
+                    mOnSeekBarChangeListener.onStopTrackingTouch(seekBar);
+                }
+            }
+        });
+    }
+
+    @Override
+    public void setOnSeekBarChangeListener(@Nullable SeekBar.OnSeekBarChangeListener listener) {
+        mOnSeekBarChangeListener = listener;
     }
 
     static int scaleProgressToVolume(int progress) {
@@ -39,11 +79,11 @@
     }
 
     static int scaleVolumeToProgress(int volume) {
-        return volume == 0 ? 0 : INITIAL_PROGRESS + volume * SCALE_SIZE;
+        return volume * SCALE_SIZE;
     }
 
     int getVolume() {
-        return getProgress() / SCALE_SIZE;
+        return scaleProgressToVolume(getProgress());
     }
 
     void setVolume(int volume) {
@@ -51,10 +91,18 @@
     }
 
     void setMaxVolume(int maxVolume) {
-        setMax(maxVolume * SCALE_SIZE);
+        setMax(scaleVolumeToProgress(maxVolume));
     }
 
     void resetVolume() {
         setProgress(getMin());
     }
+
+    int getPercentage() {
+        // The progress -> volume -> progress conversion is necessary to ensure that progress
+        // strictly corresponds to an integer volume value.
+        // Example: 10424 (progress) -> 10 (volume) -> 10000 (progress).
+        int normalizedProgress = scaleVolumeToProgress(scaleProgressToVolume(getProgress()));
+        return (int) ((double) normalizedProgress * 100 / getMax());
+    }
 }