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

Acquire a partial wake lock when GPX recording is running #2709

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<queries>
<intent>
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/de/blau/android/services/TrackerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -48,6 +50,7 @@
import androidx.preference.PreferenceManager;
import de.blau.android.App;
import de.blau.android.AsyncResult;
import de.blau.android.BuildConfig;
import de.blau.android.ErrorCodes;
import de.blau.android.Logic;
import de.blau.android.Main;
Expand All @@ -74,8 +77,9 @@

public class TrackerService extends Service {

private static final int TAG_LEN = Math.min(LOG_TAG_LEN, TrackerService.class.getSimpleName().length());
private static final String DEBUG_TAG = TrackerService.class.getSimpleName().substring(0, TAG_LEN);
private static final String WAKELOCK_TAG = BuildConfig.APPLICATION_ID + ":gpx_recording";
private static final int TAG_LEN = Math.min(LOG_TAG_LEN, TrackerService.class.getSimpleName().length());
private static final String DEBUG_TAG = TrackerService.class.getSimpleName().substring(0, TAG_LEN);

private static final float TRACK_LOCATION_MIN_ACCURACY = 200f;

Expand Down Expand Up @@ -161,6 +165,8 @@ private enum GpsSource {
private Sensor pressure = null;
private Sensor temperature = null;

private WakeLock wakeLock = null;

@Override
public void onCreate() {
super.onCreate();
Expand Down Expand Up @@ -378,12 +384,15 @@ public void startBugAutoDownload() {
* Actually starts tracking. Gets called by {@link #onStartCommand(Intent, int, int)} when the service is started.
* See {@link #startTracking()} for the public method to call when tracking should be started.
*/
private void startTrackingInternal() {
private synchronized void startTrackingInternal() {
Log.i(DEBUG_TAG, "Start tracking");
if (startInternal()) {
tracking = true;
track.markNewSegment();
startAutosave();
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_TAG);
wakeLock.acquire();
}
}

Expand Down Expand Up @@ -464,7 +473,7 @@ private boolean startInternal() {
*
* @param deleteTrack true if the track should be deleted, false if it should be kept
*/
public void stopTracking(boolean deleteTrack) {
public synchronized void stopTracking(boolean deleteTrack) {
Log.d(DEBUG_TAG, "Stop tracking");
if (autosaveFuture != null) {
Log.i(DEBUG_TAG, "Cancelling autosave");
Expand All @@ -482,6 +491,10 @@ public void stopTracking(boolean deleteTrack) {
track.save();
}
tracking = false;
if (wakeLock != null && wakeLock.isHeld()) {
wakeLock.release();
wakeLock = null;
}
stop();
}

Expand Down
Loading