FileDocCategorySizeDatePackage
DrmProvider.javaAPI DocAndroid 1.5 API13731Wed May 06 22:42:48 BST 2009com.android.providers.drm

DrmProvider

public class DrmProvider extends ContentProvider
Drm content provider. See {@link android.provider.DrmStore} for details.
hide

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
Constructors Summary
Methods Summary
private voidcreateTables(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 intdelete(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 voiddeleteFiles(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 voiddropTables(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 ContentValuesensureFile(ContentValues initialValues)
Ensures there is a file in the _data column of values, if one isn't present a new file is created.

param
initialValues the values passed to insert by the caller
return
the new values

        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 voidgetTableAndWhere(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.StringgetType(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.Uriinsert(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 booleanonCreate()

        mOpenHelper = new OpenDatabaseHelper(getContext());
        return true;
    
public android.os.ParcelFileDescriptoropenFile(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.Cursorquery(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 intupdate(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;