blob: 98891238e4353a06cbafd79b37c24f23fd32d2ad [file] [log] [blame]
John Spurlock81bef1d72014-05-16 15:50:13 -04001/*
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
17package com.android.settings.notification;
18
19import android.content.ContentResolver;
20import android.content.Context;
21import android.media.AudioManager;
22import android.net.Uri;
23import android.preference.PreferenceManager;
24import android.preference.SeekBarPreference;
25import android.preference.SeekBarVolumizer;
26import android.util.AttributeSet;
27import android.util.Log;
28import android.view.View;
29import android.widget.SeekBar;
30
31import com.android.settings.R;
32
33/** A slider preference that directly controls an audio stream volume (no dialog) **/
34public class VolumeSeekBarPreference extends SeekBarPreference
35 implements PreferenceManager.OnActivityStopListener {
36 private static final String TAG = "VolumeSeekBarPreference";
John Spurlock81bef1d72014-05-16 15:50:13 -040037 private final Context mContext;
38
39 private int mStream;
40 private SeekBar mSeekBar;
41 private SeekBarVolumizer mVolumizer;
42 private Callback mCallback;
43
44 public VolumeSeekBarPreference(Context context, AttributeSet attrs) {
45 super(context, attrs);
46 mContext = context;
47 }
48
49 public void setStream(int stream) {
50 mStream = stream;
51 }
52
53 public void setCallback(Callback callback) {
54 mCallback = callback;
55 }
56
57 @Override
58 public void onActivityStop() {
59 if (mVolumizer != null) {
60 mVolumizer.stop();
61 }
62 }
63
64 @Override
65 protected void onBindView(View view) {
66 super.onBindView(view);
67 if (mStream == 0) {
68 Log.w(TAG, "No stream found, not binding volumizer");
69 return;
70 }
71 getPreferenceManager().registerOnActivityStopListener(this);
72 final SeekBar seekBar = (SeekBar) view.findViewById(com.android.internal.R.id.seekbar);
73 if (seekBar == mSeekBar) return;
74 mSeekBar = seekBar;
75 final SeekBarVolumizer.Callback sbvc = new SeekBarVolumizer.Callback() {
76 @Override
77 public void onSampleStarting(SeekBarVolumizer sbv) {
78 if (mCallback != null) {
79 mCallback.onSampleStarting(sbv);
80 }
81 }
82 };
83 final Uri sampleUri = mStream == AudioManager.STREAM_MUSIC ? getMediaVolumeUri() : null;
John Spurlock85315532014-05-27 13:12:35 -040084 if (mVolumizer == null) {
85 mVolumizer = new SeekBarVolumizer(mContext, mStream, sampleUri, sbvc);
86 }
87 mVolumizer.setSeekBar(mSeekBar);
John Spurlock81bef1d72014-05-16 15:50:13 -040088 }
89
90 private Uri getMediaVolumeUri() {
91 return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
92 + mContext.getPackageName()
93 + "/" + R.raw.media_volume);
94 }
95
96 public interface Callback {
97 void onSampleStarting(SeekBarVolumizer sbv);
98 }
99}