|
1 | 1 | package com.catherine.materialdesignapp.providers;
|
2 | 2 |
|
3 | 3 | import android.content.ContentProvider;
|
| 4 | +import android.content.ContentUris; |
4 | 5 | import android.content.ContentValues;
|
5 | 6 | import android.content.UriMatcher;
|
6 | 7 | import android.database.Cursor;
|
7 | 8 | import android.net.Uri;
|
8 | 9 | import android.text.TextUtils;
|
| 10 | +import android.util.Log; |
9 | 11 | import androidx.annotation.NonNull;
|
10 | 12 | import androidx.annotation.Nullable;
|
11 | 13 | import androidx.room.Room;
|
| 14 | +import androidx.sqlite.db.SimpleSQLiteQuery; |
| 15 | +import androidx.sqlite.db.SupportSQLiteQuery; |
| 16 | +import androidx.sqlite.db.SupportSQLiteQueryBuilder; |
| 17 | +import com.catherine.materialdesignapp.jetpack.StringListConverter; |
| 18 | +import com.catherine.materialdesignapp.jetpack.daos.AlbumDao; |
12 | 19 | import com.catherine.materialdesignapp.jetpack.databases.AlbumRoomDatabase;
|
| 20 | +import com.catherine.materialdesignapp.jetpack.entities.Album; |
| 21 | + |
| 22 | +import java.util.Arrays; |
13 | 23 |
|
14 | 24 | public class AlbumsProvider extends ContentProvider {
|
15 | 25 | public final static String TAG = AlbumsProvider.class.getSimpleName();
|
16 | 26 | private final static String AUTHORITY = "com.catherine.materialdesignapp.providers.AlbumsProvider";
|
17 | 27 | private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
18 | 28 |
|
19 | 29 |
|
| 30 | + private static final int ALBUMS = 1; |
| 31 | + private static final int ALBUMS_ID = 2; |
| 32 | + |
20 | 33 | static {
|
21 |
| - sURIMatcher.addURI(AUTHORITY, "albums", 0); |
22 |
| - sURIMatcher.addURI(AUTHORITY, "albums/#", 1); |
| 34 | + sURIMatcher.addURI(AUTHORITY, "albums", ALBUMS); |
| 35 | + sURIMatcher.addURI(AUTHORITY, "albums/#", ALBUMS_ID); |
23 | 36 | }
|
24 | 37 |
|
25 |
| - private AlbumRoomDatabase albumRoomDatabase; |
| 38 | + private AlbumDao albumDao; |
26 | 39 |
|
27 | 40 |
|
28 | 41 | @Override
|
29 | 42 | public boolean onCreate() {
|
30 |
| - albumRoomDatabase = Room.databaseBuilder(getContext(), |
| 43 | + if (getContext() == null) |
| 44 | + return false; |
| 45 | + |
| 46 | + AlbumRoomDatabase albumRoomDatabase = Room.databaseBuilder(getContext(), |
31 | 47 | AlbumRoomDatabase.class, "album_database")
|
32 |
| - // Wipes and rebuilds instead of migrating |
33 |
| - // if no Migration object. |
34 |
| - // Migration is not part of this practical. |
35 | 48 | .fallbackToDestructiveMigration()
|
36 | 49 | .build();
|
| 50 | + albumDao = albumRoomDatabase.albumDao(); |
37 | 51 | return true;
|
38 | 52 | }
|
39 | 53 |
|
40 | 54 | @Override
|
41 | 55 | public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
|
42 |
| - String query = "SELECT title FROM album_table"; |
43 |
| - String[] args = null; |
44 |
| - if (!TextUtils.isEmpty(sortOrder)) { |
45 |
| - query += " ORDER BY ? ASC"; |
46 |
| - args = new String[]{sortOrder}; |
| 56 | + SupportSQLiteQuery mQuery; |
| 57 | + switch (sURIMatcher.match(uri)) { |
| 58 | + case ALBUMS: |
| 59 | + mQuery = SupportSQLiteQueryBuilder |
| 60 | + .builder("album_table") |
| 61 | + .columns(projection) |
| 62 | + .selection(selection, selectionArgs) |
| 63 | + .orderBy(sortOrder) |
| 64 | + .create(); |
| 65 | + return albumDao.select(mQuery); |
| 66 | + case ALBUMS_ID: |
| 67 | + if (!TextUtils.isEmpty(selection) && !selection.trim().contains("_id=")) { |
| 68 | + String mSelection = selection + " AND _id = ?"; |
| 69 | + String[] mSelectionArgs = new String[selectionArgs.length + 1]; |
| 70 | + System.arraycopy(selectionArgs, 0, mSelectionArgs, 0, selectionArgs.length); |
| 71 | + mSelectionArgs[mSelectionArgs.length - 1] = ContentUris.parseId(uri) + ""; |
| 72 | + mQuery = SupportSQLiteQueryBuilder |
| 73 | + .builder("album_table") |
| 74 | + .columns(projection) |
| 75 | + .selection(mSelection, mSelectionArgs) |
| 76 | + .orderBy(sortOrder) |
| 77 | + .create(); |
| 78 | + } else { |
| 79 | + mQuery = SupportSQLiteQueryBuilder |
| 80 | + .builder("album_table") |
| 81 | + .columns(projection) |
| 82 | + .selection(selection, selectionArgs) |
| 83 | + .orderBy(sortOrder) |
| 84 | + .create(); |
| 85 | + } |
| 86 | + return albumDao.select(mQuery); |
| 87 | + default: |
| 88 | + throw new IllegalArgumentException("Unknown URI: " + uri); |
47 | 89 | }
|
48 |
| - return albumRoomDatabase.query(query, args); |
49 | 90 | }
|
50 | 91 |
|
51 | 92 |
|
52 | 93 | @Override
|
53 | 94 | public String getType(@NonNull Uri uri) {
|
54 |
| - return null; |
| 95 | + switch (sURIMatcher.match(uri)) { |
| 96 | + case ALBUMS: |
| 97 | + return "vnd.android.cursor.dir/" + AUTHORITY + "." + "albums"; |
| 98 | + case ALBUMS_ID: |
| 99 | + return "vnd.android.cursor.item/" + AUTHORITY + "." + "albums/#"; |
| 100 | + default: |
| 101 | + throw new IllegalArgumentException("Unknown URI: " + uri); |
| 102 | + } |
55 | 103 | }
|
56 | 104 |
|
57 | 105 | @Override
|
58 | 106 | public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
|
59 |
| - return null; |
| 107 | + if (getContext() == null || values == null) |
| 108 | + return null; |
| 109 | + |
| 110 | + switch (sURIMatcher.match(uri)) { |
| 111 | + case ALBUMS: |
| 112 | + Album album = new Album(); |
| 113 | + if (values.containsKey("title")) |
| 114 | + album.setTitle(values.getAsString("title")); |
| 115 | + |
| 116 | + if (values.containsKey("artist")) |
| 117 | + album.setArtist(values.getAsString("artist")); |
| 118 | + |
| 119 | + if (values.containsKey("image")) |
| 120 | + album.setImage(values.getAsString("image")); |
| 121 | + |
| 122 | + if (values.containsKey("thumbnail_image")) |
| 123 | + album.setThumbnail_image(values.getAsString("thumbnail_image")); |
| 124 | + |
| 125 | + if (values.containsKey("url")) |
| 126 | + album.setUrl(values.getAsString("url")); |
| 127 | + |
| 128 | + if (values.containsKey("songs")) { |
| 129 | + album.setSongs(StringListConverter.fromString("songs")); |
| 130 | + } |
| 131 | + long id = albumDao.insert(album); |
| 132 | + getContext().getContentResolver().notifyChange(uri, null); |
| 133 | + return ContentUris.withAppendedId(uri, id); |
| 134 | + case ALBUMS_ID: |
| 135 | + throw new IllegalArgumentException("Invalid URI, cannot insert with _id: " + uri); |
| 136 | + default: |
| 137 | + throw new IllegalArgumentException("Unknown URI: " + uri); |
| 138 | + } |
60 | 139 | }
|
61 | 140 |
|
62 | 141 | @Override
|
63 | 142 | public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
|
64 |
| - return 0; |
| 143 | + int count; |
| 144 | + SimpleSQLiteQuery query; |
| 145 | + switch (sURIMatcher.match(uri)) { |
| 146 | + case ALBUMS: |
| 147 | + if (TextUtils.isEmpty(selection) || selection.trim().contains("_id=")) |
| 148 | + throw new IllegalArgumentException("Invalid URI, cannot delete without _id: " + uri); |
| 149 | + |
| 150 | + query = new SimpleSQLiteQuery("DELETE FROM album_table WHERE " + selection, selectionArgs); |
| 151 | + count = albumDao.deleteByRawQuery(query); |
| 152 | + getContext().getContentResolver().notifyChange(uri, null); |
| 153 | + return count; |
| 154 | + case ALBUMS_ID: |
| 155 | + if (getContext() == null) |
| 156 | + return -1; |
| 157 | + |
| 158 | + String mSelection; |
| 159 | + String[] mSelectionArgs; |
| 160 | + if (TextUtils.isEmpty(selection)) { |
| 161 | + mSelection = "DELETE FROM album_table WHERE _id = ?"; |
| 162 | + mSelectionArgs = new String[]{ContentUris.parseId(uri) + ""}; |
| 163 | + } else if (!selection.trim().contains("_id=")) { |
| 164 | + mSelection = "DELETE FROM album_table WHERE " + selection + " AND _id = ?"; |
| 165 | + mSelectionArgs = new String[selectionArgs.length + 1]; |
| 166 | + System.arraycopy(selectionArgs, 0, mSelectionArgs, 0, selectionArgs.length); |
| 167 | + mSelectionArgs[mSelectionArgs.length - 1] = ContentUris.parseId(uri) + ""; |
| 168 | + } else { |
| 169 | + throw new IllegalArgumentException("Invalid URI, cannot delete with duplicated _id: " + uri); |
| 170 | + } |
| 171 | + |
| 172 | + query = new SimpleSQLiteQuery(mSelection, mSelectionArgs); |
| 173 | + count = albumDao.deleteByRawQuery(query); |
| 174 | + getContext().getContentResolver().notifyChange(uri, null); |
| 175 | + return count; |
| 176 | + default: |
| 177 | + throw new IllegalArgumentException("Unknown URI: " + uri); |
| 178 | + } |
65 | 179 | }
|
66 | 180 |
|
67 | 181 | @Override
|
68 |
| - public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) { |
69 |
| - return 0; |
| 182 | + public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String |
| 183 | + selection, @Nullable String[] selectionArgs) { |
| 184 | + if (getContext() == null) |
| 185 | + return -1; |
| 186 | + |
| 187 | + if (sURIMatcher.match(uri) != ALBUMS && sURIMatcher.match(uri) != ALBUMS_ID) |
| 188 | + throw new IllegalArgumentException("Invalid URI: " + uri); |
| 189 | + |
| 190 | + if (values == null) |
| 191 | + throw new IllegalArgumentException("Invalid URI, cannot update without values: " + uri); |
| 192 | + |
| 193 | + String header = "UPDATE album_table SET "; |
| 194 | + StringBuilder mQuery = new StringBuilder(header); |
| 195 | + if (values.containsKey("title")) |
| 196 | + mQuery.append(appendQuery(values, "title")); |
| 197 | + |
| 198 | + if (values.containsKey("artist")) |
| 199 | + mQuery.append(appendQuery(values, "artist")); |
| 200 | + |
| 201 | + if (values.containsKey("image")) |
| 202 | + mQuery.append(appendQuery(values, "image")); |
| 203 | + |
| 204 | + if (values.containsKey("thumbnail_image")) |
| 205 | + mQuery.append(appendQuery(values, "thumbnail_image")); |
| 206 | + |
| 207 | + if (values.containsKey("url")) |
| 208 | + mQuery.append(appendQuery(values, "url")); |
| 209 | + |
| 210 | + if (values.containsKey("songs")) |
| 211 | + mQuery.append(appendQuery(values, "songs")); |
| 212 | + |
| 213 | + if (header.length() == mQuery.length()) |
| 214 | + throw new IllegalArgumentException("Invalid URI, cannot update with incorrect values: " + uri); |
| 215 | + |
| 216 | + mQuery.delete(mQuery.length() - " AND ".length(), mQuery.length()); |
| 217 | + |
| 218 | + String[] mSelectionArgs = selectionArgs; |
| 219 | + if (sURIMatcher.match(uri) == ALBUMS) { |
| 220 | + if (!TextUtils.isEmpty(selection)) { |
| 221 | + mQuery.append(" WHERE "); |
| 222 | + mQuery.append(selection); |
| 223 | + } |
| 224 | + } else { |
| 225 | + // sURIMatcher.match(uri) == ALBUMS_ID |
| 226 | + if (TextUtils.isEmpty(selection)) { |
| 227 | + mQuery.append(" WHERE _id = ?"); |
| 228 | + mSelectionArgs = new String[]{ContentUris.parseId(uri) + ""}; |
| 229 | + } else if (!selection.trim().contains("_id=")) { |
| 230 | + mQuery.append(" WHERE "); |
| 231 | + mQuery.append(selection); |
| 232 | + mSelectionArgs = new String[selectionArgs.length + 1]; |
| 233 | + System.arraycopy(selectionArgs, 0, mSelectionArgs, 0, selectionArgs.length); |
| 234 | + mSelectionArgs[mSelectionArgs.length - 1] = ContentUris.parseId(uri) + ""; |
| 235 | + } else { |
| 236 | + throw new IllegalArgumentException("Invalid URI, cannot update with duplicated _id: " + uri); |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + Log.d(TAG, "query: " + mQuery.toString()); |
| 241 | + Log.d(TAG, "args: " + Arrays.toString(mSelectionArgs)); |
| 242 | + SimpleSQLiteQuery query = new SimpleSQLiteQuery(mQuery.toString(), mSelectionArgs); |
| 243 | + Cursor cursor = albumDao.updateByRawQuery(query); |
| 244 | + getContext().getContentResolver().notifyChange(uri, null); |
| 245 | + return cursor.getCount(); |
| 246 | + } |
| 247 | + |
| 248 | + private String appendQuery(ContentValues values, String str) { |
| 249 | + StringBuilder sb = new StringBuilder(); |
| 250 | + if (values.containsKey(str)) { |
| 251 | + sb.append(str); |
| 252 | + sb.append(" = "); |
| 253 | + sb.append(values.getAsString(str)); |
| 254 | + sb.append(" AND "); |
| 255 | + } |
| 256 | + return sb.toString(); |
70 | 257 | }
|
71 | 258 |
|
72 | 259 | }
|
0 commit comments