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