blob: 8c70c915ff4b921c89b02a264fcc4524f0c05075 [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;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080020import android.content.BroadcastReceiver;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.IntentFilter;
25import android.media.AudioManager;
26import android.os.Bundle;
27import android.os.IMountService;
28import 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
42public class SoundAndDisplaySettings extends PreferenceActivity implements
43 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;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080049
50 private static final String KEY_SILENT = "silent";
51 private static final String KEY_VIBRATE = "vibrate";
52 private static final String KEY_SCREEN_TIMEOUT = "screen_timeout";
53 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";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080056 private static final String KEY_ANIMATIONS = "animations";
The Android Open Source Project648bf5f2009-03-09 11:52:14 -070057 private static final String KEY_ACCELEROMETER = "accelerometer";
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -080058 private static final String KEY_PLAY_MEDIA_NOTIFICATION_SOUNDS =
59 "play_media_notification_sounds";
60 private static final String KEY_EMERGENCY_TONE = "emergency_tone";
61 private static final String KEY_SOUND_SETTINGS = "sound_settings";
62 private static final String KEY_NOTIFICATION_PULSE = "notification_pulse";
63
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080064 private CheckBoxPreference mSilent;
65
66 private CheckBoxPreference mPlayMediaNotificationSounds;
67
68 private IMountService mMountService = null;
69
70 /*
71 * If we are currently in one of the silent modes (the ringer mode is set to either
72 * "silent mode" or "vibrate mode"), then toggling the "Phone vibrate"
73 * preference will switch between "silent mode" and "vibrate mode".
74 * Otherwise, it will adjust the normal ringer mode's ring or ring+vibrate
75 * setting.
76 */
77 private CheckBoxPreference mVibrate;
78 private CheckBoxPreference mDtmfTone;
79 private CheckBoxPreference mSoundEffects;
Dan Murphy22e18682009-09-22 11:47:08 -050080 private CheckBoxPreference mHapticFeedback;
Dianne Hackborn76315392009-09-03 13:33:55 -070081 private ListPreference mAnimations;
The Android Open Source Project648bf5f2009-03-09 11:52:14 -070082 private CheckBoxPreference mAccelerometer;
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -080083 private CheckBoxPreference mNotificationPulse;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080084 private float[] mAnimationScales;
85
86 private AudioManager mAudioManager;
87
88 private IWindowManager mWindowManager;
89
90 private BroadcastReceiver mReceiver = new BroadcastReceiver() {
91 @Override
92 public void onReceive(Context context, Intent intent) {
93 updateState(false);
94 }
95 };
96
97 @Override
98 protected void onCreate(Bundle savedInstanceState) {
99 super.onCreate(savedInstanceState);
100 ContentResolver resolver = getContentResolver();
Chouting Zhang386278a2009-06-24 14:25:43 -0500101 int activePhoneType = TelephonyManager.getDefault().getPhoneType();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800102
103 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
104 mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
105
106 mMountService = IMountService.Stub.asInterface(ServiceManager.getService("mount"));
107
108 addPreferencesFromResource(R.xml.sound_and_display_settings);
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800109
Chouting Zhang386278a2009-06-24 14:25:43 -0500110 if (TelephonyManager.PHONE_TYPE_CDMA != activePhoneType) {
111 // device is not CDMA, do not display CDMA emergency_tone
112 getPreferenceScreen().removePreference(findPreference(KEY_EMERGENCY_TONE));
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800113 }
Chouting Zhang386278a2009-06-24 14:25:43 -0500114
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800115 mSilent = (CheckBoxPreference) findPreference(KEY_SILENT);
116 mPlayMediaNotificationSounds = (CheckBoxPreference) findPreference(KEY_PLAY_MEDIA_NOTIFICATION_SOUNDS);
117
118 mVibrate = (CheckBoxPreference) findPreference(KEY_VIBRATE);
119 mDtmfTone = (CheckBoxPreference) findPreference(KEY_DTMF_TONE);
120 mDtmfTone.setPersistent(false);
121 mDtmfTone.setChecked(Settings.System.getInt(resolver,
122 Settings.System.DTMF_TONE_WHEN_DIALING, 1) != 0);
123 mSoundEffects = (CheckBoxPreference) findPreference(KEY_SOUND_EFFECTS);
124 mSoundEffects.setPersistent(false);
125 mSoundEffects.setChecked(Settings.System.getInt(resolver,
126 Settings.System.SOUND_EFFECTS_ENABLED, 0) != 0);
Dan Murphy22e18682009-09-22 11:47:08 -0500127 mHapticFeedback = (CheckBoxPreference) findPreference(KEY_HAPTIC_FEEDBACK);
128 mHapticFeedback.setPersistent(false);
129 mHapticFeedback.setChecked(Settings.System.getInt(resolver,
130 Settings.System.HAPTIC_FEEDBACK_ENABLED, 0) != 0);
Dianne Hackborn76315392009-09-03 13:33:55 -0700131 mAnimations = (ListPreference) findPreference(KEY_ANIMATIONS);
132 mAnimations.setOnPreferenceChangeListener(this);
The Android Open Source Project648bf5f2009-03-09 11:52:14 -0700133 mAccelerometer = (CheckBoxPreference) findPreference(KEY_ACCELEROMETER);
134 mAccelerometer.setPersistent(false);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800135
136 ListPreference screenTimeoutPreference =
137 (ListPreference) findPreference(KEY_SCREEN_TIMEOUT);
138 screenTimeoutPreference.setValue(String.valueOf(Settings.System.getInt(
139 resolver, SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE)));
140 screenTimeoutPreference.setOnPreferenceChangeListener(this);
Chouting Zhang386278a2009-06-24 14:25:43 -0500141
142 if (TelephonyManager.PHONE_TYPE_CDMA == activePhoneType) {
143 ListPreference emergencyTonePreference =
144 (ListPreference) findPreference(KEY_EMERGENCY_TONE);
145 emergencyTonePreference.setValue(String.valueOf(Settings.System.getInt(
146 resolver, Settings.System.EMERGENCY_TONE, FALLBACK_EMERGENCY_TONE_VALUE)));
147 emergencyTonePreference.setOnPreferenceChangeListener(this);
148 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800149
150 PreferenceGroup soundSettings = (PreferenceGroup) findPreference(KEY_SOUND_SETTINGS);
151 mNotificationPulse = (CheckBoxPreference)
152 soundSettings.findPreference(KEY_NOTIFICATION_PULSE);
153 if (mNotificationPulse != null && soundSettings != null &&
154 getResources().getBoolean(R.bool.has_intrusive_led) == false) {
155 soundSettings.removePreference(mNotificationPulse);
156 } else {
157 try {
158 mNotificationPulse.setChecked(Settings.System.getInt(resolver,
159 Settings.System.NOTIFICATION_LIGHT_PULSE) == 1);
160 mNotificationPulse.setOnPreferenceChangeListener(this);
161 } catch (SettingNotFoundException snfe) {
162 Log.e(TAG, Settings.System.NOTIFICATION_LIGHT_PULSE + " not found");
163 }
164 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800165 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800166
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800167 @Override
168 protected void onResume() {
169 super.onResume();
170
171 updateState(true);
172
173 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
174 registerReceiver(mReceiver, filter);
175 }
176
177 @Override
178 protected void onPause() {
179 super.onPause();
180
181 unregisterReceiver(mReceiver);
182 }
183
184 private void updateState(boolean force) {
185 final int ringerMode = mAudioManager.getRingerMode();
186 final boolean silentOrVibrateMode =
187 ringerMode != AudioManager.RINGER_MODE_NORMAL;
188
189 if (silentOrVibrateMode != mSilent.isChecked() || force) {
190 mSilent.setChecked(silentOrVibrateMode);
191 }
192
193 try {
194 mPlayMediaNotificationSounds.setChecked(mMountService.getPlayNotificationSounds());
195 } catch (RemoteException e) {
196 }
197
198 boolean vibrateSetting;
199 if (silentOrVibrateMode) {
200 vibrateSetting = ringerMode == AudioManager.RINGER_MODE_VIBRATE;
201 } else {
202 vibrateSetting = mAudioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER)
203 == AudioManager.VIBRATE_SETTING_ON;
204 }
205 if (vibrateSetting != mVibrate.isChecked() || force) {
206 mVibrate.setChecked(vibrateSetting);
207 }
208
Jason Parekh2c9b3262009-03-24 17:48:28 -0700209 int silentModeStreams = Settings.System.getInt(getContentResolver(),
210 Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
211 boolean isAlarmInclSilentMode = (silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0;
212 mSilent.setSummary(isAlarmInclSilentMode ?
213 R.string.silent_mode_incl_alarm_summary :
214 R.string.silent_mode_summary);
215
Dianne Hackborn76315392009-09-03 13:33:55 -0700216 int animations = 0;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800217 try {
218 mAnimationScales = mWindowManager.getAnimationScales();
219 } catch (RemoteException e) {
220 }
221 if (mAnimationScales != null) {
Dianne Hackborn76315392009-09-03 13:33:55 -0700222 if (mAnimationScales.length >= 1) {
223 animations = ((int)(mAnimationScales[0]+.5f)) % 10;
224 }
225 if (mAnimationScales.length >= 2) {
226 animations += (((int)(mAnimationScales[1]+.5f)) & 0x7) * 10;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800227 }
228 }
Dianne Hackborn76315392009-09-03 13:33:55 -0700229 int idx = 0;
230 int best = 0;
231 CharSequence[] aents = mAnimations.getEntryValues();
232 for (int i=0; i<aents.length; i++) {
233 int val = Integer.parseInt(aents[i].toString());
234 if (val <= animations && val > best) {
235 best = val;
236 idx = i;
237 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800238 }
Dianne Hackborn76315392009-09-03 13:33:55 -0700239 mAnimations.setValueIndex(idx);
240 updateAnimationsSummary(mAnimations.getValue());
The Android Open Source Project648bf5f2009-03-09 11:52:14 -0700241 mAccelerometer.setChecked(Settings.System.getInt(
242 getContentResolver(),
243 Settings.System.ACCELEROMETER_ROTATION, 0) != 0);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800244 }
245
Dianne Hackborn76315392009-09-03 13:33:55 -0700246 private void updateAnimationsSummary(Object value) {
247 CharSequence[] summaries = getResources().getTextArray(R.array.animations_summaries);
248 CharSequence[] values = mAnimations.getEntryValues();
249 for (int i=0; i<values.length; i++) {
Marco Nelissen620e5f92009-09-09 09:06:40 -0700250 //Log.i("foo", "Comparing entry "+ values[i] + " to current "
251 // + mAnimations.getValue());
Dianne Hackborn76315392009-09-03 13:33:55 -0700252 if (values[i].equals(value)) {
253 mAnimations.setSummary(summaries[i]);
254 break;
255 }
256 }
257 }
258
Amith Yamasanieb72bcd2009-07-07 11:37:08 -0700259 private void setRingerMode(boolean silent, boolean vibrate) {
260 if (silent) {
261 mAudioManager.setRingerMode(vibrate ? AudioManager.RINGER_MODE_VIBRATE :
262 AudioManager.RINGER_MODE_SILENT);
263 } else {
264 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
265 mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
266 vibrate ? AudioManager.VIBRATE_SETTING_ON
267 : AudioManager.VIBRATE_SETTING_OFF);
268 }
269 }
270
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800271 @Override
272 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
Amith Yamasanieb72bcd2009-07-07 11:37:08 -0700273 if (preference == mSilent || preference == mVibrate) {
274 setRingerMode(mSilent.isChecked(), mVibrate.isChecked());
275 if (preference == mSilent) updateState(false);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800276 } else if (preference == mPlayMediaNotificationSounds) {
277 try {
278 mMountService.setPlayNotificationSounds(mPlayMediaNotificationSounds.isChecked());
279 } catch (RemoteException e) {
280 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800281 } else if (preference == mDtmfTone) {
282 Settings.System.putInt(getContentResolver(), Settings.System.DTMF_TONE_WHEN_DIALING,
283 mDtmfTone.isChecked() ? 1 : 0);
284
285 } else if (preference == mSoundEffects) {
286 if (mSoundEffects.isChecked()) {
287 mAudioManager.loadSoundEffects();
288 } else {
289 mAudioManager.unloadSoundEffects();
290 }
291 Settings.System.putInt(getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED,
292 mSoundEffects.isChecked() ? 1 : 0);
Dan Murphy22e18682009-09-22 11:47:08 -0500293
294 } else if (preference == mHapticFeedback) {
295 Settings.System.putInt(getContentResolver(), Settings.System.HAPTIC_FEEDBACK_ENABLED,
296 mHapticFeedback.isChecked() ? 1 : 0);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800297
The Android Open Source Project648bf5f2009-03-09 11:52:14 -0700298 } else if (preference == mAccelerometer) {
299 Settings.System.putInt(getContentResolver(),
300 Settings.System.ACCELEROMETER_ROTATION,
301 mAccelerometer.isChecked() ? 1 : 0);
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800302 } else if (preference == mNotificationPulse) {
303 boolean value = mNotificationPulse.isChecked();
304 Settings.System.putInt(getContentResolver(),
305 Settings.System.NOTIFICATION_LIGHT_PULSE, value ? 1 : 0);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800306 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800307
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800308 return true;
309 }
310
311 public boolean onPreferenceChange(Preference preference, Object objValue) {
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800312 final String key = preference.getKey();
313 if (KEY_ANIMATIONS.equals(key)) {
Dianne Hackborn76315392009-09-03 13:33:55 -0700314 try {
315 int value = Integer.parseInt((String) objValue);
316 if (mAnimationScales.length >= 1) {
317 mAnimationScales[0] = value%10;
318 }
319 if (mAnimationScales.length >= 2) {
320 mAnimationScales[1] = (value/10)%10;
321 }
322 try {
323 mWindowManager.setAnimationScales(mAnimationScales);
324 } catch (RemoteException e) {
325 }
326 updateAnimationsSummary(objValue);
327 } catch (NumberFormatException e) {
328 Log.e(TAG, "could not persist animation setting", e);
329 }
330
331 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800332 if (KEY_SCREEN_TIMEOUT.equals(key)) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800333 int value = Integer.parseInt((String) objValue);
334 try {
335 Settings.System.putInt(getContentResolver(),
336 SCREEN_OFF_TIMEOUT, value);
337 } catch (NumberFormatException e) {
338 Log.e(TAG, "could not persist screen timeout setting", e);
339 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800340 } else if (KEY_EMERGENCY_TONE.equals(key)) {
Chouting Zhang386278a2009-06-24 14:25:43 -0500341 int value = Integer.parseInt((String) objValue);
342 try {
343 Settings.System.putInt(getContentResolver(),
344 Settings.System.EMERGENCY_TONE, value);
345 } catch (NumberFormatException e) {
346 Log.e(TAG, "could not persist emergency tone setting", e);
347 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800348 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800349
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800350 return true;
351 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800352}