权限检查联系人ProfileProvider

每日一贴,今天的内容关键字为权限检查

    ProfileProvider继承自AbstractContyactsProvider。

    源代码请自行下载

    每日一道理
书籍好比一架梯子,它能引领人们登上文化的殿堂;书籍如同一把钥匙,它将帮助我们开启心灵的智慧之窗;书籍犹如一条小船,它会载着我们驶向知识的海洋。
package com.android.providers.contacts;

import android.content.ContentValues;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import java.io.FileNotFoundException;
import java.util.Locale;

/**
 * Simple content provider to handle directing profile-specific calls against a separate
 * database from the rest of contacts.
 */
public class ProfileProvider extends AbstractContactsProvider {

    private static final String READ_PERMISSION = "android.permission.READ_PROFILE";
    private static final String WRITE_PERMISSION = "android.permission.WRITE_PROFILE";

    // The Contacts provider handles most of the logic - this provider is only invoked when the
    // URI belongs to a profile action, setting up the proper database.
    private final ContactsProvider2 mDelegate;

    public ProfileProvider(ContactsProvider2 delegate) {
        mDelegate = delegate;
    }

    /**
     * Performs a permission check on the read profile permission.  Checks the delegate contacts
     * provider to see whether this is an authorized one-time-use URI.
     * @param uri The URI being accessed.
     */
    public void enforceReadPermission(Uri uri) {//检查是不是有读权限
        if (!mDelegate.isValidPreAuthorizedUri(uri)) {
            mDelegate.getContext().enforceCallingOrSelfPermission(READ_PERMISSION, null);
        }
    }

    /**
     * Performs a permission check on the write profile permission.
     */
    public void enforceWritePermission() {//检查是不是有写权限
        mDelegate.getContext().enforceCallingOrSelfPermission(WRITE_PERMISSION, null);
    }
    /**
     * 重写getDatabaseHelper,父类的方法
     */
    @Override
    protected ProfileDatabaseHelper getDatabaseHelper(Context context) {
        return ProfileDatabaseHelper.getInstance(context);
    }

    @Override
    protected ThreadLocal<ContactsTransaction> getTransactionHolder() {//得到线程当地存储的对象
        return mDelegate.getTransactionHolder();
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
            String sortOrder) {
        enforceReadPermission(uri);//检查权限,如果没有权限抛出异常
        mDelegate.substituteDb(getDatabaseHelper().getReadableDatabase());//替换数据库  profile.db
        return mDelegate.queryLocal(uri, projection, selection, selectionArgs, sortOrder, -1);
    }

    @Override
    protected Uri insertInTransaction(Uri uri, ContentValues values) {
        enforceWritePermission();//检查写权限,如果不存在权限,抛出异常
        useProfileDbForTransaction();//启动触发器,并切换数据库到profile.db
        return mDelegate.insertInTransaction(uri, values);
    }

    @Override
    protected int updateInTransaction(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        enforceWritePermission();//检查写权限,如果不存在权限,抛出异常
        useProfileDbForTransaction();//启动触发器,并切换数据库到profile.db
        return mDelegate.updateInTransaction(uri, values, selection, selectionArgs);
    }

    @Override
    protected int deleteInTransaction(Uri uri, String selection, String[] selectionArgs) {
        enforceWritePermission();//检查写权限,如果不存在权限,抛出异常
        useProfileDbForTransaction();//启动触发器,并切换数据库到profile.db
        return mDelegate.deleteInTransaction(uri, selection, selectionArgs);
    }

    @Override
    public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException {
        if (mode != null && mode.contains("w")) {
            enforceWritePermission();//检查写权限,如果不存在权限,抛出异常
            mDelegate.substituteDb(getDatabaseHelper().getWritableDatabase());//切换到profile.db上
        } else {
            enforceReadPermission(uri);//检查权限,如果不存在权限,抛出异常
            mDelegate.substituteDb(getDatabaseHelper().getReadableDatabase());
        }
        return mDelegate.openAssetFileLocal(uri, mode);
    }

    private void useProfileDbForTransaction() {
        ContactsTransaction transaction = getCurrentTransaction();//得到线程当地存储,mTreasactionLocal来自mDelegate对象
        SQLiteDatabase db = getDatabaseHelper().getWritableDatabase();//子类重写了getDatabaseHelper()方法  profile.db。
        transaction.startTransactionForDb(db, ContactsProvider2.PROFILE_DB_TAG, this);
        mDelegate.substituteDb(db);//切换数据库到profile.db
    }

    @Override
    protected void notifyChange() {
        mDelegate.notifyChange();
    }

    protected void notifyChange(boolean syncToNetwork) {
        mDelegate.notifyChange(syncToNetwork);
    }

    protected Locale getLocale() {
        return mDelegate.getLocale();
    }

    public void onBegin() {
        mDelegate.switchToProfileMode();
        mDelegate.onBegin();
    }

    public void onCommit() {
        mDelegate.switchToProfileMode();
        mDelegate.onCommit();
    }

    @Override
    public void onRollback() {
        mDelegate.switchToProfileMode();
        mDelegate.onRollback();
    }

    @Override
    protected boolean yield(ContactsTransaction transaction) {
        return mDelegate.yield(transaction);
    }

    @Override
    public String getType(Uri uri) {
        return mDelegate.getType(uri);
    }
}

    

文章结束给大家分享下程序员的一些笑话语录: 这个世界上只有10种人:懂得二进制的和不懂得二进制的。

--------------------------------- 原创文章 By
权限和检查
---------------------------------

原文地址:https://www.cnblogs.com/xinyuyuanm/p/3150408.html