mendhak/gpslogger

Short logging interval not working properly on Android 12

Closed this issue · 4 comments

Since I started using an S21 with Android 12, I noticed a "bug" with one of my logging profiles. The logging interval is set to 60 seconds, but the app only attempts to log a point after 90 seconds or so. Considering that this wasn't an issue on my old phone, I think it's related to Google's attempts to improve battery life. Turning off battery optimizations for GPSLogger hasn't helped though.

I've dug a bit into the code and documentation to see if I can find something.

In setAlarmForNextPoint(), there is a check do see if the device is dozing. Considering that I've set battery optimizations for the app to "Unrestricted", I assume that isDozing() always returns false.
In that case, the alarm for the next recording would be set using AlarmManager.set(), which according to the documentation "may be deferred and delivered some time later" since API 19.
On the other hand, if isDozing() returns true, the next alarm is set using AlarmManager.setExactAndAllowWhileIdle which does not seem to be impacted by the random delays.
There is also AlarmManager.setExact() with which "the alarm will be delivered as nearly as possible to the requested trigger time", whatever that means. Google lists an Alarm clock as an example, but I would have expected those to use setExactAndAllowWhileIdle().

Now, I don't have much experience developing on Android, but based on the documentation I'm wondering if it would make sense to replace the set() call with either setExact() or setExactAndAllowWhileIdle(). Yes, it could have an impact on battery usage on modern Android versions, but personally I think that users of an app like this one are aware that short logging intervals will use more battery. Maybe it could also make sense to add a setting to allow per-profile customization of how precise one wants the interval to be?

Hey @JanHellwig thanks for digging through that logic, it was quite useful. I think what you have said sounds reasonable. If you are setting a short logging interval, it will use more battery. In this commit ee96c9b you will see I've just replaced both types of calls with setExactAndAllowWhileIdle.

If you're able to test, I've put an APK here: https://github.com/mendhak/gpslogger/releases/tag/v122-rc1

I'll try running this with short intervals like 1s, 5s, and normal intervals like 60s, 120s, to see how it does.

I did a quick check and it seems to work properly with my 60s interval profile. Thank you.

Unrelated to this, but I did have to uninstall the app before installing the RC as Android was complaining about it conflicting with the version from F-Droid. Is it possible to have them compatible somehow? That would make testing RCs a bit easier.

Hey thanks for testing, that will be useful to others. I'm still going to try it out a bit more before publishing.

It's not possible to have the APKs compatible. The F-Ddroid version is signed by F-Droid developers (they even build it themselves), and the APK I post on Github is signed by me. That's why Android OS complains about it.