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 f58d4e8..03e8db9 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java @@ -126,7 +126,7 @@ public class HomeFragment extends Fragment { @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { - pageId = Math.max(1, NumberUtils.safeParseLong(charSequence)); + pageId = Math.max(1, NumberUtils.safeParseLong(safeAndroidText2String(charSequence))); Log.d(CLASS_TAG, "text changed"); } @@ -252,11 +252,16 @@ public class HomeFragment extends Fragment { } private Long fetchLocationPageSize() { - return NumberUtils.safeParseLong(pageSizeWidget.getText()); + CharSequence page_size = pageSizeWidget.getText(); + return NumberUtils.safeParseLong(safeAndroidText2String(page_size)); + } + + private String safeAndroidText2String(CharSequence page_size) { + return page_size != null ? page_size.toString() : null; } private Long fetchLocationPageId() { - return Math.max(0L, NumberUtils.safeParseLong(pageIdWidget.getText())); + return Math.max(0L, NumberUtils.safeParseLong(safeAndroidText2String(pageIdWidget.getText()))); } private Double fetchRadiusX() { 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 4e1070c..ffbb727 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java @@ -5,6 +5,7 @@ import android.location.Location; import android.location.LocationManager; import android.os.Bundle; import android.support.v4.app.Fragment; +import android.text.Editable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -58,14 +59,16 @@ public class WikiLocationsFragment extends Fragment { @Override public void onClick(View v) { 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(), location.getLatitude(), location.getLongitude(), NumberUtils.safeParseLong( - ((EditText) inflatedView.findViewById(R.id.wiki_search_limit)).getText()), + search_limit != null ? search_limit.toString(): null), NumberUtils.safeParseLong( - ((EditText) inflatedView.findViewById(R.id.wiki_search_radius)).getText()), + radius_limit != null ? radius_limit.toString() : null), new WikiResponseCallback() { @Override public void callback(WikiStatus status, WikiResponse response) { diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/NumberUtils.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/NumberUtils.java index ca35248..5240307 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/NumberUtils.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/NumberUtils.java @@ -4,15 +4,15 @@ package pl.tpolgrabia.urbanexplorer.utils; * Created by tpolgrabia on 27.08.16. */ public class NumberUtils { - public static Long safeParseLong(CharSequence charSequence) { - if (charSequence == null) { - return 1L; + public static Long safeParseLong(String s) { + if (s == null || "".equals(s)) { + return null; } try { - return Long.parseLong(charSequence.toString()); + return Long.parseLong(s); } catch (NumberFormatException e) { - return 1L; + return null; } } 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 196adf3..8ea555d 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiUtils.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiUtils.java @@ -19,6 +19,7 @@ import pl.tpolgrabia.urbanexplorer.dto.WikiThumbnail; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Objects; /** * Created by tpolgrabia on 28.08.16. @@ -26,12 +27,15 @@ import java.util.List; public class WikiUtils { private static final String CLASS_TAG = WikiUtils.class.getSimpleName(); private static final String WIKI_FORMAT = "json"; + 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; public static void fetchNearPlaces(Context ctx, final double latitude, final double longitude, - final long resultsLimit, - final long radiusLimit, + final Long resultsLimit, + final Long radiusLimit, final WikiResponseCallback callback) { final AQuery aq = new AQuery(ctx); aq.ajax("TODO", JSONObject.class, new AjaxCallback(){ @@ -48,8 +52,9 @@ public class WikiUtils { "&wbptterms=description" + "&generator=geosearch" + "&ggscoord=" + latitude + "%7C" + longitude + - "&ggsradius=" + radiusLimit + - "&ggslimit=" + resultsLimit + + "&ggsradius=" + Math.max(WIKI_MIN_RADIUS, ifNullSet(radiusLimit, 10000L)) + + "&ggslimit=" + Math.min(WIKI_MIN_RESULTS, Math.max(WIKI_MAX_RESULTS_LIMIT, + checkIfNullLong(resultsLimit))) + "&format=" + WIKI_FORMAT; aq.ajax(qurl, JSONObject.class, new AjaxCallback() { @Override @@ -70,6 +75,14 @@ public class WikiUtils { }); } + private static T ifNullSet(T val, T def) { + return val == null ? def : val; + } + + private static Long checkIfNullLong(Long lval) { + return lval != null ? lval : 0L; + } + public static WikiResponse fetchWikiResponse(JSONObject object) throws JSONException { if (object == null) { return null;