예제 #1
0
 public static Alarm calculateNextAlert(final Context context) {
   Alarm alarm = null;
   long minTime = Long.MAX_VALUE;
   long now = System.currentTimeMillis();
   Cursor cursor = getFilteredAlarmsCursor(context.getContentResolver());
   if (cursor != null) {
     if (cursor.moveToFirst()) {
       do {
         Alarm a = new Alarm(context, cursor);
         // A time of 0 indicates this is a repeating alarm, so
         // calculate the time to get the next alert.
         if (a.time == 0) {
           a.time = calculateAlarm(a);
         } else if (a.time < now) {
           Log.v("Disabling expired alarm set for " + Log.formatTime(a.time));
           // Expired alarm, disable it and move along.
           enableAlarmInternal(context, a, false);
           continue;
         }
         if (a.time < minTime) {
           minTime = a.time;
           alarm = a;
         }
       } while (cursor.moveToNext());
     }
     cursor.close();
   }
   return alarm;
 }
예제 #2
0
  /** Disables non-repeating alarms that have passed. Called at boot. */
  public static void disableExpiredAlarms(final Context context) {
    Cursor cur = getFilteredAlarmsCursor(context.getContentResolver());
    long now = System.currentTimeMillis();

    if (cur.moveToFirst()) {
      do {
        Alarm alarm = new Alarm(context, cur);
        // A time of 0 means this alarm repeats. If the time is
        // non-zero, check if the time is before now.
        if (alarm.time != 0 && alarm.time < now) {
          Log.v("Disabling expired alarm set for " + Log.formatTime(alarm.time));
          enableAlarmInternal(context, alarm, false);
        }
      } while (cur.moveToNext());
    }
    cur.close();
  }
예제 #3
0
 /**
  * A convenience method to enable or disable an alarm.
  *
  * @param id corresponds to the _id column
  * @param enabled corresponds to the ENABLED column
  */
 public static void enableAlarm(final Context context, final int id, boolean enabled) {
   enableAlarmInternal(context, id, enabled);
   setNextAlert(context);
 }
예제 #4
0
 private static void enableAlarmInternal(final Context context, final int id, boolean enabled) {
   enableAlarmInternal(context, getAlarm(context, context.getContentResolver(), id), enabled);
 }
예제 #5
0
  private static Alarm calculateNextAlert(final Context context) {
    long minTime = Long.MAX_VALUE;
    long now = System.currentTimeMillis();
    final SharedPreferences prefs = context.getSharedPreferences(PREFERENCES, 0);

    Set<Alarm> alarms = new HashSet<Alarm>();

    // We need to to build the list of alarms from both the snoozed list and the scheduled
    // list.  For a non-repeating alarm, when it goes of, it becomes disabled.  A snoozed
    // non-repeating alarm is not in the active list in the database.

    // first go through the snoozed alarms
    final Set<String> snoozedIds = prefs.getStringSet(PREF_SNOOZE_IDS, new HashSet<String>());
    for (String snoozedAlarm : snoozedIds) {
      final int alarmId = Integer.parseInt(snoozedAlarm);
      final Alarm a = getAlarm(context.getContentResolver(), alarmId);
      alarms.add(a);
    }

    // Now add the scheduled alarms
    final Cursor cursor = getFilteredAlarmsCursor(context.getContentResolver());
    if (cursor != null) {
      try {
        if (cursor.moveToFirst()) {
          do {
            final Alarm a = new Alarm(cursor);
            alarms.add(a);
          } while (cursor.moveToNext());
        }
      } finally {
        cursor.close();
      }
    }

    Alarm alarm = null;

    for (Alarm a : alarms) {
      // A time of 0 indicates this is a repeating alarm, so
      // calculate the time to get the next alert.
      if (a.time == 0) {
        a.time = calculateAlarm(a);
        if (!a.daysOfWeek.isRepeatSet()) {
          saveAlarmTime(context, a, a.time);
        }
      }

      // Update the alarm if it has been snoozed
      updateAlarmTimeForSnooze(prefs, a);

      if (a.time < now) {
        Log.v("Disabling expired alarm set for " + Log.formatTime(a.time));
        // Expired alarm, disable it and move along.
        enableAlarmInternal(context, a, false);
        continue;
      }
      if (a.time < minTime) {
        minTime = a.time;
        alarm = a;
      }
    }

    return alarm;
  }