@Override
  public void onReceive(Context context, Intent intent) {
    // Acquire wake-lock.
    WakeLocker.acquire(context);

    this.context = context;

    // Fetch extras.
    Bundle extras = intent.getExtras();
    final int alarmId = new AlarmIntentHelper(intent).getAlarmId();

    if (!this.isRequirementsMet(alarmId)) {
      return;
    }

    // Fetching alarm
    this.alarm = SFApplication.get().getPersister().fetchAlarmById(alarmId);

    // Start the alarm, this will wake the screen up!
    this.startAlarm(extras);

    if (alarm.isSpeech()) {
      startSpeech();
    }
  }
  /**
   * Returns a SpannableString of the enabled days in an alarm,<br>
   * where the days are differently colored if enabled/disabled.
   *
   * @param alarm the alarm to make text for.
   * @return the string.
   */
  public static final SpannableString makeEnabledDaysText(final Alarm alarm) {
    // Compute weekday names & join.
    String[] names = DateTextUtils.getWeekdayNames(ENABLED_DAYS_INDICE_LENGTH, Locale.getDefault());

    SpannableString text = new SpannableString(StringUtils.WS_JOINER.join(names));

    // Create spans for enabled days.
    boolean[] enabledDays = alarm.getEnabledDays();
    if (enabledDays.length != names.length || names.length != 7) {
      throw new AssertionError("A week has 7 days, wrong array lengths!");
    }

    int enabledColor = Color.WHITE;
    int disabledColor = SFApplication.get().getResources().getColor(R.color.nearly_background_text);

    int start = 0;
    for (int i = 0; i < enabledDays.length; i++) {
      boolean enabled = enabledDays[i];
      int length = names[i].length();

      int color = enabled ? enabledColor : disabledColor;
      text.setSpan(new ForegroundColorSpan(color), start, start + length, 0);

      start += length + 1;
    }

    return text;
  }
  /**
   * 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);
    }
  }