blob: 67fc533b61aa16f070a99e4ceab279a1afe5b694 [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";
Amith Yamasanid2b3ab02009-12-02 13:56:05 -080063 private static final String KEY_DOCK_SETTINGS = "dock_settings";
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -080064
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080065 private CheckBoxPreference mSilent;
66
67 private CheckBoxPreference mPlayMediaNotificationSounds;
68
Amith Yamasanid2b3ab02009-12-02 13:56:05 -080069 private Preference mDockSettings;
70 private boolean mHasDockSettings;
71
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080072 private IMountService mMountService = null;
73
74 /*
75 * If we are currently in one of the silent modes (the ringer mode is set to either
76 * "silent mode" or "vibrate mode"), then toggling the "Phone vibrate"
77 * preference will switch between "silent mode" and "vibrate mode".
78 * Otherwise, it will adjust the normal ringer mode's ring or ring+vibrate
79 * setting.
80 */
81 private CheckBoxPreference mVibrate;
82 private CheckBoxPreference mDtmfTone;
83 private CheckBoxPreference mSoundEffects;
Dan Murphy22e18682009-09-22 11:47:08 -050084 private CheckBoxPreference mHapticFeedback;
Dianne Hackborn76315392009-09-03 13:33:55 -070085 private ListPreference mAnimations;
The Android Open Source Project648bf5f2009-03-09 11:52:14 -070086 private CheckBoxPreference mAccelerometer;
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -080087 private CheckBoxPreference mNotificationPulse;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080088 private float[] mAnimationScales;
89
90 private AudioManager mAudioManager;
91
92 private IWindowManager mWindowManager;
93
94 private BroadcastReceiver mReceiver = new BroadcastReceiver() {
95 @Override
96 public void onReceive(Context context, Intent intent) {
Amith Yamasanid2b3ab02009-12-02 13:56:05 -080097 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
98 updateState(false);
99 } else if (intent.getAction().equals(Intent.ACTION_DOCK_EVENT)) {
100 handleDockChange(intent);
101 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800102 }
103 };
104
Amith Yamasanid2b3ab02009-12-02 13:56:05 -0800105 private PreferenceGroup mSoundSettings;
106
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800107 @Override
108 protected void onCreate(Bundle savedInstanceState) {
109 super.onCreate(savedInstanceState);
110 ContentResolver resolver = getContentResolver();
Chouting Zhang386278a2009-06-24 14:25:43 -0500111 int activePhoneType = TelephonyManager.getDefault().getPhoneType();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800112
113 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
114 mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
115
116 mMountService = IMountService.Stub.asInterface(ServiceManager.getService("mount"));
117
118 addPreferencesFromResource(R.xml.sound_and_display_settings);
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800119
Chouting Zhang386278a2009-06-24 14:25:43 -0500120 if (TelephonyManager.PHONE_TYPE_CDMA != activePhoneType) {
121 // device is not CDMA, do not display CDMA emergency_tone
122 getPreferenceScreen().removePreference(findPreference(KEY_EMERGENCY_TONE));
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800123 }
Chouting Zhang386278a2009-06-24 14:25:43 -0500124
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800125 mSilent = (CheckBoxPreference) findPreference(KEY_SILENT);
126 mPlayMediaNotificationSounds = (CheckBoxPreference) findPreference(KEY_PLAY_MEDIA_NOTIFICATION_SOUNDS);
127
128 mVibrate = (CheckBoxPreference) findPreference(KEY_VIBRATE);
129 mDtmfTone = (CheckBoxPreference) findPreference(KEY_DTMF_TONE);
130 mDtmfTone.setPersistent(false);
131 mDtmfTone.setChecked(Settings.System.getInt(resolver,
132 Settings.System.DTMF_TONE_WHEN_DIALING, 1) != 0);
133 mSoundEffects = (CheckBoxPreference) findPreference(KEY_SOUND_EFFECTS);
134 mSoundEffects.setPersistent(false);
135 mSoundEffects.setChecked(Settings.System.getInt(resolver,
136 Settings.System.SOUND_EFFECTS_ENABLED, 0) != 0);
Dan Murphy22e18682009-09-22 11:47:08 -0500137 mHapticFeedback = (CheckBoxPreference) findPreference(KEY_HAPTIC_FEEDBACK);
138 mHapticFeedback.setPersistent(false);
139 mHapticFeedback.setChecked(Settings.System.getInt(resolver,
140 Settings.System.HAPTIC_FEEDBACK_ENABLED, 0) != 0);
Dianne Hackborn76315392009-09-03 13:33:55 -0700141 mAnimations = (ListPreference) findPreference(KEY_ANIMATIONS);
142 mAnimations.setOnPreferenceChangeListener(this);
The Android Open Source Project648bf5f2009-03-09 11:52:14 -0700143 mAccelerometer = (CheckBoxPreference) findPreference(KEY_ACCELEROMETER);
144 mAccelerometer.setPersistent(false);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800145
146 ListPreference screenTimeoutPreference =
147 (ListPreference) findPreference(KEY_SCREEN_TIMEOUT);
148 screenTimeoutPreference.setValue(String.valueOf(Settings.System.getInt(
149 resolver, SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE)));
150 screenTimeoutPreference.setOnPreferenceChangeListener(this);
Chouting Zhang386278a2009-06-24 14:25:43 -0500151
152 if (TelephonyManager.PHONE_TYPE_CDMA == activePhoneType) {
153 ListPreference emergencyTonePreference =
154 (ListPreference) findPreference(KEY_EMERGENCY_TONE);
155 emergencyTonePreference.setValue(String.valueOf(Settings.System.getInt(
156 resolver, Settings.System.EMERGENCY_TONE, FALLBACK_EMERGENCY_TONE_VALUE)));
157 emergencyTonePreference.setOnPreferenceChangeListener(this);
158 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800159
Amith Yamasanid2b3ab02009-12-02 13:56:05 -0800160 mSoundSettings = (PreferenceGroup) findPreference(KEY_SOUND_SETTINGS);
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800161 mNotificationPulse = (CheckBoxPreference)
Amith Yamasanid2b3ab02009-12-02 13:56:05 -0800162 mSoundSettings.findPreference(KEY_NOTIFICATION_PULSE);
163 if (mNotificationPulse != null &&
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800164 getResources().getBoolean(R.bool.has_intrusive_led) == false) {
Amith Yamasanid2b3ab02009-12-02 13:56:05 -0800165 mSoundSettings.removePreference(mNotificationPulse);
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800166 } else {
167 try {
168 mNotificationPulse.setChecked(Settings.System.getInt(resolver,
169 Settings.System.NOTIFICATION_LIGHT_PULSE) == 1);
170 mNotificationPulse.setOnPreferenceChangeListener(this);
171 } catch (SettingNotFoundException snfe) {
172 Log.e(TAG, Settings.System.NOTIFICATION_LIGHT_PULSE + " not found");
173 }
174 }
Amith Yamasanid2b3ab02009-12-02 13:56:05 -0800175
176 initDockSettings();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800177 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800178
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800179 @Override
180 protected void onResume() {
181 super.onResume();
182
183 updateState(true);
184
185 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
Amith Yamasanid2b3ab02009-12-02 13:56:05 -0800186 if (mHasDockSettings) {
187 if (mDockSettings != null) {
188 mSoundSettings.removePreference(mDockSettings);
189 }
190 filter.addAction(Intent.ACTION_DOCK_EVENT);
191 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800192 registerReceiver(mReceiver, filter);
193 }
194
195 @Override
196 protected void onPause() {
197 super.onPause();
198
199 unregisterReceiver(mReceiver);
200 }
201
Amith Yamasanid2b3ab02009-12-02 13:56:05 -0800202 private void initDockSettings() {
203 mDockSettings = mSoundSettings.findPreference(KEY_DOCK_SETTINGS);
204 mHasDockSettings = getResources().getBoolean(R.bool.has_dock_settings);
205 if (mDockSettings != null) {
206 mSoundSettings.removePreference(mDockSettings);
207 // Don't care even if we dock
208 if (getResources().getBoolean(R.bool.has_dock_settings) == false) {
209 mDockSettings = null;
210 }
211 }
212 }
213
214 private void handleDockChange(Intent intent) {
215 if (mHasDockSettings && mDockSettings != null) {
216 int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, 0);
217 if (dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
218 // Show dock settings item
219 mSoundSettings.addPreference(mDockSettings);
220 } else {
221 // Remove dock settings item
222 mSoundSettings.removePreference(mDockSettings);
223 }
224 }
225 }
226
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800227 private void updateState(boolean force) {
228 final int ringerMode = mAudioManager.getRingerMode();
229 final boolean silentOrVibrateMode =
230 ringerMode != AudioManager.RINGER_MODE_NORMAL;
231
232 if (silentOrVibrateMode != mSilent.isChecked() || force) {
233 mSilent.setChecked(silentOrVibrateMode);
234 }
235
236 try {
237 mPlayMediaNotificationSounds.setChecked(mMountService.getPlayNotificationSounds());
238 } catch (RemoteException e) {
239 }
240
241 boolean vibrateSetting;
242 if (silentOrVibrateMode) {
243 vibrateSetting = ringerMode == AudioManager.RINGER_MODE_VIBRATE;
244 } else {
245 vibrateSetting = mAudioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER)
246 == AudioManager.VIBRATE_SETTING_ON;
247 }
248 if (vibrateSetting != mVibrate.isChecked() || force) {
249 mVibrate.setChecked(vibrateSetting);
250 }
251
Jason Parekh2c9b3262009-03-24 17:48:28 -0700252 int silentModeStreams = Settings.System.getInt(getContentResolver(),
253 Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
254 boolean isAlarmInclSilentMode = (silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0;
255 mSilent.setSummary(isAlarmInclSilentMode ?
256 R.string.silent_mode_incl_alarm_summary :
257 R.string.silent_mode_summary);
258
Dianne Hackborn76315392009-09-03 13:33:55 -0700259 int animations = 0;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800260 try {
261 mAnimationScales = mWindowManager.getAnimationScales();
262 } catch (RemoteException e) {
263 }
264 if (mAnimationScales != null) {
Dianne Hackborn76315392009-09-03 13:33:55 -0700265 if (mAnimationScales.length >= 1) {
266 animations = ((int)(mAnimationScales[0]+.5f)) % 10;
267 }
268 if (mAnimationScales.length >= 2) {
269 animations += (((int)(mAnimationScales[1]+.5f)) & 0x7) * 10;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800270 }
271 }
Dianne Hackborn76315392009-09-03 13:33:55 -0700272 int idx = 0;
273 int best = 0;
274 CharSequence[] aents = mAnimations.getEntryValues();
275 for (int i=0; i<aents.length; i++) {
276 int val = Integer.parseInt(aents[i].toString());
277 if (val <= animations && val > best) {
278 best = val;
279 idx = i;
280 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800281 }
Dianne Hackborn76315392009-09-03 13:33:55 -0700282 mAnimations.setValueIndex(idx);
283 updateAnimationsSummary(mAnimations.getValue());
The Android Open Source Project648bf5f2009-03-09 11:52:14 -0700284 mAccelerometer.setChecked(Settings.System.getInt(
285 getContentResolver(),
286 Settings.System.ACCELEROMETER_ROTATION, 0) != 0);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800287 }
288
Dianne Hackborn76315392009-09-03 13:33:55 -0700289 private void updateAnimationsSummary(Object value) {
290 CharSequence[] summaries = getResources().getTextArray(R.array.animations_summaries);
291 CharSequence[] values = mAnimations.getEntryValues();
292 for (int i=0; i<values.length; i++) {
Marco Nelissen620e5f92009-09-09 09:06:40 -0700293 //Log.i("foo", "Comparing entry "+ values[i] + " to current "
294 // + mAnimations.getValue());
Dianne Hackborn76315392009-09-03 13:33:55 -0700295 if (values[i].equals(value)) {
296 mAnimations.setSummary(summaries[i]);
297 break;
298 }
299 }
300 }
301
Amith Yamasanieb72bcd2009-07-07 11:37:08 -0700302 private void setRingerMode(boolean silent, boolean vibrate) {
303 if (silent) {
304 mAudioManager.setRingerMode(vibrate ? AudioManager.RINGER_MODE_VIBRATE :
305 AudioManager.RINGER_MODE_SILENT);
306 } else {
307 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
308 mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
309 vibrate ? AudioManager.VIBRATE_SETTING_ON
310 : AudioManager.VIBRATE_SETTING_OFF);
311 }
312 }
313
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800314 @Override
315 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
Amith Yamasanieb72bcd2009-07-07 11:37:08 -0700316 if (preference == mSilent || preference == mVibrate) {
317 setRingerMode(mSilent.isChecked(), mVibrate.isChecked());
318 if (preference == mSilent) updateState(false);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800319 } else if (preference == mPlayMediaNotificationSounds) {
320 try {
321 mMountService.setPlayNotificationSounds(mPlayMediaNotificationSounds.isChecked());
322 } catch (RemoteException e) {
323 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800324 } else if (preference == mDtmfTone) {
325 Settings.System.putInt(getContentResolver(), Settings.System.DTMF_TONE_WHEN_DIALING,
326 mDtmfTone.isChecked() ? 1 : 0);
327
328 } else if (preference == mSoundEffects) {
329 if (mSoundEffects.isChecked()) {
330 mAudioManager.loadSoundEffects();
331 } else {
332 mAudioManager.unloadSoundEffects();
333 }
334 Settings.System.putInt(getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED,
335 mSoundEffects.isChecked() ? 1 : 0);
Dan Murphy22e18682009-09-22 11:47:08 -0500336
337 } else if (preference == mHapticFeedback) {
338 Settings.System.putInt(getContentResolver(), Settings.System.HAPTIC_FEEDBACK_ENABLED,
339 mHapticFeedback.isChecked() ? 1 : 0);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800340
The Android Open Source Project648bf5f2009-03-09 11:52:14 -0700341 } else if (preference == mAccelerometer) {
342 Settings.System.putInt(getContentResolver(),
343 Settings.System.ACCELEROMETER_ROTATION,
344 mAccelerometer.isChecked() ? 1 : 0);
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800345 } else if (preference == mNotificationPulse) {
346 boolean value = mNotificationPulse.isChecked();
347 Settings.System.putInt(getContentResolver(),
348 Settings.System.NOTIFICATION_LIGHT_PULSE, value ? 1 : 0);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800349 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800350
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800351 return true;
352 }
353
354 public boolean onPreferenceChange(Preference preference, Object objValue) {
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800355 final String key = preference.getKey();
356 if (KEY_ANIMATIONS.equals(key)) {
Dianne Hackborn76315392009-09-03 13:33:55 -0700357 try {
358 int value = Integer.parseInt((String) objValue);
359 if (mAnimationScales.length >= 1) {
360 mAnimationScales[0] = value%10;
361 }
362 if (mAnimationScales.length >= 2) {
363 mAnimationScales[1] = (value/10)%10;
364 }
365 try {
366 mWindowManager.setAnimationScales(mAnimationScales);
367 } catch (RemoteException e) {
368 }
369 updateAnimationsSummary(objValue);
370 } catch (NumberFormatException e) {
371 Log.e(TAG, "could not persist animation setting", e);
372 }
373
374 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800375 if (KEY_SCREEN_TIMEOUT.equals(key)) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800376 int value = Integer.parseInt((String) objValue);
377 try {
378 Settings.System.putInt(getContentResolver(),
379 SCREEN_OFF_TIMEOUT, value);
380 } catch (NumberFormatException e) {
381 Log.e(TAG, "could not persist screen timeout setting", e);
382 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800383 } else if (KEY_EMERGENCY_TONE.equals(key)) {
Chouting Zhang386278a2009-06-24 14:25:43 -0500384 int value = Integer.parseInt((String) objValue);
385 try {
386 Settings.System.putInt(getContentResolver(),
387 Settings.System.EMERGENCY_TONE, value);
388 } catch (NumberFormatException e) {
389 Log.e(TAG, "could not persist emergency tone setting", e);
390 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800391 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800392
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800393 return true;
394 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800395}