Skip to content

Commit

Permalink
Acquire a partial wake lock when GPX recording is running
Browse files Browse the repository at this point in the history
Partially fixes
#2406
  • Loading branch information
simonpoole committed Nov 2, 2024
1 parent a8dcccb commit 748be98
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
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

0 comments on commit 748be98

Please sign in to comment.