blob: c7bfe71558e0047d5b4354be7921e79145ad0505 [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);
109 mVibrateInSilent = (CheckBoxPreference) findPreference(KEY_VIBRATE_IN_SILENT);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800110 mDtmfTone = (CheckBoxPreference) findPreference(KEY_DTMF_TONE);
111 mDtmfTone.setPersistent(false);
112 mDtmfTone.setChecked(Settings.System.getInt(resolver,
113 Settings.System.DTMF_TONE_WHEN_DIALING, 1) != 0);
114 mSoundEffects = (CheckBoxPreference) findPreference(KEY_SOUND_EFFECTS);
115 mSoundEffects.setPersistent(false);
116 mSoundEffects.setChecked(Settings.System.getInt(resolver,
117 Settings.System.SOUND_EFFECTS_ENABLED, 0) != 0);
Dan Murphy22e18682009-09-22 11:47:08 -0500118 mHapticFeedback = (CheckBoxPreference) findPreference(KEY_HAPTIC_FEEDBACK);
119 mHapticFeedback.setPersistent(false);
120 mHapticFeedback.setChecked(Settings.System.getInt(resolver,
121 Settings.System.HAPTIC_FEEDBACK_ENABLED, 0) != 0);
Daniel Sandler231d8802010-02-17 15:35:21 -0500122 mLockSounds = (CheckBoxPreference) findPreference(KEY_LOCK_SOUNDS);
123 mLockSounds.setPersistent(false);
124 mLockSounds.setChecked(Settings.System.getInt(resolver,
125 Settings.System.LOCKSCREEN_SOUNDS_ENABLED, 1) != 0);
Chouting Zhang386278a2009-06-24 14:25:43 -0500126
127 if (TelephonyManager.PHONE_TYPE_CDMA == activePhoneType) {
128 ListPreference emergencyTonePreference =
129 (ListPreference) findPreference(KEY_EMERGENCY_TONE);
130 emergencyTonePreference.setValue(String.valueOf(Settings.System.getInt(
131 resolver, Settings.System.EMERGENCY_TONE, FALLBACK_EMERGENCY_TONE_VALUE)));
132 emergencyTonePreference.setOnPreferenceChangeListener(this);
133 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800134
Amith Yamasanid2b3ab02009-12-02 13:56:05 -0800135 mSoundSettings = (PreferenceGroup) findPreference(KEY_SOUND_SETTINGS);
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800136 mNotificationPulse = (CheckBoxPreference)
Amith Yamasanid2b3ab02009-12-02 13:56:05 -0800137 mSoundSettings.findPreference(KEY_NOTIFICATION_PULSE);
138 if (mNotificationPulse != null &&
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800139 getResources().getBoolean(R.bool.has_intrusive_led) == false) {
Amith Yamasanid2b3ab02009-12-02 13:56:05 -0800140 mSoundSettings.removePreference(mNotificationPulse);
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800141 } else {
142 try {
143 mNotificationPulse.setChecked(Settings.System.getInt(resolver,
144 Settings.System.NOTIFICATION_LIGHT_PULSE) == 1);
145 mNotificationPulse.setOnPreferenceChangeListener(this);
146 } catch (SettingNotFoundException snfe) {
147 Log.e(TAG, Settings.System.NOTIFICATION_LIGHT_PULSE + " not found");
148 }
149 }
Amith Yamasanid2b3ab02009-12-02 13:56:05 -0800150
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800151 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800152
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800153 @Override
154 protected void onResume() {
155 super.onResume();
Michael Chandf9504e2009-12-06 01:04:04 -0800156
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800157 updateState(true);
Michael Chandf9504e2009-12-06 01:04:04 -0800158
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800159 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
160 registerReceiver(mReceiver, filter);
161 }
162
163 @Override
164 protected void onPause() {
165 super.onPause();
166
167 unregisterReceiver(mReceiver);
168 }
169
Daniel Sandler58cf8292010-02-26 15:25:04 -0500170 // updateState in fact updates the UI to reflect the system state
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800171 private void updateState(boolean force) {
172 final int ringerMode = mAudioManager.getRingerMode();
Daniel Sandler58cf8292010-02-26 15:25:04 -0500173
174 // NB: in the UI we now simply call this "silent mode". A separate
175 // setting controls whether we're in RINGER_MODE_SILENT or
176 // RINGER_MODE_VIBRATE.
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800177 final boolean silentOrVibrateMode =
178 ringerMode != AudioManager.RINGER_MODE_NORMAL;
Michael Chandf9504e2009-12-06 01:04:04 -0800179
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800180 if (silentOrVibrateMode != mSilent.isChecked() || force) {
181 mSilent.setChecked(silentOrVibrateMode);
182 }
183
Daniel Sandler58cf8292010-02-26 15:25:04 -0500184 mVibrateInSilent.setEnabled(silentOrVibrateMode);
185 mVibrateInSilent.setChecked(Settings.System.getInt(
186 getContentResolver(),
187 Settings.System.VIBRATE_IN_SILENT,
188 1) == 1);
189
190 // Control phone vibe independent of silent mode
191 String phoneVibrateSetting = String.valueOf(
192 mAudioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER));
193
194 if (! phoneVibrateSetting.equals(mPhoneVibrate.getValue()) || force) {
195 mPhoneVibrate.setValue(phoneVibrateSetting);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800196 }
Michael Chandf9504e2009-12-06 01:04:04 -0800197
Jason Parekh2c9b3262009-03-24 17:48:28 -0700198 int silentModeStreams = Settings.System.getInt(getContentResolver(),
199 Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
Michael Chandf9504e2009-12-06 01:04:04 -0800200 boolean isAlarmInclSilentMode = (silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0;
Jason Parekh2c9b3262009-03-24 17:48:28 -0700201 mSilent.setSummary(isAlarmInclSilentMode ?
202 R.string.silent_mode_incl_alarm_summary :
203 R.string.silent_mode_summary);
Michael Chandf9504e2009-12-06 01:04:04 -0800204
Dianne Hackborn76315392009-09-03 13:33:55 -0700205 }
Michael Chandf9504e2009-12-06 01:04:04 -0800206
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800207 @Override
208 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
Daniel Sandler58cf8292010-02-26 15:25:04 -0500209 if (preference == mSilent) {
210 if (mSilent.isChecked()) {
211 boolean vibeInSilent = mVibrateInSilent.isChecked();
212 mAudioManager.setRingerMode(
213 vibeInSilent ? AudioManager.RINGER_MODE_VIBRATE
214 : AudioManager.RINGER_MODE_SILENT);
215 } else {
216 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
217 }
218 updateState(false);
219 } else if (preference == mPhoneVibrate) {
220 int vibeSetting = new Integer(mPhoneVibrate.getValue()).intValue();
221 switch (vibeSetting) {
222 case AudioManager.VIBRATE_SETTING_ON:
223 case AudioManager.VIBRATE_SETTING_OFF:
224 case AudioManager.VIBRATE_SETTING_ONLY_SILENT:
225 mAudioManager.setVibrateSetting(
226 AudioManager.VIBRATE_TYPE_RINGER,
227 vibeSetting);
228 updateState(false);
229 break;
230 }
231 } else if (preference == mVibrateInSilent) {
232 boolean vibeInSilent = mVibrateInSilent.isChecked();
233 Settings.System.putInt(getContentResolver(),
234 Settings.System.VIBRATE_IN_SILENT,
235 vibeInSilent ? 1 : 0);
236 int ringerMode = mAudioManager.getRingerMode();
237 if (ringerMode != AudioManager.RINGER_MODE_NORMAL) {
238 mAudioManager.setRingerMode(vibeInSilent
239 ? AudioManager.RINGER_MODE_VIBRATE
240 : AudioManager.RINGER_MODE_SILENT);
241 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800242 } else if (preference == mDtmfTone) {
243 Settings.System.putInt(getContentResolver(), Settings.System.DTMF_TONE_WHEN_DIALING,
244 mDtmfTone.isChecked() ? 1 : 0);
Michael Chandf9504e2009-12-06 01:04:04 -0800245
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800246 } else if (preference == mSoundEffects) {
247 if (mSoundEffects.isChecked()) {
248 mAudioManager.loadSoundEffects();
249 } else {
250 mAudioManager.unloadSoundEffects();
251 }
252 Settings.System.putInt(getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED,
253 mSoundEffects.isChecked() ? 1 : 0);
Dan Murphy22e18682009-09-22 11:47:08 -0500254
255 } else if (preference == mHapticFeedback) {
256 Settings.System.putInt(getContentResolver(), Settings.System.HAPTIC_FEEDBACK_ENABLED,
257 mHapticFeedback.isChecked() ? 1 : 0);
Michael Chandf9504e2009-12-06 01:04:04 -0800258
Daniel Sandler231d8802010-02-17 15:35:21 -0500259 } else if (preference == mLockSounds) {
260 Settings.System.putInt(getContentResolver(), Settings.System.LOCKSCREEN_SOUNDS_ENABLED,
261 mLockSounds.isChecked() ? 1 : 0);
262
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800263 } else if (preference == mNotificationPulse) {
264 boolean value = mNotificationPulse.isChecked();
265 Settings.System.putInt(getContentResolver(),
266 Settings.System.NOTIFICATION_LIGHT_PULSE, value ? 1 : 0);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800267 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800268
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800269 return true;
270 }
271
272 public boolean onPreferenceChange(Preference preference, Object objValue) {
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800273 final String key = preference.getKey();
Amith Yamasani992f1022010-01-25 09:17:53 -0800274 if (KEY_EMERGENCY_TONE.equals(key)) {
Chouting Zhang386278a2009-06-24 14:25:43 -0500275 int value = Integer.parseInt((String) objValue);
276 try {
277 Settings.System.putInt(getContentResolver(),
278 Settings.System.EMERGENCY_TONE, value);
279 } catch (NumberFormatException e) {
280 Log.e(TAG, "could not persist emergency tone setting", e);
281 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800282 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800283
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800284 return true;
285 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800286}