From 97c46183c3f422431ed1a0e32866d28e152b5b36 Mon Sep 17 00:00:00 2001 From: Dennis Guse Date: Mon, 20 Jul 2020 20:10:16 +0200 Subject: [PATCH] TrackRecordingServiceConnection: always bind (and start) the TrackRecordingService. Seemed to be a memory optimization. --- .../TrackRecordingServiceConnection.java | 101 ++++++------------ .../opentracks/util/ServiceUtils.java | 2 +- 2 files changed, 35 insertions(+), 68 deletions(-) diff --git a/src/main/java/de/dennisguse/opentracks/services/TrackRecordingServiceConnection.java b/src/main/java/de/dennisguse/opentracks/services/TrackRecordingServiceConnection.java index 9c4bffdc9..d865ee759 100644 --- a/src/main/java/de/dennisguse/opentracks/services/TrackRecordingServiceConnection.java +++ b/src/main/java/de/dennisguse/opentracks/services/TrackRecordingServiceConnection.java @@ -33,7 +33,6 @@ import de.dennisguse.opentracks.R; import de.dennisguse.opentracks.TrackEditActivity; import de.dennisguse.opentracks.util.IntentUtils; import de.dennisguse.opentracks.util.PreferencesUtils; -import de.dennisguse.opentracks.util.ServiceUtils; /** * Wrapper for the track recording service. @@ -66,14 +65,43 @@ public class TrackRecordingServiceConnection implements ServiceConnection, Death * Starts and binds the service. */ public void startAndBind(Context context) { - bindService(context, true); + if (trackRecordingService != null) { + // Service is already started and bound. + return; + } + + Log.i(TAG, "Starting the service."); + context.startService(new Intent(context, TrackRecordingService.class)); + + startConnection(context); } /** - * Binds the service if it is started. + * Resumes the track recording service connection. + * + * @param context the context */ - private void bindIfStarted(Context context) { - bindService(context, false); + public void startConnection(@NonNull Context context) { + if (trackRecordingService != null) { + // Service is already started and bound. + return; + } + + Log.i(TAG, "Binding the service."); + int flags = BuildConfig.DEBUG ? Context.BIND_DEBUG_UNBIND : 0; + context.bindService(new Intent(context, TrackRecordingService.class), this, flags); + } + + /** + * Unbinds the service (but leave it running). + */ + public void unbind(Context context) { + try { + context.unbindService(this); + } catch (IllegalArgumentException e) { + // Means not bound to the service. OK to ignore. + } + setTrackRecordingService(null); } /** @@ -91,7 +119,6 @@ public class TrackRecordingServiceConnection implements ServiceConnection, Death return trackRecordingService; } - /** * Sets the trackRecordingService. * @@ -104,18 +131,6 @@ public class TrackRecordingServiceConnection implements ServiceConnection, Death } } - /** - * Unbinds the service (but leave it running). - */ - public void unbind(Context context) { - try { - context.unbindService(this); - } catch (IllegalArgumentException e) { - // Means not bound to the service. OK to ignore. - } - setTrackRecordingService(null); - } - @Override public void onServiceConnected(ComponentName className, IBinder service) { Log.i(TAG, "Connected to the service."); @@ -139,44 +154,6 @@ public class TrackRecordingServiceConnection implements ServiceConnection, Death setTrackRecordingService(null); } - /** - * Binds the service if it is started. - * - * @param startIfNeeded start the service if needed - */ - private void bindService(Context context, boolean startIfNeeded) { - if (trackRecordingService != null) { - // Service is already started and bound. - return; - } - - if (!startIfNeeded && !ServiceUtils.isTrackRecordingServiceRunning(context)) { - Log.d(TAG, "Service is not started. Not binding it."); - return; - } - - if (startIfNeeded) { - Log.i(TAG, "Starting the service."); - context.startService(new Intent(context, TrackRecordingService.class)); - } - - Log.i(TAG, "Binding the service."); - int flags = BuildConfig.DEBUG ? Context.BIND_DEBUG_UNBIND : 0; - context.bindService(new Intent(context, TrackRecordingService.class), this, flags); - } - - /** - * Resumes the track recording service connection. - * - * @param context the context - */ - public void startConnection(@NonNull Context context) { - bindIfStarted(context); - if (!ServiceUtils.isTrackRecordingServiceRunning(context)) { - resetRecordingState(context); - } - } - /** * Resumes the recording track. */ @@ -197,16 +174,6 @@ public class TrackRecordingServiceConnection implements ServiceConnection, Death } } - private static void resetRecordingState(Context context) { - if (PreferencesUtils.isRecording(context)) { - PreferencesUtils.setLong(context, R.string.recording_track_id_key, PreferencesUtils.RECORDING_TRACK_ID_DEFAULT); - } - boolean recordingTrackPaused = PreferencesUtils.isRecordingTrackPaused(context); - if (!recordingTrackPaused) { - PreferencesUtils.defaultRecordingTrackPaused(context); - } - } - /** * Adds a marker. * @@ -241,7 +208,7 @@ public class TrackRecordingServiceConnection implements ServiceConnection, Death public void stopRecording(@NonNull Context context, boolean showEditor) { TrackRecordingServiceInterface trackRecordingService = getServiceIfBound(); if (trackRecordingService == null) { - resetRecordingState(context); + Log.e(TAG, "TrackRecordingService not connected."); } else { try { if (showEditor) { diff --git a/src/main/java/de/dennisguse/opentracks/util/ServiceUtils.java b/src/main/java/de/dennisguse/opentracks/util/ServiceUtils.java index 1b756b400..740540cf5 100644 --- a/src/main/java/de/dennisguse/opentracks/util/ServiceUtils.java +++ b/src/main/java/de/dennisguse/opentracks/util/ServiceUtils.java @@ -18,7 +18,7 @@ public class ServiceUtils { * * @param context the current context */ - @Deprecated + @Deprecated //TODO This method must be removed as the service will nowadays always be started public static boolean isTrackRecordingServiceRunning(Context context) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); if (activityManager == null) {