[core] Invalidate cache when pm is dead while caching (#1007)
This commit is contained in:
parent
cc1168bad9
commit
f2d4092589
|
|
@ -84,8 +84,6 @@ public class ConfigManager {
|
||||||
private final SQLiteDatabase db =
|
private final SQLiteDatabase db =
|
||||||
SQLiteDatabase.openOrCreateDatabase(ConfigFileManager.dbPath, null);
|
SQLiteDatabase.openOrCreateDatabase(ConfigFileManager.dbPath, null);
|
||||||
|
|
||||||
private boolean packageStarted = false;
|
|
||||||
|
|
||||||
private boolean resourceHook = false;
|
private boolean resourceHook = false;
|
||||||
private boolean verboseLog = true;
|
private boolean verboseLog = true;
|
||||||
private String miscPath = null;
|
private String miscPath = null;
|
||||||
|
|
@ -236,7 +234,7 @@ public class ConfigManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void updateManager() {
|
public synchronized void updateManager() {
|
||||||
if (!packageStarted) return;
|
if (!PackageService.isAlive()) return;
|
||||||
try {
|
try {
|
||||||
PackageInfo info = PackageService.getPackageInfo(manager, 0, 0);
|
PackageInfo info = PackageService.getPackageInfo(manager, 0, 0);
|
||||||
if (info != null) {
|
if (info != null) {
|
||||||
|
|
@ -249,7 +247,7 @@ public class ConfigManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ensureManager() {
|
public void ensureManager() {
|
||||||
if (!packageStarted) return;
|
if (!PackageService.isAlive()) return;
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
if (PackageService.installManagerIfAbsent(manager, ConfigFileManager.managerApkPath)) {
|
if (PackageService.installManagerIfAbsent(manager, ConfigFileManager.managerApkPath)) {
|
||||||
updateManager();
|
updateManager();
|
||||||
|
|
@ -260,10 +258,9 @@ public class ConfigManager {
|
||||||
static ConfigManager getInstance() {
|
static ConfigManager getInstance() {
|
||||||
if (instance == null)
|
if (instance == null)
|
||||||
instance = new ConfigManager();
|
instance = new ConfigManager();
|
||||||
if (!instance.packageStarted) {
|
if (instance.lastModuleCacheTime == 0 || instance.lastScopeCacheTime == 0) {
|
||||||
if (PackageService.getPackageManager() != null) {
|
if (PackageService.isAlive()) {
|
||||||
Log.d(TAG, "pm is ready, updating cache");
|
Log.d(TAG, "pm is ready, updating cache");
|
||||||
instance.packageStarted = true;
|
|
||||||
// must ensure cache is valid for later usage
|
// must ensure cache is valid for later usage
|
||||||
instance.updateCaches(true);
|
instance.updateCaches(true);
|
||||||
instance.updateManager();
|
instance.updateManager();
|
||||||
|
|
@ -351,9 +348,16 @@ public class ConfigManager {
|
||||||
return config.getOrDefault(group, new ConcurrentHashMap<>());
|
return config.getOrDefault(group, new ConcurrentHashMap<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private synchronized void clearCache() {
|
||||||
|
lastScopeCacheTime = 0;
|
||||||
|
lastModuleCacheTime = 0;
|
||||||
|
cachedModule.clear();
|
||||||
|
cachedScope.clear();
|
||||||
|
}
|
||||||
|
|
||||||
private synchronized void cacheModules() {
|
private synchronized void cacheModules() {
|
||||||
// skip caching when pm is not yet available
|
// skip caching when pm is not yet available
|
||||||
if (!packageStarted) return;
|
if (!PackageService.isAlive()) return;
|
||||||
if (lastModuleCacheTime >= requestModuleCacheTime) return;
|
if (lastModuleCacheTime >= requestModuleCacheTime) return;
|
||||||
else lastModuleCacheTime = SystemClock.elapsedRealtime();
|
else lastModuleCacheTime = SystemClock.elapsedRealtime();
|
||||||
try (Cursor cursor = db.query(true, "modules", new String[]{"module_pkg_name", "apk_path"},
|
try (Cursor cursor = db.query(true, "modules", new String[]{"module_pkg_name", "apk_path"},
|
||||||
|
|
@ -413,8 +417,14 @@ public class ConfigManager {
|
||||||
module.appId = pkgInfo.applicationInfo.uid;
|
module.appId = pkgInfo.applicationInfo.uid;
|
||||||
cachedModule.put(packageName, module);
|
cachedModule.put(packageName, module);
|
||||||
}
|
}
|
||||||
obsoleteModules.forEach(this::removeModuleWithoutCache);
|
if (PackageService.isAlive()) {
|
||||||
obsoletePaths.forEach(this::updateModuleApkPath);
|
obsoleteModules.forEach(this::removeModuleWithoutCache);
|
||||||
|
obsoletePaths.forEach(this::updateModuleApkPath);
|
||||||
|
} else {
|
||||||
|
Log.w(TAG, "pm is dead while caching. invalidating...");
|
||||||
|
clearCache();
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Log.d(TAG, "cached modules");
|
Log.d(TAG, "cached modules");
|
||||||
for (String module : cachedModule.keySet()) {
|
for (String module : cachedModule.keySet()) {
|
||||||
|
|
@ -425,7 +435,7 @@ public class ConfigManager {
|
||||||
|
|
||||||
private synchronized void cacheScopes() {
|
private synchronized void cacheScopes() {
|
||||||
// skip caching when pm is not yet available
|
// skip caching when pm is not yet available
|
||||||
if (!packageStarted) return;
|
if (!PackageService.isAlive()) return;
|
||||||
if (lastScopeCacheTime >= requestScopeCacheTime) return;
|
if (lastScopeCacheTime >= requestScopeCacheTime) return;
|
||||||
else lastScopeCacheTime = SystemClock.elapsedRealtime();
|
else lastScopeCacheTime = SystemClock.elapsedRealtime();
|
||||||
cachedScope.clear();
|
cachedScope.clear();
|
||||||
|
|
@ -489,13 +499,19 @@ public class ConfigManager {
|
||||||
Log.e(TAG, Log.getStackTraceString(e));
|
Log.e(TAG, Log.getStackTraceString(e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (Application obsoletePackage : obsoletePackages) {
|
if (PackageService.isAlive()) {
|
||||||
Log.d(TAG, "removing obsolete package: " + obsoletePackage.packageName + "/" + obsoletePackage.userId);
|
for (Application obsoletePackage : obsoletePackages) {
|
||||||
removeAppWithoutCache(obsoletePackage);
|
Log.d(TAG, "removing obsolete package: " + obsoletePackage.packageName + "/" + obsoletePackage.userId);
|
||||||
}
|
removeAppWithoutCache(obsoletePackage);
|
||||||
for (Application obsoleteModule : obsoleteModules) {
|
}
|
||||||
Log.d(TAG, "removing obsolete module: " + obsoleteModule.packageName + "/" + obsoleteModule.userId);
|
for (Application obsoleteModule : obsoleteModules) {
|
||||||
removeModuleScopeWithoutCache(obsoleteModule);
|
Log.d(TAG, "removing obsolete module: " + obsoleteModule.packageName + "/" + obsoleteModule.userId);
|
||||||
|
removeModuleScopeWithoutCache(obsoleteModule);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Log.w(TAG, "pm is dead while caching. invalidating...");
|
||||||
|
clearCache();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Log.d(TAG, "cached Scope");
|
Log.d(TAG, "cached Scope");
|
||||||
|
|
|
||||||
|
|
@ -79,6 +79,11 @@ public class PackageService {
|
||||||
|
|
||||||
private static IPackageManager pm = null;
|
private static IPackageManager pm = null;
|
||||||
private static IBinder binder = null;
|
private static IBinder binder = null;
|
||||||
|
|
||||||
|
static boolean isAlive() {
|
||||||
|
return binder != null && binder.isBinderAlive();
|
||||||
|
}
|
||||||
|
|
||||||
private static final IBinder.DeathRecipient recipient = new IBinder.DeathRecipient() {
|
private static final IBinder.DeathRecipient recipient = new IBinder.DeathRecipient() {
|
||||||
@Override
|
@Override
|
||||||
public void binderDied() {
|
public void binderDied() {
|
||||||
|
|
@ -89,7 +94,7 @@ public class PackageService {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
public static IPackageManager getPackageManager() {
|
private static IPackageManager getPackageManager() {
|
||||||
if (binder == null && pm == null) {
|
if (binder == null && pm == null) {
|
||||||
binder = ServiceManager.getService("package");
|
binder = ServiceManager.getService("package");
|
||||||
if (binder == null) return null;
|
if (binder == null) return null;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue