diff --git a/omniNotes/build.gradle b/omniNotes/build.gradle index 418f5d9387..34417c1246 100644 --- a/omniNotes/build.gradle +++ b/omniNotes/build.gradle @@ -181,7 +181,9 @@ dependencies { exclude group: 'com.android.support' } implementation 'com.github.federicoiosue:Springpad-Importer:1.0.1' - implementation 'com.github.federicoiosue:Omni-Notes-Commons:1.2.0' + implementation ('com.github.federicoiosue:Omni-Notes-Commons:1.3.0-SNAPSHOT') { + exclude group: 'org.projectlombok' + } implementation ('com.github.federicoiosue:checklistview:3.2.1') { transitive=false } diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/DetailFragment.java b/omniNotes/src/main/java/it/feio/android/omninotes/DetailFragment.java index 505ff23ba4..d02d891cf5 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/DetailFragment.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/DetailFragment.java @@ -1243,21 +1243,15 @@ private void categorizeNote() { .positiveColorRes(R.color.colorPrimary) .negativeText(R.string.remove_category) .negativeColorRes(R.color.colorAccent) - .onPositive(new MaterialDialog.SingleButtonCallback() { - @Override - public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { - Intent intent = new Intent(mainActivity, CategoryActivity.class); - intent.putExtra("noHome", true); - startActivityForResult(intent, CATEGORY); - } - }) - .onNegative(new MaterialDialog.SingleButtonCallback() { - @Override - public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { - noteTmp.setCategory(null); - setTagMarkerColor(null); - } - }).build(); + .onPositive((dialog1, which) -> { + Intent intent = new Intent(mainActivity, CategoryActivity.class); + intent.putExtra("noHome", true); + startActivityForResult(intent, CATEGORY); + }) + .onNegative((dialog12, which) -> { + noteTmp.setCategory(null); + setTagMarkerColor(null); + }).build(); dialog.getListView().setOnItemClickListener((parent, view, position, id) -> { noteTmp.setCategory(categories.get(position)); @@ -1609,7 +1603,7 @@ public void onNoteSaved(Note noteSaved) { EventBus.getDefault().post(new NotesUpdatedEvent(Collections.singletonList(noteSaved))); deleteMergedNotes(mergedNotesIds); if (noteTmp.getAlarm() != null && !noteTmp.getAlarm().equals(note.getAlarm())) { - ReminderHelper.showReminderMessage(noteTmp.getAlarm()); + ReminderHelper.showReminderMessage(String.valueOf(noteTmp.getAlarm())); } } note = new Note(noteSaved); @@ -1718,7 +1712,7 @@ private String initReminder(Note note) { if (noteTmp.getAlarm() == null) { return ""; } - long reminder = parseLong(note.getAlarm()); + long reminder = note.getAlarm(); String rrule = note.getRecurrenceRule(); if (!TextUtils.isEmpty(rrule)) { return DateHelper.getNoteRecurrentReminderText(reminder, rrule); @@ -1995,8 +1989,7 @@ public void onRecurrenceReminderPicked(String recurrenceRule) { noteTmp.setRecurrenceRule(recurrenceRule); if (!TextUtils.isEmpty(recurrenceRule)) { Log.d(Constants.TAG, "Recurrent reminder set: " + recurrenceRule); - datetime.setText(DateHelper.getNoteRecurrentReminderText(parseLong(noteTmp - .getAlarm()), recurrenceRule)); + datetime.setText(DateHelper.getNoteRecurrentReminderText(noteTmp.getAlarm(), recurrenceRule)); } } diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/OmniNotes.java b/omniNotes/src/main/java/it/feio/android/omninotes/OmniNotes.java index a7cc4bc4d6..0afc9b284c 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/OmniNotes.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/OmniNotes.java @@ -21,7 +21,6 @@ import android.content.SharedPreferences; import android.content.res.Configuration; import android.os.StrictMode; -import android.support.annotation.NonNull; import android.support.multidex.MultiDexApplication; import com.squareup.leakcanary.LeakCanary; diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/SnoozeActivity.java b/omniNotes/src/main/java/it/feio/android/omninotes/SnoozeActivity.java index c208db3bdd..dc70d53e4c 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/SnoozeActivity.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/SnoozeActivity.java @@ -75,7 +75,7 @@ private void manageNotification(SharedPreferences prefs) { updateNoteReminder(newReminder, note); finish(); } else if (Constants.ACTION_POSTPONE.equals(getIntent().getAction())) { - postpone(prefs, Long.parseLong(note.getAlarm()), note.getRecurrenceRule()); + postpone(prefs, note.getAlarm(), note.getRecurrenceRule()); } else { Intent intent = new Intent(this, MainActivity.class); intent.putExtra(Constants.INTENT_KEY, note.get_id()); @@ -132,7 +132,7 @@ public void onRecurrenceReminderPicked(String recurrenceRule) { public static void setNextRecurrentReminder(Note note) { if (!TextUtils.isEmpty(note.getRecurrenceRule())) { - long nextReminder = DateHelper.nextReminderFromRecurrenceRule(Long.parseLong(note.getAlarm()), note + long nextReminder = DateHelper.nextReminderFromRecurrenceRule(note.getAlarm(), note .getRecurrenceRule()); if (nextReminder > 0) { updateNoteReminder(nextReminder, note, true); @@ -154,7 +154,7 @@ private static void updateNoteReminder(long reminder, Note noteToUpdate, boolean new SaveNoteTask(false).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, noteToUpdate); } else { ReminderHelper.addReminder(OmniNotes.getAppContext(), noteToUpdate, reminder); - ReminderHelper.showReminderMessage(noteToUpdate.getAlarm()); + ReminderHelper.showReminderMessage(String.valueOf(noteToUpdate.getAlarm())); } } diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/db/DbHelper.java b/omniNotes/src/main/java/it/feio/android/omninotes/db/DbHelper.java index 29ef14368a..099174962b 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/db/DbHelper.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/db/DbHelper.java @@ -198,7 +198,6 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } - // Inserting or updating single note public Note updateNote(Note note, boolean updateLastModification) { SQLiteDatabase db = getDatabase(true); @@ -206,18 +205,18 @@ public Note updateNote(Note note, boolean updateLastModification) { ? Security.encrypt(note.getContent(), prefs.getString(Constants.PREF_PASSWORD, "")) : note.getContent(); - // To ensure note and attachments insertions are atomical and boost performances transaction are used + // To ensure note and attachments insertions are atomic 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() + values.put(KEY_CREATION, note.getCreation() > 0 ? note.getCreation() : Calendar.getInstance() .getTimeInMillis()); - values.put(KEY_LAST_MODIFICATION, updateLastModification ? Calendar - .getInstance().getTimeInMillis() : (note.getLastModification() != null ? note.getLastModification() : - Calendar - .getInstance().getTimeInMillis())); + long lastModification = note.getLastModification() > 0 && !updateLastModification + ? note.getLastModification() + : Calendar.getInstance().getTimeInMillis(); + values.put(KEY_LAST_MODIFICATION, lastModification); values.put(KEY_ARCHIVED, note.isArchived()); values.put(KEY_TRASHED, note.isTrashed()); values.put(KEY_REMINDER, note.getAlarm()); @@ -227,10 +226,8 @@ public Note updateNote(Note note, boolean updateLastModification) { 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); + values.put(KEY_LOCKED, note.isLocked()); + values.put(KEY_CHECKLIST, note.isChecklist()); db.insertWithOnConflict(TABLE_NOTES, KEY_ID, values, SQLiteDatabase.CONFLICT_REPLACE); Log.d(Constants.TAG, "Updated note titled '" + note.getTitle() + "'"); @@ -252,7 +249,7 @@ public Note updateNote(Note note, boolean updateLastModification) { db.close(); // Fill the note with correct data before returning it - note.setCreation(note.getCreation() != null ? note.getCreation() : values.getAsLong(KEY_CREATION)); + note.setCreation(note.getCreation() > 0 ? note.getCreation() : values.getAsLong(KEY_CREATION)); note.setLastModification(values.getAsLong(KEY_LAST_MODIFICATION)); return note; @@ -451,8 +448,8 @@ public List getNotes(String whereCondition, boolean order) { 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.setAlarm(cursor.getLong(i++)); + note.setReminderFired(cursor.getInt(i++) != 0); note.setRecurrenceRule(cursor.getString(i++)); note.setLatitude(cursor.getString(i++)); note.setLongitude(cursor.getString(i++)); @@ -982,8 +979,8 @@ public Stats getStats() { } else { notesActive++; } - if (note.getAlarm() != null && Long.parseLong(note.getAlarm()) > 0) { - if (Long.parseLong(note.getAlarm()) > Calendar.getInstance().getTimeInMillis()) { + if (note.getAlarm() != null && note.getAlarm() > 0) { + if (note.getAlarm() > Calendar.getInstance().getTimeInMillis()) { remindersFuture++; } else { reminders++; diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/helpers/NotesHelper.java b/omniNotes/src/main/java/it/feio/android/omninotes/helpers/NotesHelper.java index 62f58f13a3..dae9233b6e 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/helpers/NotesHelper.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/helpers/NotesHelper.java @@ -75,7 +75,7 @@ public static void addAttachments(boolean keepMergedNotes, Note note, ArrayList< public static Note mergeNotes(List notes, boolean keepMergedNotes) { boolean locked = false; ArrayList attachments = new ArrayList<>(); - String reminder = null; + Long reminder = null; String reminderRecurrenceRule = null; Double latitude = null; Double longitude = null; @@ -91,8 +91,8 @@ public static Note mergeNotes(List notes, boolean keepMergedNotes) { for (Note note : notes) { appendContent(note, content, includeTitle); locked = locked || note.isLocked(); - String currentReminder = note.getAlarm(); - if (!StringUtils.isEmpty(currentReminder) && reminder == null) { + Long currentReminder = note.getAlarm(); + if (reminder == null && currentReminder != null) { reminder = currentReminder; reminderRecurrenceRule = note.getRecurrenceRule(); } diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/models/Note.java b/omniNotes/src/main/java/it/feio/android/omninotes/models/Note.java index 3e75274322..730bcbb5bc 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/models/Note.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/models/Note.java @@ -53,9 +53,9 @@ public Note() { } - public Note(Long creation, Long lastModification, String title, String content, Integer archived, - Integer trashed, String alarm, String recurrenceRule, Integer reminderFired, String latitude, String longitude, Category - category, Integer locked, Integer checklist) { + public Note(Long creation, Long lastModification, String title, String content, boolean archived, + boolean trashed, long alarm, String recurrenceRule, boolean reminderFired, String latitude, String longitude, Category + category, boolean locked, boolean checklist) { super(creation, lastModification, title, content, archived, trashed, alarm, reminderFired, recurrenceRule, latitude, longitude, category, locked, checklist); @@ -69,21 +69,21 @@ public Note(Note note) { private Note(Parcel in) { - setCreation(in.readString()); - setLastModification(in.readString()); + setCreation(in.readLong()); + setLastModification(in.readLong()); setTitle(in.readString()); setContent(in.readString()); - setArchived(in.readInt()); - setTrashed(in.readInt()); - setAlarm(in.readString()); - setReminderFired(in.readInt()); + setArchived(in.readInt() != 0); + setTrashed(in.readInt() != 0); + setAlarm(in.readLong()); + setReminderFired(in.readInt() != 0); setRecurrenceRule(in.readString()); setLatitude(in.readString()); setLongitude(in.readString()); setAddress(in.readString()); super.setCategory(in.readParcelable(Category.class.getClassLoader())); - setLocked(in.readInt()); - setChecklist(in.readInt()); + setLocked(in.readInt() != 0); + setChecklist(in.readInt() != 0); in.readList(getAttachmentsList(), Attachment.class.getClassLoader()); } @@ -172,7 +172,7 @@ public void writeToParcel(Parcel parcel, int flags) { parcel.writeString(getContent()); parcel.writeInt(isArchived() ? 1 : 0); parcel.writeInt(isTrashed() ? 1 : 0); - parcel.writeString(getAlarm()); + parcel.writeLong(getAlarm()); parcel.writeInt(isReminderFired() ? 1 : 0); parcel.writeString(getRecurrenceRule()); parcel.writeString(String.valueOf(getLatitude())); diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/models/adapters/NoteAdapter.java b/omniNotes/src/main/java/it/feio/android/omninotes/models/adapters/NoteAdapter.java index db37d9677a..0c3a9f341a 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/models/adapters/NoteAdapter.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/models/adapters/NoteAdapter.java @@ -194,7 +194,7 @@ private void manageCloserNote(List notes, int navigation) { if (navigation == Navigation.REMINDERS) { for (int i = 0; i < notes.size(); i++) { long now = Calendar.getInstance().getTimeInMillis(); - long reminder = Long.parseLong(notes.get(i).getAlarm()); + long reminder = notes.get(i).getAlarm(); if (now < reminder && reminder < closestNoteReminder) { closestNotePosition = i; closestNoteReminder = reminder; diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/utils/ReminderHelper.java b/omniNotes/src/main/java/it/feio/android/omninotes/utils/ReminderHelper.java index f7d664c10a..1a44469dd5 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/utils/ReminderHelper.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/utils/ReminderHelper.java @@ -39,7 +39,7 @@ public class ReminderHelper { public static void addReminder(Context context, Note note) { if (note.getAlarm() != null) { - addReminder(context, note, Long.parseLong(note.getAlarm())); + addReminder(context, note, note.getAlarm()); } } @@ -70,13 +70,13 @@ public static boolean checkReminder(Context context, Note note) { static int getRequestCode(Note note) { - Long longCode = note.getCreation() != null ? note.getCreation() : Calendar.getInstance().getTimeInMillis() / 1000L; + Long longCode = note.getCreation() > 0 ? note.getCreation() : Calendar.getInstance().getTimeInMillis() / 1000L; return longCode.intValue(); } public static void removeReminder(Context context, Note note) { - if (!TextUtils.isEmpty(note.getAlarm())) { + if (note.getAlarm() != null) { AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, AlarmReceiver.class); PendingIntent p = PendingIntent.getBroadcast(context, getRequestCode(note), intent, 0); diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/utils/TextHelper.java b/omniNotes/src/main/java/it/feio/android/omninotes/utils/TextHelper.java index 074fcab5ef..4cf60af08c 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/utils/TextHelper.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/utils/TextHelper.java @@ -132,12 +132,11 @@ public static String getDateText(Context mContext, Note note, int navigation) { (), prefs.getBoolean(Constants.PREF_PRETTIFIED_DATES, true)); break; case DbHelper.KEY_REMINDER: - String noteReminder = note.getAlarm(); - if (TextUtils.isEmpty(noteReminder)) { + if (note.getAlarm() == null) { dateText = mContext.getString(R.string.no_reminder_set); } else { dateText = mContext.getString(R.string.alarm_set_on) + " " + DateHelper.getDateTimeShort(mContext, - Long.parseLong(noteReminder)); + note.getAlarm()); } break; default: diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/utils/date/DateUtils.java b/omniNotes/src/main/java/it/feio/android/omninotes/utils/date/DateUtils.java index a33fdf3b5e..1c55883d66 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/utils/date/DateUtils.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/utils/date/DateUtils.java @@ -174,12 +174,8 @@ public static boolean isFuture(String timestamp) { /** * Checks if a epoch-date timestamp is in the future */ - public static boolean isFuture(long timestamp) { - try { - return timestamp > Calendar.getInstance().getTimeInMillis(); - } catch (Exception e) { - return false; - } + public static boolean isFuture(Long timestamp) { + return timestamp != null && timestamp > Calendar.getInstance().getTimeInMillis(); } public static String prettyTime(String timeInMillisec) { diff --git a/omniNotes/src/test/java/it/feio/android/omninotes/utils/date/DateUtilsTest.java b/omniNotes/src/test/java/it/feio/android/omninotes/utils/date/DateUtilsTest.java index c1794af037..98868cdf9b 100644 --- a/omniNotes/src/test/java/it/feio/android/omninotes/utils/date/DateUtilsTest.java +++ b/omniNotes/src/test/java/it/feio/android/omninotes/utils/date/DateUtilsTest.java @@ -75,6 +75,12 @@ public void isFuture() { assertFalse(DateUtils.isFuture(previousMinute)); } + @Test + public void isFutureManagesNullValues() { + Long longValue = null; + assertFalse(DateUtils.isFuture(longValue)); + } + @Test public void isSameDay() { long today = Calendar.getInstance().getTimeInMillis();