Example #1
0
  /**
   * Common method for notes retrieval. It accepts a query to perform and returns matching records.
   */
  public List<Note> getNotes(String whereCondition, boolean order) {
    List<Note> noteList = new ArrayList<>();

    String sort_column, sort_order = "";

    // Getting sorting criteria from preferences. Reminder screen forces sorting.
    if (Navigation.checkNavigation(Navigation.REMINDERS)) {
      sort_column = KEY_REMINDER;
    } else {
      sort_column = prefs.getString(Constants.PREF_SORTING_COLUMN, KEY_TITLE);
    }
    if (order) {
      sort_order =
          KEY_TITLE.equals(sort_column) || KEY_REMINDER.equals(sort_column) ? " ASC " : " DESC ";
    }

    // In case of title sorting criteria it must be handled empty title by concatenating content
    sort_column = KEY_TITLE.equals(sort_column) ? KEY_TITLE + "||" + KEY_CONTENT : sort_column;

    // In case of reminder sorting criteria the empty reminder notes must be moved on bottom of
    // results
    sort_column =
        KEY_REMINDER.equals(sort_column)
            ? "IFNULL(" + KEY_REMINDER + ", " + "" + Constants.TIMESTAMP_UNIX_EPOCH + ")"
            : sort_column;

    // Generic query to be specialized with conditions passed as parameter
    String query =
        "SELECT "
            + KEY_CREATION
            + ","
            + KEY_LAST_MODIFICATION
            + ","
            + KEY_TITLE
            + ","
            + KEY_CONTENT
            + ","
            + KEY_ARCHIVED
            + ","
            + KEY_TRASHED
            + ","
            + KEY_REMINDER
            + ","
            + KEY_REMINDER_FIRED
            + ","
            + KEY_RECURRENCE_RULE
            + ","
            + KEY_LATITUDE
            + ","
            + KEY_LONGITUDE
            + ","
            + KEY_ADDRESS
            + ","
            + KEY_LOCKED
            + ","
            + KEY_CHECKLIST
            + ","
            + KEY_CATEGORY
            + ","
            + KEY_CATEGORY_NAME
            + ","
            + KEY_CATEGORY_DESCRIPTION
            + ","
            + KEY_CATEGORY_COLOR
            + " FROM "
            + TABLE_NOTES
            + " LEFT JOIN "
            + TABLE_CATEGORY
            + " USING( "
            + KEY_CATEGORY
            + ") "
            + whereCondition
            + (order ? " ORDER BY " + sort_column + sort_order : "");

    Log.v(Constants.TAG, "Query: " + query);

    Cursor cursor = null;
    try {
      cursor = getDatabase().rawQuery(query, null);

      // Looping through all rows and adding to list
      if (cursor.moveToFirst()) {
        do {
          int i = 0;
          Note note = new Note();
          note.setCreation(cursor.getLong(i++));
          note.setLastModification(cursor.getLong(i++));
          note.setTitle(cursor.getString(i++));
          note.setContent(cursor.getString(i++));
          note.setArchived("1".equals(cursor.getString(i++)));
          note.setTrashed("1".equals(cursor.getString(i++)));
          note.setAlarm(cursor.getString(i++));
          note.setReminderFired(cursor.getInt(i++));
          note.setRecurrenceRule(cursor.getString(i++));
          note.setLatitude(cursor.getString(i++));
          note.setLongitude(cursor.getString(i++));
          note.setAddress(cursor.getString(i++));
          note.setLocked("1".equals(cursor.getString(i++)));
          note.setChecklist("1".equals(cursor.getString(i++)));

          // Eventual decryption of content
          if (note.isLocked()) {
            note.setContent(
                Security.decrypt(note.getContent(), prefs.getString(Constants.PREF_PASSWORD, "")));
          }

          // Set category
          long categoryId = cursor.getLong(i++);
          if (categoryId != 0) {
            Category category =
                new Category(
                    categoryId,
                    cursor.getString(i++),
                    cursor.getString(i++),
                    cursor.getString(i++));
            note.setCategory(category);
          }

          // Add eventual attachments uri
          note.setAttachmentsList(getNoteAttachments(note));

          // Adding note to list
          noteList.add(note);

        } while (cursor.moveToNext());
      }

    } finally {
      if (cursor != null) cursor.close();
    }

    Log.v(Constants.TAG, "Query: Retrieval finished!");
    return noteList;
  }
Example #2
0
  // Inserting or updating single note
  public Note updateNote(Note note, boolean updateLastModification) {
    SQLiteDatabase db = getDatabase(true);

    String content;
    if (note.isLocked()) {
      content = Security.encrypt(note.getContent(), prefs.getString(Constants.PREF_PASSWORD, ""));
    } else {
      content = note.getContent();
    }

    // To ensure note and attachments insertions are atomical and boost performances transaction are
    // used
    db.beginTransaction();

    ContentValues values = new ContentValues();
    values.put(KEY_TITLE, note.getTitle());
    values.put(KEY_CONTENT, content);
    values.put(
        KEY_CREATION,
        note.getCreation() != null ? note.getCreation() : Calendar.getInstance().getTimeInMillis());
    values.put(
        KEY_LAST_MODIFICATION,
        updateLastModification
            ? Calendar.getInstance().getTimeInMillis()
            : (note.getLastModification() != null
                ? note.getLastModification()
                : Calendar.getInstance().getTimeInMillis()));
    values.put(KEY_ARCHIVED, note.isArchived());
    values.put(KEY_TRASHED, note.isTrashed());
    values.put(KEY_REMINDER, note.getAlarm());
    values.put(KEY_REMINDER_FIRED, note.isReminderFired());
    values.put(KEY_RECURRENCE_RULE, note.getRecurrenceRule());
    values.put(KEY_LATITUDE, note.getLatitude());
    values.put(KEY_LONGITUDE, note.getLongitude());
    values.put(KEY_ADDRESS, note.getAddress());
    values.put(KEY_CATEGORY, note.getCategory() != null ? note.getCategory().getId() : null);
    boolean locked = note.isLocked() != null ? note.isLocked() : false;
    values.put(KEY_LOCKED, locked);
    boolean checklist = note.isChecklist() != null ? note.isChecklist() : false;
    values.put(KEY_CHECKLIST, checklist);

    db.insertWithOnConflict(TABLE_NOTES, KEY_ID, values, SQLiteDatabase.CONFLICT_REPLACE);
    Log.d(Constants.TAG, "Updated note titled '" + note.getTitle() + "'");

    // Updating attachments
    List<Attachment> deletedAttachments = note.getAttachmentsListOld();
    for (Attachment attachment : note.getAttachmentsList()) {
      updateAttachment(
          note.get_id() != null ? note.get_id() : values.getAsLong(KEY_CREATION), attachment, db);
      deletedAttachments.remove(attachment);
    }
    // Remove from database deleted attachments
    for (Attachment attachmentDeleted : deletedAttachments) {
      db.delete(
          TABLE_ATTACHMENTS,
          KEY_ATTACHMENT_ID + " = ?",
          new String[] {String.valueOf(attachmentDeleted.getId())});
    }

    db.setTransactionSuccessful();
    db.endTransaction();

    // Fill the note with correct data before returning it
    note.setCreation(
        note.getCreation() != null ? note.getCreation() : values.getAsLong(KEY_CREATION));
    note.setLastModification(values.getAsLong(KEY_LAST_MODIFICATION));

    return note;
  }