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.lifecycle.LifecycleObserver; 23 import androidx.lifecycle.OnLifecycleEvent; 24 import androidx.preference.PreferenceScreen; 25 26 import com.android.settings.notification.VolumeSeekBarPreference.Callback; 27 import com.android.settingslib.core.lifecycle.Lifecycle; 28 29 /** 30 * Base class for preference controller that handles VolumeSeekBarPreference 31 */ 32 public abstract class VolumeSeekBarPreferenceController extends 33 AdjustVolumeRestrictedPreferenceController implements LifecycleObserver { 34 35 protected VolumeSeekBarPreference mPreference; 36 protected VolumeSeekBarPreference.Callback mVolumePreferenceCallback; 37 protected AudioHelper mHelper; 38 VolumeSeekBarPreferenceController(Context context, String key)39 public VolumeSeekBarPreferenceController(Context context, String key) { 40 super(context, key); 41 setAudioHelper(new AudioHelper(context)); 42 } 43 44 @VisibleForTesting setAudioHelper(AudioHelper helper)45 void setAudioHelper(AudioHelper helper) { 46 mHelper = helper; 47 } 48 setCallback(Callback callback)49 public void setCallback(Callback callback) { 50 mVolumePreferenceCallback = callback; 51 } 52 53 @Override displayPreference(PreferenceScreen screen)54 public void displayPreference(PreferenceScreen screen) { 55 super.displayPreference(screen); 56 if (isAvailable()) { 57 mPreference = screen.findPreference(getPreferenceKey()); 58 mPreference.setCallback(mVolumePreferenceCallback); 59 mPreference.setStream(getAudioStream()); 60 mPreference.setMuteIcon(getMuteIcon()); 61 } 62 } 63 64 @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) onResume()65 public void onResume() { 66 if (mPreference != null) { 67 mPreference.onActivityResume(); 68 } 69 } 70 71 @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) onPause()72 public void onPause() { 73 if (mPreference != null) { 74 mPreference.onActivityPause(); 75 } 76 } 77 78 @Override getSliderPosition()79 public int getSliderPosition() { 80 if (mPreference != null) { 81 return mPreference.getProgress(); 82 } 83 return mHelper.getStreamVolume(getAudioStream()); 84 } 85 86 @Override setSliderPosition(int position)87 public boolean setSliderPosition(int position) { 88 if (mPreference != null) { 89 mPreference.setProgress(position); 90 } 91 return mHelper.setStreamVolume(getAudioStream(), position); 92 } 93 94 @Override getMax()95 public int getMax() { 96 if (mPreference != null) { 97 return mPreference.getMax(); 98 } 99 return mHelper.getMaxVolume(getAudioStream()); 100 } 101 102 @Override getMin()103 public int getMin() { 104 if (mPreference != null) { 105 return mPreference.getMin(); 106 } 107 return mHelper.getMinVolume(getAudioStream()); 108 } 109 getAudioStream()110 protected abstract int getAudioStream(); 111 getMuteIcon()112 protected abstract int getMuteIcon(); 113 114 } 115