diff --git a/app/src/main/java/com/fox2code/mmm/MainApplication.java b/app/src/main/java/com/fox2code/mmm/MainApplication.java index 1a5d921..8333d37 100644 --- a/app/src/main/java/com/fox2code/mmm/MainApplication.java +++ b/app/src/main/java/com/fox2code/mmm/MainApplication.java @@ -68,7 +68,7 @@ public class MainApplication extends CompatApplication { } public MainApplication() { - if (INSTANCE != null) + if (INSTANCE != null && INSTANCE != this) throw new IllegalStateException("Duplicate application instance!"); INSTANCE = this; } @@ -280,6 +280,7 @@ public class MainApplication extends CompatApplication { @Override public void onCreate() { + if (INSTANCE == null) INSTANCE = this; super.onCreate(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { if (MainApplication.isMonetEnabled()) { diff --git a/app/src/main/java/com/fox2code/mmm/XHooks.java b/app/src/main/java/com/fox2code/mmm/XHooks.java index d22cc5b..88b60df 100644 --- a/app/src/main/java/com/fox2code/mmm/XHooks.java +++ b/app/src/main/java/com/fox2code/mmm/XHooks.java @@ -16,6 +16,9 @@ import com.fox2code.mmm.repo.RepoManager; */ @Keep public class XHooks { + @Keep + public static void onRepoManagerInitialized() {} + @Keep public static boolean isModuleActive(String moduleId) { return ModuleManager.isModuleActive(moduleId); diff --git a/app/src/main/java/com/fox2code/mmm/repo/RepoManager.java b/app/src/main/java/com/fox2code/mmm/repo/RepoManager.java index 413533e..663a88b 100644 --- a/app/src/main/java/com/fox2code/mmm/repo/RepoManager.java +++ b/app/src/main/java/com/fox2code/mmm/repo/RepoManager.java @@ -5,6 +5,7 @@ import android.content.SharedPreferences; import android.util.Log; import com.fox2code.mmm.MainApplication; +import com.fox2code.mmm.XHooks; import com.fox2code.mmm.androidacy.AndroidacyRepoData; import com.fox2code.mmm.manager.ModuleInfo; import com.fox2code.mmm.utils.Files; @@ -45,6 +46,7 @@ public final class RepoManager { MainApplication mainApplication = MainApplication.getINSTANCE(); if (mainApplication != null) { INSTANCE = new RepoManager(mainApplication); + XHooks.onRepoManagerInitialized(); } else { throw new RuntimeException("Getting RepoManager too soon!"); } @@ -58,8 +60,10 @@ public final class RepoManager { private final LinkedHashMap repoData; private final HashMap modules; private final AndroidacyRepoData androidacyRepoData; + private boolean initialized; private RepoManager(MainApplication mainApplication) { + this.initialized = false; this.mainApplication = mainApplication; this.repoData = new LinkedHashMap<>(); this.modules = new HashMap<>(); @@ -69,19 +73,24 @@ public final class RepoManager { this.addAndroidacyRepoData(); // Populate default cache for (RepoData repoData:this.repoData.values()) { - for (RepoModule repoModule:repoData.moduleHashMap.values()) { - if (!repoModule.moduleInfo.hasFlag(ModuleInfo.FLAG_METADATA_INVALID)) { - RepoModule registeredRepoModule = this.modules.get(repoModule.id); - if (registeredRepoModule == null) { - this.modules.put(repoModule.id, repoModule); - } else if (repoModule.moduleInfo.versionCode > - registeredRepoModule.moduleInfo.versionCode) { - this.modules.put(repoModule.id, repoModule); - } - } else { - Log.e(TAG, "Detected module with invalid metadata: " + - repoModule.repoName + "/" + repoModule.id); + this.populateDefaultCache(repoData); + } + this.initialized = true; + } + + private void populateDefaultCache(RepoData repoData) { + for (RepoModule repoModule:repoData.moduleHashMap.values()) { + if (!repoModule.moduleInfo.hasFlag(ModuleInfo.FLAG_METADATA_INVALID)) { + RepoModule registeredRepoModule = this.modules.get(repoModule.id); + if (registeredRepoModule == null) { + this.modules.put(repoModule.id, repoModule); + } else if (repoModule.moduleInfo.versionCode > + registeredRepoModule.moduleInfo.versionCode) { + this.modules.put(repoModule.id, repoModule); } + } else { + Log.e(TAG, "Detected module with invalid metadata: " + + repoModule.repoName + "/" + repoModule.id); } } } @@ -121,18 +130,18 @@ public final class RepoManager { return this.repoUpdating; } - public final void afterUpdate() { + public void afterUpdate() { if (this.repoUpdating) synchronized (this.repoUpdateLock) {} } - public final void runAfterUpdate(Runnable runnable) { + public void runAfterUpdate(Runnable runnable) { synchronized (this.repoUpdateLock) { runnable.run(); } } // MultiThread friendly method - public final void update(UpdateListener updateListener) { + public void update(UpdateListener updateListener) { if (!this.repoUpdating) { // Do scan synchronized (this.repoUpdateLock) { @@ -255,6 +264,9 @@ public final class RepoManager { .getSharedPreferences("mmm_" + id, Context.MODE_PRIVATE); RepoData repoData = new RepoData(url, cacheRoot, sharedPreferences); this.repoData.put(url, repoData); + if (this.initialized) { + this.populateDefaultCache(repoData); + } return repoData; }