From 22f5f5108299c3d7fed20c193189bacb763c8057 Mon Sep 17 00:00:00 2001 From: Yusuke Matsubara Date: Tue, 16 May 2017 17:49:23 +0900 Subject: [PATCH] Read once and reuse the query file's content --- app/src/main/assets/queries/nearby_query.rq | 2 +- .../nrw/commons/nearby/NearbyController.java | 5 +-- .../free/nrw/commons/nearby/NearbyPlaces.java | 44 ++++++++++--------- .../fr/free/nrw/commons/utils/FileUtils.java | 33 +++++++------- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/app/src/main/assets/queries/nearby_query.rq b/app/src/main/assets/queries/nearby_query.rq index b6610c6c21..1542233c1d 100644 --- a/app/src/main/assets/queries/nearby_query.rq +++ b/app/src/main/assets/queries/nearby_query.rq @@ -13,7 +13,7 @@ SELECT SERVICE wikibase:around { ?item wdt:P625 ?location. bd:serviceParam wikibase:center "Point(${LONG} ${LAT})"^^geo:wktLiteral. - bd:serviceParam wikibase:radius "${RADIUS}" . # Radius in kilometers. + bd:serviceParam wikibase:radius "${RAD}" . # Radius in kilometers. } # ... and without an image. 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 0a290784ae..1c0699e176 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 @@ -41,10 +41,7 @@ public static List loadAttractionsFromLocation(LatLng curLatLng, Context NearbyPlaces nearbyPlaces = CommonsApplication.getInstance().getNearbyPlaces(); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); List places = prefs.getBoolean("useWikidata", true) - ? nearbyPlaces.getFromWikidataQuery( - context, - curLatLng, - Locale.getDefault().getLanguage()) + ? nearbyPlaces.getFromWikidataQuery(curLatLng, Locale.getDefault().getLanguage()) : nearbyPlaces.getFromWikiNeedsPictures(); if (curLatLng != null) { Timber.d("Sorting places by distance..."); 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 3478e74e9b..365d3e06fe 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,6 +1,5 @@ package fr.free.nrw.commons.nearby; -import android.content.Context; import android.net.Uri; import android.os.StrictMode; @@ -9,7 +8,6 @@ import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; -import java.net.URLEncoder; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -25,22 +23,32 @@ public class NearbyPlaces { private static final int MIN_RESULTS = 40; - private static final double INITIAL_RADIUS = 1.0; // in kilometer - private static final double MAX_RADIUS = 300.0; // in kilometer + private static final double INITIAL_RADIUS = 1.0; // in kilometers + private static final double MAX_RADIUS = 300.0; // in kilometers 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 Uri WIKIDATA_QUERY_URL = Uri.parse("https://query.wikidata.org/sparql"); + private static final Uri WIKIDATA_QUERY_UI_URL = Uri.parse("https://query.wikidata.org/"); + private final String wikidataQuery; private double radius = INITIAL_RADIUS; private List places; - List getFromWikidataQuery(Context context, - LatLng curLatLng, - String lang) { + public NearbyPlaces() { + try { + String query = FileUtils.readFromResource("/assets/queries/nearby_query.rq"); + wikidataQuery = query; + Timber.v(wikidataQuery); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + List getFromWikidataQuery(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(context, curLatLng, lang, radius); + places = getFromWikidataQuery(curLatLng, lang, radius); Timber.d("%d results at radius: %f", places.size(), radius); if (places.size() >= MIN_RESULTS) { break; @@ -58,28 +66,24 @@ List getFromWikidataQuery(Context context, return places; } - private List getFromWikidataQuery(Context context, - LatLng cur, + private List getFromWikidataQuery(LatLng cur, String lang, double radius) throws IOException { List places = new ArrayList<>(); - String query = FileUtils.readFromFile(context, "queries/nearby_query.rq") - .replace("${RADIUS}", String.format(Locale.ROOT, "%.2f", radius)) + String query = wikidataQuery + .replace("${RAD}", String.format(Locale.ROOT, "%.2f", radius)) .replace("${LAT}", String.format(Locale.ROOT, "%.4f", cur.latitude)) .replace("${LONG}", String.format(Locale.ROOT, "%.4f", cur.longitude)) .replace("${LANG}", lang); - Timber.d("Wikidata query "+ query); + Timber.v("# Wikidata query: \n" + query); // format as a URL - String url = WIKIDATA_QUERY_URL.replace( - "${QUERY}", - URLEncoder.encode(query, "utf-8").replace("+", "%20") - ); - - Timber.d(url); + Timber.d(WIKIDATA_QUERY_UI_URL.buildUpon().fragment(query).build().toString()); + String url = WIKIDATA_QUERY_URL.buildUpon() + .appendQueryParameter("query", query).build().toString(); URLConnection conn = new URL(url).openConnection(); conn.setRequestProperty("Accept", "text/tab-separated-values"); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 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 index 3df50b31fc..0596327e33 100644 --- a/app/src/main/java/fr/free/nrw/commons/utils/FileUtils.java +++ b/app/src/main/java/fr/free/nrw/commons/utils/FileUtils.java @@ -1,37 +1,36 @@ package fr.free.nrw.commons.utils; -import android.content.Context; - import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; -import timber.log.Timber; +import fr.free.nrw.commons.CommonsApplication; public class FileUtils { - public static String readFromFile(Context context, String fileName) { - String stringBuilder = ""; + /** + * Read and return the content of a resource file as string. + * + * @param fileName asset file's path (e.g. "/assets/queries/nearby_query.rq") + * @return the content of the file + */ + public static String readFromResource(String fileName) throws IOException { + StringBuffer buffer = new StringBuffer(); BufferedReader reader = null; try { reader = new BufferedReader( - new InputStreamReader(context.getAssets().open(fileName))); - String mLine; - while ((mLine = reader.readLine()) != null) { - stringBuilder += mLine + "\n"; + new InputStreamReader( + CommonsApplication.class.getResourceAsStream(fileName), "UTF-8")); + String line; + while ((line = reader.readLine()) != null) { + buffer.append(line + "\n"); } - } catch (IOException e) { - Timber.e("File not found exception", e); } finally { if (reader != null) { - try { - reader.close(); - } catch (IOException e) { - //log the exception - } + reader.close(); } } - return stringBuilder; + return buffer.toString(); } /**