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