/**
   * Called when an alarm was triggered.
   *
   * @param context Application context
   * @param intent Received intent with content data
   */
  @Override
  public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();
    Options options;

    try {
      String data = bundle.getString(Options.EXTRA);
      JSONObject dict = new JSONObject(data);

      options = new Options(context).parse(dict);
    } catch (JSONException e) {
      e.printStackTrace();
      return;
    }

    if (options == null) return;

    if (isFirstAlarmInFuture(options)) return;

    Builder builder = new Builder(options);
    Notification notification = buildNotification(builder);
    boolean updated = notification.isUpdate(false);

    onTrigger(notification, updated);
  }
  /*
   * If you set a repeating alarm at 11:00 in the morning and it
   * should trigger every morning at 08:00 o'clock, it will
   * immediately fire. E.g. Android tries to make up for the
   * 'forgotten' reminder for that day. Therefore we ignore the event
   * if Android tries to 'catch up'.
   */
  private Boolean isFirstAlarmInFuture(Options options) {
    Notification notification = new Builder(options).build();

    if (!notification.isRepeating()) return false;

    Calendar now = Calendar.getInstance();
    Calendar alarm = Calendar.getInstance();

    alarm.setTime(notification.getOptions().getTriggerDate());

    int alarmHour = alarm.get(Calendar.HOUR_OF_DAY);
    int alarmMin = alarm.get(Calendar.MINUTE);
    int currentHour = now.get(Calendar.HOUR_OF_DAY);
    int currentMin = now.get(Calendar.MINUTE);

    return (currentHour != alarmHour && currentMin != alarmMin);
  }
  /** Launch main intent from package. */
  public void launchApp(Notification notification) {
    Context context = getApplicationContext();
    String pkgName = context.getPackageName();

    Intent intent = context.getPackageManager().getLaunchIntentForPackage(pkgName);

    intent.putExtra(Options.EXTRA, notification.toString());

    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    context.startActivity(intent);
  }