From 8feb06f93b8bf69d323de3813c65a0ba684eea97 Mon Sep 17 00:00:00 2001 From: Stefan Niedermann Date: Mon, 5 Oct 2020 16:31:11 +0200 Subject: [PATCH] #831 Migrate from SQLiteOpenHelper to Room Widgets --- .../18.json | 68 +++++++++++++++++- .../notes/persistence/NotesDatabase.java | 15 ---- .../persistence/dao/WidgetNotesListDao.java | 6 ++ .../entity/WidgetNotesListEntity.java | 72 +++++++++++++++++++ .../entity/WidgetSingleNoteEntity.java | 8 +++ .../NoteListWidgetConfigurationActivity.java | 7 +- 6 files changed, 156 insertions(+), 20 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 baa0a94c..9cbda007 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": "cf4429b4afde78dd86d282a0d0dfea47", + "identityHash": "a4ed702c70d8211c3310d6ea3600c097", "entities": [ { "tableName": "LocalAccountEntity", @@ -207,12 +207,76 @@ }, "indices": [], "foreignKeys": [] + }, + { + "tableName": "WidgetSingleNoteEntity", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, PRIMARY KEY(`id`))", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "columnNames": [ + "id" + ], + "autoGenerate": false + }, + "indices": [], + "foreignKeys": [] + }, + { + "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`))", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "accountId", + "columnName": "accountId", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "themeMode", + "columnName": "themeMode", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "mode", + "columnName": "mode", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "categoryId", + "columnName": "categoryId", + "affinity": "INTEGER", + "notNull": false + } + ], + "primaryKey": { + "columnNames": [ + "id" + ], + "autoGenerate": false + }, + "indices": [], + "foreignKeys": [] } ], "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, 'cf4429b4afde78dd86d282a0d0dfea47')" + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'a4ed702c70d8211c3310d6ea3600c097')" ] } } \ 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 05b8118a..d0ed3c4f 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 @@ -593,21 +593,6 @@ public class NotesDatabase extends AbstractNotesDatabase { return data; } - public void createOrUpdateNoteListWidgetData(@NonNull NoteListsWidgetData data) throws SQLException { - validateAccountId(data.getAccountId()); - final SQLiteDatabase db = getWritableDatabase(); - final ContentValues values = new ContentValues(5); - if (data.getMode() != MODE_DISPLAY_CATEGORY && data.getCategoryId() != null) { - throw new UnsupportedOperationException("Cannot create a widget with a categoryId when mode is not " + MODE_DISPLAY_CATEGORY); - } - values.put(key_id, data.getAppWidgetId()); - values.put(key_account_id, data.getAccountId()); - values.put(key_category_id, data.getCategoryId()); - values.put(key_theme_mode, data.getThemeMode()); - values.put(key_mode, data.getMode()); - db.replaceOrThrow(table_widget_note_list, null, values); - } - private static void validateAccountId(long accountId) { if (accountId < 1) { throw new IllegalArgumentException("accountId must be greater than 0"); 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 272b9820..185c2ae2 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,12 +1,18 @@ package it.niedermann.owncloud.notes.persistence.dao; +import androidx.annotation.NonNull; import androidx.room.Dao; +import androidx.room.Insert; import androidx.room.Query; +import it.niedermann.owncloud.notes.persistence.entity.WidgetNotesListEntity; + @Dao public interface WidgetNotesListDao { @Query("DELETE FROM WidgetNotesListEntity WHERE id = :appWidgetId") void removeNoteListWidget(int appWidgetId); + @Insert + void createOrUpdateNoteListWidgetData(@NonNull WidgetNotesListEntity data); } 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 f5cdf650..7a56cf2d 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 @@ -1,10 +1,82 @@ package it.niedermann.owncloud.notes.persistence.entity; +import androidx.annotation.IntRange; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.room.Entity; +import androidx.room.Ignore; import androidx.room.PrimaryKey; @Entity() public class WidgetNotesListEntity { + @Ignore + public static final int MODE_DISPLAY_ALL = 0; + @Ignore + public static final int MODE_DISPLAY_STARRED = 1; + @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; + } + + public void setCategoryId(@Nullable Long categoryId) { + this.categoryId = categoryId; + } + + @NonNull + @Override + public String toString() { + return "NoteListsWidgetData{" + + "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 80d03cfc..0679fb40 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 @@ -13,4 +13,12 @@ import it.niedermann.owncloud.notes.shared.model.DBStatus; public class WidgetSingleNoteEntity { @PrimaryKey private long id; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } } \ No newline at end of file diff --git a/app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidgetConfigurationActivity.java b/app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidgetConfigurationActivity.java index c694522b..ac6cfdd7 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidgetConfigurationActivity.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/widget/notelist/NoteListWidgetConfigurationActivity.java @@ -24,6 +24,7 @@ import it.niedermann.owncloud.notes.R; import it.niedermann.owncloud.notes.LockedActivity; import it.niedermann.owncloud.notes.main.MainActivity; import it.niedermann.owncloud.notes.persistence.NotesRoomDatabase; +import it.niedermann.owncloud.notes.persistence.entity.WidgetNotesListEntity; import it.niedermann.owncloud.notes.shared.model.LocalAccount; import it.niedermann.owncloud.notes.main.NavigationAdapter; import it.niedermann.owncloud.notes.main.NavigationAdapter.CategoryNavigationItem; @@ -90,9 +91,9 @@ public class NoteListWidgetConfigurationActivity extends LockedActivity { adapterCategories = new NavigationAdapter(this, new NavigationAdapter.ClickListener() { @Override public void onItemClick(NavigationAdapter.NavigationItem item) { - NoteListsWidgetData data = new NoteListsWidgetData(); + WidgetNotesListEntity data = new WidgetNotesListEntity(); - data.setAppWidgetId(appWidgetId); + data.setId(appWidgetId); if (itemRecent.equals(item)) { data.setMode(NoteListsWidgetData.MODE_DISPLAY_ALL); } else if (itemFavorites.equals(item)) { @@ -109,7 +110,7 @@ public class NoteListWidgetConfigurationActivity extends LockedActivity { data.setAccountId(localAccount.getId()); data.setThemeMode(NotesApplication.getAppTheme(getApplicationContext()).getModeId()); - sqliteOpenHelperDatabase.createOrUpdateNoteListWidgetData(data); + roomDatabase.getWidgetNotesListDao().createOrUpdateNoteListWidgetData(data); Intent updateIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, getApplicationContext(), NoteListWidget.class);