From abbdb5687b2f4505b16d3f8fad3efbab3ea1cf90 Mon Sep 17 00:00:00 2001 From: Patrick Scott Date: Tue, 28 Apr 2009 16:37:04 -0400 Subject: [PATCH] Add a setting to change the snooze duration. Add a ListPreference to the SettingsActivity that has entries for 5, 10, 15, 20, 25, and 30 minutes. Use the entry value as the summary so the user knows the current setting. Use the preference value or the default of 10 in AlarmAlert. --- res/values/strings.xml | 26 +++++++++++++++ res/xml/settings.xml | 8 +++++ src/com/android/alarmclock/AlarmAlert.java | 12 +++++-- src/com/android/alarmclock/SettingsActivity.java | 40 +++++++++++++++++------- 4 files changed, 71 insertions(+), 15 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 817b5a7..15a8a87 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -166,6 +166,32 @@ Play alarm even when the phone is in silent mode + + Snooze duration + + + + 5 minutes + 10 minutes + 15 minutes + 20 minutes + 25 minutes + 30 minutes + + + + + 5 + 10 + 15 + 20 + 25 + 30 + + + diff --git a/res/xml/settings.xml b/res/xml/settings.xml index 5863e89..d8d3366 100644 --- a/res/xml/settings.xml +++ b/res/xml/settings.xml @@ -23,4 +23,12 @@ android:title="@string/alarm_in_silent_mode_title" android:summary="@string/alarm_in_silent_mode_summary" /> + + diff --git a/src/com/android/alarmclock/AlarmAlert.java b/src/com/android/alarmclock/AlarmAlert.java index a89a884..09e2c40 100644 --- a/src/com/android/alarmclock/AlarmAlert.java +++ b/src/com/android/alarmclock/AlarmAlert.java @@ -24,6 +24,7 @@ import android.content.SharedPreferences; import android.content.res.Configuration; import android.graphics.PixelFormat; import android.os.Bundle; +import android.preference.PreferenceManager; import android.view.KeyEvent; import android.view.View; import android.view.ViewGroup; @@ -41,7 +42,7 @@ import java.util.Calendar; */ public class AlarmAlert extends Activity { - private static final int SNOOZE_MINUTES = 10; + private static final String DEFAULT_SNOOZE = "10"; private static final int UNKNOWN = 0; private static final int SNOOZE = 1; private static final int DISMISS = 2; @@ -188,8 +189,13 @@ public class AlarmAlert extends Activity { } // If the next alarm is set for sooner than the snooze interval, don't // snooze. Instead, toast the user that the snooze will not be set. + final String snooze = + PreferenceManager.getDefaultSharedPreferences(this) + .getString("snooze_duration", DEFAULT_SNOOZE); + int snoozeMinutes = Integer.parseInt(snooze); + final long snoozeTime = System.currentTimeMillis() - + (1000 * 60 * SNOOZE_MINUTES); + + (1000 * 60 * snoozeMinutes); final long nextAlarm = Alarms.calculateNextAlert(AlarmAlert.this).getAlert(); String displayTime = null; @@ -204,7 +210,7 @@ public class AlarmAlert extends Activity { mLabel); Alarms.setNextAlert(AlarmAlert.this); displayTime = getString(R.string.alarm_alert_snooze_set, - SNOOZE_MINUTES); + snoozeMinutes); mState = SNOOZE; } // Intentionally log the snooze time for debugging. diff --git a/src/com/android/alarmclock/SettingsActivity.java b/src/com/android/alarmclock/SettingsActivity.java index 44c4045..59bf040 100644 --- a/src/com/android/alarmclock/SettingsActivity.java +++ b/src/com/android/alarmclock/SettingsActivity.java @@ -19,6 +19,7 @@ package com.android.alarmclock; import android.media.AudioManager; import android.os.Bundle; import android.preference.CheckBoxPreference; +import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceScreen; @@ -27,23 +28,31 @@ import android.provider.Settings; /** * Settings for the Alarm Clock. */ -public class SettingsActivity extends PreferenceActivity { +public class SettingsActivity extends PreferenceActivity + implements Preference.OnPreferenceChangeListener { private static final int ALARM_STREAM_TYPE_BIT = 1 << AudioManager.STREAM_ALARM; - + private static final String KEY_ALARM_IN_SILENT_MODE = "alarm_in_silent_mode"; + private static final String KEY_ALARM_SNOOZE = + "snooze_duration"; private CheckBoxPreference mAlarmInSilentModePref; - + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - + addPreferencesFromResource(R.xml.settings); - + mAlarmInSilentModePref = (CheckBoxPreference) findPreference(KEY_ALARM_IN_SILENT_MODE); + + final ListPreference snooze = + (ListPreference) findPreference(KEY_ALARM_SNOOZE); + snooze.setSummary(snooze.getEntry()); + snooze.setOnPreferenceChangeListener(this); } @Override @@ -55,34 +64,41 @@ public class SettingsActivity extends PreferenceActivity { @Override public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { - + if (preference == mAlarmInSilentModePref) { - + int ringerModeStreamTypes = Settings.System.getInt( getContentResolver(), Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0); - + if (mAlarmInSilentModePref.isChecked()) { ringerModeStreamTypes &= ~ALARM_STREAM_TYPE_BIT; } else { ringerModeStreamTypes |= ALARM_STREAM_TYPE_BIT; } - + Settings.System.putInt(getContentResolver(), Settings.System.MODE_RINGER_STREAMS_AFFECTED, ringerModeStreamTypes); - + return true; } - + return super.onPreferenceTreeClick(preferenceScreen, preference); } + public boolean onPreferenceChange(Preference pref, Object newValue) { + ListPreference listPref = (ListPreference) pref; + int idx = listPref.findIndexOfValue((String) newValue); + listPref.setSummary(listPref.getEntries()[idx]); + return true; + } + private void refresh() { int silentModeStreams = Settings.System.getInt(getContentResolver(), Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0); mAlarmInSilentModePref.setChecked( (silentModeStreams & ALARM_STREAM_TYPE_BIT) == 0); } - + } -- 2.11.0