| John Spurlock | 81bef1d7 | 2014-05-16 15:50:13 -0400 | [diff] [blame] | 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.PreferenceManager; |
| 24 | import android.preference.SeekBarPreference; |
| 25 | import android.preference.SeekBarVolumizer; |
| 26 | import android.util.AttributeSet; |
| 27 | import android.util.Log; |
| 28 | import android.view.View; |
| 29 | import android.widget.SeekBar; |
| 30 | |
| 31 | import com.android.settings.R; |
| 32 | |
| 33 | /** A slider preference that directly controls an audio stream volume (no dialog) **/ |
| 34 | public class VolumeSeekBarPreference extends SeekBarPreference |
| 35 | implements PreferenceManager.OnActivityStopListener { |
| 36 | private static final String TAG = "VolumeSeekBarPreference"; |
| John Spurlock | 81bef1d7 | 2014-05-16 15:50:13 -0400 | [diff] [blame] | 37 | |
| 38 | private int mStream; |
| 39 | private SeekBar mSeekBar; |
| 40 | private SeekBarVolumizer mVolumizer; |
| 41 | private Callback mCallback; |
| 42 | |
| Fabrice Di Meglio | dbaf080 | 2014-06-17 11:37:55 -0700 | [diff] [blame^] | 43 | 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 Spurlock | 81bef1d7 | 2014-05-16 15:50:13 -0400 | [diff] [blame] | 52 | public VolumeSeekBarPreference(Context context, AttributeSet attrs) { |
| Fabrice Di Meglio | dbaf080 | 2014-06-17 11:37:55 -0700 | [diff] [blame^] | 53 | this(context, attrs, 0); |
| 54 | } |
| 55 | |
| 56 | public VolumeSeekBarPreference(Context context) { |
| 57 | this(context, null); |
| John Spurlock | 81bef1d7 | 2014-05-16 15:50:13 -0400 | [diff] [blame] | 58 | } |
| 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 Spurlock | 8531553 | 2014-05-27 13:12:35 -0400 | [diff] [blame] | 95 | if (mVolumizer == null) { |
| Fabrice Di Meglio | dbaf080 | 2014-06-17 11:37:55 -0700 | [diff] [blame^] | 96 | mVolumizer = new SeekBarVolumizer(getContext(), mStream, sampleUri, sbvc); |
| John Spurlock | 8531553 | 2014-05-27 13:12:35 -0400 | [diff] [blame] | 97 | } |
| 98 | mVolumizer.setSeekBar(mSeekBar); |
| John Spurlock | 81bef1d7 | 2014-05-16 15:50:13 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | private Uri getMediaVolumeUri() { |
| 102 | return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" |
| Fabrice Di Meglio | dbaf080 | 2014-06-17 11:37:55 -0700 | [diff] [blame^] | 103 | + getContext().getPackageName() |
| John Spurlock | 81bef1d7 | 2014-05-16 15:50:13 -0400 | [diff] [blame] | 104 | + "/" + R.raw.media_volume); |
| 105 | } |
| 106 | |
| 107 | public interface Callback { |
| 108 | void onSampleStarting(SeekBarVolumizer sbv); |
| 109 | } |
| 110 | } |