/** Counts words in a note */ public int getWords(Note note) { int count = 0; String[] fields = {note.getTitle(), note.getContent()}; for (String field : fields) { boolean word = false; int endOfLine = field.length() - 1; for (int i = 0; i < field.length(); i++) { // if the char is a letter, word = true. if (Character.isLetter(field.charAt(i)) && i != endOfLine) { word = true; // if char isn't a letter and there have been letters before, // counter goes up. } else if (!Character.isLetter(field.charAt(i)) && word) { count++; word = false; // last word of String; if it doesn't end with a non letter, it // wouldn't count without this. } else if (Character.isLetter(field.charAt(i)) && i == endOfLine) { count++; } } } return count; }
/** Counts chars in a note */ public int getChars(Note note) { int count = 0; count += note.getTitle().length(); count += note.getContent().length(); return count; }
// 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; }