DrmProviderpublic class DrmProvider extends ContentProvider Drm content provider. See {@link android.provider.DrmStore} for details. |
Fields Summary |
---|
static final GetTableAndWhereOutParameter | sGetTableAndWhereParam | private static String | TAG | private static final int | AUDIO | private static final int | AUDIO_ID | private static final int | IMAGES | private static final int | IMAGES_ID | private static final UriMatcher | URI_MATCHER | private static final String[] | MIME_TYPE_PROJECTION | private android.database.sqlite.SQLiteOpenHelper | mOpenHelper |
Methods Summary |
---|
private void | createTables(android.database.sqlite.SQLiteDatabase db)Creates the table that'll hold the download information.
db.execSQL("CREATE TABLE audio (" +
"_id INTEGER PRIMARY KEY," +
"_data TEXT," +
"_size INTEGER," +
"title TEXT," +
"mime_type TEXT" +
");");
db.execSQL("CREATE TABLE images (" +
"_id INTEGER PRIMARY KEY," +
"_data TEXT," +
"_size INTEGER," +
"title TEXT," +
"mime_type TEXT" +
");");
| public int | delete(android.net.Uri uri, java.lang.String userWhere, java.lang.String[] whereArgs)
if (getContext().checkCallingOrSelfPermission(Manifest.permission.ACCESS_DRM)
!= PackageManager.PERMISSION_GRANTED) {
throw new SecurityException("Requires DRM permission");
}
int count;
int match = URI_MATCHER.match(uri);
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
synchronized (sGetTableAndWhereParam) {
getTableAndWhere(uri, match, userWhere, sGetTableAndWhereParam);
switch (match) {
default:
deleteFiles(uri, userWhere, whereArgs);
count = db.delete(sGetTableAndWhereParam.table,
sGetTableAndWhereParam.where, whereArgs);
break;
}
}
return count;
| private void | deleteFiles(android.net.Uri uri, java.lang.String userWhere, java.lang.String[] whereArgs)
Cursor c = query(uri, new String [] { "_data" }, userWhere, whereArgs, null);
try {
if (c != null && c.moveToFirst()) {
String prefix = getContext().getFilesDir().getPath();
do {
String path = c.getString(0);
if (!path.startsWith(prefix)) {
throw new SecurityException("Attempted to delete a non-DRM file");
}
new File(path).delete();
} while (c.moveToNext());
}
} finally {
if (c != null) {
c.close();
}
}
| private void | dropTables(android.database.sqlite.SQLiteDatabase db)Deletes the table that holds the download information.
// TODO: error handling
db.execSQL("DROP TABLE IF EXISTS audio");
db.execSQL("DROP TABLE IF EXISTS images");
| private ContentValues | ensureFile(ContentValues initialValues)Ensures there is a file in the _data column of values, if one isn't
present a new file is created.
try {
File parent = getContext().getFilesDir();
parent.mkdirs();
File file = File.createTempFile("DRM-", ".data", parent);
ContentValues values = new ContentValues(initialValues);
values.put("_data", file.toString());
return values;
} catch (IOException e) {
Log.e(TAG, "Failed to create data file in ensureFile");
return null;
}
| private void | getTableAndWhere(android.net.Uri uri, int match, java.lang.String userWhere, com.android.providers.drm.DrmProvider$GetTableAndWhereOutParameter out)
String where = null;
switch (match) {
case AUDIO:
out.table = "audio";
break;
case AUDIO_ID:
out.table = "audio";
where = "_id=" + uri.getPathSegments().get(1);
break;
case IMAGES:
out.table = "images";
break;
case IMAGES_ID:
out.table = "images";
where = "_id=" + uri.getPathSegments().get(1);
break;
default:
throw new UnsupportedOperationException(
"Unknown or unsupported URL: " + uri.toString());
}
// Add in the user requested WHERE clause, if needed
if (!TextUtils.isEmpty(userWhere)) {
if (!TextUtils.isEmpty(where)) {
out.where = where + " AND (" + userWhere + ")";
} else {
out.where = userWhere;
}
} else {
out.where = where;
}
| public java.lang.String | getType(android.net.Uri url)
switch (URI_MATCHER.match(url)) {
case AUDIO_ID:
case IMAGES_ID:
Cursor c = query(url, MIME_TYPE_PROJECTION, null, null, null);
if (c != null && c.getCount() == 1) {
c.moveToFirst();
String mimeType = c.getString(1);
c.deactivate();
return mimeType;
}
break;
}
throw new IllegalStateException("Unknown URL");
| public android.net.Uri | insert(android.net.Uri uri, ContentValues initialValues)
if (getContext().checkCallingOrSelfPermission(Manifest.permission.ACCESS_DRM)
!= PackageManager.PERMISSION_GRANTED) {
throw new SecurityException("Requires DRM permission");
}
long rowId;
int match = URI_MATCHER.match(uri);
Uri newUri = null;
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
if (initialValues == null) {
initialValues = new ContentValues();
}
switch (match) {
case AUDIO: {
ContentValues values = ensureFile(initialValues);
if (values == null) return null;
rowId = db.insert("audio", "title", values);
if (rowId > 0) {
newUri = ContentUris.withAppendedId(DrmStore.Audio.CONTENT_URI, rowId);
}
break;
}
case IMAGES: {
ContentValues values = ensureFile(initialValues);
if (values == null) return null;
rowId = db.insert("images", "title", values);
if (rowId > 0) {
newUri = ContentUris.withAppendedId(DrmStore.Images.CONTENT_URI, rowId);
}
break;
}
default:
throw new UnsupportedOperationException("Invalid URI " + uri);
}
if (newUri != null) {
getContext().getContentResolver().notifyChange(uri, null);
}
return newUri;
| public boolean | onCreate()
mOpenHelper = new OpenDatabaseHelper(getContext());
return true;
| public android.os.ParcelFileDescriptor | openFile(android.net.Uri uri, java.lang.String mode)
if (getContext().checkCallingOrSelfPermission(Manifest.permission.ACCESS_DRM)
!= PackageManager.PERMISSION_GRANTED) {
throw new SecurityException("Requires DRM permission");
}
return openFileHelper(uri, mode);
| public android.database.Cursor | query(android.net.Uri uri, java.lang.String[] projectionIn, java.lang.String selection, java.lang.String[] selectionArgs, java.lang.String sort)
String groupBy = null;
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
switch (URI_MATCHER.match(uri)) {
case AUDIO:
qb.setTables("audio");
break;
case AUDIO_ID:
qb.setTables("audio");
qb.appendWhere("_id=" + uri.getPathSegments().get(1));
break;
case IMAGES:
qb.setTables("images");
break;
case IMAGES_ID:
qb.setTables("images");
qb.appendWhere("_id=" + uri.getPathSegments().get(1));
break;
default:
throw new IllegalStateException("Unknown URL: " + uri.toString());
}
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
Cursor c = qb.query(db, projectionIn, selection,
selectionArgs, groupBy, null, sort);
if (c != null) {
c.setNotificationUri(getContext().getContentResolver(), uri);
}
return c;
| public int | update(android.net.Uri uri, ContentValues initialValues, java.lang.String userWhere, java.lang.String[] whereArgs)
if (getContext().checkCallingOrSelfPermission(Manifest.permission.ACCESS_DRM)
!= PackageManager.PERMISSION_GRANTED) {
throw new SecurityException("Requires DRM permission");
}
int count;
int match = URI_MATCHER.match(uri);
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
synchronized (sGetTableAndWhereParam) {
getTableAndWhere(uri, match, userWhere, sGetTableAndWhereParam);
switch (match) {
default:
count = db.update(sGetTableAndWhereParam.table, initialValues,
sGetTableAndWhereParam.where, whereArgs);
break;
}
}
return count;
|
|