diff --git a/mobile/android/app/src/main/kotlin/app/alextran/immich/ImmichApp.kt b/mobile/android/app/src/main/kotlin/app/alextran/immich/ImmichApp.kt index 5a3b0e1f3d..4474c63e09 100644 --- a/mobile/android/app/src/main/kotlin/app/alextran/immich/ImmichApp.kt +++ b/mobile/android/app/src/main/kotlin/app/alextran/immich/ImmichApp.kt @@ -1,8 +1,11 @@ package app.alextran.immich import android.app.Application +import android.os.Handler +import android.os.Looper import androidx.work.Configuration import androidx.work.WorkManager +import app.alextran.immich.background.BackgroundEngineLock import app.alextran.immich.background.BackgroundWorkerApiImpl class ImmichApp : Application() { @@ -17,6 +20,11 @@ class ImmichApp : Application() { // As a workaround, we also run a backup check when initializing the application ContentObserverWorker.startBackupWorker(context = this, delayMilliseconds = 0) - BackgroundWorkerApiImpl.enqueueBackgroundWorker(this) + Handler(Looper.getMainLooper()).postDelayed({ + // We can only check the engine count and not the status of the lock here, + // as the previous start might have been killed without unlocking. + if (BackgroundEngineLock.connectEngines > 0) return@postDelayed + BackgroundWorkerApiImpl.enqueueBackgroundWorker(this) + }, 5000) } } diff --git a/mobile/android/app/src/main/kotlin/app/alextran/immich/background/BackgroundEngineLock.kt b/mobile/android/app/src/main/kotlin/app/alextran/immich/background/BackgroundEngineLock.kt index d8afe32b5c..504267a4e5 100644 --- a/mobile/android/app/src/main/kotlin/app/alextran/immich/background/BackgroundEngineLock.kt +++ b/mobile/android/app/src/main/kotlin/app/alextran/immich/background/BackgroundEngineLock.kt @@ -8,43 +8,46 @@ import java.util.concurrent.atomic.AtomicInteger private const val TAG = "BackgroundEngineLock" class BackgroundEngineLock(context: Context) : BackgroundWorkerLockApi, FlutterPlugin { - private val ctx: Context = context.applicationContext + private val ctx: Context = context.applicationContext - companion object { + companion object { - private var engineCount = AtomicInteger(0) + private var engineCount = AtomicInteger(0) - private fun checkAndEnforceBackgroundLock(ctx: Context) { - // work manager task is running while the main app is opened, cancel the worker - if (BackgroundWorkerPreferences(ctx).isLocked() && - engineCount.get() > 1 && - BackgroundWorkerApiImpl.isBackgroundWorkerRunning() - ) { - Log.i(TAG, "Background worker is locked, cancelling the background worker") - BackgroundWorkerApiImpl.cancelBackgroundWorker(ctx) - } - } + val connectEngines: Int + get() = engineCount.get() + + private fun checkAndEnforceBackgroundLock(ctx: Context) { + // work manager task is running while the main app is opened, cancel the worker + if (BackgroundWorkerPreferences(ctx).isLocked() && + connectEngines > 1 && + BackgroundWorkerApiImpl.isBackgroundWorkerRunning() + ) { + Log.i(TAG, "Background worker is locked, cancelling the background worker") + BackgroundWorkerApiImpl.cancelBackgroundWorker(ctx) + } } + } - override fun lock() { - BackgroundWorkerPreferences(ctx).setLocked(true) - checkAndEnforceBackgroundLock(ctx) - Log.i(TAG, "Background worker is locked") - } + override fun lock() { + BackgroundWorkerPreferences(ctx).setLocked(true) + checkAndEnforceBackgroundLock(ctx) + Log.i(TAG, "Background worker is locked") + } - override fun unlock() { - BackgroundWorkerPreferences(ctx).setLocked(false) - Log.i(TAG, "Background worker is unlocked") - } + override fun unlock() { + BackgroundWorkerPreferences(ctx).setLocked(false) + Log.i(TAG, "Background worker is unlocked") + } - override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) { - checkAndEnforceBackgroundLock(binding.applicationContext) - engineCount.incrementAndGet() - Log.i(TAG, "Flutter engine attached. Attached Engines count: $engineCount") - } + override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) { + checkAndEnforceBackgroundLock(binding.applicationContext) + engineCount.incrementAndGet() + Log.i(TAG, "Flutter engine attached. Attached Engines count: $engineCount") + } - override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { - engineCount.decrementAndGet() - Log.i(TAG, "Flutter engine detached. Attached Engines count: $engineCount") - } + override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { + engineCount.decrementAndGet() + Log.i(TAG, "Flutter engine detached. Attached Engines count: $engineCount") + } }