Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to run scheduled exports in foreground service where possible #131

Merged
merged 2 commits into from
Nov 13, 2023
Merged

Try to run scheduled exports in foreground service where possible #131

merged 2 commits into from
Nov 13, 2023

Conversation

chenxiaolong
Copy link
Contributor

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 inside WorkManager's foreground service. Unfortunately, on Android 12+, this is not allowed by default because ExportWorker's startup method of using a delay on a OneTimeWorkRequestBuilder 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

@chenxiaolong
Copy link
Contributor Author

I've tested this with Android 14 on a Pixel device, but unfortunately, I don't have any devices running Android <=13 that are capable of calls/SMS. I'd appreciate it if someone could help test there to make sure I haven't broken anything.

To manually trigger the scheduled job:

  1. Enable scheduled exports from the UI.

  2. Get the job ID by running:

    adb shell dumpsys jobscheduler | grep 'JOB #.*sms_ie'

    It will print out something like the following. The number after the first slash is the job ID (8 in this example).

      JOB #u0a312/8: c66a305 com.github.tmo1.sms_ie/androidx.work.impl.background.systemjob.SystemJobService
    
  3. Trigger the job ID.

    adb shell cmd jobscheduler run -f com.github.tmo1.sms_ie <job ID>

    Note that the ID will change after every run.

@AndroFlipper
Copy link

Trying it tonight (Android 11). Will let you know...

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 inside
WorkManager's foreground service. Unfortunately, on Android 12+, this is
not allowed by default because ExportWorker's startup method of using a
delay on a OneTimeWorkRequestBuilder 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

Signed-off-by: Andrew Gunnerson <[email protected]>
This fixes a crash on older Android versions caused by findPreference
calls to items from the scheduled exports category, which are removed
earlier in the function.

Signed-off-by: Andrew Gunnerson <[email protected]>
@tmo1 tmo1 merged commit eb6b379 into tmo1:master Nov 13, 2023
1 check passed
@chenxiaolong chenxiaolong deleted the android-14 branch November 13, 2023 02:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ExportWorker gets killed by Android 14 due to new excessive binder call tracker feature
3 participants