forked from upstream-mirrors/OpenTracks
Cleanup: simplify TrackServiceConnection.
This commit is contained in:
+27
-31
@@ -42,7 +42,7 @@ import de.dennisguse.opentracks.data.models.Marker;
|
|||||||
*
|
*
|
||||||
* @author Rodrigo Damazio
|
* @author Rodrigo Damazio
|
||||||
*/
|
*/
|
||||||
public class TrackRecordingServiceConnection implements ServiceConnection, DeathRecipient {
|
public class TrackRecordingServiceConnection {
|
||||||
|
|
||||||
private static final String TAG = TrackRecordingServiceConnection.class.getSimpleName();
|
private static final String TAG = TrackRecordingServiceConnection.class.getSimpleName();
|
||||||
|
|
||||||
@@ -50,6 +50,30 @@ public class TrackRecordingServiceConnection implements ServiceConnection, Death
|
|||||||
|
|
||||||
private TrackRecordingService trackRecordingService;
|
private TrackRecordingService trackRecordingService;
|
||||||
|
|
||||||
|
private final ServiceConnection serviceConnection = new ServiceConnection() {
|
||||||
|
@Override
|
||||||
|
public void onServiceConnected(ComponentName className, IBinder service) {
|
||||||
|
Log.i(TAG, "Connected to the service: " + service);
|
||||||
|
try {
|
||||||
|
service.linkToDeath(deathRecipient, 0);
|
||||||
|
} catch (RemoteException e) {
|
||||||
|
Log.e(TAG, "Failed to bind a death recipient.", e);
|
||||||
|
}
|
||||||
|
setTrackRecordingService(((TrackRecordingService.Binder) service).getService());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onServiceDisconnected(ComponentName className) {
|
||||||
|
Log.i(TAG, "Disconnected from the service.");
|
||||||
|
setTrackRecordingService(null);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private final DeathRecipient deathRecipient = () -> {
|
||||||
|
Log.d(TAG, "Service died.");
|
||||||
|
setTrackRecordingService(null);
|
||||||
|
};
|
||||||
|
|
||||||
public TrackRecordingServiceConnection() {
|
public TrackRecordingServiceConnection() {
|
||||||
callback = null;
|
callback = null;
|
||||||
}
|
}
|
||||||
@@ -104,7 +128,7 @@ public class TrackRecordingServiceConnection implements ServiceConnection, Death
|
|||||||
|
|
||||||
Log.i(TAG, "Binding the service.");
|
Log.i(TAG, "Binding the service.");
|
||||||
int flags = BuildConfig.DEBUG ? Context.BIND_DEBUG_UNBIND : 0;
|
int flags = BuildConfig.DEBUG ? Context.BIND_DEBUG_UNBIND : 0;
|
||||||
context.bindService(new Intent(context, TrackRecordingService.class), this, flags);
|
context.bindService(new Intent(context, TrackRecordingService.class), serviceConnection, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -113,7 +137,7 @@ public class TrackRecordingServiceConnection implements ServiceConnection, Death
|
|||||||
//TODO This is often called for one-shot operations and should be refactored as unbinding is required.
|
//TODO This is often called for one-shot operations and should be refactored as unbinding is required.
|
||||||
public void unbind(Context context) {
|
public void unbind(Context context) {
|
||||||
try {
|
try {
|
||||||
context.unbindService(this);
|
context.unbindService(serviceConnection);
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
// Means not bound to the service. OK to ignore.
|
// Means not bound to the service. OK to ignore.
|
||||||
}
|
}
|
||||||
@@ -130,35 +154,10 @@ public class TrackRecordingServiceConnection implements ServiceConnection, Death
|
|||||||
if (callback != null) {
|
if (callback != null) {
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
callback.onConnected(value, this);
|
callback.onConnected(value, this);
|
||||||
} else {
|
|
||||||
callback.onDisconnected();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onServiceConnected(ComponentName className, IBinder service) {
|
|
||||||
Log.i(TAG, "Connected to the service: " + service);
|
|
||||||
try {
|
|
||||||
service.linkToDeath(this, 0);
|
|
||||||
} catch (RemoteException e) {
|
|
||||||
Log.e(TAG, "Failed to bind a death recipient.", e);
|
|
||||||
}
|
|
||||||
setTrackRecordingService(((TrackRecordingService.Binder) service).getService());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onServiceDisconnected(ComponentName className) {
|
|
||||||
Log.i(TAG, "Disconnected from the service.");
|
|
||||||
setTrackRecordingService(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void binderDied() {
|
|
||||||
Log.d(TAG, "Service died.");
|
|
||||||
setTrackRecordingService(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public Marker.Id addMarker(Context context, String name, String category, String description, String photoUrl) {
|
public Marker.Id addMarker(Context context, String name, String category, String description, String photoUrl) {
|
||||||
if (trackRecordingService == null) {
|
if (trackRecordingService == null) {
|
||||||
@@ -191,8 +190,5 @@ public class TrackRecordingServiceConnection implements ServiceConnection, Death
|
|||||||
|
|
||||||
public interface Callback {
|
public interface Callback {
|
||||||
void onConnected(TrackRecordingService service, TrackRecordingServiceConnection self);
|
void onConnected(TrackRecordingService service, TrackRecordingServiceConnection self);
|
||||||
|
|
||||||
default void onDisconnected() {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user