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.alarmclock; 18 19 import android.media.AudioManager; 20 import android.os.Bundle; 21 import android.preference.CheckBoxPreference; 22 import android.preference.ListPreference; 23 import android.preference.Preference; 24 import android.preference.PreferenceActivity; 25 import android.preference.PreferenceScreen; 26 import android.provider.Settings; 27 28 /** 29 * Settings for the Alarm Clock. 30 */ 31 public class SettingsActivity extends PreferenceActivity 32 implements Preference.OnPreferenceChangeListener { 33 34 private static final int ALARM_STREAM_TYPE_BIT = 35 1 << AudioManager.STREAM_ALARM; 36 37 private static final String KEY_ALARM_IN_SILENT_MODE = 38 "alarm_in_silent_mode"; 39 static final String KEY_ALARM_SNOOZE = 40 "snooze_duration"; 41 static final String KEY_VOLUME_BEHAVIOR = 42 "volume_button_setting"; 43 44 @Override onCreate(Bundle savedInstanceState)45 protected void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 addPreferencesFromResource(R.xml.settings); 48 } 49 50 @Override onResume()51 protected void onResume() { 52 super.onResume(); 53 refresh(); 54 } 55 56 @Override onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)57 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, 58 Preference preference) { 59 if (KEY_ALARM_IN_SILENT_MODE.equals(preference.getKey())) { 60 CheckBoxPreference pref = (CheckBoxPreference) preference; 61 int ringerModeStreamTypes = Settings.System.getInt( 62 getContentResolver(), 63 Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0); 64 65 if (pref.isChecked()) { 66 ringerModeStreamTypes &= ~ALARM_STREAM_TYPE_BIT; 67 } else { 68 ringerModeStreamTypes |= ALARM_STREAM_TYPE_BIT; 69 } 70 71 Settings.System.putInt(getContentResolver(), 72 Settings.System.MODE_RINGER_STREAMS_AFFECTED, 73 ringerModeStreamTypes); 74 75 return true; 76 } 77 78 return super.onPreferenceTreeClick(preferenceScreen, preference); 79 } 80 onPreferenceChange(Preference pref, Object newValue)81 public boolean onPreferenceChange(Preference pref, Object newValue) { 82 final ListPreference listPref = (ListPreference) pref; 83 final int idx = listPref.findIndexOfValue((String) newValue); 84 listPref.setSummary(listPref.getEntries()[idx]); 85 return true; 86 } 87 refresh()88 private void refresh() { 89 final CheckBoxPreference alarmInSilentModePref = 90 (CheckBoxPreference) findPreference(KEY_ALARM_IN_SILENT_MODE); 91 final int silentModeStreams = 92 Settings.System.getInt(getContentResolver(), 93 Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0); 94 alarmInSilentModePref.setChecked( 95 (silentModeStreams & ALARM_STREAM_TYPE_BIT) == 0); 96 97 final ListPreference snooze = 98 (ListPreference) findPreference(KEY_ALARM_SNOOZE); 99 snooze.setSummary(snooze.getEntry()); 100 snooze.setOnPreferenceChangeListener(this); 101 } 102 103 } 104