1 /* 2 * Copyright (C) 2016 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.settings.notification; 18 19 import android.content.Context; 20 21 import androidx.annotation.VisibleForTesting; 22 import androidx.preference.PreferenceScreen; 23 24 import com.android.settings.R; 25 26 /** 27 * Base class for preference controller that handles VolumeSeekBarPreference 28 */ 29 public abstract class VolumeSeekBarPreferenceController extends 30 AdjustVolumeRestrictedPreferenceController { 31 32 protected VolumeSeekBarPreference mPreference; 33 protected AudioHelper mHelper; 34 protected VolumeSeekBarPreference.Listener mVolumePreferenceListener; 35 VolumeSeekBarPreferenceController(Context context, String key)36 public VolumeSeekBarPreferenceController(Context context, String key) { 37 super(context, key); 38 setAudioHelper(new AudioHelper(context)); 39 } 40 41 @VisibleForTesting setAudioHelper(AudioHelper helper)42 void setAudioHelper(AudioHelper helper) { 43 mHelper = helper; 44 } 45 46 @Override displayPreference(PreferenceScreen screen)47 public void displayPreference(PreferenceScreen screen) { 48 super.displayPreference(screen); 49 if (isAvailable()) { 50 setupVolPreference(screen); 51 } 52 } 53 setupVolPreference(PreferenceScreen screen)54 protected void setupVolPreference(PreferenceScreen screen) { 55 mPreference = screen.findPreference(getPreferenceKey()); 56 mPreference.setListener(mVolumePreferenceListener); 57 mPreference.setStream(getAudioStream()); 58 mPreference.setMuteIcon(getMuteIcon()); 59 } 60 61 @Override getSliceHighlightMenuRes()62 public int getSliceHighlightMenuRes() { 63 return R.string.menu_key_sound; 64 } 65 66 @Override getSliderPosition()67 public int getSliderPosition() { 68 if (mPreference != null) { 69 return mPreference.getProgress(); 70 } 71 return mHelper.getStreamVolume(getAudioStream()); 72 } 73 74 @Override setSliderPosition(int position)75 public boolean setSliderPosition(int position) { 76 if (mPreference != null) { 77 mPreference.setProgress(position); 78 } 79 return mHelper.setStreamVolume(getAudioStream(), position); 80 } 81 82 @Override getMax()83 public int getMax() { 84 if (mPreference != null) { 85 return mPreference.getMax(); 86 } 87 return mHelper.getMaxVolume(getAudioStream()); 88 } 89 90 @Override getMin()91 public int getMin() { 92 if (mPreference != null) { 93 return mPreference.getMin(); 94 } 95 return mHelper.getMinVolume(getAudioStream()); 96 } 97 98 /** 99 * @return the audio stream type 100 */ getAudioStream()101 public abstract int getAudioStream(); 102 getMuteIcon()103 protected abstract int getMuteIcon(); 104 105 } 106