Alarmspublic class Alarms extends Object The Alarms provider supplies info about Alarm Clock settings |
Fields Summary |
---|
public static final String | ALARM_ALERT_ACTION | public static final String | ID | public static final String | TIME | public static final String | LABEL | static final String | PREF_SNOOZE_ID | static final String | PREF_SNOOZE_TIME | static final String | PREF_SNOOZE_LABEL | private static final String | DM12 | private static final String | DM24 | private static final String | M12 | private static final String | M24 |
Methods Summary |
---|
public static synchronized android.net.Uri | addAlarm(android.content.ContentResolver contentResolver)Creates a new Alarm.
ContentValues values = new ContentValues();
values.put(Alarms.AlarmColumns.HOUR, 8);
return contentResolver.insert(AlarmColumns.CONTENT_URI, values);
| static java.util.Calendar | calculateAlarm(int hour, int minute, com.android.alarmclock.Alarms$DaysOfWeek daysOfWeek)Given an alarm in hours and minutes, return a time suitable for
setting in AlarmManager.
// start with now
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
int nowHour = c.get(Calendar.HOUR_OF_DAY);
int nowMinute = c.get(Calendar.MINUTE);
// if alarm is behind current time, advance one day
if (hour < nowHour ||
hour == nowHour && minute <= nowMinute) {
c.add(Calendar.DAY_OF_YEAR, 1);
}
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
int addDays = daysOfWeek.getNextAlarm(c);
/* Log.v("** TIMES * " + c.getTimeInMillis() + " hour " + hour +
" minute " + minute + " dow " + c.get(Calendar.DAY_OF_WEEK) + " from now " +
addDays); */
if (addDays > 0) c.add(Calendar.DAY_OF_WEEK, addDays);
return c;
| static com.android.alarmclock.Alarms$AlarmCalculator | calculateNextAlert(android.content.Context context)
ContentResolver resolver = context.getContentResolver();
AlarmCalculator alarmCalc = new AlarmCalculator();
getAlarms(resolver, alarmCalc);
return alarmCalc;
| public static synchronized void | deleteAlarm(android.content.Context context, int alarmId)Removes an existing Alarm. If this alarm is snoozing, disables
snooze. Sets next alert.
ContentResolver contentResolver = context.getContentResolver();
/* If alarm is snoozing, lose it */
int snoozeId = getSnoozeAlarmId(context);
if (snoozeId == alarmId) disableSnoozeAlert(context);
Uri uri = ContentUris.withAppendedId(AlarmColumns.CONTENT_URI, alarmId);
deleteAlarm(contentResolver, uri);
setNextAlert(context);
| private static synchronized void | deleteAlarm(android.content.ContentResolver contentResolver, android.net.Uri uri)
contentResolver.delete(uri, "", null);
| static void | disableAlert(android.content.Context context, int id)Disables alert in AlarmManger and StatusBar.
AlarmManager am = (AlarmManager)
context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(ALARM_ALERT_ACTION);
intent.putExtra(ID, id);
PendingIntent sender = PendingIntent.getBroadcast(
context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.cancel(sender);
setStatusBarIcon(context, false);
saveNextAlarm(context, "");
| public static void | disableExpiredAlarms(android.content.Context context)Disables non-repeating alarms that have passed. Called at
boot.
Cursor cur = getAlarmsCursor(context.getContentResolver());
long now = System.currentTimeMillis();
if (cur.moveToFirst()) {
do {
// Get the field values
int id = cur.getInt(AlarmColumns.ALARM_ID_INDEX);
boolean enabled = cur.getInt(
AlarmColumns.ALARM_ENABLED_INDEX) == 1 ? true : false;
DaysOfWeek daysOfWeek = new DaysOfWeek(
cur.getInt(AlarmColumns.ALARM_DAYS_OF_WEEK_INDEX));
long time = cur.getLong(AlarmColumns.ALARM_TIME_INDEX);
if (enabled && !daysOfWeek.isRepeatSet() && time < now) {
if (Log.LOGV) Log.v(
"** DISABLE " + id + " now " + now +" set " + time);
enableAlarmInternal(context, id, false);
}
} while (cur.moveToNext());
}
cur.close();
| static int | disableSnoozeAlert(android.content.Context context)
int id = getSnoozeAlarmId(context);
if (id == -1) return -1;
saveSnoozeAlert(context, -1, 0, null);
return id;
| public static synchronized void | enableAlarm(android.content.Context context, int id, boolean enabled)A convenience method to enable or disable an alarm.
int aid = disableSnoozeAlert(context);
if (aid != -1 && aid != id) enableAlarmInternal(context, aid, false);
enableAlarmInternal(context, id, enabled);
setNextAlert(context);
| private static synchronized void | enableAlarmInternal(android.content.Context context, int id, boolean enabled)
ContentResolver resolver = context.getContentResolver();
class EnableAlarm implements AlarmSettings {
public int mHour;
public int mMinutes;
public DaysOfWeek mDaysOfWeek;
public void reportAlarm(
int idx, boolean enabled, int hour, int minutes,
DaysOfWeek daysOfWeek, boolean vibrate, String message,
String alert) {
mHour = hour;
mMinutes = minutes;
mDaysOfWeek = daysOfWeek;
}
}
ContentValues values = new ContentValues(2);
values.put(AlarmColumns.ENABLED, enabled ? 1 : 0);
/* If we are enabling the alarm, load hour/minutes/daysOfWeek
from db, so we can calculate alarm time */
if (enabled) {
EnableAlarm enableAlarm = new EnableAlarm();
getAlarm(resolver, enableAlarm, id);
if (enableAlarm.mDaysOfWeek == null) {
/* Under monkey, sometimes reportAlarm is never
called */
Log.e("** enableAlarmInternal failed " + id + " h " +
enableAlarm.mHour + " m " + enableAlarm.mMinutes);
return;
}
long time = calculateAlarm(enableAlarm.mHour, enableAlarm.mMinutes,
enableAlarm.mDaysOfWeek).getTimeInMillis();
values.put(AlarmColumns.ALARM_TIME, time);
}
resolver.update(ContentUris.withAppendedId(AlarmColumns.CONTENT_URI, id),
values, null, null);
| static void | enableAlert(android.content.Context context, int id, java.lang.String label, long atTimeInMillis)Sets alert in AlarmManger and StatusBar. This is what will
actually launch the alert when the alarm triggers.
Note: In general, apps should call setNextAlert() instead of
this method. setAlert() is only used outside this class when
the alert is not to be driven by the state of the db. "Snooze"
uses this API, as we do not want to alter the alarm in the db
with each snooze.
AlarmManager am = (AlarmManager)
context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(ALARM_ALERT_ACTION);
if (Log.LOGV) Log.v("** setAlert id " + id + " atTime " + atTimeInMillis);
intent.putExtra(ID, id);
intent.putExtra(LABEL, label);
intent.putExtra(TIME, atTimeInMillis);
PendingIntent sender = PendingIntent.getBroadcast(
context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
if (true) {
am.set(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender);
} else {
// a five-second alarm, for testing
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000,
sender);
}
setStatusBarIcon(context, true);
Calendar c = Calendar.getInstance();
c.setTime(new java.util.Date(atTimeInMillis));
String timeString = formatDayAndTime(context, c);
saveNextAlarm(context, timeString);
| private static boolean | enableSnoozeAlert(android.content.Context context)If there is a snooze set, enable it in AlarmManager
SharedPreferences prefs = context.getSharedPreferences(
AlarmClock.PREFERENCES, 0);
int id = prefs.getInt(PREF_SNOOZE_ID, -1);
if (id == -1) return false;
long atTimeInMillis = prefs.getLong(PREF_SNOOZE_TIME, -1);
if (id == -1) return false;
// Try to get the label from the snooze preference. If null, use the
// default label.
String label = prefs.getString(PREF_SNOOZE_LABEL, null);
if (label == null) {
label = context.getString(R.string.default_label);
}
enableAlert(context, id, label, atTimeInMillis);
return true;
| private static java.lang.String | formatDayAndTime(android.content.Context context, java.util.Calendar c)Shows day and time -- used for lock screen
String format = get24HourMode(context) ? DM24 : DM12;
return (c == null) ? "" : (String)DateFormat.format(format, c);
| static java.lang.String | formatTime(android.content.Context context, int hour, int minute, com.android.alarmclock.Alarms$DaysOfWeek daysOfWeek)
Calendar c = calculateAlarm(hour, minute, daysOfWeek);
return formatTime(context, c);
| static java.lang.String | formatTime(android.content.Context context, java.util.Calendar c)
String format = get24HourMode(context) ? M24 : M12;
return (c == null) ? "" : (String)DateFormat.format(format, c);
| static boolean | get24HourMode(android.content.Context context)
return android.text.format.DateFormat.is24HourFormat(context);
| public static synchronized void | getAlarm(android.content.ContentResolver contentResolver, com.android.alarmclock.Alarms$AlarmSettings alarmSetting, int alarmId)Calls the AlarmSettings.reportAlarm interface on alarm with given
alarmId
Cursor cursor = contentResolver.query(
ContentUris.withAppendedId(AlarmColumns.CONTENT_URI, alarmId),
AlarmColumns.ALARM_QUERY_COLUMNS,
null, null, AlarmColumns.DEFAULT_SORT_ORDER);
getAlarms(alarmSetting, cursor);
cursor.close();
| public static synchronized void | getAlarms(android.content.ContentResolver contentResolver, com.android.alarmclock.Alarms$AlarmSettings alarmSettings)Calls the AlarmSettings.reportAlarm interface on all alarms found in db.
Cursor cursor = getAlarmsCursor(contentResolver);
getAlarms(alarmSettings, cursor);
cursor.close();
| private static synchronized void | getAlarms(com.android.alarmclock.Alarms$AlarmSettings alarmSettings, android.database.Cursor cur)
if (cur.moveToFirst()) {
do {
// Get the field values
int id = cur.getInt(AlarmColumns.ALARM_ID_INDEX);
int hour = cur.getInt(AlarmColumns.ALARM_HOUR_INDEX);
int minutes = cur.getInt(AlarmColumns.ALARM_MINUTES_INDEX);
int daysOfWeek = cur.getInt(AlarmColumns.ALARM_DAYS_OF_WEEK_INDEX);
boolean enabled = cur.getInt(AlarmColumns.ALARM_ENABLED_INDEX) == 1 ? true : false;
boolean vibrate = cur.getInt(AlarmColumns.ALARM_VIBRATE_INDEX) == 1 ? true : false;
String message = cur.getString(AlarmColumns.ALARM_MESSAGE_INDEX);
String alert = cur.getString(AlarmColumns.ALARM_ALERT_INDEX);
alarmSettings.reportAlarm(
id, enabled, hour, minutes, new DaysOfWeek(daysOfWeek),
vibrate, message, alert);
} while (cur.moveToNext());
}
| public static synchronized android.database.Cursor | getAlarmsCursor(android.content.ContentResolver contentResolver)Queries all alarms
return contentResolver.query(
AlarmColumns.CONTENT_URI, AlarmColumns.ALARM_QUERY_COLUMNS,
null, null, AlarmColumns.DEFAULT_SORT_ORDER);
| private static android.app.NotificationManager | getNotificationManager(android.content.Context context)
return (NotificationManager) context.getSystemService(
context.NOTIFICATION_SERVICE);
| private static int | getSnoozeAlarmId(android.content.Context context)
SharedPreferences prefs = context.getSharedPreferences(
AlarmClock.PREFERENCES, 0);
return prefs.getInt(PREF_SNOOZE_ID, -1);
| static void | saveNextAlarm(android.content.Context context, java.lang.String timeString)Save time of the next alarm, as a formatted string, into the system
settings so those who care can make use of it.
Settings.System.putString(context.getContentResolver(),
Settings.System.NEXT_ALARM_FORMATTED,
timeString);
| static void | saveSnoozeAlert(android.content.Context context, int id, long atTimeInMillis, java.lang.String label)
SharedPreferences prefs = context.getSharedPreferences(
AlarmClock.PREFERENCES, 0);
SharedPreferences.Editor ed = prefs.edit();
ed.putInt(PREF_SNOOZE_ID, id);
ed.putLong(PREF_SNOOZE_TIME, atTimeInMillis);
ed.putString(PREF_SNOOZE_LABEL, label);
ed.commit();
| public static synchronized void | setAlarm(android.content.Context context, int id, boolean enabled, int hour, int minutes, com.android.alarmclock.Alarms$DaysOfWeek daysOfWeek, boolean vibrate, java.lang.String message, java.lang.String alert)A convenience method to set an alarm in the Alarms
content provider.
ContentValues values = new ContentValues(8);
ContentResolver resolver = context.getContentResolver();
long time = calculateAlarm(hour, minutes, daysOfWeek).getTimeInMillis();
if (Log.LOGV) Log.v(
"** setAlarm * idx " + id + " hour " + hour + " minutes " +
minutes + " enabled " + enabled + " time " + time);
values.put(AlarmColumns.ENABLED, enabled ? 1 : 0);
values.put(AlarmColumns.HOUR, hour);
values.put(AlarmColumns.MINUTES, minutes);
values.put(AlarmColumns.ALARM_TIME, time);
values.put(AlarmColumns.DAYS_OF_WEEK, daysOfWeek.getCoded());
values.put(AlarmColumns.VIBRATE, vibrate);
values.put(AlarmColumns.MESSAGE, message);
values.put(AlarmColumns.ALERT, alert);
resolver.update(ContentUris.withAppendedId(AlarmColumns.CONTENT_URI, id),
values, null, null);
int aid = disableSnoozeAlert(context);
if (aid != -1 && aid != id) enableAlarmInternal(context, aid, false);
setNextAlert(context);
| public static void | setNextAlert(android.content.Context context)Called at system startup, on time/timezone change, and whenever
the user changes alarm settings. Activates snooze if set,
otherwise loads all alarms, activates next alert.
int snoozeId = getSnoozeAlarmId(context);
if (snoozeId == -1) {
AlarmCalculator ac = calculateNextAlert(context);
int id = ac.getIndex();
long atTime = ac.getAlert();
if (atTime < Long.MAX_VALUE) {
enableAlert(context, id, ac.getLabel(), atTime);
} else {
disableAlert(context, id);
}
} else {
enableSnoozeAlert(context);
}
| private static void | setStatusBarIcon(android.content.Context context, boolean enabled)Tells the StatusBar whether the alarm is enabled or disabled
Intent alarmChanged = new Intent(Intent.ACTION_ALARM_CHANGED);
alarmChanged.putExtra("alarmSet", enabled);
context.sendBroadcast(alarmChanged);
|
|