mirror of https://github.com/tasks/tasks
Enable auto manage for google api client
parent
ce1a84cc98
commit
747236e21e
@ -1,12 +1,16 @@
|
||||
package org.tasks.dialogs;
|
||||
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
|
||||
import org.tasks.location.LocationApi;
|
||||
import org.tasks.location.OnLocationPickedHandler;
|
||||
|
||||
public class LocationPickerDialog {
|
||||
public static void pickLocation(LocationApi locationApi, Fragment fragment, OnLocationPickedHandler onLocationPickedHandler) {
|
||||
|
||||
public LocationPickerDialog(OnLocationPickedHandler onLocationPickedHandler) {
|
||||
|
||||
}
|
||||
|
||||
public void show(FragmentManager childFragmentManager, String fragTagLocationPicker) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,76 @@
|
||||
package org.tasks.location;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
|
||||
import com.google.android.gms.common.ConnectionResult;
|
||||
import com.google.android.gms.common.api.GoogleApiClient;
|
||||
import com.google.android.gms.location.LocationServices;
|
||||
import com.google.android.gms.location.places.Places;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.tasks.injection.ForApplication;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class GoogleApi implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(GoogleApi.class);
|
||||
|
||||
private GoogleApiClient.Builder builder;
|
||||
private GoogleApiClient googleApiClient;
|
||||
private GoogleApiClientConnectionHandler googleApiClientConnectionHandler;
|
||||
private boolean enableAutoManage;
|
||||
|
||||
public interface GoogleApiClientConnectionHandler {
|
||||
void onConnect(GoogleApiClient client);
|
||||
}
|
||||
|
||||
@Inject
|
||||
public GoogleApi(@ForApplication Context context) {
|
||||
builder = new GoogleApiClient.Builder(context, this, this)
|
||||
.addConnectionCallbacks(this);
|
||||
}
|
||||
|
||||
public GoogleApi requestLocationServices() {
|
||||
builder.addApi(LocationServices.API);
|
||||
return this;
|
||||
}
|
||||
|
||||
public GoogleApi requestGeoData() {
|
||||
builder.addApi(Places.GEO_DATA_API);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void connect(final GoogleApiClientConnectionHandler googleApiClientConnectionHandler) {
|
||||
this.googleApiClientConnectionHandler = googleApiClientConnectionHandler;
|
||||
googleApiClient = builder.build();
|
||||
if (!enableAutoManage) {
|
||||
googleApiClient.connect();
|
||||
}
|
||||
}
|
||||
|
||||
protected GoogleApi enableAutoManage(FragmentActivity fragmentActivity, GoogleApiClient.OnConnectionFailedListener onConnectionFailedListener) {
|
||||
enableAutoManage = true;
|
||||
builder.enableAutoManage(fragmentActivity, 0, onConnectionFailedListener);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectionFailed(ConnectionResult connectionResult) {
|
||||
log.error("onConnectionFailed({})", connectionResult);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnected(Bundle bundle) {
|
||||
log.info("onConnected(Bundle)");
|
||||
googleApiClientConnectionHandler.onConnect(googleApiClient);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectionSuspended(int i) {
|
||||
log.info("onConnectionSuspended({})", i);
|
||||
}
|
||||
}
|
||||
@ -1,51 +0,0 @@
|
||||
package org.tasks.location;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.google.android.gms.common.api.GoogleApiClient;
|
||||
import com.google.android.gms.location.LocationServices;
|
||||
import com.google.android.gms.location.places.Places;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.tasks.injection.ForApplication;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class GoogleApiClientProvider {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(GoogleApiClientProvider.class);
|
||||
|
||||
private Context context;
|
||||
|
||||
public interface withApi extends GoogleApiClient.OnConnectionFailedListener {
|
||||
void doWork(GoogleApiClient googleApiClient);
|
||||
}
|
||||
|
||||
@Inject
|
||||
public GoogleApiClientProvider(@ForApplication Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public void getApi(final withApi callback) {
|
||||
final GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context)
|
||||
.addOnConnectionFailedListener(callback)
|
||||
.addApi(Places.GEO_DATA_API)
|
||||
.addApi(LocationServices.API)
|
||||
.build();
|
||||
googleApiClient.registerConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
|
||||
@Override
|
||||
public void onConnected(Bundle bundle) {
|
||||
log.info("onConnected({})", bundle);
|
||||
callback.doWork(googleApiClient);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectionSuspended(int i) {
|
||||
log.info("onConnectionSuspended({})", i);
|
||||
}
|
||||
});
|
||||
googleApiClient.connect();
|
||||
}
|
||||
}
|
||||
@ -1,84 +0,0 @@
|
||||
package org.tasks.location;
|
||||
|
||||
import android.location.Location;
|
||||
|
||||
import com.google.android.gms.common.ConnectionResult;
|
||||
import com.google.android.gms.common.api.GoogleApiClient;
|
||||
import com.google.android.gms.common.api.ResultCallback;
|
||||
import com.google.android.gms.location.LocationServices;
|
||||
import com.google.android.gms.location.places.AutocompletePredictionBuffer;
|
||||
import com.google.android.gms.location.places.PlaceBuffer;
|
||||
import com.google.android.gms.location.places.Places;
|
||||
import com.google.android.gms.maps.model.LatLng;
|
||||
import com.google.android.gms.maps.model.LatLngBounds;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class LocationApi {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(LocationApi.class);
|
||||
|
||||
private GoogleApiClientProvider googleApiClientProvider;
|
||||
|
||||
@Inject
|
||||
public LocationApi(GoogleApiClientProvider googleApiClientProvider) {
|
||||
this.googleApiClientProvider = googleApiClientProvider;
|
||||
}
|
||||
|
||||
public void getPlaceDetails(final String placeId, final ResultCallback<PlaceBuffer> callback) {
|
||||
googleApiClientProvider.getApi(new GoogleApiClientProvider.withApi() {
|
||||
@Override
|
||||
public void doWork(final GoogleApiClient googleApiClient) {
|
||||
Places.GeoDataApi.getPlaceById(googleApiClient, placeId)
|
||||
.setResultCallback(new ResultCallback<PlaceBuffer>() {
|
||||
@Override
|
||||
public void onResult(PlaceBuffer places) {
|
||||
callback.onResult(places);
|
||||
googleApiClient.disconnect();
|
||||
}
|
||||
}, 15, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectionFailed(ConnectionResult connectionResult) {
|
||||
log.error("onConnectionFailed({})", connectionResult);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void getAutocompletePredictions(final String constraint, final ResultCallback<AutocompletePredictionBuffer> callback) {
|
||||
googleApiClientProvider.getApi(new GoogleApiClientProvider.withApi() {
|
||||
@Override
|
||||
public void doWork(final GoogleApiClient googleApiClient) {
|
||||
final LatLngBounds bounds = LatLngBounds.builder().include(getLastKnownLocation(googleApiClient)).build();
|
||||
Places.GeoDataApi.getAutocompletePredictions(googleApiClient, constraint, bounds, null)
|
||||
.setResultCallback(new ResultCallback<AutocompletePredictionBuffer>() {
|
||||
@Override
|
||||
public void onResult(AutocompletePredictionBuffer autocompletePredictions) {
|
||||
callback.onResult(autocompletePredictions);
|
||||
googleApiClient.disconnect();
|
||||
}
|
||||
}, 15, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectionFailed(ConnectionResult connectionResult) {
|
||||
log.error("onConnectionFailed({})", connectionResult);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private LatLng getLastKnownLocation(GoogleApiClient googleApiClient) {
|
||||
try {
|
||||
Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
|
||||
return new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
|
||||
} catch (Exception e) {
|
||||
return new LatLng(0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,97 @@
|
||||
package org.tasks.location;
|
||||
|
||||
import android.content.IntentSender;
|
||||
import android.location.Location;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.google.android.gms.common.ConnectionResult;
|
||||
import com.google.android.gms.common.api.GoogleApiClient;
|
||||
import com.google.android.gms.common.api.ResultCallback;
|
||||
import com.google.android.gms.location.LocationServices;
|
||||
import com.google.android.gms.location.places.AutocompletePredictionBuffer;
|
||||
import com.google.android.gms.location.places.PlaceBuffer;
|
||||
import com.google.android.gms.location.places.Places;
|
||||
import com.google.android.gms.maps.model.LatLng;
|
||||
import com.google.android.gms.maps.model.LatLngBounds;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.tasks.R;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class ManagedGoogleApi extends GoogleApi implements GoogleApi.GoogleApiClientConnectionHandler {
|
||||
private static final int RC_RESOLVE_GPS_ISSUE = 10009;
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ManagedGoogleApi.class);
|
||||
private FragmentActivity fragmentActivity;
|
||||
private GoogleApiClient googleApiClient;
|
||||
|
||||
@Inject
|
||||
public ManagedGoogleApi(FragmentActivity fragmentActivity) {
|
||||
super(fragmentActivity);
|
||||
|
||||
this.fragmentActivity = fragmentActivity;
|
||||
enableAutoManage(fragmentActivity, this);
|
||||
}
|
||||
|
||||
public void connect() {
|
||||
if (googleApiClient == null) {
|
||||
super.connect(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectionFailed(ConnectionResult connectionResult) {
|
||||
if (connectionResult.hasResolution()) {
|
||||
try {
|
||||
connectionResult.startResolutionForResult(fragmentActivity, RC_RESOLVE_GPS_ISSUE);
|
||||
} catch (IntentSender.SendIntentException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
} else {
|
||||
Toast.makeText(fragmentActivity, String.format("%s: %s\n%s",
|
||||
fragmentActivity.getString(R.string.app_name),
|
||||
fragmentActivity.getString(R.string.common_google_play_services_notification_ticker),
|
||||
connectionResult.getErrorCode()), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnect(GoogleApiClient googleApiClient) {
|
||||
this.googleApiClient = googleApiClient;
|
||||
}
|
||||
|
||||
public void getPlaceDetails(final String placeId, final ResultCallback<PlaceBuffer> callback) {
|
||||
Places.GeoDataApi.getPlaceById(googleApiClient, placeId)
|
||||
.setResultCallback(new ResultCallback<PlaceBuffer>() {
|
||||
@Override
|
||||
public void onResult(PlaceBuffer places) {
|
||||
callback.onResult(places);
|
||||
}
|
||||
}, 15, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public void getAutocompletePredictions(final String constraint, final ResultCallback<AutocompletePredictionBuffer> callback) {
|
||||
final LatLngBounds bounds = LatLngBounds.builder().include(getLastKnownLocation(googleApiClient)).build();
|
||||
Places.GeoDataApi.getAutocompletePredictions(googleApiClient, constraint, bounds, null)
|
||||
.setResultCallback(new ResultCallback<AutocompletePredictionBuffer>() {
|
||||
@Override
|
||||
public void onResult(AutocompletePredictionBuffer autocompletePredictions) {
|
||||
callback.onResult(autocompletePredictions);
|
||||
}
|
||||
}, 15, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
private LatLng getLastKnownLocation(GoogleApiClient googleApiClient) {
|
||||
try {
|
||||
Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
|
||||
return new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
|
||||
} catch (Exception e) {
|
||||
return new LatLng(0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,27 @@
|
||||
package org.tasks.injection;
|
||||
|
||||
import android.support.v4.app.DialogFragment;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
|
||||
import org.tasks.dialogs.LocationPickerDialog;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
@Module(addsTo = TasksModule.class,
|
||||
injects = {
|
||||
LocationPickerDialog.class
|
||||
},
|
||||
library = true)
|
||||
public class DialogFragmentModule {
|
||||
private DialogFragment dialogFragment;
|
||||
|
||||
public DialogFragmentModule(DialogFragment dialogFragment) {
|
||||
this.dialogFragment = dialogFragment;
|
||||
}
|
||||
|
||||
@Provides
|
||||
public FragmentActivity getFragmentActivity() {
|
||||
return dialogFragment.getActivity();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
package org.tasks.injection;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.support.v4.app.DialogFragment;
|
||||
|
||||
public class InjectingDialogFragment extends DialogFragment {
|
||||
private boolean injected;
|
||||
|
||||
@Override
|
||||
public void onAttach(Activity activity) {
|
||||
super.onAttach(activity);
|
||||
|
||||
if (!injected) {
|
||||
((Injector) activity).getObjectGraph().plus(new DialogFragmentModule(this)).inject(this);
|
||||
injected = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue