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 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.ContentResolver; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
Marco Nelissen | 6eca4b3 | 2011-07-15 15:02:03 -0700 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
| 25 | import android.content.pm.ResolveInfo; |
Amith Yamasani | 823bf0c | 2011-08-01 15:55:06 -0700 | [diff] [blame^] | 26 | import android.database.Cursor; |
| 27 | import android.database.sqlite.SQLiteException; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 28 | import android.media.AudioManager; |
Amith Yamasani | 823bf0c | 2011-08-01 15:55:06 -0700 | [diff] [blame^] | 29 | import android.media.Ringtone; |
| 30 | import android.media.RingtoneManager; |
Marco Nelissen | 6eca4b3 | 2011-07-15 15:02:03 -0700 | [diff] [blame] | 31 | import android.media.audiofx.AudioEffect; |
Amith Yamasani | 823bf0c | 2011-08-01 15:55:06 -0700 | [diff] [blame^] | 32 | import android.net.Uri; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 33 | import android.os.Bundle; |
Amith Yamasani | 823bf0c | 2011-08-01 15:55:06 -0700 | [diff] [blame^] | 34 | import android.os.Handler; |
| 35 | import android.os.Message; |
Dianne Hackborn | 0a115aa | 2010-11-04 11:40:31 -0700 | [diff] [blame] | 36 | import android.os.Vibrator; |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 37 | import android.preference.CheckBoxPreference; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 38 | import android.preference.ListPreference; |
| 39 | import android.preference.Preference; |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 40 | import android.preference.PreferenceGroup; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 41 | import android.preference.PreferenceScreen; |
Amith Yamasani | 823bf0c | 2011-08-01 15:55:06 -0700 | [diff] [blame^] | 42 | import android.provider.MediaStore; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 43 | import android.provider.Settings; |
Amith Yamasani | 823bf0c | 2011-08-01 15:55:06 -0700 | [diff] [blame^] | 44 | import android.provider.MediaStore.Images.Media; |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 45 | import android.provider.Settings.SettingNotFoundException; |
| 46 | import android.telephony.TelephonyManager; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 47 | import android.util.Log; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 48 | |
Marco Nelissen | 6eca4b3 | 2011-07-15 15:02:03 -0700 | [diff] [blame] | 49 | import java.util.List; |
| 50 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 51 | public class SoundSettings extends SettingsPreferenceFragment implements |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 52 | Preference.OnPreferenceChangeListener { |
Amith Yamasani | 823bf0c | 2011-08-01 15:55:06 -0700 | [diff] [blame^] | 53 | private static final String TAG = "SoundSettings"; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 54 | |
| 55 | /** If there is no setting in the provider, use this. */ |
Chouting Zhang | 386278a | 2009-06-24 14:25:43 -0500 | [diff] [blame] | 56 | private static final int FALLBACK_EMERGENCY_TONE_VALUE = 0; |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 57 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 58 | private static final String KEY_SILENT = "silent"; |
| 59 | private static final String KEY_VIBRATE = "vibrate"; |
Joe Onorato | 057f181 | 2011-01-12 15:35:47 -0800 | [diff] [blame] | 60 | private static final String KEY_RING_VOLUME = "ring_volume"; |
Marco Nelissen | 6eca4b3 | 2011-07-15 15:02:03 -0700 | [diff] [blame] | 61 | private static final String KEY_MUSICFX = "musicfx"; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 62 | private static final String KEY_DTMF_TONE = "dtmf_tone"; |
| 63 | private static final String KEY_SOUND_EFFECTS = "sound_effects"; |
Dan Murphy | 22e1868 | 2009-09-22 11:47:08 -0500 | [diff] [blame] | 64 | private static final String KEY_HAPTIC_FEEDBACK = "haptic_feedback"; |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 65 | private static final String KEY_EMERGENCY_TONE = "emergency_tone"; |
| 66 | private static final String KEY_SOUND_SETTINGS = "sound_settings"; |
| 67 | private static final String KEY_NOTIFICATION_PULSE = "notification_pulse"; |
Daniel Sandler | 231d880 | 2010-02-17 15:35:21 -0500 | [diff] [blame] | 68 | private static final String KEY_LOCK_SOUNDS = "lock_sounds"; |
Amith Yamasani | 60133dd | 2010-09-11 14:17:31 -0700 | [diff] [blame] | 69 | private static final String KEY_RINGTONE = "ringtone"; |
| 70 | private static final String KEY_NOTIFICATION_SOUND = "notification_sound"; |
| 71 | private static final String KEY_CATEGORY_CALLS = "category_calls"; |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 72 | |
Daniel Sandler | 2c91784 | 2010-04-21 15:24:24 -0400 | [diff] [blame] | 73 | private static final String VALUE_VIBRATE_NEVER = "never"; |
| 74 | private static final String VALUE_VIBRATE_ALWAYS = "always"; |
| 75 | private static final String VALUE_VIBRATE_ONLY_SILENT = "silent"; |
| 76 | private static final String VALUE_VIBRATE_UNLESS_SILENT = "notsilent"; |
| 77 | |
Amith Yamasani | 60133dd | 2010-09-11 14:17:31 -0700 | [diff] [blame] | 78 | private static final String[] NEED_VOICE_CAPABILITY = { |
| 79 | KEY_RINGTONE, KEY_DTMF_TONE, KEY_CATEGORY_CALLS, |
| 80 | KEY_EMERGENCY_TONE |
| 81 | }; |
| 82 | |
Amith Yamasani | 823bf0c | 2011-08-01 15:55:06 -0700 | [diff] [blame^] | 83 | private static final int MSG_UPDATE_RINGTONE_SUMMARY = 1; |
| 84 | private static final int MSG_UPDATE_NOTIFICATION_SUMMARY = 2; |
| 85 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 86 | private CheckBoxPreference mSilent; |
| 87 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 88 | /* |
| 89 | * If we are currently in one of the silent modes (the ringer mode is set to either |
| 90 | * "silent mode" or "vibrate mode"), then toggling the "Phone vibrate" |
| 91 | * preference will switch between "silent mode" and "vibrate mode". |
| 92 | * Otherwise, it will adjust the normal ringer mode's ring or ring+vibrate |
| 93 | * setting. |
| 94 | */ |
Daniel Sandler | 2c91784 | 2010-04-21 15:24:24 -0400 | [diff] [blame] | 95 | private ListPreference mVibrate; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 96 | private CheckBoxPreference mDtmfTone; |
| 97 | private CheckBoxPreference mSoundEffects; |
Dan Murphy | 22e1868 | 2009-09-22 11:47:08 -0500 | [diff] [blame] | 98 | private CheckBoxPreference mHapticFeedback; |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 99 | private CheckBoxPreference mNotificationPulse; |
Marco Nelissen | 6eca4b3 | 2011-07-15 15:02:03 -0700 | [diff] [blame] | 100 | private Preference mMusicFx; |
Daniel Sandler | 231d880 | 2010-02-17 15:35:21 -0500 | [diff] [blame] | 101 | private CheckBoxPreference mLockSounds; |
Amith Yamasani | 823bf0c | 2011-08-01 15:55:06 -0700 | [diff] [blame^] | 102 | private Preference mRingtonePreference; |
| 103 | private Preference mNotificationPreference; |
| 104 | |
| 105 | private Runnable mRingtoneLookupRunnable; |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 106 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 107 | private AudioManager mAudioManager; |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 108 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 109 | private BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 110 | @Override |
| 111 | public void onReceive(Context context, Intent intent) { |
Amith Yamasani | d2b3ab0 | 2009-12-02 13:56:05 -0800 | [diff] [blame] | 112 | if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) { |
| 113 | updateState(false); |
Amith Yamasani | d2b3ab0 | 2009-12-02 13:56:05 -0800 | [diff] [blame] | 114 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 115 | } |
| 116 | }; |
| 117 | |
Amith Yamasani | 823bf0c | 2011-08-01 15:55:06 -0700 | [diff] [blame^] | 118 | private Handler mHandler = new Handler() { |
| 119 | public void handleMessage(Message msg) { |
| 120 | switch (msg.what) { |
| 121 | case MSG_UPDATE_RINGTONE_SUMMARY: |
| 122 | mRingtonePreference.setSummary((CharSequence) msg.obj); |
| 123 | break; |
| 124 | case MSG_UPDATE_NOTIFICATION_SUMMARY: |
| 125 | mNotificationPreference.setSummary((CharSequence) msg.obj); |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | }; |
| 130 | |
Amith Yamasani | d2b3ab0 | 2009-12-02 13:56:05 -0800 | [diff] [blame] | 131 | private PreferenceGroup mSoundSettings; |
| 132 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 133 | @Override |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 134 | public void onCreate(Bundle savedInstanceState) { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 135 | super.onCreate(savedInstanceState); |
| 136 | ContentResolver resolver = getContentResolver(); |
Wink Saville | 327147e | 2011-02-02 11:30:25 -0800 | [diff] [blame] | 137 | int activePhoneType = TelephonyManager.getDefault().getCurrentPhoneType(); |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 138 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 139 | mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 140 | |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 141 | addPreferencesFromResource(R.xml.sound_settings); |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 142 | |
Chouting Zhang | 386278a | 2009-06-24 14:25:43 -0500 | [diff] [blame] | 143 | if (TelephonyManager.PHONE_TYPE_CDMA != activePhoneType) { |
| 144 | // device is not CDMA, do not display CDMA emergency_tone |
| 145 | getPreferenceScreen().removePreference(findPreference(KEY_EMERGENCY_TONE)); |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 146 | } |
Chouting Zhang | 386278a | 2009-06-24 14:25:43 -0500 | [diff] [blame] | 147 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 148 | mSilent = (CheckBoxPreference) findPreference(KEY_SILENT); |
Joe Onorato | 057f181 | 2011-01-12 15:35:47 -0800 | [diff] [blame] | 149 | if (!getResources().getBoolean(R.bool.has_silent_mode)) { |
| 150 | getPreferenceScreen().removePreference(mSilent); |
| 151 | findPreference(KEY_RING_VOLUME).setDependency(null); |
| 152 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 153 | |
Daniel Sandler | 2c91784 | 2010-04-21 15:24:24 -0400 | [diff] [blame] | 154 | mVibrate = (ListPreference) findPreference(KEY_VIBRATE); |
| 155 | mVibrate.setOnPreferenceChangeListener(this); |
Daniel Sandler | 4ce4f75 | 2010-03-08 15:08:34 -0500 | [diff] [blame] | 156 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 157 | mDtmfTone = (CheckBoxPreference) findPreference(KEY_DTMF_TONE); |
| 158 | mDtmfTone.setPersistent(false); |
| 159 | mDtmfTone.setChecked(Settings.System.getInt(resolver, |
| 160 | Settings.System.DTMF_TONE_WHEN_DIALING, 1) != 0); |
| 161 | mSoundEffects = (CheckBoxPreference) findPreference(KEY_SOUND_EFFECTS); |
| 162 | mSoundEffects.setPersistent(false); |
| 163 | mSoundEffects.setChecked(Settings.System.getInt(resolver, |
Amith Yamasani | 823bf0c | 2011-08-01 15:55:06 -0700 | [diff] [blame^] | 164 | Settings.System.SOUND_EFFECTS_ENABLED, 1) != 0); |
Dan Murphy | 22e1868 | 2009-09-22 11:47:08 -0500 | [diff] [blame] | 165 | mHapticFeedback = (CheckBoxPreference) findPreference(KEY_HAPTIC_FEEDBACK); |
| 166 | mHapticFeedback.setPersistent(false); |
| 167 | mHapticFeedback.setChecked(Settings.System.getInt(resolver, |
Amith Yamasani | 823bf0c | 2011-08-01 15:55:06 -0700 | [diff] [blame^] | 168 | Settings.System.HAPTIC_FEEDBACK_ENABLED, 1) != 0); |
Daniel Sandler | 231d880 | 2010-02-17 15:35:21 -0500 | [diff] [blame] | 169 | mLockSounds = (CheckBoxPreference) findPreference(KEY_LOCK_SOUNDS); |
| 170 | mLockSounds.setPersistent(false); |
| 171 | mLockSounds.setChecked(Settings.System.getInt(resolver, |
| 172 | Settings.System.LOCKSCREEN_SOUNDS_ENABLED, 1) != 0); |
Chouting Zhang | 386278a | 2009-06-24 14:25:43 -0500 | [diff] [blame] | 173 | |
Amith Yamasani | 823bf0c | 2011-08-01 15:55:06 -0700 | [diff] [blame^] | 174 | mRingtonePreference = findPreference(KEY_RINGTONE); |
| 175 | mNotificationPreference = findPreference(KEY_NOTIFICATION_SOUND); |
| 176 | |
Amith Yamasani | edac9af | 2010-11-17 09:08:21 -0800 | [diff] [blame] | 177 | if (!((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).hasVibrator()) { |
| 178 | getPreferenceScreen().removePreference(mVibrate); |
| 179 | getPreferenceScreen().removePreference(mHapticFeedback); |
| 180 | } |
| 181 | |
Chouting Zhang | 386278a | 2009-06-24 14:25:43 -0500 | [diff] [blame] | 182 | if (TelephonyManager.PHONE_TYPE_CDMA == activePhoneType) { |
| 183 | ListPreference emergencyTonePreference = |
| 184 | (ListPreference) findPreference(KEY_EMERGENCY_TONE); |
| 185 | emergencyTonePreference.setValue(String.valueOf(Settings.System.getInt( |
| 186 | resolver, Settings.System.EMERGENCY_TONE, FALLBACK_EMERGENCY_TONE_VALUE))); |
| 187 | emergencyTonePreference.setOnPreferenceChangeListener(this); |
| 188 | } |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 189 | |
Amith Yamasani | d2b3ab0 | 2009-12-02 13:56:05 -0800 | [diff] [blame] | 190 | mSoundSettings = (PreferenceGroup) findPreference(KEY_SOUND_SETTINGS); |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 191 | mNotificationPulse = (CheckBoxPreference) |
Amith Yamasani | d2b3ab0 | 2009-12-02 13:56:05 -0800 | [diff] [blame] | 192 | mSoundSettings.findPreference(KEY_NOTIFICATION_PULSE); |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 193 | if (mNotificationPulse != null |
| 194 | && getResources().getBoolean(R.bool.has_intrusive_led) == false) { |
Amith Yamasani | d2b3ab0 | 2009-12-02 13:56:05 -0800 | [diff] [blame] | 195 | mSoundSettings.removePreference(mNotificationPulse); |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 196 | } else { |
| 197 | try { |
| 198 | mNotificationPulse.setChecked(Settings.System.getInt(resolver, |
| 199 | Settings.System.NOTIFICATION_LIGHT_PULSE) == 1); |
| 200 | mNotificationPulse.setOnPreferenceChangeListener(this); |
| 201 | } catch (SettingNotFoundException snfe) { |
| 202 | Log.e(TAG, Settings.System.NOTIFICATION_LIGHT_PULSE + " not found"); |
| 203 | } |
| 204 | } |
Amith Yamasani | d2b3ab0 | 2009-12-02 13:56:05 -0800 | [diff] [blame] | 205 | |
Marco Nelissen | 6eca4b3 | 2011-07-15 15:02:03 -0700 | [diff] [blame] | 206 | mMusicFx = mSoundSettings.findPreference(KEY_MUSICFX); |
| 207 | Intent i = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL); |
| 208 | PackageManager p = getPackageManager(); |
| 209 | List<ResolveInfo> ris = p.queryIntentActivities(i, PackageManager.GET_DISABLED_COMPONENTS); |
| 210 | if (ris.size() <= 2) { |
| 211 | // no need to show the item if there is no choice for the user to make |
| 212 | // note: the built in musicfx panel has two activities (one being a |
| 213 | // compatibility shim that launches either the other activity, or a |
| 214 | // third party one), hence the check for <=2. If the implementation |
| 215 | // of the compatbility layer changes, this check may need to be updated. |
| 216 | mSoundSettings.removePreference(mMusicFx); |
| 217 | } |
| 218 | |
Amith Yamasani | 60133dd | 2010-09-11 14:17:31 -0700 | [diff] [blame] | 219 | if (!Utils.isVoiceCapable(getActivity())) { |
| 220 | for (String prefKey : NEED_VOICE_CAPABILITY) { |
| 221 | Preference pref = findPreference(prefKey); |
| 222 | if (pref != null) { |
| 223 | getPreferenceScreen().removePreference(pref); |
| 224 | } |
| 225 | } |
| 226 | } |
Amith Yamasani | 823bf0c | 2011-08-01 15:55:06 -0700 | [diff] [blame^] | 227 | |
| 228 | mRingtoneLookupRunnable = new Runnable() { |
| 229 | public void run() { |
| 230 | if (mRingtonePreference != null) { |
| 231 | updateRingtoneName(RingtoneManager.TYPE_RINGTONE, mRingtonePreference, |
| 232 | MSG_UPDATE_RINGTONE_SUMMARY); |
| 233 | } |
| 234 | if (mNotificationPreference != null) { |
| 235 | updateRingtoneName(RingtoneManager.TYPE_NOTIFICATION, mNotificationPreference, |
| 236 | MSG_UPDATE_NOTIFICATION_SUMMARY); |
| 237 | } |
| 238 | } |
| 239 | }; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 240 | } |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 241 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 242 | @Override |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 243 | public void onResume() { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 244 | super.onResume(); |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 245 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 246 | updateState(true); |
Amith Yamasani | 823bf0c | 2011-08-01 15:55:06 -0700 | [diff] [blame^] | 247 | lookupRingtoneNames(); |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 248 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 249 | IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION); |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 250 | getActivity().registerReceiver(mReceiver, filter); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | @Override |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 254 | public void onPause() { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 255 | super.onPause(); |
| 256 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 257 | getActivity().unregisterReceiver(mReceiver); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Daniel Sandler | 2c91784 | 2010-04-21 15:24:24 -0400 | [diff] [blame] | 260 | private String getPhoneVibrateSettingValue() { |
| 261 | boolean vibeInSilent = (Settings.System.getInt( |
| 262 | getContentResolver(), |
| 263 | Settings.System.VIBRATE_IN_SILENT, |
| 264 | 1) == 1); |
| 265 | |
| 266 | // Control phone vibe independent of silent mode |
| 267 | int callsVibrateSetting = |
| 268 | mAudioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER); |
| 269 | |
| 270 | if (vibeInSilent) { |
| 271 | if (callsVibrateSetting == AudioManager.VIBRATE_SETTING_OFF) { |
| 272 | // this state does not make sense; fix it up for the user |
| 273 | mAudioManager.setVibrateSetting( |
| 274 | AudioManager.VIBRATE_TYPE_RINGER, |
| 275 | AudioManager.VIBRATE_SETTING_ONLY_SILENT); |
| 276 | } |
| 277 | if (callsVibrateSetting == AudioManager.VIBRATE_SETTING_ON) { |
| 278 | return VALUE_VIBRATE_ALWAYS; |
| 279 | } else { |
| 280 | return VALUE_VIBRATE_ONLY_SILENT; |
| 281 | } |
| 282 | } else { |
| 283 | if (callsVibrateSetting == AudioManager.VIBRATE_SETTING_ONLY_SILENT) { |
| 284 | // this state does not make sense; fix it up |
| 285 | mAudioManager.setVibrateSetting( |
| 286 | AudioManager.VIBRATE_TYPE_RINGER, |
| 287 | AudioManager.VIBRATE_SETTING_OFF); |
| 288 | } |
| 289 | if (callsVibrateSetting == AudioManager.VIBRATE_SETTING_ON) { |
| 290 | return VALUE_VIBRATE_UNLESS_SILENT; |
| 291 | } else { |
| 292 | return VALUE_VIBRATE_NEVER; |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | private void setPhoneVibrateSettingValue(String value) { |
| 298 | boolean vibeInSilent; |
| 299 | int callsVibrateSetting; |
| 300 | |
| 301 | if (value.equals(VALUE_VIBRATE_UNLESS_SILENT)) { |
| 302 | callsVibrateSetting = AudioManager.VIBRATE_SETTING_ON; |
| 303 | vibeInSilent = false; |
| 304 | } else if (value.equals(VALUE_VIBRATE_NEVER)) { |
| 305 | callsVibrateSetting = AudioManager.VIBRATE_SETTING_OFF; |
| 306 | vibeInSilent = false; |
| 307 | } else if (value.equals(VALUE_VIBRATE_ONLY_SILENT)) { |
| 308 | callsVibrateSetting = AudioManager.VIBRATE_SETTING_ONLY_SILENT; |
| 309 | vibeInSilent = true; |
| 310 | } else { //VALUE_VIBRATE_ALWAYS |
| 311 | callsVibrateSetting = AudioManager.VIBRATE_SETTING_ON; |
| 312 | vibeInSilent = true; |
| 313 | } |
| 314 | |
| 315 | Settings.System.putInt(getContentResolver(), |
| 316 | Settings.System.VIBRATE_IN_SILENT, |
| 317 | vibeInSilent ? 1 : 0); |
| 318 | |
| 319 | // might need to switch the ringer mode from one kind of "silent" to |
| 320 | // another |
| 321 | if (mSilent.isChecked()) { |
| 322 | mAudioManager.setRingerMode( |
| 323 | vibeInSilent ? AudioManager.RINGER_MODE_VIBRATE |
| 324 | : AudioManager.RINGER_MODE_SILENT); |
| 325 | } |
| 326 | |
| 327 | mAudioManager.setVibrateSetting( |
| 328 | AudioManager.VIBRATE_TYPE_RINGER, |
| 329 | callsVibrateSetting); |
| 330 | } |
| 331 | |
Daniel Sandler | 58cf829 | 2010-02-26 15:25:04 -0500 | [diff] [blame] | 332 | // updateState in fact updates the UI to reflect the system state |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 333 | private void updateState(boolean force) { |
Amith Yamasani | eb99f2f | 2010-12-15 14:12:43 -0800 | [diff] [blame] | 334 | if (getActivity() == null) return; |
| 335 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 336 | final int ringerMode = mAudioManager.getRingerMode(); |
Daniel Sandler | 58cf829 | 2010-02-26 15:25:04 -0500 | [diff] [blame] | 337 | |
| 338 | // NB: in the UI we now simply call this "silent mode". A separate |
| 339 | // setting controls whether we're in RINGER_MODE_SILENT or |
| 340 | // RINGER_MODE_VIBRATE. |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 341 | final boolean silentOrVibrateMode = |
| 342 | ringerMode != AudioManager.RINGER_MODE_NORMAL; |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 343 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 344 | if (silentOrVibrateMode != mSilent.isChecked() || force) { |
| 345 | mSilent.setChecked(silentOrVibrateMode); |
| 346 | } |
| 347 | |
Daniel Sandler | 2c91784 | 2010-04-21 15:24:24 -0400 | [diff] [blame] | 348 | String phoneVibrateSetting = getPhoneVibrateSettingValue(); |
Daniel Sandler | 58cf829 | 2010-02-26 15:25:04 -0500 | [diff] [blame] | 349 | |
Daniel Sandler | 2c91784 | 2010-04-21 15:24:24 -0400 | [diff] [blame] | 350 | if (! phoneVibrateSetting.equals(mVibrate.getValue()) || force) { |
| 351 | mVibrate.setValue(phoneVibrateSetting); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 352 | } |
Daniel Sandler | 2c91784 | 2010-04-21 15:24:24 -0400 | [diff] [blame] | 353 | mVibrate.setSummary(mVibrate.getEntry()); |
Amith Yamasani | 823bf0c | 2011-08-01 15:55:06 -0700 | [diff] [blame^] | 354 | } |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 355 | |
Amith Yamasani | 823bf0c | 2011-08-01 15:55:06 -0700 | [diff] [blame^] | 356 | private void updateRingtoneName(int type, Preference preference, int msg) { |
| 357 | if (preference == null) return; |
| 358 | Context context = getActivity(); |
| 359 | if (context == null) return; |
| 360 | Uri ringtoneUri = RingtoneManager.getActualDefaultRingtoneUri(context, type); |
| 361 | CharSequence summary = context.getString(com.android.internal.R.string.ringtone_unknown); |
| 362 | // Is it a silent ringtone? |
| 363 | if (ringtoneUri == null) { |
| 364 | summary = context.getString(com.android.internal.R.string.ringtone_silent); |
| 365 | } else { |
| 366 | // Fetch the ringtone title from the media provider |
| 367 | try { |
| 368 | Cursor cursor = context.getContentResolver().query(ringtoneUri, |
| 369 | new String[] { MediaStore.Audio.Media.TITLE }, null, null, null); |
| 370 | if (cursor.moveToFirst()) { |
| 371 | summary = cursor.getString(0); |
| 372 | } |
| 373 | } catch (SQLiteException sqle) { |
| 374 | // Unknown title for the ringtone |
| 375 | } |
| 376 | } |
| 377 | mHandler.sendMessage(mHandler.obtainMessage(msg, summary)); |
| 378 | } |
| 379 | |
| 380 | private void lookupRingtoneNames() { |
| 381 | new Thread(mRingtoneLookupRunnable).start(); |
Dianne Hackborn | 7631539 | 2009-09-03 13:33:55 -0700 | [diff] [blame] | 382 | } |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 383 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 384 | @Override |
| 385 | public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { |
Daniel Sandler | 58cf829 | 2010-02-26 15:25:04 -0500 | [diff] [blame] | 386 | if (preference == mSilent) { |
| 387 | if (mSilent.isChecked()) { |
Daniel Sandler | 2c91784 | 2010-04-21 15:24:24 -0400 | [diff] [blame] | 388 | boolean vibeInSilent = (1 == Settings.System.getInt( |
| 389 | getContentResolver(), |
| 390 | Settings.System.VIBRATE_IN_SILENT, |
| 391 | 1)); |
Daniel Sandler | 58cf829 | 2010-02-26 15:25:04 -0500 | [diff] [blame] | 392 | mAudioManager.setRingerMode( |
| 393 | vibeInSilent ? AudioManager.RINGER_MODE_VIBRATE |
| 394 | : AudioManager.RINGER_MODE_SILENT); |
| 395 | } else { |
| 396 | mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); |
| 397 | } |
| 398 | updateState(false); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 399 | } else if (preference == mDtmfTone) { |
| 400 | Settings.System.putInt(getContentResolver(), Settings.System.DTMF_TONE_WHEN_DIALING, |
| 401 | mDtmfTone.isChecked() ? 1 : 0); |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 402 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 403 | } else if (preference == mSoundEffects) { |
| 404 | if (mSoundEffects.isChecked()) { |
| 405 | mAudioManager.loadSoundEffects(); |
| 406 | } else { |
| 407 | mAudioManager.unloadSoundEffects(); |
| 408 | } |
| 409 | Settings.System.putInt(getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED, |
| 410 | mSoundEffects.isChecked() ? 1 : 0); |
Dan Murphy | 22e1868 | 2009-09-22 11:47:08 -0500 | [diff] [blame] | 411 | |
| 412 | } else if (preference == mHapticFeedback) { |
| 413 | Settings.System.putInt(getContentResolver(), Settings.System.HAPTIC_FEEDBACK_ENABLED, |
| 414 | mHapticFeedback.isChecked() ? 1 : 0); |
Michael Chan | df9504e | 2009-12-06 01:04:04 -0800 | [diff] [blame] | 415 | |
Daniel Sandler | 231d880 | 2010-02-17 15:35:21 -0500 | [diff] [blame] | 416 | } else if (preference == mLockSounds) { |
| 417 | Settings.System.putInt(getContentResolver(), Settings.System.LOCKSCREEN_SOUNDS_ENABLED, |
| 418 | mLockSounds.isChecked() ? 1 : 0); |
| 419 | |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 420 | } else if (preference == mNotificationPulse) { |
| 421 | boolean value = mNotificationPulse.isChecked(); |
| 422 | Settings.System.putInt(getContentResolver(), |
| 423 | Settings.System.NOTIFICATION_LIGHT_PULSE, value ? 1 : 0); |
Marco Nelissen | 6eca4b3 | 2011-07-15 15:02:03 -0700 | [diff] [blame] | 424 | } else if (preference == mMusicFx) { |
| 425 | // let the framework fire off the intent |
| 426 | return false; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 427 | } |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 428 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 429 | return true; |
| 430 | } |
| 431 | |
| 432 | public boolean onPreferenceChange(Preference preference, Object objValue) { |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 433 | final String key = preference.getKey(); |
Amith Yamasani | 992f102 | 2010-01-25 09:17:53 -0800 | [diff] [blame] | 434 | if (KEY_EMERGENCY_TONE.equals(key)) { |
Chouting Zhang | 386278a | 2009-06-24 14:25:43 -0500 | [diff] [blame] | 435 | try { |
Peter Kuterna | 378b6f6 | 2010-11-28 22:35:31 +0100 | [diff] [blame] | 436 | int value = Integer.parseInt((String) objValue); |
Chouting Zhang | 386278a | 2009-06-24 14:25:43 -0500 | [diff] [blame] | 437 | Settings.System.putInt(getContentResolver(), |
| 438 | Settings.System.EMERGENCY_TONE, value); |
| 439 | } catch (NumberFormatException e) { |
| 440 | Log.e(TAG, "could not persist emergency tone setting", e); |
| 441 | } |
Daniel Sandler | 2c91784 | 2010-04-21 15:24:24 -0400 | [diff] [blame] | 442 | } else if (preference == mVibrate) { |
| 443 | setPhoneVibrateSettingValue(objValue.toString()); |
| 444 | updateState(false); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 445 | } |
Amith Yamasani | 8f2fb65b | 2009-12-01 19:06:14 -0800 | [diff] [blame] | 446 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 447 | return true; |
| 448 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 449 | } |