/** * Updates or insert a new a category * * @param category Category to be updated or inserted * @return Rows affected or new inserted category id */ public Category updateCategory(Category category) { ContentValues values = new ContentValues(); values.put( KEY_CATEGORY_ID, category.getId() != null ? category.getId() : Calendar.getInstance().getTimeInMillis()); values.put(KEY_CATEGORY_NAME, category.getName()); values.put(KEY_CATEGORY_DESCRIPTION, category.getDescription()); values.put(KEY_CATEGORY_COLOR, category.getColor()); getDatabase(true) .insertWithOnConflict( TABLE_CATEGORY, KEY_CATEGORY_ID, values, SQLiteDatabase.CONFLICT_REPLACE); return category; }
/** * Search for notes with reminder * * @param filterPastReminders Excludes past reminders * @return Notes list */ public List<Note> getNotesWithReminder(boolean filterPastReminders) { String whereCondition = " WHERE " + KEY_REMINDER + (filterPastReminders ? " >= " + Calendar.getInstance().getTimeInMillis() : " IS NOT NULL") + " AND " + KEY_ARCHIVED + " IS NOT 1" + " AND " + KEY_TRASHED + " IS NOT 1"; return getNotes(whereCondition, true); }
/** New attachment insertion */ public Attachment updateAttachment(long noteId, Attachment attachment, SQLiteDatabase db) { ContentValues valuesAttachments = new ContentValues(); valuesAttachments.put( KEY_ATTACHMENT_ID, attachment.getId() != null ? attachment.getId() : Calendar.getInstance().getTimeInMillis()); valuesAttachments.put(KEY_ATTACHMENT_NOTE_ID, noteId); valuesAttachments.put(KEY_ATTACHMENT_URI, attachment.getUri().toString()); valuesAttachments.put(KEY_ATTACHMENT_MIME_TYPE, attachment.getMime_type()); valuesAttachments.put(KEY_ATTACHMENT_NAME, attachment.getName()); valuesAttachments.put(KEY_ATTACHMENT_SIZE, attachment.getSize()); valuesAttachments.put(KEY_ATTACHMENT_LENGTH, attachment.getLength()); db.insertWithOnConflict( TABLE_ATTACHMENTS, KEY_ATTACHMENT_ID, valuesAttachments, SQLiteDatabase.CONFLICT_REPLACE); return attachment; }
// 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; }
/** Retrieves statistics data based on app usage */ public Stats getStats() { Stats mStats = new Stats(); // Categories mStats.setCategories(getCategories().size()); // Everything about notes and their text stats int notesActive = 0, notesArchived = 0, notesTrashed = 0, reminders = 0, remindersFuture = 0, checklists = 0, notesMasked = 0, tags = 0, locations = 0; int totalWords = 0, totalChars = 0, maxWords = 0, maxChars = 0, avgWords = 0, avgChars = 0; int words, chars; List<Note> notes = getAllNotes(false); for (Note note : notes) { if (note.isTrashed()) { notesTrashed++; } else if (note.isArchived()) { notesArchived++; } else { notesActive++; } if (note.getAlarm() != null && Long.parseLong(note.getAlarm()) > 0) { if (Long.parseLong(note.getAlarm()) > Calendar.getInstance().getTimeInMillis()) { remindersFuture++; } else { reminders++; } } if (note.isChecklist()) { checklists++; } if (note.isLocked()) { notesMasked++; } tags += TagsHelper.retrieveTags(note).size(); if (note.getLongitude() != null && note.getLongitude() != 0) { locations++; } words = getWords(note); chars = getChars(note); if (words > maxWords) { maxWords = words; } if (chars > maxChars) { maxChars = chars; } totalWords += words; totalChars += chars; } mStats.setNotesActive(notesActive); mStats.setNotesArchived(notesArchived); mStats.setNotesTrashed(notesTrashed); mStats.setReminders(reminders); mStats.setRemindersFutures(remindersFuture); mStats.setNotesChecklist(checklists); mStats.setNotesMasked(notesMasked); mStats.setTags(tags); mStats.setLocation(locations); avgWords = totalWords / (notes.size() != 0 ? notes.size() : 1); avgChars = totalChars / (notes.size() != 0 ? notes.size() : 1); mStats.setWords(totalWords); mStats.setWordsMax(maxWords); mStats.setWordsAvg(avgWords); mStats.setChars(totalChars); mStats.setCharsMax(maxChars); mStats.setCharsAvg(avgChars); // Everything about attachments int attachmentsAll = 0, images = 0, videos = 0, audioRecordings = 0, sketches = 0, files = 0; List<Attachment> attachments = getAllAttachments(); for (Attachment attachment : attachments) { if (Constants.MIME_TYPE_IMAGE.equals(attachment.getMime_type())) { images++; } else if (Constants.MIME_TYPE_VIDEO.equals(attachment.getMime_type())) { videos++; } else if (Constants.MIME_TYPE_AUDIO.equals(attachment.getMime_type())) { audioRecordings++; } else if (Constants.MIME_TYPE_SKETCH.equals(attachment.getMime_type())) { sketches++; } else if (Constants.MIME_TYPE_FILES.equals(attachment.getMime_type())) { files++; } } mStats.setAttachments(attachmentsAll); mStats.setImages(images); mStats.setVideos(videos); mStats.setAudioRecordings(audioRecordings); mStats.setSketches(sketches); mStats.setFiles(files); return mStats; }