1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.systemui.media.dialog; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.widget.SeekBar; 22 23 /** 24 * Customized SeekBar for MediaOutputDialog, apply scale between device volume and progress, to make 25 * adjustment smoother. 26 */ 27 public class MediaOutputSeekbar extends SeekBar { 28 private static final int SCALE_SIZE = 1000; 29 private static final int INITIAL_PROGRESS = 500; 30 public static final int VOLUME_PERCENTAGE_SCALE_SIZE = 100000; 31 MediaOutputSeekbar(Context context, AttributeSet attrs)32 public MediaOutputSeekbar(Context context, AttributeSet attrs) { 33 super(context, attrs); 34 setMin(0); 35 } 36 scaleProgressToVolume(int progress)37 static int scaleProgressToVolume(int progress) { 38 return progress / SCALE_SIZE; 39 } 40 scaleVolumeToProgress(int volume)41 static int scaleVolumeToProgress(int volume) { 42 return volume == 0 ? 0 : INITIAL_PROGRESS + volume * SCALE_SIZE; 43 } 44 getVolume()45 int getVolume() { 46 return getProgress() / SCALE_SIZE; 47 } 48 setVolume(int volume)49 void setVolume(int volume) { 50 setProgress(scaleVolumeToProgress(volume), true); 51 } 52 setMaxVolume(int maxVolume)53 void setMaxVolume(int maxVolume) { 54 setMax(maxVolume * SCALE_SIZE); 55 } 56 resetVolume()57 void resetVolume() { 58 setProgress(getMin()); 59 } 60 } 61