Ejemplo n.º 1
0
  /**
   * Fix and update all alarm instance when a time change event occurs.
   *
   * @param context application context
   */
  public static void fixAlarmInstances(Context context) {
    // Register all instances after major time changes or when phone restarts
    final ContentResolver contentResolver = context.getContentResolver();
    final Calendar currentTime = getCurrentTime();
    for (AlarmInstance instance : AlarmInstance.getInstances(contentResolver, null)) {
      final Alarm alarm = Alarm.getAlarm(contentResolver, instance.mAlarmId);
      final Calendar priorAlarmTime = alarm.getPreviousAlarmTime(instance.getAlarmTime());
      final Calendar missedTTLTime = instance.getMissedTimeToLive();
      if (currentTime.before(priorAlarmTime) || currentTime.after(missedTTLTime)) {
        final Calendar oldAlarmTime = instance.getAlarmTime();
        final Calendar newAlarmTime = alarm.getNextAlarmTime(currentTime);
        final CharSequence oldTime = DateFormat.format("MM/dd/yyyy hh:mm a", oldAlarmTime);
        final CharSequence newTime = DateFormat.format("MM/dd/yyyy hh:mm a", newAlarmTime);
        LogUtils.i(
            "A time change has caused an existing alarm scheduled to fire at %s to"
                + " be replaced by a new alarm scheduled to fire at %s",
            oldTime, newTime);

        // The time change is so dramatic the AlarmInstance doesn't make any sense;
        // remove it and schedule the new appropriate instance.
        AlarmStateManager.setDismissState(context, instance);
      } else {
        registerInstance(context, instance, false);
      }
    }

    updateNextAlarm(context);
  }
Ejemplo n.º 2
0
  /**
   * Returns an alarm instance of an alarm that's going to fire next.
   *
   * @param context application context
   * @return an alarm instance that will fire earliest relative to current time.
   */
  public static AlarmInstance getNextFiringAlarm(Context context) {
    final ContentResolver cr = context.getContentResolver();
    final String activeAlarmQuery = AlarmInstance.ALARM_STATE + "<" + AlarmInstance.FIRED_STATE;
    final List<AlarmInstance> alarmInstances = AlarmInstance.getInstances(cr, activeAlarmQuery);

    AlarmInstance nextAlarm = null;
    for (AlarmInstance instance : alarmInstances) {
      if (nextAlarm == null || instance.getAlarmTime().before(nextAlarm.getAlarmTime())) {
        nextAlarm = instance;
      }
    }
    return nextAlarm;
  }