-
Notifications
You must be signed in to change notification settings - Fork 86
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
A ForegroundService for Lambdanative. #256
Conversation
Exports a single procedure: `foreground-service!` A NOOP on anything but Android. On Android: (foreground-service! #t) Start the service. (foreground-service! #f) Stop the service. The service will send EVENT_IDLE every 30 seconds to your application. The main use is to prevent Android from aggressivly stopping the application when it should keep running.
Doesn't loaders/android/bootstrap.java.in#L116 give you an EVENT_IDLE event every 1/4 second, which should keep things alive? |
Newer versions of Android are rather aggressive in killing services.
To cope with the issue one needs to start a foreground service. See:
https://stackoverflow.com/questions/53612307/foreground-notification-service-not-working-in-one-plus-devices/56195214#56195214
After submitting the pull request I learned that this might still not be
enough. Though completely handling the problem will need at least API
version 26. I'll update the branch when I managed to get that done. (Right
now I need to re-learn what I did to install API version 19 and install 26
too. :-/ so much for memory.)
…
|
Okay thanks for looking into this further. If you still have the SDK installed, simply change the |
Am Mon, 19 Aug 2019 12:04:17 -0700
schrieb Matthias Görges <[email protected]>:
Okay thanks for looking into this further. If you still have the SDK
installed, simply change the `ANDROIDAPI` in the SETUP file, clear
the android build cache (`make scrub` is a bit much) and you should
be good to go.
Yeah, except that it changes my ANDROIDAPI=26 to 24 and complains that
24 is not a target. Looks as if I'll have to install a different ndk.
|
Am Mon, 19 Aug 2019 12:04:17 -0700
schrieb Matthias Görges <[email protected]>:
Okay thanks for looking into this further.
One issue/question looms already on the horizon: It looks like I'll
need to call Android APIs not yet available in say version 19. Is
there a paved route how to make the java compiler happy? As far as I
can see it will complain about unknown classes/methods.
And I really don't want to write code which works only for Android 8
and above.
|
As for the SDK, you probably only will need to add the missing one with the sdk manager something like As for calling new APIs, there isn't and #ifdef equivalent in Java. You might be able to some |
See comments in ANDROID_xml_permissions and ANDROID_java_activityadditions how to compile for older API versions and possible issues wrt. restrictions regarding google play store.
See comments in ANDROID_xml_permissions and ANDROID_java_activityadditions how to compile for older API versions and possible issues wrt. restrictions regarding google play store.
…danative into new-module-androidforeground
There are still issues. After removing and reinstalling, at least one device with Android 8.1 does no longer start LN applications build with API version 26 at all. Reason: None of the permissions requested via Manifest are granted at all. They appear as requested, but are disabled. By now I don't yet know how to fix this. |
Thanks for the update, and working on the battery optimization piece. I believe @bclayton05 also encountered permission problems over the summer, however, for camera permissions? I don't recall details, but it might be that the |
I never fully understood where the problem was coming from, but I ended up solving my permission issues by explicitly confirming permissions each time the user wanted to interact with the desired permission-blocked feature. As @mgorges mentioned, explicitly requesting permissions when they hadn’t been granted was the solution. |
Since Marshmallow permissions are not automatically granted as specified in the Manifest. As a consequence LN apps die when accessing the SDCard, e.g. when unpacking embedded files. This patch adds a fragile hack to enable an ifdef-alike trick to exclude code portions when targeting older APIs and uses it to request the permission at startup. Plus create the system-directory if it does not already exists.
If this really is required we should consider adding this to modules/eventloop/eventloop.scm, in the e.g. in eventloop/eventloop.scm#L264 for Alternatively, you could make at least the |
I'm unsure. The eventloop is essentially always required on Android, isn't it? Many applications may not need to run all the time. I'd rather keep the foregroundservice separate.
This might be easy enough to add at the cost of some flexibility. Especially when it comes to termination. (So far I'd seen the app die and the foregroundservice still in the notifications.) However I'll have to understand more about termination and Android. Some docs indicate that one should abstain from ever explicitly terminating. Dragons ahead: Simply calling |
@bclayton05: I'm trying to solve the camera permission here too. Right now the app always dies, which permission did you request explicitely and when/where in the source. Right now my LN app still dies as son as I try to access the camera. Though this might be some other stupidity on my part. |
|
There might be more pieces - this is from an app doing video things by @bclayton05 |
Looks like the culprit is another pitfall in newer Android APIs (here 26), which makes the approach to use startActivityForResult to obtain a photo fragile. Question: How would I redirect (log or log alike) messages from Java code to the LN logfile? The exception I ran into with simply requesting CAMERA permissions (after those being granted):
|
I am currently not aware of a way to redirect Android crash dumps into LambdaNative through the JNI interface or any other approach. In the Debugging section of the Wiki we do refer only to |
Am Tue, 03 Sep 2019 17:47:14 -0700
schrieb Matthias Görges <[email protected]>:
I am currently not aware of a way to redirect Android crash dumps
into LambdaNative through the JNI interface or any other approach. In
the
[Debugging](https://github.com/part-cw/lambdanative/wiki/Debugging)
section of the Wiki we do refer only to `logcat` which is what you
also used here.
Actually I did not yet bother to use adb.
In principle I do think this is something that would
make things easier from our perspective,
That's been my feeling too. I just threw these lines into my apps
`ANDROID_java_oncreate:
try {
java.text.SimpleDateFormat formatter = new
java.text.SimpleDateFormat("yyyyMMdd_hhmmss");
String ts = formatter.format(new java.util.Date());
System.setErr(new java.io.PrintStream(new
FileOutputStream("/sdcard/@SYS_APPNAME@/log/log_" + ts + ".j.txt"),
true)); }
catch (java.io.FileNotFoundException e) {}
And catch exceptions which I print like this `e.printStackTrace(System.err);`
However I'd rather like to use the very same log file and LN itself.
but it might also confuse
people who use the typical Android approach and expect it to show
there (too)? Thus if you come across something that allows us to get
a redirected copy that would be great.
The open question for me is how I would achieve to use just one logfile.
|
Am Mon, 02 Sep 2019 12:47:28 -0700
schrieb Matthias Görges <[email protected]>:
There might be more pieces - this is from an add doing video things
by @bclayton05
This tends to go beyond my ability to dig out a reasonable solution.
The issue is related to `android.os.StrictMode.onFileUriExposed`.
All my attempts to contain it turned out to be futile. Internet search
hints towards `androidx`'s `FileProvider` which is also provided by the
support library.
Hence the question: how bad would it be to require the support library.
I understand it's not nice as it's a piece of regulary changing,
unstable code.
But at the other hand I'm short of solutions.
Currently I'm back where I started. The camera works with API level
19, but Android kills my service all the time, thereby killing
availability and responsiveness. Let alone that there is now again
this anoing icon in the status line.
|
If you can get it into JNI, there is a log_c function, which you can call with a string. |
Did you look at this: https://stackoverflow.com/questions/44821017/fileuriexposedexception-using-android-7, in particular their workaround? |
< Am Thu, 05 Sep 2019 12:04:23 -0700
schrieb Matthias Görges <[email protected]>:
> The issue is related to `android.os.StrictMode.onFileUriExposed`.
Did you look at this:
https://stackoverflow.com/questions/44821017/fileuriexposedexception-using-android-7,
in particular their workaround?
I'm afraid, I ran into this the past two day already.
Since Android 7, we don't use file: scheme as uri for intent, you
have to use FileProvider
... a naughty way to do it without adding FileProvider...
StrictMode.VmPolicy.Builder builder = new
StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
… This will simply ignore the URI exposure and you will get the access.
****** *this is not the best practice* ******
still, recommended way is by using FileProvider.
----
To summarize: I feel the work around is good enough for testing. My
goal, however, is rather to get around bad user experience,
unmaintainable code etc. (otherwise I'd document in screenshots how to
get around Androids new limitations/optimizations by just granting all
sorts of permissions frowned upon by google).
Hence I'd consider tampering with StrictMode a viable solution at best
if it was a single statement not affecting any state but the one in
scope. 'setVmPolicy' --- Hm, I know only so much, but this looks to me
like opening the pandora box, doen't it?
Still I'd love to get around the need of the support library. :-(
|
Am Thu, 05 Sep 2019 12:01:41 -0700
schrieb Matthias Görges <[email protected]>:
> The open question for me is how I would achieve to use just one
> logfile.
If you can get it into JNI, there is a
[log_c](https://github.com/part-cw/lambdanative/blob/9f2403be16a75430d8839064fb137b6d978267e0/modules/ln_core/log.scm#L98)
function, which you can call with a string.
This might do the trick once lambdanative is initialized.
Unfortunately the things I needed to debug have been before that's the
case.
Probably I'll have to do it the other way around and test logdir's
existence from Java and open the logfile at the Scheme side in append
mode.
|
It seems that this is becoming a needed feature for #339, however it has merge conflicts in loaders/android/boostrap.java.in and also unrelated changes to packtool, some more of the ifdef API23 pieces we already have? Can I rebase this against master in a branch I don't own to start a new clean copy or would you have to? |
Am Thu, 08 Oct 2020 12:06:13 -0700
schrieb Matthias Görges <[email protected]>:
It seems that this is becoming a needed feature for #339, however it
has merge conflicts in loaders/android/boostrap.java.in and also
unrelated changes to packtool, some more of the ifdef API23 pieces we
already have? Can I rebase this against master in a branch I don't
own to start a new clean copy or would you have to?
Ah, forget the aged PR here.
Those updates are only local here. Not yet polished and pushed.
|
Okay, then I will await an update and in the meantime leave this untouched. Thanks. |
Am Thu, 08 Oct 2020 15:54:29 -0700
schrieb Matthias Görges <[email protected]>:
Okay, then I will await an update and in the meantime leave this
untouched. Thanks.
While my code now works for <whatever-has-changed-recently> it no
longer works for older API versions.
Supporting all the differences between the Android API versions
challenges the limits of what can be done using the simple `substool`.
Assuming gambit new a bit more about the build environment - as #367
proposes - there should be a way to do this.
We need to discuss (or simply inform me) however where Android API
specific files (.java, .java.in, .jar etc.) should be dropped of which
**will have to be** created while the module is compiled and what's a
good spot to copy those over into the build directory.
|
Maybe some of the others on our team can help with this guidance? Otherwise we'll have to defer this by two weeks as I need to focus on a grant submission deadline. Sorry, but I simply don't have the capacity left as I am already behind. |
… new-module-androidforeground
Please don't remove support for older versions. I still need to compile for at least API 19.
BTW: I have to blame myself wrt. this module/PR no longer working since two days. Apparently I managed to NOT include all the required files AND delete them after the move from the development directory into the pull request. Which amounts to me loosing a couple of days of research and testing. I still need this to be resolved, but ... I'll have to redo it. This testing area was yet under version control even in the private branch. Too bad. My bad. |
Sorry to hear that - I can relate how frustrating this is; the last time I did something like it TimeMachine saved me but that was lucky as my work machine didn't have it. |
Replacement version in PR #404 |
Closing in favor of #404 |
Exports a single procedure:
foreground-service!
A NOOP on anything but Android. On Android:
(foreground-service! #t) Start the service.
(foreground-service! #f) Stop the service.
The service will send EVENT_IDLE every 30 seconds to your application.
The main use is to prevent Android from aggressivly stopping the
application when it should keep running.