diff options
| author | 2023-03-08 15:38:33 -0500 | |
|---|---|---|
| committer | 2023-06-03 00:05:38 -0700 | |
| commit | 9e8ab499dcaac206cb83d7e64861e54755d30b82 (patch) | |
| tree | dc30dc3bc35ef5c44306d2395518e428fa90c625 /src/android | |
| parent | android: Convert GameDatabase to Kotlin (diff) | |
| download | yuzu-9e8ab499dcaac206cb83d7e64861e54755d30b82.tar.gz yuzu-9e8ab499dcaac206cb83d7e64861e54755d30b82.tar.xz yuzu-9e8ab499dcaac206cb83d7e64861e54755d30b82.zip | |
android: Convert GameProvider to Kotlin
Diffstat (limited to 'src/android')
| -rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/model/GameProvider.java | 138 | ||||
| -rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/model/GameProvider.kt | 127 |
2 files changed, 127 insertions, 138 deletions
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/GameProvider.java b/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/GameProvider.java deleted file mode 100644 index eff4b1e2d..000000000 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/GameProvider.java +++ /dev/null | |||
| @@ -1,138 +0,0 @@ | |||
| 1 | package org.yuzu.yuzu_emu.model; | ||
| 2 | |||
| 3 | import android.content.ContentProvider; | ||
| 4 | import android.content.ContentValues; | ||
| 5 | import android.database.Cursor; | ||
| 6 | import android.database.sqlite.SQLiteDatabase; | ||
| 7 | import android.net.Uri; | ||
| 8 | |||
| 9 | import androidx.annotation.NonNull; | ||
| 10 | |||
| 11 | import org.yuzu.yuzu_emu.BuildConfig; | ||
| 12 | import org.yuzu.yuzu_emu.utils.Log; | ||
| 13 | |||
| 14 | /** | ||
| 15 | * Provides an interface allowing Activities to interact with the SQLite database. | ||
| 16 | * CRUD methods in this class can be called by Activities using getContentResolver(). | ||
| 17 | */ | ||
| 18 | public final class GameProvider extends ContentProvider { | ||
| 19 | public static final String REFRESH_LIBRARY = "refresh"; | ||
| 20 | public static final String RESET_LIBRARY = "reset"; | ||
| 21 | |||
| 22 | public static final String AUTHORITY = "content://" + BuildConfig.APPLICATION_ID + ".provider"; | ||
| 23 | public static final Uri URI_FOLDER = | ||
| 24 | Uri.parse(AUTHORITY + "/" + GameDatabase.TABLE_NAME_FOLDERS + "/"); | ||
| 25 | public static final Uri URI_REFRESH = Uri.parse(AUTHORITY + "/" + REFRESH_LIBRARY + "/"); | ||
| 26 | public static final Uri URI_RESET = Uri.parse(AUTHORITY + "/" + RESET_LIBRARY + "/"); | ||
| 27 | |||
| 28 | public static final String MIME_TYPE_FOLDER = "vnd.android.cursor.item/vnd.dolphin.folder"; | ||
| 29 | public static final String MIME_TYPE_GAME = "vnd.android.cursor.item/vnd.dolphin.game"; | ||
| 30 | |||
| 31 | |||
| 32 | private GameDatabase mDbHelper; | ||
| 33 | |||
| 34 | @Override | ||
| 35 | public boolean onCreate() { | ||
| 36 | Log.info("[GameProvider] Creating Content Provider..."); | ||
| 37 | |||
| 38 | mDbHelper = new GameDatabase(getContext()); | ||
| 39 | |||
| 40 | return true; | ||
| 41 | } | ||
| 42 | |||
| 43 | @Override | ||
| 44 | public Cursor query(@NonNull Uri uri, String[] projection, String selection, | ||
| 45 | String[] selectionArgs, String sortOrder) { | ||
| 46 | Log.info("[GameProvider] Querying URI: " + uri); | ||
| 47 | |||
| 48 | SQLiteDatabase db = mDbHelper.getReadableDatabase(); | ||
| 49 | |||
| 50 | String table = uri.getLastPathSegment(); | ||
| 51 | |||
| 52 | if (table == null) { | ||
| 53 | Log.error("[GameProvider] Badly formatted URI: " + uri); | ||
| 54 | return null; | ||
| 55 | } | ||
| 56 | |||
| 57 | Cursor cursor = db.query(table, projection, selection, selectionArgs, null, null, sortOrder); | ||
| 58 | cursor.setNotificationUri(getContext().getContentResolver(), uri); | ||
| 59 | |||
| 60 | return cursor; | ||
| 61 | } | ||
| 62 | |||
| 63 | @Override | ||
| 64 | public String getType(@NonNull Uri uri) { | ||
| 65 | Log.verbose("[GameProvider] Getting MIME type for URI: " + uri); | ||
| 66 | String lastSegment = uri.getLastPathSegment(); | ||
| 67 | |||
| 68 | if (lastSegment == null) { | ||
| 69 | Log.error("[GameProvider] Badly formatted URI: " + uri); | ||
| 70 | return null; | ||
| 71 | } | ||
| 72 | |||
| 73 | if (lastSegment.equals(GameDatabase.TABLE_NAME_FOLDERS)) { | ||
| 74 | return MIME_TYPE_FOLDER; | ||
| 75 | } else if (lastSegment.equals(GameDatabase.TABLE_NAME_GAMES)) { | ||
| 76 | return MIME_TYPE_GAME; | ||
| 77 | } | ||
| 78 | |||
| 79 | Log.error("[GameProvider] Unknown MIME type for URI: " + uri); | ||
| 80 | return null; | ||
| 81 | } | ||
| 82 | |||
| 83 | @Override | ||
| 84 | public Uri insert(@NonNull Uri uri, ContentValues values) { | ||
| 85 | Log.info("[GameProvider] Inserting row at URI: " + uri); | ||
| 86 | |||
| 87 | SQLiteDatabase database = mDbHelper.getWritableDatabase(); | ||
| 88 | String table = uri.getLastPathSegment(); | ||
| 89 | |||
| 90 | if (table != null) { | ||
| 91 | if (table.equals(RESET_LIBRARY)) { | ||
| 92 | mDbHelper.resetDatabase(database); | ||
| 93 | return uri; | ||
| 94 | } | ||
| 95 | if (table.equals(REFRESH_LIBRARY)) { | ||
| 96 | Log.info( | ||
| 97 | "[GameProvider] URI specified table REFRESH_LIBRARY. No insertion necessary; refreshing library contents..."); | ||
| 98 | mDbHelper.scanLibrary(database); | ||
| 99 | return uri; | ||
| 100 | } | ||
| 101 | |||
| 102 | long id = database.insertWithOnConflict(table, null, values, SQLiteDatabase.CONFLICT_IGNORE); | ||
| 103 | |||
| 104 | // If insertion was successful... | ||
| 105 | if (id > 0) { | ||
| 106 | // If we just added a folder, add its contents to the game list. | ||
| 107 | if (table.equals(GameDatabase.TABLE_NAME_FOLDERS)) { | ||
| 108 | mDbHelper.scanLibrary(database); | ||
| 109 | } | ||
| 110 | |||
| 111 | // Notify the UI that its contents should be refreshed. | ||
| 112 | getContext().getContentResolver().notifyChange(uri, null); | ||
| 113 | uri = Uri.withAppendedPath(uri, Long.toString(id)); | ||
| 114 | } else { | ||
| 115 | Log.error("[GameProvider] Row already exists: " + uri + " id: " + id); | ||
| 116 | } | ||
| 117 | } else { | ||
| 118 | Log.error("[GameProvider] Badly formatted URI: " + uri); | ||
| 119 | } | ||
| 120 | |||
| 121 | database.close(); | ||
| 122 | |||
| 123 | return uri; | ||
| 124 | } | ||
| 125 | |||
| 126 | @Override | ||
| 127 | public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) { | ||
| 128 | Log.error("[GameProvider] Delete operations unsupported. URI: " + uri); | ||
| 129 | return 0; | ||
| 130 | } | ||
| 131 | |||
| 132 | @Override | ||
| 133 | public int update(@NonNull Uri uri, ContentValues values, String selection, | ||
| 134 | String[] selectionArgs) { | ||
| 135 | Log.error("[GameProvider] Update operations unsupported. URI: " + uri); | ||
| 136 | return 0; | ||
| 137 | } | ||
| 138 | } | ||
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/GameProvider.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/GameProvider.kt new file mode 100644 index 000000000..c9818172d --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/GameProvider.kt | |||
| @@ -0,0 +1,127 @@ | |||
| 1 | package org.yuzu.yuzu_emu.model | ||
| 2 | |||
| 3 | import android.content.ContentProvider | ||
| 4 | import android.content.ContentValues | ||
| 5 | import android.database.Cursor | ||
| 6 | import android.database.sqlite.SQLiteDatabase | ||
| 7 | import android.net.Uri | ||
| 8 | import org.yuzu.yuzu_emu.BuildConfig | ||
| 9 | import org.yuzu.yuzu_emu.utils.Log | ||
| 10 | |||
| 11 | /** | ||
| 12 | * Provides an interface allowing Activities to interact with the SQLite database. | ||
| 13 | * CRUD methods in this class can be called by Activities using getContentResolver(). | ||
| 14 | */ | ||
| 15 | class GameProvider : ContentProvider() { | ||
| 16 | private var mDbHelper: GameDatabase? = null | ||
| 17 | override fun onCreate(): Boolean { | ||
| 18 | Log.info("[GameProvider] Creating Content Provider...") | ||
| 19 | mDbHelper = GameDatabase(context!!) | ||
| 20 | return true | ||
| 21 | } | ||
| 22 | |||
| 23 | override fun query( | ||
| 24 | uri: Uri, | ||
| 25 | projection: Array<String>?, | ||
| 26 | selection: String?, | ||
| 27 | selectionArgs: Array<String>?, | ||
| 28 | sortOrder: String? | ||
| 29 | ): Cursor? { | ||
| 30 | Log.info("[GameProvider] Querying URI: $uri") | ||
| 31 | val db = mDbHelper!!.readableDatabase | ||
| 32 | val table = uri.lastPathSegment | ||
| 33 | if (table == null) { | ||
| 34 | Log.error("[GameProvider] Badly formatted URI: $uri") | ||
| 35 | return null | ||
| 36 | } | ||
| 37 | val cursor = db.query(table, projection, selection, selectionArgs, null, null, sortOrder) | ||
| 38 | cursor.setNotificationUri(context!!.contentResolver, uri) | ||
| 39 | return cursor | ||
| 40 | } | ||
| 41 | |||
| 42 | override fun getType(uri: Uri): String? { | ||
| 43 | Log.verbose("[GameProvider] Getting MIME type for URI: $uri") | ||
| 44 | val lastSegment = uri.lastPathSegment | ||
| 45 | if (lastSegment == null) { | ||
| 46 | Log.error("[GameProvider] Badly formatted URI: $uri") | ||
| 47 | return null | ||
| 48 | } | ||
| 49 | if (lastSegment == GameDatabase.TABLE_NAME_FOLDERS) { | ||
| 50 | return MIME_TYPE_FOLDER | ||
| 51 | } else if (lastSegment == GameDatabase.TABLE_NAME_GAMES) { | ||
| 52 | return MIME_TYPE_GAME | ||
| 53 | } | ||
| 54 | Log.error("[GameProvider] Unknown MIME type for URI: $uri") | ||
| 55 | return null | ||
| 56 | } | ||
| 57 | |||
| 58 | override fun insert(uri: Uri, values: ContentValues?): Uri { | ||
| 59 | var realUri = uri | ||
| 60 | Log.info("[GameProvider] Inserting row at URI: $realUri") | ||
| 61 | val database = mDbHelper!!.writableDatabase | ||
| 62 | val table = realUri.lastPathSegment | ||
| 63 | if (table != null) { | ||
| 64 | if (table == RESET_LIBRARY) { | ||
| 65 | mDbHelper!!.resetDatabase(database) | ||
| 66 | return realUri | ||
| 67 | } | ||
| 68 | if (table == REFRESH_LIBRARY) { | ||
| 69 | Log.info( | ||
| 70 | "[GameProvider] URI specified table REFRESH_LIBRARY. No insertion necessary; refreshing library contents..." | ||
| 71 | ) | ||
| 72 | mDbHelper!!.scanLibrary(database) | ||
| 73 | return realUri | ||
| 74 | } | ||
| 75 | val id = | ||
| 76 | database.insertWithOnConflict(table, null, values, SQLiteDatabase.CONFLICT_IGNORE) | ||
| 77 | |||
| 78 | // If insertion was successful... | ||
| 79 | if (id > 0) { | ||
| 80 | // If we just added a folder, add its contents to the game list. | ||
| 81 | if (table == GameDatabase.TABLE_NAME_FOLDERS) { | ||
| 82 | mDbHelper!!.scanLibrary(database) | ||
| 83 | } | ||
| 84 | |||
| 85 | // Notify the UI that its contents should be refreshed. | ||
| 86 | context!!.contentResolver.notifyChange(realUri, null) | ||
| 87 | realUri = Uri.withAppendedPath(realUri, id.toString()) | ||
| 88 | } else { | ||
| 89 | Log.error("[GameProvider] Row already exists: $realUri id: $id") | ||
| 90 | } | ||
| 91 | } else { | ||
| 92 | Log.error("[GameProvider] Badly formatted URI: $realUri") | ||
| 93 | } | ||
| 94 | database.close() | ||
| 95 | return realUri | ||
| 96 | } | ||
| 97 | |||
| 98 | override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int { | ||
| 99 | Log.error("[GameProvider] Delete operations unsupported. URI: $uri") | ||
| 100 | return 0 | ||
| 101 | } | ||
| 102 | |||
| 103 | override fun update( | ||
| 104 | uri: Uri, values: ContentValues?, selection: String?, | ||
| 105 | selectionArgs: Array<String>? | ||
| 106 | ): Int { | ||
| 107 | Log.error("[GameProvider] Update operations unsupported. URI: $uri") | ||
| 108 | return 0 | ||
| 109 | } | ||
| 110 | |||
| 111 | companion object { | ||
| 112 | const val REFRESH_LIBRARY = "refresh" | ||
| 113 | const val RESET_LIBRARY = "reset" | ||
| 114 | private const val AUTHORITY = "content://${BuildConfig.APPLICATION_ID}.provider" | ||
| 115 | |||
| 116 | @JvmField | ||
| 117 | val URI_FOLDER: Uri = Uri.parse("$AUTHORITY/${GameDatabase.TABLE_NAME_FOLDERS}/") | ||
| 118 | |||
| 119 | @JvmField | ||
| 120 | val URI_REFRESH: Uri = Uri.parse("$AUTHORITY/$REFRESH_LIBRARY/") | ||
| 121 | |||
| 122 | @JvmField | ||
| 123 | val URI_RESET: Uri = Uri.parse("$AUTHORITY/$RESET_LIBRARY/") | ||
| 124 | const val MIME_TYPE_FOLDER = "vnd.android.cursor.item/vnd.yuzu.folder" | ||
| 125 | const val MIME_TYPE_GAME = "vnd.android.cursor.item/vnd.yuzu.game" | ||
| 126 | } | ||
| 127 | } | ||