Exemplo n.º 1
0
  /**
   * Builds and returns text for the "exact" time an alarm occurs as opposed to the period left for
   * it to occur.<br>
   * In English, 12:00 today would become "Today 12:00", tomorrow would be come "Tomorrow 12:00",
   * and on Monday it would become
   *
   * @param formats the formats to use, e.g: [Today %1$s, Tomorrow %1$s, %2$s %1$s].
   * @param noActive if no alarm was active, this is used.
   * @param now current time in Unix epoch timestamp.
   * @param ats an AlarmTimestamp: the information about the alarm & its timestamp.
   * @param locale the locale to use for weekdays.
   * @return the built time-to string.
   */
  public static final String getTime(
      String[] formats, String noActive, long now, AlarmTimestamp ats, Locale locale) {

    if (ats == AlarmTimestamp.INVALID) {
      return noActive;
    } else {
      if (ats.getMillis() < now) {
        throw new RuntimeException("Time given is before now.");
      }

      // Prepare replacements.
      Alarm alarm = ats.getAlarm();
      String timeReplacement = StringUtils.joinTime(alarm.getHour(), alarm.getMinute());

      // Calculate start of tomorrow.
      DateTime nowTime = new DateTime(now);
      DateTime time = new DateTime(ats.getMillis());
      LocalDate tomorrow = new LocalDate(nowTime).plusDays(1);
      DateTime startOfTomorrow = tomorrow.toDateTimeAtStartOfDay(nowTime.getZone());

      if (time.isBefore(startOfTomorrow)) {
        // Alarm is today.
        Log.d(TAG, "today");
        return String.format(formats[0], timeReplacement);
      }

      // Calculate start of the day after tomorrow.
      LocalDate afterTomorrow = tomorrow.plusDays(1);
      DateTime startOfAfterTomorrow = afterTomorrow.toDateTimeAtStartOfDay(nowTime.getZone());

      if (time.isBefore(startOfAfterTomorrow)) {
        Log.d(TAG, "tomorrow");
        // Alarm is tomorrow.
        return String.format(formats[1], timeReplacement);
      }

      // Alarm is after tomorrow.
      Log.d(TAG, "after tomorrow");
      String weekday = new DateTime(ats.getMillis()).dayOfWeek().getAsText(locale);
      return String.format(formats[2], timeReplacement, weekday);
    }
  }
Exemplo n.º 2
0
  /**
   * Builds and returns text for the time to an alarm given resources, current time, and
   * AlarmTimestamp when given a resources bundle.
   *
   * @param res Android resources.
   * @param now current time in unix epoch timestamp.
   * @param ats an AlarmTimestamp: the information about the alarm & its timestamp.
   * @return the built time-to string.
   */
  public static final String getTimeToText(Resources res, long now, AlarmTimestamp ats) {
    Period diff = null;

    // ats is invalid when all the alarms have been turned off. INVALID has the value null.
    // therefore, we must do a nullcheck, otherwise we get an exception.
    if (ats != AlarmTimestamp.INVALID) {
      diff = new Period(now, ats.getMillis());
    }

    String[] formats = res.getStringArray(R.array.earliest_time_formats);
    String[] partFormats = res.getStringArray(R.array.earliest_time_formats_parts);

    return getTimeToText(formats, partFormats, diff, ats);
  }