The Android Open Source Project | abc48f8 | 2008-12-17 18:06:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.media.AudioManager; |
| 21 | import android.preference.VolumePreference; |
| 22 | import android.preference.VolumePreference.SeekBarVolumizer; |
| 23 | import android.provider.Settings; |
| 24 | import android.util.AttributeSet; |
| 25 | import android.view.View; |
| 26 | import android.widget.CheckBox; |
| 27 | import android.widget.CompoundButton; |
| 28 | import android.widget.SeekBar; |
| 29 | import android.widget.TextView; |
| 30 | |
| 31 | /** |
| 32 | * Special preference type that allows configuration of both the ring volume and |
| 33 | * notification volume. |
| 34 | */ |
| 35 | public class RingerVolumePreference extends VolumePreference implements |
| 36 | CheckBox.OnCheckedChangeListener { |
| 37 | private static final String TAG = "RingerVolumePreference"; |
| 38 | |
| 39 | private CheckBox mNotificationsUseRingVolumeCheckbox; |
| 40 | private SeekBarVolumizer mNotificationSeekBarVolumizer; |
| 41 | private TextView mNotificationVolumeTitle; |
| 42 | |
| 43 | public RingerVolumePreference(Context context, AttributeSet attrs) { |
| 44 | super(context, attrs); |
| 45 | |
| 46 | // The always visible seekbar is for ring volume |
| 47 | setStreamType(AudioManager.STREAM_RING); |
| 48 | |
| 49 | setDialogLayoutResource(R.layout.preference_dialog_ringervolume); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | protected void onBindDialogView(View view) { |
| 54 | super.onBindDialogView(view); |
| 55 | |
| 56 | mNotificationsUseRingVolumeCheckbox = |
| 57 | (CheckBox) view.findViewById(R.id.same_notification_volume); |
| 58 | mNotificationsUseRingVolumeCheckbox.setOnCheckedChangeListener(this); |
| 59 | mNotificationsUseRingVolumeCheckbox.setChecked(Settings.System.getInt( |
| 60 | getContext().getContentResolver(), |
| 61 | Settings.System.NOTIFICATIONS_USE_RING_VOLUME, 1) == 1); |
| 62 | |
| 63 | final SeekBar seekBar = (SeekBar) view.findViewById(R.id.notification_volume_seekbar); |
| 64 | mNotificationSeekBarVolumizer = new SeekBarVolumizer(getContext(), seekBar, |
| 65 | AudioManager.STREAM_NOTIFICATION); |
| 66 | |
| 67 | mNotificationVolumeTitle = (TextView) view.findViewById(R.id.notification_volume_title); |
| 68 | |
| 69 | setNotificationVolumeVisibility(!mNotificationsUseRingVolumeCheckbox.isChecked()); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | protected void onDialogClosed(boolean positiveResult) { |
| 74 | super.onDialogClosed(positiveResult); |
| 75 | |
| 76 | if (!positiveResult && mNotificationSeekBarVolumizer != null) { |
| 77 | mNotificationSeekBarVolumizer.revertVolume(); |
| 78 | } |
| 79 | |
| 80 | cleanup(); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void onActivityStop() { |
| 85 | super.onActivityStop(); |
| 86 | cleanup(); |
| 87 | } |
| 88 | |
| 89 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 90 | setNotificationVolumeVisibility(!isChecked); |
| 91 | |
| 92 | Settings.System.putInt(getContext().getContentResolver(), |
| 93 | Settings.System.NOTIFICATIONS_USE_RING_VOLUME, isChecked ? 1 : 0); |
| 94 | |
| 95 | if (isChecked) { |
| 96 | // The user wants the notification to be same as ring, so do a |
| 97 | // one-time sync right now |
| 98 | AudioManager audioManager = (AudioManager) getContext() |
| 99 | .getSystemService(Context.AUDIO_SERVICE); |
| 100 | audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, |
| 101 | audioManager.getStreamVolume(AudioManager.STREAM_RING), 0); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | protected void onSampleStarting(SeekBarVolumizer volumizer) { |
| 107 | super.onSampleStarting(volumizer); |
| 108 | |
| 109 | if (mNotificationSeekBarVolumizer != null && volumizer != mNotificationSeekBarVolumizer) { |
| 110 | mNotificationSeekBarVolumizer.stopSample(); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | private void setNotificationVolumeVisibility(boolean visible) { |
| 115 | if (mNotificationSeekBarVolumizer != null) { |
| 116 | mNotificationSeekBarVolumizer.getSeekBar().setVisibility( |
| 117 | visible ? View.VISIBLE : View.GONE); |
| 118 | mNotificationVolumeTitle.setVisibility(visible ? View.VISIBLE : View.GONE); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | private void cleanup() { |
| 123 | if (mNotificationSeekBarVolumizer != null) { |
| 124 | mNotificationSeekBarVolumizer.stop(); |
| 125 | mNotificationSeekBarVolumizer = null; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | } |