Try to run scheduled exports in foreground service where possible #131
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Android 14 introduced a change in the
CachedAppOptimizer
component that kills apps that perform too many binder transactions in the background [1]. This can happen where there are many messages to export (2000 or so from empirical testing).This restriction does not apply to foreground services, so this commit adds support for opportunistically running
ExportWorker
insideWorkManager
's foreground service. Unfortunately, on Android 12+, this is not allowed by default becauseExportWorker
's startup method of using a delay on aOneTimeWorkRequestBuilder
is not one of the exemptions for running a foreground service.To allow a foreground service to run, the user must disable Android's battery optimizations for the app. This commit adds a toggle to do so in the sms-ie's own settings so the user doesn't need to dig into Android's Settings to find the option. If foreground service access is not available (because eg. battery optimizations are enabled or the app is running on an old version of Android), then the worker will fall back to running in a plain old background service like before.
[1] https://android.googlesource.com/platform/frameworks/base.git/+/71d75c09b9a06732a6edb4d1488d2aa3eb779e14%5E%21/
[2] https://developer.android.com/guide/components/foreground-services#background-start-restriction-exemptions
Fixes: #129