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.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<WikiPage> {
public WikiLocationsAdapter(Context ctx, List<WikiPage> locations) {
public class WikiLocationsAdapter extends ArrayAdapter<WikiAppObject> {
public WikiLocationsAdapter(Context ctx, List<WikiAppObject> 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);
}
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(

View File

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

View File

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

View File

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

View File

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

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.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<JSONObject>() {
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>() {
@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<WikiGeoObject> fetchQueriesData(JSONObject object) {
String val;
Iterator<String> it = object.keys();
public static List<WikiGeoObject> fetchQueriesData(JSONArray object) {
List<WikiGeoObject> 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<WikiGeoObject> 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<JSONObject>() {
"&pageids=" + StringUtils.join(pageIds, "|") +
"&format=json", JSONObject.class, new AjaxCallback<JSONObject>() {
@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) {

View File

@ -19,16 +19,6 @@
android:layout_height="wrap_content"
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"
android:layout_width="wrap_content"
android:layout_height="wrap_content"