public ArrayList<ActionEvent> getAllItems(Context context, boolean isHistory) {
    DatabaseHelper dh = new DatabaseHelper(context, TABLE_NAME);
    SQLiteDatabase db = dh.getReadableDatabase();

    ArrayList<ActionEvent> aeList = new ArrayList<ActionEvent>();
    Cursor c =
        db.query(
            TABLE_NAME,
            null,
            KEY_IS_HISTORY + "='" + getBooleanToLong(isHistory) + "'",
            null,
            null,
            null,
            "_id" + " DESC");
    while (c.moveToNext()) {
      ActionEvent ae =
          new ActionEvent(
              c.getString(c.getColumnIndex(KEY_ACTION_NAME)),
              c.getLong(c.getColumnIndex(KEY_START_TIMESTAMP)),
              c.getString(c.getColumnIndex(KEY_ACTION_NOTE)),
              getLongToBoolean(c.getLong(c.getColumnIndex(KEY_IS_HISTORY))),
              c.getLong(c.getColumnIndex(KEY_START_DEALY)),
              c.getLong(c.getColumnIndex(KEY_START_DEALY)));
      ae.setBreakTimestamp(c.getLong(c.getColumnIndex(KEY_BREAK_TIMESTAMP)));
      ae.setState(c.getString(c.getColumnIndex(KEY_STATE)));
      aeList.add(ae);
    }
    c.close();
    db.close();
    return aeList;
  }
 public boolean update(Context context, ActionEvent ae) {
   ContentValues target = new ContentValues();
   target.put(KEY_STATE, ae.getEventState());
   target.put(KEY_BREAK_TIMESTAMP, ae.getBreakTimestamp());
   target.put(KEY_IS_HISTORY, getBooleanToLong(ae.isHistory()));
   target.put(KEY_BREAK_DEALY, ae.getBreakDelay());
   return update(context, ae, target);
 }
  private boolean update(Context context, ActionEvent ae, final ContentValues target) {
    DatabaseHelper dh = new DatabaseHelper(context, TABLE_NAME);
    SQLiteDatabase db = dh.getWritableDatabase();

    if (db.isOpen()) {
      db.update(
          TABLE_NAME,
          target,
          KEY_START_TIMESTAMP + "=?",
          new String[] {Long.toString(ae.getStartTimestamp())});
      db.close();
      return true;
    } else {
      db.close();
      return false;
    }
  }
 private boolean isInsert(SQLiteDatabase db, ActionEvent ae) {
   Cursor c =
       db.query(
           TABLE_NAME,
           new String[] {KEY_START_TIMESTAMP},
           KEY_START_TIMESTAMP + "=" + ae.getStartTimestamp(),
           null,
           null,
           null,
           null);
   // check the record
   if (c.getCount() > 0) {
     c.close();
     db.close();
     return true;
   } else {
     c.close();
     db.close();
     return false;
   }
 }
  public boolean insert(Context context, ActionEvent ae) {
    DatabaseHelper dh = new DatabaseHelper(context, TABLE_NAME);
    SQLiteDatabase db = dh.getWritableDatabase();

    ContentValues target = null;
    if (db.isOpen()) {
      target = new ContentValues();
      target.put(KEY_ACTION_NAME, ae.getActionEventName());
      target.put(KEY_START_TIMESTAMP, ae.getStartTimestamp());
      target.put(KEY_STATE, ae.getEventState());
      target.put(KEY_BREAK_TIMESTAMP, ae.getBreakTimestamp());
      target.put(KEY_IS_HISTORY, getBooleanToLong(ae.isHistory()));
      target.put(KEY_ACTION_NOTE, ae.getNoteContent());
      target.put(KEY_START_DEALY, ae.getStartDelay());
      target.put(KEY_BREAK_DEALY, ae.getBreakDelay());
      db.insert(TABLE_NAME, null, target);
    } else {
      db.close();
      return false;
    }
    target = null;
    return isInsert(db, ae);
  }