Example #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;
 }
Example #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();
  }
Example #3
0
  /**
   * Sets alert in AlarmManger and StatusBar. This is what will actually launch the alert when the
   * alarm triggers.
   *
   * @param alarm Alarm.
   * @param atTimeInMillis milliseconds since epoch
   */
  private static void enableAlert(
      Context context, final Alarm alarm, final long atTimeInMillis, boolean disablePoweroffAlarm) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    // Intentionally verbose: always log the alarm time to provide useful
    // information in bug reports.
    Log.v("Alarm set for id=" + alarm.id + " " + Log.formatTime(atTimeInMillis));

    Intent intent = new Intent(ALARM_ALERT_ACTION);

    // XXX: This is a slight hack to avoid an exception in the remote
    // AlarmManagerService process. The AlarmManager adds extra data to
    // this Intent which causes it to inflate. Since the remote process
    // does not know about the Alarm class, it throws a
    // ClassNotFoundException.
    //
    // To avoid this, we marshall the data ourselves and then parcel a plain
    // byte[] array. The AlarmReceiver class knows to build the Alarm
    // object from the byte[] array.
    Parcel out = Parcel.obtain();
    alarm.writeToParcel(out, 0);
    out.setDataPosition(0);
    intent.putExtra(ALARM_RAW_DATA, out.marshall());

    PendingIntent sender =
        PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    // changed by MTK start
    // disable power-off alarm when device encrypted.
    if (!disablePoweroffAlarm && "unencrypted".equals(SystemProperties.get("ro.crypto.state"))) {
      // not enabled device encrypt, enable power-off alarm
      am.set(POWER_OFF_WAKE_UP, atTimeInMillis, sender);
    } else {
      // enabled device encrypt, use google default design
      am.set(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender);
    }
    // changed by MTK end
    storeNearestAlarm(context, alarm);
    // am.set(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender);

    setStatusBarIcon(context, true);

    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(atTimeInMillis);
    String timeString = formatDayAndTime(context, c);
    saveNextAlarm(context, timeString);
  }
Example #4
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;
  }