check whether app is in foreground before starting service

This commit is contained in:
Thibault Deckers 2024-10-13 23:33:46 +02:00
parent bb89756815
commit ee4dd38bbe
2 changed files with 12 additions and 1 deletions

View file

@ -1,5 +1,6 @@
package deckers.thibault.aves.channel.calls
import android.app.ActivityManager
import android.content.Context
import androidx.work.ExistingWorkPolicy
import androidx.work.OneTimeWorkRequestBuilder
@ -51,6 +52,17 @@ class AnalysisHandler(private val activity: FlutterFragmentActivity, private val
return
}
val activityManager: ActivityManager = activity.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val runningAppProcesses = activityManager.runningAppProcesses
if (runningAppProcesses != null) {
val importance = runningAppProcesses[0].importance
if (importance < ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
// the app is in the background
result.error("startAnalysis-background", "app is in the background (process importance=$importance)", null)
return
}
}
// can be null or empty
val allEntryIds = call.argument<List<Int>>("entryIds")

View file

@ -495,7 +495,6 @@ abstract class CollectionSource with SourceBase, AlbumMixin, CountryMixin, Place
}
}
}
// TODO TLAD check whether app is in foreground before starting service
if (startAnalysisService) {
await AnalysisService.startService(
force: force,