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; |
Michael Chan | 0bd445b | 2009-12-03 00:31:59 -0800 | [diff] [blame] | 20 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 21 | import android.content.BroadcastReceiver; |
| 22 | import android.content.ContentResolver; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.IntentFilter; |
| 26 | import android.media.AudioManager; |
| 27 | import android.os.Bundle; |
| 28 | import android.os.IMountService; |
| 29 | import android.os.RemoteException; |
| 30 | import android.os.ServiceManager; |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 31 | import android.preference.CheckBoxPreference; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 32 | import android.preference.ListPreference; |
| 33 | import android.preference.Preference; |
| 34 | import android.preference.PreferenceActivity; |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 35 | import android.preference.PreferenceGroup; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 36 | import android.preference.PreferenceScreen; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 37 | import android.provider.Settings; |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 38 | import android.provider.Settings.SettingNotFoundException; |
| 39 | import android.telephony.TelephonyManager; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 40 | import android.util.Log; |
| 41 | import android.view.IWindowManager; |
| 42 | |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame^] | 43 | public class SoundSettings extends PreferenceActivity implements |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 44 | Preference.OnPreferenceChangeListener { |
| 45 | private static final String TAG = "SoundAndDisplaysSettings"; |
| 46 | |
| 47 | /** If there is no setting in the provider, use this. */ |
| 48 | private static final int FALLBACK_SCREEN_TIMEOUT_VALUE = 30000; |
Chouting Zhang | 386278a | 2009-06-24 14:25:43 -0500 | [diff] [blame] | 49 | private static final int FALLBACK_EMERGENCY_TONE_VALUE = 0; |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 50 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 51 | private static final String KEY_SILENT = "silent"; |
| 52 | private static final String KEY_VIBRATE = "vibrate"; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 53 | private static final String KEY_DTMF_TONE = "dtmf_tone"; |
| 54 | private static final String KEY_SOUND_EFFECTS = "sound_effects"; |
Dan Murphy | 22e1868 | 2009-09-22 11:47:08 -0500 | [diff] [blame] | 55 | private static final String KEY_HAPTIC_FEEDBACK = "haptic_feedback"; |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 56 | private static final String KEY_PLAY_MEDIA_NOTIFICATION_SOUNDS = |
| 57 | "play_media_notification_sounds"; |
| 58 | private static final String KEY_EMERGENCY_TONE = "emergency_tone"; |
| 59 | private static final String KEY_SOUND_SETTINGS = "sound_settings"; |
| 60 | private static final String KEY_NOTIFICATION_PULSE = "notification_pulse"; |
| 61 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 62 | private CheckBoxPreference mSilent; |
| 63 | |
| 64 | private CheckBoxPreference mPlayMediaNotificationSounds; |
| 65 | |
| 66 | private IMountService mMountService = null; |
| 67 | |
| 68 | /* |
| 69 | * If we are currently in one of the silent modes (the ringer mode is set to either |
| 70 | * "silent mode" or "vibrate mode"), then toggling the "Phone vibrate" |
| 71 | * preference will switch between "silent mode" and "vibrate mode". |
| 72 | * Otherwise, it will adjust the normal ringer mode's ring or ring+vibrate |
| 73 | * setting. |
| 74 | */ |
| 75 | private CheckBoxPreference mVibrate; |
| 76 | private CheckBoxPreference mDtmfTone; |
| 77 | private CheckBoxPreference mSoundEffects; |
Dan Murphy | 22e1868 | 2009-09-22 11:47:08 -0500 | [diff] [blame] | 78 | private CheckBoxPreference mHapticFeedback; |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 79 | private CheckBoxPreference mNotificationPulse; |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 80 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 81 | private AudioManager mAudioManager; |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 82 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 83 | private BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 84 | @Override |
| 85 | public void onReceive(Context context, Intent intent) { |
Amith Yamasani | d2b3ab0 | 2009-12-02 13:56:05 -0800 | [diff] [blame] | 86 | if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) { |
| 87 | updateState(false); |
Amith Yamasani | d2b3ab0 | 2009-12-02 13:56:05 -0800 | [diff] [blame] | 88 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 89 | } |
| 90 | }; |
| 91 | |
Amith Yamasani | d2b3ab0 | 2009-12-02 13:56:05 -0800 | [diff] [blame] | 92 | private PreferenceGroup mSoundSettings; |
| 93 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 94 | @Override |
| 95 | protected void onCreate(Bundle savedInstanceState) { |
| 96 | super.onCreate(savedInstanceState); |
| 97 | ContentResolver resolver = getContentResolver(); |
Chouting Zhang | 386278a | 2009-06-24 14:25:43 -0500 | [diff] [blame] | 98 | int activePhoneType = TelephonyManager.getDefault().getPhoneType(); |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 99 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 100 | mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 101 | |
| 102 | mMountService = IMountService.Stub.asInterface(ServiceManager.getService("mount")); |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 103 | |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame^] | 104 | addPreferencesFromResource(R.xml.sound_settings); |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 105 | |
Chouting Zhang | 386278a | 2009-06-24 14:25:43 -0500 | [diff] [blame] | 106 | if (TelephonyManager.PHONE_TYPE_CDMA != activePhoneType) { |
| 107 | // device is not CDMA, do not display CDMA emergency_tone |
| 108 | getPreferenceScreen().removePreference(findPreference(KEY_EMERGENCY_TONE)); |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 109 | } |
Chouting Zhang | 386278a | 2009-06-24 14:25:43 -0500 | [diff] [blame] | 110 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 111 | mSilent = (CheckBoxPreference) findPreference(KEY_SILENT); |
| 112 | mPlayMediaNotificationSounds = (CheckBoxPreference) findPreference(KEY_PLAY_MEDIA_NOTIFICATION_SOUNDS); |
| 113 | |
| 114 | mVibrate = (CheckBoxPreference) findPreference(KEY_VIBRATE); |
| 115 | mDtmfTone = (CheckBoxPreference) findPreference(KEY_DTMF_TONE); |
| 116 | mDtmfTone.setPersistent(false); |
| 117 | mDtmfTone.setChecked(Settings.System.getInt(resolver, |
| 118 | Settings.System.DTMF_TONE_WHEN_DIALING, 1) != 0); |
| 119 | mSoundEffects = (CheckBoxPreference) findPreference(KEY_SOUND_EFFECTS); |
| 120 | mSoundEffects.setPersistent(false); |
| 121 | mSoundEffects.setChecked(Settings.System.getInt(resolver, |
| 122 | Settings.System.SOUND_EFFECTS_ENABLED, 0) != 0); |
Dan Murphy | 22e1868 | 2009-09-22 11:47:08 -0500 | [diff] [blame] | 123 | mHapticFeedback = (CheckBoxPreference) findPreference(KEY_HAPTIC_FEEDBACK); |
| 124 | mHapticFeedback.setPersistent(false); |
| 125 | mHapticFeedback.setChecked(Settings.System.getInt(resolver, |
| 126 | Settings.System.HAPTIC_FEEDBACK_ENABLED, 0) != 0); |
Chouting Zhang | 386278a | 2009-06-24 14:25:43 -0500 | [diff] [blame] | 127 | |
| 128 | if (TelephonyManager.PHONE_TYPE_CDMA == activePhoneType) { |
| 129 | ListPreference emergencyTonePreference = |
| 130 | (ListPreference) findPreference(KEY_EMERGENCY_TONE); |
| 131 | emergencyTonePreference.setValue(String.valueOf(Settings.System.getInt( |
| 132 | resolver, Settings.System.EMERGENCY_TONE, FALLBACK_EMERGENCY_TONE_VALUE))); |
| 133 | emergencyTonePreference.setOnPreferenceChangeListener(this); |
| 134 | } |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 135 | |
Amith Yamasani | d2b3ab0 | 2009-12-02 13:56:05 -0800 | [diff] [blame] | 136 | mSoundSettings = (PreferenceGroup) findPreference(KEY_SOUND_SETTINGS); |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 137 | mNotificationPulse = (CheckBoxPreference) |
Amith Yamasani | d2b3ab0 | 2009-12-02 13:56:05 -0800 | [diff] [blame] | 138 | mSoundSettings.findPreference(KEY_NOTIFICATION_PULSE); |
| 139 | if (mNotificationPulse != null && |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 140 | getResources().getBoolean(R.bool.has_intrusive_led) == false) { |
Amith Yamasani | d2b3ab0 | 2009-12-02 13:56:05 -0800 | [diff] [blame] | 141 | mSoundSettings.removePreference(mNotificationPulse); |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 142 | } else { |
| 143 | try { |
| 144 | mNotificationPulse.setChecked(Settings.System.getInt(resolver, |
| 145 | Settings.System.NOTIFICATION_LIGHT_PULSE) == 1); |
| 146 | mNotificationPulse.setOnPreferenceChangeListener(this); |
| 147 | } catch (SettingNotFoundException snfe) { |
| 148 | Log.e(TAG, Settings.System.NOTIFICATION_LIGHT_PULSE + " not found"); |
| 149 | } |
| 150 | } |
Amith Yamasani | d2b3ab0 | 2009-12-02 13:56:05 -0800 | [diff] [blame] | 151 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 152 | } |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 153 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 154 | @Override |
| 155 | protected void onResume() { |
| 156 | super.onResume(); |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 157 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 158 | updateState(true); |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 159 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 160 | IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION); |
| 161 | registerReceiver(mReceiver, filter); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | protected void onPause() { |
| 166 | super.onPause(); |
| 167 | |
| 168 | unregisterReceiver(mReceiver); |
| 169 | } |
| 170 | |
| 171 | private void updateState(boolean force) { |
| 172 | final int ringerMode = mAudioManager.getRingerMode(); |
| 173 | final boolean silentOrVibrateMode = |
| 174 | ringerMode != AudioManager.RINGER_MODE_NORMAL; |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 175 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 176 | if (silentOrVibrateMode != mSilent.isChecked() || force) { |
| 177 | mSilent.setChecked(silentOrVibrateMode); |
| 178 | } |
| 179 | |
| 180 | try { |
| 181 | mPlayMediaNotificationSounds.setChecked(mMountService.getPlayNotificationSounds()); |
| 182 | } catch (RemoteException e) { |
| 183 | } |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 184 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 185 | boolean vibrateSetting; |
| 186 | if (silentOrVibrateMode) { |
| 187 | vibrateSetting = ringerMode == AudioManager.RINGER_MODE_VIBRATE; |
| 188 | } else { |
| 189 | vibrateSetting = mAudioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER) |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 190 | == AudioManager.VIBRATE_SETTING_ON; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 191 | } |
| 192 | if (vibrateSetting != mVibrate.isChecked() || force) { |
| 193 | mVibrate.setChecked(vibrateSetting); |
| 194 | } |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 195 | |
Jason Parekh | 2c9b326 | 2009-03-24 17:48:28 -0700 | [diff] [blame] | 196 | int silentModeStreams = Settings.System.getInt(getContentResolver(), |
| 197 | Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0); |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 198 | boolean isAlarmInclSilentMode = (silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0; |
Jason Parekh | 2c9b326 | 2009-03-24 17:48:28 -0700 | [diff] [blame] | 199 | mSilent.setSummary(isAlarmInclSilentMode ? |
| 200 | R.string.silent_mode_incl_alarm_summary : |
| 201 | R.string.silent_mode_summary); |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 202 | |
Dianne Hackborn | 7631539 | 2009-09-03 13:33:55 -0700 | [diff] [blame] | 203 | } |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 204 | |
Amith Yamasani | eb72bcd | 2009-07-07 11:37:08 -0700 | [diff] [blame] | 205 | private void setRingerMode(boolean silent, boolean vibrate) { |
| 206 | if (silent) { |
| 207 | mAudioManager.setRingerMode(vibrate ? AudioManager.RINGER_MODE_VIBRATE : |
| 208 | AudioManager.RINGER_MODE_SILENT); |
| 209 | } else { |
| 210 | mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); |
| 211 | mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, |
| 212 | vibrate ? AudioManager.VIBRATE_SETTING_ON |
| 213 | : AudioManager.VIBRATE_SETTING_OFF); |
| 214 | } |
| 215 | } |
| 216 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 217 | @Override |
| 218 | public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { |
Amith Yamasani | eb72bcd | 2009-07-07 11:37:08 -0700 | [diff] [blame] | 219 | if (preference == mSilent || preference == mVibrate) { |
| 220 | setRingerMode(mSilent.isChecked(), mVibrate.isChecked()); |
| 221 | if (preference == mSilent) updateState(false); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 222 | } else if (preference == mPlayMediaNotificationSounds) { |
| 223 | try { |
| 224 | mMountService.setPlayNotificationSounds(mPlayMediaNotificationSounds.isChecked()); |
| 225 | } catch (RemoteException e) { |
| 226 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 227 | } else if (preference == mDtmfTone) { |
| 228 | Settings.System.putInt(getContentResolver(), Settings.System.DTMF_TONE_WHEN_DIALING, |
| 229 | mDtmfTone.isChecked() ? 1 : 0); |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 230 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 231 | } else if (preference == mSoundEffects) { |
| 232 | if (mSoundEffects.isChecked()) { |
| 233 | mAudioManager.loadSoundEffects(); |
| 234 | } else { |
| 235 | mAudioManager.unloadSoundEffects(); |
| 236 | } |
| 237 | Settings.System.putInt(getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED, |
| 238 | mSoundEffects.isChecked() ? 1 : 0); |
Dan Murphy | 22e1868 | 2009-09-22 11:47:08 -0500 | [diff] [blame] | 239 | |
| 240 | } else if (preference == mHapticFeedback) { |
| 241 | Settings.System.putInt(getContentResolver(), Settings.System.HAPTIC_FEEDBACK_ENABLED, |
| 242 | mHapticFeedback.isChecked() ? 1 : 0); |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 243 | |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 244 | } else if (preference == mNotificationPulse) { |
| 245 | boolean value = mNotificationPulse.isChecked(); |
| 246 | Settings.System.putInt(getContentResolver(), |
| 247 | Settings.System.NOTIFICATION_LIGHT_PULSE, value ? 1 : 0); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 248 | } |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 249 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 250 | return true; |
| 251 | } |
| 252 | |
| 253 | public boolean onPreferenceChange(Preference preference, Object objValue) { |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 254 | final String key = preference.getKey(); |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame^] | 255 | if (KEY_EMERGENCY_TONE.equals(key)) { |
Chouting Zhang | 386278a | 2009-06-24 14:25:43 -0500 | [diff] [blame] | 256 | int value = Integer.parseInt((String) objValue); |
| 257 | try { |
| 258 | Settings.System.putInt(getContentResolver(), |
| 259 | Settings.System.EMERGENCY_TONE, value); |
| 260 | } catch (NumberFormatException e) { |
| 261 | Log.e(TAG, "could not persist emergency tone setting", e); |
| 262 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 263 | } |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 264 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 265 | return true; |
| 266 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 267 | } |