SettingsProvider之onCreate、lookupValue、call

转载请注明出处:http://blog.csdn.net/droyon/article/details/35558735

一、onCreate
  • 构建USER_OWNER的Databases以及相关缓冲数据表。
    establishDbTracking(UserHandle.USER_OWNER);
  • 构建广播监听。

    getContext().registerReceiver(new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    if (intent.getAction().equals(Intent.ACTION_USER_REMOVED)) {
                        final int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
                                UserHandle.USER_OWNER);
                        if (userHandle != UserHandle.USER_OWNER) {
                            onUserRemoved(userHandle);
                        }
                    }
                }
            }, userFilter);


二、lookupValue
  • 读取缓冲。
    Bundle value = cache.get(key);
                if (value != null) {
                    if (value != TOO_LARGE_TO_CACHE_MARKER) {
                        return value;
                    }
                    // else we fall through and read the value from disk
                } else if (cache.fullyMatchesDisk()) {//当前已经全部和Disk(硬盘)保持了一直,没有必要读去硬盘。
                    // Fast path (very common).  Don't even try touch disk
                    // if we know we've slurped it all in.  Trying to
                    // touch the disk would mean waiting for yaffs2 to
                    // give us access, which could takes hundreds of
                    // milliseconds.  And we're very likely being called
                    // from somebody's UI thread...
                    //不要试图读取硬盘,假如我们知道我们已经读取了所有的数据。
                    return NULL_SETTING;
                }
  • 从数据库 中读取数据

    1、try {
                cursor = db.query(table, COLUMN_VALUE, "name=?", new String[]{key},
                                  null, null, null, null);
                if (cursor != null && cursor.getCount() == 1) {
                    cursor.moveToFirst();
                    return cache.putIfAbsent(key, cursor.getString(0));//更新cache并返回
                }
            }
    2、加入cursor.getCount == 1,那么:cache.putIfAbsent方法:
    public Bundle putIfAbsent(String key, String value) {
                Bundle bundle = (value == null) ? NULL_SETTING : Bundle.forPair("value", value);
                if (value == null || value.length() <= MAX_CACHE_ENTRY_SIZE) {//如果value为null,或者value的长度小于500字符
                    synchronized (this) {
                        if (get(key) == null) {
                            put(key, bundle);//bundle可能:NULL_SETTINGS、Bundle.forPair("value", value)
                        }
                    }
                }//【value不为null,且value的长度大于500,直接返回bundle,bundle为Bundle.forPair("value", value)】
                return bundle;
            }
    3、如果上一步出现一场,或者cursor==null,那么就要跳出这一层,然后执行:cache.putIfAbsent(key, null)
    cache.putIfAbsent(key, null);
    更新cache的内容为NULL_SETTINGS。

三、call的方法执行。
  • 根据不同GET_方法,执行lookupValue方法。
    if (Settings.CALL_METHOD_GET_SYSTEM.equals(method)) {
                if (LOCAL_LOGV) Slog.v(TAG, "call(system:" + request + ") for " + callingUser);
                dbHelper = getOrEstablishDatabase(callingUser);
                cache = sSystemCaches.get(callingUser);
                return lookupValue(dbHelper, TABLE_SYSTEM, cache, request);
            }
            if (Settings.CALL_METHOD_GET_SECURE.equals(method)) {
                if (LOCAL_LOGV) Slog.v(TAG, "call(secure:" + request + ") for " + callingUser);
                dbHelper = getOrEstablishDatabase(callingUser);
                cache = sSecureCaches.get(callingUser);
                return lookupValue(dbHelper, TABLE_SECURE, cache, request);
            }
            if (Settings.CALL_METHOD_GET_GLOBAL.equals(method)) {
                if (LOCAL_LOGV) Slog.v(TAG, "call(global:" + request + ") for " + callingUser);
                // fast path: owner db & cache are immutable after onCreate() so we need not
                // guard on the attempt to look them up
                return lookupValue(getOrEstablishDatabase(UserHandle.USER_OWNER), TABLE_GLOBAL,
                        sGlobalCache, request);
            }
  • 根据不同PUT_方法,执行insertForUser。
    final ContentValues values = new ContentValues();
            values.put(Settings.NameValueTable.NAME, request);
            values.put(Settings.NameValueTable.VALUE, newValue);
            if (Settings.CALL_METHOD_PUT_SYSTEM.equals(method)) {
                if (LOCAL_LOGV) Slog.v(TAG, "call_put(system:" + request + "=" + newValue + ") for " + callingUser);
                insertForUser(Settings.System.CONTENT_URI, values, callingUser);
            } else if (Settings.CALL_METHOD_PUT_SECURE.equals(method)) {
                if (LOCAL_LOGV) Slog.v(TAG, "call_put(secure:" + request + "=" + newValue + ") for " + callingUser);
                insertForUser(Settings.Secure.CONTENT_URI, values, callingUser);
            } else if (Settings.CALL_METHOD_PUT_GLOBAL.equals(method)) {
                if (LOCAL_LOGV) Slog.v(TAG, "call_put(global:" + request + "=" + newValue + ") for " + callingUser);
                insertForUser(Settings.Global.CONTENT_URI, values, callingUser);
            } else {
                Slog.w(TAG, "call() with invalid method: " + method);
            }

你可能感兴趣的:(SettingsProvider之onCreate、lookupValue、call)