SettingsProvider之多用户概念

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

1、
  • // Each defined user has their own settings
    protected final SparseArray<DatabaseHelper> mOpenHelpers = new SparseArray<DatabaseHelper>();
  • private static final SparseArray<SettingsCache> sSystemCaches
            = new SparseArray<SettingsCache>();
  •     private static final SparseArray<SettingsCache> sSecureCaches
            = new SparseArray<SettingsCache>();
  •     private static final SettingsCache sGlobalCache = new SettingsCache(TABLE_GLOBAL);
  • // The count of how many known (handled by SettingsProvider)
        // database mutations are currently being handled for this user.
        // Used by file observers to not reload the database when it's ourselves
        // modifying it.
        private static final SparseArray<AtomicInteger> sKnownMutationsInFlight
                = new SparseArray<AtomicInteger>();//用户评判是否为用户操作

2、删除用户时,同步更新所有存储用户概念的变量
void onUserRemoved(int userHandle) {
        synchronized (this) {
            // the db file itself will be deleted automatically, but we need to tear down
            // our caches and other internal bookkeeping.
            FileObserver observer = sObserverInstances.get(userHandle);
            if (observer != null) {
                observer.stopWatching();
                sObserverInstances.delete(userHandle);
            }

            mOpenHelpers.delete(userHandle);
            sSystemCaches.delete(userHandle);
            sSecureCaches.delete(userHandle);
            sKnownMutationsInFlight.delete(userHandle);
        }
    }

3、establishDbTracking(UserHandle.USER_OWNER);
private void establishDbTracking(int userHandle) {
        if (LOCAL_LOGV) {
            Slog.i(TAG, "Installing settings db helper and caches for user " + userHandle);
        }

        DatabaseHelper dbhelper;

        synchronized (this) {
            dbhelper = mOpenHelpers.get(userHandle);//每个user有一个dbHelper,如果为null,重新创建一个
            if (dbhelper == null) {
                dbhelper = new DatabaseHelper(getContext(), userHandle);
                mOpenHelpers.append(userHandle, dbhelper);

                sSystemCaches.append(userHandle, new SettingsCache(TABLE_SYSTEM));
                sSecureCaches.append(userHandle, new SettingsCache(TABLE_SECURE));
                sKnownMutationsInFlight.append(userHandle, new AtomicInteger(0));
            }
        }

        // Initialization of the db *outside* the locks.  It's possible that racing
        // threads might wind up here, the second having read the cache entries
        // written by the first, but that's benign: the SQLite helper implementation
        // manages concurrency itself, and it's important that we not run the db
        // initialization with any of our own locks held, so we're fine.
        SQLiteDatabase db = dbhelper.getWritableDatabase();

        // Watch for external modifications to the database files,
        // keeping our caches in sync.  We synchronize the observer set
        // separately, and of course it has to run after the db file
        // itself was set up by the DatabaseHelper.
        synchronized (sObserverInstances) {
            if (sObserverInstances.get(userHandle) == null) {//FileObserver如果为null,创建一个。
                SettingsFileObserver observer = new SettingsFileObserver(userHandle, db.getPath());
                sObserverInstances.append(userHandle, observer);
                observer.startWatching();
            }
        }

        ensureAndroidIdIsSet(userHandle);

        startAsyncCachePopulation(userHandle);
    }

4、存储一个随机的AndroidId。
private boolean ensureAndroidIdIsSet(int userHandle) {
        final Cursor c = queryForUser(Settings.Secure.CONTENT_URI,
                new String[] { Settings.NameValueTable.VALUE },
                Settings.NameValueTable.NAME + "=?",
                new String[] { Settings.Secure.ANDROID_ID }, null,
                userHandle);
        try {
            final String value = c.moveToNext() ? c.getString(0) : null;
            if (value == null) {
                // sanity-check the user before touching the db
                final UserInfo user = mUserManager.getUserInfo(userHandle);
                if (user == null) {
                    // can happen due to races when deleting users; treat as benign
                    return false;
                }

                final SecureRandom random = new SecureRandom();
                final String newAndroidIdValue = Long.toHexString(random.nextLong());
                final ContentValues values = new ContentValues();
                values.put(Settings.NameValueTable.NAME, Settings.Secure.ANDROID_ID);
                values.put(Settings.NameValueTable.VALUE, newAndroidIdValue);
                final Uri uri = insertForUser(Settings.Secure.CONTENT_URI, values, userHandle);
                if (uri == null) {
                    Slog.e(TAG, "Unable to generate new ANDROID_ID for user " + userHandle);
                    return false;
                }
                Slog.d(TAG, "Generated and saved new ANDROID_ID [" + newAndroidIdValue
                        + "] for user " + userHandle);
                // Write a dropbox entry if it's a restricted profile
                if (user.isRestricted()) {
                    DropBoxManager dbm = (DropBoxManager)
                            getContext().getSystemService(Context.DROPBOX_SERVICE);
                    if (dbm != null && dbm.isTagEnabled(DROPBOX_TAG_USERLOG)) {
                        dbm.addText(DROPBOX_TAG_USERLOG, System.currentTimeMillis()
                                + ",restricted_profile_ssaid,"
                                + newAndroidIdValue + "\n");
                    }
                }
            }
            return true;
        } finally {
            c.close();
        }
    }

5、为用户将Settings中的内容,更新到SettingsCache中;
private void startAsyncCachePopulation(int userHandle) {
        new CachePrefetchThread(userHandle).start();
    }


Android的SettingsProvider是一个系统级应用程序,用于管理设备的设置信息。当Android系统升级时,SettingsProvider也会相应地进行升级。 首先,升级会带来一些新的设置选项。随着Android版本的更新,新的功能和特性将被引入到系统中,这些功能和特性将需要对应的设置选项来进行配置。因此,SettingsProvider需要进行升级,以支持这些新的设置选项。 其次,升级还会修复一些已知的问题和漏洞。在旧版本的SettingsProvider中可能存在一些bug或者安全漏洞,这些问题会被逐步修复和解决。升级后的SettingsProvider将包含修复后的代码,以提高系统的稳定性和安全性。 此外,升级还可能会改变SettingsProvider的数据结构或存储方式。为了满足新的需求和功能,SettingsProvider的数据存储可能需要进行调整和优化。这可能涉及到数据库表结构的更改或者数据存储的格式改变等。 最后,升级还可能会提供更好的性能和响应速度。随着系统的发展,我们对设备的性能和用户体验的要求也在不断提高。升级后的SettingsProvider可能会经过优化,以提供更快的数据查询和更新速度,从而使用户的设置操作更加流畅和高效。 综上所述,Android系统的升级会带来SettingsProvider的升级,以支持新的设置选项、修复问题和漏洞、改变数据结构和存储方式,并提供更好的性能和响应速度。这些升级将不断提高Android设备的功能和使用体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hailushijie

您的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值