From a5a5159e47a7ff2ca3ab739b5901b9ac352ea25c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20P=C3=B3=C5=82grabia?= Date: Sun, 11 Sep 2016 13:54:14 +0200 Subject: [PATCH] Works fetching distance to wiki pages. --- .../adapters/WikiLocationsAdapter.java | 17 ++-- .../dto/wiki/app/WikiAppObject.java | 9 +++ .../urbanexplorer/fragments/HomeFragment.java | 7 +- .../fragments/WikiLocationsFragment.java | 77 ++++++++++--------- .../urbanexplorer/utils/NumberUtils.java | 20 ++++- .../utils/WikiAppResponseCallback.java | 13 ++++ .../urbanexplorer/utils/WikiUtils.java | 64 ++++++++++----- .../main/res/layout/wiki_locations_item.xml | 10 --- 8 files changed, 142 insertions(+), 75 deletions(-) create mode 100644 app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiAppResponseCallback.java diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/adapters/WikiLocationsAdapter.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/adapters/WikiLocationsAdapter.java index 3b66b0a..7b723cf 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/adapters/WikiLocationsAdapter.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/adapters/WikiLocationsAdapter.java @@ -4,19 +4,21 @@ import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.*; +import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.TextView; import com.nostra13.universalimageloader.core.ImageLoader; import pl.tpolgrabia.urbanexplorer.MainActivity; import pl.tpolgrabia.urbanexplorer.R; -import pl.tpolgrabia.urbanexplorer.dto.wiki.generator.WikiPage; +import pl.tpolgrabia.urbanexplorer.dto.wiki.app.WikiAppObject; import java.util.List; /** * Created by tpolgrabia on 01.09.16. */ -public class WikiLocationsAdapter extends ArrayAdapter { - public WikiLocationsAdapter(Context ctx, List locations) { +public class WikiLocationsAdapter extends ArrayAdapter { + public WikiLocationsAdapter(Context ctx, List locations) { super(ctx, R.layout.wiki_locations_item, locations); } @@ -31,11 +33,14 @@ public class WikiLocationsAdapter extends ArrayAdapter { inflatedView = inflater.inflate(R.layout.wiki_locations_item,parent,false); } - WikiPage wikiPage = getItem(position); + WikiAppObject wikiPage = getItem(position); // wiki page image preview ImageView imgPreview = (ImageView) inflatedView.findViewById(R.id.wiki_locs_item_img_preview); - String url = wikiPage.getThumbnail() != null ? wikiPage.getThumbnail().getSource() : null; + String url = wikiPage.getThumbnail() != null ? wikiPage.getThumbnail() : null; + + TextView locDistanceInfo = (TextView) inflatedView.findViewById(R.id.wiki_locs_item_distance); + locDistanceInfo.setText("" + wikiPage.getDistance() / 1000.0 + " km"); if (url != null) { ImageLoader.getInstance().displayImage( diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/wiki/app/WikiAppObject.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/wiki/app/WikiAppObject.java index 0241b55..6dbb1b8 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/wiki/app/WikiAppObject.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/wiki/app/WikiAppObject.java @@ -12,6 +12,7 @@ public class WikiAppObject implements Serializable { private Double latitude; private Double longitude; private Double distance; + private Long pageId; public String getUrl() { return url; @@ -72,4 +73,12 @@ public class WikiAppObject implements Serializable { ", distance='" + distance + '\'' + '}'; } + + public Long getPageId() { + return pageId; + } + + public void setPageId(Long pageId) { + this.pageId = pageId; + } } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java index 803b803..63f3819 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java @@ -290,7 +290,12 @@ public class HomeFragment extends Fragment { } public void updateLocationInfo() { - TextView locationInfo = (TextView) getView().findViewById(R.id.locationInfo); + final View view = getView(); + if (view == null) { + Log.wtf(CLASS_TAG, "Fragment has no view"); + return; + } + TextView locationInfo = (TextView) view.findViewById(R.id.locationInfo); locationService = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE); Location currLocation = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(getActivity())); Log.v(CLASS_TAG, "Current location: " + currLocation + ", locationInfo: " + locationInfo); 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 ccf3ed2..4ef69f1 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java @@ -22,14 +22,15 @@ import pl.tpolgrabia.urbanexplorer.MainActivity; import pl.tpolgrabia.urbanexplorer.R; import pl.tpolgrabia.urbanexplorer.adapters.WikiLocationsAdapter; import pl.tpolgrabia.urbanexplorer.callbacks.StandardLocationListenerCallback; -import pl.tpolgrabia.urbanexplorer.callbacks.WikiResponseCallback; import pl.tpolgrabia.urbanexplorer.callbacks.WikiStatus; -import pl.tpolgrabia.urbanexplorer.dto.wiki.generator.WikiPage; -import pl.tpolgrabia.urbanexplorer.dto.wiki.generator.WikiResponse; +import pl.tpolgrabia.urbanexplorer.dto.wiki.app.WikiAppObject; import pl.tpolgrabia.urbanexplorer.utils.LocationUtils; import pl.tpolgrabia.urbanexplorer.utils.NumberUtils; +import pl.tpolgrabia.urbanexplorer.utils.WikiAppResponseCallback; import pl.tpolgrabia.urbanexplorer.utils.WikiUtils; +import java.util.List; + import static android.content.Context.LOCATION_SERVICE; /** @@ -73,17 +74,17 @@ public class WikiLocationsFragment extends Fragment { final Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(getActivity())); Editable search_limit = ((EditText) inflatedView.findViewById(R.id.wiki_search_limit)).getText(); Editable radius_limit = ((EditText) inflatedView.findViewById(R.id.wiki_search_radius)).getText(); - WikiUtils.fetchNearPlaces( - getActivity(), + + + WikiUtils.fetchAppData(getActivity(), location.getLatitude(), location.getLongitude(), + NumberUtils.safeParseDouble(search_limit != null ? search_limit.toString() : null), NumberUtils.safeParseLong( - search_limit != null ? search_limit.toString(): null), - NumberUtils.safeParseLong( - radius_limit != null ? radius_limit.toString() : null), - new WikiResponseCallback() { + radius_limit != null ? radius_limit.toString() : null), + new WikiAppResponseCallback() { @Override - public void callback(WikiStatus status, final WikiResponse response) { + public void callback(WikiStatus status, final List appObjects) { // handling here wiki locations if (status != WikiStatus.SUCCESS) { Toast.makeText(getActivity(), "Sorry, currently we have problem with interfacing wiki" + @@ -101,43 +102,45 @@ public class WikiLocationsFragment extends Fragment { // Intent intent = new Intent(android.content.Intent.ACTION_VIEW, // Uri.parse(item.get); // startActivity(intent); - final WikiPage item = response.getPages().get(position); + final WikiAppObject item = appObjects.get(position); new AQuery(getActivity()).ajax( - "https://en.wikipedia.org/w/api.php?action=query&prop=info&pageids=" - + item.getPageId() + "&inprop=url&format=json", - JSONObject.class, - new AjaxCallback() { - @Override - public void callback(String url, JSONObject object, AjaxStatus status) { - if (status.getCode() != 200) { - Toast.makeText(getActivity(), - "Sorry, network error code: " + status.getCode(), - Toast.LENGTH_LONG) - .show(); - return; - } + "https://en.wikipedia.org/w/api.php?action=query&prop=info&pageids=" + + item.getPageId() + "&inprop=url&format=json", + JSONObject.class, + new AjaxCallback() { + @Override + public void callback(String url, JSONObject object, AjaxStatus status) { + if (status.getCode() != 200) { + Toast.makeText(getActivity(), + "Sorry, network error code: " + status.getCode(), + Toast.LENGTH_LONG) + .show(); + return; + } - try { - String wikiUrl = object.getJSONObject("query") - .getJSONObject("pages") - .getJSONObject(item.getPageId().toString()) - .getString("fullurl"); - Intent intent = new Intent(android.content.Intent.ACTION_VIEW, - Uri.parse(wikiUrl)); - startActivity(intent); - } catch (JSONException e) { - Log.e(CLASS_TAG, "Error", e); - } + try { + String wikiUrl = object.getJSONObject("query") + .getJSONObject("pages") + .getJSONObject(item.getPageId().toString()) + .getString("fullurl"); + Intent intent = new Intent(android.content.Intent.ACTION_VIEW, + Uri.parse(wikiUrl)); + startActivity(intent); + } catch (JSONException e) { + Log.e(CLASS_TAG, "Error", e); } } + } ); return false; } }); - locations.setAdapter(new WikiLocationsAdapter(getActivity(), response.getPages())); + locations.setAdapter(new WikiLocationsAdapter(getActivity(), appObjects)); } - }); + } + ); + } }); diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/NumberUtils.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/NumberUtils.java index 5240307..2e4b74b 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/NumberUtils.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/NumberUtils.java @@ -5,15 +5,31 @@ package pl.tpolgrabia.urbanexplorer.utils; */ public class NumberUtils { public static Long safeParseLong(String s) { - if (s == null || "".equals(s)) { + if (s == null || "".equals(s.trim())) { return null; } + String trimmed = s.trim(); + try { - return Long.parseLong(s); + return Long.parseLong(trimmed); } catch (NumberFormatException e) { return null; } } + + public static Double safeParseDouble(String s) { + if (s == null || "".equals(s.trim())) { + return null; + } + + String trimmed = s.trim(); + + try { + return Double.parseDouble(trimmed); + } catch (NumberFormatException e) { + return null; + } + } } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiAppResponseCallback.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiAppResponseCallback.java new file mode 100644 index 0000000..a254aac --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiAppResponseCallback.java @@ -0,0 +1,13 @@ +package pl.tpolgrabia.urbanexplorer.utils; + +import pl.tpolgrabia.urbanexplorer.callbacks.WikiStatus; +import pl.tpolgrabia.urbanexplorer.dto.wiki.app.WikiAppObject; + +import java.util.List; + +/** + * Created by tpolgrabia on 11.09.16. + */ +public interface WikiAppResponseCallback { + void callback(WikiStatus status, List appObjects); +} diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiUtils.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiUtils.java index efb48b7..2e46640 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiUtils.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiUtils.java @@ -2,6 +2,7 @@ package pl.tpolgrabia.urbanexplorer.utils; import android.content.Context; import android.util.Log; +import android.widget.Toast; import com.androidquery.AQuery; import com.androidquery.callback.AjaxCallback; import com.androidquery.callback.AjaxStatus; @@ -31,6 +32,8 @@ public class WikiUtils { private static final long WIKI_MIN_RADIUS = 10L; private static final Long WIKI_MAX_RESULTS_LIMIT = 500L; private static final Long WIKI_MIN_RESULTS = 10L; + private static final Double WIKI_STD_RADIUS = 10000.0; + private static final Long WIKI_STD_LIMIT = 10L; public static void fetchNearPlaces(Context ctx, final double latitude, @@ -157,22 +160,32 @@ public class WikiUtils { Double radius, Long limit, final WikiGeoResponseCallback callback) { + + if (radius == null) { + radius = WIKI_STD_RADIUS; + } + + if (limit == null) { + limit = WIKI_STD_LIMIT; + } + AQuery aq = new AQuery(ctx); - aq.ajax("https://en.wikipedia.org/w/api.php" + - "?action=query" + - "&list=geosearch" + - "&gscoord=" + latitude + "%7C" + longitude + - "gsradius=" + radius + - "&gslimit=" + limit, JSONObject.class, new AjaxCallback() { + aq.ajax("https://en.wikipedia.org/w/api.php?action=query&list=geosearch&gscoord=52.2181737%7C21.1530673&gsradius=10000&gslimit=10&format=json", JSONObject.class, new AjaxCallback() { @Override public void callback(String url, JSONObject object, AjaxStatus status) { + Log.v(CLASS_TAG, "Finished waiting for " + url + + " with status " + status.getCode() + ":" + status.getMessage() + + " and response: " + object); if (status.getCode() == 200) { try { callback.callback(WikiStatus.SUCCESS, fetchWikiGeoResponse(object)); } catch (Throwable t) { + Log.e(CLASS_TAG, "General error during fetching", t); callback.callback(WikiStatus.GENERAL_ERROR, null); } } else { + Log.e(CLASS_TAG, "Couldn't fetch wiki metadata " + object + + ", status: " + status.getCode() + ": " + status.getMessage() + " from url: " + url); callback.callback(WikiStatus.NETWORK_ERROR, null); } super.callback(url, object, status); @@ -184,17 +197,16 @@ public class WikiUtils { public static WikiGeoResponse fetchWikiGeoResponse(JSONObject object) { WikiGeoResponse response = new WikiGeoResponse(); response.setBatchComplete(object.optBoolean("batch_complete")); - response.setQuery(fetchQueriesData(object.optJSONObject("query"))); + response.setQuery(fetchQueriesData(object.optJSONObject("query").optJSONArray("geosearch"))); return response; } - public static List fetchQueriesData(JSONObject object) { - String val; - Iterator it = object.keys(); + public static List fetchQueriesData(JSONArray object) { List geoObjects = new ArrayList<>(); - while (it.hasNext()) { - val = it.next(); - JSONObject geoPage = object.optJSONObject(val); + int n = object.length(); + int idx; + for (idx = 0; idx < n; idx++) { + JSONObject geoPage = object.optJSONObject(idx); geoObjects.add(fetchWikiGeoObject(geoPage)); } return geoObjects; @@ -217,11 +229,19 @@ public class WikiUtils { final Double longitude, final Double radius, final Long limit, - final WikiResponseCallback callback) { + final WikiAppResponseCallback callback) { fetchGeoSearchWikiMetadata(ctx, latitude, longitude, radius, limit, new WikiGeoResponseCallback() { @Override public void callback(WikiStatus status, WikiGeoResponse response) { + + Log.v(CLASS_TAG, "Fetching finished with status: " + status + " and values: " + response); + + if (status != WikiStatus.SUCCESS) { + Toast.makeText(ctx, "Sorry, couldn't fetch wiki metadata", Toast.LENGTH_SHORT).show(); + return; + } + final List geoItems = response.getQuery(); if (geoItems == null) { return; @@ -237,7 +257,6 @@ public class WikiUtils { geoItemsMap.put(geoItem.getPageId(), geoItem); } - WikiResponseCallback tcallback = callback; fetchPageInfos(ctx, pageIds, @@ -245,6 +264,7 @@ public class WikiUtils { @Override public void callback(WikiStatus status, WikiResponse response) { if (status != WikiStatus.SUCCESS) { + callback.callback(WikiStatus.NETWORK_ERROR, null); return; } @@ -256,13 +276,18 @@ public class WikiUtils { appObject.setDistance(geoItemsMap.get(page.getPageId()).getDistance()); appObject.setLatitude(page.getCoordinates().get(0).getLatitude()); appObject.setLongitude(page.getCoordinates().get(0).getLongitude()); - appObject.setThumbnail(page.getThumbnail().getSource()); - appObject.setUrl(page.getThumbnail().getSource()); + final WikiThumbnail thumbonail = page.getThumbnail(); + final String thumSource = thumbonail != null ? thumbonail.getSource() : null; + appObject.setThumbnail(thumSource); + appObject.setUrl(thumSource); + appObject.setPageId(page.getPageId()); results.add(appObject); } // TODO here add callback invocation with result + callback.callback(WikiStatus.SUCCESS, results); + } }); } @@ -280,10 +305,11 @@ public class WikiUtils { "&pithumbsize=144" + "&pilimit=50" + "&wbptterms=description" + - "&pageids=" + StringUtils.join(pageIds, "|"), JSONObject.class, new AjaxCallback() { + "&pageids=" + StringUtils.join(pageIds, "|") + + "&format=json", JSONObject.class, new AjaxCallback() { @Override public void callback(String url, JSONObject object, AjaxStatus status) { - if (status.getCode() != 200) { + if (status.getCode() == 200) { try { callback.callback(WikiStatus.SUCCESS, fetchWikiResponse(object)); } catch (Throwable t) { diff --git a/app/src/main/res/layout/wiki_locations_item.xml b/app/src/main/res/layout/wiki_locations_item.xml index f90925e..1aa3239 100644 --- a/app/src/main/res/layout/wiki_locations_item.xml +++ b/app/src/main/res/layout/wiki_locations_item.xml @@ -19,16 +19,6 @@ android:layout_height="wrap_content" android:text="Fill Title" /> - - - -