Skip to content

Core: Improve background execution reliability - #1273

Merged
d4rken merged 3 commits into
mainfrom
foreground-execution
Jul 5, 2024
Merged

d4rken merged 3 commits into
mainfrom
foreground-execution

Conversation

@d4rken

@d4rken d4rken commented Jul 5, 2024

Copy link
Copy Markdown
Member

Fixes #1247

d4rken added 3 commits July 5, 2024 11:19
Declare `FOREGROUND_SERVICE_DATA_SYNC`, which is the type declared for the `WorkManager` but we didn't request the permission.
Though this permission may have been superseded by `FOREGROUND_SERVICE_SPECIAL_USE` from the `RecorderService` already...
This may throw `ForegroundServiceStartNotAllowedException`
I don't see why we didn't use this, seems to have no downside during testing.
@d4rken d4rken added bug Something isn't working as expected c: Scheduler labels Jul 5, 2024
@d4rken
d4rken merged commit 103f240 into main Jul 5, 2024
@d4rken
d4rken deleted the foreground-execution branch July 5, 2024 11:23
d4rken added a commit that referenced this pull request Jul 5, 2024
Show a hint about disabling battery optimizations on newer Android versions where this may affect launching a foreground task, e.g. Android 14+.

I tested various API levels and starting with Android 12 (API31), we get an exception when starting foreground services while optimizations are enabled.

We didn't use `REQUEST_IGNORE_BATTERY_OPTIMIZATIONS` and shouldn't as black box Google Play is potentially allergic to it's use.
Do the safe way of directing the user to the system settings.

Also see #1273 and #1247

```
14:15:39.704 SDMSE:Scheduler:Worker               D  Declaring as foreground task
14:15:39.704 SDMSE:Scheduler:Worker               D  Supplying getForegroundInfo() with Schedule(id=96b7c483-863d-4c9f-9baa-b5e1b6083ddc, createdAt=2024-07-05T12:14:14.886Z, scheduledAt=2024-07-05T12:14:14.886Z, hour=14, minute=15, label=Test Schedule 96b7c483-863d-4c9f-9baa-b5e1b6083ddc, repeatInterval=PT24H, useCorpseFinder=false, useSystemCleaner=false, useAppCleaner=false, commandsAfterSchedule=[], executedAt=null)
14:15:39.705 WM-Processor                         I  Moving WorkSpec (5bb950de-d2c4-4622-91bf-58408fbbbf8c) to the foreground
14:15:39.707 ActivityManager                      W  Background started FGS: Disallowed [callingPackage: eu.darken.sdmse; callingUid: 10149; uidState: CEM ; intent: Intent { act=ACTION_START_FOREGROUND cmp=eu.darken.sdmse/androidx.work.impl.foreground.SystemForegroundService (has extras) }; code:DENIED; tempAllowListReason:<null>; targetSdkVersion:34; callerTargetSdkVersion:34; startForegroundCount:0; bindFromPackage:null]
14:15:39.707 ActivityManager                      W  startForegroundService() not allowed due to mAllowStartForeground false: service eu.darken.sdmse/androidx.work.impl.foreground.SystemForegroundService
14:15:39.708 SDMSE:Scheduler:Worker               E  Can't execute in foreground: android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service eu.darken.sdmse/androidx.work.impl.foreground.SystemForegroundService
                                                     	at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:54)
                                                     	at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:50)
                                                     	at android.os.Parcel.readParcelable(Parcel.java:3334)
                                                     	at android.os.Parcel.createExceptionOrNull(Parcel.java:2421)
                                                     	at android.os.Parcel.createException(Parcel.java:2410)
                                                     	at android.os.Parcel.readException(Parcel.java:2393)
                                                     	at android.os.Parcel.readException(Parcel.java:2335)
                                                     	at android.app.IActivityManager$Stub$Proxy.startService(IActivityManager.java:6033)
                                                     	at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1856)
                                                     	at android.app.ContextImpl.startForegroundService(ContextImpl.java:1832)
                                                     	at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:781)
                                                     	at androidx.core.content.ContextCompat$Api26Impl.startForegroundService(ContextCompat.java:1091)
                                                     	at androidx.core.content.ContextCompat.startForegroundService(ContextCompat.java:749)
                                                     	at androidx.work.impl.Processor.startForeground(Processor.java:161)
                                                     	at androidx.work.impl.utils.WorkForegroundUpdater$1.run(WorkForegroundUpdater.java:100)
                                                     	at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
                                                     	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
                                                     	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
                                                     	at java.lang.Thread.run(Thread.java:920)
---------------------------- PROCESS STARTED (6577) for package eu.darken.sdmse ----------------------------
14:15:40.786 ActivityManager                      W  Background start not allowed: service Intent { act=ACTION_STOP_FOREGROUND cmp=eu.darken.sdmse/androidx.work.impl.foreground.SystemForegroundService } to eu.darken.sdmse/androidx.work.impl.foreground.SystemForegroundService from pid=6577 uid=10149 pkg=eu.darken.sdmse startFg?=false
14:15:40.786 WM-Processor                         E  Unable to stop foreground service
                                                     android.app.BackgroundServiceStartNotAllowedException: Not allowed to start service Intent { act=ACTION_STOP_FOREGROUND cmp=eu.darken.sdmse/androidx.work.impl.foreground.SystemForegroundService }: app is in background uid UidRecord{da4fe1f u0a149 CEM  idle change:cached procs:0 seq(0,0,0)}
                                                     	at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1870)
                                                     	at android.app.ContextImpl.startService(ContextImpl.java:1826)
                                                     	at android.content.ContextWrapper.startService(ContextWrapper.java:776)
                                                     	at androidx.work.impl.Processor.stopForegroundService(Processor.java:318)
                                                     	at androidx.work.impl.Processor.stopForeground(Processor.java:224)
                                                     	at androidx.work.impl.WorkerWrapper.resolve(WorkerWrapper.java:460)
                                                     	at androidx.work.impl.WorkerWrapper.setSucceededAndResolve(WorkerWrapper.java:600)
                                                     	at androidx.work.impl.WorkerWrapper.handleResult(WorkerWrapper.java:477)
                                                     	at androidx.work.impl.WorkerWrapper.onWorkFinished(WorkerWrapper.java:354)
                                                     	at androidx.work.impl.WorkerWrapper$2.run(WorkerWrapper.java:331)
                                                     	at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
                                                     	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
                                                     	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
                                                     	at java.lang.Thread.run(Thread.java:920)
```
d4rken added a commit that referenced this pull request Jul 5, 2024
* Scheduler: Hint about battery optimizations

Show a hint about disabling battery optimizations on newer Android versions where this may affect launching a foreground task, e.g. Android 14+.

I tested various API levels and starting with Android 12 (API31), we get an exception when starting foreground services while optimizations are enabled.

We didn't use `REQUEST_IGNORE_BATTERY_OPTIMIZATIONS` and shouldn't as black box Google Play is potentially allergic to it's use.
Do the safe way of directing the user to the system settings.

Also see #1273 and #1247

```
14:15:39.704 SDMSE:Scheduler:Worker               D  Declaring as foreground task
14:15:39.704 SDMSE:Scheduler:Worker               D  Supplying getForegroundInfo() with Schedule(id=96b7c483-863d-4c9f-9baa-b5e1b6083ddc, createdAt=2024-07-05T12:14:14.886Z, scheduledAt=2024-07-05T12:14:14.886Z, hour=14, minute=15, label=Test Schedule 96b7c483-863d-4c9f-9baa-b5e1b6083ddc, repeatInterval=PT24H, useCorpseFinder=false, useSystemCleaner=false, useAppCleaner=false, commandsAfterSchedule=[], executedAt=null)
14:15:39.705 WM-Processor                         I  Moving WorkSpec (5bb950de-d2c4-4622-91bf-58408fbbbf8c) to the foreground
14:15:39.707 ActivityManager                      W  Background started FGS: Disallowed [callingPackage: eu.darken.sdmse; callingUid: 10149; uidState: CEM ; intent: Intent { act=ACTION_START_FOREGROUND cmp=eu.darken.sdmse/androidx.work.impl.foreground.SystemForegroundService (has extras) }; code:DENIED; tempAllowListReason:<null>; targetSdkVersion:34; callerTargetSdkVersion:34; startForegroundCount:0; bindFromPackage:null]
14:15:39.707 ActivityManager                      W  startForegroundService() not allowed due to mAllowStartForeground false: service eu.darken.sdmse/androidx.work.impl.foreground.SystemForegroundService
14:15:39.708 SDMSE:Scheduler:Worker               E  Can't execute in foreground: android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service eu.darken.sdmse/androidx.work.impl.foreground.SystemForegroundService
                                                     	at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:54)
                                                     	at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:50)
                                                     	at android.os.Parcel.readParcelable(Parcel.java:3334)
                                                     	at android.os.Parcel.createExceptionOrNull(Parcel.java:2421)
                                                     	at android.os.Parcel.createException(Parcel.java:2410)
                                                     	at android.os.Parcel.readException(Parcel.java:2393)
                                                     	at android.os.Parcel.readException(Parcel.java:2335)
                                                     	at android.app.IActivityManager$Stub$Proxy.startService(IActivityManager.java:6033)
                                                     	at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1856)
                                                     	at android.app.ContextImpl.startForegroundService(ContextImpl.java:1832)
                                                     	at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:781)
                                                     	at androidx.core.content.ContextCompat$Api26Impl.startForegroundService(ContextCompat.java:1091)
                                                     	at androidx.core.content.ContextCompat.startForegroundService(ContextCompat.java:749)
                                                     	at androidx.work.impl.Processor.startForeground(Processor.java:161)
                                                     	at androidx.work.impl.utils.WorkForegroundUpdater$1.run(WorkForegroundUpdater.java:100)
                                                     	at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
                                                     	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
                                                     	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
                                                     	at java.lang.Thread.run(Thread.java:920)
---------------------------- PROCESS STARTED (6577) for package eu.darken.sdmse ----------------------------
14:15:40.786 ActivityManager                      W  Background start not allowed: service Intent { act=ACTION_STOP_FOREGROUND cmp=eu.darken.sdmse/androidx.work.impl.foreground.SystemForegroundService } to eu.darken.sdmse/androidx.work.impl.foreground.SystemForegroundService from pid=6577 uid=10149 pkg=eu.darken.sdmse startFg?=false
14:15:40.786 WM-Processor                         E  Unable to stop foreground service
                                                     android.app.BackgroundServiceStartNotAllowedException: Not allowed to start service Intent { act=ACTION_STOP_FOREGROUND cmp=eu.darken.sdmse/androidx.work.impl.foreground.SystemForegroundService }: app is in background uid UidRecord{da4fe1f u0a149 CEM  idle change:cached procs:0 seq(0,0,0)}
                                                     	at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1870)
                                                     	at android.app.ContextImpl.startService(ContextImpl.java:1826)
                                                     	at android.content.ContextWrapper.startService(ContextWrapper.java:776)
                                                     	at androidx.work.impl.Processor.stopForegroundService(Processor.java:318)
                                                     	at androidx.work.impl.Processor.stopForeground(Processor.java:224)
                                                     	at androidx.work.impl.WorkerWrapper.resolve(WorkerWrapper.java:460)
                                                     	at androidx.work.impl.WorkerWrapper.setSucceededAndResolve(WorkerWrapper.java:600)
                                                     	at androidx.work.impl.WorkerWrapper.handleResult(WorkerWrapper.java:477)
                                                     	at androidx.work.impl.WorkerWrapper.onWorkFinished(WorkerWrapper.java:354)
                                                     	at androidx.work.impl.WorkerWrapper$2.run(WorkerWrapper.java:331)
                                                     	at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
                                                     	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
                                                     	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
                                                     	at java.lang.Thread.run(Thread.java:920)
```

* Code cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working as expected c: Scheduler

Projects

None yet

Development

Successfully merging this pull request may close these issues.

System Cleaner scan results not showing when completed in background

1 participant