The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings; |
| 18 | |
| 19 | import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT; |
Jean-Michel Trivi | ef0bed3 | 2009-06-05 18:37:29 -0700 | [diff] [blame] | 20 | import static android.provider.Settings.System.COMPATIBILITY_MODE; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 21 | |
| 22 | import android.content.BroadcastReceiver; |
| 23 | import android.content.ContentResolver; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
| 27 | import android.media.AudioManager; |
| 28 | import android.os.Bundle; |
| 29 | import android.os.IMountService; |
| 30 | import android.os.RemoteException; |
| 31 | import android.os.ServiceManager; |
| 32 | import android.preference.ListPreference; |
| 33 | import android.preference.Preference; |
| 34 | import android.preference.PreferenceActivity; |
| 35 | import android.preference.PreferenceScreen; |
| 36 | import android.preference.CheckBoxPreference; |
| 37 | import android.provider.Settings; |
| 38 | import android.util.Log; |
| 39 | import android.view.IWindowManager; |
Chouting Zhang | 386278a | 2009-06-24 14:25:43 -0500 | [diff] [blame] | 40 | import android.telephony.TelephonyManager; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 41 | |
| 42 | public 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 Zhang | 386278a | 2009-06-24 14:25:43 -0500 | [diff] [blame] | 48 | private static final int FALLBACK_EMERGENCY_TONE_VALUE = 0; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 49 | |
| 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"; |
| 55 | private static final String KEY_ANIMATIONS = "animations"; |
The Android Open Source Project | 648bf5f | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 56 | private static final String KEY_ACCELEROMETER = "accelerometer"; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 57 | private static final String KEY_PLAY_MEDIA_NOTIFICATION_SOUNDS = "play_media_notification_sounds"; |
Chouting Zhang | 386278a | 2009-06-24 14:25:43 -0500 | [diff] [blame] | 58 | private static final String KEY_EMERGENCY_TONE ="emergency_tone"; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 59 | |
| 60 | private CheckBoxPreference mSilent; |
| 61 | |
| 62 | private CheckBoxPreference mPlayMediaNotificationSounds; |
| 63 | |
| 64 | private IMountService mMountService = null; |
| 65 | |
| 66 | /* |
| 67 | * If we are currently in one of the silent modes (the ringer mode is set to either |
| 68 | * "silent mode" or "vibrate mode"), then toggling the "Phone vibrate" |
| 69 | * preference will switch between "silent mode" and "vibrate mode". |
| 70 | * Otherwise, it will adjust the normal ringer mode's ring or ring+vibrate |
| 71 | * setting. |
| 72 | */ |
| 73 | private CheckBoxPreference mVibrate; |
| 74 | private CheckBoxPreference mDtmfTone; |
| 75 | private CheckBoxPreference mSoundEffects; |
Dianne Hackborn | 7631539 | 2009-09-03 13:33:55 -0700 | [diff] [blame] | 76 | private ListPreference mAnimations; |
The Android Open Source Project | 648bf5f | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 77 | private CheckBoxPreference mAccelerometer; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 78 | private float[] mAnimationScales; |
| 79 | |
| 80 | private AudioManager mAudioManager; |
| 81 | |
| 82 | private IWindowManager mWindowManager; |
| 83 | |
| 84 | private BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 85 | @Override |
| 86 | public void onReceive(Context context, Intent intent) { |
| 87 | updateState(false); |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | @Override |
| 92 | protected void onCreate(Bundle savedInstanceState) { |
| 93 | super.onCreate(savedInstanceState); |
| 94 | ContentResolver resolver = getContentResolver(); |
Chouting Zhang | 386278a | 2009-06-24 14:25:43 -0500 | [diff] [blame] | 95 | int activePhoneType = TelephonyManager.getDefault().getPhoneType(); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 96 | |
| 97 | mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); |
| 98 | mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window")); |
| 99 | |
| 100 | mMountService = IMountService.Stub.asInterface(ServiceManager.getService("mount")); |
| 101 | |
| 102 | addPreferencesFromResource(R.xml.sound_and_display_settings); |
| 103 | |
Chouting Zhang | 386278a | 2009-06-24 14:25:43 -0500 | [diff] [blame] | 104 | if (TelephonyManager.PHONE_TYPE_CDMA != activePhoneType) { |
| 105 | // device is not CDMA, do not display CDMA emergency_tone |
| 106 | getPreferenceScreen().removePreference(findPreference(KEY_EMERGENCY_TONE)); |
| 107 | } |
| 108 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 109 | mSilent = (CheckBoxPreference) findPreference(KEY_SILENT); |
| 110 | mPlayMediaNotificationSounds = (CheckBoxPreference) findPreference(KEY_PLAY_MEDIA_NOTIFICATION_SOUNDS); |
| 111 | |
| 112 | mVibrate = (CheckBoxPreference) findPreference(KEY_VIBRATE); |
| 113 | mDtmfTone = (CheckBoxPreference) findPreference(KEY_DTMF_TONE); |
| 114 | mDtmfTone.setPersistent(false); |
| 115 | mDtmfTone.setChecked(Settings.System.getInt(resolver, |
| 116 | Settings.System.DTMF_TONE_WHEN_DIALING, 1) != 0); |
| 117 | mSoundEffects = (CheckBoxPreference) findPreference(KEY_SOUND_EFFECTS); |
| 118 | mSoundEffects.setPersistent(false); |
| 119 | mSoundEffects.setChecked(Settings.System.getInt(resolver, |
| 120 | Settings.System.SOUND_EFFECTS_ENABLED, 0) != 0); |
Dianne Hackborn | 7631539 | 2009-09-03 13:33:55 -0700 | [diff] [blame] | 121 | mAnimations = (ListPreference) findPreference(KEY_ANIMATIONS); |
| 122 | mAnimations.setOnPreferenceChangeListener(this); |
The Android Open Source Project | 648bf5f | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 123 | mAccelerometer = (CheckBoxPreference) findPreference(KEY_ACCELEROMETER); |
| 124 | mAccelerometer.setPersistent(false); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 125 | |
| 126 | ListPreference screenTimeoutPreference = |
| 127 | (ListPreference) findPreference(KEY_SCREEN_TIMEOUT); |
| 128 | screenTimeoutPreference.setValue(String.valueOf(Settings.System.getInt( |
| 129 | resolver, SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE))); |
| 130 | screenTimeoutPreference.setOnPreferenceChangeListener(this); |
Chouting Zhang | 386278a | 2009-06-24 14:25:43 -0500 | [diff] [blame] | 131 | |
| 132 | if (TelephonyManager.PHONE_TYPE_CDMA == activePhoneType) { |
| 133 | ListPreference emergencyTonePreference = |
| 134 | (ListPreference) findPreference(KEY_EMERGENCY_TONE); |
| 135 | emergencyTonePreference.setValue(String.valueOf(Settings.System.getInt( |
| 136 | resolver, Settings.System.EMERGENCY_TONE, FALLBACK_EMERGENCY_TONE_VALUE))); |
| 137 | emergencyTonePreference.setOnPreferenceChangeListener(this); |
| 138 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
| 142 | protected void onResume() { |
| 143 | super.onResume(); |
| 144 | |
| 145 | updateState(true); |
| 146 | |
| 147 | IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION); |
| 148 | registerReceiver(mReceiver, filter); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | protected void onPause() { |
| 153 | super.onPause(); |
| 154 | |
| 155 | unregisterReceiver(mReceiver); |
| 156 | } |
| 157 | |
| 158 | private void updateState(boolean force) { |
| 159 | final int ringerMode = mAudioManager.getRingerMode(); |
| 160 | final boolean silentOrVibrateMode = |
| 161 | ringerMode != AudioManager.RINGER_MODE_NORMAL; |
| 162 | |
| 163 | if (silentOrVibrateMode != mSilent.isChecked() || force) { |
| 164 | mSilent.setChecked(silentOrVibrateMode); |
| 165 | } |
| 166 | |
| 167 | try { |
| 168 | mPlayMediaNotificationSounds.setChecked(mMountService.getPlayNotificationSounds()); |
| 169 | } catch (RemoteException e) { |
| 170 | } |
| 171 | |
| 172 | boolean vibrateSetting; |
| 173 | if (silentOrVibrateMode) { |
| 174 | vibrateSetting = ringerMode == AudioManager.RINGER_MODE_VIBRATE; |
| 175 | } else { |
| 176 | vibrateSetting = mAudioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER) |
| 177 | == AudioManager.VIBRATE_SETTING_ON; |
| 178 | } |
| 179 | if (vibrateSetting != mVibrate.isChecked() || force) { |
| 180 | mVibrate.setChecked(vibrateSetting); |
| 181 | } |
| 182 | |
Jason Parekh | 2c9b326 | 2009-03-24 17:48:28 -0700 | [diff] [blame] | 183 | int silentModeStreams = Settings.System.getInt(getContentResolver(), |
| 184 | Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0); |
| 185 | boolean isAlarmInclSilentMode = (silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0; |
| 186 | mSilent.setSummary(isAlarmInclSilentMode ? |
| 187 | R.string.silent_mode_incl_alarm_summary : |
| 188 | R.string.silent_mode_summary); |
| 189 | |
Dianne Hackborn | 7631539 | 2009-09-03 13:33:55 -0700 | [diff] [blame] | 190 | int animations = 0; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 191 | try { |
| 192 | mAnimationScales = mWindowManager.getAnimationScales(); |
| 193 | } catch (RemoteException e) { |
| 194 | } |
| 195 | if (mAnimationScales != null) { |
Dianne Hackborn | 7631539 | 2009-09-03 13:33:55 -0700 | [diff] [blame] | 196 | if (mAnimationScales.length >= 1) { |
| 197 | animations = ((int)(mAnimationScales[0]+.5f)) % 10; |
| 198 | } |
| 199 | if (mAnimationScales.length >= 2) { |
| 200 | animations += (((int)(mAnimationScales[1]+.5f)) & 0x7) * 10; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 201 | } |
| 202 | } |
Dianne Hackborn | 7631539 | 2009-09-03 13:33:55 -0700 | [diff] [blame] | 203 | int idx = 0; |
| 204 | int best = 0; |
| 205 | CharSequence[] aents = mAnimations.getEntryValues(); |
| 206 | for (int i=0; i<aents.length; i++) { |
| 207 | int val = Integer.parseInt(aents[i].toString()); |
| 208 | if (val <= animations && val > best) { |
| 209 | best = val; |
| 210 | idx = i; |
| 211 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 212 | } |
Dianne Hackborn | 7631539 | 2009-09-03 13:33:55 -0700 | [diff] [blame] | 213 | mAnimations.setValueIndex(idx); |
| 214 | updateAnimationsSummary(mAnimations.getValue()); |
The Android Open Source Project | 648bf5f | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 215 | mAccelerometer.setChecked(Settings.System.getInt( |
| 216 | getContentResolver(), |
| 217 | Settings.System.ACCELEROMETER_ROTATION, 0) != 0); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Dianne Hackborn | 7631539 | 2009-09-03 13:33:55 -0700 | [diff] [blame] | 220 | private void updateAnimationsSummary(Object value) { |
| 221 | CharSequence[] summaries = getResources().getTextArray(R.array.animations_summaries); |
| 222 | CharSequence[] values = mAnimations.getEntryValues(); |
| 223 | for (int i=0; i<values.length; i++) { |
Marco Nelissen | 620e5f9 | 2009-09-09 09:06:40 -0700 | [diff] [blame^] | 224 | //Log.i("foo", "Comparing entry "+ values[i] + " to current " |
| 225 | // + mAnimations.getValue()); |
Dianne Hackborn | 7631539 | 2009-09-03 13:33:55 -0700 | [diff] [blame] | 226 | if (values[i].equals(value)) { |
| 227 | mAnimations.setSummary(summaries[i]); |
| 228 | break; |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
Amith Yamasani | eb72bcd | 2009-07-07 11:37:08 -0700 | [diff] [blame] | 233 | private void setRingerMode(boolean silent, boolean vibrate) { |
| 234 | if (silent) { |
| 235 | mAudioManager.setRingerMode(vibrate ? AudioManager.RINGER_MODE_VIBRATE : |
| 236 | AudioManager.RINGER_MODE_SILENT); |
| 237 | } else { |
| 238 | mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); |
| 239 | mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, |
| 240 | vibrate ? AudioManager.VIBRATE_SETTING_ON |
| 241 | : AudioManager.VIBRATE_SETTING_OFF); |
| 242 | } |
| 243 | } |
| 244 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 245 | @Override |
| 246 | public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { |
Amith Yamasani | eb72bcd | 2009-07-07 11:37:08 -0700 | [diff] [blame] | 247 | if (preference == mSilent || preference == mVibrate) { |
| 248 | setRingerMode(mSilent.isChecked(), mVibrate.isChecked()); |
| 249 | if (preference == mSilent) updateState(false); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 250 | } else if (preference == mPlayMediaNotificationSounds) { |
| 251 | try { |
| 252 | mMountService.setPlayNotificationSounds(mPlayMediaNotificationSounds.isChecked()); |
| 253 | } catch (RemoteException e) { |
| 254 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 255 | } else if (preference == mDtmfTone) { |
| 256 | Settings.System.putInt(getContentResolver(), Settings.System.DTMF_TONE_WHEN_DIALING, |
| 257 | mDtmfTone.isChecked() ? 1 : 0); |
| 258 | |
| 259 | } else if (preference == mSoundEffects) { |
| 260 | if (mSoundEffects.isChecked()) { |
| 261 | mAudioManager.loadSoundEffects(); |
| 262 | } else { |
| 263 | mAudioManager.unloadSoundEffects(); |
| 264 | } |
| 265 | Settings.System.putInt(getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED, |
| 266 | mSoundEffects.isChecked() ? 1 : 0); |
| 267 | |
The Android Open Source Project | 648bf5f | 2009-03-09 11:52:14 -0700 | [diff] [blame] | 268 | } else if (preference == mAccelerometer) { |
| 269 | Settings.System.putInt(getContentResolver(), |
| 270 | Settings.System.ACCELEROMETER_ROTATION, |
| 271 | mAccelerometer.isChecked() ? 1 : 0); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 272 | } |
| 273 | return true; |
| 274 | } |
| 275 | |
| 276 | public boolean onPreferenceChange(Preference preference, Object objValue) { |
Dianne Hackborn | 7631539 | 2009-09-03 13:33:55 -0700 | [diff] [blame] | 277 | if (KEY_ANIMATIONS.equals(preference.getKey())) { |
| 278 | try { |
| 279 | int value = Integer.parseInt((String) objValue); |
| 280 | if (mAnimationScales.length >= 1) { |
| 281 | mAnimationScales[0] = value%10; |
| 282 | } |
| 283 | if (mAnimationScales.length >= 2) { |
| 284 | mAnimationScales[1] = (value/10)%10; |
| 285 | } |
| 286 | try { |
| 287 | mWindowManager.setAnimationScales(mAnimationScales); |
| 288 | } catch (RemoteException e) { |
| 289 | } |
| 290 | updateAnimationsSummary(objValue); |
| 291 | } catch (NumberFormatException e) { |
| 292 | Log.e(TAG, "could not persist animation setting", e); |
| 293 | } |
| 294 | |
| 295 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 296 | if (KEY_SCREEN_TIMEOUT.equals(preference.getKey())) { |
| 297 | int value = Integer.parseInt((String) objValue); |
| 298 | try { |
| 299 | Settings.System.putInt(getContentResolver(), |
| 300 | SCREEN_OFF_TIMEOUT, value); |
| 301 | } catch (NumberFormatException e) { |
| 302 | Log.e(TAG, "could not persist screen timeout setting", e); |
| 303 | } |
Chouting Zhang | 386278a | 2009-06-24 14:25:43 -0500 | [diff] [blame] | 304 | } else if (KEY_EMERGENCY_TONE.equals(preference.getKey())) { |
| 305 | int value = Integer.parseInt((String) objValue); |
| 306 | try { |
| 307 | Settings.System.putInt(getContentResolver(), |
| 308 | Settings.System.EMERGENCY_TONE, value); |
| 309 | } catch (NumberFormatException e) { |
| 310 | Log.e(TAG, "could not persist emergency tone setting", e); |
| 311 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | return true; |
| 315 | } |
| 316 | |
| 317 | } |