From a6b830c5cd6a706e0dca23dba0f32674a56d6203 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Wed, 29 Mar 2023 21:55:07 +0600 Subject: [PATCH 1/7] 1147-Migrate_old_table_notes issue: https://gitlab.e.foundation/e/os/backlog/-/issues/1147 Migrate missing notes from `NOTES` table to `Note` table. As `NOTES` table is not added in the roomDB entityList, adding now will clean all current data & recreate the table. To bypass this, we need to retrieve old table data via raw-SQL. The note is missing, is checked by comparing `NOTES` table's note content & note title with the `Note` table's notes. --- .../owncloud/notes/main/MainActivity.java | 2 + .../owncloud/notes/main/MainViewModel.java | 5 + .../notes/persistence/NotesDatabase.java | 23 ++- .../notes/persistence/NotesRepository.java | 67 +++++++++ .../owncloud/notes/persistence/OldNote.java | 137 ++++++++++++++++++ .../notes/persistence/OldNoteRetriever.java | 83 +++++++++++ .../notes/persistence/dao/NoteDao.java | 3 + .../owncloud/notes/shared/model/DBStatus.java | 14 ++ 8 files changed, 330 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/it/niedermann/owncloud/notes/persistence/OldNote.java create mode 100644 app/src/main/java/it/niedermann/owncloud/notes/persistence/OldNoteRetriever.java diff --git a/app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java b/app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java index c1d4a119d..fee778b7a 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java @@ -175,6 +175,8 @@ public class MainActivity extends LockedActivity implements NoteClickListener, A executor.submit(() -> { try { final var account = mainViewModel.getLocalAccountByAccountName(SingleAccountHelper.getCurrentSingleSignOnAccount(getApplicationContext()).name); + mainViewModel.migrateOldNotes(account); // add migrated notes to currently active account + runOnUiThread(() -> mainViewModel.postCurrentAccount(account)); } catch (NextcloudFilesAppAccountNotFoundException e) { // Verbose log output for https://github.com/stefan-niedermann/nextcloud-notes/issues/1256 diff --git a/app/src/main/java/it/niedermann/owncloud/notes/main/MainViewModel.java b/app/src/main/java/it/niedermann/owncloud/notes/main/MainViewModel.java index 5ce45bd3e..601e89171 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/main/MainViewModel.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/main/MainViewModel.java @@ -663,4 +663,9 @@ public class MainViewModel extends AndroidViewModel { final var lower = input.toLowerCase(Locale.ROOT); return lower.contains("failed to connect") && lower.contains("network is unreachable"); } + + public void migrateOldNotes(@NonNull Account account) { + repo.migrateOldNotesIfPossible(account.getId()); + } + } \ No newline at end of file diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesDatabase.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesDatabase.java index ac9898c1e..db7d7ded5 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesDatabase.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesDatabase.java @@ -1,7 +1,6 @@ package it.niedermann.owncloud.notes.persistence; import android.content.Context; -import trikita.log.Log; import androidx.annotation.NonNull; import androidx.room.Database; @@ -10,6 +9,8 @@ import androidx.room.RoomDatabase; import androidx.room.TypeConverters; import androidx.sqlite.db.SupportSQLiteDatabase; +import java.util.List; + import it.niedermann.owncloud.notes.persistence.dao.AccountDao; import it.niedermann.owncloud.notes.persistence.dao.CategoryOptionsDao; import it.niedermann.owncloud.notes.persistence.dao.NoteDao; @@ -35,6 +36,7 @@ import it.niedermann.owncloud.notes.persistence.migration.Migration_20_21; import it.niedermann.owncloud.notes.persistence.migration.Migration_21_22; import it.niedermann.owncloud.notes.persistence.migration.Migration_22_23; import it.niedermann.owncloud.notes.persistence.migration.Migration_9_10; +import trikita.log.Log; @Database( entities = { @@ -61,9 +63,9 @@ public abstract class NotesDatabase extends RoomDatabase { private static NotesDatabase create(final Context context) { return Room.databaseBuilder( - context, - NotesDatabase.class, - NOTES_DB_NAME) + context, + NotesDatabase.class, + NOTES_DB_NAME) .addMigrations( new Migration_9_10(), // v2.0.0 new Migration_10_11(context), @@ -105,4 +107,17 @@ public abstract class NotesDatabase extends RoomDatabase { public abstract WidgetSingleNoteDao getWidgetSingleNoteDao(); public abstract WidgetNotesListDao getWidgetNotesListDao(); + + /** + * retrieve notes from old `NOTES` table for migration. + * If we try to follow the *ROOM way* (entity, dao) for `NOTES` table, the existing data will be removed. + * So we have to retrieve old entries via raw sql commands. + * + * Check for more details + * @return list of old notes entries + */ + @NonNull + public List getNotesFromOldTable() { + return OldNoteRetriever.getNotesFromOldTable(this); + } } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java index 9770197b9..0d9cd954f 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java @@ -21,6 +21,7 @@ import android.content.pm.ShortcutManager; import android.graphics.drawable.Icon; import android.net.ConnectivityManager; import android.text.TextUtils; + import trikita.log.Log; import androidx.annotation.AnyThread; @@ -79,6 +80,8 @@ import retrofit2.Call; @SuppressWarnings("UnusedReturnValue") public class NotesRepository { + private static final String pref_key_migration_done = "old_note_migration_done"; + private static final String TAG = NotesRepository.class.getSimpleName(); private static NotesRepository instance; @@ -958,4 +961,68 @@ public class NotesRepository { public void updateDisplayName(long id, @Nullable String displayName) { db.getAccountDao().updateDisplayName(id, displayName); } + + /** + * migrate old notes to latest note table. + * if the migration is already done, skip. + * link the provided account to the migrated notes, as old notes don't have account entry to them. + * + * * Check for more details. + * @param accountId which will be used to link migrated notes to account + */ + @AnyThread + public void migrateOldNotesIfPossible(long accountId) { + if (isMigrationDone()) { + return; + } + + migrateOldNotes(accountId); + } + + private void migrateOldNotes(long accountId) { + try { + executor.submit(() -> { + List oldNotes = db.getNotesFromOldTable(); + if (oldNotes.isEmpty()) { + updatePrefOnMigrationDone(); + return; + } + + for (OldNote oldNote : oldNotes) { + if (isNoteAlreadyExistsInNewTable(oldNote)) { + continue; + } + + addNewNote(accountId, oldNote); + } + + updatePrefOnMigrationDone(); + }); + } catch (Exception e) { + Log.e(TAG, e.getMessage()); + } + } + + private boolean isNoteAlreadyExistsInNewTable(OldNote oldNote) { + return db.getNoteDao().countByTitleAndContent(oldNote.getTitle(), oldNote.getContent()) > 0; + } + + private void addNewNote(long accountId, OldNote oldNote) { + Note newNote = new Note(oldNote.getRemoteId(), oldNote.getModified(), oldNote.getTitle(), oldNote.getContent(), oldNote.getCategory(), oldNote.getFavorite(), oldNote.getETag()); + newNote.setStatus(oldNote.getStatus()); + newNote.setAccountId(accountId); + db.getNoteDao().addNote(newNote); + } + + private boolean isMigrationDone() { + final var sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()); + return sharedPreferences.getBoolean(pref_key_migration_done, false); + } + + private void updatePrefOnMigrationDone() { + final var sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()); + sharedPreferences.edit() + .putBoolean(pref_key_migration_done, true) + .apply(); + } } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/OldNote.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/OldNote.java new file mode 100644 index 000000000..196dcfdca --- /dev/null +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/OldNote.java @@ -0,0 +1,137 @@ +/* + * Copyright MURENA SAS 2023 + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package it.niedermann.owncloud.notes.persistence; + + +import androidx.annotation.NonNull; + +import java.io.Serializable; +import java.util.Calendar; + +import it.niedermann.owncloud.notes.shared.model.DBStatus; + +/** + * Entity representation for `NOTES` table. + * This is added to migrate old `NOTES` items to new `Note` table. + * + * Check for more details + */ +public class OldNote implements Serializable { + + private long id; + + private long remoteId; + + private DBStatus status; + + private String title; + + private Calendar modified; + + private String content; + + private boolean favorite = false; + + private String category = ""; + + private String eTag; + + public OldNote(long id, long remoteId, Calendar modified, String title, String content, boolean favorite, String category, String eTag, DBStatus status) { + this.id = id; + this.remoteId = remoteId; + this.status = status; + this.title = title; + this.modified = modified; + this.content = content; + this.favorite = favorite; + this.category = category; + this.eTag = eTag; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public long getRemoteId() { + return remoteId; + } + + public void setRemoteId(long remoteId) { + this.remoteId = remoteId; + } + + public DBStatus getStatus() { + return status; + } + + public void setStatus(DBStatus status) { + this.status = status; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public Calendar getModified() { + return modified; + } + + public void setModified(Calendar modified) { + this.modified = modified; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public boolean getFavorite() { + return favorite; + } + + public void setFavorite(boolean favorite) { + this.favorite = favorite; + } + + @NonNull + public String getCategory() { + return category; + } + + public void setCategory(@NonNull String category) { + this.category = category; + } + + public String getETag() { + return eTag; + } + + public void setETag(String eTag) { + this.eTag = eTag; + } +} diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/OldNoteRetriever.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/OldNoteRetriever.java new file mode 100644 index 000000000..0f5ad98a8 --- /dev/null +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/OldNoteRetriever.java @@ -0,0 +1,83 @@ +/* + * Copyright MURENA SAS 2023 + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package it.niedermann.owncloud.notes.persistence; + +import android.database.Cursor; + +import androidx.annotation.NonNull; +import androidx.room.RoomDatabase; +import androidx.sqlite.db.SupportSQLiteDatabase; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Collections; +import java.util.List; + +import it.niedermann.owncloud.notes.shared.model.DBStatus; + +/** + * This is a helper class which loads all notes from old table. + * Because of massive version jump, not-sync notes are not migrated to latest `Note` table from old `NOTES` table. + * This helper class load the notes from the `NOTES` table for migration. + * + * Check for more details + */ +public final class OldNoteRetriever { + + private static final String OLD_NOTES_TABLE_NAME = "NOTES"; + + private OldNoteRetriever() { + } + + @NonNull + public static List getNotesFromOldTable(@NonNull RoomDatabase database) { + SupportSQLiteDatabase supportSQLiteDatabase = database.getOpenHelper().getWritableDatabase(); + + if (!isOldNoteTableExists(supportSQLiteDatabase)) { // old notes table can not be existed for fresh install + return Collections.emptyList(); + } + + return retrieveOldNotes(supportSQLiteDatabase); + } + + @NonNull + private static List retrieveOldNotes(SupportSQLiteDatabase supportSQLiteDatabase) { + Cursor oldNotesCursor = supportSQLiteDatabase.query("SELECT * FROM " + OLD_NOTES_TABLE_NAME); + List notes = new ArrayList<>(); + + while (oldNotesCursor.moveToNext()) { + notes.add(getOldNoteFromCursor(oldNotesCursor)); + } + + oldNotesCursor.close(); + return notes; + } + + private static boolean isOldNoteTableExists(SupportSQLiteDatabase supportSQLiteDatabase) { + Cursor tableExistCursor = supportSQLiteDatabase.query("SELECT name FROM sqlite_master WHERE type='table' AND name='" + OLD_NOTES_TABLE_NAME + "'"); + boolean tableExists = tableExistCursor.moveToNext(); + tableExistCursor.close(); + return tableExists; + } + + @NonNull + private static OldNote getOldNoteFromCursor(@NonNull Cursor cursor) { + Calendar modified = Calendar.getInstance(); + modified.setTimeInMillis(cursor.getLong(4) * 1000); + return new OldNote(cursor.getLong(0), cursor.getLong(1), modified, cursor.getString(3), cursor.getString(5), cursor.getInt(6) > 0, cursor.getString(7), cursor.getString(8), DBStatus.parse(cursor.getString(2))); + } +} diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/NoteDao.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/NoteDao.java index cd48b5d80..ce1475ccf 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/NoteDao.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/NoteDao.java @@ -193,4 +193,7 @@ public interface NoteDao { @Query("SELECT COUNT(*) FROM NOTE WHERE STATUS != '' AND accountId = :accountId") Long countUnsynchronizedNotes(long accountId); + + @Query("SELECT COUNT(*) FROM NOTE WHERE title = :title AND content = :content") + Long countByTitleAndContent(String title, String content); } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/shared/model/DBStatus.java b/app/src/main/java/it/niedermann/owncloud/notes/shared/model/DBStatus.java index ef06a277d..332d1b013 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/shared/model/DBStatus.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/shared/model/DBStatus.java @@ -37,4 +37,18 @@ public enum DBStatus { DBStatus(@NonNull String title) { this.title = title; } + + /** + * Parse a String an get the appropriate DBStatus enum element. + * + * @param str The String containing the DBStatus identifier. Must not null. + * @return The DBStatus fitting to the String. + */ + public static DBStatus parse(@NonNull String str) { + if (str.isEmpty()) { + return DBStatus.VOID; + } else { + return DBStatus.valueOf(str); + } + } } -- GitLab From f39de026c0077be7cfcc88b461756137b44520d1 Mon Sep 17 00:00:00 2001 From: Jonathan Klee Date: Fri, 31 Mar 2023 09:45:02 +0000 Subject: [PATCH 2/7] Apply 1 suggestion(s) to 1 file(s) --- .../niedermann/owncloud/notes/persistence/NotesRepository.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java index 0d9cd954f..a2a230fb2 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java @@ -999,7 +999,7 @@ public class NotesRepository { updatePrefOnMigrationDone(); }); } catch (Exception e) { - Log.e(TAG, e.getMessage()); + Log.e(TAG, "An exception has been caught", e); } } -- GitLab From 235d2cd502c38d5fec0259913d7a785d3ede5077 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Fri, 31 Mar 2023 15:55:15 +0600 Subject: [PATCH 3/7] Add null check annotations --- .../notes/persistence/NotesRepository.java | 6 ++--- .../owncloud/notes/persistence/OldNote.java | 24 ++++++++++++++----- .../notes/persistence/OldNoteRetriever.java | 4 ++-- .../owncloud/notes/shared/model/DBStatus.java | 11 +++++---- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java index a2a230fb2..15ca7e14b 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java @@ -989,7 +989,7 @@ public class NotesRepository { } for (OldNote oldNote : oldNotes) { - if (isNoteAlreadyExistsInNewTable(oldNote)) { + if (oldNote == null || isNoteAlreadyExistsInNewTable(oldNote)) { continue; } @@ -1003,11 +1003,11 @@ public class NotesRepository { } } - private boolean isNoteAlreadyExistsInNewTable(OldNote oldNote) { + private boolean isNoteAlreadyExistsInNewTable(@NonNull OldNote oldNote) { return db.getNoteDao().countByTitleAndContent(oldNote.getTitle(), oldNote.getContent()) > 0; } - private void addNewNote(long accountId, OldNote oldNote) { + private void addNewNote(long accountId, @NonNull OldNote oldNote) { Note newNote = new Note(oldNote.getRemoteId(), oldNote.getModified(), oldNote.getTitle(), oldNote.getContent(), oldNote.getCategory(), oldNote.getFavorite(), oldNote.getETag()); newNote.setStatus(oldNote.getStatus()); newNote.setAccountId(accountId); diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/OldNote.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/OldNote.java index 196dcfdca..2da73b7aa 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/OldNote.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/OldNote.java @@ -18,6 +18,7 @@ package it.niedermann.owncloud.notes.persistence; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import java.io.Serializable; import java.util.Calendar; @@ -36,21 +37,27 @@ public class OldNote implements Serializable { private long remoteId; + @NonNull private DBStatus status; + @NonNull private String title; + @Nullable private Calendar modified; + @NonNull private String content; private boolean favorite = false; + @NonNull private String category = ""; + @Nullable private String eTag; - public OldNote(long id, long remoteId, Calendar modified, String title, String content, boolean favorite, String category, String eTag, DBStatus status) { + public OldNote(long id, long remoteId, @Nullable Calendar modified, @NonNull String title, @NonNull String content, boolean favorite, @NonNull String category, @Nullable String eTag, @NonNull DBStatus status) { this.id = id; this.remoteId = remoteId; this.status = status; @@ -78,35 +85,39 @@ public class OldNote implements Serializable { this.remoteId = remoteId; } + @NonNull public DBStatus getStatus() { return status; } - public void setStatus(DBStatus status) { + public void setStatus(@NonNull DBStatus status) { this.status = status; } + @NonNull public String getTitle() { return title; } - public void setTitle(String title) { + public void setTitle(@NonNull String title) { this.title = title; } + @Nullable public Calendar getModified() { return modified; } - public void setModified(Calendar modified) { + public void setModified(@Nullable Calendar modified) { this.modified = modified; } + @NonNull public String getContent() { return content; } - public void setContent(String content) { + public void setContent(@NonNull String content) { this.content = content; } @@ -127,11 +138,12 @@ public class OldNote implements Serializable { this.category = category; } + @Nullable public String getETag() { return eTag; } - public void setETag(String eTag) { + public void setETag(@Nullable String eTag) { this.eTag = eTag; } } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/OldNoteRetriever.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/OldNoteRetriever.java index 0f5ad98a8..e34e468da 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/OldNoteRetriever.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/OldNoteRetriever.java @@ -55,7 +55,7 @@ public final class OldNoteRetriever { } @NonNull - private static List retrieveOldNotes(SupportSQLiteDatabase supportSQLiteDatabase) { + private static List retrieveOldNotes(@NonNull SupportSQLiteDatabase supportSQLiteDatabase) { Cursor oldNotesCursor = supportSQLiteDatabase.query("SELECT * FROM " + OLD_NOTES_TABLE_NAME); List notes = new ArrayList<>(); @@ -67,7 +67,7 @@ public final class OldNoteRetriever { return notes; } - private static boolean isOldNoteTableExists(SupportSQLiteDatabase supportSQLiteDatabase) { + private static boolean isOldNoteTableExists(@NonNull SupportSQLiteDatabase supportSQLiteDatabase) { Cursor tableExistCursor = supportSQLiteDatabase.query("SELECT name FROM sqlite_master WHERE type='table' AND name='" + OLD_NOTES_TABLE_NAME + "'"); boolean tableExists = tableExistCursor.moveToNext(); tableExistCursor.close(); diff --git a/app/src/main/java/it/niedermann/owncloud/notes/shared/model/DBStatus.java b/app/src/main/java/it/niedermann/owncloud/notes/shared/model/DBStatus.java index 332d1b013..26c204473 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/shared/model/DBStatus.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/shared/model/DBStatus.java @@ -1,6 +1,7 @@ package it.niedermann.owncloud.notes.shared.model; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; /** * Helps to distinguish between different local change types for Server Synchronization. @@ -41,14 +42,14 @@ public enum DBStatus { /** * Parse a String an get the appropriate DBStatus enum element. * - * @param str The String containing the DBStatus identifier. Must not null. + * @param str The String containing the DBStatus identifier. * @return The DBStatus fitting to the String. */ - public static DBStatus parse(@NonNull String str) { - if (str.isEmpty()) { + public static DBStatus parse(@Nullable String str) { + if (str == null || str.isEmpty()) { return DBStatus.VOID; - } else { - return DBStatus.valueOf(str); } + + return DBStatus.valueOf(str); } } -- GitLab From 05fa4d0a7dc3743d47b68e313ea4f9cc8e8d82d6 Mon Sep 17 00:00:00 2001 From: Vincent Bourgmayer Date: Mon, 3 Apr 2023 04:48:25 +0000 Subject: [PATCH 4/7] Apply 4 suggestion(s) to 1 file(s) --- .../owncloud/notes/persistence/OldNoteRetriever.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/OldNoteRetriever.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/OldNoteRetriever.java index e34e468da..2e2e6a7d2 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/OldNoteRetriever.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/OldNoteRetriever.java @@ -45,7 +45,7 @@ public final class OldNoteRetriever { @NonNull public static List getNotesFromOldTable(@NonNull RoomDatabase database) { - SupportSQLiteDatabase supportSQLiteDatabase = database.getOpenHelper().getWritableDatabase(); + final SupportSQLiteDatabase supportSQLiteDatabase = database.getOpenHelper().getWritableDatabase(); if (!isOldNoteTableExists(supportSQLiteDatabase)) { // old notes table can not be existed for fresh install return Collections.emptyList(); @@ -56,7 +56,7 @@ public final class OldNoteRetriever { @NonNull private static List retrieveOldNotes(@NonNull SupportSQLiteDatabase supportSQLiteDatabase) { - Cursor oldNotesCursor = supportSQLiteDatabase.query("SELECT * FROM " + OLD_NOTES_TABLE_NAME); + final Cursor oldNotesCursor = supportSQLiteDatabase.query("SELECT * FROM " + OLD_NOTES_TABLE_NAME); List notes = new ArrayList<>(); while (oldNotesCursor.moveToNext()) { @@ -68,15 +68,15 @@ public final class OldNoteRetriever { } private static boolean isOldNoteTableExists(@NonNull SupportSQLiteDatabase supportSQLiteDatabase) { - Cursor tableExistCursor = supportSQLiteDatabase.query("SELECT name FROM sqlite_master WHERE type='table' AND name='" + OLD_NOTES_TABLE_NAME + "'"); - boolean tableExists = tableExistCursor.moveToNext(); + final Cursor tableExistCursor = supportSQLiteDatabase.query("SELECT name FROM sqlite_master WHERE type='table' AND name='" + OLD_NOTES_TABLE_NAME + "'"); + final boolean tableExists = tableExistCursor.moveToNext(); tableExistCursor.close(); return tableExists; } @NonNull private static OldNote getOldNoteFromCursor(@NonNull Cursor cursor) { - Calendar modified = Calendar.getInstance(); + final Calendar modified = Calendar.getInstance(); modified.setTimeInMillis(cursor.getLong(4) * 1000); return new OldNote(cursor.getLong(0), cursor.getLong(1), modified, cursor.getString(3), cursor.getString(5), cursor.getInt(6) > 0, cursor.getString(7), cursor.getString(8), DBStatus.parse(cursor.getString(2))); } -- GitLab From 18ab57fd319113a220cecb9297af7d47db096e07 Mon Sep 17 00:00:00 2001 From: Vincent Bourgmayer Date: Mon, 3 Apr 2023 04:48:36 +0000 Subject: [PATCH 5/7] Apply 1 suggestion(s) to 1 file(s) --- .../niedermann/owncloud/notes/persistence/NotesRepository.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java index 15ca7e14b..301cfec6a 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java @@ -982,7 +982,7 @@ public class NotesRepository { private void migrateOldNotes(long accountId) { try { executor.submit(() -> { - List oldNotes = db.getNotesFromOldTable(); + final List oldNotes = db.getNotesFromOldTable(); if (oldNotes.isEmpty()) { updatePrefOnMigrationDone(); return; -- GitLab From 8f25710e10710033aec997fc10f1ef820c03e202 Mon Sep 17 00:00:00 2001 From: Vincent Bourgmayer Date: Mon, 3 Apr 2023 05:08:48 +0000 Subject: [PATCH 6/7] Apply 1 suggestion(s) to 1 file(s) --- .../java/it/niedermann/owncloud/notes/main/MainActivity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java b/app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java index fee778b7a..51ab9eeef 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java @@ -175,7 +175,7 @@ public class MainActivity extends LockedActivity implements NoteClickListener, A executor.submit(() -> { try { final var account = mainViewModel.getLocalAccountByAccountName(SingleAccountHelper.getCurrentSingleSignOnAccount(getApplicationContext()).name); - mainViewModel.migrateOldNotes(account); // add migrated notes to currently active account + mainViewModel.migrateOldNotes(account); runOnUiThread(() -> mainViewModel.postCurrentAccount(account)); } catch (NextcloudFilesAppAccountNotFoundException e) { -- GitLab From d614f48ea5c0c0aa9096fe555f66247c0c5838d5 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Mon, 3 Apr 2023 12:28:38 +0600 Subject: [PATCH 7/7] Update migration pref key name --- .../owncloud/notes/persistence/NotesRepository.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java index 301cfec6a..e99e88916 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java @@ -80,7 +80,7 @@ import retrofit2.Call; @SuppressWarnings("UnusedReturnValue") public class NotesRepository { - private static final String pref_key_migration_done = "old_note_migration_done"; + private static final String PREF_KEY_MIGRATION_DONE = "old_note_migration_done"; private static final String TAG = NotesRepository.class.getSimpleName(); @@ -982,7 +982,7 @@ public class NotesRepository { private void migrateOldNotes(long accountId) { try { executor.submit(() -> { - final List oldNotes = db.getNotesFromOldTable(); + List oldNotes = db.getNotesFromOldTable(); if (oldNotes.isEmpty()) { updatePrefOnMigrationDone(); return; @@ -1016,13 +1016,13 @@ public class NotesRepository { private boolean isMigrationDone() { final var sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()); - return sharedPreferences.getBoolean(pref_key_migration_done, false); + return sharedPreferences.getBoolean(PREF_KEY_MIGRATION_DONE, false); } private void updatePrefOnMigrationDone() { final var sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()); sharedPreferences.edit() - .putBoolean(pref_key_migration_done, true) + .putBoolean(PREF_KEY_MIGRATION_DONE, true) .apply(); } } -- GitLab