From ed5be93a6dbfbfb054959bcf68b2cff4242bc5c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20P=C3=B3=C5=82grabia?= Date: Tue, 1 Nov 2016 22:37:41 +0100 Subject: [PATCH] Partially replaced old wiki android query invocations. --- .../urbanexplorer/AppConstants.java | 2 +- .../fragments/WikiLocationsFragment.java | 25 ++++++++++-- .../urbanexplorer/worker/WikiWorker.java | 36 ++++++++++++----- app/src/main/res/values-de/strings.xml | 2 +- gradlew | 2 +- .../tpolgrabia/wikibinding/WikiService.java | 7 ++-- .../dto/generator/WikiLocation.java | 6 +++ .../wikibinding/dto/generator/WikiPage.java | 7 ++++ .../wikibinding/dto/generator/WikiQuery2.java | 25 ++++++++++++ .../dto/generator/WikiResponse2.java | 40 +++++++++++++++++++ .../dto/generator/WikiThumbnail.java | 5 +++ .../dto/geosearch/WikiGeoObject.java | 9 +++++ .../dto/geosearch/WikiGeoResponse2.java | 33 +++++++++++++++ .../wikibinding/dto/geosearch/WikiQuery.java | 25 ++++++++++++ .../utils/WikiDebugInterceptor.java | 34 ++++++++++++++++ .../wikibinding/utils/WikiUtils.java | 23 ++++++----- 16 files changed, 253 insertions(+), 28 deletions(-) create mode 100644 wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/generator/WikiQuery2.java create mode 100644 wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/generator/WikiResponse2.java create mode 100644 wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/geosearch/WikiGeoResponse2.java create mode 100644 wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/geosearch/WikiQuery.java create mode 100644 wikibinding/src/main/java/pl/tpolgrabia/wikibinding/utils/WikiDebugInterceptor.java diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java index 89c10ee..001f12d 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java @@ -6,7 +6,7 @@ import java.util.Locale; * Created by tpolgrabia on 27.08.16. */ public class AppConstants { - public static final AppStage RELEASE = AppStage.FINAL; + public static final AppStage RELEASE = AppStage.DEVELOPMENT; public static final float MIN_DISTANCE = 100; public static final long GPS_LOCATION_UPDATE_FREQ = 15000; public static final float GPS_LOCATION_DISTANCE_FREQ = MIN_DISTANCE; diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java index fec45a5..25d3375 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java @@ -3,6 +3,7 @@ package pl.tpolgrabia.urbanexplorer.fragments; import android.content.Context; import android.content.SharedPreferences; +import android.location.Location; import android.location.LocationManager; import android.os.Bundle; import android.preference.PreferenceManager; @@ -23,7 +24,11 @@ import pl.tpolgrabia.urbanexplorer.AppConstants; import pl.tpolgrabia.urbanexplorer.R; import pl.tpolgrabia.urbanexplorer.adapters.WikiLocationsAdapter; import pl.tpolgrabia.urbanexplorer.callbacks.wiki.*; +import pl.tpolgrabia.urbanexplorer.dto.wiki.WikiRequestDto; import pl.tpolgrabia.urbanexplorer.events.RefreshSettingsEvent; +import pl.tpolgrabia.urbanexplorer.worker.WikiWorker; +import pl.tpolgrabia.urbanexplorerutils.utils.LocationUtils; +import pl.tpolgrabia.urbanexplorerutils.utils.SettingsUtils; import pl.tpolgrabia.wikibinding.dto.app.WikiAppObject; import pl.tpolgrabia.urbanexplorerutils.events.DataLoadingFinishEvent; import pl.tpolgrabia.urbanexplorerutils.events.RefreshEvent; @@ -105,8 +110,8 @@ public class WikiLocationsFragment extends Fragment { public void fetchWikiLocations() { lg.trace("Fetch wiki locations"); - final FragmentActivity activity = getActivity(); - if (activity == null) { + final FragmentActivity ctx = getActivity(); + if (ctx == null) { lg.warn("Activity shouldn't be null. No headless fragment"); EventBus.getDefault().post(new DataLoadingFinishEvent(this)); return; @@ -124,7 +129,21 @@ public class WikiLocationsFragment extends Fragment { return; } - wikiUtils.fetchAppData(new WikiFetchAppDataCallback(this, activity)); + // wikiUtils.fetchAppData(new WikiFetchAppDataCallback(this, activity)); + // FIXME hardcoded locale value + final Location location = LocationUtils.getLastKnownLocation(ctx); + if (location == null) { + lg.warn("Location not available"); + return; + } + + WikiRequestDto dto = new WikiRequestDto(); + dto.setLatitude(location.getLatitude()); + dto.setLongitude(location.getLongitude()); + dto.setLimit(SettingsUtils.fetchSearchLimit(ctx)); + dto.setRadius(SettingsUtils.fetchRadiusLimit(ctx)); + WikiWorker worker = new WikiWorker(ctx, this, "en"); + worker.execute(dto); } @Override diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/worker/WikiWorker.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/worker/WikiWorker.java index 68514e1..3b82006 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/worker/WikiWorker.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/worker/WikiWorker.java @@ -18,8 +18,9 @@ import pl.tpolgrabia.urbanexplorerutils.events.DataLoadingFinishEvent; import pl.tpolgrabia.wikibinding.dto.app.WikiAppObject; import pl.tpolgrabia.wikibinding.dto.generator.WikiPage; import pl.tpolgrabia.wikibinding.dto.generator.WikiResponse; +import pl.tpolgrabia.wikibinding.dto.generator.WikiResponse2; import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoObject; -import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoResponse; +import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoResponse2; import pl.tpolgrabia.wikibinding.utils.WikiUtils; import retrofit2.Response; @@ -50,10 +51,14 @@ public class WikiWorker extends AsyncTask doInBackground(WikiRequestDto... params) { + + lg.info("Fetching {} wiki results", params.length); + List results = new ArrayList<>(); for (WikiRequestDto param : params) { + lg.debug("Fetching wiki results for {}", param); try { - Response apiResult = wikiUtils.fetchGeoSearchWikiMetadata2( + Response apiResult = wikiUtils.fetchGeoSearchWikiMetadata2( param.getLatitude(), param.getLongitude(), param.getRadius(), @@ -67,9 +72,16 @@ public class WikiWorker extends AsyncTask pageIds = new ArrayList<>(); - final List geoItems = apiGeoResponse.getQuery(); + final List geoItems = apiGeoResponse.getQuery().getGeosearch(); + + if (geoItems == null) { + return results; + } + for (WikiGeoObject geoObject : geoItems) { pageIds.add(geoObject.getPageId()); } @@ -79,7 +91,8 @@ public class WikiWorker extends AsyncTask pageInfoResponse = wikiUtils.fetchPageInfos2(pageIds); + lg.debug("Fetching wiki page infos for {}", pageIds); + Response pageInfoResponse = wikiUtils.fetchPageInfos2(pageIds); int pageInfoResponseCode = pageInfoResponse.code(); if (pageInfoResponseCode != 200) { lg.warn("Invalid http code {}. Message: {}. Try it later again", @@ -87,18 +100,23 @@ public class WikiWorker extends AsyncTaskPanoramio Sucheeinstellungen X-Radius X-Radius - Panoramio Unterladung's größe + Panoramio Unterladung\'s größe Benutzer Lage X-Radius Lage Y-Radius diff --git a/gradlew b/gradlew index f5113a6..d4ddf86 100755 --- a/gradlew +++ b/gradlew @@ -96,7 +96,7 @@ if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then warn "Could not set maximum file descriptor limit: $MAX_FD" fi else - warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + warn "Could not geosearch maximum file descriptor limit: $MAX_FD_LIMIT" fi fi diff --git a/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/WikiService.java b/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/WikiService.java index 80b745f..613790a 100644 --- a/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/WikiService.java +++ b/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/WikiService.java @@ -1,7 +1,8 @@ package pl.tpolgrabia.wikibinding; import pl.tpolgrabia.wikibinding.dto.generator.WikiResponse; -import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoResponse; +import pl.tpolgrabia.wikibinding.dto.generator.WikiResponse2; +import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoResponse2; import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.Query; @@ -11,7 +12,7 @@ import retrofit2.http.Query; */ public interface WikiService { @GET("api.php?action=query&list=geosearch&format=json") - Call fetchGeoSearch( + Call fetchGeoSearch( @Query("gscoord") String gscoord, @Query("gsradius") Double radius, @Query("gslimit") Long limit); @@ -25,6 +26,6 @@ public interface WikiService { "&pilimit=50" + "&wbptterms=description" + "&format=json") - Call fetchPageInfos( + Call fetchPageInfos( @Query("pageids") String pageIds); } diff --git a/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/generator/WikiLocation.java b/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/generator/WikiLocation.java index d66f3db..ad01580 100644 --- a/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/generator/WikiLocation.java +++ b/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/generator/WikiLocation.java @@ -1,5 +1,7 @@ package pl.tpolgrabia.wikibinding.dto.generator; +import com.google.gson.annotations.SerializedName; + import java.io.Serializable; /** @@ -7,9 +9,13 @@ import java.io.Serializable; */ public class WikiLocation implements Serializable{ private static final long serialVersionUID = 2574692501816893919L; + @SerializedName("globe") private String globe; + @SerializedName("lat") private Double latitude; + @SerializedName("lon") private Double longitude; + @SerializedName("primary") private String primary; public static long getSerialVersionUID() { diff --git a/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/generator/WikiPage.java b/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/generator/WikiPage.java index cf73220..1f3aaa8 100644 --- a/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/generator/WikiPage.java +++ b/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/generator/WikiPage.java @@ -1,16 +1,23 @@ package pl.tpolgrabia.wikibinding.dto.generator; +import com.google.gson.annotations.SerializedName; + import java.util.List; /** * Created by tpolgrabia on 28.08.16. */ public class WikiPage { + @SerializedName("coordinates") private List coordinates; private Long index; + @SerializedName("ns") private Long ns; + @SerializedName("pageid") private Long pageId; + @SerializedName("thumbnail") private WikiThumbnail thumbnail; + @SerializedName("title") private String title; public List getCoordinates() { diff --git a/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/generator/WikiQuery2.java b/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/generator/WikiQuery2.java new file mode 100644 index 0000000..f17340b --- /dev/null +++ b/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/generator/WikiQuery2.java @@ -0,0 +1,25 @@ +package pl.tpolgrabia.wikibinding.dto.generator; + +import java.util.Map; + +/** + * Created by tpolgrabia on 01.11.16. + */ +public class WikiQuery2 { + private Map pages; + + public Map getPages() { + return pages; + } + + public void setPages(Map pages) { + this.pages = pages; + } + + @Override + public String toString() { + return "WikiQuery2{" + + "pages=" + pages + + '}'; + } +} diff --git a/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/generator/WikiResponse2.java b/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/generator/WikiResponse2.java new file mode 100644 index 0000000..76e5c2e --- /dev/null +++ b/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/generator/WikiResponse2.java @@ -0,0 +1,40 @@ +package pl.tpolgrabia.wikibinding.dto.generator; + +import pl.tpolgrabia.wikibinding.dto.geosearch.WikiQuery; + +import java.io.Serializable; +import java.util.List; +import java.util.Map; + +/** + * Created by tpolgrabia on 28.08.16. + */ +public class WikiResponse2 implements Serializable { + private static final long serialVersionUID = 2208673089408151268L; + private Boolean batchComplete; + private WikiQuery2 query; + + public Boolean getBatchComplete() { + return batchComplete; + } + + public void setBatchComplete(Boolean batchComplete) { + this.batchComplete = batchComplete; + } + + public WikiQuery2 getQuery() { + return query; + } + + public void setQuery(WikiQuery2 query) { + this.query = query; + } + + @Override + public String toString() { + return "WikiResponse2{" + + "batchComplete=" + batchComplete + + ", query=" + query + + '}'; + } +} diff --git a/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/generator/WikiThumbnail.java b/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/generator/WikiThumbnail.java index 58b137b..59d4a3f 100644 --- a/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/generator/WikiThumbnail.java +++ b/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/generator/WikiThumbnail.java @@ -1,11 +1,16 @@ package pl.tpolgrabia.wikibinding.dto.generator; +import com.google.gson.annotations.SerializedName; + /** * Created by tpolgrabia on 28.08.16. */ public class WikiThumbnail { + @SerializedName("height") private Long height; + @SerializedName("source") private String source; + @SerializedName("width") private Long width; public Long getHeight() { diff --git a/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/geosearch/WikiGeoObject.java b/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/geosearch/WikiGeoObject.java index 295cb67..f01915e 100644 --- a/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/geosearch/WikiGeoObject.java +++ b/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/geosearch/WikiGeoObject.java @@ -1,5 +1,7 @@ package pl.tpolgrabia.wikibinding.dto.geosearch; +import com.google.gson.annotations.SerializedName; + import java.io.Serializable; /** @@ -7,12 +9,19 @@ import java.io.Serializable; */ public class WikiGeoObject implements Serializable { private static final long serialVersionUID = 4527861009683008530L; + @SerializedName("pageid") private Long pageId; + @SerializedName("ns") private Long ns; + @SerializedName("title") private String title; + @SerializedName("lat") private Double latitude; + @SerializedName("lon") private Double longitude; + @SerializedName("dist") private Double distance; + @SerializedName("primary") private String primary; public Long getPageId() { diff --git a/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/geosearch/WikiGeoResponse2.java b/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/geosearch/WikiGeoResponse2.java new file mode 100644 index 0000000..62b6bde --- /dev/null +++ b/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/geosearch/WikiGeoResponse2.java @@ -0,0 +1,33 @@ +package pl.tpolgrabia.wikibinding.dto.geosearch; + +/** + * Created by tpolgrabia on 05.09.16. + */ +public class WikiGeoResponse2 { + private Boolean batchComplete; + private WikiQuery query; + + public Boolean getBatchComplete() { + return batchComplete; + } + + public void setBatchComplete(Boolean batchComplete) { + this.batchComplete = batchComplete; + } + + public WikiQuery getQuery() { + return query; + } + + public void setQuery(WikiQuery query) { + this.query = query; + } + + @Override + public String toString() { + return "WikiGeoResponse2{" + + "batchComplete=" + batchComplete + + ", query=" + query + + '}'; + } +} diff --git a/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/geosearch/WikiQuery.java b/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/geosearch/WikiQuery.java new file mode 100644 index 0000000..cd8f308 --- /dev/null +++ b/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/dto/geosearch/WikiQuery.java @@ -0,0 +1,25 @@ +package pl.tpolgrabia.wikibinding.dto.geosearch; + +import java.util.List; + +/** + * Created by tpolgrabia on 01.11.16. + */ +public class WikiQuery { + private List geosearch; + + public List getGeosearch() { + return geosearch; + } + + public void setGeosearch(List geosearch) { + this.geosearch = geosearch; + } + + @Override + public String toString() { + return "WikiQuery{" + + "geosearch=" + geosearch + + '}'; + } +} diff --git a/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/utils/WikiDebugInterceptor.java b/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/utils/WikiDebugInterceptor.java new file mode 100644 index 0000000..e6b24eb --- /dev/null +++ b/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/utils/WikiDebugInterceptor.java @@ -0,0 +1,34 @@ +package pl.tpolgrabia.wikibinding.utils; + +import android.util.Log; +import okhttp3.Interceptor; +import okhttp3.Request; +import okhttp3.Response; +import okhttp3.ResponseBody; + +import java.io.IOException; + +/** + * Created by tpolgrabia on 01.11.16. + */ +public class WikiDebugInterceptor implements Interceptor { + @Override + public Response intercept(Chain chain) throws IOException { + final Request req = chain.request(); + Log.d("XXX", "Url: " + req.url()); + okhttp3.Response response = chain.proceed(req); + boolean successFull = response.isSuccessful(); + int code = response.code(); + String message = response.message(); + String msg = response.body().string(); + Log.d("XXX", String.format("Got wiki response. Is successfull: %d, code: %d, message: %s, msg: %s", + successFull ? 1 : 0, + code, + message, + msg)); + // now we repeat once again (because we have used the stream) + return response.newBuilder() + .body(ResponseBody.create(response.body().contentType(), msg)) + .build(); + } +} diff --git a/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/utils/WikiUtils.java b/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/utils/WikiUtils.java index 8ae9f69..12bd8d6 100644 --- a/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/utils/WikiUtils.java +++ b/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/utils/WikiUtils.java @@ -2,6 +2,7 @@ package pl.tpolgrabia.wikibinding.utils; import android.content.Context; import android.location.Location; +import android.util.Log; import android.widget.Toast; import com.androidquery.AQuery; import com.androidquery.callback.AjaxCallback; @@ -23,12 +24,10 @@ import pl.tpolgrabia.wikibinding.callback.WikiAppResponseCallback; import pl.tpolgrabia.wikibinding.callback.WikiResponseCallback; import pl.tpolgrabia.wikibinding.callback.WikiStatus; import pl.tpolgrabia.wikibinding.dto.app.WikiAppObject; -import pl.tpolgrabia.wikibinding.dto.generator.WikiLocation; -import pl.tpolgrabia.wikibinding.dto.generator.WikiPage; -import pl.tpolgrabia.wikibinding.dto.generator.WikiResponse; -import pl.tpolgrabia.wikibinding.dto.generator.WikiThumbnail; +import pl.tpolgrabia.wikibinding.dto.generator.*; import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoObject; import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoResponse; +import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoResponse2; import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoResponseCallback; import retrofit2.Response; import retrofit2.Retrofit; @@ -141,12 +140,13 @@ public class WikiUtils { return appObject; } - public Response fetchGeoSearchWikiMetadata2(Double latitude, - Double longitude, - Double radius, - Long limit) throws IOException { + public Response fetchGeoSearchWikiMetadata2(Double latitude, + Double longitude, + Double radius, + Long limit) throws IOException { OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); // TODO httpClient.addInterceptor(new RetrofitDebugInterceptor()); + httpClient.addInterceptor(new WikiDebugInterceptor()); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://" + countryCode + ".wikipedia.org/w/") @@ -154,8 +154,10 @@ public class WikiUtils { .client(httpClient.build()) .build(); + final String gscoord = "" + latitude + "|" + longitude; + Log.d("XXX", "GSCoord" + gscoord); return retrofit.create(WikiService.class).fetchGeoSearch( - String.format("%s%7C%s", latitude, longitude), + gscoord, radius, limit ).execute(); @@ -310,10 +312,11 @@ public class WikiUtils { } - public Response fetchPageInfos2(List pageIds) throws IOException { + public Response fetchPageInfos2(List pageIds) throws IOException { OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); // TODO httpClient.addInterceptor(new RetrofitDebugInterceptor()); + httpClient.addInterceptor(new WikiDebugInterceptor()); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://" + countryCode + ".wikipedia.org/w/")