I'm developing an Android app and want to use Dagger as my DI framework. But I don't know how to inject dependencies that use callbacks.
For example I want to get the location and I use GoogleApiClient for this:
public class LocationProvider implements ILocationProvider,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient googleApiClient;
private ILocationRequester requester;
public LocationProvider(@NonNull Context context, @NonNull ILocationRequester requester) {
this.requester = requester;
googleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void beginGetLocation() {
googleApiClient.connect();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
requester.locationFound(lastLocation);
}
@Override
public void onConnectionSuspended(int i) {
requester.locationFound(null);
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
requester.locationFound(null);
}
}
In this case I'd like to inject the GoogleApiClient instance using Dagger to be able to mock it in my tests, but as it depends on this class I can't. The scenario is valid for any long running operation that uses callbacks even if I use another class to implement the callback.
Does anybody know a solution to this?