diff options
author | Stefan Niedermann <info@niedermann.it> | 2020-10-05 21:51:07 +0300 |
---|---|---|
committer | Stefan Niedermann <info@niedermann.it> | 2020-10-05 21:51:07 +0300 |
commit | 3a8077cd909b41402b6286732395d692ab2843c0 (patch) | |
tree | 8233ede5995309f00055ebf7c57733b0288e55b2 | |
parent | 8feb06f93b8bf69d323de3813c65a0ba684eea97 (diff) |
#831 Migrate from SQLiteOpenHelper to Room
22 files changed, 322 insertions, 403 deletions
diff --git a/app/schemas/it.niedermann.owncloud.notes.persistence.NotesRoomDatabase/18.json b/app/schemas/it.niedermann.owncloud.notes.persistence.NotesRoomDatabase/18.json index 9cbda007..5103867a 100644 --- a/app/schemas/it.niedermann.owncloud.notes.persistence.NotesRoomDatabase/18.json +++ b/app/schemas/it.niedermann.owncloud.notes.persistence.NotesRoomDatabase/18.json @@ -2,7 +2,7 @@ "formatVersion": 1, "database": { "version": 18, - "identityHash": "a4ed702c70d8211c3310d6ea3600c097", + "identityHash": "4b66ff34ab595ccc99419eba8b2de6c7", "entities": [ { "tableName": "LocalAccountEntity", @@ -80,7 +80,7 @@ }, { "tableName": "NoteEntity", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `remoteId` INTEGER NOT NULL, `accountId` INTEGER NOT NULL, `status` TEXT, `title` TEXT, `modified` INTEGER NOT NULL, `content` TEXT, `favorite` INTEGER, `eTag` TEXT, `excerpt` TEXT, `scrollY` INTEGER NOT NULL, `category_id` INTEGER, `category_accountId` INTEGER, `category_title` TEXT, PRIMARY KEY(`id`))", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `remoteId` INTEGER NOT NULL, `accountId` INTEGER NOT NULL, `status` TEXT, `title` TEXT, `modified` INTEGER NOT NULL, `content` TEXT, `favorite` INTEGER, `eTag` TEXT, `excerpt` TEXT, `scrollY` INTEGER NOT NULL, `category_id` INTEGER, `category_accountId` INTEGER, `category_title` TEXT, `category_categorySortingMethod` INTEGER, PRIMARY KEY(`id`))", "fields": [ { "fieldPath": "id", @@ -165,6 +165,12 @@ "columnName": "category_title", "affinity": "TEXT", "notNull": false + }, + { + "fieldPath": "category.categorySortingMethod", + "columnName": "category_categorySortingMethod", + "affinity": "INTEGER", + "notNull": false } ], "primaryKey": { @@ -178,7 +184,7 @@ }, { "tableName": "CategoryEntity", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `accountId` INTEGER NOT NULL, `title` TEXT, PRIMARY KEY(`id`))", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `accountId` INTEGER NOT NULL, `title` TEXT, `categorySortingMethod` INTEGER, PRIMARY KEY(`id`))", "fields": [ { "fieldPath": "id", @@ -197,6 +203,12 @@ "columnName": "title", "affinity": "TEXT", "notNull": false + }, + { + "fieldPath": "categorySortingMethod", + "columnName": "categorySortingMethod", + "affinity": "INTEGER", + "notNull": false } ], "primaryKey": { @@ -210,13 +222,31 @@ }, { "tableName": "WidgetSingleNoteEntity", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, PRIMARY KEY(`id`))", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`noteId` INTEGER NOT NULL, `id` INTEGER NOT NULL, `accountId` INTEGER NOT NULL, `themeMode` INTEGER NOT NULL, PRIMARY KEY(`id`))", "fields": [ { + "fieldPath": "noteId", + "columnName": "noteId", + "affinity": "INTEGER", + "notNull": true + }, + { "fieldPath": "id", "columnName": "id", "affinity": "INTEGER", "notNull": true + }, + { + "fieldPath": "accountId", + "columnName": "accountId", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "themeMode", + "columnName": "themeMode", + "affinity": "INTEGER", + "notNull": true } ], "primaryKey": { @@ -230,37 +260,37 @@ }, { "tableName": "WidgetNotesListEntity", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `accountId` INTEGER NOT NULL, `themeMode` INTEGER NOT NULL, `mode` INTEGER NOT NULL, `categoryId` INTEGER, PRIMARY KEY(`id`))", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`mode` INTEGER NOT NULL, `categoryId` INTEGER, `id` INTEGER NOT NULL, `accountId` INTEGER NOT NULL, `themeMode` INTEGER NOT NULL, PRIMARY KEY(`id`))", "fields": [ { - "fieldPath": "id", - "columnName": "id", + "fieldPath": "mode", + "columnName": "mode", "affinity": "INTEGER", "notNull": true }, { - "fieldPath": "accountId", - "columnName": "accountId", + "fieldPath": "categoryId", + "columnName": "categoryId", "affinity": "INTEGER", - "notNull": true + "notNull": false }, { - "fieldPath": "themeMode", - "columnName": "themeMode", + "fieldPath": "id", + "columnName": "id", "affinity": "INTEGER", "notNull": true }, { - "fieldPath": "mode", - "columnName": "mode", + "fieldPath": "accountId", + "columnName": "accountId", "affinity": "INTEGER", "notNull": true }, { - "fieldPath": "categoryId", - "columnName": "categoryId", + "fieldPath": "themeMode", + "columnName": "themeMode", "affinity": "INTEGER", - "notNull": false + "notNull": true } ], "primaryKey": { @@ -276,7 +306,7 @@ "views": [], "setupQueries": [ "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", - "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'a4ed702c70d8211c3310d6ea3600c097')" + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '4b66ff34ab595ccc99419eba8b2de6c7')" ] } }
\ No newline at end of file diff --git a/app/src/androidTest/java/it/niedermann/owncloud/notes/persistence/NotesNotesRoomDatabaseTest.java b/app/src/androidTest/java/it/niedermann/owncloud/notes/persistence/NotesNotesRoomDatabaseTest.java index d22d1953..73548545 100644 --- a/app/src/androidTest/java/it/niedermann/owncloud/notes/persistence/NotesNotesRoomDatabaseTest.java +++ b/app/src/androidTest/java/it/niedermann/owncloud/notes/persistence/NotesNotesRoomDatabaseTest.java @@ -574,12 +574,12 @@ public class NotesNotesRoomDatabaseTest { long noteID = roomDatabase.addNote(account.getId(), cloudNote); // check the default value of ordering_method - CategorySortingMethod defaultMethod = sqliteOpenHelperDatabase.getCategoryOrderByTitle(account.getId(), "CodingDiary"); + CategorySortingMethod defaultMethod = roomDatabase.getCategoryDao().getCategoryOrderByTitle(account.getId(), "CodingDiary"); assertEquals(defaultMethod, CategorySortingMethod.getCSM(0)); // modify the value of ordering_method and check - sqliteOpenHelperDatabase.modifyCategoryOrderByTitle(account.getId(), "CodingDiary", CategorySortingMethod.getCSM(1)); - CategorySortingMethod methodAfterModify = sqliteOpenHelperDatabase.getCategoryOrderByTitle(account.getId(), "CodingDiary"); + roomDatabase.getCategoryDao().modifyCategoryOrderByTitle(account.getId(), "CodingDiary", CategorySortingMethod.getCSM(1)); + CategorySortingMethod methodAfterModify = roomDatabase.getCategoryDao().getCategoryOrderByTitle(account.getId(), "CodingDiary"); assertEquals(methodAfterModify, CategorySortingMethod.getCSM(1)); // delete the Node @@ -596,12 +596,12 @@ public class NotesNotesRoomDatabaseTest { long noteID = roomDatabase.addNote(account.getId(), cloudNote); // check the default value of ordering_method - CategorySortingMethod defaultMethod = sqliteOpenHelperDatabase.getCategoryOrder(account.getId(), new Category("CodingDiary", false)); + CategorySortingMethod defaultMethod = roomDatabase.getCategoryOrder(account.getId(), new Category("CodingDiary", false)); assertEquals(defaultMethod, CategorySortingMethod.getCSM(0)); // modify the value of ordering_method and check - sqliteOpenHelperDatabase.modifyCategoryOrderByTitle(account.getId(), "CodingDiary", CategorySortingMethod.getCSM(1)); - CategorySortingMethod methodAfterModify = sqliteOpenHelperDatabase.getCategoryOrder(account.getId(), new Category("CodingDiary", false)); + roomDatabase.getCategoryDao().modifyCategoryOrderByTitle(account.getId(), "CodingDiary", CategorySortingMethod.getCSM(1)); + CategorySortingMethod methodAfterModify = roomDatabase.getCategoryOrder(account.getId(), new Category("CodingDiary", false)); assertEquals(methodAfterModify, CategorySortingMethod.getCSM(1)); // delete the Node @@ -615,31 +615,31 @@ public class NotesNotesRoomDatabaseTest { spe.apply(); // check default value // all notes - defaultMethod = sqliteOpenHelperDatabase.getCategoryOrder(account.getId(), new Category(null, false)); + defaultMethod = roomDatabase.getCategoryOrder(account.getId(), new Category(null, false)); assertEquals(defaultMethod, CategorySortingMethod.getCSM(0)); // uncategorized - defaultMethod = sqliteOpenHelperDatabase.getCategoryOrder(account.getId(), new Category("", false)); + defaultMethod = roomDatabase.getCategoryOrder(account.getId(), new Category("", false)); assertEquals(defaultMethod, CategorySortingMethod.getCSM(0)); // favorite - defaultMethod = sqliteOpenHelperDatabase.getCategoryOrder(account.getId(), new Category(null, true)); + defaultMethod = roomDatabase.getCategoryOrder(account.getId(), new Category(null, true)); assertEquals(defaultMethod, CategorySortingMethod.getCSM(0)); // modify the value of ordering_method and check // all notes - sqliteOpenHelperDatabase.modifyCategoryOrder(account.getId(), new Category(null, false), CategorySortingMethod.getCSM(1)); - methodAfterModify = sqliteOpenHelperDatabase.getCategoryOrder(account.getId(), new Category(null, false)); + roomDatabase.modifyCategoryOrder(account.getId(), new Category(null, false), CategorySortingMethod.getCSM(1)); + methodAfterModify = roomDatabase.getCategoryOrder(account.getId(), new Category(null, false)); assertEquals(methodAfterModify, CategorySortingMethod.getCSM(1)); // uncategorized - sqliteOpenHelperDatabase.modifyCategoryOrder(account.getId(), new Category("", false), CategorySortingMethod.getCSM(1)); - methodAfterModify = sqliteOpenHelperDatabase.getCategoryOrder(account.getId(), new Category("", false)); + roomDatabase.modifyCategoryOrder(account.getId(), new Category("", false), CategorySortingMethod.getCSM(1)); + methodAfterModify = roomDatabase.getCategoryOrder(account.getId(), new Category("", false)); assertEquals(methodAfterModify, CategorySortingMethod.getCSM(1)); // favorite - sqliteOpenHelperDatabase.modifyCategoryOrder(account.getId(), new Category(null, true), CategorySortingMethod.getCSM(1)); - methodAfterModify = sqliteOpenHelperDatabase.getCategoryOrder(account.getId(), new Category(null, true)); + roomDatabase.modifyCategoryOrder(account.getId(), new Category(null, true), CategorySortingMethod.getCSM(1)); + methodAfterModify = roomDatabase.getCategoryOrder(account.getId(), new Category(null, true)); assertEquals(methodAfterModify, CategorySortingMethod.getCSM(1)); // delete SharedPreferences diff --git a/app/src/main/java/it/niedermann/owncloud/notes/edit/BaseNoteFragment.java b/app/src/main/java/it/niedermann/owncloud/notes/edit/BaseNoteFragment.java index 82cbdeb4..7772f33e 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/edit/BaseNoteFragment.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/edit/BaseNoteFragment.java @@ -236,7 +236,7 @@ public abstract class BaseNoteFragment extends BrandedFragment implements Catego listener.close(); return true; case R.id.menu_favorite: - sqliteOpenHelperDatabase.toggleFavorite(ssoAccount, note, null); + roomDatabase.toggleFavoriteAndSync(ssoAccount, note.getId(), null); listener.onNoteUpdated(note); prepareFavoriteOption(item); return true; 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 5822f2a7..e99a6dde 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 @@ -413,14 +413,14 @@ public class MainActivity extends LockedActivity implements NoteClickListener, V activityBinding.sortingMethod.setOnClickListener((v) -> { CategorySortingMethod method; - method = sqliteOpenHelperDatabase.getCategoryOrder(localAccount.getId(), navigationSelection); + method = roomDatabase.getCategoryOrder(localAccount.getId(), navigationSelection); if (method == CategorySortingMethod.SORT_LEXICOGRAPHICAL_ASC) { method = CategorySortingMethod.SORT_MODIFIED_DESC; } else { method = CategorySortingMethod.SORT_LEXICOGRAPHICAL_ASC; } - sqliteOpenHelperDatabase.modifyCategoryOrder(localAccount.getId(), navigationSelection, method); + roomDatabase.modifyCategoryOrder(localAccount.getId(), navigationSelection, method); refreshLists(); updateSortMethodIcon(localAccount.getId()); }); @@ -717,7 +717,7 @@ public class MainActivity extends LockedActivity implements NoteClickListener, V * Updates sorting method icon. */ private void updateSortMethodIcon(long localAccountId) { - CategorySortingMethod method = sqliteOpenHelperDatabase.getCategoryOrder(localAccountId, navigationSelection); + CategorySortingMethod method = roomDatabase.getCategoryOrder(localAccountId, navigationSelection); if (method == CategorySortingMethod.SORT_LEXICOGRAPHICAL_ASC) { activityBinding.sortingMethod.setImageResource(R.drawable.alphabetical_asc); activityBinding.sortingMethod.setContentDescription(getString(R.string.sort_last_modified)); @@ -885,8 +885,8 @@ public class MainActivity extends LockedActivity implements NoteClickListener, V @Override public void onNoteFavoriteClick(int position, View view) { DBNote note = (DBNote) adapter.getItem(position); - NotesDatabase db = NotesDatabase.getInstance(view.getContext()); - db.toggleFavorite(ssoAccount, note, syncCallBack); + NotesRoomDatabase db = NotesRoomDatabase.getInstance(view.getContext()); + db.toggleFavoriteAndSync(ssoAccount, note.getId(), syncCallBack); adapter.notifyItemChanged(position); refreshLists(); } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/main/items/list/NotesListViewItemTouchHelper.java b/app/src/main/java/it/niedermann/owncloud/notes/main/items/list/NotesListViewItemTouchHelper.java index 131e5c56..caa80d7d 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/main/items/list/NotesListViewItemTouchHelper.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/main/items/list/NotesListViewItemTouchHelper.java @@ -96,7 +96,7 @@ public class NotesListViewItemTouchHelper extends ItemTouchHelper { break; case ItemTouchHelper.RIGHT: final DBNote adapterNote = (DBNote) adapter.getItem(viewHolder.getAdapterPosition()); - sqliteOpenHelperDatabase.toggleFavorite(ssoAccount, adapterNote, syncCallBack); + roomDatabase.toggleFavoriteAndSync(ssoAccount, adapterNote.getId(), syncCallBack); refreshLists.run(); break; default: diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/LoadNotesListTask.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/LoadNotesListTask.java index 0380b0be..b5449e3c 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/LoadNotesListTask.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/LoadNotesListTask.java @@ -39,9 +39,10 @@ public class LoadNotesListTask extends AsyncTask<Void, Void, List<Item>> { @Override protected List<Item> doInBackground(Void... voids) { List<DBNote> noteList; - NotesDatabase db = NotesDatabase.getInstance(context); - CategorySortingMethod sortingMethod = db.getCategoryOrder(accountId, category); - noteList = db.searchNotes(accountId, searchQuery, category.category, category.favorite, sortingMethod); + NotesDatabase sqliteOpenHelperDatabase = NotesDatabase.getInstance(context); + NotesRoomDatabase roomDatabase = NotesRoomDatabase.getInstance(context); + CategorySortingMethod sortingMethod = roomDatabase.getCategoryOrder(accountId, category); + noteList = sqliteOpenHelperDatabase.searchNotes(accountId, searchQuery, category.category, category.favorite, sortingMethod); if (category.category == null) { if (sortingMethod == CategorySortingMethod.SORT_MODIFIED_DESC) { 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 d0ed3c4f..da4782c6 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 @@ -2,27 +2,15 @@ package it.niedermann.owncloud.notes.persistence; import android.content.ContentValues; import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.content.pm.ShortcutInfo; -import android.content.pm.ShortcutManager; import android.content.res.Resources; import android.database.Cursor; -import android.database.DatabaseUtils; -import android.database.SQLException; -import android.database.sqlite.SQLiteConstraintException; import android.database.sqlite.SQLiteDatabase; -import android.graphics.Color; -import android.graphics.drawable.Icon; -import android.os.Build; import android.text.TextUtils; import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.WorkerThread; -import androidx.core.content.ContextCompat; -import androidx.preference.PreferenceManager; import com.nextcloud.android.sso.AccountImporter; import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException; @@ -33,36 +21,21 @@ import org.json.JSONException; import java.util.ArrayList; import java.util.Calendar; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; import java.util.List; -import java.util.Map; -import java.util.NoSuchElementException; -import java.util.Set; import it.niedermann.owncloud.notes.R; -import it.niedermann.owncloud.notes.edit.EditNoteActivity; import it.niedermann.owncloud.notes.main.NavigationAdapter; import it.niedermann.owncloud.notes.shared.model.ApiVersion; -import it.niedermann.owncloud.notes.shared.model.Capabilities; -import it.niedermann.owncloud.notes.shared.model.Category; import it.niedermann.owncloud.notes.shared.model.CategorySortingMethod; import it.niedermann.owncloud.notes.shared.model.CloudNote; import it.niedermann.owncloud.notes.shared.model.DBNote; import it.niedermann.owncloud.notes.shared.model.DBStatus; import it.niedermann.owncloud.notes.shared.model.ISyncCallback; import it.niedermann.owncloud.notes.shared.model.LocalAccount; -import it.niedermann.owncloud.notes.shared.util.ColorUtil; import it.niedermann.owncloud.notes.shared.util.NoteUtil; -import it.niedermann.owncloud.notes.widget.notelist.NoteListsWidgetData; -import it.niedermann.owncloud.notes.widget.singlenote.SingleNoteWidget; -import it.niedermann.owncloud.notes.widget.singlenote.SingleNoteWidgetData; -import static it.niedermann.owncloud.notes.edit.EditNoteActivity.ACTION_SHORTCUT; import static it.niedermann.owncloud.notes.shared.util.NoteUtil.generateNoteExcerpt; import static it.niedermann.owncloud.notes.widget.notelist.NoteListWidget.updateNoteListWidgets; -import static it.niedermann.owncloud.notes.widget.notelist.NoteListsWidgetData.MODE_DISPLAY_CATEGORY; import static it.niedermann.owncloud.notes.widget.singlenote.SingleNoteWidget.updateSingleNoteWidgets; /** @@ -302,33 +275,6 @@ public class NotesDatabase extends AbstractNotesDatabase { return categories; } - public String getCategoryTitleById(long accountId, long categoryId) { - validateAccountId(accountId); - final String categoryTitle; - final Cursor cursor = getReadableDatabase().query(table_category, new String[]{key_category_title}, key_category_id + " = ?", new String[]{String.valueOf(categoryId)}, null, null, null); - if (cursor.moveToFirst()) { - categoryTitle = cursor.getString(0); - } else { - categoryTitle = null; - } - cursor.close(); - return categoryTitle; - } - - public void toggleFavorite(SingleSignOnAccount ssoAccount, @NonNull DBNote note, @Nullable ISyncCallback callback) { - note.setFavorite(!note.isFavorite()); - note.setStatus(DBStatus.LOCAL_EDITED); - SQLiteDatabase db = this.getWritableDatabase(); - ContentValues values = new ContentValues(2); - values.put(key_status, note.getStatus().getTitle()); - values.put(key_favorite, note.isFavorite() ? "1" : "0"); - db.update(table_notes, values, key_id + " = ?", new String[]{String.valueOf(note.getId())}); - if (callback != null) { - serverSyncHelper.addCallbackPush(ssoAccount, callback); - } - serverSyncHelper.scheduleSync(ssoAccount, true); - } - private long addCategory(long accountId, @NonNull String title) { validateAccountId(accountId); SQLiteDatabase db = getWritableDatabase(); @@ -490,32 +436,6 @@ public class NotesDatabase extends AbstractNotesDatabase { return false; } - - /** - * Set the category for a given note. - * This method will search in the database to find out the category id in the db. - * If there is no such category existing, this method will create it and search again. - * - * @param ssoAccount The single sign on account - * @param note The note which will be updated - * @param category The category title which should be used to find the category id. - * @param callback When the synchronization is finished, this callback will be invoked (optional). - */ - public void setCategory(SingleSignOnAccount ssoAccount, @NonNull DBNote note, @NonNull String category, @Nullable ISyncCallback callback) { - note.setCategory(category); - note.setStatus(DBStatus.LOCAL_EDITED); - SQLiteDatabase db = this.getWritableDatabase(); - ContentValues values = new ContentValues(2); - values.put(key_status, note.getStatus().getTitle()); - int id = getCategoryIdByTitle(note.getAccountId(), note.getCategory()); - values.put(key_category, id); - db.update(table_notes, values, key_id + " = ?", new String[]{String.valueOf(note.getId())}); - removeEmptyCategory(note.getAccountId()); - if (callback != null) { - serverSyncHelper.addCallbackPush(ssoAccount, callback); - } - serverSyncHelper.scheduleSync(ssoAccount, true); - } /** * @param localAccount the {@link LocalAccount} that should be deleted * @throws IllegalArgumentException if no account has been deleted by the given accountId @@ -542,57 +462,6 @@ public class NotesDatabase extends AbstractNotesDatabase { Log.v(TAG, "Deleted " + deletedNotes + " notes from account " + localAccount.getId()); } - /** - * @param appWidgetId the id of the {@link SingleNoteWidget} - * @return {@link SingleNoteWidgetData} - * @throws NoSuchElementException in case there is no {@link SingleNoteWidgetData} for the given appWidgetId - */ - @NonNull - public SingleNoteWidgetData getSingleNoteWidgetData(int appWidgetId) throws NoSuchElementException { - SingleNoteWidgetData data = new SingleNoteWidgetData(); - final SQLiteDatabase db = getReadableDatabase(); - final Cursor cursor = db.query(table_widget_single_notes, new String[]{key_account_id, key_note_id, key_theme_mode}, key_id + " = ?", new String[]{String.valueOf(appWidgetId)}, null, null, null, null); - if (cursor.moveToNext()) { - data.setAppWidgetId(appWidgetId); - data.setAccountId(cursor.getLong(0)); - data.setNoteId(cursor.getLong(1)); - data.setThemeMode(cursor.getInt(2)); - } else { - throw new NoSuchElementException(); - } - cursor.close(); - return data; - } - - public void createOrUpdateSingleNoteWidgetData(@NonNull SingleNoteWidgetData data) throws SQLException { - validateAccountId(data.getAccountId()); - final SQLiteDatabase db = getWritableDatabase(); - final ContentValues values = new ContentValues(4); - values.put(key_id, data.getAppWidgetId()); - values.put(key_account_id, data.getAccountId()); - values.put(key_note_id, data.getNoteId()); - values.put(key_theme_mode, data.getThemeMode()); - db.replaceOrThrow(table_widget_single_notes, null, values); - } - - @NonNull - public NoteListsWidgetData getNoteListWidgetData(int appWidgetId) throws NoSuchElementException { - NoteListsWidgetData data = new NoteListsWidgetData(); - final SQLiteDatabase db = getReadableDatabase(); - final Cursor cursor = db.query(table_widget_note_list, new String[]{key_account_id, key_category_id, key_theme_mode, key_mode}, key_id + " = ?", new String[]{String.valueOf(appWidgetId)}, null, null, null, null); - if (cursor.moveToNext()) { - data.setAppWidgetId(appWidgetId); - data.setAccountId(cursor.getLong(0)); - data.setCategoryId(cursor.getLong(1)); - data.setThemeMode(cursor.getInt(2)); - data.setMode(cursor.getInt(3)); - } else { - throw new NoSuchElementException(); - } - cursor.close(); - return data; - } - private static void validateAccountId(long accountId) { if (accountId < 1) { throw new IllegalArgumentException("accountId must be greater than 0"); @@ -608,10 +477,8 @@ public class NotesDatabase extends AbstractNotesDatabase { * * @param accountId The user {@link LocalAccount} Id * @param categoryTitle The category title which will be search in the db - * - * @deprecated replaced by #{{@link NotesRoomDatabase#getOrCreateCategoryIdByTitle(long, String)}} - * * @return -1 if there is no such category else the corresponding id + * @deprecated replaced by #{{@link NotesRoomDatabase#getOrCreateCategoryIdByTitle(long, String)}} */ @NonNull @WorkerThread @@ -655,140 +522,4 @@ public class NotesDatabase extends AbstractNotesDatabase { key_category_id + " NOT IN (SELECT " + key_category + " FROM " + table_notes + ")", null); } - - /** - * This function is used to get the sorting method of a category by title. - * The sorting method of the category can be used to decide - * to use which sorting method to show the notes for each categories. - * - * @param accountId The user accountID - * @param categoryTitle The category title - * @return The sorting method in {@link CategorySortingMethod} enum format - */ - public CategorySortingMethod getCategoryOrderByTitle(long accountId, String categoryTitle) { - validateAccountId(accountId); - - long categoryId = getCategoryIdByTitle(accountId, categoryTitle); - - SQLiteDatabase db = getReadableDatabase(); - int orderIndex; - try (Cursor cursor = db.query(table_category, new String[]{key_category_sorting_method}, - key_category_id + " = ?", new String[]{String.valueOf(categoryId)}, - null, null, null)) { - orderIndex = 0; - while (cursor.moveToNext()) { - orderIndex = cursor.getInt(0); - } - } - - return CategorySortingMethod.getCSM(orderIndex); - } - - /** - * This method is used to modify the sorting method for one category by title. - * The user can determine use which sorting method to show the notes for a category. - * When the user changes the sorting method, this method should be called. - * - * @param accountId The user accountID - * @param categoryTitle The category title - * @param sortingMethod The sorting method in {@link CategorySortingMethod} enum format - */ - public void modifyCategoryOrderByTitle( - long accountId, String categoryTitle, CategorySortingMethod sortingMethod) { - validateAccountId(accountId); - - long categoryId = getCategoryIdByTitle(accountId, categoryTitle); - - SQLiteDatabase db = getWritableDatabase(); - - ContentValues values = new ContentValues(); - values.put(key_category_sorting_method, sortingMethod.getCSMID()); - db.update(table_category, values, - key_category_id + " = ?", new String[]{String.valueOf(categoryId)}); - } - - /** - * Gets the sorting method of a category, the category can be normal category or - * one of "All notes", "Favorite", and "Uncategorized". - * If category is one of these three, sorting method will be got from android.content.SharedPreference. - * The sorting method of the category can be used to decide - * to use which sorting method to show the notes for each categories. - * - * @param accountId The user accountID - * @param category The category - * @return The sorting method in CategorySortingMethod enum format - */ - public CategorySortingMethod getCategoryOrder(long accountId, Category category) { - validateAccountId(accountId); - - final Context ctx = getContext().getApplicationContext(); - final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(ctx); - int orderIndex; - - if (category.category == null) { - if (category.favorite != null && category.favorite) { - // Favorite - orderIndex = sp.getInt(ctx.getString(R.string.action_sorting_method) + - ' ' + ctx.getString(R.string.label_favorites), - 0); - } else { - // All notes - orderIndex = sp.getInt(ctx.getString(R.string.action_sorting_method) + - ' ' + ctx.getString(R.string.label_all_notes), - 0); - } - } else if (category.category.isEmpty()) { - // Uncategorized - orderIndex = sp.getInt(ctx.getString(R.string.action_sorting_method) + - ' ' + ctx.getString(R.string.action_uncategorized), - 0); - } else { - return getCategoryOrderByTitle(accountId, category.category); - } - - return CategorySortingMethod.getCSM(orderIndex); - } - - /** - * Modifies the sorting method for one category, the category can be normal category or - * one of "All notes", "Favorite", and "Uncategorized". - * If category is one of these three, sorting method will be modified in android.content.SharedPreference. - * The user can determine use which sorting method to show the notes for a category. - * When the user changes the sorting method, this method should be called. - * - * @param accountId The user accountID - * @param category The category to be modified - * @param sortingMethod The sorting method in {@link CategorySortingMethod} enum format - */ - public void modifyCategoryOrder( - long accountId, Category category, CategorySortingMethod sortingMethod) { - validateAccountId(accountId); - - final Context ctx = getContext().getApplicationContext(); - final SharedPreferences.Editor sp = PreferenceManager.getDefaultSharedPreferences(ctx).edit(); - int orderIndex = sortingMethod.getCSMID(); - if (category.category == null) { - if (category.favorite != null && category.favorite) { - // Favorite - sp.putInt(ctx.getString(R.string.action_sorting_method) + - ' ' + ctx.getString(R.string.label_favorites), - orderIndex); - } else { - // All notes - sp.putInt(ctx.getString(R.string.action_sorting_method) + - ' ' + ctx.getString(R.string.label_all_notes), - orderIndex); - } - } else if (category.category.isEmpty()) { - // Uncategorized - sp.putInt(ctx.getString(R.string.action_sorting_method) + - ' ' + ctx.getString(R.string.action_uncategorized), - orderIndex); - } else { - modifyCategoryOrderByTitle(accountId, category.category, sortingMethod); - return; - } - sp.apply(); - } - } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRoomDatabase.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRoomDatabase.java index efac971a..0535cb81 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRoomDatabase.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRoomDatabase.java @@ -1,9 +1,12 @@ package it.niedermann.owncloud.notes.persistence; +import android.content.ContentValues; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.content.pm.ShortcutInfo; import android.content.pm.ShortcutManager; +import android.database.sqlite.SQLiteDatabase; import android.graphics.drawable.Icon; import android.os.Build; import android.text.TextUtils; @@ -12,6 +15,7 @@ import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.WorkerThread; +import androidx.preference.PreferenceManager; import androidx.room.Database; import androidx.room.Room; import androidx.room.RoomDatabase; @@ -41,6 +45,8 @@ import it.niedermann.owncloud.notes.persistence.entity.NoteEntity; import it.niedermann.owncloud.notes.persistence.entity.WidgetNotesListEntity; import it.niedermann.owncloud.notes.persistence.entity.WidgetSingleNoteEntity; import it.niedermann.owncloud.notes.shared.model.Capabilities; +import it.niedermann.owncloud.notes.shared.model.Category; +import it.niedermann.owncloud.notes.shared.model.CategorySortingMethod; import it.niedermann.owncloud.notes.shared.model.CloudNote; import it.niedermann.owncloud.notes.shared.model.DBNote; import it.niedermann.owncloud.notes.shared.model.DBStatus; @@ -336,4 +342,96 @@ public abstract class NotesRoomDatabase extends RoomDatabase { } return result; } + + /** + * Modifies the sorting method for one category, the category can be normal category or + * one of "All notes", "Favorite", and "Uncategorized". + * If category is one of these three, sorting method will be modified in android.content.SharedPreference. + * The user can determine use which sorting method to show the notes for a category. + * When the user changes the sorting method, this method should be called. + * + * @param accountId The user accountID + * @param category The category to be modified + * @param sortingMethod The sorting method in {@link CategorySortingMethod} enum format + */ + public void modifyCategoryOrder( + long accountId, Category category, CategorySortingMethod sortingMethod) { + validateAccountId(accountId); + + final Context ctx = context.getApplicationContext(); + final SharedPreferences.Editor sp = PreferenceManager.getDefaultSharedPreferences(ctx).edit(); + int orderIndex = sortingMethod.getCSMID(); + if (category.category == null) { + if (category.favorite != null && category.favorite) { + // Favorite + sp.putInt(ctx.getString(R.string.action_sorting_method) + + ' ' + ctx.getString(R.string.label_favorites), + orderIndex); + } else { + // All notes + sp.putInt(ctx.getString(R.string.action_sorting_method) + + ' ' + ctx.getString(R.string.label_all_notes), + orderIndex); + } + } else if (category.category.isEmpty()) { + // Uncategorized + sp.putInt(ctx.getString(R.string.action_sorting_method) + + ' ' + ctx.getString(R.string.action_uncategorized), + orderIndex); + } else { + getCategoryDao().modifyCategoryOrderByTitle(accountId, category.category, sortingMethod); + return; + } + sp.apply(); + } + + /** + * Gets the sorting method of a category, the category can be normal category or + * one of "All notes", "Favorite", and "Uncategorized". + * If category is one of these three, sorting method will be got from android.content.SharedPreference. + * The sorting method of the category can be used to decide + * to use which sorting method to show the notes for each categories. + * + * @param accountId The user accountID + * @param category The category + * @return The sorting method in CategorySortingMethod enum format + */ + public CategorySortingMethod getCategoryOrder(long accountId, Category category) { + validateAccountId(accountId); + + final Context ctx = context.getApplicationContext(); + final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(ctx); + int orderIndex; + + if (category.category == null) { + if (category.favorite != null && category.favorite) { + // Favorite + orderIndex = sp.getInt(ctx.getString(R.string.action_sorting_method) + + ' ' + ctx.getString(R.string.label_favorites), + 0); + } else { + // All notes + orderIndex = sp.getInt(ctx.getString(R.string.action_sorting_method) + + ' ' + ctx.getString(R.string.label_all_notes), + 0); + } + } else if (category.category.isEmpty()) { + // Uncategorized + orderIndex = sp.getInt(ctx.getString(R.string.action_sorting_method) + + ' ' + ctx.getString(R.string.action_uncategorized), + 0); + } else { + return getCategoryDao().getCategoryOrderByTitle(accountId, category.category); + } + + return CategorySortingMethod.getCSM(orderIndex); + } + + public void toggleFavoriteAndSync(SingleSignOnAccount ssoAccount, long noteId, @Nullable ISyncCallback callback) { + getNoteDao().toggleFavorite(noteId); + if (callback != null) { + syncHelper.addCallbackPush(ssoAccount, callback); + } + syncHelper.scheduleSync(ssoAccount, true); + } } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/CategoryDao.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/CategoryDao.java index 1800fdf1..06d6af76 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/CategoryDao.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/CategoryDao.java @@ -1,18 +1,29 @@ package it.niedermann.owncloud.notes.persistence.dao; import android.content.ContentValues; +import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.room.Dao; import androidx.room.Insert; import androidx.room.Query; +import com.nextcloud.android.sso.model.SingleSignOnAccount; + import it.niedermann.owncloud.notes.persistence.entity.CategoryEntity; +import it.niedermann.owncloud.notes.shared.model.CategorySortingMethod; +import it.niedermann.owncloud.notes.shared.model.DBNote; +import it.niedermann.owncloud.notes.shared.model.DBStatus; +import it.niedermann.owncloud.notes.shared.model.ISyncCallback; @Dao public interface CategoryDao { + @Insert + Long addCategory(CategoryEntity entity); + /** * This function will be called when the category or note is updated. * Because sometime we will remove some notes in categories. @@ -25,10 +36,34 @@ public interface CategoryDao { @Query("DELETE FROM CategoryEntity WHERE accountId = :accountId AND id NOT IN (SELECT category_id FROM NoteEntity)") void removeEmptyCategory(long accountId); - @Insert - Long addCategory(@NonNull CategoryEntity entity); - @Query("SELECT id FROM CategoryEntity WHERE accountId = :accountId AND title = :title") - Long getCategoryIdByTitle(long accountId, @NonNull String title); + Long getCategoryIdByTitle(long accountId, String title); + + + /** + * This method is used to modify the sorting method for one category by title. + * The user can determine use which sorting method to show the notes for a category. + * When the user changes the sorting method, this method should be called. + * + * @param accountId The user accountID + * @param categoryTitle The category title + * @param sortingMethod The sorting method in {@link CategorySortingMethod} enum format + */ + @Query("UPDATE CategoryEntity SET categorySortingMethod = :sortingMethod WHERE id = (SELECT id FROM CategoryEntity WHERE accountId = :accountId AND title = :categoryTitle)") + void modifyCategoryOrderByTitle(long accountId, String categoryTitle, CategorySortingMethod sortingMethod); + + /** + * This function is used to get the sorting method of a category by title. + * The sorting method of the category can be used to decide + * to use which sorting method to show the notes for each categories. + * + * @param accountId The user accountID + * @param categoryTitle The category title + * @return The sorting method in {@link CategorySortingMethod} enum format + */ + @Query("SELECT categorySortingMethod FROM CategoryEntity WHERE accountId = :accountId AND title = :categoryTitle") + CategorySortingMethod getCategoryOrderByTitle(long accountId, String categoryTitle); + @Query("SELECT title FROM CategoryEntity WHERE id = :categoryId") + String getCategoryTitleById(long categoryId); } 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 ff387668..c9c63ac4 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 @@ -69,4 +69,7 @@ public interface NoteDao { @Query("SELECT * FROM NoteEntity WHERE status != \"LOCAL_DELETED\" AND accountId = :accountId ORDER BY modified DESC LIMIT 4") List<NoteEntity> getRecentNotes(long accountId); + + @Query("UPDATE NoteEntity SET status = \"LOCAL_EDITED\", favorite = ((favorite | 1) - (favorite & 1)) WHERE id = :id") + void toggleFavorite(long id); } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/WidgetNotesListDao.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/WidgetNotesListDao.java index 185c2ae2..444ecb62 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/WidgetNotesListDao.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/WidgetNotesListDao.java @@ -1,18 +1,27 @@ package it.niedermann.owncloud.notes.persistence.dao; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; + import androidx.annotation.NonNull; import androidx.room.Dao; import androidx.room.Insert; import androidx.room.Query; +import java.util.NoSuchElementException; + import it.niedermann.owncloud.notes.persistence.entity.WidgetNotesListEntity; +import it.niedermann.owncloud.notes.widget.notelist.NoteListsWidgetData; @Dao public interface WidgetNotesListDao { + @Insert + void createOrUpdateNoteListWidgetData(WidgetNotesListEntity data); + @Query("DELETE FROM WidgetNotesListEntity WHERE id = :appWidgetId") void removeNoteListWidget(int appWidgetId); - @Insert - void createOrUpdateNoteListWidgetData(@NonNull WidgetNotesListEntity data); + @Query("SELECT * FROM WidgetNotesListEntity WHERE id = :appWidgetId") + NoteListsWidgetData getNoteListWidgetData(int appWidgetId); } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/WidgetSingleNoteDao.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/WidgetSingleNoteDao.java index e79ea1db..be3a3a2a 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/WidgetSingleNoteDao.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/WidgetSingleNoteDao.java @@ -1,18 +1,21 @@ package it.niedermann.owncloud.notes.persistence.dao; -import android.database.sqlite.SQLiteDatabase; - -import androidx.annotation.NonNull; import androidx.room.Dao; import androidx.room.Insert; +import androidx.room.OnConflictStrategy; import androidx.room.Query; -import it.niedermann.owncloud.notes.persistence.entity.CategoryEntity; +import it.niedermann.owncloud.notes.persistence.entity.WidgetSingleNoteEntity; @Dao public interface WidgetSingleNoteDao { - @Query("DELETE FROM WidgetSingleNoteEntity WHERE id = :appWidgetId") - void removeSingleNoteWidget(int appWidgetId); + @Insert(onConflict = OnConflictStrategy.REPLACE) + void createOrUpdateSingleNoteWidgetData(WidgetSingleNoteEntity data); + + @Query("DELETE FROM WidgetSingleNoteEntity WHERE id = :id") + void removeSingleNoteWidget(int id); + @Query("SELECT * FROM WidgetSingleNoteEntity WHERE id = :id") + WidgetSingleNoteEntity getSingleNoteWidgetData(int id); } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/CategoryEntity.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/CategoryEntity.java index 907b12f3..da4c6702 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/CategoryEntity.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/CategoryEntity.java @@ -3,12 +3,15 @@ package it.niedermann.owncloud.notes.persistence.entity; import androidx.room.Entity; import androidx.room.PrimaryKey; +import it.niedermann.owncloud.notes.shared.model.CategorySortingMethod; + @Entity public class CategoryEntity { @PrimaryKey private long id; private long accountId; private String title; + private CategorySortingMethod categorySortingMethod; public long getId() { return id; @@ -33,6 +36,14 @@ public class CategoryEntity { public void setTitle(String title) { this.title = title; } + + public CategorySortingMethod getCategorySortingMethod() { + return categorySortingMethod; + } + + public void setCategorySortingMethod(CategorySortingMethod categorySortingMethod) { + this.categorySortingMethod = categorySortingMethod; + } } // "FOREIGN KEY(" + key_category + ") REFERENCES " + table_category + "(" + key_category_id + "), " + // "FOREIGN KEY(" + key_account_id + ") REFERENCES " + table_accounts + "(" + key_id + "))"); diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/Converters.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/Converters.java index 06e027b5..9e8ecd36 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/Converters.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/Converters.java @@ -2,6 +2,7 @@ package it.niedermann.owncloud.notes.persistence.entity; import androidx.room.TypeConverter; +import it.niedermann.owncloud.notes.shared.model.CategorySortingMethod; import it.niedermann.owncloud.notes.shared.model.DBStatus; public class Converters { @@ -16,4 +17,14 @@ public class Converters { return status == null ? null : status.getTitle(); } + @TypeConverter + public static CategorySortingMethod categorySortingMethodFromString(Integer value) { + return value == null ? null : CategorySortingMethod.getCSM(value); + } + + @TypeConverter + public static Integer dbStatusToString(CategorySortingMethod categorySortingMethod) { + return categorySortingMethod == null ? null : categorySortingMethod.getCSMID(); + } + } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/WidgetNotesListEntity.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/WidgetNotesListEntity.java index 7a56cf2d..9180f552 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/WidgetNotesListEntity.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/WidgetNotesListEntity.java @@ -7,8 +7,11 @@ import androidx.room.Entity; import androidx.room.Ignore; import androidx.room.PrimaryKey; +import it.niedermann.owncloud.notes.widget.AbstractWidgetData; + @Entity() -public class WidgetNotesListEntity { +public class WidgetNotesListEntity extends AbstractWidgetData { + @Ignore public static final int MODE_DISPLAY_ALL = 0; @Ignore @@ -16,36 +19,12 @@ public class WidgetNotesListEntity { @Ignore public static final int MODE_DISPLAY_CATEGORY = 2; - @PrimaryKey - private long id; - - private long accountId; - - @IntRange(from = 0, to = 2) - private int themeMode; - @IntRange(from = 0, to = 2) private int mode; - public long getId() { - return id; - } - - public void setId(long id) { - this.id = id; - } - @Nullable private Long categoryId; - public int getMode() { - return mode; - } - - public void setMode(@IntRange(from = 0, to = 2) int mode) { - this.mode = mode; - } - @Nullable public Long getCategoryId() { return categoryId; @@ -55,28 +34,19 @@ public class WidgetNotesListEntity { this.categoryId = categoryId; } - @NonNull + public void setMode(int mode) { + this.mode = mode; + } + + public int getMode() { + return mode; + } + @Override public String toString() { - return "NoteListsWidgetData{" + + return "WidgetNotesListEntity{" + "mode=" + mode + ", categoryId=" + categoryId + '}'; } - - public long getAccountId() { - return accountId; - } - - public void setAccountId(long accountId) { - this.accountId = accountId; - } - - public int getThemeMode() { - return themeMode; - } - - public void setThemeMode(int themeMode) { - this.themeMode = themeMode; - } }
\ No newline at end of file diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/WidgetSingleNoteEntity.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/WidgetSingleNoteEntity.java index 0679fb40..b818fefd 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/WidgetSingleNoteEntity.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/WidgetSingleNoteEntity.java @@ -1,24 +1,29 @@ package it.niedermann.owncloud.notes.persistence.entity; -import androidx.room.Embedded; import androidx.room.Entity; -import androidx.room.PrimaryKey; +import androidx.room.Ignore; -import java.util.Calendar; - -import it.niedermann.owncloud.notes.shared.model.DBNote; -import it.niedermann.owncloud.notes.shared.model.DBStatus; +import it.niedermann.owncloud.notes.widget.AbstractWidgetData; @Entity() -public class WidgetSingleNoteEntity { - @PrimaryKey - private long id; +public class WidgetSingleNoteEntity extends AbstractWidgetData { + private long noteId; + + public WidgetSingleNoteEntity() { + + } + + @Ignore + public WidgetSingleNoteEntity(int id, long accountId, long noteId, int modeId) { + super(id, accountId, modeId); + setNoteId(noteId); + } - public long getId() { - return id; + public long getNoteId() { + return noteId; } - public void setId(long id) { - this.id = id; + public void setNoteId(long noteId) { + this.noteId = noteId; } }
\ No newline at end of file diff --git a/app/src/main/java/it/niedermann/owncloud/notes/widget/AbstractWidgetData.java b/app/src/main/java/it/niedermann/owncloud/notes/widget/AbstractWidgetData.java index 019a2153..9873d100 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/widget/AbstractWidgetData.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/widget/AbstractWidgetData.java @@ -1,27 +1,32 @@ package it.niedermann.owncloud.notes.widget; +import androidx.annotation.IntRange; +import androidx.room.PrimaryKey; + public abstract class AbstractWidgetData { - private int appWidgetId; + @PrimaryKey + private int id; private long accountId; + @IntRange(from = 0, to = 2) private int themeMode; protected AbstractWidgetData() { } - protected AbstractWidgetData(int appWidgetId, long accountId, int themeMode) { - this.appWidgetId = appWidgetId; + protected AbstractWidgetData(int id, long accountId, @IntRange(from = 0, to = 2) int themeMode) { + this.id = id; this.accountId = accountId; this.themeMode = themeMode; } - public int getAppWidgetId() { - return appWidgetId; + public int getId() { + return id; } - public void setAppWidgetId(int appWidgetId) { - this.appWidgetId = appWidgetId; + public void setId(int id) { + this.id = id; } public long getAccountId() { @@ -32,11 +37,12 @@ public abstract class AbstractWidgetData { this.accountId = accountId; } + @IntRange(from = 0, to = 2) public int getThemeMode() { return themeMode; } - public void setThemeMode(int themeMode) { + public void setThemeMode(@IntRange(from = 0, to = 2) int themeMode) { this.themeMode = themeMode; } } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidget.java b/app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidget.java index c2a0f1af..1c94433c 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidget.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidget.java @@ -45,12 +45,12 @@ public class NoteListWidget extends AppWidgetProvider { for (int appWidgetId : appWidgetIds) { try { - final NoteListsWidgetData data = sqliteOpenHelperDatabase.getNoteListWidgetData(appWidgetId); + final NoteListsWidgetData data = roomDatabase.getWidgetNotesListDao().getNoteListWidgetData(appWidgetId); final LocalAccountEntity localAccountEntity = roomDatabase.getLocalAccountDao().getAccount(data.getAccountId()); String category = null; if (data.getCategoryId() != null) { - category = sqliteOpenHelperDatabase.getCategoryTitleById(data.getAccountId(), data.getCategoryId()); + category = roomDatabase.getCategoryDao().getCategoryTitleById(data.getCategoryId()); } darkTheme = DarkModeSetting.fromModeID(data.getThemeMode()); diff --git a/app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidgetFactory.java b/app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidgetFactory.java index 5fde5010..c5b1da01 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidgetFactory.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidgetFactory.java @@ -12,6 +12,7 @@ import android.widget.RemoteViewsService; import java.util.List; import it.niedermann.owncloud.notes.R; +import it.niedermann.owncloud.notes.persistence.NotesRoomDatabase; import it.niedermann.owncloud.notes.preferences.DarkModeSetting; import it.niedermann.owncloud.notes.edit.EditNoteActivity; import it.niedermann.owncloud.notes.shared.model.DBNote; @@ -28,7 +29,8 @@ public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFact private final Context context; private final NoteListsWidgetData data; private final boolean darkTheme; - private NotesDatabase db; + private NotesDatabase sqliteOpenHelperDatabase; + private NotesRoomDatabase roomDatabase; private List<DBNote> dbNotes; NoteListWidgetFactory(Context context, Intent intent) { @@ -36,8 +38,9 @@ public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFact final int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); - db = NotesDatabase.getInstance(context); - data = db.getNoteListWidgetData(appWidgetId); + sqliteOpenHelperDatabase = NotesDatabase.getInstance(context); + roomDatabase = NotesRoomDatabase.getInstance(context); + data = roomDatabase.getWidgetNotesListDao().getNoteListWidgetData(appWidgetId); darkTheme = NotesApplication.isDarkThemeActive(context, DarkModeSetting.fromModeID(data.getThemeMode())); } @@ -52,14 +55,14 @@ public class NoteListWidgetFactory implements RemoteViewsService.RemoteViewsFact Log.v(TAG, "--- data - " + data); switch (data.getMode()) { case MODE_DISPLAY_ALL: - dbNotes = db.getNotes(data.getAccountId()); + dbNotes = sqliteOpenHelperDatabase.getNotes(data.getAccountId()); break; case MODE_DISPLAY_STARRED: - dbNotes = db.searchNotes(data.getAccountId(), null, null, true); + dbNotes = sqliteOpenHelperDatabase.searchNotes(data.getAccountId(), null, null, true); break; case MODE_DISPLAY_CATEGORY: if (data.getCategoryId() != null) { - dbNotes = db.searchNotes(data.getAccountId(), null, db.getCategoryTitleById(data.getAccountId(), data.getCategoryId()), null); + dbNotes = sqliteOpenHelperDatabase.searchNotes(data.getAccountId(), null, roomDatabase.getCategoryDao().getCategoryTitleById(data.getCategoryId()), null); } break; } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidget.java b/app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidget.java index c7b94d15..4f5fdcdc 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidget.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidget.java @@ -14,6 +14,7 @@ import java.util.NoSuchElementException; import it.niedermann.owncloud.notes.R; import it.niedermann.owncloud.notes.persistence.NotesRoomDatabase; +import it.niedermann.owncloud.notes.persistence.entity.WidgetSingleNoteEntity; import it.niedermann.owncloud.notes.preferences.DarkModeSetting; import it.niedermann.owncloud.notes.edit.EditNoteActivity; import it.niedermann.owncloud.notes.edit.BaseNoteFragment; @@ -26,11 +27,11 @@ public class SingleNoteWidget extends AppWidgetProvider { static void updateAppWidget(Context context, AppWidgetManager awm, int[] appWidgetIds) { final Intent templateIntent = new Intent(context, EditNoteActivity.class); - final NotesDatabase db = NotesDatabase.getInstance(context); + final NotesRoomDatabase db = NotesRoomDatabase.getInstance(context); for (int appWidgetId : appWidgetIds) { try { - final SingleNoteWidgetData data = db.getSingleNoteWidgetData(appWidgetId); + final WidgetSingleNoteEntity data = db.getWidgetSingleNoteDao().getSingleNoteWidgetData(appWidgetId); templateIntent.putExtra(BaseNoteFragment.PARAM_ACCOUNT_ID, data.getAccountId()); diff --git a/app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidgetConfigurationActivity.java b/app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidgetConfigurationActivity.java index 25d4f408..fd17fe51 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidgetConfigurationActivity.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidgetConfigurationActivity.java @@ -15,6 +15,7 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import it.niedermann.owncloud.notes.R; import it.niedermann.owncloud.notes.exception.ExceptionHandler; import it.niedermann.owncloud.notes.main.MainActivity; +import it.niedermann.owncloud.notes.persistence.entity.WidgetSingleNoteEntity; import it.niedermann.owncloud.notes.shared.model.DBNote; import it.niedermann.owncloud.notes.NotesApplication; @@ -52,8 +53,8 @@ public class SingleNoteWidgetConfigurationActivity extends MainActivity { int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); try { - sqliteOpenHelperDatabase.createOrUpdateSingleNoteWidgetData( - new SingleNoteWidgetData( + roomDatabase.getWidgetSingleNoteDao().createOrUpdateSingleNoteWidgetData( + new WidgetSingleNoteEntity( appWidgetId, note.getAccountId(), note.getId(), diff --git a/app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidgetFactory.java b/app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidgetFactory.java index 431bcd7b..1ef8223c 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidgetFactory.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/widget/singlenote/SingleNoteWidgetFactory.java @@ -16,6 +16,7 @@ import java.util.NoSuchElementException; import it.niedermann.owncloud.notes.R; import it.niedermann.owncloud.notes.persistence.NotesRoomDatabase; import it.niedermann.owncloud.notes.persistence.entity.NoteEntity; +import it.niedermann.owncloud.notes.persistence.entity.WidgetSingleNoteEntity; import it.niedermann.owncloud.notes.preferences.DarkModeSetting; import it.niedermann.owncloud.notes.edit.EditNoteActivity; import it.niedermann.owncloud.notes.shared.model.DBNote; @@ -47,7 +48,7 @@ public class SingleNoteWidgetFactory implements RemoteViewsService.RemoteViewsFa markdownProcessor = new MarkdownProcessor(this.context); markdownProcessor.factory(TextFactory.create()); try { - SingleNoteWidgetData data = sqliteOpenHelperDatabase.getSingleNoteWidgetData(appWidgetId); + WidgetSingleNoteEntity data = roomDatabase.getWidgetSingleNoteDao().getSingleNoteWidgetData(appWidgetId); darkModeActive = NotesApplication.isDarkThemeActive(context, DarkModeSetting.fromModeID(data.getThemeMode())); } catch (NoSuchElementException e) { Log.w(TAG, "Widget with ID " + appWidgetId + " seems to be not configured yet."); @@ -64,7 +65,7 @@ public class SingleNoteWidgetFactory implements RemoteViewsService.RemoteViewsFa @Override public void onDataSetChanged() { try { - final SingleNoteWidgetData data = sqliteOpenHelperDatabase.getSingleNoteWidgetData(appWidgetId); + final WidgetSingleNoteEntity data = roomDatabase.getWidgetSingleNoteDao().getSingleNoteWidgetData(appWidgetId); final long noteId = data.getNoteId(); Log.v(TAG, "Fetch note with id " + noteId); note = NoteEntity.entityToDBNote(roomDatabase.getNoteDao().getNote(data.getAccountId(), noteId)); |