/** Used in L and later devices where "next alarm" is stored in the Alarm Manager. */
  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  private static void updateNextAlarmInAlarmManager(Context context, AlarmInstance nextAlarm) {
    // Sets a surrogate alarm with alarm manager that provides the AlarmClockInfo for the
    // alarm that is going to fire next. The operation is constructed such that it is ignored
    // by AlarmStateManager.

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    int flags = nextAlarm == null ? PendingIntent.FLAG_NO_CREATE : 0;
    PendingIntent operation =
        PendingIntent.getBroadcast(
            context, 0 /* requestCode */, AlarmStateManager.createIndicatorIntent(context), flags);

    if (nextAlarm != null) {
      long alarmTime = nextAlarm.getAlarmTime().getTimeInMillis();

      // Create an intent that can be used to show or edit details of the next alarm.
      PendingIntent viewIntent =
          PendingIntent.getActivity(
              context,
              nextAlarm.hashCode(),
              AlarmNotifications.createViewAlarmIntent(context, nextAlarm),
              PendingIntent.FLAG_UPDATE_CURRENT);

      AlarmManager.AlarmClockInfo info = new AlarmManager.AlarmClockInfo(alarmTime, viewIntent);
      alarmManager.setAlarmClock(info, operation);
    } else if (operation != null) {
      alarmManager.cancel(operation);
    }
  }
    @Override
    public void cancelScheduledInstanceStateChange(Context context, AlarmInstance instance) {
      LogUtils.v("Canceling instance " + instance.mId + " timers");

      // Create a PendingIntent that will match any one set for this instance
      PendingIntent pendingIntent =
          PendingIntent.getBroadcast(
              context,
              instance.hashCode(),
              createStateChangeIntent(context, ALARM_MANAGER_TAG, instance, null),
              PendingIntent.FLAG_NO_CREATE);

      if (pendingIntent != null) {
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.cancel(pendingIntent);
        pendingIntent.cancel();
      }
    }
    @Override
    public void scheduleInstanceStateChange(
        Context context, Calendar time, AlarmInstance instance, int newState) {
      final long timeInMillis = time.getTimeInMillis();
      LogUtils.v(
          "Scheduling state change %d to instance %d at %s (%d)",
          newState, instance.mId, AlarmUtils.getFormattedTime(context, time), timeInMillis);
      final Intent stateChangeIntent =
          createStateChangeIntent(context, ALARM_MANAGER_TAG, instance, newState);
      // Treat alarm state change as high priority, use foreground broadcasts
      stateChangeIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
      PendingIntent pendingIntent =
          PendingIntent.getBroadcast(
              context, instance.hashCode(), stateChangeIntent, PendingIntent.FLAG_UPDATE_CURRENT);

      final AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
      if (Utils.isKitKatOrLater()) {
        am.setExact(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);
      } else {
        am.set(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);
      }
    }