OSDN Git Service

am 04b646df: Merge "Possible NumberFormatException of parseInt is outside try-catch"
[android-x86/packages-apps-Settings.git] / src / com / android / settings / SoundSettings.java
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.android.settings;
18
19 import android.content.BroadcastReceiver;
20 import android.content.ContentResolver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.media.AudioManager;
25 import android.os.Bundle;
26 import android.os.RemoteException;
27 import android.os.ServiceManager;
28 import android.preference.CheckBoxPreference;
29 import android.preference.ListPreference;
30 import android.preference.Preference;
31 import android.preference.PreferenceActivity;
32 import android.preference.PreferenceGroup;
33 import android.preference.PreferenceScreen;
34 import android.provider.Settings;
35 import android.provider.Settings.SettingNotFoundException;
36 import android.telephony.TelephonyManager;
37 import android.util.Log;
38 import android.view.IWindowManager;
39
40 public class SoundSettings extends PreferenceActivity implements
41         Preference.OnPreferenceChangeListener {
42     private static final String TAG = "SoundAndDisplaysSettings";
43
44     /** If there is no setting in the provider, use this. */
45     private static final int FALLBACK_SCREEN_TIMEOUT_VALUE = 30000;
46     private static final int FALLBACK_EMERGENCY_TONE_VALUE = 0;
47
48     private static final String KEY_SILENT = "silent";
49     private static final String KEY_VIBRATE = "vibrate";
50     private static final String KEY_DTMF_TONE = "dtmf_tone";
51     private static final String KEY_SOUND_EFFECTS = "sound_effects";
52     private static final String KEY_HAPTIC_FEEDBACK = "haptic_feedback";
53     private static final String KEY_EMERGENCY_TONE = "emergency_tone";
54     private static final String KEY_SOUND_SETTINGS = "sound_settings";
55     private static final String KEY_NOTIFICATION_PULSE = "notification_pulse";
56     private static final String KEY_LOCK_SOUNDS = "lock_sounds";
57
58     private static final String VALUE_VIBRATE_NEVER = "never";
59     private static final String VALUE_VIBRATE_ALWAYS = "always";
60     private static final String VALUE_VIBRATE_ONLY_SILENT = "silent";
61     private static final String VALUE_VIBRATE_UNLESS_SILENT = "notsilent";
62
63     private CheckBoxPreference mSilent;
64
65     /*
66      * If we are currently in one of the silent modes (the ringer mode is set to either
67      * "silent mode" or "vibrate mode"), then toggling the "Phone vibrate"
68      * preference will switch between "silent mode" and "vibrate mode".
69      * Otherwise, it will adjust the normal ringer mode's ring or ring+vibrate
70      * setting.
71      */
72     private ListPreference mVibrate;
73     private CheckBoxPreference mDtmfTone;
74     private CheckBoxPreference mSoundEffects;
75     private CheckBoxPreference mHapticFeedback;
76     private CheckBoxPreference mNotificationPulse;
77     private CheckBoxPreference mLockSounds;
78
79     private AudioManager mAudioManager;
80
81     private BroadcastReceiver mReceiver = new BroadcastReceiver() {
82         @Override
83         public void onReceive(Context context, Intent intent) {
84             if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
85                 updateState(false);
86             }
87         }
88     };
89
90     private PreferenceGroup mSoundSettings;
91
92     @Override
93     protected void onCreate(Bundle savedInstanceState) {
94         super.onCreate(savedInstanceState);
95         ContentResolver resolver = getContentResolver();
96         int activePhoneType = TelephonyManager.getDefault().getPhoneType();
97
98         mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
99
100         addPreferencesFromResource(R.xml.sound_settings);
101
102         if (TelephonyManager.PHONE_TYPE_CDMA != activePhoneType) {
103             // device is not CDMA, do not display CDMA emergency_tone
104             getPreferenceScreen().removePreference(findPreference(KEY_EMERGENCY_TONE));
105         }
106
107         mSilent = (CheckBoxPreference) findPreference(KEY_SILENT);
108
109         mVibrate = (ListPreference) findPreference(KEY_VIBRATE);
110         mVibrate.setOnPreferenceChangeListener(this);
111
112         mDtmfTone = (CheckBoxPreference) findPreference(KEY_DTMF_TONE);
113         mDtmfTone.setPersistent(false);
114         mDtmfTone.setChecked(Settings.System.getInt(resolver,
115                 Settings.System.DTMF_TONE_WHEN_DIALING, 1) != 0);
116         mSoundEffects = (CheckBoxPreference) findPreference(KEY_SOUND_EFFECTS);
117         mSoundEffects.setPersistent(false);
118         mSoundEffects.setChecked(Settings.System.getInt(resolver,
119                 Settings.System.SOUND_EFFECTS_ENABLED, 0) != 0);
120         mHapticFeedback = (CheckBoxPreference) findPreference(KEY_HAPTIC_FEEDBACK);
121         mHapticFeedback.setPersistent(false);
122         mHapticFeedback.setChecked(Settings.System.getInt(resolver,
123                 Settings.System.HAPTIC_FEEDBACK_ENABLED, 0) != 0);
124         mLockSounds = (CheckBoxPreference) findPreference(KEY_LOCK_SOUNDS);
125         mLockSounds.setPersistent(false);
126         mLockSounds.setChecked(Settings.System.getInt(resolver,
127                 Settings.System.LOCKSCREEN_SOUNDS_ENABLED, 1) != 0);
128
129         if (TelephonyManager.PHONE_TYPE_CDMA == activePhoneType) {
130             ListPreference emergencyTonePreference =
131                 (ListPreference) findPreference(KEY_EMERGENCY_TONE);
132             emergencyTonePreference.setValue(String.valueOf(Settings.System.getInt(
133                 resolver, Settings.System.EMERGENCY_TONE, FALLBACK_EMERGENCY_TONE_VALUE)));
134             emergencyTonePreference.setOnPreferenceChangeListener(this);
135         }
136
137         mSoundSettings = (PreferenceGroup) findPreference(KEY_SOUND_SETTINGS);
138         mNotificationPulse = (CheckBoxPreference)
139                 mSoundSettings.findPreference(KEY_NOTIFICATION_PULSE);
140         if (mNotificationPulse != null &&
141                 getResources().getBoolean(R.bool.has_intrusive_led) == false) {
142             mSoundSettings.removePreference(mNotificationPulse);
143         } else {
144             try {
145                 mNotificationPulse.setChecked(Settings.System.getInt(resolver,
146                         Settings.System.NOTIFICATION_LIGHT_PULSE) == 1);
147                 mNotificationPulse.setOnPreferenceChangeListener(this);
148             } catch (SettingNotFoundException snfe) {
149                 Log.e(TAG, Settings.System.NOTIFICATION_LIGHT_PULSE + " not found");
150             }
151         }
152
153     }
154
155     @Override
156     protected void onResume() {
157         super.onResume();
158
159         updateState(true);
160
161         IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
162         registerReceiver(mReceiver, filter);
163     }
164
165     @Override
166     protected void onPause() {
167         super.onPause();
168
169         unregisterReceiver(mReceiver);
170     }
171
172     private String getPhoneVibrateSettingValue() {
173         boolean vibeInSilent = (Settings.System.getInt(
174             getContentResolver(),
175             Settings.System.VIBRATE_IN_SILENT,
176             1) == 1);
177
178         // Control phone vibe independent of silent mode
179         int callsVibrateSetting = 
180             mAudioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER);
181
182         if (vibeInSilent) {
183             if (callsVibrateSetting == AudioManager.VIBRATE_SETTING_OFF) {
184                 // this state does not make sense; fix it up for the user
185                 mAudioManager.setVibrateSetting(
186                     AudioManager.VIBRATE_TYPE_RINGER,
187                     AudioManager.VIBRATE_SETTING_ONLY_SILENT);
188             }
189             if (callsVibrateSetting == AudioManager.VIBRATE_SETTING_ON) {
190                 return VALUE_VIBRATE_ALWAYS;
191             } else {
192                 return VALUE_VIBRATE_ONLY_SILENT;
193             }
194         } else {
195             if (callsVibrateSetting == AudioManager.VIBRATE_SETTING_ONLY_SILENT) {
196                 // this state does not make sense; fix it up
197                 mAudioManager.setVibrateSetting(
198                     AudioManager.VIBRATE_TYPE_RINGER,
199                     AudioManager.VIBRATE_SETTING_OFF);
200             }
201             if (callsVibrateSetting == AudioManager.VIBRATE_SETTING_ON) {
202                 return VALUE_VIBRATE_UNLESS_SILENT;
203             } else {
204                 return VALUE_VIBRATE_NEVER;
205             }
206         }
207     }
208
209     private void setPhoneVibrateSettingValue(String value) {
210         boolean vibeInSilent;
211         int callsVibrateSetting;
212
213         if (value.equals(VALUE_VIBRATE_UNLESS_SILENT)) {
214             callsVibrateSetting = AudioManager.VIBRATE_SETTING_ON;
215             vibeInSilent = false;
216         } else if (value.equals(VALUE_VIBRATE_NEVER)) {
217             callsVibrateSetting = AudioManager.VIBRATE_SETTING_OFF;
218             vibeInSilent = false;
219         } else if (value.equals(VALUE_VIBRATE_ONLY_SILENT)) {
220             callsVibrateSetting = AudioManager.VIBRATE_SETTING_ONLY_SILENT;
221             vibeInSilent = true;
222         } else { //VALUE_VIBRATE_ALWAYS
223             callsVibrateSetting = AudioManager.VIBRATE_SETTING_ON;
224             vibeInSilent = true;
225         }
226
227         Settings.System.putInt(getContentResolver(),
228             Settings.System.VIBRATE_IN_SILENT,
229             vibeInSilent ? 1 : 0);
230
231         // might need to switch the ringer mode from one kind of "silent" to
232         // another
233         if (mSilent.isChecked()) {
234             mAudioManager.setRingerMode(
235                 vibeInSilent ? AudioManager.RINGER_MODE_VIBRATE
236                              : AudioManager.RINGER_MODE_SILENT);
237         }
238
239         mAudioManager.setVibrateSetting(
240             AudioManager.VIBRATE_TYPE_RINGER,
241             callsVibrateSetting);
242     }
243
244     // updateState in fact updates the UI to reflect the system state
245     private void updateState(boolean force) {
246         final int ringerMode = mAudioManager.getRingerMode();
247
248         // NB: in the UI we now simply call this "silent mode". A separate
249         // setting controls whether we're in RINGER_MODE_SILENT or
250         // RINGER_MODE_VIBRATE.
251         final boolean silentOrVibrateMode =
252                 ringerMode != AudioManager.RINGER_MODE_NORMAL;
253
254         if (silentOrVibrateMode != mSilent.isChecked() || force) {
255             mSilent.setChecked(silentOrVibrateMode);
256         }
257
258         String phoneVibrateSetting = getPhoneVibrateSettingValue();
259
260         if (! phoneVibrateSetting.equals(mVibrate.getValue()) || force) {
261             mVibrate.setValue(phoneVibrateSetting);
262         }
263         mVibrate.setSummary(mVibrate.getEntry());
264
265         int silentModeStreams = Settings.System.getInt(getContentResolver(),
266                 Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
267         boolean isAlarmInclSilentMode = (silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0;
268         mSilent.setSummary(isAlarmInclSilentMode ?
269                 R.string.silent_mode_incl_alarm_summary :
270                 R.string.silent_mode_summary);
271     }
272
273     @Override
274     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
275         if (preference == mSilent) {
276             if (mSilent.isChecked()) {
277                 boolean vibeInSilent = (1 == Settings.System.getInt(
278                     getContentResolver(),
279                     Settings.System.VIBRATE_IN_SILENT,
280                     1));
281                 mAudioManager.setRingerMode(
282                     vibeInSilent ? AudioManager.RINGER_MODE_VIBRATE
283                                  : AudioManager.RINGER_MODE_SILENT);
284             } else {
285                 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
286             }
287             updateState(false);
288         } else if (preference == mDtmfTone) {
289             Settings.System.putInt(getContentResolver(), Settings.System.DTMF_TONE_WHEN_DIALING,
290                     mDtmfTone.isChecked() ? 1 : 0);
291
292         } else if (preference == mSoundEffects) {
293             if (mSoundEffects.isChecked()) {
294                 mAudioManager.loadSoundEffects();
295             } else {
296                 mAudioManager.unloadSoundEffects();
297             }
298             Settings.System.putInt(getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED,
299                     mSoundEffects.isChecked() ? 1 : 0);
300
301         } else if (preference == mHapticFeedback) {
302             Settings.System.putInt(getContentResolver(), Settings.System.HAPTIC_FEEDBACK_ENABLED,
303                     mHapticFeedback.isChecked() ? 1 : 0);
304
305         } else if (preference == mLockSounds) {
306             Settings.System.putInt(getContentResolver(), Settings.System.LOCKSCREEN_SOUNDS_ENABLED,
307                     mLockSounds.isChecked() ? 1 : 0);
308
309         } else if (preference == mNotificationPulse) {
310             boolean value = mNotificationPulse.isChecked();
311             Settings.System.putInt(getContentResolver(),
312                     Settings.System.NOTIFICATION_LIGHT_PULSE, value ? 1 : 0);
313         }
314
315         return true;
316     }
317
318     public boolean onPreferenceChange(Preference preference, Object objValue) {
319         final String key = preference.getKey();
320         if (KEY_EMERGENCY_TONE.equals(key)) {
321             try {
322                 int value = Integer.parseInt((String) objValue);
323                 Settings.System.putInt(getContentResolver(),
324                         Settings.System.EMERGENCY_TONE, value);
325             } catch (NumberFormatException e) {
326                 Log.e(TAG, "could not persist emergency tone setting", e);
327             }
328         } else if (preference == mVibrate) {
329             setPhoneVibrateSettingValue(objValue.toString());
330             updateState(false);
331         }
332
333         return true;
334     }
335 }