python-for-android icon indicating copy to clipboard operation
python-for-android copied to clipboard

WIP: workmanager and bound services support

Open rnixx opened this issue 4 years ago • 7 comments
trafficstars

This is a work in progress PR addressing upcoming restrictions with foreground services as of android 12.

See https://developer.android.com/about/versions/12/foreground-services

2 major improvements are implemented:

  • Support bound services.
  • Support running python tasks via WorkManager (https://developer.android.com/topic/libraries/architecture/workmanager)

Currently the implementation lives in service_library bootstrap as this is what's needed right now for a project.

Maybe it makes sense to create a dedicated bootstrap for this features, as especially work managers might be very useful if called from a kivy app via jnius (e.g. tracking GPS periodically). Therefor we'd need a mechanism to optionally "merge" several bootstraps, e.g. a kivy app might depend on sdl and workmanager bootstraps or you would like to create a aar library including workmanager feature. We also might just add workmanager and bound service support to common bootstrap, but this implies some more build dependencies (androidx.work:work-runtime, androidx.work:work-multiprocess, androidx.concurrent:concurrent-futures)

While playing around with starting python in main.c, we came along the problem with injecting sys.exit when python ends. When using workmanager workers this would just kill the entire application so we made it optional. If someone can explain why injecting sys.exit is needed at all this would be nice.

TODO

  • [ ] Figure out the original need of injecting sys.exit when python ends and whether and when this needs to be kept.
  • [ ] Discuss whether to create a dedicated bootstrap for workmanager, or maybe just add it to common bootstraps since workmanager and bound service support are in general useful features
  • [ ] If added to common bootstraps, add some convenience API to android recipe to work with services and work managers.
  • [ ] Proper python shutdown if system requests workers to stop
  • [ ] Cleanup
  • [ ] Documentation

rnixx avatar Jun 16 '21 06:06 rnixx

I see all tests are passing - does anything else need to be done with this PR before it's ready to go? Very keen to see this merged.

rtibbles avatar Apr 06 '22 15:04 rtibbles

Any objections if I rebase this and polish it up? We'd like to be able to use this functionality.

dbnicholson avatar Apr 26 '22 20:04 dbnicholson

Any objections if I rebase this and polish it up? We'd like to be able to use this functionality.

Unless @rnixx wants to take care of it, I don't see any blocking.

@AndreMiras considering that you already started the review, any thoughts?

misl6 avatar Apr 26 '22 21:04 misl6

Sure go for it. I'm not as active as I used to be for test/reviews unfortunately

AndreMiras avatar Apr 28 '22 21:04 AndreMiras

@misl6 @dbnicholson @zworkb

Sorry for leaving this PR abandoned for so long. Of course if have no problem if the work done so far gets picked up by someone else.

Referring to https://github.com/kivy/python-for-android/pull/2613

We had the use case to use p4a for a service only inside a native Kotlin application. In an accepted solutions this case ideally continues to work.

Referring the removal of the bound service - honestly i am a little out of our original context right now - this need to be double checked about the original intention.

rnixx avatar Dec 20 '22 16:12 rnixx

Hi, Any update on that ? WorkManager is crucial for having jobs not being stoped by android...

Bastian82 avatar Jan 19 '23 12:01 Bastian82