1 /* 2 * Copyright (C) 2009 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.deskclock; 18 19 import android.content.SharedPreferences; 20 import android.media.AudioManager; 21 import android.media.RingtoneManager; 22 import android.net.Uri; 23 import android.os.Bundle; 24 import android.preference.CheckBoxPreference; 25 import android.preference.ListPreference; 26 import android.preference.Preference; 27 import android.preference.PreferenceActivity; 28 import android.preference.PreferenceScreen; 29 import android.preference.RingtonePreference; 30 import android.provider.Settings; 31 32 /** 33 * Settings for the Alarm Clock. 34 */ 35 public class SettingsActivity extends PreferenceActivity 36 implements Preference.OnPreferenceChangeListener { 37 38 private static final int ALARM_STREAM_TYPE_BIT = 39 1 << AudioManager.STREAM_ALARM; 40 41 private static final String KEY_ALARM_IN_SILENT_MODE = 42 "alarm_in_silent_mode"; 43 static final String KEY_ALARM_SNOOZE = 44 "snooze_duration"; 45 static final String KEY_VOLUME_BEHAVIOR = 46 "volume_button_setting"; 47 static final String KEY_DEFAULT_RINGTONE = 48 "default_ringtone"; 49 static final String KEY_AUTO_SILENCE = 50 "auto_silence"; 51 52 @Override 53 protected void onCreate(Bundle savedInstanceState) { 54 super.onCreate(savedInstanceState); 55 addPreferencesFromResource(R.xml.settings); 56 57 final AlarmPreference ringtone = 58 (AlarmPreference) findPreference(KEY_DEFAULT_RINGTONE); 59 Uri alert = RingtoneManager.getActualDefaultRingtoneUri(this, 60 RingtoneManager.TYPE_ALARM); 61 if (alert != null) { 62 ringtone.setAlert(alert); 63 } 64 ringtone.setChangeDefault(); 65 } 66 67 @Override 68 protected void onResume() { 69 super.onResume(); 70 refresh(); 71 } 72 73 @Override 74 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, 75 Preference preference) { 76 if (KEY_ALARM_IN_SILENT_MODE.equals(preference.getKey())) { 77 CheckBoxPreference pref = (CheckBoxPreference) preference; 78 int ringerModeStreamTypes = Settings.System.getInt( 79 getContentResolver(), 80 Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0); 81 82 if (pref.isChecked()) { 83 ringerModeStreamTypes &= ~ALARM_STREAM_TYPE_BIT; 84 } else { 85 ringerModeStreamTypes |= ALARM_STREAM_TYPE_BIT; 86 } 87 88 Settings.System.putInt(getContentResolver(), 89 Settings.System.MODE_RINGER_STREAMS_AFFECTED, 90 ringerModeStreamTypes); 91 92 return true; 93 } 94 95 return super.onPreferenceTreeClick(preferenceScreen, preference); 96 } 97 98 public boolean onPreferenceChange(Preference pref, Object newValue) { 99 if (KEY_ALARM_SNOOZE.equals(pref.getKey())) { 100 final ListPreference listPref = (ListPreference) pref; 101 final int idx = listPref.findIndexOfValue((String) newValue); 102 listPref.setSummary(listPref.getEntries()[idx]); 103 } else if (KEY_AUTO_SILENCE.equals(pref.getKey())) { 104 final ListPreference listPref = (ListPreference) pref; 105 String delay = (String) newValue; 106 updateAutoSnoozeSummary(listPref, delay); 107 } 108 return true; 109 } 110 111 private void updateAutoSnoozeSummary(ListPreference listPref, 112 String delay) { 113 int i = Integer.parseInt(delay); 114 if (i == -1) { 115 listPref.setSummary(R.string.auto_silence_never); 116 } else { 117 listPref.setSummary(getString(R.string.auto_silence_summary, i)); 118 } 119 } 120 121 122 private void refresh() { 123 final CheckBoxPreference alarmInSilentModePref = 124 (CheckBoxPreference) findPreference(KEY_ALARM_IN_SILENT_MODE); 125 final int silentModeStreams = 126 Settings.System.getInt(getContentResolver(), 127 Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0); 128 alarmInSilentModePref.setChecked( 129 (silentModeStreams & ALARM_STREAM_TYPE_BIT) == 0); 130 131 ListPreference listPref = 132 (ListPreference) findPreference(KEY_ALARM_SNOOZE); 133 listPref.setSummary(listPref.getEntry()); 134 listPref.setOnPreferenceChangeListener(this); 135 136 listPref = (ListPreference) findPreference(KEY_AUTO_SILENCE); 137 String delay = listPref.getValue(); 138 updateAutoSnoozeSummary(listPref, delay); 139 listPref.setOnPreferenceChangeListener(this); 140 } 141 } 142