标签:settingsprovider settingsprovider之增删改
转载请注明出处:http://blog.csdn.net/droyon/article/details/35558697
当delete或者update时,需要清空缓冲区并重新加载数据。
1、invalidateCache()//得到当前用户的SettingsCache,remove所有。
public SettingsCache cacheForTable(final int callingUser, String tableName) {
if (TABLE_SYSTEM.equals(tableName)) {
return getOrConstructCache(callingUser, sSystemCaches);
}
if (TABLE_SECURE.equals(tableName)) {
return getOrConstructCache(callingUser, sSecureCaches);
}
if (TABLE_GLOBAL.equals(tableName)) {
return sGlobalCache;
}
return null;
}二、bulkInsert
public int bulkInsert(Uri uri, ContentValues[] values) {
final int callingUser = UserHandle.getCallingUserId();//得到请求用户id
SettingsCache cache = cacheForTable(callingUser, args.table);//得到SettingsCache
int numValues = values.length;
for (int i = 0; i < numValues; i++) {
if (db.insert(args.table, null, values[i]) < 0) return 0;
SettingsCache.populate(cache, values[i]);
if (LOCAL_LOGV) Log.v(TAG, args.table + " <- " + values[i]);
}
得到SettingsCache(getOrConstructCache ----》 getOrEstablishDatabase ----》 establishDbTracking ---》 startAsyncCachePopulation(userHandle);)
1、if (TABLE_SYSTEM.equals(tableName)) {
return getOrConstructCache(callingUser, sSystemCaches);
}
if (TABLE_SECURE.equals(tableName)) {
return getOrConstructCache(callingUser, sSecureCaches);
}
if (TABLE_GLOBAL.equals(tableName)) {
return sGlobalCache;
}
2、private SettingsCache getOrConstructCache(int callingUser, SparseArray<SettingsCache> which) {
getOrEstablishDatabase(callingUser); // ignore return value; we don't need it
return which.get(callingUser);
}
3、private DatabaseHelper getOrEstablishDatabase(int callingUser) {
if (callingUser >= Process.SYSTEM_UID) {
if (USER_CHECK_THROWS) {
throw new IllegalArgumentException("Uid rather than user handle: " + callingUser);
} else {
Slog.wtf(TAG, "establish db for uid rather than user: " + callingUser);
}
}
long oldId = Binder.clearCallingIdentity();
try {
DatabaseHelper dbHelper = mOpenHelpers.get(callingUser);
if (null == dbHelper) {
establishDbTracking(callingUser);
dbHelper = mOpenHelpers.get(callingUser);
}
return dbHelper;
} finally {
Binder.restoreCallingIdentity(oldId);
}
}
4、private void startAsyncCachePopulation(int userHandle) {
new CachePrefetchThread(userHandle).start();
}
class CachePrefetchThread extends Thread {
private int mUserHandle;
CachePrefetchThread(int userHandle) {
super("populate-settings-caches");
mUserHandle = userHandle;
}
@Override
public void run() {
fullyPopulateCaches(mUserHandle);
}
}
5、fullyPopulateCaches
private void fullyPopulateCaches(final int userHandle) {
DatabaseHelper dbHelper = mOpenHelpers.get(userHandle);
// Only populate the globals cache once, for the owning user
if (userHandle == UserHandle.USER_OWNER) {
fullyPopulateCache(dbHelper, TABLE_GLOBAL, sGlobalCache);
}
fullyPopulateCache(dbHelper, TABLE_SECURE, sSecureCaches.get(userHandle));
fullyPopulateCache(dbHelper, TABLE_SYSTEM, sSystemCaches.get(userHandle));
}
得到DatabaseHelper。
DatabaseHelper dbH = getOrEstablishDatabase(
TABLE_GLOBAL.equals(args.table) ? UserHandle.USER_OWNER : callingUser);
插入到数据库,更新数据到SettingsCache。
for (int i = 0; i < numValues; i++) {
if (db.insert(args.table, null, values[i]) < 0) return 0;
SettingsCache.populate(cache, values[i]);
if (LOCAL_LOGV) Log.v(TAG, args.table + " <- " + values[i]);
}
发送通知。
sendNotify(uri, callingUser);
四、delete
重新填充SettingsCache缓冲区。(startAsyncCachePopulation(callingUser); ---》 new CachePrefetchThread(userHandle).start();)
startAsyncCachePopulation(callingUser);
五、update
重新填充SettingsCache缓冲区。
startAsyncCachePopulation(callingUser);
六、query ---》 queryForUser
SettingsProvider之增删改查,布布扣,bubuko.com
标签:settingsprovider settingsprovider之增删改
原文地址:http://blog.csdn.net/droyon/article/details/35558697