blob: 45fd0dbeecf0bb0897b9fb52b6506bb361aa2463 [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
Chris Wren2bc32ae2012-09-24 14:23:46 -040019import com.android.settings.bluetooth.DockEventReceiver;
20
21import android.app.AlertDialog;
22import android.app.Dialog;
23import android.bluetooth.BluetoothDevice;
24import android.content.BroadcastReceiver;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080025import android.content.ContentResolver;
26import android.content.Context;
27import android.content.Intent;
Chris Wren2bc32ae2012-09-24 14:23:46 -040028import android.content.IntentFilter;
Marco Nelissen6eca4b32011-07-15 15:02:03 -070029import android.content.pm.PackageManager;
30import android.content.pm.ResolveInfo;
Amith Yamasani823bf0c2011-08-01 15:55:06 -070031import android.database.Cursor;
32import android.database.sqlite.SQLiteException;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080033import android.media.AudioManager;
Amith Yamasani823bf0c2011-08-01 15:55:06 -070034import android.media.RingtoneManager;
Marco Nelissen6eca4b32011-07-15 15:02:03 -070035import android.media.audiofx.AudioEffect;
Amith Yamasani823bf0c2011-08-01 15:55:06 -070036import android.net.Uri;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080037import android.os.Bundle;
Amith Yamasani823bf0c2011-08-01 15:55:06 -070038import android.os.Handler;
39import android.os.Message;
Dianne Hackborn0a115aa2010-11-04 11:40:31 -070040import android.os.Vibrator;
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -080041import android.preference.CheckBoxPreference;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080042import android.preference.ListPreference;
43import android.preference.Preference;
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -080044import android.preference.PreferenceGroup;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080045import android.preference.PreferenceScreen;
Amith Yamasani823bf0c2011-08-01 15:55:06 -070046import android.provider.MediaStore;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080047import android.provider.Settings;
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -080048import android.telephony.TelephonyManager;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080049import android.util.Log;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080050
Marco Nelissen6eca4b32011-07-15 15:02:03 -070051import java.util.List;
52
Amith Yamasanid7993472010-08-18 13:59:28 -070053public class SoundSettings extends SettingsPreferenceFragment implements
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080054 Preference.OnPreferenceChangeListener {
Amith Yamasani823bf0c2011-08-01 15:55:06 -070055 private static final String TAG = "SoundSettings";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080056
Chris Wren2bc32ae2012-09-24 14:23:46 -040057 private static final int DIALOG_NOT_DOCKED = 1;
58
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080059 /** If there is no setting in the provider, use this. */
Chouting Zhang386278a2009-06-24 14:25:43 -050060 private static final int FALLBACK_EMERGENCY_TONE_VALUE = 0;
Michael Chandf9504e2009-12-06 01:04:04 -080061
Daisuke Miyakawa8b878db2012-05-08 12:26:23 -070062 private static final String KEY_VIBRATE = "vibrate_when_ringing";
Joe Onorato057f1812011-01-12 15:35:47 -080063 private static final String KEY_RING_VOLUME = "ring_volume";
Marco Nelissen6eca4b32011-07-15 15:02:03 -070064 private static final String KEY_MUSICFX = "musicfx";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080065 private static final String KEY_DTMF_TONE = "dtmf_tone";
66 private static final String KEY_SOUND_EFFECTS = "sound_effects";
Dan Murphy22e18682009-09-22 11:47:08 -050067 private static final String KEY_HAPTIC_FEEDBACK = "haptic_feedback";
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -080068 private static final String KEY_EMERGENCY_TONE = "emergency_tone";
69 private static final String KEY_SOUND_SETTINGS = "sound_settings";
Daniel Sandler231d8802010-02-17 15:35:21 -050070 private static final String KEY_LOCK_SOUNDS = "lock_sounds";
Amith Yamasani60133dd2010-09-11 14:17:31 -070071 private static final String KEY_RINGTONE = "ringtone";
72 private static final String KEY_NOTIFICATION_SOUND = "notification_sound";
Daisuke Miyakawadba00b62012-05-16 08:41:38 -070073 private static final String KEY_CATEGORY_CALLS = "category_calls_and_notification";
Chris Wren2bc32ae2012-09-24 14:23:46 -040074 private static final String KEY_DOCK_CATEGORY = "dock_category";
Eric Laurente00cd9f2012-10-26 18:55:16 -070075 private static final String KEY_DOCK_AUDIO_SETTINGS = "dock_audio";
Chris Wren2bc32ae2012-09-24 14:23:46 -040076 private static final String KEY_DOCK_SOUNDS = "dock_sounds";
Eric Laurente00cd9f2012-10-26 18:55:16 -070077 private static final String KEY_DOCK_AUDIO_MEDIA_ENABLED = "dock_audio_media_enabled";
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -080078
Amith Yamasani60133dd2010-09-11 14:17:31 -070079 private static final String[] NEED_VOICE_CAPABILITY = {
80 KEY_RINGTONE, KEY_DTMF_TONE, KEY_CATEGORY_CALLS,
Henrik Baard2ac8da32013-02-27 08:57:03 +010081 KEY_EMERGENCY_TONE, KEY_VIBRATE
Amith Yamasani60133dd2010-09-11 14:17:31 -070082 };
83
Amith Yamasani823bf0c2011-08-01 15:55:06 -070084 private static final int MSG_UPDATE_RINGTONE_SUMMARY = 1;
85 private static final int MSG_UPDATE_NOTIFICATION_SUMMARY = 2;
86
Daisuke Miyakawa8b878db2012-05-08 12:26:23 -070087 private CheckBoxPreference mVibrateWhenRinging;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080088 private CheckBoxPreference mDtmfTone;
89 private CheckBoxPreference mSoundEffects;
Dan Murphy22e18682009-09-22 11:47:08 -050090 private CheckBoxPreference mHapticFeedback;
Marco Nelissen6eca4b32011-07-15 15:02:03 -070091 private Preference mMusicFx;
Daniel Sandler231d8802010-02-17 15:35:21 -050092 private CheckBoxPreference mLockSounds;
Amith Yamasani823bf0c2011-08-01 15:55:06 -070093 private Preference mRingtonePreference;
94 private Preference mNotificationPreference;
95
96 private Runnable mRingtoneLookupRunnable;
Michael Chandf9504e2009-12-06 01:04:04 -080097
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080098 private AudioManager mAudioManager;
Michael Chandf9504e2009-12-06 01:04:04 -080099
Chris Wren2bc32ae2012-09-24 14:23:46 -0400100 private Preference mDockAudioSettings;
101 private CheckBoxPreference mDockSounds;
102 private Intent mDockIntent;
Eric Laurente00cd9f2012-10-26 18:55:16 -0700103 private CheckBoxPreference mDockAudioMediaEnabled;
Chris Wren2bc32ae2012-09-24 14:23:46 -0400104
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700105 private Handler mHandler = new Handler() {
106 public void handleMessage(Message msg) {
107 switch (msg.what) {
108 case MSG_UPDATE_RINGTONE_SUMMARY:
109 mRingtonePreference.setSummary((CharSequence) msg.obj);
110 break;
111 case MSG_UPDATE_NOTIFICATION_SUMMARY:
112 mNotificationPreference.setSummary((CharSequence) msg.obj);
113 break;
114 }
115 }
116 };
117
Chris Wren2bc32ae2012-09-24 14:23:46 -0400118 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
119 @Override
120 public void onReceive(Context context, Intent intent) {
121 if (intent.getAction().equals(Intent.ACTION_DOCK_EVENT)) {
122 handleDockChange(intent);
123 }
124 }
125 };
126
Amith Yamasanid2b3ab02009-12-02 13:56:05 -0800127 private PreferenceGroup mSoundSettings;
128
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800129 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700130 public void onCreate(Bundle savedInstanceState) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800131 super.onCreate(savedInstanceState);
132 ContentResolver resolver = getContentResolver();
Wink Saville327147e2011-02-02 11:30:25 -0800133 int activePhoneType = TelephonyManager.getDefault().getCurrentPhoneType();
Michael Chandf9504e2009-12-06 01:04:04 -0800134
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800135 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800136
Amith Yamasani992f1022010-01-25 09:17:53 -0800137 addPreferencesFromResource(R.xml.sound_settings);
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800138
Chouting Zhang386278a2009-06-24 14:25:43 -0500139 if (TelephonyManager.PHONE_TYPE_CDMA != activePhoneType) {
140 // device is not CDMA, do not display CDMA emergency_tone
141 getPreferenceScreen().removePreference(findPreference(KEY_EMERGENCY_TONE));
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800142 }
Chouting Zhang386278a2009-06-24 14:25:43 -0500143
Joe Onorato057f1812011-01-12 15:35:47 -0800144 if (!getResources().getBoolean(R.bool.has_silent_mode)) {
Joe Onorato057f1812011-01-12 15:35:47 -0800145 findPreference(KEY_RING_VOLUME).setDependency(null);
146 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800147
Eric Laurent296d9bb2013-03-19 18:24:47 -0700148 if (getResources().getBoolean(com.android.internal.R.bool.config_useFixedVolume)) {
149 // device with fixed volume policy, do not display volumes submenu
150 getPreferenceScreen().removePreference(findPreference(KEY_RING_VOLUME));
151 }
152
Daisuke Miyakawa8b878db2012-05-08 12:26:23 -0700153 mVibrateWhenRinging = (CheckBoxPreference) findPreference(KEY_VIBRATE);
154 mVibrateWhenRinging.setPersistent(false);
155 mVibrateWhenRinging.setChecked(Settings.System.getInt(resolver,
156 Settings.System.VIBRATE_WHEN_RINGING, 0) != 0);
157
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800158 mDtmfTone = (CheckBoxPreference) findPreference(KEY_DTMF_TONE);
159 mDtmfTone.setPersistent(false);
160 mDtmfTone.setChecked(Settings.System.getInt(resolver,
161 Settings.System.DTMF_TONE_WHEN_DIALING, 1) != 0);
162 mSoundEffects = (CheckBoxPreference) findPreference(KEY_SOUND_EFFECTS);
163 mSoundEffects.setPersistent(false);
164 mSoundEffects.setChecked(Settings.System.getInt(resolver,
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700165 Settings.System.SOUND_EFFECTS_ENABLED, 1) != 0);
Dan Murphy22e18682009-09-22 11:47:08 -0500166 mHapticFeedback = (CheckBoxPreference) findPreference(KEY_HAPTIC_FEEDBACK);
167 mHapticFeedback.setPersistent(false);
168 mHapticFeedback.setChecked(Settings.System.getInt(resolver,
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700169 Settings.System.HAPTIC_FEEDBACK_ENABLED, 1) != 0);
Daniel Sandler231d8802010-02-17 15:35:21 -0500170 mLockSounds = (CheckBoxPreference) findPreference(KEY_LOCK_SOUNDS);
171 mLockSounds.setPersistent(false);
172 mLockSounds.setChecked(Settings.System.getInt(resolver,
173 Settings.System.LOCKSCREEN_SOUNDS_ENABLED, 1) != 0);
Chouting Zhang386278a2009-06-24 14:25:43 -0500174
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700175 mRingtonePreference = findPreference(KEY_RINGTONE);
176 mNotificationPreference = findPreference(KEY_NOTIFICATION_SOUND);
177
Daisuke Miyakawa8b878db2012-05-08 12:26:23 -0700178 Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
179 if (vibrator == null || !vibrator.hasVibrator()) {
Amith Yamasanic861cf82012-10-02 14:51:46 -0700180 removePreference(KEY_VIBRATE);
181 removePreference(KEY_HAPTIC_FEEDBACK);
182 }
Amith Yamasaniedac9af2010-11-17 09:08:21 -0800183
Chouting Zhang386278a2009-06-24 14:25:43 -0500184 if (TelephonyManager.PHONE_TYPE_CDMA == activePhoneType) {
185 ListPreference emergencyTonePreference =
186 (ListPreference) findPreference(KEY_EMERGENCY_TONE);
Jeff Sharkeya2c12a62012-10-01 13:39:24 -0700187 emergencyTonePreference.setValue(String.valueOf(Settings.Global.getInt(
188 resolver, Settings.Global.EMERGENCY_TONE, FALLBACK_EMERGENCY_TONE_VALUE)));
Chouting Zhang386278a2009-06-24 14:25:43 -0500189 emergencyTonePreference.setOnPreferenceChangeListener(this);
190 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800191
Amith Yamasanid2b3ab02009-12-02 13:56:05 -0800192 mSoundSettings = (PreferenceGroup) findPreference(KEY_SOUND_SETTINGS);
Amith Yamasanid2b3ab02009-12-02 13:56:05 -0800193
Marco Nelissen6eca4b32011-07-15 15:02:03 -0700194 mMusicFx = mSoundSettings.findPreference(KEY_MUSICFX);
195 Intent i = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL);
196 PackageManager p = getPackageManager();
197 List<ResolveInfo> ris = p.queryIntentActivities(i, PackageManager.GET_DISABLED_COMPONENTS);
198 if (ris.size() <= 2) {
199 // no need to show the item if there is no choice for the user to make
200 // note: the built in musicfx panel has two activities (one being a
201 // compatibility shim that launches either the other activity, or a
202 // third party one), hence the check for <=2. If the implementation
203 // of the compatbility layer changes, this check may need to be updated.
204 mSoundSettings.removePreference(mMusicFx);
205 }
206
Amith Yamasani60133dd2010-09-11 14:17:31 -0700207 if (!Utils.isVoiceCapable(getActivity())) {
208 for (String prefKey : NEED_VOICE_CAPABILITY) {
209 Preference pref = findPreference(prefKey);
210 if (pref != null) {
211 getPreferenceScreen().removePreference(pref);
212 }
213 }
214 }
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700215
216 mRingtoneLookupRunnable = new Runnable() {
217 public void run() {
218 if (mRingtonePreference != null) {
219 updateRingtoneName(RingtoneManager.TYPE_RINGTONE, mRingtonePreference,
220 MSG_UPDATE_RINGTONE_SUMMARY);
221 }
222 if (mNotificationPreference != null) {
223 updateRingtoneName(RingtoneManager.TYPE_NOTIFICATION, mNotificationPreference,
224 MSG_UPDATE_NOTIFICATION_SUMMARY);
225 }
226 }
227 };
Chris Wren2bc32ae2012-09-24 14:23:46 -0400228
229 initDockSettings();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800230 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800231
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800232 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700233 public void onResume() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800234 super.onResume();
Michael Chandf9504e2009-12-06 01:04:04 -0800235
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700236 lookupRingtoneNames();
Chris Wren2bc32ae2012-09-24 14:23:46 -0400237
238 IntentFilter filter = new IntentFilter(Intent.ACTION_DOCK_EVENT);
239 getActivity().registerReceiver(mReceiver, filter);
240 }
241
242 @Override
243 public void onPause() {
244 super.onPause();
245
246 getActivity().unregisterReceiver(mReceiver);
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700247 }
Michael Chandf9504e2009-12-06 01:04:04 -0800248
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700249 private void updateRingtoneName(int type, Preference preference, int msg) {
250 if (preference == null) return;
Dan Sandler347f9442014-03-11 01:32:47 -0400251 final CharSequence summary = updateRingtoneName(getActivity(), type);
252 if (summary == null) return;
253 mHandler.sendMessage(mHandler.obtainMessage(msg, summary));
254 }
255
256 public static CharSequence updateRingtoneName(Context context, int type) {
257 if (context == null) return null;
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700258 Uri ringtoneUri = RingtoneManager.getActualDefaultRingtoneUri(context, type);
259 CharSequence summary = context.getString(com.android.internal.R.string.ringtone_unknown);
260 // Is it a silent ringtone?
261 if (ringtoneUri == null) {
262 summary = context.getString(com.android.internal.R.string.ringtone_silent);
263 } else {
264 // Fetch the ringtone title from the media provider
265 try {
266 Cursor cursor = context.getContentResolver().query(ringtoneUri,
267 new String[] { MediaStore.Audio.Media.TITLE }, null, null, null);
Amith Yamasanic86755b2011-10-04 15:00:21 -0700268 if (cursor != null) {
269 if (cursor.moveToFirst()) {
270 summary = cursor.getString(0);
271 }
272 cursor.close();
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700273 }
274 } catch (SQLiteException sqle) {
275 // Unknown title for the ringtone
276 }
277 }
Dan Sandler347f9442014-03-11 01:32:47 -0400278 return summary;
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700279 }
280
281 private void lookupRingtoneNames() {
282 new Thread(mRingtoneLookupRunnable).start();
Dianne Hackborn76315392009-09-03 13:33:55 -0700283 }
Michael Chandf9504e2009-12-06 01:04:04 -0800284
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800285 @Override
286 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
Daisuke Miyakawa8b878db2012-05-08 12:26:23 -0700287 if (preference == mVibrateWhenRinging) {
288 Settings.System.putInt(getContentResolver(), Settings.System.VIBRATE_WHEN_RINGING,
289 mVibrateWhenRinging.isChecked() ? 1 : 0);
290 } else if (preference == mDtmfTone) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800291 Settings.System.putInt(getContentResolver(), Settings.System.DTMF_TONE_WHEN_DIALING,
292 mDtmfTone.isChecked() ? 1 : 0);
Michael Chandf9504e2009-12-06 01:04:04 -0800293
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800294 } else if (preference == mSoundEffects) {
295 if (mSoundEffects.isChecked()) {
296 mAudioManager.loadSoundEffects();
297 } else {
298 mAudioManager.unloadSoundEffects();
299 }
300 Settings.System.putInt(getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED,
301 mSoundEffects.isChecked() ? 1 : 0);
Dan Murphy22e18682009-09-22 11:47:08 -0500302
303 } else if (preference == mHapticFeedback) {
304 Settings.System.putInt(getContentResolver(), Settings.System.HAPTIC_FEEDBACK_ENABLED,
305 mHapticFeedback.isChecked() ? 1 : 0);
Michael Chandf9504e2009-12-06 01:04:04 -0800306
Daniel Sandler231d8802010-02-17 15:35:21 -0500307 } else if (preference == mLockSounds) {
308 Settings.System.putInt(getContentResolver(), Settings.System.LOCKSCREEN_SOUNDS_ENABLED,
309 mLockSounds.isChecked() ? 1 : 0);
310
Marco Nelissen6eca4b32011-07-15 15:02:03 -0700311 } else if (preference == mMusicFx) {
312 // let the framework fire off the intent
313 return false;
Chris Wren2bc32ae2012-09-24 14:23:46 -0400314 } else if (preference == mDockAudioSettings) {
315 int dockState = mDockIntent != null
316 ? mDockIntent.getIntExtra(Intent.EXTRA_DOCK_STATE, 0)
317 : Intent.EXTRA_DOCK_STATE_UNDOCKED;
Eric Laurente00cd9f2012-10-26 18:55:16 -0700318
Chris Wren2bc32ae2012-09-24 14:23:46 -0400319 if (dockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) {
320 showDialog(DIALOG_NOT_DOCKED);
321 } else {
Eric Laurente00cd9f2012-10-26 18:55:16 -0700322 boolean isBluetooth = mDockIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) != null;
323
324 if (isBluetooth) {
325 Intent i = new Intent(mDockIntent);
326 i.setAction(DockEventReceiver.ACTION_DOCK_SHOW_UI);
327 i.setClass(getActivity(), DockEventReceiver.class);
328 getActivity().sendBroadcast(i);
329 } else {
330 PreferenceScreen ps = (PreferenceScreen)mDockAudioSettings;
331 Bundle extras = ps.getExtras();
332 extras.putBoolean("checked",
333 Settings.Global.getInt(getContentResolver(),
334 Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, 0) == 1);
335 super.onPreferenceTreeClick(ps, ps);
336 }
Chris Wren2bc32ae2012-09-24 14:23:46 -0400337 }
338 } else if (preference == mDockSounds) {
Jeff Sharkey860eceb2012-10-18 15:38:42 -0700339 Settings.Global.putInt(getContentResolver(), Settings.Global.DOCK_SOUNDS_ENABLED,
Chris Wren2bc32ae2012-09-24 14:23:46 -0400340 mDockSounds.isChecked() ? 1 : 0);
Eric Laurente00cd9f2012-10-26 18:55:16 -0700341 } else if (preference == mDockAudioMediaEnabled) {
342 Settings.Global.putInt(getContentResolver(), Settings.Global.DOCK_AUDIO_MEDIA_ENABLED,
343 mDockAudioMediaEnabled.isChecked() ? 1 : 0);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800344 }
345 return true;
346 }
347
348 public boolean onPreferenceChange(Preference preference, Object objValue) {
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800349 final String key = preference.getKey();
Amith Yamasani992f1022010-01-25 09:17:53 -0800350 if (KEY_EMERGENCY_TONE.equals(key)) {
Chouting Zhang386278a2009-06-24 14:25:43 -0500351 try {
Peter Kuterna378b6f62010-11-28 22:35:31 +0100352 int value = Integer.parseInt((String) objValue);
Jeff Sharkeya2c12a62012-10-01 13:39:24 -0700353 Settings.Global.putInt(getContentResolver(),
354 Settings.Global.EMERGENCY_TONE, value);
Chouting Zhang386278a2009-06-24 14:25:43 -0500355 } catch (NumberFormatException e) {
356 Log.e(TAG, "could not persist emergency tone setting", e);
357 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800358 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800359
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800360 return true;
361 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700362
363 @Override
364 protected int getHelpResource() {
365 return R.string.help_url_sound;
366 }
Chris Wren2bc32ae2012-09-24 14:23:46 -0400367
368 private boolean needsDockSettings() {
369 return getResources().getBoolean(R.bool.has_dock_settings);
370 }
371
372 private void initDockSettings() {
Eric Laurente00cd9f2012-10-26 18:55:16 -0700373 ContentResolver resolver = getContentResolver();
374
Chris Wren2bc32ae2012-09-24 14:23:46 -0400375 if (needsDockSettings()) {
Chris Wren2bc32ae2012-09-24 14:23:46 -0400376 mDockSounds = (CheckBoxPreference) findPreference(KEY_DOCK_SOUNDS);
377 mDockSounds.setPersistent(false);
Jeff Sharkey860eceb2012-10-18 15:38:42 -0700378 mDockSounds.setChecked(Settings.Global.getInt(resolver,
379 Settings.Global.DOCK_SOUNDS_ENABLED, 0) != 0);
Eric Laurente00cd9f2012-10-26 18:55:16 -0700380 mDockAudioSettings = findPreference(KEY_DOCK_AUDIO_SETTINGS);
381 mDockAudioSettings.setEnabled(false);
Chris Wren2bc32ae2012-09-24 14:23:46 -0400382 } else {
383 getPreferenceScreen().removePreference(findPreference(KEY_DOCK_CATEGORY));
Eric Laurente00cd9f2012-10-26 18:55:16 -0700384 getPreferenceScreen().removePreference(findPreference(KEY_DOCK_AUDIO_SETTINGS));
Chris Wren2bc32ae2012-09-24 14:23:46 -0400385 getPreferenceScreen().removePreference(findPreference(KEY_DOCK_SOUNDS));
Eric Laurente00cd9f2012-10-26 18:55:16 -0700386 Settings.Global.putInt(resolver, Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, 1);
Chris Wren2bc32ae2012-09-24 14:23:46 -0400387 }
388 }
389
390 private void handleDockChange(Intent intent) {
391 if (mDockAudioSettings != null) {
392 int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, 0);
393
Eric Laurente00cd9f2012-10-26 18:55:16 -0700394 boolean isBluetooth =
395 intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) != null;
Chris Wren2bc32ae2012-09-24 14:23:46 -0400396
Eric Laurente00cd9f2012-10-26 18:55:16 -0700397 mDockIntent = intent;
Chris Wren2bc32ae2012-09-24 14:23:46 -0400398
399 if (dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
400 // remove undocked dialog if currently showing.
401 try {
402 removeDialog(DIALOG_NOT_DOCKED);
403 } catch (IllegalArgumentException iae) {
404 // Maybe it was already dismissed
405 }
Eric Laurente00cd9f2012-10-26 18:55:16 -0700406
407 if (isBluetooth) {
408 mDockAudioSettings.setEnabled(true);
409 } else {
410 if (dockState == Intent.EXTRA_DOCK_STATE_LE_DESK) {
411 ContentResolver resolver = getContentResolver();
412 mDockAudioSettings.setEnabled(true);
413 if (Settings.Global.getInt(resolver,
414 Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, -1) == -1) {
415 Settings.Global.putInt(resolver,
416 Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, 0);
417 }
418 mDockAudioMediaEnabled =
419 (CheckBoxPreference) findPreference(KEY_DOCK_AUDIO_MEDIA_ENABLED);
420 mDockAudioMediaEnabled.setPersistent(false);
421 mDockAudioMediaEnabled.setChecked(
422 Settings.Global.getInt(resolver,
423 Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, 0) != 0);
424 } else {
425 mDockAudioSettings.setEnabled(false);
426 }
427 }
428 } else {
429 mDockAudioSettings.setEnabled(false);
Chris Wren2bc32ae2012-09-24 14:23:46 -0400430 }
431 }
432 }
433
434 @Override
435 public Dialog onCreateDialog(int id) {
436 if (id == DIALOG_NOT_DOCKED) {
437 return createUndockedMessage();
438 }
439 return null;
440 }
441
442 private Dialog createUndockedMessage() {
443 final AlertDialog.Builder ab = new AlertDialog.Builder(getActivity());
444 ab.setTitle(R.string.dock_not_found_title);
445 ab.setMessage(R.string.dock_not_found_text);
446 ab.setPositiveButton(android.R.string.ok, null);
447 return ab.create();
448 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800449}
Chris Wren2bc32ae2012-09-24 14:23:46 -0400450