Skip to content

Commit 8fb3181

Browse files
committed
Fix a few bugs. Tip of the hat to Ashok Gelal <ashokgelal@gmail.com>
1 parent 5fc19ef commit 8fb3181

File tree

1 file changed

+54
-80
lines changed

1 file changed

+54
-80
lines changed

FinchVideo/src/com/oreilly/demo/android/pa/finchvideo/provider/SimpleFinchVideoContentProvider.java

Lines changed: 54 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -22,51 +22,52 @@
2222
*/
2323
public class SimpleFinchVideoContentProvider extends ContentProvider {
2424
public static final String SIMPLE_VIDEO = "simple_video";
25+
public static final String VIDEO_TABLE_NAME = "videos";
2526

2627
private static final int VIDEOS = 1;
2728
private static final int VIDEO_ID = 2;
28-
2929
private static UriMatcher sUriMatcher;
30-
31-
private static HashMap<String, String> sVideosProjectionMap;
32-
3330
static {
3431
sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
3532
sUriMatcher.addURI(FinchVideo.SIMPLE_AUTHORITY, FinchVideo.SimpleVideos.VIDEO_NAME,
36-
VIDEOS);
33+
VIDEOS);
3734
// use of the hash character indicates matching of an id
3835
sUriMatcher.addURI(FinchVideo.SIMPLE_AUTHORITY,
39-
FinchVideo.SimpleVideos.VIDEO_NAME + "/#", VIDEO_ID);
36+
FinchVideo.SimpleVideos.VIDEO_NAME + "/#", VIDEO_ID);
37+
}
4038

39+
private static HashMap<String, String> sVideosProjectionMap;
40+
static {
4141
// example projection map, not actually used in this application
4242
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);
5147
}
5248

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-
5849
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);
6355
}
6456

6557
@Override
6658
public void onCreate(SQLiteDatabase sqLiteDatabase) {
6759
createTable(sqLiteDatabase);
6860
}
6961

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+
7071
private void createTable(SQLiteDatabase sqLiteDatabase) {
7172
String qs = "CREATE TABLE " + VIDEO_TABLE_NAME + " (" +
7273
BaseColumns._ID +
@@ -76,41 +77,30 @@ private void createTable(SQLiteDatabase sqLiteDatabase) {
7677
FinchVideo.SimpleVideos.URI_NAME + " TEXT);";
7778
sqLiteDatabase.execSQL(qs);
7879
}
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-
}
8880
}
8981

90-
private Context mContext;
91-
private SimpleVideoDbHelper mOpenDbHelper;
92-
93-
private SQLiteDatabase mDb;
9482

95-
public SimpleFinchVideoContentProvider() {
96-
}
97-
98-
public SimpleFinchVideoContentProvider(Context context) {
99-
mContext = context;
100-
init();
101-
}
83+
private SimpleVideoDbHelper mOpenDbHelper;
10284

10385
@Override
10486
public boolean onCreate() {
105-
init();
87+
mOpenDbHelper = new SimpleVideoDbHelper(getContext());
10688
return true;
10789
}
10890

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+
}
114104
}
115105

116106
@Override
@@ -128,25 +118,24 @@ public Cursor query(Uri uri, String[] projection, String where,
128118
int match = sUriMatcher.match(uri);
129119

130120
Cursor c;
131-
132121
switch (match) {
133122
case VIDEOS:
134123
// query the database for all videos
135-
c = mDb.query(VIDEO_TABLE_NAME, projection,
124+
c = getDb().query(VIDEO_TABLE_NAME, projection,
136125
where, whereArgs,
137-
null, null, sortOrder);
126+
null, null, orderBy);
138127

139128
c.setNotificationUri(getContext().getContentResolver(),
140129
FinchVideo.SimpleVideos.CONTENT_URI);
141130
break;
142131
case VIDEO_ID:
143132
// query the database for a specific video
144133
long videoID = ContentUris.parseId(uri);
145-
c = mDb.query(VIDEO_TABLE_NAME, projection,
134+
c = getDb().query(VIDEO_TABLE_NAME, projection,
146135
BaseColumns._ID + " = " + videoID +
147136
(!TextUtils.isEmpty(where) ?
148137
" AND (" + where + ')' : ""),
149-
whereArgs, null, null, sortOrder);
138+
whereArgs, null, null, orderBy);
150139
c.setNotificationUri(getContext().getContentResolver(),
151140
FinchVideo.SimpleVideos.CONTENT_URI);
152141
break;
@@ -157,21 +146,6 @@ public Cursor query(Uri uri, String[] projection, String where,
157146
return c;
158147
}
159148

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-
175149
@Override
176150
public Uri insert(Uri uri, ContentValues initialValues) {
177151
// Validate the requested uri
@@ -206,12 +180,12 @@ private void verifyValues(ContentValues values) {
206180
// Make sure that the fields are all set
207181
if (!values.containsKey(FinchVideo.SimpleVideos.TITLE_NAME)) {
208182
Resources r = Resources.getSystem();
209-
values.put(FinchVideo.SimpleVideos.URI_NAME,
183+
values.put(FinchVideo.SimpleVideos.TITLE_NAME,
210184
r.getString(android.R.string.untitled));
211185
}
212186

213187
if (!values.containsKey(FinchVideo.SimpleVideos.DESCRIPTION_NAME)) {
214-
values.put(FinchVideo.SimpleVideos.URI_NAME, "");
188+
values.put(FinchVideo.SimpleVideos.DESCRIPTION_NAME, "");
215189
}
216190

217191
if (!values.containsKey(FinchVideo.SimpleVideos.URI_NAME)) {
@@ -226,46 +200,43 @@ public int delete(Uri uri, String where, String[] whereArgs) {
226200

227201
switch (match) {
228202
case VIDEOS:
229-
affected = mDb.delete(VIDEO_TABLE_NAME,
203+
affected = getDb().delete(VIDEO_TABLE_NAME,
230204
(!TextUtils.isEmpty(where) ?
231205
" AND (" + where + ')' : ""),
232206
whereArgs);
233207
break;
234208
case VIDEO_ID:
235209
long videoId = ContentUris.parseId(uri);
236-
affected = mDb.delete(VIDEO_TABLE_NAME,
210+
affected = getDb().delete(VIDEO_TABLE_NAME,
237211
BaseColumns._ID + "=" + videoId
238212
+ (!TextUtils.isEmpty(where) ?
239213
" AND (" + where + ')' : ""),
240214
whereArgs);
241-
242-
// the call to notify the uri after deletion is explicit
243-
getContext().getContentResolver().notifyChange(uri, null);
244-
245215
break;
246216
default:
247217
throw new IllegalArgumentException("unknown video element: " +
248218
uri);
249219
}
250220

221+
getContext().getContentResolver().notifyChange(uri, null);
251222
return affected;
252223
}
253224

254225
@Override
255226
public int update(Uri uri, ContentValues values, String where,
256227
String[] whereArgs)
257228
{
258-
SQLiteDatabase db = mOpenDbHelper.getWritableDatabase();
259229
int affected;
230+
260231
switch (sUriMatcher.match(uri)) {
261232
case VIDEOS:
262-
affected = db.update(VIDEO_TABLE_NAME, values,
233+
affected = getDb().update(VIDEO_TABLE_NAME, values,
263234
where, whereArgs);
264235
break;
265236

266237
case VIDEO_ID:
267238
String videoId = uri.getPathSegments().get(1);
268-
affected = db.update(VIDEO_TABLE_NAME, values,
239+
affected = getDb().update(VIDEO_TABLE_NAME, values,
269240
BaseColumns._ID + "=" + videoId
270241
+ (!TextUtils.isEmpty(where) ?
271242
" AND (" + where + ')' : ""),
@@ -277,6 +248,9 @@ public int update(Uri uri, ContentValues values, String where,
277248
}
278249

279250
getContext().getContentResolver().notifyChange(uri, null);
251+
280252
return affected;
281253
}
254+
255+
private SQLiteDatabase getDb() { return mOpenDbHelper.getWritableDatabase(); }
282256
}

0 commit comments

Comments
 (0)