1 /* 2 * Copyright (C) 2014 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.ContentResolver; 20 import android.content.Context; 21 import android.media.AudioManager; 22 import android.net.Uri; 23 import android.preference.SeekBarVolumizer; 24 import android.support.annotation.VisibleForTesting; 25 import android.support.v7.preference.PreferenceViewHolder; 26 import android.text.TextUtils; 27 import android.util.AttributeSet; 28 import android.util.Log; 29 import android.view.View; 30 import android.widget.ImageView; 31 import android.widget.SeekBar; 32 import android.widget.TextView; 33 34 import com.android.settings.R; 35 import com.android.settings.widget.SeekBarPreference; 36 37 import java.util.Objects; 38 39 /** A slider preference that directly controls an audio stream volume (no dialog) **/ 40 public class VolumeSeekBarPreference extends SeekBarPreference { 41 private static final String TAG = "VolumeSeekBarPreference"; 42 43 private int mStream; 44 private SeekBar mSeekBar; 45 private SeekBarVolumizer mVolumizer; 46 private Callback mCallback; 47 private ImageView mIconView; 48 private TextView mSuppressionTextView; 49 private String mSuppressionText; 50 private boolean mMuted; 51 private boolean mZenMuted; 52 private int mIconResId; 53 private int mMuteIconResId; 54 private boolean mStopped; 55 @VisibleForTesting 56 AudioManager mAudioManager; 57 58 public VolumeSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr, 59 int defStyleRes) { 60 super(context, attrs, defStyleAttr, defStyleRes); 61 setLayoutResource(R.layout.preference_volume_slider); 62 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 63 } 64 65 public VolumeSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr) { 66 super(context, attrs, defStyleAttr); 67 setLayoutResource(R.layout.preference_volume_slider); 68 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 69 } 70 71 public VolumeSeekBarPreference(Context context, AttributeSet attrs) { 72 super(context, attrs); 73 setLayoutResource(R.layout.preference_volume_slider); 74 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 75 } 76 77 public VolumeSeekBarPreference(Context context) { 78 super(context); 79 setLayoutResource(R.layout.preference_volume_slider); 80 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 81 } 82 83 public void setStream(int stream) { 84 mStream = stream; 85 setMax(mAudioManager.getStreamMaxVolume(mStream)); 86 setProgress(mAudioManager.getStreamVolume(mStream)); 87 } 88 89 public void setCallback(Callback callback) { 90 mCallback = callback; 91 } 92 93 public void onActivityResume() { 94 if (mStopped) { 95 init(); 96 } 97 } 98 99 public void onActivityPause() { 100 mStopped = true; 101 if (mVolumizer != null) { 102 mVolumizer.stop(); 103 mVolumizer = null; 104 } 105 } 106 107 @Override 108 public void onBindViewHolder(PreferenceViewHolder view) { 109 super.onBindViewHolder(view); 110 if (mStream == 0) { 111 Log.w(TAG, "No stream found, not binding volumizer"); 112 return; 113 } 114 mSeekBar = (SeekBar) view.findViewById(com.android.internal.R.id.seekbar); 115 mIconView = (ImageView) view.findViewById(com.android.internal.R.id.icon); 116 mSuppressionTextView = (TextView) view.findViewById(R.id.suppression_text); 117 init(); 118 } 119 120 private void init() { 121 if (mSeekBar == null) return; 122 final SeekBarVolumizer.Callback sbvc = new SeekBarVolumizer.Callback() { 123 @Override 124 public void onSampleStarting(SeekBarVolumizer sbv) { 125 if (mCallback != null) { 126 mCallback.onSampleStarting(sbv); 127 } 128 } 129 @Override 130 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) { 131 if (mCallback != null) { 132 mCallback.onStreamValueChanged(mStream, progress); 133 } 134 } 135 @Override 136 public void onMuted(boolean muted, boolean zenMuted) { 137 if (mMuted == muted && mZenMuted == zenMuted) return; 138 mMuted = muted; 139 mZenMuted = zenMuted; 140 updateIconView(); 141 } 142 }; 143 final Uri sampleUri = mStream == AudioManager.STREAM_MUSIC ? getMediaVolumeUri() : null; 144 if (mVolumizer == null) { 145 mVolumizer = new SeekBarVolumizer(getContext(), mStream, sampleUri, sbvc); 146 } 147 mVolumizer.start(); 148 mVolumizer.setSeekBar(mSeekBar); 149 updateIconView(); 150 mCallback.onStreamValueChanged(mStream, mSeekBar.getProgress()); 151 updateSuppressionText(); 152 if (!isEnabled()) { 153 mSeekBar.setEnabled(false); 154 mVolumizer.stop(); 155 } 156 } 157 158 // during initialization, this preference is the SeekBar listener 159 @Override 160 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) { 161 super.onProgressChanged(seekBar, progress, fromTouch); 162 if (mCallback != null) { 163 mCallback.onStreamValueChanged(mStream, progress); 164 } 165 } 166 167 private void updateIconView() { 168 if (mIconView == null) return; 169 if (mIconResId != 0) { 170 mIconView.setImageResource(mIconResId); 171 } else if (mMuteIconResId != 0 && mMuted && !mZenMuted) { 172 mIconView.setImageResource(mMuteIconResId); 173 } else { 174 mIconView.setImageDrawable(getIcon()); 175 } 176 } 177 178 public void showIcon(int resId) { 179 // Instead of using setIcon, which will trigger listeners, this just decorates the 180 // preference temporarily with a new icon. 181 if (mIconResId == resId) return; 182 mIconResId = resId; 183 updateIconView(); 184 } 185 186 public void setMuteIcon(int resId) { 187 if (mMuteIconResId == resId) return; 188 mMuteIconResId = resId; 189 updateIconView(); 190 } 191 192 private Uri getMediaVolumeUri() { 193 return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" 194 + getContext().getPackageName() 195 + "/" + R.raw.media_volume); 196 } 197 198 public void setSuppressionText(String text) { 199 if (Objects.equals(text, mSuppressionText)) return; 200 mSuppressionText = text; 201 updateSuppressionText(); 202 } 203 204 private void updateSuppressionText() { 205 if (mSuppressionTextView != null && mSeekBar != null) { 206 mSuppressionTextView.setText(mSuppressionText); 207 final boolean showSuppression = !TextUtils.isEmpty(mSuppressionText); 208 mSuppressionTextView.setVisibility(showSuppression ? View.VISIBLE : View.INVISIBLE); 209 mSeekBar.setVisibility(showSuppression ? View.INVISIBLE : View.VISIBLE); 210 } 211 } 212 213 public interface Callback { 214 void onSampleStarting(SeekBarVolumizer sbv); 215 void onStreamValueChanged(int stream, int progress); 216 } 217 } 218