mirror of https://github.com/tasks/tasks
parent
6f9d2ae273
commit
6f6c72faff
@ -1,27 +0,0 @@
|
|||||||
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", "UnusedParameters"})
|
|
||||||
public class LocationPickerDialog extends DialogFragment {
|
|
||||||
|
|
||||||
public LocationPickerDialog() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void show(FragmentManager childFragmentManager, String fragTagLocationPicker) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setOnLocationPickedHandler(LocationPickerActivity locationPickerActivity) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setOnCancelListener(LocationPickerActivity locationPickerActivity) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,11 +0,0 @@
|
|||||||
package org.tasks.location;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
public class LocationApi {
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
public LocationApi() {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,16 @@
|
|||||||
|
package org.tasks.location;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.content.Intent;
|
||||||
|
|
||||||
|
import org.tasks.preferences.Preferences;
|
||||||
|
|
||||||
|
public class PlacePicker {
|
||||||
|
public static Intent getIntent(Activity activity) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Geofence getPlace(Activity activity, Intent data, Preferences preferences) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,154 +0,0 @@
|
|||||||
package org.tasks.dialogs;
|
|
||||||
|
|
||||||
import android.app.Dialog;
|
|
||||||
import android.content.DialogInterface;
|
|
||||||
import android.content.IntentSender;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.support.annotation.NonNull;
|
|
||||||
import android.support.v4.app.FragmentActivity;
|
|
||||||
import android.view.LayoutInflater;
|
|
||||||
import android.view.View;
|
|
||||||
import android.widget.AdapterView;
|
|
||||||
import android.widget.AutoCompleteTextView;
|
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import com.google.android.gms.common.ConnectionResult;
|
|
||||||
import com.google.android.gms.common.GooglePlayServicesUtil;
|
|
||||||
import com.google.android.gms.common.api.GoogleApiClient;
|
|
||||||
import com.google.android.gms.common.api.ResultCallback;
|
|
||||||
import com.google.android.gms.location.places.Place;
|
|
||||||
import com.google.android.gms.location.places.PlaceBuffer;
|
|
||||||
import com.google.android.gms.maps.model.LatLng;
|
|
||||||
|
|
||||||
import org.tasks.R;
|
|
||||||
import org.tasks.injection.InjectingDialogFragment;
|
|
||||||
import org.tasks.location.Geofence;
|
|
||||||
import org.tasks.location.GoogleApi;
|
|
||||||
import org.tasks.location.OnLocationPickedHandler;
|
|
||||||
import org.tasks.location.PlaceAutocompleteAdapter;
|
|
||||||
import org.tasks.preferences.ActivityPreferences;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
import timber.log.Timber;
|
|
||||||
|
|
||||||
public class LocationPickerDialog extends InjectingDialogFragment implements GoogleApiClient.OnConnectionFailedListener {
|
|
||||||
|
|
||||||
private static final int RC_RESOLVE_GPS_ISSUE = 10009;
|
|
||||||
|
|
||||||
private PlaceAutocompleteAdapter mAdapter;
|
|
||||||
|
|
||||||
@Inject FragmentActivity fragmentActivity;
|
|
||||||
@Inject GoogleApi googleApi;
|
|
||||||
@Inject DialogBuilder dialogBuilder;
|
|
||||||
@Inject ActivityPreferences activityPreferences;
|
|
||||||
|
|
||||||
private OnLocationPickedHandler onLocationPickedHandler;
|
|
||||||
private DialogInterface.OnCancelListener onCancelListener;
|
|
||||||
private boolean resolvingError;
|
|
||||||
|
|
||||||
public void setOnLocationPickedHandler(OnLocationPickedHandler onLocationPickedHandler) {
|
|
||||||
this.onLocationPickedHandler = onLocationPickedHandler;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NonNull
|
|
||||||
@Override
|
|
||||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
|
||||||
activityPreferences.applyDialogTheme();
|
|
||||||
|
|
||||||
googleApi.connect(this);
|
|
||||||
|
|
||||||
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
|
|
||||||
View view = layoutInflater.inflate(R.layout.location_picker_dialog, null);
|
|
||||||
AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) view.findViewById(R.id.address_entry);
|
|
||||||
autoCompleteTextView.setOnItemClickListener(mAutocompleteClickListener);
|
|
||||||
mAdapter = new PlaceAutocompleteAdapter(googleApi, fragmentActivity, android.R.layout.simple_list_item_1);
|
|
||||||
autoCompleteTextView.setAdapter(mAdapter);
|
|
||||||
|
|
||||||
return dialogBuilder.newDialog()
|
|
||||||
.setView(view)
|
|
||||||
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
|
||||||
if (onCancelListener != null) {
|
|
||||||
onCancelListener.onCancel(dialog);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.show();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDestroyView() {
|
|
||||||
super.onDestroyView();
|
|
||||||
|
|
||||||
googleApi.disconnect();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void error(String text) {
|
|
||||||
Timber.e(text);
|
|
||||||
Toast.makeText(fragmentActivity, text, Toast.LENGTH_LONG).show();
|
|
||||||
}
|
|
||||||
|
|
||||||
private AdapterView.OnItemClickListener mAutocompleteClickListener
|
|
||||||
= new AdapterView.OnItemClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
|
||||||
final PlaceAutocompleteAdapter.PlaceAutocomplete item = mAdapter.getItem(position);
|
|
||||||
final String placeId = String.valueOf(item.placeId);
|
|
||||||
Timber.i("Autocomplete item selected: %s", item.description);
|
|
||||||
googleApi.getPlaceDetails(placeId, mUpdatePlaceDetailsCallback);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback
|
|
||||||
= new ResultCallback<PlaceBuffer>() {
|
|
||||||
@Override
|
|
||||||
public void onResult(PlaceBuffer places) {
|
|
||||||
if (places.getStatus().isSuccess()) {
|
|
||||||
final Place place = places.get(0);
|
|
||||||
LatLng latLng = place.getLatLng();
|
|
||||||
Geofence geofence = new Geofence(place.getName().toString(), latLng.latitude, latLng.longitude, activityPreferences.getIntegerFromString(R.string.p_geofence_radius, 250));
|
|
||||||
Timber.i("Picked %s", geofence);
|
|
||||||
onLocationPickedHandler.onLocationPicked(geofence);
|
|
||||||
dismiss();
|
|
||||||
} else {
|
|
||||||
error("Error looking up location details - " + places.getStatus().toString());
|
|
||||||
}
|
|
||||||
places.release();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public void setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) {
|
|
||||||
this.onCancelListener = onCancelListener;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCancel(DialogInterface dialog) {
|
|
||||||
super.onCancel(dialog);
|
|
||||||
|
|
||||||
if (onCancelListener != null) {
|
|
||||||
onCancelListener.onCancel(dialog);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onConnectionFailed(ConnectionResult connectionResult) {
|
|
||||||
if (resolvingError) {
|
|
||||||
Timber.i("Ignoring %s, already resolving error", connectionResult);
|
|
||||||
} else if (connectionResult.hasResolution()) {
|
|
||||||
try {
|
|
||||||
resolvingError = true;
|
|
||||||
connectionResult.startResolutionForResult(fragmentActivity, RC_RESOLVE_GPS_ISSUE);
|
|
||||||
} catch (IntentSender.SendIntentException e) {
|
|
||||||
Timber.e(e, e.getMessage());
|
|
||||||
googleApi.connect(this);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
resolvingError = true;
|
|
||||||
GooglePlayServicesUtil
|
|
||||||
.getErrorDialog(connectionResult.getErrorCode(), fragmentActivity, RC_RESOLVE_GPS_ISSUE)
|
|
||||||
.show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,117 +0,0 @@
|
|||||||
package org.tasks.location;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.widget.ArrayAdapter;
|
|
||||||
import android.widget.Filter;
|
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import com.google.android.gms.common.api.ResultCallback;
|
|
||||||
import com.google.android.gms.common.api.Status;
|
|
||||||
import com.google.android.gms.location.places.AutocompletePrediction;
|
|
||||||
import com.google.android.gms.location.places.AutocompletePredictionBuffer;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import timber.log.Timber;
|
|
||||||
|
|
||||||
public class PlaceAutocompleteAdapter
|
|
||||||
extends ArrayAdapter<PlaceAutocompleteAdapter.PlaceAutocomplete> {
|
|
||||||
|
|
||||||
private final GoogleApi googleApi;
|
|
||||||
|
|
||||||
private List<PlaceAutocomplete> mResultList = new ArrayList<>();
|
|
||||||
|
|
||||||
public PlaceAutocompleteAdapter(GoogleApi googleApi, Context context, int resource) {
|
|
||||||
super(context, resource);
|
|
||||||
this.googleApi = googleApi;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getCount() {
|
|
||||||
return mResultList == null ? 0 : mResultList.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PlaceAutocomplete getItem(int position) {
|
|
||||||
return mResultList.get(position);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void getAutocomplete(CharSequence constraint) {
|
|
||||||
googleApi.getAutocompletePredictions(constraint.toString(), onResults);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Filter getFilter() {
|
|
||||||
return new Filter() {
|
|
||||||
@Override
|
|
||||||
protected FilterResults performFiltering(CharSequence constraint) {
|
|
||||||
FilterResults filterResults = new FilterResults();
|
|
||||||
if (constraint != null) {
|
|
||||||
getAutocomplete(constraint);
|
|
||||||
}
|
|
||||||
filterResults.values = mResultList;
|
|
||||||
filterResults.count = mResultList.size();
|
|
||||||
return filterResults;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void publishResults(CharSequence constraint, FilterResults results) {
|
|
||||||
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private ResultCallback<AutocompletePredictionBuffer> onResults = new ResultCallback<AutocompletePredictionBuffer>() {
|
|
||||||
@Override
|
|
||||||
public void onResult(AutocompletePredictionBuffer autocompletePredictions) {
|
|
||||||
final Status status = autocompletePredictions.getStatus();
|
|
||||||
if (!status.isSuccess()) {
|
|
||||||
Toast.makeText(getContext(), "Error contacting API: " + status.toString(),
|
|
||||||
Toast.LENGTH_SHORT).show();
|
|
||||||
Timber.e("Error getting autocomplete prediction API call: %s", status.toString());
|
|
||||||
autocompletePredictions.release();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Timber.i("Query completed. Received %s predictions", autocompletePredictions.getCount());
|
|
||||||
|
|
||||||
Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator();
|
|
||||||
List<PlaceAutocomplete> resultList = new ArrayList<>(autocompletePredictions.getCount());
|
|
||||||
while (iterator.hasNext()) {
|
|
||||||
AutocompletePrediction prediction = iterator.next();
|
|
||||||
resultList.add(new PlaceAutocomplete(prediction.getPlaceId(),
|
|
||||||
prediction.getDescription()));
|
|
||||||
}
|
|
||||||
|
|
||||||
setResults(resultList);
|
|
||||||
autocompletePredictions.release();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private void setResults(List<PlaceAutocomplete> results) {
|
|
||||||
mResultList = results;
|
|
||||||
if (mResultList != null && mResultList.size() > 0) {
|
|
||||||
notifyDataSetChanged();
|
|
||||||
} else {
|
|
||||||
notifyDataSetInvalidated();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class PlaceAutocomplete {
|
|
||||||
|
|
||||||
public CharSequence placeId;
|
|
||||||
public CharSequence description;
|
|
||||||
|
|
||||||
PlaceAutocomplete(CharSequence placeId, CharSequence description) {
|
|
||||||
this.placeId = placeId;
|
|
||||||
this.description = description;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return description.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,41 @@
|
|||||||
|
package org.tasks.location;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
|
||||||
|
import com.google.android.gms.common.GooglePlayServicesRepairableException;
|
||||||
|
import com.google.android.gms.location.places.Place;
|
||||||
|
import com.google.android.gms.maps.model.LatLng;
|
||||||
|
|
||||||
|
import org.tasks.R;
|
||||||
|
import org.tasks.preferences.Preferences;
|
||||||
|
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
public class PlacePicker {
|
||||||
|
public static Intent getIntent(Activity activity) {
|
||||||
|
com.google.android.gms.location.places.ui.PlacePicker.IntentBuilder builder =
|
||||||
|
new com.google.android.gms.location.places.ui.PlacePicker.IntentBuilder();
|
||||||
|
try {
|
||||||
|
return builder.build(activity);
|
||||||
|
} catch (GooglePlayServicesRepairableException e) {
|
||||||
|
Timber.e(e, e.getMessage());
|
||||||
|
activity.startActivity(e.getIntent());
|
||||||
|
} catch (GooglePlayServicesNotAvailableException e) {
|
||||||
|
Timber.e(e, e.getMessage());
|
||||||
|
Toast.makeText(activity, R.string.common_google_play_services_notification_ticker, Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Geofence getPlace(Context context, Intent data, Preferences preferences) {
|
||||||
|
Place place = com.google.android.gms.location.places.ui.PlacePicker.getPlace(context, data);
|
||||||
|
LatLng latLng = place.getLatLng();
|
||||||
|
Geofence geofence = new Geofence(place.getName().toString(), latLng.latitude, latLng.longitude, preferences.getIntegerFromString(R.string.p_geofence_radius, 250));
|
||||||
|
Timber.i("Picked %s", geofence);
|
||||||
|
return geofence;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,28 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
|
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
android:orientation="vertical"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:paddingLeft="@dimen/horizontal_page_margin"
|
|
||||||
android:paddingRight="@dimen/horizontal_page_margin"
|
|
||||||
android:paddingTop="@dimen/vertical_page_margin"
|
|
||||||
android:paddingBottom="@dimen/vertical_page_margin">
|
|
||||||
|
|
||||||
<AutoCompleteTextView
|
|
||||||
android:layout_width="fill_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:gravity="top"
|
|
||||||
android:layout_gravity="top"
|
|
||||||
android:inputType="textPostalAddress"
|
|
||||||
android:imeOptions="flagNoExtractUi"
|
|
||||||
android:hint="@string/type_a_location"
|
|
||||||
android:id="@+id/address_entry" />
|
|
||||||
|
|
||||||
<ImageView
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:src="@drawable/powered_by_google_light"
|
|
||||||
android:layout_gravity="end"/>
|
|
||||||
|
|
||||||
</LinearLayout>
|
|
||||||
@ -1,54 +0,0 @@
|
|||||||
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;
|
|
||||||
import org.tasks.preferences.ActivityPreferences;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
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";
|
|
||||||
|
|
||||||
@Inject ActivityPreferences activityPreferences;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
|
|
||||||
activityPreferences.applyDialogTheme();
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,5 +0,0 @@
|
|||||||
package org.tasks.location;
|
|
||||||
|
|
||||||
public interface OnLocationPickedHandler {
|
|
||||||
void onLocationPicked(Geofence geofence);
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue