| 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 | 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 Spurlock | 8531553 | 2014-05-27 13:12:35 -0400 | [diff] [blame] | 84 | if (mVolumizer == null) { |
| 85 | mVolumizer = new SeekBarVolumizer(mContext, mStream, sampleUri, sbvc); |
| 86 | } |
| 87 | mVolumizer.setSeekBar(mSeekBar); |
| John Spurlock | 81bef1d7 | 2014-05-16 15:50:13 -0400 | [diff] [blame] | 88 | } |
| 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 | } |