22
22
*/
23
23
public class SimpleFinchVideoContentProvider extends ContentProvider {
24
24
public static final String SIMPLE_VIDEO = "simple_video" ;
25
+ public static final String VIDEO_TABLE_NAME = "videos" ;
25
26
26
27
private static final int VIDEOS = 1 ;
27
28
private static final int VIDEO_ID = 2 ;
28
-
29
29
private static UriMatcher sUriMatcher ;
30
-
31
- private static HashMap <String , String > sVideosProjectionMap ;
32
-
33
30
static {
34
31
sUriMatcher = new UriMatcher (UriMatcher .NO_MATCH );
35
32
sUriMatcher .addURI (FinchVideo .SIMPLE_AUTHORITY , FinchVideo .SimpleVideos .VIDEO_NAME ,
36
- VIDEOS );
33
+ VIDEOS );
37
34
// use of the hash character indicates matching of an id
38
35
sUriMatcher .addURI (FinchVideo .SIMPLE_AUTHORITY ,
39
- FinchVideo .SimpleVideos .VIDEO_NAME + "/#" , VIDEO_ID );
36
+ FinchVideo .SimpleVideos .VIDEO_NAME + "/#" , VIDEO_ID );
37
+ }
40
38
39
+ private static HashMap <String , String > sVideosProjectionMap ;
40
+ static {
41
41
// example projection map, not actually used in this application
42
42
sVideosProjectionMap = new HashMap <String , String >();
43
- sVideosProjectionMap .put (BaseColumns ._ID ,
44
- BaseColumns ._ID );
45
- sVideosProjectionMap .put (FinchVideo .Videos .TITLE ,
46
- FinchVideo .Videos .TITLE );
47
- sVideosProjectionMap .put (FinchVideo .Videos .VIDEO ,
48
- FinchVideo .Videos .VIDEO );
49
- sVideosProjectionMap .put (FinchVideo .Videos .DESCRIPTION ,
50
- FinchVideo .Videos .DESCRIPTION );
43
+ sVideosProjectionMap .put (BaseColumns ._ID , BaseColumns ._ID );
44
+ sVideosProjectionMap .put (FinchVideo .Videos .TITLE , FinchVideo .Videos .TITLE );
45
+ sVideosProjectionMap .put (FinchVideo .Videos .VIDEO , FinchVideo .Videos .VIDEO );
46
+ sVideosProjectionMap .put (FinchVideo .Videos .DESCRIPTION , FinchVideo .Videos .DESCRIPTION );
51
47
}
52
48
53
- public static final String VIDEO_TABLE_NAME = "videos" ;
54
-
55
- public static final String DATABASE_NAME = SIMPLE_VIDEO + ".db" ;
56
- static int DATABASE_VERSION = 2 ;
57
-
58
49
private static class SimpleVideoDbHelper extends SQLiteOpenHelper {
59
- private SimpleVideoDbHelper (Context context , String name ,
60
- SQLiteDatabase .CursorFactory factory )
61
- {
62
- super (context , name , factory , DATABASE_VERSION );
50
+ private static final String DATABASE_NAME = SIMPLE_VIDEO + ".db" ;
51
+ private static int DATABASE_VERSION = 2 ;
52
+
53
+ SimpleVideoDbHelper (Context context ) {
54
+ super (context , DATABASE_NAME , null , DATABASE_VERSION );
63
55
}
64
56
65
57
@ Override
66
58
public void onCreate (SQLiteDatabase sqLiteDatabase ) {
67
59
createTable (sqLiteDatabase );
68
60
}
69
61
62
+ @ Override
63
+ public void onUpgrade (SQLiteDatabase sqLiteDatabase ,
64
+ int oldv , int newv )
65
+ {
66
+ sqLiteDatabase .execSQL ("DROP TABLE IF EXISTS " +
67
+ VIDEO_TABLE_NAME + ";" );
68
+ createTable (sqLiteDatabase );
69
+ }
70
+
70
71
private void createTable (SQLiteDatabase sqLiteDatabase ) {
71
72
String qs = "CREATE TABLE " + VIDEO_TABLE_NAME + " (" +
72
73
BaseColumns ._ID +
@@ -76,41 +77,30 @@ private void createTable(SQLiteDatabase sqLiteDatabase) {
76
77
FinchVideo .SimpleVideos .URI_NAME + " TEXT);" ;
77
78
sqLiteDatabase .execSQL (qs );
78
79
}
79
-
80
- @ Override
81
- public void onUpgrade (SQLiteDatabase sqLiteDatabase ,
82
- int oldv , int newv )
83
- {
84
- sqLiteDatabase .execSQL ("DROP TABLE IF EXISTS " +
85
- VIDEO_TABLE_NAME + ";" );
86
- createTable (sqLiteDatabase );
87
- }
88
80
}
89
81
90
- private Context mContext ;
91
- private SimpleVideoDbHelper mOpenDbHelper ;
92
-
93
- private SQLiteDatabase mDb ;
94
82
95
- public SimpleFinchVideoContentProvider () {
96
- }
97
-
98
- public SimpleFinchVideoContentProvider (Context context ) {
99
- mContext = context ;
100
- init ();
101
- }
83
+ private SimpleVideoDbHelper mOpenDbHelper ;
102
84
103
85
@ Override
104
86
public boolean onCreate () {
105
- init ( );
87
+ mOpenDbHelper = new SimpleVideoDbHelper ( getContext () );
106
88
return true ;
107
89
}
108
90
109
- // allows object initialization to be reused.
110
- private void init () {
111
- mOpenDbHelper = new SimpleVideoDbHelper (getContext (),
112
- DATABASE_NAME , null );
113
- mDb = mOpenDbHelper .getWritableDatabase ();
91
+ @ Override
92
+ public String getType (Uri uri ) {
93
+ switch (sUriMatcher .match (uri )) {
94
+ case VIDEOS :
95
+ return FinchVideo .SimpleVideos .CONTENT_TYPE ;
96
+
97
+ case VIDEO_ID :
98
+ return FinchVideo .SimpleVideos .CONTENT_VIDEO_TYPE ;
99
+
100
+ default :
101
+ throw new IllegalArgumentException ("Unknown video type: " +
102
+ uri );
103
+ }
114
104
}
115
105
116
106
@ Override
@@ -128,25 +118,24 @@ public Cursor query(Uri uri, String[] projection, String where,
128
118
int match = sUriMatcher .match (uri );
129
119
130
120
Cursor c ;
131
-
132
121
switch (match ) {
133
122
case VIDEOS :
134
123
// query the database for all videos
135
- c = mDb .query (VIDEO_TABLE_NAME , projection ,
124
+ c = getDb () .query (VIDEO_TABLE_NAME , projection ,
136
125
where , whereArgs ,
137
- null , null , sortOrder );
126
+ null , null , orderBy );
138
127
139
128
c .setNotificationUri (getContext ().getContentResolver (),
140
129
FinchVideo .SimpleVideos .CONTENT_URI );
141
130
break ;
142
131
case VIDEO_ID :
143
132
// query the database for a specific video
144
133
long videoID = ContentUris .parseId (uri );
145
- c = mDb .query (VIDEO_TABLE_NAME , projection ,
134
+ c = getDb () .query (VIDEO_TABLE_NAME , projection ,
146
135
BaseColumns ._ID + " = " + videoID +
147
136
(!TextUtils .isEmpty (where ) ?
148
137
" AND (" + where + ')' : "" ),
149
- whereArgs , null , null , sortOrder );
138
+ whereArgs , null , null , orderBy );
150
139
c .setNotificationUri (getContext ().getContentResolver (),
151
140
FinchVideo .SimpleVideos .CONTENT_URI );
152
141
break ;
@@ -157,21 +146,6 @@ public Cursor query(Uri uri, String[] projection, String where,
157
146
return c ;
158
147
}
159
148
160
- @ Override
161
- public String getType (Uri uri ) {
162
- switch (sUriMatcher .match (uri )) {
163
- case VIDEOS :
164
- return FinchVideo .SimpleVideos .CONTENT_TYPE ;
165
-
166
- case VIDEO_ID :
167
- return FinchVideo .SimpleVideos .CONTENT_VIDEO_TYPE ;
168
-
169
- default :
170
- throw new IllegalArgumentException ("Unknown video type: " +
171
- uri );
172
- }
173
- }
174
-
175
149
@ Override
176
150
public Uri insert (Uri uri , ContentValues initialValues ) {
177
151
// Validate the requested uri
@@ -206,12 +180,12 @@ private void verifyValues(ContentValues values) {
206
180
// Make sure that the fields are all set
207
181
if (!values .containsKey (FinchVideo .SimpleVideos .TITLE_NAME )) {
208
182
Resources r = Resources .getSystem ();
209
- values .put (FinchVideo .SimpleVideos .URI_NAME ,
183
+ values .put (FinchVideo .SimpleVideos .TITLE_NAME ,
210
184
r .getString (android .R .string .untitled ));
211
185
}
212
186
213
187
if (!values .containsKey (FinchVideo .SimpleVideos .DESCRIPTION_NAME )) {
214
- values .put (FinchVideo .SimpleVideos .URI_NAME , "" );
188
+ values .put (FinchVideo .SimpleVideos .DESCRIPTION_NAME , "" );
215
189
}
216
190
217
191
if (!values .containsKey (FinchVideo .SimpleVideos .URI_NAME )) {
@@ -226,46 +200,43 @@ public int delete(Uri uri, String where, String[] whereArgs) {
226
200
227
201
switch (match ) {
228
202
case VIDEOS :
229
- affected = mDb .delete (VIDEO_TABLE_NAME ,
203
+ affected = getDb () .delete (VIDEO_TABLE_NAME ,
230
204
(!TextUtils .isEmpty (where ) ?
231
205
" AND (" + where + ')' : "" ),
232
206
whereArgs );
233
207
break ;
234
208
case VIDEO_ID :
235
209
long videoId = ContentUris .parseId (uri );
236
- affected = mDb .delete (VIDEO_TABLE_NAME ,
210
+ affected = getDb () .delete (VIDEO_TABLE_NAME ,
237
211
BaseColumns ._ID + "=" + videoId
238
212
+ (!TextUtils .isEmpty (where ) ?
239
213
" AND (" + where + ')' : "" ),
240
214
whereArgs );
241
-
242
- // the call to notify the uri after deletion is explicit
243
- getContext ().getContentResolver ().notifyChange (uri , null );
244
-
245
215
break ;
246
216
default :
247
217
throw new IllegalArgumentException ("unknown video element: " +
248
218
uri );
249
219
}
250
220
221
+ getContext ().getContentResolver ().notifyChange (uri , null );
251
222
return affected ;
252
223
}
253
224
254
225
@ Override
255
226
public int update (Uri uri , ContentValues values , String where ,
256
227
String [] whereArgs )
257
228
{
258
- SQLiteDatabase db = mOpenDbHelper .getWritableDatabase ();
259
229
int affected ;
230
+
260
231
switch (sUriMatcher .match (uri )) {
261
232
case VIDEOS :
262
- affected = db .update (VIDEO_TABLE_NAME , values ,
233
+ affected = getDb () .update (VIDEO_TABLE_NAME , values ,
263
234
where , whereArgs );
264
235
break ;
265
236
266
237
case VIDEO_ID :
267
238
String videoId = uri .getPathSegments ().get (1 );
268
- affected = db .update (VIDEO_TABLE_NAME , values ,
239
+ affected = getDb () .update (VIDEO_TABLE_NAME , values ,
269
240
BaseColumns ._ID + "=" + videoId
270
241
+ (!TextUtils .isEmpty (where ) ?
271
242
" AND (" + where + ')' : "" ),
@@ -277,6 +248,9 @@ public int update(Uri uri, ContentValues values, String where,
277
248
}
278
249
279
250
getContext ().getContentResolver ().notifyChange (uri , null );
251
+
280
252
return affected ;
281
253
}
254
+
255
+ private SQLiteDatabase getDb () { return mOpenDbHelper .getWritableDatabase (); }
282
256
}
0 commit comments