首页 > 代码库 > SettingsProvider之Settings.System(Secure)内部类

SettingsProvider之Settings.System(Secure)内部类

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

SettingsProvider采用了双缓冲,我们前面说过SettingsProvider中存在SettingsCache缓冲区,那么在Settings.java中还存在另外一个缓冲区,它就是NameValueCache。

1、
 NameValueTable用于描述SettingsProvider的数据表,封装了putString方法。
  /**
     * Common base for tables of name/value settings.
     */
    public static class NameValueTable implements BaseColumns {
        public static final String NAME = "name";
        public static final String VALUE = http://www.mamicode.com/"value";>

2、

2.1、封装了lazyGetProvider,根据uri得到相应的ContentProvider,此处为IContentProvider,binder跨进程数据访问。

2.2、封装了getStringForUser方法。此方法分两部分,首先得到long newValuesVersion = SystemProperties.getLong(mVersionSystemProperty, 0);,首先从本地cache中得到想要的数据,如果本地cache中不存在想要的数据,那么进行第二部分,查询数据库。

2.3,如果newVersion不等于目前的version,说明本地的cache和SettingsProvider中的数据存在不同步,需要清空本地cache,重新加载。

// Our own user's settings data uses a client-side cache
                synchronized (this) {
                    if (mValuesVersion != newValuesVersion) {
                        if (LOCAL_LOGV || false) {
                            Log.v(TAG, "invalidate [" + mUri.getLastPathSegment() + "]: current "
                                    + newValuesVersion + " != cached " + mValuesVersion);
                        }

                        mValues.clear();
                        mValuesVersion = newValuesVersion;
                    }

                    if (mValues.containsKey(name)) {
                        return mValues.get(name);  // Could be null, that's OK -- negative caching
                    }


顾名思义   nameValue   的缓冲池。
// Thread-safe.
    private static class NameValueCache {
        private final String mVersionSystemProperty;
        private final Uri mUri;

        private static final String[] SELECT_VALUE =
            http://www.mamicode.com/new String[] { Settings.NameValueTable.VALUE };>

3、public static final class Bookmarks implements BaseColumns{
     public static Intent getIntentForShortcut(ContentResolver cr, char shortcut)//构建shortCut的intent
        {
            Intent intent = null;

            Cursor c = cr.query(CONTENT_URI,
                    sIntentProjection, sShortcutSelection,
                    new String[] { String.valueOf((int) shortcut) }, ORDERING);
            // Keep trying until we find a valid shortcut
            try {
                while (intent == null && c.moveToNext()) {
                    try {
                        String intentURI = c.getString(c.getColumnIndexOrThrow(INTENT));
                        intent = Intent.parseUri(intentURI, 0);
                    } catch (java.net.URISyntaxException e) {
                        // The stored URL is bad...  ignore it.
                    } catch (IllegalArgumentException e) {
                        // Column not found
                        Log.w(TAG, "Intent column not found", e);
                    }
                }
            } finally {
                if (c != null) c.close();
            }

            return intent;
        }



        public static Uri add(ContentResolver cr,
                                           Intent intent,
                                           String title,
                                           String folder,
                                           char shortcut,
                                           int ordering)
        {//加入书签
            // If a shortcut is supplied, and it is already defined for
            // another bookmark, then remove the old definition.
            if (shortcut != 0) {
                cr.delete(CONTENT_URI, sShortcutSelection,
                        new String[] { String.valueOf((int) shortcut) });
            }

            ContentValues values = new ContentValues();
            if (title != null) values.put(TITLE, title);
            if (folder != null) values.put(FOLDER, folder);
            values.put(INTENT, intent.toUri(0));
            if (shortcut != 0) values.put(SHORTCUT, (int) shortcut);
            values.put(ORDERING, ordering);
            return cr.insert(CONTENT_URI, values);
        }


public static CharSequence getTitle(Context context, Cursor cursor) {//得到title
            int titleColumn = cursor.getColumnIndex(TITLE);
            int intentColumn = cursor.getColumnIndex(INTENT);
            if (titleColumn == -1 || intentColumn == -1) {
                throw new IllegalArgumentException(
                        "The cursor must contain the TITLE and INTENT columns.");
            }

            String title = cursor.getString(titleColumn);
            if (!TextUtils.isEmpty(title)) {
                return title;
            }

            String intentUri = cursor.getString(intentColumn);
            if (TextUtils.isEmpty(intentUri)) {
                return "";
            }

            Intent intent;
            try {
                intent = Intent.parseUri(intentUri, 0);
            } catch (URISyntaxException e) {
                return "";
            }

            PackageManager packageManager = context.getPackageManager();
            ResolveInfo info = packageManager.resolveActivity(intent, 0);
            return info != null ? info.loadLabel(packageManager) : "";
        }
}