blob: 11a83a759dcef637125bea05ea13e893f24504c7 [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
38 private int mStream;
39 private SeekBar mSeekBar;
40 private SeekBarVolumizer mVolumizer;
41 private Callback mCallback;
42
Fabrice Di Megliodbaf0802014-06-17 11:37:55 -070043 public VolumeSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr,
44 int defStyleRes) {
45 super(context, attrs, defStyleAttr, defStyleRes);
46 }
47
48 public VolumeSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr) {
49 this(context, attrs, defStyleAttr, 0);
50 }
51
John Spurlock81bef1d72014-05-16 15:50:13 -040052 public VolumeSeekBarPreference(Context context, AttributeSet attrs) {
Fabrice Di Megliodbaf0802014-06-17 11:37:55 -070053 this(context, attrs, 0);
54 }
55
56 public VolumeSeekBarPreference(Context context) {
57 this(context, null);
John Spurlock81bef1d72014-05-16 15:50:13 -040058 }
59
60 public void setStream(int stream) {
61 mStream = stream;
62 }
63
64 public void setCallback(Callback callback) {
65 mCallback = callback;
66 }
67
68 @Override
69 public void onActivityStop() {
70 if (mVolumizer != null) {
71 mVolumizer.stop();
72 }
73 }
74
75 @Override
76 protected void onBindView(View view) {
77 super.onBindView(view);
78 if (mStream == 0) {
79 Log.w(TAG, "No stream found, not binding volumizer");
80 return;
81 }
82 getPreferenceManager().registerOnActivityStopListener(this);
83 final SeekBar seekBar = (SeekBar) view.findViewById(com.android.internal.R.id.seekbar);
84 if (seekBar == mSeekBar) return;
85 mSeekBar = seekBar;
86 final SeekBarVolumizer.Callback sbvc = new SeekBarVolumizer.Callback() {
87 @Override
88 public void onSampleStarting(SeekBarVolumizer sbv) {
89 if (mCallback != null) {
90 mCallback.onSampleStarting(sbv);
91 }
92 }
93 };
94 final Uri sampleUri = mStream == AudioManager.STREAM_MUSIC ? getMediaVolumeUri() : null;
John Spurlock85315532014-05-27 13:12:35 -040095 if (mVolumizer == null) {
Fabrice Di Megliodbaf0802014-06-17 11:37:55 -070096 mVolumizer = new SeekBarVolumizer(getContext(), mStream, sampleUri, sbvc);
John Spurlock85315532014-05-27 13:12:35 -040097 }
98 mVolumizer.setSeekBar(mSeekBar);
John Spurlock81bef1d72014-05-16 15:50:13 -040099 }
100
101 private Uri getMediaVolumeUri() {
102 return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
Fabrice Di Megliodbaf0802014-06-17 11:37:55 -0700103 + getContext().getPackageName()
John Spurlock81bef1d72014-05-16 15:50:13 -0400104 + "/" + R.raw.media_volume);
105 }
106
107 public interface Callback {
108 void onSampleStarting(SeekBarVolumizer sbv);
109 }
110}