From beb4925ff3d12c36f248e3447de1e6cac62a0231 Mon Sep 17 00:00:00 2001 From: Fox2Code Date: Mon, 5 Sep 2022 11:32:19 +0200 Subject: [PATCH] Improve application Thread safety. --- .../background/BackgroundBootListener.java | 14 ------- .../background/BackgroundUpdateChecker.java | 42 ++++++++++--------- .../mmm/module/ModuleViewListBuilder.java | 2 + .../com/fox2code/mmm/utils/SyncManager.java | 6 +++ 4 files changed, 30 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/com/fox2code/mmm/background/BackgroundBootListener.java b/app/src/main/java/com/fox2code/mmm/background/BackgroundBootListener.java index d45197a..dd2a703 100644 --- a/app/src/main/java/com/fox2code/mmm/background/BackgroundBootListener.java +++ b/app/src/main/java/com/fox2code/mmm/background/BackgroundBootListener.java @@ -5,8 +5,6 @@ import android.content.Context; import android.content.Intent; import com.fox2code.mmm.MainApplication; -import com.fox2code.mmm.installer.InstallerInitializer; -import com.fox2code.mmm.manager.ModuleManager; public class BackgroundBootListener extends BroadcastReceiver { private static final String BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED"; @@ -20,17 +18,5 @@ public class BackgroundBootListener extends BroadcastReceiver { BackgroundUpdateChecker.doCheck(context); } } - if (ModuleManager.FORCE_NEED_FALLBACK && - MainApplication.hasGottenRootAccess()) { - InstallerInitializer.tryGetMagiskPathAsync(new InstallerInitializer.Callback() { - @Override - public void onPathReceived(String path) { - ModuleManager.getINSTANCE().scan(); - } - - @Override - public void onFailure(int error) {} - }); - } } } diff --git a/app/src/main/java/com/fox2code/mmm/background/BackgroundUpdateChecker.java b/app/src/main/java/com/fox2code/mmm/background/BackgroundUpdateChecker.java index f1a4974..50d08fb 100644 --- a/app/src/main/java/com/fox2code/mmm/background/BackgroundUpdateChecker.java +++ b/app/src/main/java/com/fox2code/mmm/background/BackgroundUpdateChecker.java @@ -54,28 +54,30 @@ public class BackgroundUpdateChecker extends Worker { static void doCheck(Context context) { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); + ModuleManager.getINSTANCE().scanAsync(); RepoManager.getINSTANCE().update(null); - ModuleManager.getINSTANCE().scan(); - int moduleUpdateCount = 0; - HashMap repoModules = - RepoManager.getINSTANCE().getModules(); - for (LocalModuleInfo localModuleInfo : - ModuleManager.getINSTANCE().getModules().values()) { - if ("twrp-keep".equals(localModuleInfo.id)) continue; - RepoModule repoModule = repoModules.get(localModuleInfo.id); - localModuleInfo.checkModuleUpdate(); - if (localModuleInfo.updateVersionCode > localModuleInfo.versionCode && - !PropUtils.isNullString(localModuleInfo.updateVersion)) { - moduleUpdateCount++; - } else if (repoModule != null && - repoModule.moduleInfo.versionCode > localModuleInfo.versionCode && - !PropUtils.isNullString(repoModule.moduleInfo.version)) { - moduleUpdateCount++; + ModuleManager.getINSTANCE().runAfterScan(() -> { + int moduleUpdateCount = 0; + HashMap repoModules = + RepoManager.getINSTANCE().getModules(); + for (LocalModuleInfo localModuleInfo : + ModuleManager.getINSTANCE().getModules().values()) { + if ("twrp-keep".equals(localModuleInfo.id)) continue; + RepoModule repoModule = repoModules.get(localModuleInfo.id); + localModuleInfo.checkModuleUpdate(); + if (localModuleInfo.updateVersionCode > localModuleInfo.versionCode && + !PropUtils.isNullString(localModuleInfo.updateVersion)) { + moduleUpdateCount++; + } else if (repoModule != null && + repoModule.moduleInfo.versionCode > localModuleInfo.versionCode && + !PropUtils.isNullString(repoModule.moduleInfo.version)) { + moduleUpdateCount++; + } } - } - if (moduleUpdateCount != 0) { - postNotification(context, moduleUpdateCount); - } + if (moduleUpdateCount != 0) { + postNotification(context, moduleUpdateCount); + } + }); } public static void postNotification(Context context, int updateCount) { diff --git a/app/src/main/java/com/fox2code/mmm/module/ModuleViewListBuilder.java b/app/src/main/java/com/fox2code/mmm/module/ModuleViewListBuilder.java index bb163ed..ba91b4b 100644 --- a/app/src/main/java/com/fox2code/mmm/module/ModuleViewListBuilder.java +++ b/app/src/main/java/com/fox2code/mmm/module/ModuleViewListBuilder.java @@ -114,6 +114,8 @@ public class ModuleViewListBuilder { public void applyTo(final RecyclerView moduleList,final ModuleViewAdapter moduleViewAdapter) { if (this.updating) return; this.updating = true; + ModuleManager.getINSTANCE().afterScan(); + RepoManager.getINSTANCE().afterUpdate(); final ArrayList moduleHolders; final int newNotificationsLen; final boolean first; diff --git a/app/src/main/java/com/fox2code/mmm/utils/SyncManager.java b/app/src/main/java/com/fox2code/mmm/utils/SyncManager.java index 0f0d327..f0d85c8 100644 --- a/app/src/main/java/com/fox2code/mmm/utils/SyncManager.java +++ b/app/src/main/java/com/fox2code/mmm/utils/SyncManager.java @@ -15,6 +15,12 @@ public abstract class SyncManager { private boolean syncing; private long lastSync; + public final void scanAsync() { + if (!this.syncing) { + new Thread(this::scan, "Scan Thread").start(); + } + } + public final void scan() { this.update(null); }