forked from upstream-mirrors/OpenTracks
TrackRecordingServiceConnection: always bind (and start) the TrackRecordingService.
Seemed to be a memory optimization.
This commit is contained in:
+34
-67
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user