Merge remote-tracking branch 'upstream/master'

This commit is contained in:
Dmitry Brant 2017-05-19 16:27:08 +02:00
commit 7a4062a377
8 changed files with 67 additions and 43 deletions

View file

@ -25,6 +25,10 @@ dependencies {
testCompile 'junit:junit:4.12'
androidTestCompile "com.android.support:support-annotations:${project.supportLibVersion}"
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.1'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
}
android {

View file

@ -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.

View file

@ -23,6 +23,8 @@ import fr.free.nrw.commons.modifications.ModifierSequence;
import fr.free.nrw.commons.auth.AccountUtil;
import fr.free.nrw.commons.nearby.NearbyPlaces;
import com.squareup.leakcanary.LeakCanary;
import org.acra.ACRA;
import org.acra.ReportingInteractionMode;
import org.acra.annotation.ReportsCrashes;
@ -40,6 +42,13 @@ import org.apache.http.params.CoreProtocolPNames;
import java.io.File;
import java.io.IOException;
import fr.free.nrw.commons.auth.AccountUtil;
import fr.free.nrw.commons.caching.CacheController;
import fr.free.nrw.commons.category.Category;
import fr.free.nrw.commons.contributions.Contribution;
import fr.free.nrw.commons.data.DBOpenHelper;
import fr.free.nrw.commons.modifications.ModifierSequence;
import fr.free.nrw.commons.nearby.NearbyPlaces;
import fr.free.nrw.commons.utils.FileUtils;
import timber.log.Timber;
@ -147,6 +156,12 @@ public class CommonsApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
if (LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}
LeakCanary.install(this);
Timber.plant(new Timber.DebugTree());

View file

@ -71,7 +71,6 @@ public class ContributionsSyncAdapter extends AbstractThreadedSyncAdapter {
while(!done) {
try {
Timber.d("Example API query: https://commons.wikimedia.org/w/api.php?action=query&list=logevents&letype=upload&leuser=Sandaru&lelimit=5");
MWApi.RequestBuilder builder = api.action("query")
.param("list", "logevents")
.param("letype", "upload")

View file

@ -149,6 +149,12 @@ public class NearbyActivity extends NavigationBaseActivity {
checkGps();
}
@Override
protected void onPause() {
super.onPause();
nearbyAsyncTask.cancel(true);
}
protected void refreshView() {
nearbyAsyncTask = new NearbyAsyncTask(this);
nearbyAsyncTask.execute();

View file

@ -41,10 +41,7 @@ public class NearbyController {
NearbyPlaces nearbyPlaces = CommonsApplication.getInstance().getNearbyPlaces();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
List<Place> 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...");

View file

@ -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.IOException;
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 @@ import timber.log.Timber;
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<Place> places;
List<Place> 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<Place> getFromWikidataQuery(LatLng curLatLng, String lang) {
List<Place> 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 @@ public class NearbyPlaces {
return places;
}
private List<Place> getFromWikidataQuery(Context context,
LatLng cur,
private List<Place> getFromWikidataQuery(LatLng cur,
String lang,
double radius)
throws IOException {
List<Place> 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()));

View file

@ -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();
}
/**