diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java index d1e1f79..0f3c9bc 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java @@ -1,5 +1,6 @@ package pl.tpolgrabia.urbanexplorer; +import android.content.Context; import android.content.Intent; import android.location.Location; import android.location.LocationManager; @@ -86,6 +87,7 @@ public class MainActivity extends ActionBarActivity implements GestureDetector.O // lLinearLayout locations = (LinearLayout) findViewById(R.id.locations); // locations.setOnTouchListener(new OnSwipeTouchListener); gestureDetector = new GestureDetectorCompat(this, this); + locationCallback = new StandardLocationListener(); initLocalication(); } @@ -239,13 +241,17 @@ public class MainActivity extends ActionBarActivity implements GestureDetector.O private void initLocalication() { if (checkForLocalicatonEnabled()) return; + final Context ctx = this; + locationCallback.setLocationChangedCallback(new StandardLocationListenerCallback() { @Override public void callback(Location location) { double lat = location.getLatitude(); double lng = location.getLongitude(); - TextView locationInfo = (TextView) findViewById(R.id.locationInfo); - locationInfo.setText("Location: (" + lat + "," + lng + ")"); + // getSupportFragmentManager().findFragmentById(R.id.wiki_) + // TextView locationInfo = (TextView) findViewById(R.id.locationInfo); + // locationInfo.setText("Location: (" + lat + "," + lng + ")"); + Toast.makeText(ctx, "Location: (" + lat + "," + lng + ")", Toast.LENGTH_SHORT).show(); } }); } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/adapters/WikiLocationsAdapter.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/adapters/WikiLocationsAdapter.java new file mode 100644 index 0000000..1f6e7cc --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/adapters/WikiLocationsAdapter.java @@ -0,0 +1,55 @@ +package pl.tpolgrabia.urbanexplorer.adapters; + +import android.content.Context; +import android.text.Layout; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.*; +import com.nostra13.universalimageloader.core.ImageLoader; +import pl.tpolgrabia.urbanexplorer.MainActivity; +import pl.tpolgrabia.urbanexplorer.R; +import pl.tpolgrabia.urbanexplorer.dto.WikiPage; + +import java.util.List; + +/** + * Created by tpolgrabia on 01.09.16. + */ +public class WikiLocationsAdapter extends ArrayAdapter { + public WikiLocationsAdapter(Context ctx, List locations) { + super(ctx, R.layout.wiki_locations_item, locations); + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); + View inflatedView; + if (convertView != null) { + // reusing old view + inflatedView = convertView; + } else { + inflatedView = inflater.inflate(R.layout.wiki_locations_item,parent,false); + } + + WikiPage 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; + + if (url != null) { + ImageLoader.getInstance().displayImage( + url, + imgPreview, + MainActivity.options); + } + + // wiki page title + TextView pageTitle = (TextView) inflatedView.findViewById(R.id.wiki_locs_item_title); + pageTitle.setText(wikiPage.getTitle()); + + + return inflatedView; + } +} 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 5fc20b8..f58d4e8 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java @@ -51,7 +51,7 @@ public class HomeFragment extends Fragment { private TextView locationsResultInfo; private StandardLocationListener locationCallback = new StandardLocationListener(); private LocationManager locationService; - private String locationProvider = LocationUtils.getDefaultLocation(getActivity()); + private String locationProvider = null; public HomeFragment() { // Required empty public constructor @@ -61,7 +61,7 @@ public class HomeFragment extends Fragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); aq = new AQuery(getActivity()); - + locationProvider = LocationUtils.getDefaultLocation(getActivity()); locationService = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE); } 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 b689c5e..ab05f32 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java @@ -10,9 +10,14 @@ import android.view.View; import android.view.ViewGroup; import android.widget.Button; +import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import pl.tpolgrabia.urbanexplorer.R; +import pl.tpolgrabia.urbanexplorer.adapters.WikiLocationsAdapter; +import pl.tpolgrabia.urbanexplorer.callbacks.WikiResponseCallback; +import pl.tpolgrabia.urbanexplorer.callbacks.WikiStatus; +import pl.tpolgrabia.urbanexplorer.dto.WikiResponse; import pl.tpolgrabia.urbanexplorer.utils.LocationUtils; import pl.tpolgrabia.urbanexplorer.utils.WikiUtils; @@ -55,8 +60,23 @@ public class WikiLocationsFragment extends Fragment { fetchPlaces.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(getActivity())); - WikiUtils.fetchNearPlaces(this, location.getLatitude(), location.getLongitude()); + final Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(getActivity())); + WikiUtils.fetchNearPlaces(getActivity(), location.getLatitude(), location.getLongitude(), 10L, new WikiResponseCallback() { + @Override + public void callback(WikiStatus status, WikiResponse response) { + // 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.setAdapter(new WikiLocationsAdapter(getActivity(), response.getPages())); + } + }); } }); 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 5178986..7e50874 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiUtils.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiUtils.java @@ -49,19 +49,18 @@ public class WikiUtils { "&ggscoord=" + latitude + "%7C" + longitude + "&ggsradius=10000" + "&ggslimit=" + resultsLimit + - "&format" + WIKI_FORMAT; + "&format=" + WIKI_FORMAT; aq.ajax(qurl, JSONObject.class, new AjaxCallback() { @Override public void callback(String url, JSONObject object, AjaxStatus status) { if (status.getCode() == 200) { - callback.callback(WikiStatus.SUCCESS, null); - } else { try { - callback.callback(WikiStatus.NETWORK_ERROR, fetchWikiResponse(object)); + callback.callback(WikiStatus.SUCCESS, fetchWikiResponse(object)); } catch (JSONException e) { - Log.e(CLASS_TAG, "General error", e); - callback.callback(WikiStatus.GENERAL_ERROR, null); + Log.e(CLASS_TAG, "JSon error", e); } + } else { + callback.callback(WikiStatus.NETWORK_ERROR, null); } } }); @@ -71,6 +70,10 @@ public class WikiUtils { } public static WikiResponse fetchWikiResponse(JSONObject object) throws JSONException { + if (object == null) { + return null; + } + WikiResponse wikiResponse = new WikiResponse(); wikiResponse.setBatchComplete(Boolean.valueOf(object.getString("batchcomplete"))); wikiResponse.setPages(fetchPages(object.getJSONObject("query").getJSONObject("pages"))); @@ -89,16 +92,19 @@ public class WikiUtils { public static WikiPage fetchPage(JSONObject jpage) throws JSONException { WikiPage wikiPage = new WikiPage(); - wikiPage.setCoordinates(fetchCoordinates(jpage.getJSONArray("coordinates"))); - wikiPage.setIndex(jpage.getLong("index")); - wikiPage.setNs(jpage.getLong("ns")); - wikiPage.setPageId(jpage.getLong("pageId")); - wikiPage.setThumbnail(fetchThumbnail(jpage.getJSONObject("thumbnail"))); - wikiPage.setTitle(jpage.getString("title")); - return null; + wikiPage.setCoordinates(fetchCoordinates(jpage.optJSONArray("coordinates"))); + wikiPage.setIndex(jpage.optLong("index")); + wikiPage.setNs(jpage.optLong("ns")); + wikiPage.setPageId(jpage.optLong("pageid")); + wikiPage.setThumbnail(fetchThumbnail(jpage.optJSONObject("thumbnail"))); + wikiPage.setTitle(jpage.optString("title")); + return wikiPage; } public static WikiThumbnail fetchThumbnail(JSONObject jthumbnail) throws JSONException { + if (jthumbnail == null) { + return null; + } WikiThumbnail wikiThumbnail = new WikiThumbnail(); wikiThumbnail.setWidth(jthumbnail.getLong("width")); wikiThumbnail.setHeight(jthumbnail.getLong("height")); @@ -107,11 +113,15 @@ public class WikiUtils { } public static List fetchCoordinates(JSONArray jcoordinates) throws JSONException { + if (jcoordinates == null) { + return null; + } + List wikiLocations = new ArrayList(); int n = jcoordinates.length(); for (int i = 0; i < n; i++) { - wikiLocations.add(fetchCoordinate(jcoordinates.getJSONObject(i))) + wikiLocations.add(fetchCoordinate(jcoordinates.getJSONObject(i))); } return wikiLocations;