• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     protected VolumeSeekBarPreference.Listener mVolumePreferenceListener;
40 
VolumeSeekBarPreferenceController(Context context, String key)41     public VolumeSeekBarPreferenceController(Context context, String key) {
42         super(context, key);
43         setAudioHelper(new AudioHelper(context));
44     }
45 
46     @VisibleForTesting
setAudioHelper(AudioHelper helper)47     void setAudioHelper(AudioHelper helper) {
48         mHelper = helper;
49     }
50 
setCallback(Callback callback)51     public void setCallback(Callback callback) {
52         mVolumePreferenceCallback = callback;
53     }
54 
55     @Override
displayPreference(PreferenceScreen screen)56     public void displayPreference(PreferenceScreen screen) {
57         super.displayPreference(screen);
58         if (isAvailable()) {
59             setupVolPreference(screen);
60         }
61     }
62 
setupVolPreference(PreferenceScreen screen)63     protected void setupVolPreference(PreferenceScreen screen) {
64         mPreference = screen.findPreference(getPreferenceKey());
65         mPreference.setCallback(mVolumePreferenceCallback);
66         mPreference.setListener(mVolumePreferenceListener);
67         mPreference.setStream(getAudioStream());
68         mPreference.setMuteIcon(getMuteIcon());
69     }
70 
71     @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
onResume()72     public void onResume() {
73         if (mPreference != null) {
74             mPreference.onActivityResume();
75         }
76     }
77 
78     @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
onPause()79     public void onPause() {
80         if (mPreference != null) {
81             mPreference.onActivityPause();
82         }
83     }
84 
85     @Override
getSliceHighlightMenuRes()86     public int getSliceHighlightMenuRes() {
87         return R.string.menu_key_sound;
88     }
89 
90     @Override
getSliderPosition()91     public int getSliderPosition() {
92         if (mPreference != null) {
93             return mPreference.getProgress();
94         }
95         return mHelper.getStreamVolume(getAudioStream());
96     }
97 
98     @Override
setSliderPosition(int position)99     public boolean setSliderPosition(int position) {
100         if (mPreference != null) {
101             mPreference.setProgress(position);
102         }
103         return mHelper.setStreamVolume(getAudioStream(), position);
104     }
105 
106     @Override
getMax()107     public int getMax() {
108         if (mPreference != null) {
109             return mPreference.getMax();
110         }
111         return mHelper.getMaxVolume(getAudioStream());
112     }
113 
114     @Override
getMin()115     public int getMin() {
116         if (mPreference != null) {
117             return mPreference.getMin();
118         }
119         return mHelper.getMinVolume(getAudioStream());
120     }
121 
122     /**
123      * @return the audio stream type
124      */
getAudioStream()125     public abstract int getAudioStream();
126 
getMuteIcon()127     protected abstract int getMuteIcon();
128 
129 }
130