public void destroy() throws Exception {
   if (started) {
     server.stop();
     sessions.clear();
     started = false;
     System.setProperties(savedProperties);
     initServer();
   }
 }
 public static int getNotificationId(long id) {
   Integer newId = NOTIFICATION_ID.get(id);
   if (newId == null) {
     synchronized (MEMORY_STORE) {
       newId = NOTIFICATION_ID.get(id);
       if (newId == null) {
         newId = MEMORY_STORE.getNextInt();
         NOTIFICATION_ID.put(id, newId);
       }
     }
   }
   return newId;
 }
 static {
   MEMORY_STORE.setAtomicInt(new AtomicInteger(0));
 }
public class NotificationUtils {
  public static final String ID = "id";
  public static final String TITLE = "title";
  public static final String DUE_DATE = "dueDate";
  public static final String LIST_ID = "listId";

  private static final ConcurrentHashMap<Long, Integer> NOTIFICATION_ID =
      new ConcurrentHashMap<Long, Integer>(100);
  private static final MemoryStore MEMORY_STORE = MemoryStore.getInstance();

  static {
    MEMORY_STORE.setAtomicInt(new AtomicInteger(0));
  }

  public static int getNotificationId(long id) {
    Integer newId = NOTIFICATION_ID.get(id);
    if (newId == null) {
      synchronized (MEMORY_STORE) {
        newId = NOTIFICATION_ID.get(id);
        if (newId == null) {
          newId = MEMORY_STORE.getNextInt();
          NOTIFICATION_ID.put(id, newId);
        }
      }
    }
    return newId;
  }

  public static void setReminder(
      Context context,
      long id,
      String title,
      long dueDate,
      long listId,
      long reminderDate,
      long reminderInterval) {
    String actionName = context.getResources().getString(R.string.intent_action_alarm);
    Intent alarmIntent = new Intent(actionName);
    alarmIntent.putExtra(NotificationUtils.ID, id);
    alarmIntent.putExtra(NotificationUtils.TITLE, title);
    alarmIntent.putExtra(NotificationUtils.DUE_DATE, dueDate);
    alarmIntent.putExtra(NotificationUtils.LIST_ID, listId);
    PendingIntent alarmPendingIntent =
        PendingIntent.getBroadcast(context, getNotificationId(id), alarmIntent, 0);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (reminderInterval > 0) {
      alarmManager.setRepeating(
          AlarmManager.RTC_WAKEUP, reminderDate, reminderInterval, alarmPendingIntent);
      if (Log.LOGV) Log.v("TaskDetailDialog set recurring reminder at " + dueDate);
    } else {
      alarmManager.set(AlarmManager.RTC_WAKEUP, reminderDate, alarmPendingIntent);
      if (Log.LOGV) Log.v("TaskDetailDialog set reminder at " + dueDate);
    }
  }

  public static void setReminder(Context context, GTask task) {
    String actionName = context.getResources().getString(R.string.intent_action_alarm);
    Intent alarmIntent = new Intent(actionName);
    alarmIntent.putExtra(NotificationUtils.ID, task.id);
    alarmIntent.putExtra(NotificationUtils.TITLE, task.title);
    alarmIntent.putExtra(NotificationUtils.DUE_DATE, task.dueDate);
    alarmIntent.putExtra(NotificationUtils.LIST_ID, task.list.id);
    alarmIntent.putExtra("task", task);
    PendingIntent alarmPendingIntent =
        PendingIntent.getBroadcast(
            context, getNotificationId(task.id), alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (task.reminderInterval > 0) {
      alarmManager.setRepeating(
          AlarmManager.RTC_WAKEUP, task.reminderDate, task.reminderInterval, alarmPendingIntent);
      if (Log.LOGV) Log.v("TaskDetailDialog set recurring reminder at " + task.dueDate);
    } else {
      alarmManager.set(AlarmManager.RTC_WAKEUP, task.reminderDate, alarmPendingIntent);
      if (Log.LOGV) Log.v("TaskDetailDialog set reminder at " + task.dueDate);
    }
  }

  public static void cancelReminder(Context context, GTask task) {
    if (task != null) {
      String actionName = context.getResources().getString(R.string.intent_action_alarm);
      Intent alarmIntent = new Intent(actionName);
      alarmIntent.putExtra(NotificationUtils.ID, task.id);
      if (task.title != null) alarmIntent.putExtra(NotificationUtils.TITLE, task.title);
      alarmIntent.putExtra(NotificationUtils.DUE_DATE, task.dueDate);
      alarmIntent.putExtra(NotificationUtils.LIST_ID, task.list.id);
      alarmIntent.putExtra("task", task);
      PendingIntent alarmPendingIntent =
          PendingIntent.getBroadcast(
              context, getNotificationId(task.id), alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
      AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
      alarmManager.cancel(alarmPendingIntent);
      alarmPendingIntent.cancel();
      if (Log.LOGV) Log.v("TaskDetailDialog deleted reminder set at " + task.dueDate);
    }
  }
}