From cce8715fad808d9f894b4ccf096c3c8f73836ac2 Mon Sep 17 00:00:00 2001 From: maskara Date: Sun, 14 May 2017 11:33:39 +0200 Subject: [PATCH] Showing an overlay dialog when a nearby item is tapped --- app/src/main/assets/queries/nearby_query.txt | 43 +++++++ .../nrw/commons/nearby/NearbyBaseMarker.java | 88 ++++++++++++++ .../nrw/commons/nearby/NearbyController.java | 36 +++--- .../nrw/commons/nearby/NearbyInfoDialog.java | 110 ++++++++++++++++++ .../commons/nearby/NearbyListFragment.java | 10 +- .../nrw/commons/nearby/NearbyMapFragment.java | 26 ++++- .../free/nrw/commons/nearby/NearbyMarker.java | 31 +++++ .../free/nrw/commons/nearby/NearbyPlaces.java | 73 +++++------- .../fr/free/nrw/commons/nearby/Place.java | 6 +- .../nrw/commons/ui/widget/OverlayDialog.java | 48 ++++++++ .../fr/free/nrw/commons/utils/DialogUtil.java | 80 +++++++++++++ .../fr/free/nrw/commons/utils/FileUtils.java | 33 ++++++ .../main/res/layout/dialog_nearby_info.xml | 97 +++++++++++++++ app/src/main/res/values/dimens.xml | 1 + app/src/main/res/values/styles.xml | 6 + 15 files changed, 609 insertions(+), 79 deletions(-) create mode 100644 app/src/main/assets/queries/nearby_query.txt create mode 100644 app/src/main/java/fr/free/nrw/commons/nearby/NearbyBaseMarker.java create mode 100644 app/src/main/java/fr/free/nrw/commons/nearby/NearbyInfoDialog.java create mode 100644 app/src/main/java/fr/free/nrw/commons/nearby/NearbyMarker.java create mode 100644 app/src/main/java/fr/free/nrw/commons/ui/widget/OverlayDialog.java create mode 100644 app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.java create mode 100644 app/src/main/java/fr/free/nrw/commons/utils/FileUtils.java create mode 100644 app/src/main/res/layout/dialog_nearby_info.xml diff --git a/app/src/main/assets/queries/nearby_query.txt b/app/src/main/assets/queries/nearby_query.txt new file mode 100644 index 000000000..02f741228 --- /dev/null +++ b/app/src/main/assets/queries/nearby_query.txt @@ -0,0 +1,43 @@ +SELECT + (SAMPLE(?location) as ?location) + ?item + (SAMPLE(COALESCE(?item_label_preferred_language, ?item_label_any_language)) as ?label) + (SAMPLE(?classId) as ?class) + (SAMPLE(COALESCE(?class_label_preferred_language, ?class_label_any_language, "?")) as ?class_label) + (SAMPLE(COALESCE(?icon0, ?icon1)) as ?icon) + (SAMPLE(COALESCE(?emoji0, ?emoji1)) as ?emoji) + (SAMPLE(?sitelink) as ?sitelink) + WHERE { + # Around given location... + SERVICE wikibase:around { + ?item wdt:P625 ?location. + bd:serviceParam wikibase:center "Point(${LONG} ${LAT})"^^geo:wktLiteral. + bd:serviceParam wikibase:radius "${RADIUS}" . # Radius in kilometers. + } + + # ... and without an image. + MINUS {?item wdt:P18 []} + + # Get the label in the preferred language of the user, or any other language if no label is available in that language. + OPTIONAL {?item rdfs:label ?item_label_preferred_language. FILTER (lang(?item_label_preferred_language) = "${LANG}")} + OPTIONAL {?item rdfs:label ?item_label_any_language} + + # Get the class label in the preferred language of the user, or any other language if no label is available in that language. + OPTIONAL { + ?item p:P31/ps:P31 ?classId. + OPTIONAL {?classId rdfs:label ?class_label_preferred_language. FILTER (lang(?class_label_preferred_language) = "${LANG}")} + OPTIONAL {?classId rdfs:label ?class_label_any_language} + + # Get icon + OPTIONAL { ?classId wdt:P2910 ?icon0. } + OPTIONAL { ?classId wdt:P279*/wdt:P2910 ?icon1. } + # Get emoji + OPTIONAL { ?classId wdt:P487 ?emoji0. } + OPTIONAL { ?classId wdt:P279*/wdt:P487 ?emoji1. } + OPTIONAL { + ?sitelink schema:about ?item . + ?sitelink schema:inLanguage "en" + } + } + } + GROUP BY ?item \ No newline at end of file diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyBaseMarker.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyBaseMarker.java new file mode 100644 index 000000000..686b3e6ce --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyBaseMarker.java @@ -0,0 +1,88 @@ +package fr.free.nrw.commons.nearby; + +import android.graphics.Bitmap; +import android.net.Uri; +import android.os.Parcel; +import android.os.Parcelable; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions; +import com.mapbox.mapboxsdk.annotations.Icon; +import com.mapbox.mapboxsdk.annotations.IconFactory; +import com.mapbox.mapboxsdk.geometry.LatLng; + +import fr.free.nrw.commons.utils.UriDeserializer; +import fr.free.nrw.commons.utils.UriSerializer; + +public class NearbyBaseMarker extends BaseMarkerOptions { + private Place place; + public NearbyBaseMarker() { + + } + + public NearbyBaseMarker place(Place place) { + this.place = place; + return getThis(); + } + + public NearbyBaseMarker(Parcel in) { + Gson gson = new GsonBuilder() + .registerTypeAdapter(Uri.class, new UriDeserializer()) + .create(); + + position((LatLng) in.readParcelable(LatLng.class.getClassLoader())); + snippet(in.readString()); + String iconId = in.readString(); + Bitmap iconBitmap = in.readParcelable(Bitmap.class.getClassLoader()); + Icon icon = IconFactory.recreate(iconId, iconBitmap); + icon(icon); + title(in.readString()); + String gsonString = in.readString(); + place(gson.fromJson(gsonString, Place.class)); + } + + @Override + public NearbyBaseMarker getThis() { + return this; + } + + @Override + public NearbyMarker getMarker() { + return new NearbyMarker(this, place); + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + Gson gson = new GsonBuilder() + .registerTypeAdapter(Uri.class, new UriSerializer()) + .create(); + + dest.writeParcelable(position, flags); + dest.writeString(snippet); + dest.writeString(icon.getId()); + dest.writeParcelable(icon.getBitmap(), flags); + dest.writeString(title); + dest.writeString(gson.toJson(place)); + } + + public Place getPlace() { + return place; + } + + public static final Parcelable.Creator CREATOR + = new Parcelable.Creator() { + public NearbyBaseMarker createFromParcel(Parcel in) { + return new NearbyBaseMarker(in); + } + + public NearbyBaseMarker[] newArray(int size) { + return new NearbyBaseMarker[size]; + } + }; +} diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java index dc36d165e..ba2aec1d9 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java @@ -1,17 +1,10 @@ package fr.free.nrw.commons.nearby; -import static fr.free.nrw.commons.utils.LengthUtils.computeDistanceBetween; -import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween; - import android.content.Context; import android.content.SharedPreferences; +import android.graphics.drawable.Icon; import android.preference.PreferenceManager; -import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions; -import com.mapbox.mapboxsdk.annotations.MarkerOptions; - -import fr.free.nrw.commons.location.LatLng; - import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -20,8 +13,12 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import fr.free.nrw.commons.location.LatLng; import timber.log.Timber; +import static fr.free.nrw.commons.utils.LengthUtils.computeDistanceBetween; +import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween; + public class NearbyController { private static final int MAX_RESULTS = 1000; @@ -40,7 +37,9 @@ public class NearbyController { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); List places = prefs.getBoolean("useWikidata", true) ? NearbyPlaces.getInstance().getFromWikidataQuery( - curLatLng, Locale.getDefault().getLanguage()) + context, + curLatLng, + Locale.getDefault().getLanguage()) : NearbyPlaces.getInstance().getFromWikiNeedsPictures(); if (curLatLng != null) { Timber.d("Sorting places by distance..."); @@ -85,19 +84,24 @@ public class NearbyController { * @param placeList list of nearby places in Place data type * @return BaseMarkerOprions list that holds nearby places */ - public static List loadAttractionsFromLocationToBaseMarkerOptions( + public static List loadAttractionsFromLocationToBaseMarkerOptions( LatLng curLatLng, List placeList) { - List baseMarkerOptionses = new ArrayList<>(); + List baseMarkerOptionses = new ArrayList<>(); placeList = placeList.subList(0, Math.min(placeList.size(), MAX_RESULTS)); for (Place place: placeList) { String distance = formatDistanceBetween(curLatLng, place.location); place.setDistance(distance); - baseMarkerOptionses.add(new MarkerOptions() - .position(new com.mapbox.mapboxsdk.geometry - .LatLng(place.location.latitude,place.location.longitude)) - .title(place.name) - .snippet(place.description)); + + NearbyBaseMarker nearbyBaseMarker = new NearbyBaseMarker(); + nearbyBaseMarker.title(place.name); + nearbyBaseMarker.position( + new com.mapbox.mapboxsdk.geometry.LatLng( + place.location.latitude, + place.location.longitude)); + nearbyBaseMarker.place(place); + + baseMarkerOptionses.add(nearbyBaseMarker); } return baseMarkerOptionses; } diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyInfoDialog.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyInfoDialog.java new file mode 100644 index 000000000..e777eb3ee --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyInfoDialog.java @@ -0,0 +1,110 @@ +package fr.free.nrw.commons.nearby; + +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.support.v4.app.FragmentActivity; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import butterknife.BindView; +import butterknife.ButterKnife; +import butterknife.OnClick; +import butterknife.Unbinder; +import fr.free.nrw.commons.R; +import fr.free.nrw.commons.Utils; +import fr.free.nrw.commons.location.LatLng; +import fr.free.nrw.commons.ui.widget.OverlayDialog; +import fr.free.nrw.commons.utils.DialogUtil; + +public class NearbyInfoDialog extends OverlayDialog { + + private final static String ARG_TITLE = "placeTitle"; + private final static String ARG_DESC = "placeDesc"; + private final static String ARG_LATITUDE = "latitude"; + private final static String ARG_LONGITUDE = "longitude"; + private final static String ARG_ARTICLE_LINK = "articleLink"; + private final static String ARG_WIKI_DATA_LINK = "wikiDataLink"; + + @BindView(R.id.link_preview_title) + TextView placeTitle; + @BindView(R.id.link_preview_extract) + TextView placeDescription; + + @BindView(R.id.link_preview_go_button) + TextView goToButton; + + private Unbinder unbinder; + + private LatLng location; + private Uri articleLink; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.dialog_nearby_info, container, false); + unbinder = ButterKnife.bind(this, view); + initUi(); + return view; + } + + private void initUi() { + Bundle bundle = getArguments(); + placeTitle.setText(bundle.getString(ARG_TITLE)); + placeDescription.setText(bundle.getString(ARG_DESC)); + location = new LatLng(bundle.getDouble(ARG_LATITUDE), bundle.getDouble(ARG_LONGITUDE)); + getArticleLink(bundle); + } + + private void getArticleLink(Bundle bundle) { + String articleLink = bundle.getString(ARG_ARTICLE_LINK); + articleLink = articleLink.replace("<", "").replace(">", ""); + + if (Utils.isNullOrWhiteSpace(articleLink) || articleLink == "\n") { + articleLink = bundle.getString(ARG_WIKI_DATA_LINK).replace("<", "").replace(">", ""); + } + + if (!Utils.isNullOrWhiteSpace(articleLink) && articleLink != "\n") { + this.articleLink = Uri.parse(articleLink); + } else { + goToButton.setVisibility(View.GONE); + } + } + + public static void showYourself(FragmentActivity fragmentActivity, Place place) { + NearbyInfoDialog mDialog = new NearbyInfoDialog(); + Bundle bundle = new Bundle(); + bundle.putString(ARG_TITLE, place.name); + bundle.putString(ARG_DESC, place.description); + bundle.putDouble(ARG_LATITUDE, place.location.latitude); + bundle.putDouble(ARG_LONGITUDE, place.location.longitude); + bundle.putString(ARG_ARTICLE_LINK, place.siteLink.toString()); + bundle.putString(ARG_WIKI_DATA_LINK, place.wikiDataLink.toString()); + mDialog.setArguments(bundle); + DialogUtil.showSafely(fragmentActivity, mDialog); + } + + @Override + public void onDestroyView() { + super.onDestroyView(); + unbinder.unbind(); + } + + @OnClick(R.id.link_preview_directions_button) + void onDirectionsClick() { + //Open map app at given position + Uri gmmIntentUri = Uri.parse("geo:0,0?q=" + location.latitude + "," + location.longitude); + Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri); + + if (mapIntent.resolveActivity(getActivity().getPackageManager()) != null) { + startActivity(mapIntent); + } + } + + @OnClick(R.id.link_preview_go_button) + void onReadArticleClick() { + Intent browserIntent = new Intent(Intent.ACTION_VIEW, articleLink); + startActivity(browserIntent); + } +} diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyListFragment.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyListFragment.java index eafa10382..048f1d450 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyListFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyListFragment.java @@ -28,8 +28,6 @@ import java.util.List; import timber.log.Timber; public class NearbyListFragment extends ListFragment { - - //private NearbyAsyncTask nearbyAsyncTask; private Gson gson; private List placeList; private LatLng curLatLng; @@ -98,12 +96,6 @@ public class NearbyListFragment extends ListFragment { Timber.d("Item at position %d has coords: Lat: %f Long: %f", position, latitude, longitude); - //Open map app at given position - Uri gmmIntentUri = Uri.parse("geo:0,0?q=" + latitude + "," + longitude); - Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri); - - if (mapIntent.resolveActivity(getActivity().getPackageManager()) != null) { - startActivity(mapIntent); - } + NearbyInfoDialog.showYourself(getActivity(), place); } } diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyMapFragment.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyMapFragment.java index 07702c6c0..de92a596b 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyMapFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyMapFragment.java @@ -2,6 +2,7 @@ package fr.free.nrw.commons.nearby; import android.net.Uri; import android.os.Bundle; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; @@ -11,7 +12,7 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import com.mapbox.mapboxsdk.Mapbox; -import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions; +import com.mapbox.mapboxsdk.annotations.Marker; import com.mapbox.mapboxsdk.camera.CameraPosition; import com.mapbox.mapboxsdk.constants.Style; import com.mapbox.mapboxsdk.geometry.LatLng; @@ -21,18 +22,17 @@ import com.mapbox.mapboxsdk.maps.MapboxMapOptions; import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; import com.mapbox.services.android.telemetry.MapboxTelemetry; -import fr.free.nrw.commons.R; -import fr.free.nrw.commons.utils.UriDeserializer; - import java.lang.reflect.Type; import java.util.List; +import fr.free.nrw.commons.R; +import fr.free.nrw.commons.utils.UriDeserializer; + public class NearbyMapFragment extends android.support.v4.app.Fragment { - //private NearbyAsyncTask nearbyAsyncTask; private MapView mapView; private Gson gson; private List placeList; - private List baseMarkerOptionses; + private List baseMarkerOptionses; private fr.free.nrw.commons.location.LatLng curLatLng; public NearbyMapFragment() { @@ -79,9 +79,23 @@ public class NearbyMapFragment extends android.support.v4.app.Fragment { @Override public void onMapReady(MapboxMap mapboxMap) { mapboxMap.addMarkers(baseMarkerOptionses); + + mapboxMap.setOnMarkerClickListener(new MapboxMap.OnMarkerClickListener() { + @Override + public boolean onMarkerClick(@NonNull Marker marker) { + if (marker instanceof NearbyMarker) { + NearbyMarker nearbyMarker = (NearbyMarker) marker; + Place place = nearbyMarker.getNearbyBaseMarker().getPlace(); + NearbyInfoDialog.showYourself(getActivity(), place); + } + return false; + } + }); } }); + setHasOptionsMenu(false); + return mapView; } diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyMarker.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyMarker.java new file mode 100644 index 000000000..c65ede203 --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyMarker.java @@ -0,0 +1,31 @@ +package fr.free.nrw.commons.nearby; + +import com.mapbox.mapboxsdk.annotations.Marker; + +public class NearbyMarker extends Marker { + private Place place; + private NearbyBaseMarker nearbyBaseMarker; + + /** + * Creates a instance of {@link Marker} using the builder of Marker. + * + * @param baseMarkerOptions The builder used to construct the Marker. + */ + public NearbyMarker(NearbyBaseMarker baseMarkerOptions, Place place) { + super(baseMarkerOptions); + this.place = place; + this.nearbyBaseMarker = baseMarkerOptions; + } + + public NearbyBaseMarker getNearbyBaseMarker() { + return nearbyBaseMarker; + } + + public Place getPlace() { + return place; + } + + public void setNearbyBaseMarker(NearbyBaseMarker nearbyBaseMarker) { + this.nearbyBaseMarker = nearbyBaseMarker; + } +} diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyPlaces.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyPlaces.java index 44d415505..a6d08be04 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyPlaces.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyPlaces.java @@ -1,11 +1,9 @@ package fr.free.nrw.commons.nearby; +import android.content.Context; import android.net.Uri; import android.os.StrictMode; -import fr.free.nrw.commons.Utils; -import fr.free.nrw.commons.location.LatLng; - import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; @@ -19,6 +17,9 @@ import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; +import fr.free.nrw.commons.Utils; +import fr.free.nrw.commons.location.LatLng; +import fr.free.nrw.commons.utils.FileUtils; import timber.log.Timber; public class NearbyPlaces { @@ -28,44 +29,6 @@ public class NearbyPlaces { private static final double MAX_RADIUS = 300.0; private static final double RADIUS_MULTIPLIER = 1.618; private static final String WIKIDATA_QUERY_URL = "https://query.wikidata.org/sparql?query=${QUERY}"; - private static final String WIKIDATA_QUERY_TEMPLATE = "SELECT\n" + - " (SAMPLE(?location) as ?location)\n" + - " ?item\n" + - " (SAMPLE(COALESCE(?item_label_preferred_language, ?item_label_any_language)) as ?label)\n" + - " (SAMPLE(?classId) as ?class)\n" + - " (SAMPLE(COALESCE(?class_label_preferred_language, ?class_label_any_language, \"?\")) as ?class_label)\n" + - " (SAMPLE(COALESCE(?icon0, ?icon1)) as ?icon)\n" + - " (SAMPLE(COALESCE(?emoji0, ?emoji1)) as ?emoji)\n" + - "WHERE {\n" + - " # Around given location...\n" + - " SERVICE wikibase:around {\n" + - " ?item wdt:P625 ?location.\n" + - " bd:serviceParam wikibase:center \"Point(${LONG} ${LAT})\"^^geo:wktLiteral. \n" + - " bd:serviceParam wikibase:radius \"${RADIUS}\" . # Radius in kilometers.\n" + - " }\n" + - " \n" + - " # ... and without an image.\n" + - " MINUS {?item wdt:P18 []}\n" + - " \n" + - " # Get the label in the preferred language of the user, or any other language if no label is available in that language.\n" + - " OPTIONAL {?item rdfs:label ?item_label_preferred_language. FILTER (lang(?item_label_preferred_language) = \"${LANG}\")}\n" + - " OPTIONAL {?item rdfs:label ?item_label_any_language}\n" + - " \n" + - " # Get the class label in the preferred language of the user, or any other language if no label is available in that language.\n" + - " OPTIONAL {\n" + - " ?item p:P31/ps:P31 ?classId.\n" + - " OPTIONAL {?classId rdfs:label ?class_label_preferred_language. FILTER (lang(?class_label_preferred_language) = \"${LANG}\")}\n" + - " OPTIONAL {?classId rdfs:label ?class_label_any_language}\n" + - "\n" + - " # Get icon\n" + - " OPTIONAL { ?classId wdt:P2910 ?icon0. }\n" + - " OPTIONAL { ?classId wdt:P279*/wdt:P2910 ?icon1. }\n" + - " # Get emoji\n" + - " OPTIONAL { ?classId wdt:P487 ?emoji0. }\n" + - " OPTIONAL { ?classId wdt:P279*/wdt:P487 ?emoji1. }\n" + - " }\n" + - "}\n" + - "GROUP BY ?item\n"; private static NearbyPlaces singleton; private double radius = INITIAL_RADIUS; private List places; @@ -73,13 +36,15 @@ public class NearbyPlaces { private NearbyPlaces(){ } - List getFromWikidataQuery(LatLng curLatLng, String lang) { + List getFromWikidataQuery(Context context, + LatLng curLatLng, + String lang) { List places = Collections.emptyList(); try { // increase the radius gradually to find a satisfactory number of nearby places while (radius < MAX_RADIUS) { - places = getFromWikidataQuery(curLatLng, lang, radius); + places = getFromWikidataQuery(context, curLatLng, lang, radius); Timber.d("%d results at radius: %f", places.size(), radius); if (places.size() >= MIN_RESULTS) { break; @@ -97,10 +62,18 @@ public class NearbyPlaces { return places; } - private List getFromWikidataQuery(LatLng cur, String lang, double radius) + private List getFromWikidataQuery(Context context, + LatLng cur, + String lang, + double radius) throws IOException { List places = new ArrayList<>(); - String query = WIKIDATA_QUERY_TEMPLATE.replace("${RADIUS}", "" + radius) + + String query = FileUtils.readFromFile(context, "queries/nearby_query.txt"); + + Timber.d(query); + + query = query.replace("${RADIUS}", "" + radius) .replace("${LAT}", "" + String.format(Locale.ROOT, "%.3f", cur.latitude)) .replace("${LONG}", "" + String.format(Locale.ROOT, "%.3f", cur.longitude)) .replace("${LANG}", "" + lang); @@ -124,6 +97,8 @@ public class NearbyPlaces { String point = fields[0]; String name = Utils.stripLocalizedString(fields[2]); String type = Utils.stripLocalizedString(fields[4]); + String sitelink = Utils.stripLocalizedString(fields[7]); + String wikiDataLink = Utils.stripLocalizedString(fields[3]); String icon = fields[5]; double latitude = 0; @@ -145,7 +120,9 @@ public class NearbyPlaces { type, // list type, // details Uri.parse(icon), - new LatLng(latitude, longitude) + new LatLng(latitude, longitude), + Uri.parse(sitelink), + Uri.parse(wikiDataLink) )); } in.close(); @@ -202,7 +179,9 @@ public class NearbyPlaces { type, // list type, // details null, - new LatLng(latitude, longitude) + new LatLng(latitude, longitude), + null, + null )); } in.close(); diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/Place.java b/app/src/main/java/fr/free/nrw/commons/nearby/Place.java index f3e8e9438..a975282e9 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/Place.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/Place.java @@ -16,15 +16,19 @@ public class Place { public Bitmap image; public Bitmap secondaryImage; public String distance; + public Uri siteLink; + public Uri wikiDataLink; public Place(String name, String description, String longDescription, - Uri secondaryImageUrl, LatLng location) { + Uri secondaryImageUrl, LatLng location, Uri siteLink, Uri wikiDataLink) { this.name = name; this.description = description; this.longDescription = longDescription; this.secondaryImageUrl = secondaryImageUrl; this.location = location; + this.siteLink = siteLink; + this.wikiDataLink = wikiDataLink; } public void setDistance(String distance) { diff --git a/app/src/main/java/fr/free/nrw/commons/ui/widget/OverlayDialog.java b/app/src/main/java/fr/free/nrw/commons/ui/widget/OverlayDialog.java new file mode 100644 index 000000000..69bc8549f --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/ui/widget/OverlayDialog.java @@ -0,0 +1,48 @@ +package fr.free.nrw.commons.ui.widget; + +import android.app.Dialog; +import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.v4.app.DialogFragment; +import android.view.Gravity; +import android.view.View; +import android.view.Window; +import android.view.WindowManager; + +import fr.free.nrw.commons.R; + +public abstract class OverlayDialog extends DialogFragment { + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setStyle(STYLE_NORMAL, R.style.borderless_dialog); + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + setDialogLayoutToFullScreen(); + super.onViewCreated(view, savedInstanceState); + } + + private void setDialogLayoutToFullScreen() { + Window window = getDialog().getWindow(); + WindowManager.LayoutParams wlp = window.getAttributes(); + window.requestFeature(Window.FEATURE_NO_TITLE); + wlp.gravity = Gravity.BOTTOM; + wlp.width = WindowManager.LayoutParams.MATCH_PARENT; + wlp.height = WindowManager.LayoutParams.MATCH_PARENT; + window.setAttributes(wlp); + } + + @NonNull + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + Dialog dialog = super.onCreateDialog(savedInstanceState); + Window window = dialog.getWindow(); + window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); + return dialog; + } +} \ No newline at end of file diff --git a/app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.java b/app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.java new file mode 100644 index 000000000..3992324b5 --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.java @@ -0,0 +1,80 @@ +package fr.free.nrw.commons.utils; + +import android.app.Activity; +import android.app.Dialog; +import android.os.Build; +import android.support.annotation.Nullable; +import android.support.v4.app.DialogFragment; +import android.support.v4.app.FragmentActivity; + +import timber.log.Timber; + +public class DialogUtil { + + public static void dismissSafely(@Nullable Activity activity, @Nullable DialogFragment dialog) { + boolean isActivityDestroyed = false; + + if (activity == null || dialog == null) { + Timber.d("dismiss called with null activity / dialog. Ignoring."); + return; + } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + isActivityDestroyed = activity.isDestroyed(); + } + if (activity.isFinishing() || isActivityDestroyed) { + return; + } + try { + dialog.dismiss(); + + } catch (IllegalStateException e) { + Timber.e(e, "Could not dismiss dialog."); + } + } + + public static void showSafely(Activity activity, Dialog dialog) { + if (activity == null || dialog == null) { + Timber.d("Show called with null activity / dialog. Ignoring."); + return; + } + + boolean isActivityDestroyed = false; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + isActivityDestroyed = activity.isDestroyed(); + } + if (activity.isFinishing() || isActivityDestroyed) { + Timber.e("Activity is not running. Could not show dialog. "); + return; + } + try { + dialog.show(); + } catch (IllegalStateException e) { + Timber.e(e, "Could not show dialog."); + } + } + + public static void showSafely(FragmentActivity activity, DialogFragment dialog) { + boolean isActivityDestroyed = false; + + if (activity == null || dialog == null) { + Timber.d("show called with null activity / dialog. Ignoring."); + return; + } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + isActivityDestroyed = activity.isDestroyed(); + } + if (activity.isFinishing() || isActivityDestroyed) { + return; + } + + try { + if (dialog.getDialog() == null || !dialog.getDialog().isShowing()) { + dialog.show(activity.getSupportFragmentManager(), dialog.getClass().getSimpleName()); + } + } catch (IllegalStateException e) { + Timber.e(e, "Could not show dialog."); + } + } +} diff --git a/app/src/main/java/fr/free/nrw/commons/utils/FileUtils.java b/app/src/main/java/fr/free/nrw/commons/utils/FileUtils.java new file mode 100644 index 000000000..4cb20ae0a --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/utils/FileUtils.java @@ -0,0 +1,33 @@ +package fr.free.nrw.commons.utils; + +import android.content.Context; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class FileUtils { + public static String readFromFile(Context context, String fileName) { + String stringBuilder = ""; + BufferedReader reader = null; + try { + reader = new BufferedReader( + new InputStreamReader(context.getAssets().open(fileName), "UTF-8")); + String mLine; + while ((mLine = reader.readLine()) != null) { + stringBuilder += mLine + "\n"; + } + } catch (IOException e) { + //log the exception + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e) { + //log the exception + } + } + } + return stringBuilder; + } +} diff --git a/app/src/main/res/layout/dialog_nearby_info.xml b/app/src/main/res/layout/dialog_nearby_info.xml new file mode 100644 index 000000000..be21af18e --- /dev/null +++ b/app/src/main/res/layout/dialog_nearby_info.xml @@ -0,0 +1,97 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index d9f2fb74d..d48714b16 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -2,4 +2,5 @@ 120dp 4dp 8dp + 240dp diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index a253fef6a..39fa1e4c4 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -45,4 +45,10 @@ + \ No newline at end of file