AlarmProviderpublic class AlarmProvider extends android.content.ContentProvider
Fields Summary |
---|
private android.database.sqlite.SQLiteOpenHelper | mOpenHelper | private static final int | ALARMS | private static final int | ALARMS_ID | private static final android.content.UriMatcher | sURLMatcher |
Constructors Summary |
---|
public AlarmProvider()
|
Methods Summary |
---|
public int | delete(android.net.Uri url, java.lang.String where, java.lang.String[] whereArgs)
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
int count;
long rowId = 0;
switch (sURLMatcher.match(url)) {
case ALARMS:
count = db.delete("alarms", where, whereArgs);
break;
case ALARMS_ID:
String segment = url.getPathSegments().get(1);
rowId = Long.parseLong(segment);
if (TextUtils.isEmpty(where)) {
where = "_id=" + segment;
} else {
where = "_id=" + segment + " AND (" + where + ")";
}
count = db.delete("alarms", where, whereArgs);
break;
default:
throw new IllegalArgumentException("Cannot delete from URL: " + url);
}
getContext().getContentResolver().notifyChange(url, null);
return count;
| public java.lang.String | getType(android.net.Uri url)
int match = sURLMatcher.match(url);
switch (match) {
case ALARMS:
return "vnd.android.cursor.dir/alarms";
case ALARMS_ID:
return "vnd.android.cursor.item/alarms";
default:
throw new IllegalArgumentException("Unknown URL");
}
| public android.net.Uri | insert(android.net.Uri url, android.content.ContentValues initialValues)
if (sURLMatcher.match(url) != ALARMS) {
throw new IllegalArgumentException("Cannot insert into URL: " + url);
}
ContentValues values;
if (initialValues != null)
values = new ContentValues(initialValues);
else
values = new ContentValues();
if (!values.containsKey(Alarms.AlarmColumns.HOUR))
values.put(Alarms.AlarmColumns.HOUR, 0);
if (!values.containsKey(Alarms.AlarmColumns.MINUTES))
values.put(Alarms.AlarmColumns.MINUTES, 0);
if (!values.containsKey(Alarms.AlarmColumns.DAYS_OF_WEEK))
values.put(Alarms.AlarmColumns.DAYS_OF_WEEK, 0);
if (!values.containsKey(Alarms.AlarmColumns.ALARM_TIME))
values.put(Alarms.AlarmColumns.ALARM_TIME, 0);
if (!values.containsKey(Alarms.AlarmColumns.ENABLED))
values.put(Alarms.AlarmColumns.ENABLED, 0);
if (!values.containsKey(Alarms.AlarmColumns.VIBRATE))
values.put(Alarms.AlarmColumns.VIBRATE, 1);
if (!values.containsKey(Alarms.AlarmColumns.MESSAGE))
values.put(Alarms.AlarmColumns.MESSAGE, "");
if (!values.containsKey(Alarms.AlarmColumns.ALERT))
values.put(Alarms.AlarmColumns.ALERT, "");
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
long rowId = db.insert("alarms", Alarms.AlarmColumns.MESSAGE, values);
if (rowId < 0) {
throw new SQLException("Failed to insert row into " + url);
}
if (Log.LOGV) Log.v("Added alarm rowId = " + rowId);
Uri newUrl = ContentUris.withAppendedId(Alarms.AlarmColumns.CONTENT_URI, rowId);
getContext().getContentResolver().notifyChange(newUrl, null);
return newUrl;
| public boolean | onCreate()
mOpenHelper = new DatabaseHelper(getContext());
return true;
| public android.database.Cursor | query(android.net.Uri url, java.lang.String[] projectionIn, java.lang.String selection, java.lang.String[] selectionArgs, java.lang.String sort)
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
// Generate the body of the query
int match = sURLMatcher.match(url);
switch (match) {
case ALARMS:
qb.setTables("alarms");
break;
case ALARMS_ID:
qb.setTables("alarms");
qb.appendWhere("_id=");
qb.appendWhere(url.getPathSegments().get(1));
break;
default:
throw new IllegalArgumentException("Unknown URL " + url);
}
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
Cursor ret = qb.query(db, projectionIn, selection, selectionArgs,
null, null, sort);
if (ret == null) {
if (Log.LOGV) Log.v("Alarms.query: failed");
} else {
ret.setNotificationUri(getContext().getContentResolver(), url);
}
return ret;
| public int | update(android.net.Uri url, android.content.ContentValues values, java.lang.String where, java.lang.String[] whereArgs)
int count;
long rowId = 0;
int match = sURLMatcher.match(url);
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
switch (match) {
case ALARMS_ID: {
String segment = url.getPathSegments().get(1);
rowId = Long.parseLong(segment);
count = db.update("alarms", values, "_id=" + rowId, null);
break;
}
default: {
throw new UnsupportedOperationException(
"Cannot update URL: " + url);
}
}
if (Log.LOGV) Log.v("*** notifyChange() rowId: " + rowId + " url " + url);
getContext().getContentResolver().notifyChange(url, null);
return count;
|
|