8
8
import android .net .Uri ;
9
9
import android .text .TextUtils ;
10
10
import android .util .Log ;
11
+
11
12
import androidx .annotation .NonNull ;
12
13
import androidx .annotation .Nullable ;
13
14
import androidx .room .Room ;
14
15
import androidx .sqlite .db .SimpleSQLiteQuery ;
15
16
import androidx .sqlite .db .SupportSQLiteQuery ;
16
17
import androidx .sqlite .db .SupportSQLiteQueryBuilder ;
18
+
17
19
import com .catherine .materialdesignapp .jetpack .StringListConverter ;
18
20
import com .catherine .materialdesignapp .jetpack .daos .AlbumDao ;
19
21
import com .catherine .materialdesignapp .jetpack .databases .AlbumRoomDatabase ;
@@ -28,11 +30,11 @@ public class AlbumsProvider extends ContentProvider {
28
30
29
31
30
32
private static final int ALBUMS = 1 ;
31
- private static final int ALBUMS_ID = 2 ;
33
+ private static final int ALBUMS_TITLE = 2 ;
32
34
33
35
static {
34
36
sURIMatcher .addURI (AUTHORITY , "albums" , ALBUMS );
35
- sURIMatcher .addURI (AUTHORITY , "albums/#" , ALBUMS_ID );
37
+ sURIMatcher .addURI (AUTHORITY , "albums/#" , ALBUMS_TITLE );
36
38
}
37
39
38
40
private AlbumDao albumDao ;
@@ -63,9 +65,9 @@ public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable S
63
65
.orderBy (sortOrder )
64
66
.create ();
65
67
return albumDao .select (mQuery );
66
- case ALBUMS_ID :
67
- if (!TextUtils .isEmpty (selection ) && !selection .trim ().contains ("_id =" )) {
68
- String mSelection = selection + " AND _id = ?" ;
68
+ case ALBUMS_TITLE :
69
+ if (!TextUtils .isEmpty (selection ) && !selection .trim ().contains ("title =" )) {
70
+ String mSelection = selection + " AND title = ?" ;
69
71
String [] mSelectionArgs = new String [selectionArgs .length + 1 ];
70
72
System .arraycopy (selectionArgs , 0 , mSelectionArgs , 0 , selectionArgs .length );
71
73
mSelectionArgs [mSelectionArgs .length - 1 ] = ContentUris .parseId (uri ) + "" ;
@@ -95,7 +97,7 @@ public String getType(@NonNull Uri uri) {
95
97
switch (sURIMatcher .match (uri )) {
96
98
case ALBUMS :
97
99
return "vnd.android.cursor.dir/" + AUTHORITY + "." + "albums" ;
98
- case ALBUMS_ID :
100
+ case ALBUMS_TITLE :
99
101
return "vnd.android.cursor.item/" + AUTHORITY + "." + "albums/#" ;
100
102
default :
101
103
throw new IllegalArgumentException ("Unknown URI: " + uri );
@@ -131,8 +133,8 @@ public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
131
133
long id = albumDao .insert (album );
132
134
getContext ().getContentResolver ().notifyChange (uri , null );
133
135
return ContentUris .withAppendedId (uri , id );
134
- case ALBUMS_ID :
135
- throw new IllegalArgumentException ("Invalid URI, cannot insert with _id : " + uri );
136
+ case ALBUMS_TITLE :
137
+ throw new IllegalArgumentException ("Invalid URI, cannot insert with title : " + uri );
136
138
default :
137
139
throw new IllegalArgumentException ("Unknown URI: " + uri );
138
140
}
@@ -144,29 +146,29 @@ public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String
144
146
SimpleSQLiteQuery query ;
145
147
switch (sURIMatcher .match (uri )) {
146
148
case ALBUMS :
147
- if (TextUtils .isEmpty (selection ) || selection .trim ().contains ("_id =" ))
148
- throw new IllegalArgumentException ("Invalid URI, cannot delete without _id : " + uri );
149
+ if (TextUtils .isEmpty (selection ) || selection .trim ().contains ("title =" ))
150
+ throw new IllegalArgumentException ("Invalid URI, cannot delete without title : " + uri );
149
151
150
152
query = new SimpleSQLiteQuery ("DELETE FROM album_table WHERE " + selection , selectionArgs );
151
153
count = albumDao .deleteByRawQuery (query );
152
154
getContext ().getContentResolver ().notifyChange (uri , null );
153
155
return count ;
154
- case ALBUMS_ID :
156
+ case ALBUMS_TITLE :
155
157
if (getContext () == null )
156
158
return -1 ;
157
159
158
160
String mSelection ;
159
161
String [] mSelectionArgs ;
160
162
if (TextUtils .isEmpty (selection )) {
161
- mSelection = "DELETE FROM album_table WHERE _id = ?" ;
163
+ mSelection = "DELETE FROM album_table WHERE title = ?" ;
162
164
mSelectionArgs = new String []{ContentUris .parseId (uri ) + "" };
163
- } else if (!selection .trim ().contains ("_id =" )) {
164
- mSelection = "DELETE FROM album_table WHERE " + selection + " AND _id = ?" ;
165
+ } else if (!selection .trim ().contains ("title =" )) {
166
+ mSelection = "DELETE FROM album_table WHERE " + selection + " AND title = ?" ;
165
167
mSelectionArgs = new String [selectionArgs .length + 1 ];
166
168
System .arraycopy (selectionArgs , 0 , mSelectionArgs , 0 , selectionArgs .length );
167
169
mSelectionArgs [mSelectionArgs .length - 1 ] = ContentUris .parseId (uri ) + "" ;
168
170
} else {
169
- throw new IllegalArgumentException ("Invalid URI, cannot delete with duplicated _id : " + uri );
171
+ throw new IllegalArgumentException ("Invalid URI, cannot delete with duplicated title : " + uri );
170
172
}
171
173
172
174
query = new SimpleSQLiteQuery (mSelection , mSelectionArgs );
@@ -184,7 +186,7 @@ public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable St
184
186
if (getContext () == null )
185
187
return -1 ;
186
188
187
- if (sURIMatcher .match (uri ) != ALBUMS && sURIMatcher .match (uri ) != ALBUMS_ID )
189
+ if (sURIMatcher .match (uri ) != ALBUMS && sURIMatcher .match (uri ) != ALBUMS_TITLE )
188
190
throw new IllegalArgumentException ("Invalid URI: " + uri );
189
191
190
192
if (values == null )
@@ -222,18 +224,18 @@ public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable St
222
224
mQuery .append (selection );
223
225
}
224
226
} else {
225
- // sURIMatcher.match(uri) == ALBUMS_ID
227
+ // sURIMatcher.match(uri) == ALBUMS_TITLE
226
228
if (TextUtils .isEmpty (selection )) {
227
- mQuery .append (" WHERE _id = ?" );
229
+ mQuery .append (" WHERE title = ?" );
228
230
mSelectionArgs = new String []{ContentUris .parseId (uri ) + "" };
229
- } else if (!selection .trim ().contains ("_id =" )) {
231
+ } else if (!selection .trim ().contains ("title =" )) {
230
232
mQuery .append (" WHERE " );
231
233
mQuery .append (selection );
232
234
mSelectionArgs = new String [selectionArgs .length + 1 ];
233
235
System .arraycopy (selectionArgs , 0 , mSelectionArgs , 0 , selectionArgs .length );
234
236
mSelectionArgs [mSelectionArgs .length - 1 ] = ContentUris .parseId (uri ) + "" ;
235
237
} else {
236
- throw new IllegalArgumentException ("Invalid URI, cannot update with duplicated _id : " + uri );
238
+ throw new IllegalArgumentException ("Invalid URI, cannot update with duplicated title : " + uri );
237
239
}
238
240
}
239
241
0 commit comments