parent
cb109107ad
commit
983b39efdf
|
@ -133,10 +133,12 @@ public class MainActivity extends ActionBarActivity {
|
||||||
switch (currentFragmentId) {
|
switch (currentFragmentId) {
|
||||||
case HOME_FRAGMENT_ID:
|
case HOME_FRAGMENT_ID:
|
||||||
// switch to home fragment
|
// switch to home fragment
|
||||||
|
setTitle("Panoramio search");
|
||||||
Log.d(CLASS_TAG, "Switching to home fragment");
|
Log.d(CLASS_TAG, "Switching to home fragment");
|
||||||
switchFragment(new HomeFragment());
|
switchFragment(new HomeFragment());
|
||||||
break;
|
break;
|
||||||
case WIKI_FRAGMENT_ID:
|
case WIKI_FRAGMENT_ID:
|
||||||
|
setTitle("Wiki search");
|
||||||
// switch to wiki fragment
|
// switch to wiki fragment
|
||||||
Log.d(CLASS_TAG, "Switching to wiki fragment");
|
Log.d(CLASS_TAG, "Switching to wiki fragment");
|
||||||
switchFragment(new WikiLocationsFragment());
|
switchFragment(new WikiLocationsFragment());
|
||||||
|
|
|
@ -55,117 +55,110 @@ public class WikiLocationsFragment extends Fragment {
|
||||||
// Inflate the layout for this fragment
|
// Inflate the layout for this fragment
|
||||||
final View inflatedView = inflater.inflate(R.layout.fragment_wiki_locations, container, false);
|
final View inflatedView = inflater.inflate(R.layout.fragment_wiki_locations, container, false);
|
||||||
|
|
||||||
inflatedView.findViewById(R.id.wiki_fetch_places).setOnClickListener(
|
|
||||||
new View.OnClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onClick(View v) {
|
|
||||||
// TODO replace this
|
|
||||||
Toast.makeText(getActivity(), "Fetch wiki objects", Toast.LENGTH_SHORT);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
locationService = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE);
|
locationService = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE);
|
||||||
currentLocation = (TextView) inflatedView.findViewById(R.id.wiki_current_location);
|
currentLocation = (TextView) inflatedView.findViewById(R.id.wiki_current_location);
|
||||||
fetchPlaces = (Button)inflatedView.findViewById(R.id.wiki_fetch_places);
|
|
||||||
fetchPlaces.setOnClickListener(new View.OnClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onClick(View v) {
|
|
||||||
final Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(getActivity()));
|
|
||||||
|
|
||||||
if (location == null) {
|
|
||||||
Log.i(CLASS_TAG, "Sorry, location is still not available");
|
|
||||||
Toast.makeText(getActivity(), "Sorry, location is still not available", Toast.LENGTH_SHORT).show();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
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.fetchAppData(getActivity(),
|
|
||||||
location.getLatitude(),
|
|
||||||
location.getLongitude(),
|
|
||||||
NumberUtils.safeParseDouble(search_limit != null ? search_limit.toString() : null),
|
|
||||||
NumberUtils.safeParseLong(
|
|
||||||
radius_limit != null ? radius_limit.toString() : null),
|
|
||||||
new WikiAppResponseCallback() {
|
|
||||||
@Override
|
|
||||||
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" +
|
|
||||||
": " + status + ". Try again later", Toast.LENGTH_SHORT).show();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO on success
|
|
||||||
|
|
||||||
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 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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(), appObjects));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
MainActivity mainActivity = (MainActivity) getActivity();
|
MainActivity mainActivity = (MainActivity) getActivity();
|
||||||
mainActivity.getLocationCallback().addCallback(new StandardLocationListenerCallback() {
|
mainActivity.getLocationCallback().addCallback(new StandardLocationListenerCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void callback(Location location) {
|
public void callback(Location location) {
|
||||||
updateLocationInfo();
|
updateLocationInfo();
|
||||||
|
fetchWikiLocations();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return inflatedView;
|
return inflatedView;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void fetchWikiLocations() {
|
||||||
|
final Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(getActivity()));
|
||||||
|
|
||||||
|
if (location == null) {
|
||||||
|
Log.i(CLASS_TAG, "Sorry, location is still not available");
|
||||||
|
Toast.makeText(getActivity(), "Sorry, location is still not available", Toast.LENGTH_SHORT).show();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (getView() == null) {
|
||||||
|
Log.i(CLASS_TAG, "Wiki view is not yet initialized");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Editable search_limit = ((EditText) getView().findViewById(R.id.wiki_search_limit)).getText();
|
||||||
|
Editable radius_limit = ((EditText) getView().findViewById(R.id.wiki_search_radius)).getText();
|
||||||
|
|
||||||
|
|
||||||
|
WikiUtils.fetchAppData(getActivity(),
|
||||||
|
location.getLatitude(),
|
||||||
|
location.getLongitude(),
|
||||||
|
NumberUtils.safeParseDouble(search_limit != null ? search_limit.toString() : null),
|
||||||
|
NumberUtils.safeParseLong(
|
||||||
|
radius_limit != null ? radius_limit.toString() : null),
|
||||||
|
new WikiAppResponseCallback() {
|
||||||
|
@Override
|
||||||
|
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" +
|
||||||
|
": " + status + ". Try again later", Toast.LENGTH_SHORT).show();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO on success
|
||||||
|
|
||||||
|
ListView locations = (ListView) getView().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 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
String wikiUrl = object.getJSONObject("query")
|
||||||
|
.getJSONObject("pages")
|
||||||
|
.getJSONObject(item.getPageId().toString())
|
||||||
|
.getString("fullurl");
|
||||||
|
Intent intent = new Intent(Intent.ACTION_VIEW,
|
||||||
|
Uri.parse(wikiUrl));
|
||||||
|
startActivity(intent);
|
||||||
|
} catch (JSONException e) {
|
||||||
|
Log.e(CLASS_TAG, "Error", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
locations.setAdapter(new WikiLocationsAdapter(getActivity(), appObjects));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
updateLocationInfo();
|
updateLocationInfo();
|
||||||
|
fetchWikiLocations();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateLocationInfo() {
|
public void updateLocationInfo() {
|
||||||
|
|
|
@ -36,9 +36,4 @@
|
||||||
|
|
||||||
</ListView>
|
</ListView>
|
||||||
|
|
||||||
<Button android:id="@+id/wiki_fetch_places"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:text="Fetch wiki places in the neighbourhood"/>
|
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
Loading…
Reference in New Issue