blob: c1f773c70270e17fa0d4ab40d06cdbc486013f0f [file] [log] [blame]
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001/*
2 * Copyright (C) 2007 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;
18
19import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
Michael Chan0bd445b2009-12-03 00:31:59 -080020
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080021import android.content.BroadcastReceiver;
22import android.content.ContentResolver;
23import android.content.Context;
24import android.content.Intent;
25import android.content.IntentFilter;
26import android.media.AudioManager;
27import android.os.Bundle;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080028import android.os.RemoteException;
29import android.os.ServiceManager;
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -080030import android.preference.CheckBoxPreference;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080031import android.preference.ListPreference;
32import android.preference.Preference;
33import android.preference.PreferenceActivity;
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -080034import android.preference.PreferenceGroup;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080035import android.preference.PreferenceScreen;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080036import android.provider.Settings;
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -080037import android.provider.Settings.SettingNotFoundException;
38import android.telephony.TelephonyManager;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080039import android.util.Log;
40import android.view.IWindowManager;
41
Amith Yamasani992f1022010-01-25 09:17:53 -080042public class SoundSettings extends PreferenceActivity implements
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080043 Preference.OnPreferenceChangeListener {
44 private static final String TAG = "SoundAndDisplaysSettings";
45
46 /** If there is no setting in the provider, use this. */
47 private static final int FALLBACK_SCREEN_TIMEOUT_VALUE = 30000;
Chouting Zhang386278a2009-06-24 14:25:43 -050048 private static final int FALLBACK_EMERGENCY_TONE_VALUE = 0;
Michael Chandf9504e2009-12-06 01:04:04 -080049
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080050 private static final String KEY_SILENT = "silent";
51 private static final String KEY_VIBRATE = "vibrate";
Daniel Sandler58cf8292010-02-26 15:25:04 -050052 private static final String KEY_VIBRATE_IN_SILENT = "vibrate_in_silent";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080053 private static final String KEY_DTMF_TONE = "dtmf_tone";
54 private static final String KEY_SOUND_EFFECTS = "sound_effects";
Dan Murphy22e18682009-09-22 11:47:08 -050055 private static final String KEY_HAPTIC_FEEDBACK = "haptic_feedback";
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -080056 private static final String KEY_EMERGENCY_TONE = "emergency_tone";
57 private static final String KEY_SOUND_SETTINGS = "sound_settings";
58 private static final String KEY_NOTIFICATION_PULSE = "notification_pulse";
Daniel Sandler231d8802010-02-17 15:35:21 -050059 private static final String KEY_LOCK_SOUNDS = "lock_sounds";
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -080060
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080061 private CheckBoxPreference mSilent;
62
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080063 /*
64 * If we are currently in one of the silent modes (the ringer mode is set to either
65 * "silent mode" or "vibrate mode"), then toggling the "Phone vibrate"
66 * preference will switch between "silent mode" and "vibrate mode".
67 * Otherwise, it will adjust the normal ringer mode's ring or ring+vibrate
68 * setting.
69 */
Daniel Sandler58cf8292010-02-26 15:25:04 -050070 private ListPreference mPhoneVibrate;
71 private CheckBoxPreference mVibrateInSilent;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080072 private CheckBoxPreference mDtmfTone;
73 private CheckBoxPreference mSoundEffects;
Dan Murphy22e18682009-09-22 11:47:08 -050074 private CheckBoxPreference mHapticFeedback;
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -080075 private CheckBoxPreference mNotificationPulse;
Daniel Sandler231d8802010-02-17 15:35:21 -050076 private CheckBoxPreference mLockSounds;
Michael Chandf9504e2009-12-06 01:04:04 -080077
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080078 private AudioManager mAudioManager;
Michael Chandf9504e2009-12-06 01:04:04 -080079
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080080 private BroadcastReceiver mReceiver = new BroadcastReceiver() {
81 @Override
82 public void onReceive(Context context, Intent intent) {
Amith Yamasanid2b3ab02009-12-02 13:56:05 -080083 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
84 updateState(false);
Amith Yamasanid2b3ab02009-12-02 13:56:05 -080085 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080086 }
87 };
88
Amith Yamasanid2b3ab02009-12-02 13:56:05 -080089 private PreferenceGroup mSoundSettings;
90
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080091 @Override
92 protected void onCreate(Bundle savedInstanceState) {
93 super.onCreate(savedInstanceState);
94 ContentResolver resolver = getContentResolver();
Chouting Zhang386278a2009-06-24 14:25:43 -050095 int activePhoneType = TelephonyManager.getDefault().getPhoneType();
Michael Chandf9504e2009-12-06 01:04:04 -080096
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080097 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080098
Amith Yamasani992f1022010-01-25 09:17:53 -080099 addPreferencesFromResource(R.xml.sound_settings);
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800100
Chouting Zhang386278a2009-06-24 14:25:43 -0500101 if (TelephonyManager.PHONE_TYPE_CDMA != activePhoneType) {
102 // device is not CDMA, do not display CDMA emergency_tone
103 getPreferenceScreen().removePreference(findPreference(KEY_EMERGENCY_TONE));
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800104 }
Chouting Zhang386278a2009-06-24 14:25:43 -0500105
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800106 mSilent = (CheckBoxPreference) findPreference(KEY_SILENT);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800107
Daniel Sandler58cf8292010-02-26 15:25:04 -0500108 mPhoneVibrate = (ListPreference) findPreference(KEY_VIBRATE);
Daniel Sandler4ce4f752010-03-08 15:08:34 -0500109 mPhoneVibrate.setOnPreferenceChangeListener(this);
110
Daniel Sandler58cf8292010-02-26 15:25:04 -0500111 mVibrateInSilent = (CheckBoxPreference) findPreference(KEY_VIBRATE_IN_SILENT);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800112 mDtmfTone = (CheckBoxPreference) findPreference(KEY_DTMF_TONE);
113 mDtmfTone.setPersistent(false);
114 mDtmfTone.setChecked(Settings.System.getInt(resolver,
115 Settings.System.DTMF_TONE_WHEN_DIALING, 1) != 0);
116 mSoundEffects = (CheckBoxPreference) findPreference(KEY_SOUND_EFFECTS);
117 mSoundEffects.setPersistent(false);
118 mSoundEffects.setChecked(Settings.System.getInt(resolver,
119 Settings.System.SOUND_EFFECTS_ENABLED, 0) != 0);
Dan Murphy22e18682009-09-22 11:47:08 -0500120 mHapticFeedback = (CheckBoxPreference) findPreference(KEY_HAPTIC_FEEDBACK);
121 mHapticFeedback.setPersistent(false);
122 mHapticFeedback.setChecked(Settings.System.getInt(resolver,
123 Settings.System.HAPTIC_FEEDBACK_ENABLED, 0) != 0);
Daniel Sandler231d8802010-02-17 15:35:21 -0500124 mLockSounds = (CheckBoxPreference) findPreference(KEY_LOCK_SOUNDS);
125 mLockSounds.setPersistent(false);
126 mLockSounds.setChecked(Settings.System.getInt(resolver,
127 Settings.System.LOCKSCREEN_SOUNDS_ENABLED, 1) != 0);
Chouting Zhang386278a2009-06-24 14:25:43 -0500128
129 if (TelephonyManager.PHONE_TYPE_CDMA == activePhoneType) {
130 ListPreference emergencyTonePreference =
131 (ListPreference) findPreference(KEY_EMERGENCY_TONE);
132 emergencyTonePreference.setValue(String.valueOf(Settings.System.getInt(
133 resolver, Settings.System.EMERGENCY_TONE, FALLBACK_EMERGENCY_TONE_VALUE)));
134 emergencyTonePreference.setOnPreferenceChangeListener(this);
135 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800136
Amith Yamasanid2b3ab02009-12-02 13:56:05 -0800137 mSoundSettings = (PreferenceGroup) findPreference(KEY_SOUND_SETTINGS);
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800138 mNotificationPulse = (CheckBoxPreference)
Amith Yamasanid2b3ab02009-12-02 13:56:05 -0800139 mSoundSettings.findPreference(KEY_NOTIFICATION_PULSE);
140 if (mNotificationPulse != null &&
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800141 getResources().getBoolean(R.bool.has_intrusive_led) == false) {
Amith Yamasanid2b3ab02009-12-02 13:56:05 -0800142 mSoundSettings.removePreference(mNotificationPulse);
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800143 } else {
144 try {
145 mNotificationPulse.setChecked(Settings.System.getInt(resolver,
146 Settings.System.NOTIFICATION_LIGHT_PULSE) == 1);
147 mNotificationPulse.setOnPreferenceChangeListener(this);
148 } catch (SettingNotFoundException snfe) {
149 Log.e(TAG, Settings.System.NOTIFICATION_LIGHT_PULSE + " not found");
150 }
151 }
Amith Yamasanid2b3ab02009-12-02 13:56:05 -0800152
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800153 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800154
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800155 @Override
156 protected void onResume() {
157 super.onResume();
Michael Chandf9504e2009-12-06 01:04:04 -0800158
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800159 updateState(true);
Michael Chandf9504e2009-12-06 01:04:04 -0800160
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800161 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
162 registerReceiver(mReceiver, filter);
163 }
164
165 @Override
166 protected void onPause() {
167 super.onPause();
168
169 unregisterReceiver(mReceiver);
170 }
171
Daniel Sandler58cf8292010-02-26 15:25:04 -0500172 // updateState in fact updates the UI to reflect the system state
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800173 private void updateState(boolean force) {
174 final int ringerMode = mAudioManager.getRingerMode();
Daniel Sandler58cf8292010-02-26 15:25:04 -0500175
176 // NB: in the UI we now simply call this "silent mode". A separate
177 // setting controls whether we're in RINGER_MODE_SILENT or
178 // RINGER_MODE_VIBRATE.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800179 final boolean silentOrVibrateMode =
180 ringerMode != AudioManager.RINGER_MODE_NORMAL;
Michael Chandf9504e2009-12-06 01:04:04 -0800181
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800182 if (silentOrVibrateMode != mSilent.isChecked() || force) {
183 mSilent.setChecked(silentOrVibrateMode);
184 }
185
Daniel Sandler58cf8292010-02-26 15:25:04 -0500186 mVibrateInSilent.setEnabled(silentOrVibrateMode);
187 mVibrateInSilent.setChecked(Settings.System.getInt(
188 getContentResolver(),
189 Settings.System.VIBRATE_IN_SILENT,
190 1) == 1);
191
192 // Control phone vibe independent of silent mode
193 String phoneVibrateSetting = String.valueOf(
194 mAudioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER));
195
196 if (! phoneVibrateSetting.equals(mPhoneVibrate.getValue()) || force) {
197 mPhoneVibrate.setValue(phoneVibrateSetting);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800198 }
Michael Chandf9504e2009-12-06 01:04:04 -0800199
Jason Parekh2c9b3262009-03-24 17:48:28 -0700200 int silentModeStreams = Settings.System.getInt(getContentResolver(),
201 Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
Michael Chandf9504e2009-12-06 01:04:04 -0800202 boolean isAlarmInclSilentMode = (silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0;
Jason Parekh2c9b3262009-03-24 17:48:28 -0700203 mSilent.setSummary(isAlarmInclSilentMode ?
204 R.string.silent_mode_incl_alarm_summary :
205 R.string.silent_mode_summary);
Michael Chandf9504e2009-12-06 01:04:04 -0800206
Dianne Hackborn76315392009-09-03 13:33:55 -0700207 }
Michael Chandf9504e2009-12-06 01:04:04 -0800208
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800209 @Override
210 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
Daniel Sandler58cf8292010-02-26 15:25:04 -0500211 if (preference == mSilent) {
212 if (mSilent.isChecked()) {
213 boolean vibeInSilent = mVibrateInSilent.isChecked();
214 mAudioManager.setRingerMode(
215 vibeInSilent ? AudioManager.RINGER_MODE_VIBRATE
216 : AudioManager.RINGER_MODE_SILENT);
217 } else {
218 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
219 }
220 updateState(false);
Daniel Sandler58cf8292010-02-26 15:25:04 -0500221 } else if (preference == mVibrateInSilent) {
222 boolean vibeInSilent = mVibrateInSilent.isChecked();
223 Settings.System.putInt(getContentResolver(),
224 Settings.System.VIBRATE_IN_SILENT,
225 vibeInSilent ? 1 : 0);
226 int ringerMode = mAudioManager.getRingerMode();
227 if (ringerMode != AudioManager.RINGER_MODE_NORMAL) {
228 mAudioManager.setRingerMode(vibeInSilent
229 ? AudioManager.RINGER_MODE_VIBRATE
230 : AudioManager.RINGER_MODE_SILENT);
231 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800232 } else if (preference == mDtmfTone) {
233 Settings.System.putInt(getContentResolver(), Settings.System.DTMF_TONE_WHEN_DIALING,
234 mDtmfTone.isChecked() ? 1 : 0);
Michael Chandf9504e2009-12-06 01:04:04 -0800235
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800236 } else if (preference == mSoundEffects) {
237 if (mSoundEffects.isChecked()) {
238 mAudioManager.loadSoundEffects();
239 } else {
240 mAudioManager.unloadSoundEffects();
241 }
242 Settings.System.putInt(getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED,
243 mSoundEffects.isChecked() ? 1 : 0);
Dan Murphy22e18682009-09-22 11:47:08 -0500244
245 } else if (preference == mHapticFeedback) {
246 Settings.System.putInt(getContentResolver(), Settings.System.HAPTIC_FEEDBACK_ENABLED,
247 mHapticFeedback.isChecked() ? 1 : 0);
Michael Chandf9504e2009-12-06 01:04:04 -0800248
Daniel Sandler231d8802010-02-17 15:35:21 -0500249 } else if (preference == mLockSounds) {
250 Settings.System.putInt(getContentResolver(), Settings.System.LOCKSCREEN_SOUNDS_ENABLED,
251 mLockSounds.isChecked() ? 1 : 0);
252
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800253 } else if (preference == mNotificationPulse) {
254 boolean value = mNotificationPulse.isChecked();
255 Settings.System.putInt(getContentResolver(),
256 Settings.System.NOTIFICATION_LIGHT_PULSE, value ? 1 : 0);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800257 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800258
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800259 return true;
260 }
261
262 public boolean onPreferenceChange(Preference preference, Object objValue) {
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800263 final String key = preference.getKey();
Amith Yamasani992f1022010-01-25 09:17:53 -0800264 if (KEY_EMERGENCY_TONE.equals(key)) {
Chouting Zhang386278a2009-06-24 14:25:43 -0500265 int value = Integer.parseInt((String) objValue);
266 try {
267 Settings.System.putInt(getContentResolver(),
268 Settings.System.EMERGENCY_TONE, value);
269 } catch (NumberFormatException e) {
270 Log.e(TAG, "could not persist emergency tone setting", e);
271 }
Daniel Sandler4ce4f752010-03-08 15:08:34 -0500272 } else if (preference == mPhoneVibrate) {
273 int vibeSetting = new Integer(objValue.toString()).intValue();
274 switch (vibeSetting) {
275 case AudioManager.VIBRATE_SETTING_ON:
276 case AudioManager.VIBRATE_SETTING_OFF:
277 case AudioManager.VIBRATE_SETTING_ONLY_SILENT:
278 mAudioManager.setVibrateSetting(
279 AudioManager.VIBRATE_TYPE_RINGER,
280 vibeSetting);
281 updateState(false);
282 break;
283 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800284 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800285
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800286 return true;
287 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800288}