blob: eadd482d11cacd006abdfd9542526f582cd3d5a0 [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;
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -070050import com.android.settings.search.BaseSearchIndexProvider;
51import com.android.settings.search.Indexable;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080052
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -070053import java.util.ArrayList;
Marco Nelissen6eca4b32011-07-15 15:02:03 -070054import java.util.List;
55
Amith Yamasanid7993472010-08-18 13:59:28 -070056public class SoundSettings extends SettingsPreferenceFragment implements
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -070057 Preference.OnPreferenceChangeListener, Indexable {
Amith Yamasani823bf0c2011-08-01 15:55:06 -070058 private static final String TAG = "SoundSettings";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080059
Chris Wren2bc32ae2012-09-24 14:23:46 -040060 private static final int DIALOG_NOT_DOCKED = 1;
61
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080062 /** If there is no setting in the provider, use this. */
Chouting Zhang386278a2009-06-24 14:25:43 -050063 private static final int FALLBACK_EMERGENCY_TONE_VALUE = 0;
Michael Chandf9504e2009-12-06 01:04:04 -080064
Daisuke Miyakawa8b878db2012-05-08 12:26:23 -070065 private static final String KEY_VIBRATE = "vibrate_when_ringing";
Joe Onorato057f1812011-01-12 15:35:47 -080066 private static final String KEY_RING_VOLUME = "ring_volume";
Marco Nelissen6eca4b32011-07-15 15:02:03 -070067 private static final String KEY_MUSICFX = "musicfx";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080068 private static final String KEY_DTMF_TONE = "dtmf_tone";
69 private static final String KEY_SOUND_EFFECTS = "sound_effects";
Dan Murphy22e18682009-09-22 11:47:08 -050070 private static final String KEY_HAPTIC_FEEDBACK = "haptic_feedback";
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -080071 private static final String KEY_EMERGENCY_TONE = "emergency_tone";
72 private static final String KEY_SOUND_SETTINGS = "sound_settings";
Daniel Sandler231d8802010-02-17 15:35:21 -050073 private static final String KEY_LOCK_SOUNDS = "lock_sounds";
Amith Yamasani60133dd2010-09-11 14:17:31 -070074 private static final String KEY_RINGTONE = "ringtone";
75 private static final String KEY_NOTIFICATION_SOUND = "notification_sound";
Daisuke Miyakawadba00b62012-05-16 08:41:38 -070076 private static final String KEY_CATEGORY_CALLS = "category_calls_and_notification";
Chris Wren2bc32ae2012-09-24 14:23:46 -040077 private static final String KEY_DOCK_CATEGORY = "dock_category";
Eric Laurente00cd9f2012-10-26 18:55:16 -070078 private static final String KEY_DOCK_AUDIO_SETTINGS = "dock_audio";
Chris Wren2bc32ae2012-09-24 14:23:46 -040079 private static final String KEY_DOCK_SOUNDS = "dock_sounds";
Eric Laurente00cd9f2012-10-26 18:55:16 -070080 private static final String KEY_DOCK_AUDIO_MEDIA_ENABLED = "dock_audio_media_enabled";
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -080081
Amith Yamasani60133dd2010-09-11 14:17:31 -070082 private static final String[] NEED_VOICE_CAPABILITY = {
83 KEY_RINGTONE, KEY_DTMF_TONE, KEY_CATEGORY_CALLS,
Henrik Baard2ac8da32013-02-27 08:57:03 +010084 KEY_EMERGENCY_TONE, KEY_VIBRATE
Amith Yamasani60133dd2010-09-11 14:17:31 -070085 };
86
Amith Yamasani823bf0c2011-08-01 15:55:06 -070087 private static final int MSG_UPDATE_RINGTONE_SUMMARY = 1;
88 private static final int MSG_UPDATE_NOTIFICATION_SUMMARY = 2;
89
Daisuke Miyakawa8b878db2012-05-08 12:26:23 -070090 private CheckBoxPreference mVibrateWhenRinging;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080091 private CheckBoxPreference mDtmfTone;
92 private CheckBoxPreference mSoundEffects;
Dan Murphy22e18682009-09-22 11:47:08 -050093 private CheckBoxPreference mHapticFeedback;
Marco Nelissen6eca4b32011-07-15 15:02:03 -070094 private Preference mMusicFx;
Daniel Sandler231d8802010-02-17 15:35:21 -050095 private CheckBoxPreference mLockSounds;
Amith Yamasani823bf0c2011-08-01 15:55:06 -070096 private Preference mRingtonePreference;
97 private Preference mNotificationPreference;
98
99 private Runnable mRingtoneLookupRunnable;
Michael Chandf9504e2009-12-06 01:04:04 -0800100
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800101 private AudioManager mAudioManager;
Michael Chandf9504e2009-12-06 01:04:04 -0800102
Chris Wren2bc32ae2012-09-24 14:23:46 -0400103 private Preference mDockAudioSettings;
104 private CheckBoxPreference mDockSounds;
105 private Intent mDockIntent;
Eric Laurente00cd9f2012-10-26 18:55:16 -0700106 private CheckBoxPreference mDockAudioMediaEnabled;
Chris Wren2bc32ae2012-09-24 14:23:46 -0400107
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700108 private Handler mHandler = new Handler() {
109 public void handleMessage(Message msg) {
110 switch (msg.what) {
111 case MSG_UPDATE_RINGTONE_SUMMARY:
112 mRingtonePreference.setSummary((CharSequence) msg.obj);
113 break;
114 case MSG_UPDATE_NOTIFICATION_SUMMARY:
115 mNotificationPreference.setSummary((CharSequence) msg.obj);
116 break;
117 }
118 }
119 };
120
Chris Wren2bc32ae2012-09-24 14:23:46 -0400121 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
122 @Override
123 public void onReceive(Context context, Intent intent) {
124 if (intent.getAction().equals(Intent.ACTION_DOCK_EVENT)) {
125 handleDockChange(intent);
126 }
127 }
128 };
129
Amith Yamasanid2b3ab02009-12-02 13:56:05 -0800130 private PreferenceGroup mSoundSettings;
131
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800132 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700133 public void onCreate(Bundle savedInstanceState) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800134 super.onCreate(savedInstanceState);
135 ContentResolver resolver = getContentResolver();
Wink Saville327147e2011-02-02 11:30:25 -0800136 int activePhoneType = TelephonyManager.getDefault().getCurrentPhoneType();
Michael Chandf9504e2009-12-06 01:04:04 -0800137
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800138 mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800139
Amith Yamasani992f1022010-01-25 09:17:53 -0800140 addPreferencesFromResource(R.xml.sound_settings);
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800141
Chouting Zhang386278a2009-06-24 14:25:43 -0500142 if (TelephonyManager.PHONE_TYPE_CDMA != activePhoneType) {
143 // device is not CDMA, do not display CDMA emergency_tone
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700144 Preference pref = findPreference(KEY_EMERGENCY_TONE);
145 if (pref != null) {
146 getPreferenceScreen().removePreference(pref);
147 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800148 }
Chouting Zhang386278a2009-06-24 14:25:43 -0500149
Joe Onorato057f1812011-01-12 15:35:47 -0800150 if (!getResources().getBoolean(R.bool.has_silent_mode)) {
Joe Onorato057f1812011-01-12 15:35:47 -0800151 findPreference(KEY_RING_VOLUME).setDependency(null);
152 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800153
Eric Laurent296d9bb2013-03-19 18:24:47 -0700154 if (getResources().getBoolean(com.android.internal.R.bool.config_useFixedVolume)) {
155 // device with fixed volume policy, do not display volumes submenu
Fabrice Di Meglio769630c2014-04-24 14:48:48 -0700156 Preference pref = findPreference(KEY_RING_VOLUME);
157 if (pref != null) {
158 getPreferenceScreen().removePreference(pref);
159 }
Eric Laurent296d9bb2013-03-19 18:24:47 -0700160 }
161
Daisuke Miyakawa8b878db2012-05-08 12:26:23 -0700162 mVibrateWhenRinging = (CheckBoxPreference) findPreference(KEY_VIBRATE);
163 mVibrateWhenRinging.setPersistent(false);
164 mVibrateWhenRinging.setChecked(Settings.System.getInt(resolver,
165 Settings.System.VIBRATE_WHEN_RINGING, 0) != 0);
166
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800167 mDtmfTone = (CheckBoxPreference) findPreference(KEY_DTMF_TONE);
168 mDtmfTone.setPersistent(false);
169 mDtmfTone.setChecked(Settings.System.getInt(resolver,
170 Settings.System.DTMF_TONE_WHEN_DIALING, 1) != 0);
171 mSoundEffects = (CheckBoxPreference) findPreference(KEY_SOUND_EFFECTS);
172 mSoundEffects.setPersistent(false);
173 mSoundEffects.setChecked(Settings.System.getInt(resolver,
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700174 Settings.System.SOUND_EFFECTS_ENABLED, 1) != 0);
Dan Murphy22e18682009-09-22 11:47:08 -0500175 mHapticFeedback = (CheckBoxPreference) findPreference(KEY_HAPTIC_FEEDBACK);
176 mHapticFeedback.setPersistent(false);
177 mHapticFeedback.setChecked(Settings.System.getInt(resolver,
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700178 Settings.System.HAPTIC_FEEDBACK_ENABLED, 1) != 0);
Daniel Sandler231d8802010-02-17 15:35:21 -0500179 mLockSounds = (CheckBoxPreference) findPreference(KEY_LOCK_SOUNDS);
180 mLockSounds.setPersistent(false);
181 mLockSounds.setChecked(Settings.System.getInt(resolver,
182 Settings.System.LOCKSCREEN_SOUNDS_ENABLED, 1) != 0);
Chouting Zhang386278a2009-06-24 14:25:43 -0500183
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700184 mRingtonePreference = findPreference(KEY_RINGTONE);
185 mNotificationPreference = findPreference(KEY_NOTIFICATION_SOUND);
186
Daisuke Miyakawa8b878db2012-05-08 12:26:23 -0700187 Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
188 if (vibrator == null || !vibrator.hasVibrator()) {
Amith Yamasanic861cf82012-10-02 14:51:46 -0700189 removePreference(KEY_VIBRATE);
190 removePreference(KEY_HAPTIC_FEEDBACK);
191 }
Amith Yamasaniedac9af2010-11-17 09:08:21 -0800192
Chouting Zhang386278a2009-06-24 14:25:43 -0500193 if (TelephonyManager.PHONE_TYPE_CDMA == activePhoneType) {
194 ListPreference emergencyTonePreference =
195 (ListPreference) findPreference(KEY_EMERGENCY_TONE);
Jeff Sharkeya2c12a62012-10-01 13:39:24 -0700196 emergencyTonePreference.setValue(String.valueOf(Settings.Global.getInt(
197 resolver, Settings.Global.EMERGENCY_TONE, FALLBACK_EMERGENCY_TONE_VALUE)));
Chouting Zhang386278a2009-06-24 14:25:43 -0500198 emergencyTonePreference.setOnPreferenceChangeListener(this);
199 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800200
Amith Yamasanid2b3ab02009-12-02 13:56:05 -0800201 mSoundSettings = (PreferenceGroup) findPreference(KEY_SOUND_SETTINGS);
Amith Yamasanid2b3ab02009-12-02 13:56:05 -0800202
Marco Nelissen6eca4b32011-07-15 15:02:03 -0700203 mMusicFx = mSoundSettings.findPreference(KEY_MUSICFX);
204 Intent i = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL);
205 PackageManager p = getPackageManager();
206 List<ResolveInfo> ris = p.queryIntentActivities(i, PackageManager.GET_DISABLED_COMPONENTS);
207 if (ris.size() <= 2) {
208 // no need to show the item if there is no choice for the user to make
209 // note: the built in musicfx panel has two activities (one being a
210 // compatibility shim that launches either the other activity, or a
211 // third party one), hence the check for <=2. If the implementation
212 // of the compatbility layer changes, this check may need to be updated.
213 mSoundSettings.removePreference(mMusicFx);
214 }
215
Amith Yamasani60133dd2010-09-11 14:17:31 -0700216 if (!Utils.isVoiceCapable(getActivity())) {
217 for (String prefKey : NEED_VOICE_CAPABILITY) {
218 Preference pref = findPreference(prefKey);
219 if (pref != null) {
220 getPreferenceScreen().removePreference(pref);
221 }
222 }
223 }
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700224
225 mRingtoneLookupRunnable = new Runnable() {
226 public void run() {
227 if (mRingtonePreference != null) {
228 updateRingtoneName(RingtoneManager.TYPE_RINGTONE, mRingtonePreference,
229 MSG_UPDATE_RINGTONE_SUMMARY);
230 }
231 if (mNotificationPreference != null) {
232 updateRingtoneName(RingtoneManager.TYPE_NOTIFICATION, mNotificationPreference,
233 MSG_UPDATE_NOTIFICATION_SUMMARY);
234 }
235 }
236 };
Chris Wren2bc32ae2012-09-24 14:23:46 -0400237
238 initDockSettings();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800239 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800240
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800241 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700242 public void onResume() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800243 super.onResume();
Michael Chandf9504e2009-12-06 01:04:04 -0800244
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700245 lookupRingtoneNames();
Chris Wren2bc32ae2012-09-24 14:23:46 -0400246
247 IntentFilter filter = new IntentFilter(Intent.ACTION_DOCK_EVENT);
248 getActivity().registerReceiver(mReceiver, filter);
249 }
250
251 @Override
252 public void onPause() {
253 super.onPause();
254
255 getActivity().unregisterReceiver(mReceiver);
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700256 }
Michael Chandf9504e2009-12-06 01:04:04 -0800257
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700258 private void updateRingtoneName(int type, Preference preference, int msg) {
259 if (preference == null) return;
Dan Sandler347f9442014-03-11 01:32:47 -0400260 final CharSequence summary = updateRingtoneName(getActivity(), type);
261 if (summary == null) return;
262 mHandler.sendMessage(mHandler.obtainMessage(msg, summary));
263 }
264
265 public static CharSequence updateRingtoneName(Context context, int type) {
266 if (context == null) return null;
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700267 Uri ringtoneUri = RingtoneManager.getActualDefaultRingtoneUri(context, type);
268 CharSequence summary = context.getString(com.android.internal.R.string.ringtone_unknown);
269 // Is it a silent ringtone?
270 if (ringtoneUri == null) {
271 summary = context.getString(com.android.internal.R.string.ringtone_silent);
272 } else {
273 // Fetch the ringtone title from the media provider
274 try {
275 Cursor cursor = context.getContentResolver().query(ringtoneUri,
276 new String[] { MediaStore.Audio.Media.TITLE }, null, null, null);
Amith Yamasanic86755b2011-10-04 15:00:21 -0700277 if (cursor != null) {
278 if (cursor.moveToFirst()) {
279 summary = cursor.getString(0);
280 }
281 cursor.close();
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700282 }
283 } catch (SQLiteException sqle) {
284 // Unknown title for the ringtone
285 }
286 }
Dan Sandler347f9442014-03-11 01:32:47 -0400287 return summary;
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700288 }
289
290 private void lookupRingtoneNames() {
291 new Thread(mRingtoneLookupRunnable).start();
Dianne Hackborn76315392009-09-03 13:33:55 -0700292 }
Michael Chandf9504e2009-12-06 01:04:04 -0800293
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800294 @Override
295 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
Daisuke Miyakawa8b878db2012-05-08 12:26:23 -0700296 if (preference == mVibrateWhenRinging) {
297 Settings.System.putInt(getContentResolver(), Settings.System.VIBRATE_WHEN_RINGING,
298 mVibrateWhenRinging.isChecked() ? 1 : 0);
299 } else if (preference == mDtmfTone) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800300 Settings.System.putInt(getContentResolver(), Settings.System.DTMF_TONE_WHEN_DIALING,
301 mDtmfTone.isChecked() ? 1 : 0);
Michael Chandf9504e2009-12-06 01:04:04 -0800302
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800303 } else if (preference == mSoundEffects) {
304 if (mSoundEffects.isChecked()) {
305 mAudioManager.loadSoundEffects();
306 } else {
307 mAudioManager.unloadSoundEffects();
308 }
309 Settings.System.putInt(getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED,
310 mSoundEffects.isChecked() ? 1 : 0);
Dan Murphy22e18682009-09-22 11:47:08 -0500311
312 } else if (preference == mHapticFeedback) {
313 Settings.System.putInt(getContentResolver(), Settings.System.HAPTIC_FEEDBACK_ENABLED,
314 mHapticFeedback.isChecked() ? 1 : 0);
Michael Chandf9504e2009-12-06 01:04:04 -0800315
Daniel Sandler231d8802010-02-17 15:35:21 -0500316 } else if (preference == mLockSounds) {
317 Settings.System.putInt(getContentResolver(), Settings.System.LOCKSCREEN_SOUNDS_ENABLED,
318 mLockSounds.isChecked() ? 1 : 0);
319
Marco Nelissen6eca4b32011-07-15 15:02:03 -0700320 } else if (preference == mMusicFx) {
321 // let the framework fire off the intent
322 return false;
Chris Wren2bc32ae2012-09-24 14:23:46 -0400323 } else if (preference == mDockAudioSettings) {
324 int dockState = mDockIntent != null
325 ? mDockIntent.getIntExtra(Intent.EXTRA_DOCK_STATE, 0)
326 : Intent.EXTRA_DOCK_STATE_UNDOCKED;
Eric Laurente00cd9f2012-10-26 18:55:16 -0700327
Chris Wren2bc32ae2012-09-24 14:23:46 -0400328 if (dockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) {
329 showDialog(DIALOG_NOT_DOCKED);
330 } else {
Eric Laurente00cd9f2012-10-26 18:55:16 -0700331 boolean isBluetooth = mDockIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) != null;
332
333 if (isBluetooth) {
334 Intent i = new Intent(mDockIntent);
335 i.setAction(DockEventReceiver.ACTION_DOCK_SHOW_UI);
336 i.setClass(getActivity(), DockEventReceiver.class);
337 getActivity().sendBroadcast(i);
338 } else {
339 PreferenceScreen ps = (PreferenceScreen)mDockAudioSettings;
340 Bundle extras = ps.getExtras();
341 extras.putBoolean("checked",
342 Settings.Global.getInt(getContentResolver(),
343 Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, 0) == 1);
344 super.onPreferenceTreeClick(ps, ps);
345 }
Chris Wren2bc32ae2012-09-24 14:23:46 -0400346 }
347 } else if (preference == mDockSounds) {
Jeff Sharkey860eceb2012-10-18 15:38:42 -0700348 Settings.Global.putInt(getContentResolver(), Settings.Global.DOCK_SOUNDS_ENABLED,
Chris Wren2bc32ae2012-09-24 14:23:46 -0400349 mDockSounds.isChecked() ? 1 : 0);
Eric Laurente00cd9f2012-10-26 18:55:16 -0700350 } else if (preference == mDockAudioMediaEnabled) {
351 Settings.Global.putInt(getContentResolver(), Settings.Global.DOCK_AUDIO_MEDIA_ENABLED,
352 mDockAudioMediaEnabled.isChecked() ? 1 : 0);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800353 }
354 return true;
355 }
356
357 public boolean onPreferenceChange(Preference preference, Object objValue) {
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800358 final String key = preference.getKey();
Amith Yamasani992f1022010-01-25 09:17:53 -0800359 if (KEY_EMERGENCY_TONE.equals(key)) {
Chouting Zhang386278a2009-06-24 14:25:43 -0500360 try {
Peter Kuterna378b6f62010-11-28 22:35:31 +0100361 int value = Integer.parseInt((String) objValue);
Jeff Sharkeya2c12a62012-10-01 13:39:24 -0700362 Settings.Global.putInt(getContentResolver(),
363 Settings.Global.EMERGENCY_TONE, value);
Chouting Zhang386278a2009-06-24 14:25:43 -0500364 } catch (NumberFormatException e) {
365 Log.e(TAG, "could not persist emergency tone setting", e);
366 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800367 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800368
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800369 return true;
370 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700371
372 @Override
373 protected int getHelpResource() {
374 return R.string.help_url_sound;
375 }
Chris Wren2bc32ae2012-09-24 14:23:46 -0400376
377 private boolean needsDockSettings() {
378 return getResources().getBoolean(R.bool.has_dock_settings);
379 }
380
381 private void initDockSettings() {
Eric Laurente00cd9f2012-10-26 18:55:16 -0700382 ContentResolver resolver = getContentResolver();
383
Chris Wren2bc32ae2012-09-24 14:23:46 -0400384 if (needsDockSettings()) {
Chris Wren2bc32ae2012-09-24 14:23:46 -0400385 mDockSounds = (CheckBoxPreference) findPreference(KEY_DOCK_SOUNDS);
386 mDockSounds.setPersistent(false);
Jeff Sharkey860eceb2012-10-18 15:38:42 -0700387 mDockSounds.setChecked(Settings.Global.getInt(resolver,
388 Settings.Global.DOCK_SOUNDS_ENABLED, 0) != 0);
Eric Laurente00cd9f2012-10-26 18:55:16 -0700389 mDockAudioSettings = findPreference(KEY_DOCK_AUDIO_SETTINGS);
390 mDockAudioSettings.setEnabled(false);
Chris Wren2bc32ae2012-09-24 14:23:46 -0400391 } else {
392 getPreferenceScreen().removePreference(findPreference(KEY_DOCK_CATEGORY));
Eric Laurente00cd9f2012-10-26 18:55:16 -0700393 getPreferenceScreen().removePreference(findPreference(KEY_DOCK_AUDIO_SETTINGS));
Chris Wren2bc32ae2012-09-24 14:23:46 -0400394 getPreferenceScreen().removePreference(findPreference(KEY_DOCK_SOUNDS));
Eric Laurente00cd9f2012-10-26 18:55:16 -0700395 Settings.Global.putInt(resolver, Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, 1);
Chris Wren2bc32ae2012-09-24 14:23:46 -0400396 }
397 }
398
399 private void handleDockChange(Intent intent) {
400 if (mDockAudioSettings != null) {
401 int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, 0);
402
Eric Laurente00cd9f2012-10-26 18:55:16 -0700403 boolean isBluetooth =
404 intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) != null;
Chris Wren2bc32ae2012-09-24 14:23:46 -0400405
Eric Laurente00cd9f2012-10-26 18:55:16 -0700406 mDockIntent = intent;
Chris Wren2bc32ae2012-09-24 14:23:46 -0400407
408 if (dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
409 // remove undocked dialog if currently showing.
410 try {
411 removeDialog(DIALOG_NOT_DOCKED);
412 } catch (IllegalArgumentException iae) {
413 // Maybe it was already dismissed
414 }
Eric Laurente00cd9f2012-10-26 18:55:16 -0700415
416 if (isBluetooth) {
417 mDockAudioSettings.setEnabled(true);
418 } else {
419 if (dockState == Intent.EXTRA_DOCK_STATE_LE_DESK) {
420 ContentResolver resolver = getContentResolver();
421 mDockAudioSettings.setEnabled(true);
422 if (Settings.Global.getInt(resolver,
423 Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, -1) == -1) {
424 Settings.Global.putInt(resolver,
425 Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, 0);
426 }
427 mDockAudioMediaEnabled =
428 (CheckBoxPreference) findPreference(KEY_DOCK_AUDIO_MEDIA_ENABLED);
429 mDockAudioMediaEnabled.setPersistent(false);
430 mDockAudioMediaEnabled.setChecked(
431 Settings.Global.getInt(resolver,
432 Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, 0) != 0);
433 } else {
434 mDockAudioSettings.setEnabled(false);
435 }
436 }
437 } else {
438 mDockAudioSettings.setEnabled(false);
Chris Wren2bc32ae2012-09-24 14:23:46 -0400439 }
440 }
441 }
442
443 @Override
444 public Dialog onCreateDialog(int id) {
445 if (id == DIALOG_NOT_DOCKED) {
446 return createUndockedMessage();
447 }
448 return null;
449 }
450
451 private Dialog createUndockedMessage() {
452 final AlertDialog.Builder ab = new AlertDialog.Builder(getActivity());
453 ab.setTitle(R.string.dock_not_found_title);
454 ab.setMessage(R.string.dock_not_found_text);
455 ab.setPositiveButton(android.R.string.ok, null);
456 return ab.create();
457 }
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -0700458
459 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
460 new BaseSearchIndexProvider() {
461 @Override
462 public List<String> getNonIndexableKeys(Context context) {
463 final List<String> keys = new ArrayList<String>();
464
465 int activePhoneType = TelephonyManager.getDefault().getCurrentPhoneType();
466
467 if (TelephonyManager.PHONE_TYPE_CDMA != activePhoneType) {
468 // device is not CDMA, do not display CDMA emergency_tone
469 keys.add(KEY_EMERGENCY_TONE);
470 }
471
472 return keys;
473 }
474 };
475
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800476}
Chris Wren2bc32ae2012-09-24 14:23:46 -0400477