#831 Migrate from SQLiteOpenHelper to Room

Widgets
This commit is contained in:
Stefan Niedermann 2020-10-05 16:31:11 +02:00
parent 21ba77dc68
commit 8feb06f93b
6 changed files with 156 additions and 20 deletions

View file

@ -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')"
]
}
}

View file

@ -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");

View file

@ -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);
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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);