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