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.app.ActionBar; 20 import android.content.res.Resources; 21 import android.media.AudioManager; 22 import android.os.Bundle; 23 import android.preference.CheckBoxPreference; 24 import android.preference.ListPreference; 25 import android.preference.Preference; 26 import android.preference.PreferenceActivity; 27 import android.preference.PreferenceScreen; 28 import android.provider.Settings; 29 import android.text.format.DateUtils; 30 import android.view.Menu; 31 import android.view.MenuItem; 32 33 import java.util.TimeZone; 34 35 /** 36 * Settings for the Alarm Clock. 37 */ 38 public class SettingsActivity extends PreferenceActivity 39 implements Preference.OnPreferenceChangeListener { 40 41 private static final int ALARM_STREAM_TYPE_BIT = 42 1 << AudioManager.STREAM_ALARM; 43 44 static final String KEY_ALARM_IN_SILENT_MODE = 45 "alarm_in_silent_mode"; 46 static final String KEY_ALARM_SNOOZE = 47 "snooze_duration"; 48 static final String KEY_VOLUME_BEHAVIOR = 49 "volume_button_setting"; 50 static final String KEY_AUTO_SILENCE = 51 "auto_silence"; 52 public static final String KEY_CLOCK_STYLE = 53 "clock_style"; 54 public static final String KEY_HOME_TZ = 55 "home_time_zone"; 56 public static final String KEY_AUTO_HOME_CLOCK = 57 "automatic_home_clock"; 58 static final String KEY_VOLUME_BUTTONS = 59 "volume_button_setting"; 60 61 private static CharSequence[][] mTimezones; 62 private long mTime; 63 64 private static final boolean SHOW_DAYLIGHT_SAVINGS_INDICATOR = false; 65 66 @Override 67 protected void onCreate(Bundle savedInstanceState) { 68 super.onCreate(savedInstanceState); 69 addPreferencesFromResource(R.xml.settings); 70 71 ActionBar actionBar = getActionBar(); 72 if (actionBar != null) { 73 actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP); 74 } 75 76 // We don't want to reconstruct the timezone list every single time 77 // onResume() is called so we do it once in onCreate 78 ListPreference listPref; 79 listPref = (ListPreference) findPreference(KEY_HOME_TZ); 80 if (mTimezones == null) { 81 mTime = System.currentTimeMillis(); 82 mTimezones = getAllTimezones(); 83 } 84 85 listPref.setEntryValues(mTimezones[0]); 86 listPref.setEntries(mTimezones[1]); 87 listPref.setSummary(listPref.getEntry()); 88 listPref.setOnPreferenceChangeListener(this); 89 } 90 91 @Override 92 protected void onResume() { 93 super.onResume(); 94 refresh(); 95 } 96 97 @Override 98 public boolean onOptionsItemSelected (MenuItem item) { 99 switch (item.getItemId()) { 100 case android.R.id.home: 101 finish(); 102 return true; 103 default: 104 break; 105 } 106 return super.onOptionsItemSelected(item); 107 } 108 109 110 @Override 111 public boolean onCreateOptionsMenu (Menu menu) { 112 getMenuInflater().inflate(R.menu.settings_menu, menu); 113 MenuItem help = menu.findItem(R.id.menu_item_help); 114 if (help != null) { 115 Utils.prepareHelpMenuItem(this, help); 116 } 117 return super.onCreateOptionsMenu(menu); 118 } 119 120 @Override 121 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, 122 Preference preference) { 123 if (KEY_ALARM_IN_SILENT_MODE.equals(preference.getKey())) { 124 CheckBoxPreference pref = (CheckBoxPreference) preference; 125 int ringerModeStreamTypes = Settings.System.getInt( 126 getContentResolver(), 127 Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0); 128 129 if (pref.isChecked()) { 130 ringerModeStreamTypes &= ~ALARM_STREAM_TYPE_BIT; 131 } else { 132 ringerModeStreamTypes |= ALARM_STREAM_TYPE_BIT; 133 } 134 135 Settings.System.putInt(getContentResolver(), 136 Settings.System.MODE_RINGER_STREAMS_AFFECTED, 137 ringerModeStreamTypes); 138 139 return true; 140 } 141 142 return super.onPreferenceTreeClick(preferenceScreen, preference); 143 } 144 145 @Override 146 public boolean onPreferenceChange(Preference pref, Object newValue) { 147 if (KEY_AUTO_SILENCE.equals(pref.getKey())) { 148 final ListPreference listPref = (ListPreference) pref; 149 String delay = (String) newValue; 150 updateAutoSnoozeSummary(listPref, delay); 151 } else if (KEY_CLOCK_STYLE.equals(pref.getKey())) { 152 final ListPreference listPref = (ListPreference) pref; 153 final int idx = listPref.findIndexOfValue((String) newValue); 154 listPref.setSummary(listPref.getEntries()[idx]); 155 } else if (KEY_HOME_TZ.equals(pref.getKey())) { 156 final ListPreference listPref = (ListPreference) pref; 157 final int idx = listPref.findIndexOfValue((String) newValue); 158 listPref.setSummary(listPref.getEntries()[idx]); 159 } else if (KEY_AUTO_HOME_CLOCK.equals(pref.getKey())) { 160 boolean state =((CheckBoxPreference) pref).isChecked(); 161 Preference homeTimeZone = findPreference(KEY_HOME_TZ); 162 homeTimeZone.setEnabled(!state); 163 } else if (KEY_VOLUME_BUTTONS.equals(pref.getKey())) { 164 final ListPreference listPref = (ListPreference) pref; 165 final int idx = listPref.findIndexOfValue((String) newValue); 166 listPref.setSummary(listPref.getEntries()[idx]); 167 } 168 return true; 169 } 170 171 private void updateAutoSnoozeSummary(ListPreference listPref, 172 String delay) { 173 int i = Integer.parseInt(delay); 174 if (i == -1) { 175 listPref.setSummary(R.string.auto_silence_never); 176 } else { 177 listPref.setSummary(getString(R.string.auto_silence_summary, i)); 178 } 179 } 180 181 private void refresh() { 182 ListPreference listPref = (ListPreference) findPreference(KEY_AUTO_SILENCE); 183 String delay = listPref.getValue(); 184 updateAutoSnoozeSummary(listPref, delay); 185 listPref.setOnPreferenceChangeListener(this); 186 187 listPref = (ListPreference) findPreference(KEY_CLOCK_STYLE); 188 listPref.setSummary(listPref.getEntry()); 189 listPref.setOnPreferenceChangeListener(this); 190 191 Preference pref = findPreference(KEY_AUTO_HOME_CLOCK); 192 boolean state =((CheckBoxPreference) pref).isChecked(); 193 pref.setOnPreferenceChangeListener(this); 194 195 listPref = (ListPreference)findPreference(KEY_HOME_TZ); 196 listPref.setEnabled(state); 197 listPref.setSummary(listPref.getEntry()); 198 199 listPref = (ListPreference) findPreference(KEY_VOLUME_BUTTONS); 200 listPref.setSummary(listPref.getEntry()); 201 listPref.setOnPreferenceChangeListener(this); 202 203 SnoozeLengthDialog snoozePref = (SnoozeLengthDialog) findPreference(KEY_ALARM_SNOOZE); 204 snoozePref.setSummary(); 205 } 206 /** 207 * Returns an array of ids/time zones. This returns a double indexed array 208 * of ids and time zones for Calendar. It is an inefficient method and 209 * shouldn't be called often, but can be used for one time generation of 210 * this list. 211 * 212 * @return double array of tz ids and tz names 213 */ 214 public CharSequence[][] getAllTimezones() { 215 Resources resources = this.getResources(); 216 String[] ids = resources.getStringArray(R.array.timezone_values); 217 String[] labels = resources.getStringArray(R.array.timezone_labels); 218 if (ids.length != labels.length) { 219 Log.wtf("Timezone ids and labels have different length!"); 220 } 221 CharSequence[][] timeZones = new CharSequence[2][ids.length]; 222 for (int i = 0; i < ids.length; i++) { 223 timeZones[0][i] = ids[i]; 224 timeZones[1][i] = buildGmtDisplayName(ids[i], labels[i]); 225 } 226 return timeZones; 227 } 228 229 public String buildGmtDisplayName(String id, String displayName) { 230 TimeZone tz = TimeZone.getTimeZone(id); 231 boolean mUseDaylightTime = tz.useDaylightTime(); 232 int mOffset = tz.getOffset(mTime); 233 int p = Math.abs(mOffset); 234 StringBuilder name = new StringBuilder(); 235 name.append("GMT"); 236 237 if (mOffset < 0) { 238 name.append('-'); 239 } else { 240 name.append('+'); 241 } 242 243 name.append(p / (DateUtils.HOUR_IN_MILLIS)); 244 name.append(':'); 245 246 int min = p / 60000; 247 min %= 60; 248 249 if (min < 10) { 250 name.append('0'); 251 } 252 name.append(min); 253 name.insert(0, "("); 254 name.append(") "); 255 name.append(displayName); 256 if (mUseDaylightTime && SHOW_DAYLIGHT_SAVINGS_INDICATOR) { 257 name.append(" \u2600"); // Sun symbol 258 } 259 return name.toString(); 260 } 261 } 262