Changes for better wiki integration.
parent
f3ff01fb2f
commit
c2eca0b662
|
@ -1,19 +1,28 @@
|
||||||
package pl.tpolgrabia.urbanexplorer.fragments;
|
package pl.tpolgrabia.urbanexplorer.fragments;
|
||||||
|
|
||||||
|
|
||||||
|
import android.content.Intent;
|
||||||
import android.location.Location;
|
import android.location.Location;
|
||||||
import android.location.LocationManager;
|
import android.location.LocationManager;
|
||||||
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
import android.text.Editable;
|
import android.text.Editable;
|
||||||
|
import android.util.Log;
|
||||||
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.*;
|
||||||
|
import com.androidquery.AQuery;
|
||||||
|
import com.androidquery.callback.AjaxCallback;
|
||||||
|
import com.androidquery.callback.AjaxStatus;
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
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.WikiResponseCallback;
|
import pl.tpolgrabia.urbanexplorer.callbacks.WikiResponseCallback;
|
||||||
import pl.tpolgrabia.urbanexplorer.callbacks.WikiStatus;
|
import pl.tpolgrabia.urbanexplorer.callbacks.WikiStatus;
|
||||||
|
import pl.tpolgrabia.urbanexplorer.dto.WikiPage;
|
||||||
import pl.tpolgrabia.urbanexplorer.dto.WikiResponse;
|
import pl.tpolgrabia.urbanexplorer.dto.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;
|
||||||
|
@ -27,6 +36,7 @@ import static android.content.Context.LOCATION_SERVICE;
|
||||||
public class WikiLocationsFragment extends Fragment {
|
public class WikiLocationsFragment extends Fragment {
|
||||||
|
|
||||||
|
|
||||||
|
private static final String CLASS_TAG = WikiLocationsFragment.class.getSimpleName();
|
||||||
private LocationManager locationService;
|
private LocationManager locationService;
|
||||||
private TextView currentLocation;
|
private TextView currentLocation;
|
||||||
private Button fetchPlaces;
|
private Button fetchPlaces;
|
||||||
|
@ -71,7 +81,7 @@ public class WikiLocationsFragment extends Fragment {
|
||||||
radius_limit != null ? radius_limit.toString() : null),
|
radius_limit != null ? radius_limit.toString() : null),
|
||||||
new WikiResponseCallback() {
|
new WikiResponseCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void callback(WikiStatus status, WikiResponse response) {
|
public void callback(WikiStatus status, final WikiResponse response) {
|
||||||
// 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" +
|
||||||
|
@ -82,6 +92,47 @@ public class WikiLocationsFragment extends Fragment {
|
||||||
// TODO on success
|
// TODO on success
|
||||||
|
|
||||||
ListView locations = (ListView) inflatedView.findViewById(R.id.wiki_places);
|
ListView locations = (ListView) inflatedView.findViewById(R.id.wiki_places);
|
||||||
|
locations.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
|
||||||
|
@Override
|
||||||
|
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
|
||||||
|
// WikiPage item = response.getPages().get(position);
|
||||||
|
// Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
|
||||||
|
// Uri.parse(item.get);
|
||||||
|
// startActivity(intent);
|
||||||
|
final WikiPage item = response.getPages().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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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(), response.getPages()));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -42,7 +42,7 @@ public class WikiUtils {
|
||||||
@Override
|
@Override
|
||||||
public void callback(String url, JSONObject object, AjaxStatus status) {
|
public void callback(String url, JSONObject object, AjaxStatus status) {
|
||||||
// TODO handle response
|
// TODO handle response
|
||||||
String qurl = "https://en.wikipedia.org/w/api.php?" +
|
final String qurl = "https://en.wikipedia.org/w/api.php?" +
|
||||||
"action=query" +
|
"action=query" +
|
||||||
"&prop=coordinates%7Cpageimages%7Cpageterms" +
|
"&prop=coordinates%7Cpageimages%7Cpageterms" +
|
||||||
"&colimit=50" +
|
"&colimit=50" +
|
||||||
|
@ -63,7 +63,7 @@ public class WikiUtils {
|
||||||
try {
|
try {
|
||||||
callback.callback(WikiStatus.SUCCESS, fetchWikiResponse(object));
|
callback.callback(WikiStatus.SUCCESS, fetchWikiResponse(object));
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
Log.e(CLASS_TAG, "JSon error", e);
|
Log.e(CLASS_TAG, "JSon error: " + object.toString(), e);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
callback.callback(WikiStatus.NETWORK_ERROR, null);
|
callback.callback(WikiStatus.NETWORK_ERROR, null);
|
||||||
|
|
Loading…
Reference in New Issue