mirror of https://github.com/tasks/tasks
Add LocationPickerActivity
parent
42f4587c01
commit
7222b3824e
@ -1,17 +1,27 @@
|
||||
package org.tasks.dialogs;
|
||||
|
||||
import android.support.v4.app.DialogFragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
|
||||
import org.tasks.activities.LocationPickerActivity;
|
||||
import org.tasks.location.OnLocationPickedHandler;
|
||||
|
||||
@SuppressWarnings("EmptyMethod")
|
||||
public class LocationPickerDialog {
|
||||
@SuppressWarnings({"EmptyMethod", "UnusedParameters"})
|
||||
public class LocationPickerDialog extends DialogFragment {
|
||||
|
||||
public LocationPickerDialog(OnLocationPickedHandler onLocationPickedHandler) {
|
||||
public LocationPickerDialog() {
|
||||
|
||||
}
|
||||
|
||||
public void show(FragmentManager childFragmentManager, String fragTagLocationPicker) {
|
||||
|
||||
}
|
||||
|
||||
public void setOnLocationPickedHandler(LocationPickerActivity locationPickerActivity) {
|
||||
|
||||
}
|
||||
|
||||
public void setOnCancelListener(LocationPickerActivity locationPickerActivity) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,97 +0,0 @@
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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) {
|
||||
log.error(e.getMessage(), e);
|
||||
return new LatLng(0, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,47 @@
|
||||
package org.tasks.activities;
|
||||
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
|
||||
import org.tasks.dialogs.LocationPickerDialog;
|
||||
import org.tasks.injection.InjectingAppCompatActivity;
|
||||
import org.tasks.location.Geofence;
|
||||
import org.tasks.location.OnLocationPickedHandler;
|
||||
|
||||
public class LocationPickerActivity extends InjectingAppCompatActivity implements OnLocationPickedHandler, DialogInterface.OnCancelListener {
|
||||
|
||||
private static final String FRAG_TAG_LOCATION_PICKER = "frag_tag_location_picker";
|
||||
|
||||
public static final String EXTRA_GEOFENCE = "extra_geofence";
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
FragmentManager supportFragmentManager = getSupportFragmentManager();
|
||||
LocationPickerDialog dialog = (LocationPickerDialog) supportFragmentManager.findFragmentByTag(FRAG_TAG_LOCATION_PICKER);
|
||||
if (dialog == null) {
|
||||
dialog = new LocationPickerDialog();
|
||||
dialog.show(supportFragmentManager, FRAG_TAG_LOCATION_PICKER);
|
||||
}
|
||||
dialog.setOnCancelListener(this);
|
||||
dialog.setOnLocationPickedHandler(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLocationPicked(final Geofence geofence) {
|
||||
setResult(RESULT_OK, new Intent() {{
|
||||
putExtra(EXTRA_GEOFENCE, geofence);
|
||||
}});
|
||||
finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancel(DialogInterface dialog) {
|
||||
setResult(RESULT_CANCELED);
|
||||
finish();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue