blob: 49928f53e5ab60076e591d0b6f266a2e69e3c9a7 [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
144 getPreferenceScreen().removePreference(findPreference(KEY_EMERGENCY_TONE));
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800145 }
Chouting Zhang386278a2009-06-24 14:25:43 -0500146
Joe Onorato057f1812011-01-12 15:35:47 -0800147 if (!getResources().getBoolean(R.bool.has_silent_mode)) {
Joe Onorato057f1812011-01-12 15:35:47 -0800148 findPreference(KEY_RING_VOLUME).setDependency(null);
149 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800150
Eric Laurent296d9bb2013-03-19 18:24:47 -0700151 if (getResources().getBoolean(com.android.internal.R.bool.config_useFixedVolume)) {
152 // device with fixed volume policy, do not display volumes submenu
153 getPreferenceScreen().removePreference(findPreference(KEY_RING_VOLUME));
154 }
155
Daisuke Miyakawa8b878db2012-05-08 12:26:23 -0700156 mVibrateWhenRinging = (CheckBoxPreference) findPreference(KEY_VIBRATE);
157 mVibrateWhenRinging.setPersistent(false);
158 mVibrateWhenRinging.setChecked(Settings.System.getInt(resolver,
159 Settings.System.VIBRATE_WHEN_RINGING, 0) != 0);
160
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800161 mDtmfTone = (CheckBoxPreference) findPreference(KEY_DTMF_TONE);
162 mDtmfTone.setPersistent(false);
163 mDtmfTone.setChecked(Settings.System.getInt(resolver,
164 Settings.System.DTMF_TONE_WHEN_DIALING, 1) != 0);
165 mSoundEffects = (CheckBoxPreference) findPreference(KEY_SOUND_EFFECTS);
166 mSoundEffects.setPersistent(false);
167 mSoundEffects.setChecked(Settings.System.getInt(resolver,
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700168 Settings.System.SOUND_EFFECTS_ENABLED, 1) != 0);
Dan Murphy22e18682009-09-22 11:47:08 -0500169 mHapticFeedback = (CheckBoxPreference) findPreference(KEY_HAPTIC_FEEDBACK);
170 mHapticFeedback.setPersistent(false);
171 mHapticFeedback.setChecked(Settings.System.getInt(resolver,
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700172 Settings.System.HAPTIC_FEEDBACK_ENABLED, 1) != 0);
Daniel Sandler231d8802010-02-17 15:35:21 -0500173 mLockSounds = (CheckBoxPreference) findPreference(KEY_LOCK_SOUNDS);
174 mLockSounds.setPersistent(false);
175 mLockSounds.setChecked(Settings.System.getInt(resolver,
176 Settings.System.LOCKSCREEN_SOUNDS_ENABLED, 1) != 0);
Chouting Zhang386278a2009-06-24 14:25:43 -0500177
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700178 mRingtonePreference = findPreference(KEY_RINGTONE);
179 mNotificationPreference = findPreference(KEY_NOTIFICATION_SOUND);
180
Daisuke Miyakawa8b878db2012-05-08 12:26:23 -0700181 Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
182 if (vibrator == null || !vibrator.hasVibrator()) {
Amith Yamasanic861cf82012-10-02 14:51:46 -0700183 removePreference(KEY_VIBRATE);
184 removePreference(KEY_HAPTIC_FEEDBACK);
185 }
Amith Yamasaniedac9af2010-11-17 09:08:21 -0800186
Chouting Zhang386278a2009-06-24 14:25:43 -0500187 if (TelephonyManager.PHONE_TYPE_CDMA == activePhoneType) {
188 ListPreference emergencyTonePreference =
189 (ListPreference) findPreference(KEY_EMERGENCY_TONE);
Jeff Sharkeya2c12a62012-10-01 13:39:24 -0700190 emergencyTonePreference.setValue(String.valueOf(Settings.Global.getInt(
191 resolver, Settings.Global.EMERGENCY_TONE, FALLBACK_EMERGENCY_TONE_VALUE)));
Chouting Zhang386278a2009-06-24 14:25:43 -0500192 emergencyTonePreference.setOnPreferenceChangeListener(this);
193 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800194
Amith Yamasanid2b3ab02009-12-02 13:56:05 -0800195 mSoundSettings = (PreferenceGroup) findPreference(KEY_SOUND_SETTINGS);
Amith Yamasanid2b3ab02009-12-02 13:56:05 -0800196
Marco Nelissen6eca4b32011-07-15 15:02:03 -0700197 mMusicFx = mSoundSettings.findPreference(KEY_MUSICFX);
198 Intent i = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL);
199 PackageManager p = getPackageManager();
200 List<ResolveInfo> ris = p.queryIntentActivities(i, PackageManager.GET_DISABLED_COMPONENTS);
201 if (ris.size() <= 2) {
202 // no need to show the item if there is no choice for the user to make
203 // note: the built in musicfx panel has two activities (one being a
204 // compatibility shim that launches either the other activity, or a
205 // third party one), hence the check for <=2. If the implementation
206 // of the compatbility layer changes, this check may need to be updated.
207 mSoundSettings.removePreference(mMusicFx);
208 }
209
Amith Yamasani60133dd2010-09-11 14:17:31 -0700210 if (!Utils.isVoiceCapable(getActivity())) {
211 for (String prefKey : NEED_VOICE_CAPABILITY) {
212 Preference pref = findPreference(prefKey);
213 if (pref != null) {
214 getPreferenceScreen().removePreference(pref);
215 }
216 }
217 }
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700218
219 mRingtoneLookupRunnable = new Runnable() {
220 public void run() {
221 if (mRingtonePreference != null) {
222 updateRingtoneName(RingtoneManager.TYPE_RINGTONE, mRingtonePreference,
223 MSG_UPDATE_RINGTONE_SUMMARY);
224 }
225 if (mNotificationPreference != null) {
226 updateRingtoneName(RingtoneManager.TYPE_NOTIFICATION, mNotificationPreference,
227 MSG_UPDATE_NOTIFICATION_SUMMARY);
228 }
229 }
230 };
Chris Wren2bc32ae2012-09-24 14:23:46 -0400231
232 initDockSettings();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800233 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800234
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800235 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700236 public void onResume() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800237 super.onResume();
Michael Chandf9504e2009-12-06 01:04:04 -0800238
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700239 lookupRingtoneNames();
Chris Wren2bc32ae2012-09-24 14:23:46 -0400240
241 IntentFilter filter = new IntentFilter(Intent.ACTION_DOCK_EVENT);
242 getActivity().registerReceiver(mReceiver, filter);
243 }
244
245 @Override
246 public void onPause() {
247 super.onPause();
248
249 getActivity().unregisterReceiver(mReceiver);
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700250 }
Michael Chandf9504e2009-12-06 01:04:04 -0800251
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700252 private void updateRingtoneName(int type, Preference preference, int msg) {
253 if (preference == null) return;
Dan Sandler347f9442014-03-11 01:32:47 -0400254 final CharSequence summary = updateRingtoneName(getActivity(), type);
255 if (summary == null) return;
256 mHandler.sendMessage(mHandler.obtainMessage(msg, summary));
257 }
258
259 public static CharSequence updateRingtoneName(Context context, int type) {
260 if (context == null) return null;
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700261 Uri ringtoneUri = RingtoneManager.getActualDefaultRingtoneUri(context, type);
262 CharSequence summary = context.getString(com.android.internal.R.string.ringtone_unknown);
263 // Is it a silent ringtone?
264 if (ringtoneUri == null) {
265 summary = context.getString(com.android.internal.R.string.ringtone_silent);
266 } else {
267 // Fetch the ringtone title from the media provider
268 try {
269 Cursor cursor = context.getContentResolver().query(ringtoneUri,
270 new String[] { MediaStore.Audio.Media.TITLE }, null, null, null);
Amith Yamasanic86755b2011-10-04 15:00:21 -0700271 if (cursor != null) {
272 if (cursor.moveToFirst()) {
273 summary = cursor.getString(0);
274 }
275 cursor.close();
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700276 }
277 } catch (SQLiteException sqle) {
278 // Unknown title for the ringtone
279 }
280 }
Dan Sandler347f9442014-03-11 01:32:47 -0400281 return summary;
Amith Yamasani823bf0c2011-08-01 15:55:06 -0700282 }
283
284 private void lookupRingtoneNames() {
285 new Thread(mRingtoneLookupRunnable).start();
Dianne Hackborn76315392009-09-03 13:33:55 -0700286 }
Michael Chandf9504e2009-12-06 01:04:04 -0800287
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800288 @Override
289 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
Daisuke Miyakawa8b878db2012-05-08 12:26:23 -0700290 if (preference == mVibrateWhenRinging) {
291 Settings.System.putInt(getContentResolver(), Settings.System.VIBRATE_WHEN_RINGING,
292 mVibrateWhenRinging.isChecked() ? 1 : 0);
293 } else if (preference == mDtmfTone) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800294 Settings.System.putInt(getContentResolver(), Settings.System.DTMF_TONE_WHEN_DIALING,
295 mDtmfTone.isChecked() ? 1 : 0);
Michael Chandf9504e2009-12-06 01:04:04 -0800296
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800297 } else if (preference == mSoundEffects) {
298 if (mSoundEffects.isChecked()) {
299 mAudioManager.loadSoundEffects();
300 } else {
301 mAudioManager.unloadSoundEffects();
302 }
303 Settings.System.putInt(getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED,
304 mSoundEffects.isChecked() ? 1 : 0);
Dan Murphy22e18682009-09-22 11:47:08 -0500305
306 } else if (preference == mHapticFeedback) {
307 Settings.System.putInt(getContentResolver(), Settings.System.HAPTIC_FEEDBACK_ENABLED,
308 mHapticFeedback.isChecked() ? 1 : 0);
Michael Chandf9504e2009-12-06 01:04:04 -0800309
Daniel Sandler231d8802010-02-17 15:35:21 -0500310 } else if (preference == mLockSounds) {
311 Settings.System.putInt(getContentResolver(), Settings.System.LOCKSCREEN_SOUNDS_ENABLED,
312 mLockSounds.isChecked() ? 1 : 0);
313
Marco Nelissen6eca4b32011-07-15 15:02:03 -0700314 } else if (preference == mMusicFx) {
315 // let the framework fire off the intent
316 return false;
Chris Wren2bc32ae2012-09-24 14:23:46 -0400317 } else if (preference == mDockAudioSettings) {
318 int dockState = mDockIntent != null
319 ? mDockIntent.getIntExtra(Intent.EXTRA_DOCK_STATE, 0)
320 : Intent.EXTRA_DOCK_STATE_UNDOCKED;
Eric Laurente00cd9f2012-10-26 18:55:16 -0700321
Chris Wren2bc32ae2012-09-24 14:23:46 -0400322 if (dockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) {
323 showDialog(DIALOG_NOT_DOCKED);
324 } else {
Eric Laurente00cd9f2012-10-26 18:55:16 -0700325 boolean isBluetooth = mDockIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) != null;
326
327 if (isBluetooth) {
328 Intent i = new Intent(mDockIntent);
329 i.setAction(DockEventReceiver.ACTION_DOCK_SHOW_UI);
330 i.setClass(getActivity(), DockEventReceiver.class);
331 getActivity().sendBroadcast(i);
332 } else {
333 PreferenceScreen ps = (PreferenceScreen)mDockAudioSettings;
334 Bundle extras = ps.getExtras();
335 extras.putBoolean("checked",
336 Settings.Global.getInt(getContentResolver(),
337 Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, 0) == 1);
338 super.onPreferenceTreeClick(ps, ps);
339 }
Chris Wren2bc32ae2012-09-24 14:23:46 -0400340 }
341 } else if (preference == mDockSounds) {
Jeff Sharkey860eceb2012-10-18 15:38:42 -0700342 Settings.Global.putInt(getContentResolver(), Settings.Global.DOCK_SOUNDS_ENABLED,
Chris Wren2bc32ae2012-09-24 14:23:46 -0400343 mDockSounds.isChecked() ? 1 : 0);
Eric Laurente00cd9f2012-10-26 18:55:16 -0700344 } else if (preference == mDockAudioMediaEnabled) {
345 Settings.Global.putInt(getContentResolver(), Settings.Global.DOCK_AUDIO_MEDIA_ENABLED,
346 mDockAudioMediaEnabled.isChecked() ? 1 : 0);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800347 }
348 return true;
349 }
350
351 public boolean onPreferenceChange(Preference preference, Object objValue) {
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800352 final String key = preference.getKey();
Amith Yamasani992f1022010-01-25 09:17:53 -0800353 if (KEY_EMERGENCY_TONE.equals(key)) {
Chouting Zhang386278a2009-06-24 14:25:43 -0500354 try {
Peter Kuterna378b6f62010-11-28 22:35:31 +0100355 int value = Integer.parseInt((String) objValue);
Jeff Sharkeya2c12a62012-10-01 13:39:24 -0700356 Settings.Global.putInt(getContentResolver(),
357 Settings.Global.EMERGENCY_TONE, value);
Chouting Zhang386278a2009-06-24 14:25:43 -0500358 } catch (NumberFormatException e) {
359 Log.e(TAG, "could not persist emergency tone setting", e);
360 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800361 }
Amith Yamasani8f2fb65b2009-12-01 19:06:14 -0800362
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800363 return true;
364 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700365
366 @Override
367 protected int getHelpResource() {
368 return R.string.help_url_sound;
369 }
Chris Wren2bc32ae2012-09-24 14:23:46 -0400370
371 private boolean needsDockSettings() {
372 return getResources().getBoolean(R.bool.has_dock_settings);
373 }
374
375 private void initDockSettings() {
Eric Laurente00cd9f2012-10-26 18:55:16 -0700376 ContentResolver resolver = getContentResolver();
377
Chris Wren2bc32ae2012-09-24 14:23:46 -0400378 if (needsDockSettings()) {
Chris Wren2bc32ae2012-09-24 14:23:46 -0400379 mDockSounds = (CheckBoxPreference) findPreference(KEY_DOCK_SOUNDS);
380 mDockSounds.setPersistent(false);
Jeff Sharkey860eceb2012-10-18 15:38:42 -0700381 mDockSounds.setChecked(Settings.Global.getInt(resolver,
382 Settings.Global.DOCK_SOUNDS_ENABLED, 0) != 0);
Eric Laurente00cd9f2012-10-26 18:55:16 -0700383 mDockAudioSettings = findPreference(KEY_DOCK_AUDIO_SETTINGS);
384 mDockAudioSettings.setEnabled(false);
Chris Wren2bc32ae2012-09-24 14:23:46 -0400385 } else {
386 getPreferenceScreen().removePreference(findPreference(KEY_DOCK_CATEGORY));
Eric Laurente00cd9f2012-10-26 18:55:16 -0700387 getPreferenceScreen().removePreference(findPreference(KEY_DOCK_AUDIO_SETTINGS));
Chris Wren2bc32ae2012-09-24 14:23:46 -0400388 getPreferenceScreen().removePreference(findPreference(KEY_DOCK_SOUNDS));
Eric Laurente00cd9f2012-10-26 18:55:16 -0700389 Settings.Global.putInt(resolver, Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, 1);
Chris Wren2bc32ae2012-09-24 14:23:46 -0400390 }
391 }
392
393 private void handleDockChange(Intent intent) {
394 if (mDockAudioSettings != null) {
395 int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, 0);
396
Eric Laurente00cd9f2012-10-26 18:55:16 -0700397 boolean isBluetooth =
398 intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) != null;
Chris Wren2bc32ae2012-09-24 14:23:46 -0400399
Eric Laurente00cd9f2012-10-26 18:55:16 -0700400 mDockIntent = intent;
Chris Wren2bc32ae2012-09-24 14:23:46 -0400401
402 if (dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
403 // remove undocked dialog if currently showing.
404 try {
405 removeDialog(DIALOG_NOT_DOCKED);
406 } catch (IllegalArgumentException iae) {
407 // Maybe it was already dismissed
408 }
Eric Laurente00cd9f2012-10-26 18:55:16 -0700409
410 if (isBluetooth) {
411 mDockAudioSettings.setEnabled(true);
412 } else {
413 if (dockState == Intent.EXTRA_DOCK_STATE_LE_DESK) {
414 ContentResolver resolver = getContentResolver();
415 mDockAudioSettings.setEnabled(true);
416 if (Settings.Global.getInt(resolver,
417 Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, -1) == -1) {
418 Settings.Global.putInt(resolver,
419 Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, 0);
420 }
421 mDockAudioMediaEnabled =
422 (CheckBoxPreference) findPreference(KEY_DOCK_AUDIO_MEDIA_ENABLED);
423 mDockAudioMediaEnabled.setPersistent(false);
424 mDockAudioMediaEnabled.setChecked(
425 Settings.Global.getInt(resolver,
426 Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, 0) != 0);
427 } else {
428 mDockAudioSettings.setEnabled(false);
429 }
430 }
431 } else {
432 mDockAudioSettings.setEnabled(false);
Chris Wren2bc32ae2012-09-24 14:23:46 -0400433 }
434 }
435 }
436
437 @Override
438 public Dialog onCreateDialog(int id) {
439 if (id == DIALOG_NOT_DOCKED) {
440 return createUndockedMessage();
441 }
442 return null;
443 }
444
445 private Dialog createUndockedMessage() {
446 final AlertDialog.Builder ab = new AlertDialog.Builder(getActivity());
447 ab.setTitle(R.string.dock_not_found_title);
448 ab.setMessage(R.string.dock_not_found_text);
449 ab.setPositiveButton(android.R.string.ok, null);
450 return ab.create();
451 }
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -0700452
453 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
454 new BaseSearchIndexProvider() {
455 @Override
456 public List<String> getNonIndexableKeys(Context context) {
457 final List<String> keys = new ArrayList<String>();
458
459 int activePhoneType = TelephonyManager.getDefault().getCurrentPhoneType();
460
461 if (TelephonyManager.PHONE_TYPE_CDMA != activePhoneType) {
462 // device is not CDMA, do not display CDMA emergency_tone
463 keys.add(KEY_EMERGENCY_TONE);
464 }
465
466 return keys;
467 }
468 };
469
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800470}
Chris Wren2bc32ae2012-09-24 14:23:46 -0400471