Partially replaced old wiki android query invocations.

master
Tomasz Półgrabia 2016-11-01 22:37:41 +01:00
parent 0b4a9e3882
commit ed5be93a6d
16 changed files with 253 additions and 28 deletions

View File

@ -6,7 +6,7 @@ import java.util.Locale;
* Created by tpolgrabia on 27.08.16. * Created by tpolgrabia on 27.08.16.
*/ */
public class AppConstants { 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 float MIN_DISTANCE = 100;
public static final long GPS_LOCATION_UPDATE_FREQ = 15000; public static final long GPS_LOCATION_UPDATE_FREQ = 15000;
public static final float GPS_LOCATION_DISTANCE_FREQ = MIN_DISTANCE; public static final float GPS_LOCATION_DISTANCE_FREQ = MIN_DISTANCE;

View File

@ -3,6 +3,7 @@ package pl.tpolgrabia.urbanexplorer.fragments;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationManager; import android.location.LocationManager;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
@ -23,7 +24,11 @@ import pl.tpolgrabia.urbanexplorer.AppConstants;
import pl.tpolgrabia.urbanexplorer.R; import pl.tpolgrabia.urbanexplorer.R;
import pl.tpolgrabia.urbanexplorer.adapters.WikiLocationsAdapter; import pl.tpolgrabia.urbanexplorer.adapters.WikiLocationsAdapter;
import pl.tpolgrabia.urbanexplorer.callbacks.wiki.*; import pl.tpolgrabia.urbanexplorer.callbacks.wiki.*;
import pl.tpolgrabia.urbanexplorer.dto.wiki.WikiRequestDto;
import pl.tpolgrabia.urbanexplorer.events.RefreshSettingsEvent; 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.wikibinding.dto.app.WikiAppObject;
import pl.tpolgrabia.urbanexplorerutils.events.DataLoadingFinishEvent; import pl.tpolgrabia.urbanexplorerutils.events.DataLoadingFinishEvent;
import pl.tpolgrabia.urbanexplorerutils.events.RefreshEvent; import pl.tpolgrabia.urbanexplorerutils.events.RefreshEvent;
@ -105,8 +110,8 @@ public class WikiLocationsFragment extends Fragment {
public void fetchWikiLocations() { public void fetchWikiLocations() {
lg.trace("Fetch wiki locations"); lg.trace("Fetch wiki locations");
final FragmentActivity activity = getActivity(); final FragmentActivity ctx = getActivity();
if (activity == null) { if (ctx == null) {
lg.warn("Activity shouldn't be null. No headless fragment"); lg.warn("Activity shouldn't be null. No headless fragment");
EventBus.getDefault().post(new DataLoadingFinishEvent(this)); EventBus.getDefault().post(new DataLoadingFinishEvent(this));
return; return;
@ -124,7 +129,21 @@ public class WikiLocationsFragment extends Fragment {
return; 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 @Override

View File

@ -18,8 +18,9 @@ import pl.tpolgrabia.urbanexplorerutils.events.DataLoadingFinishEvent;
import pl.tpolgrabia.wikibinding.dto.app.WikiAppObject; import pl.tpolgrabia.wikibinding.dto.app.WikiAppObject;
import pl.tpolgrabia.wikibinding.dto.generator.WikiPage; import pl.tpolgrabia.wikibinding.dto.generator.WikiPage;
import pl.tpolgrabia.wikibinding.dto.generator.WikiResponse; 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.WikiGeoObject;
import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoResponse; import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoResponse2;
import pl.tpolgrabia.wikibinding.utils.WikiUtils; import pl.tpolgrabia.wikibinding.utils.WikiUtils;
import retrofit2.Response; import retrofit2.Response;
@ -50,10 +51,14 @@ public class WikiWorker extends AsyncTask<WikiRequestDto, Integer, List<WikiAppO
@Override @Override
protected List<WikiAppObject> doInBackground(WikiRequestDto... params) { protected List<WikiAppObject> doInBackground(WikiRequestDto... params) {
lg.info("Fetching {} wiki results", params.length);
List<WikiAppObject> results = new ArrayList<>(); List<WikiAppObject> results = new ArrayList<>();
for (WikiRequestDto param : params) { for (WikiRequestDto param : params) {
lg.debug("Fetching wiki results for {}", param);
try { try {
Response<WikiGeoResponse> apiResult = wikiUtils.fetchGeoSearchWikiMetadata2( Response<WikiGeoResponse2> apiResult = wikiUtils.fetchGeoSearchWikiMetadata2(
param.getLatitude(), param.getLatitude(),
param.getLongitude(), param.getLongitude(),
param.getRadius(), param.getRadius(),
@ -67,9 +72,16 @@ public class WikiWorker extends AsyncTask<WikiRequestDto, Integer, List<WikiAppO
return results; return results;
} }
WikiGeoResponse apiGeoResponse = apiResult.body(); lg.debug("Fetched wiki response {}", apiResult.body());
WikiGeoResponse2 apiGeoResponse = apiResult.body();
List<Long> pageIds = new ArrayList<>(); List<Long> pageIds = new ArrayList<>();
final List<WikiGeoObject> geoItems = apiGeoResponse.getQuery(); final List<WikiGeoObject> geoItems = apiGeoResponse.getQuery().getGeosearch();
if (geoItems == null) {
return results;
}
for (WikiGeoObject geoObject : geoItems) { for (WikiGeoObject geoObject : geoItems) {
pageIds.add(geoObject.getPageId()); pageIds.add(geoObject.getPageId());
} }
@ -79,7 +91,8 @@ public class WikiWorker extends AsyncTask<WikiRequestDto, Integer, List<WikiAppO
geoItemsMap.put(geoItem.getPageId(), geoItem); geoItemsMap.put(geoItem.getPageId(), geoItem);
} }
Response<WikiResponse> pageInfoResponse = wikiUtils.fetchPageInfos2(pageIds); lg.debug("Fetching wiki page infos for {}", pageIds);
Response<WikiResponse2> pageInfoResponse = wikiUtils.fetchPageInfos2(pageIds);
int pageInfoResponseCode = pageInfoResponse.code(); int pageInfoResponseCode = pageInfoResponse.code();
if (pageInfoResponseCode != 200) { if (pageInfoResponseCode != 200) {
lg.warn("Invalid http code {}. Message: {}. Try it later again", lg.warn("Invalid http code {}. Message: {}. Try it later again",
@ -87,18 +100,23 @@ public class WikiWorker extends AsyncTask<WikiRequestDto, Integer, List<WikiAppO
return results; return results;
} }
WikiResponse wikiResponse = pageInfoResponse.body(); WikiResponse2 wikiResponse = pageInfoResponse.body();
for (WikiPage page : wikiResponse.getPages()) { lg.debug("Fetched page infos response: {}", wikiResponse);
for (WikiPage page : wikiResponse.getQuery().getPages().values()) {
WikiAppObject appObject = WikiUtils.convertWikiAppObject(geoItemsMap, page); WikiAppObject appObject = WikiUtils.convertWikiAppObject(geoItemsMap, page);
results.add(appObject); results.add(appObject);
} }
} catch (IOException e) { } catch (IOException e) {
lg.error("I/O error", e); lg.error("I/O error", e);
} }
} }
success = true;
lg.info("Retrieved {} wiki results", results.size());
return results; return results;
} }
@ -108,7 +126,7 @@ public class WikiWorker extends AsyncTask<WikiRequestDto, Integer, List<WikiAppO
frag.setAppObjects(nobjects); frag.setAppObjects(nobjects);
// handling here wiki locations // handling here wiki locations
if (success) { if (!success) {
Toast.makeText(ctx, "Sorry, currently we have problem with interfacing wiki" + Toast.makeText(ctx, "Sorry, currently we have problem with interfacing wiki" +
": " + success + ". Try again later", Toast.LENGTH_SHORT).show(); ": " + success + ". Try again later", Toast.LENGTH_SHORT).show();
return; return;

View File

@ -18,7 +18,7 @@
<string name="panoramio_search_settings">Panoramio Sucheeinstellungen</string> <string name="panoramio_search_settings">Panoramio Sucheeinstellungen</string>
<string name="panoramio_radiusx_summary">X-Radius</string> <string name="panoramio_radiusx_summary">X-Radius</string>
<string name="panoramio_radiusx_title">X-Radius</string> <string name="panoramio_radiusx_title">X-Radius</string>
<string name="panoramio_bulk_size_summary">Panoramio Unterladung's größe</string> <string name="panoramio_bulk_size_summary">Panoramio Unterladung\'s größe</string>
<string name="login">Benutzer</string> <string name="login">Benutzer</string>
<string name="location_xrange">Lage X-Radius</string> <string name="location_xrange">Lage X-Radius</string>
<string name="location_yrange">Lage Y-Radius</string> <string name="location_yrange">Lage Y-Radius</string>

2
gradlew vendored
View File

@ -96,7 +96,7 @@ if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD" warn "Could not set maximum file descriptor limit: $MAX_FD"
fi fi
else 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
fi fi

View File

@ -1,7 +1,8 @@
package pl.tpolgrabia.wikibinding; package pl.tpolgrabia.wikibinding;
import pl.tpolgrabia.wikibinding.dto.generator.WikiResponse; 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.Call;
import retrofit2.http.GET; import retrofit2.http.GET;
import retrofit2.http.Query; import retrofit2.http.Query;
@ -11,7 +12,7 @@ import retrofit2.http.Query;
*/ */
public interface WikiService { public interface WikiService {
@GET("api.php?action=query&list=geosearch&format=json") @GET("api.php?action=query&list=geosearch&format=json")
Call<WikiGeoResponse> fetchGeoSearch( Call<WikiGeoResponse2> fetchGeoSearch(
@Query("gscoord") String gscoord, @Query("gscoord") String gscoord,
@Query("gsradius") Double radius, @Query("gsradius") Double radius,
@Query("gslimit") Long limit); @Query("gslimit") Long limit);
@ -25,6 +26,6 @@ public interface WikiService {
"&pilimit=50" + "&pilimit=50" +
"&wbptterms=description" + "&wbptterms=description" +
"&format=json") "&format=json")
Call<WikiResponse> fetchPageInfos( Call<WikiResponse2> fetchPageInfos(
@Query("pageids") String pageIds); @Query("pageids") String pageIds);
} }

View File

@ -1,5 +1,7 @@
package pl.tpolgrabia.wikibinding.dto.generator; package pl.tpolgrabia.wikibinding.dto.generator;
import com.google.gson.annotations.SerializedName;
import java.io.Serializable; import java.io.Serializable;
/** /**
@ -7,9 +9,13 @@ import java.io.Serializable;
*/ */
public class WikiLocation implements Serializable{ public class WikiLocation implements Serializable{
private static final long serialVersionUID = 2574692501816893919L; private static final long serialVersionUID = 2574692501816893919L;
@SerializedName("globe")
private String globe; private String globe;
@SerializedName("lat")
private Double latitude; private Double latitude;
@SerializedName("lon")
private Double longitude; private Double longitude;
@SerializedName("primary")
private String primary; private String primary;
public static long getSerialVersionUID() { public static long getSerialVersionUID() {

View File

@ -1,16 +1,23 @@
package pl.tpolgrabia.wikibinding.dto.generator; package pl.tpolgrabia.wikibinding.dto.generator;
import com.google.gson.annotations.SerializedName;
import java.util.List; import java.util.List;
/** /**
* Created by tpolgrabia on 28.08.16. * Created by tpolgrabia on 28.08.16.
*/ */
public class WikiPage { public class WikiPage {
@SerializedName("coordinates")
private List<WikiLocation> coordinates; private List<WikiLocation> coordinates;
private Long index; private Long index;
@SerializedName("ns")
private Long ns; private Long ns;
@SerializedName("pageid")
private Long pageId; private Long pageId;
@SerializedName("thumbnail")
private WikiThumbnail thumbnail; private WikiThumbnail thumbnail;
@SerializedName("title")
private String title; private String title;
public List<WikiLocation> getCoordinates() { public List<WikiLocation> getCoordinates() {

View File

@ -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<Long, WikiPage> pages;
public Map<Long, WikiPage> getPages() {
return pages;
}
public void setPages(Map<Long, WikiPage> pages) {
this.pages = pages;
}
@Override
public String toString() {
return "WikiQuery2{" +
"pages=" + pages +
'}';
}
}

View File

@ -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 +
'}';
}
}

View File

@ -1,11 +1,16 @@
package pl.tpolgrabia.wikibinding.dto.generator; package pl.tpolgrabia.wikibinding.dto.generator;
import com.google.gson.annotations.SerializedName;
/** /**
* Created by tpolgrabia on 28.08.16. * Created by tpolgrabia on 28.08.16.
*/ */
public class WikiThumbnail { public class WikiThumbnail {
@SerializedName("height")
private Long height; private Long height;
@SerializedName("source")
private String source; private String source;
@SerializedName("width")
private Long width; private Long width;
public Long getHeight() { public Long getHeight() {

View File

@ -1,5 +1,7 @@
package pl.tpolgrabia.wikibinding.dto.geosearch; package pl.tpolgrabia.wikibinding.dto.geosearch;
import com.google.gson.annotations.SerializedName;
import java.io.Serializable; import java.io.Serializable;
/** /**
@ -7,12 +9,19 @@ import java.io.Serializable;
*/ */
public class WikiGeoObject implements Serializable { public class WikiGeoObject implements Serializable {
private static final long serialVersionUID = 4527861009683008530L; private static final long serialVersionUID = 4527861009683008530L;
@SerializedName("pageid")
private Long pageId; private Long pageId;
@SerializedName("ns")
private Long ns; private Long ns;
@SerializedName("title")
private String title; private String title;
@SerializedName("lat")
private Double latitude; private Double latitude;
@SerializedName("lon")
private Double longitude; private Double longitude;
@SerializedName("dist")
private Double distance; private Double distance;
@SerializedName("primary")
private String primary; private String primary;
public Long getPageId() { public Long getPageId() {

View File

@ -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 +
'}';
}
}

View File

@ -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<WikiGeoObject> geosearch;
public List<WikiGeoObject> getGeosearch() {
return geosearch;
}
public void setGeosearch(List<WikiGeoObject> geosearch) {
this.geosearch = geosearch;
}
@Override
public String toString() {
return "WikiQuery{" +
"geosearch=" + geosearch +
'}';
}
}

View File

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

View File

@ -2,6 +2,7 @@ package pl.tpolgrabia.wikibinding.utils;
import android.content.Context; import android.content.Context;
import android.location.Location; import android.location.Location;
import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import com.androidquery.AQuery; import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback; 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.WikiResponseCallback;
import pl.tpolgrabia.wikibinding.callback.WikiStatus; import pl.tpolgrabia.wikibinding.callback.WikiStatus;
import pl.tpolgrabia.wikibinding.dto.app.WikiAppObject; import pl.tpolgrabia.wikibinding.dto.app.WikiAppObject;
import pl.tpolgrabia.wikibinding.dto.generator.WikiLocation; import pl.tpolgrabia.wikibinding.dto.generator.*;
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.geosearch.WikiGeoObject; import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoObject;
import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoResponse; import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoResponse;
import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoResponse2;
import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoResponseCallback; import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoResponseCallback;
import retrofit2.Response; import retrofit2.Response;
import retrofit2.Retrofit; import retrofit2.Retrofit;
@ -141,12 +140,13 @@ public class WikiUtils {
return appObject; return appObject;
} }
public Response<WikiGeoResponse> fetchGeoSearchWikiMetadata2(Double latitude, public Response<WikiGeoResponse2> fetchGeoSearchWikiMetadata2(Double latitude,
Double longitude, Double longitude,
Double radius, Double radius,
Long limit) throws IOException { Long limit) throws IOException {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
// TODO httpClient.addInterceptor(new RetrofitDebugInterceptor()); // TODO httpClient.addInterceptor(new RetrofitDebugInterceptor());
httpClient.addInterceptor(new WikiDebugInterceptor());
Retrofit retrofit = new Retrofit.Builder() Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://" + countryCode + ".wikipedia.org/w/") .baseUrl("https://" + countryCode + ".wikipedia.org/w/")
@ -154,8 +154,10 @@ public class WikiUtils {
.client(httpClient.build()) .client(httpClient.build())
.build(); .build();
final String gscoord = "" + latitude + "|" + longitude;
Log.d("XXX", "GSCoord" + gscoord);
return retrofit.create(WikiService.class).fetchGeoSearch( return retrofit.create(WikiService.class).fetchGeoSearch(
String.format("%s%7C%s", latitude, longitude), gscoord,
radius, radius,
limit limit
).execute(); ).execute();
@ -310,10 +312,11 @@ public class WikiUtils {
} }
public Response<WikiResponse> fetchPageInfos2(List<Long> pageIds) throws IOException { public Response<WikiResponse2> fetchPageInfos2(List<Long> pageIds) throws IOException {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
// TODO httpClient.addInterceptor(new RetrofitDebugInterceptor()); // TODO httpClient.addInterceptor(new RetrofitDebugInterceptor());
httpClient.addInterceptor(new WikiDebugInterceptor());
Retrofit retrofit = new Retrofit.Builder() Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://" + countryCode + ".wikipedia.org/w/") .baseUrl("https://" + countryCode + ".wikipedia.org/w/")