blob: 8905b859fd7e76fc22a186a5f7b37621cc8bd585 [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;
20
21import 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;
28import android.os.IMountService;
29import android.os.RemoteException;
30import android.os.ServiceManager;
31import android.preference.ListPreference;
32import android.preference.Preference;
33import android.preference.PreferenceActivity;
34import android.preference.PreferenceScreen;
35import android.preference.CheckBoxPreference;
36import android.provider.Settings;
37import android.util.Log;
38import android.view.IWindowManager;
39
40public class SoundAndDisplaySettings extends PreferenceActivity implements
41 Preference.OnPreferenceChangeListener {
42 private static final String TAG = "SoundAndDisplaysSettings";
43
44 /** If there is no setting in the provider, use this. */
45 private static final int FALLBACK_SCREEN_TIMEOUT_VALUE = 30000;
46
47 private static final String KEY_SILENT = "silent";
48 private static final String KEY_VIBRATE = "vibrate";
49 private static final String KEY_SCREEN_TIMEOUT = "screen_timeout";
50 private static final String KEY_DTMF_TONE = "dtmf_tone";
51 private static final String KEY_SOUND_EFFECTS = "sound_effects";
52 private static final String KEY_ANIMATIONS = "animations";
The Android Open Source Project648bf5f2009-03-09 11:52:14 -070053 private static final String KEY_ACCELEROMETER = "accelerometer";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080054 private static final String KEY_PLAY_MEDIA_NOTIFICATION_SOUNDS = "play_media_notification_sounds";
55
56 private CheckBoxPreference mSilent;
57
58 private CheckBoxPreference mPlayMediaNotificationSounds;
59
60 private IMountService mMountService = null;
61
62 /*
63 * If we are currently in one of the silent modes (the ringer mode is set to either
64 * "silent mode" or "vibrate mode"), then toggling the "Phone vibrate"
65 * preference will switch between "silent mode" and "vibrate mode".
66 * Otherwise, it will adjust the normal ringer mode's ring or ring+vibrate
67 * setting.
68 */
69 private CheckBoxPreference mVibrate;
70 private CheckBoxPreference mDtmfTone;
71 private CheckBoxPreference mSoundEffects;
72 private CheckBoxPreference mAnimations;
The Android Open Source Project648bf5f2009-03-09 11:52:14 -070073 private CheckBoxPreference mAccelerometer;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080074 private float[] mAnimationScales;
75
76 private AudioManager mAudioManager;
77
78 private IWindowManager mWindowManager;
79
80 private BroadcastReceiver mReceiver = new BroadcastReceiver() {
81 @Override
82 public void onReceive(Context context, Intent intent) {
83 updateState(false);
84 }
85 };
86
87 @Override
88 protected void onCreate(Bundle savedInstanceState) {
89 super.onCreate(savedInstanceState);
90 ContentResolver resolver = getContentResolver();
91
92 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
93 mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
94
95 mMountService = IMountService.Stub.asInterface(ServiceManager.getService("mount"));
96
97 addPreferencesFromResource(R.xml.sound_and_display_settings);
98
99 mSilent = (CheckBoxPreference) findPreference(KEY_SILENT);
100 mPlayMediaNotificationSounds = (CheckBoxPreference) findPreference(KEY_PLAY_MEDIA_NOTIFICATION_SOUNDS);
101
102 mVibrate = (CheckBoxPreference) findPreference(KEY_VIBRATE);
103 mDtmfTone = (CheckBoxPreference) findPreference(KEY_DTMF_TONE);
104 mDtmfTone.setPersistent(false);
105 mDtmfTone.setChecked(Settings.System.getInt(resolver,
106 Settings.System.DTMF_TONE_WHEN_DIALING, 1) != 0);
107 mSoundEffects = (CheckBoxPreference) findPreference(KEY_SOUND_EFFECTS);
108 mSoundEffects.setPersistent(false);
109 mSoundEffects.setChecked(Settings.System.getInt(resolver,
110 Settings.System.SOUND_EFFECTS_ENABLED, 0) != 0);
111 mAnimations = (CheckBoxPreference) findPreference(KEY_ANIMATIONS);
112 mAnimations.setPersistent(false);
The Android Open Source Project648bf5f2009-03-09 11:52:14 -0700113 mAccelerometer = (CheckBoxPreference) findPreference(KEY_ACCELEROMETER);
114 mAccelerometer.setPersistent(false);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800115
116 ListPreference screenTimeoutPreference =
117 (ListPreference) findPreference(KEY_SCREEN_TIMEOUT);
118 screenTimeoutPreference.setValue(String.valueOf(Settings.System.getInt(
119 resolver, SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE)));
120 screenTimeoutPreference.setOnPreferenceChangeListener(this);
121 }
122
123 @Override
124 protected void onResume() {
125 super.onResume();
126
127 updateState(true);
128
129 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
130 registerReceiver(mReceiver, filter);
131 }
132
133 @Override
134 protected void onPause() {
135 super.onPause();
136
137 unregisterReceiver(mReceiver);
138 }
139
140 private void updateState(boolean force) {
141 final int ringerMode = mAudioManager.getRingerMode();
142 final boolean silentOrVibrateMode =
143 ringerMode != AudioManager.RINGER_MODE_NORMAL;
144
145 if (silentOrVibrateMode != mSilent.isChecked() || force) {
146 mSilent.setChecked(silentOrVibrateMode);
147 }
148
149 try {
150 mPlayMediaNotificationSounds.setChecked(mMountService.getPlayNotificationSounds());
151 } catch (RemoteException e) {
152 }
153
154 boolean vibrateSetting;
155 if (silentOrVibrateMode) {
156 vibrateSetting = ringerMode == AudioManager.RINGER_MODE_VIBRATE;
157 } else {
158 vibrateSetting = mAudioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER)
159 == AudioManager.VIBRATE_SETTING_ON;
160 }
161 if (vibrateSetting != mVibrate.isChecked() || force) {
162 mVibrate.setChecked(vibrateSetting);
163 }
164
165 boolean animations = true;
166 try {
167 mAnimationScales = mWindowManager.getAnimationScales();
168 } catch (RemoteException e) {
169 }
170 if (mAnimationScales != null) {
The Android Open Source Project72ed6fe2009-03-13 13:04:25 -0700171 // We will leave the window animations alone (always set),
172 // and only use this to change the transition animations.
173 for (int i=1; i<mAnimationScales.length; i++) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800174 if (mAnimationScales[i] == 0) {
175 animations = false;
176 break;
177 }
178 }
179 }
180 if (animations != mAnimations.isChecked() || force) {
181 mAnimations.setChecked(animations);
182 }
The Android Open Source Project648bf5f2009-03-09 11:52:14 -0700183 mAccelerometer.setChecked(Settings.System.getInt(
184 getContentResolver(),
185 Settings.System.ACCELEROMETER_ROTATION, 0) != 0);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800186 }
187
188 @Override
189 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
190
191 if (preference == mSilent) {
192 final boolean silent = mSilent.isChecked();
193 mAudioManager.setRingerMode(silent ? AudioManager.RINGER_MODE_SILENT
194 : AudioManager.RINGER_MODE_NORMAL);
195 updateState(false);
196
197 } else if (preference == mPlayMediaNotificationSounds) {
198 try {
199 mMountService.setPlayNotificationSounds(mPlayMediaNotificationSounds.isChecked());
200 } catch (RemoteException e) {
201 }
202 } else if (preference == mVibrate) {
203 final boolean vibrate = mVibrate.isChecked();
204 final boolean silent = mSilent.isChecked();
205
206 if (silent) {
207 mAudioManager.setRingerMode(vibrate ? AudioManager.RINGER_MODE_VIBRATE :
208 AudioManager.RINGER_MODE_SILENT);
209 } else {
210 mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
211 vibrate ? AudioManager.VIBRATE_SETTING_ON
212 : AudioManager.VIBRATE_SETTING_OFF);
213 }
214
215 } else if (preference == mDtmfTone) {
216 Settings.System.putInt(getContentResolver(), Settings.System.DTMF_TONE_WHEN_DIALING,
217 mDtmfTone.isChecked() ? 1 : 0);
218
219 } else if (preference == mSoundEffects) {
220 if (mSoundEffects.isChecked()) {
221 mAudioManager.loadSoundEffects();
222 } else {
223 mAudioManager.unloadSoundEffects();
224 }
225 Settings.System.putInt(getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED,
226 mSoundEffects.isChecked() ? 1 : 0);
227
228 } else if (preference == mAnimations) {
The Android Open Source Project72ed6fe2009-03-13 13:04:25 -0700229 if (mAnimationScales.length > 0) {
230 // Window animations are always on.
231 mAnimationScales[0] = 1;
232 }
233 for (int i=1; i<mAnimationScales.length; i++) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800234 mAnimationScales[i] = mAnimations.isChecked() ? 1 : 0;
235 }
236 try {
237 mWindowManager.setAnimationScales(mAnimationScales);
238 } catch (RemoteException e) {
239 }
The Android Open Source Project648bf5f2009-03-09 11:52:14 -0700240
241 } else if (preference == mAccelerometer) {
242 Settings.System.putInt(getContentResolver(),
243 Settings.System.ACCELEROMETER_ROTATION,
244 mAccelerometer.isChecked() ? 1 : 0);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800245 }
246 return true;
247 }
248
249 public boolean onPreferenceChange(Preference preference, Object objValue) {
250 if (KEY_SCREEN_TIMEOUT.equals(preference.getKey())) {
251 int value = Integer.parseInt((String) objValue);
252 try {
253 Settings.System.putInt(getContentResolver(),
254 SCREEN_OFF_TIMEOUT, value);
255 } catch (NumberFormatException e) {
256 Log.e(TAG, "could not persist screen timeout setting", e);
257 }
258 }
259
260 return true;
261 }
262
263}