Works fetching distance to wiki pages.

master
Tomasz Półgrabia 2016-09-11 13:54:14 +02:00
parent ec11154ae8
commit a5a5159e47
8 changed files with 142 additions and 75 deletions

View File

@ -4,19 +4,21 @@ import android.content.Context;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; 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 com.nostra13.universalimageloader.core.ImageLoader;
import pl.tpolgrabia.urbanexplorer.MainActivity; import pl.tpolgrabia.urbanexplorer.MainActivity;
import pl.tpolgrabia.urbanexplorer.R; 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; import java.util.List;
/** /**
* Created by tpolgrabia on 01.09.16. * Created by tpolgrabia on 01.09.16.
*/ */
public class WikiLocationsAdapter extends ArrayAdapter<WikiPage> { public class WikiLocationsAdapter extends ArrayAdapter<WikiAppObject> {
public WikiLocationsAdapter(Context ctx, List<WikiPage> locations) { public WikiLocationsAdapter(Context ctx, List<WikiAppObject> locations) {
super(ctx, R.layout.wiki_locations_item, locations); super(ctx, R.layout.wiki_locations_item, locations);
} }
@ -31,11 +33,14 @@ public class WikiLocationsAdapter extends ArrayAdapter<WikiPage> {
inflatedView = inflater.inflate(R.layout.wiki_locations_item,parent,false); inflatedView = inflater.inflate(R.layout.wiki_locations_item,parent,false);
} }
WikiPage wikiPage = getItem(position); WikiAppObject wikiPage = getItem(position);
// wiki page image preview // wiki page image preview
ImageView imgPreview = (ImageView) inflatedView.findViewById(R.id.wiki_locs_item_img_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) { if (url != null) {
ImageLoader.getInstance().displayImage( ImageLoader.getInstance().displayImage(

View File

@ -12,6 +12,7 @@ public class WikiAppObject implements Serializable {
private Double latitude; private Double latitude;
private Double longitude; private Double longitude;
private Double distance; private Double distance;
private Long pageId;
public String getUrl() { public String getUrl() {
return url; return url;
@ -72,4 +73,12 @@ public class WikiAppObject implements Serializable {
", distance='" + distance + '\'' + ", distance='" + distance + '\'' +
'}'; '}';
} }
public Long getPageId() {
return pageId;
}
public void setPageId(Long pageId) {
this.pageId = pageId;
}
} }

View File

@ -290,7 +290,12 @@ public class HomeFragment extends Fragment {
} }
public void updateLocationInfo() { 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); locationService = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
Location currLocation = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(getActivity())); Location currLocation = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(getActivity()));
Log.v(CLASS_TAG, "Current location: " + currLocation + ", locationInfo: " + locationInfo); Log.v(CLASS_TAG, "Current location: " + currLocation + ", locationInfo: " + locationInfo);

View File

@ -22,14 +22,15 @@ import pl.tpolgrabia.urbanexplorer.MainActivity;
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.StandardLocationListenerCallback; import pl.tpolgrabia.urbanexplorer.callbacks.StandardLocationListenerCallback;
import pl.tpolgrabia.urbanexplorer.callbacks.WikiResponseCallback;
import pl.tpolgrabia.urbanexplorer.callbacks.WikiStatus; import pl.tpolgrabia.urbanexplorer.callbacks.WikiStatus;
import pl.tpolgrabia.urbanexplorer.dto.wiki.generator.WikiPage; import pl.tpolgrabia.urbanexplorer.dto.wiki.app.WikiAppObject;
import pl.tpolgrabia.urbanexplorer.dto.wiki.generator.WikiResponse;
import pl.tpolgrabia.urbanexplorer.utils.LocationUtils; import pl.tpolgrabia.urbanexplorer.utils.LocationUtils;
import pl.tpolgrabia.urbanexplorer.utils.NumberUtils; import pl.tpolgrabia.urbanexplorer.utils.NumberUtils;
import pl.tpolgrabia.urbanexplorer.utils.WikiAppResponseCallback;
import pl.tpolgrabia.urbanexplorer.utils.WikiUtils; import pl.tpolgrabia.urbanexplorer.utils.WikiUtils;
import java.util.List;
import static android.content.Context.LOCATION_SERVICE; import static android.content.Context.LOCATION_SERVICE;
/** /**
@ -73,17 +74,17 @@ public class WikiLocationsFragment extends Fragment {
final Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(getActivity())); final Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(getActivity()));
Editable search_limit = ((EditText) inflatedView.findViewById(R.id.wiki_search_limit)).getText(); Editable search_limit = ((EditText) inflatedView.findViewById(R.id.wiki_search_limit)).getText();
Editable radius_limit = ((EditText) inflatedView.findViewById(R.id.wiki_search_radius)).getText(); Editable radius_limit = ((EditText) inflatedView.findViewById(R.id.wiki_search_radius)).getText();
WikiUtils.fetchNearPlaces(
getActivity(),
WikiUtils.fetchAppData(getActivity(),
location.getLatitude(), location.getLatitude(),
location.getLongitude(), location.getLongitude(),
NumberUtils.safeParseDouble(search_limit != null ? search_limit.toString() : null),
NumberUtils.safeParseLong( NumberUtils.safeParseLong(
search_limit != null ? search_limit.toString(): null), radius_limit != null ? radius_limit.toString() : null),
NumberUtils.safeParseLong( new WikiAppResponseCallback() {
radius_limit != null ? radius_limit.toString() : null),
new WikiResponseCallback() {
@Override @Override
public void callback(WikiStatus status, final WikiResponse response) { public void callback(WikiStatus status, final List<WikiAppObject> appObjects) {
// handling here wiki locations // handling here wiki locations
if (status != WikiStatus.SUCCESS) { if (status != WikiStatus.SUCCESS) {
Toast.makeText(getActivity(), "Sorry, currently we have problem with interfacing wiki" + 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, // Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
// Uri.parse(item.get); // Uri.parse(item.get);
// startActivity(intent); // startActivity(intent);
final WikiPage item = response.getPages().get(position); final WikiAppObject item = appObjects.get(position);
new AQuery(getActivity()).ajax( new AQuery(getActivity()).ajax(
"https://en.wikipedia.org/w/api.php?action=query&prop=info&pageids=" "https://en.wikipedia.org/w/api.php?action=query&prop=info&pageids="
+ item.getPageId() + "&inprop=url&format=json", + item.getPageId() + "&inprop=url&format=json",
JSONObject.class, JSONObject.class,
new AjaxCallback<JSONObject>() { new AjaxCallback<JSONObject>() {
@Override @Override
public void callback(String url, JSONObject object, AjaxStatus status) { public void callback(String url, JSONObject object, AjaxStatus status) {
if (status.getCode() != 200) { if (status.getCode() != 200) {
Toast.makeText(getActivity(), Toast.makeText(getActivity(),
"Sorry, network error code: " + status.getCode(), "Sorry, network error code: " + status.getCode(),
Toast.LENGTH_LONG) Toast.LENGTH_LONG)
.show(); .show();
return; return;
} }
try { try {
String wikiUrl = object.getJSONObject("query") String wikiUrl = object.getJSONObject("query")
.getJSONObject("pages") .getJSONObject("pages")
.getJSONObject(item.getPageId().toString()) .getJSONObject(item.getPageId().toString())
.getString("fullurl"); .getString("fullurl");
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(wikiUrl)); Uri.parse(wikiUrl));
startActivity(intent); startActivity(intent);
} catch (JSONException e) { } catch (JSONException e) {
Log.e(CLASS_TAG, "Error", e); Log.e(CLASS_TAG, "Error", e);
}
} }
} }
}
); );
return false; return false;
} }
}); });
locations.setAdapter(new WikiLocationsAdapter(getActivity(), response.getPages())); locations.setAdapter(new WikiLocationsAdapter(getActivity(), appObjects));
} }
}); }
);
} }
}); });

View File

@ -5,15 +5,31 @@ package pl.tpolgrabia.urbanexplorer.utils;
*/ */
public class NumberUtils { public class NumberUtils {
public static Long safeParseLong(String s) { public static Long safeParseLong(String s) {
if (s == null || "".equals(s)) { if (s == null || "".equals(s.trim())) {
return null; return null;
} }
String trimmed = s.trim();
try { try {
return Long.parseLong(s); return Long.parseLong(trimmed);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
return null; 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;
}
}
} }

View File

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

View File

@ -2,6 +2,7 @@ package pl.tpolgrabia.urbanexplorer.utils;
import android.content.Context; import android.content.Context;
import android.util.Log; import android.util.Log;
import android.widget.Toast;
import com.androidquery.AQuery; import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback; import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus; 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_MIN_RADIUS = 10L;
private static final Long WIKI_MAX_RESULTS_LIMIT = 500L; private static final Long WIKI_MAX_RESULTS_LIMIT = 500L;
private static final Long WIKI_MIN_RESULTS = 10L; 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, public static void fetchNearPlaces(Context ctx,
final double latitude, final double latitude,
@ -157,22 +160,32 @@ public class WikiUtils {
Double radius, Double radius,
Long limit, Long limit,
final WikiGeoResponseCallback callback) { final WikiGeoResponseCallback callback) {
if (radius == null) {
radius = WIKI_STD_RADIUS;
}
if (limit == null) {
limit = WIKI_STD_LIMIT;
}
AQuery aq = new AQuery(ctx); AQuery aq = new AQuery(ctx);
aq.ajax("https://en.wikipedia.org/w/api.php" + 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<JSONObject>() {
"?action=query" +
"&list=geosearch" +
"&gscoord=" + latitude + "%7C" + longitude +
"gsradius=" + radius +
"&gslimit=" + limit, JSONObject.class, new AjaxCallback<JSONObject>() {
@Override @Override
public void callback(String url, JSONObject object, AjaxStatus status) { 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) { if (status.getCode() == 200) {
try { try {
callback.callback(WikiStatus.SUCCESS, fetchWikiGeoResponse(object)); callback.callback(WikiStatus.SUCCESS, fetchWikiGeoResponse(object));
} catch (Throwable t) { } catch (Throwable t) {
Log.e(CLASS_TAG, "General error during fetching", t);
callback.callback(WikiStatus.GENERAL_ERROR, null); callback.callback(WikiStatus.GENERAL_ERROR, null);
} }
} else { } 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); callback.callback(WikiStatus.NETWORK_ERROR, null);
} }
super.callback(url, object, status); super.callback(url, object, status);
@ -184,17 +197,16 @@ public class WikiUtils {
public static WikiGeoResponse fetchWikiGeoResponse(JSONObject object) { public static WikiGeoResponse fetchWikiGeoResponse(JSONObject object) {
WikiGeoResponse response = new WikiGeoResponse(); WikiGeoResponse response = new WikiGeoResponse();
response.setBatchComplete(object.optBoolean("batch_complete")); response.setBatchComplete(object.optBoolean("batch_complete"));
response.setQuery(fetchQueriesData(object.optJSONObject("query"))); response.setQuery(fetchQueriesData(object.optJSONObject("query").optJSONArray("geosearch")));
return response; return response;
} }
public static List<WikiGeoObject> fetchQueriesData(JSONObject object) { public static List<WikiGeoObject> fetchQueriesData(JSONArray object) {
String val;
Iterator<String> it = object.keys();
List<WikiGeoObject> geoObjects = new ArrayList<>(); List<WikiGeoObject> geoObjects = new ArrayList<>();
while (it.hasNext()) { int n = object.length();
val = it.next(); int idx;
JSONObject geoPage = object.optJSONObject(val); for (idx = 0; idx < n; idx++) {
JSONObject geoPage = object.optJSONObject(idx);
geoObjects.add(fetchWikiGeoObject(geoPage)); geoObjects.add(fetchWikiGeoObject(geoPage));
} }
return geoObjects; return geoObjects;
@ -217,11 +229,19 @@ public class WikiUtils {
final Double longitude, final Double longitude,
final Double radius, final Double radius,
final Long limit, final Long limit,
final WikiResponseCallback callback) { final WikiAppResponseCallback callback) {
fetchGeoSearchWikiMetadata(ctx, latitude, longitude, radius, limit, new WikiGeoResponseCallback() { fetchGeoSearchWikiMetadata(ctx, latitude, longitude, radius, limit, new WikiGeoResponseCallback() {
@Override @Override
public void callback(WikiStatus status, WikiGeoResponse response) { 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<WikiGeoObject> geoItems = response.getQuery(); final List<WikiGeoObject> geoItems = response.getQuery();
if (geoItems == null) { if (geoItems == null) {
return; return;
@ -237,7 +257,6 @@ public class WikiUtils {
geoItemsMap.put(geoItem.getPageId(), geoItem); geoItemsMap.put(geoItem.getPageId(), geoItem);
} }
WikiResponseCallback tcallback = callback;
fetchPageInfos(ctx, fetchPageInfos(ctx,
pageIds, pageIds,
@ -245,6 +264,7 @@ public class WikiUtils {
@Override @Override
public void callback(WikiStatus status, WikiResponse response) { public void callback(WikiStatus status, WikiResponse response) {
if (status != WikiStatus.SUCCESS) { if (status != WikiStatus.SUCCESS) {
callback.callback(WikiStatus.NETWORK_ERROR, null);
return; return;
} }
@ -256,13 +276,18 @@ public class WikiUtils {
appObject.setDistance(geoItemsMap.get(page.getPageId()).getDistance()); appObject.setDistance(geoItemsMap.get(page.getPageId()).getDistance());
appObject.setLatitude(page.getCoordinates().get(0).getLatitude()); appObject.setLatitude(page.getCoordinates().get(0).getLatitude());
appObject.setLongitude(page.getCoordinates().get(0).getLongitude()); appObject.setLongitude(page.getCoordinates().get(0).getLongitude());
appObject.setThumbnail(page.getThumbnail().getSource()); final WikiThumbnail thumbonail = page.getThumbnail();
appObject.setUrl(page.getThumbnail().getSource()); final String thumSource = thumbonail != null ? thumbonail.getSource() : null;
appObject.setThumbnail(thumSource);
appObject.setUrl(thumSource);
appObject.setPageId(page.getPageId());
results.add(appObject); results.add(appObject);
} }
// TODO here add callback invocation with result // TODO here add callback invocation with result
callback.callback(WikiStatus.SUCCESS, results);
} }
}); });
} }
@ -280,10 +305,11 @@ public class WikiUtils {
"&pithumbsize=144" + "&pithumbsize=144" +
"&pilimit=50" + "&pilimit=50" +
"&wbptterms=description" + "&wbptterms=description" +
"&pageids=" + StringUtils.join(pageIds, "|"), JSONObject.class, new AjaxCallback<JSONObject>() { "&pageids=" + StringUtils.join(pageIds, "|") +
"&format=json", JSONObject.class, new AjaxCallback<JSONObject>() {
@Override @Override
public void callback(String url, JSONObject object, AjaxStatus status) { public void callback(String url, JSONObject object, AjaxStatus status) {
if (status.getCode() != 200) { if (status.getCode() == 200) {
try { try {
callback.callback(WikiStatus.SUCCESS, fetchWikiResponse(object)); callback.callback(WikiStatus.SUCCESS, fetchWikiResponse(object));
} catch (Throwable t) { } catch (Throwable t) {

View File

@ -19,16 +19,6 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Fill Title" /> android:text="Fill Title" />
<TextView android:id="@+id/wiki_locs_item_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fill Description" />
<TextView android:id="@+id/wiki_locs_item_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fill Author" />
<TextView android:id="@+id/wiki_locs_item_distance" <TextView android:id="@+id/wiki_locs_item_distance"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"