From 2af4e791a34345a03b586cf52d27fab8aafc97ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20P=C3=B3=C5=82grabia?= Date: Thu, 6 Oct 2016 20:53:25 +0200 Subject: [PATCH] Google places settings are ready now. --- .../urbanexplorer/AppConstants.java | 1 - .../fragments/PlacesFragment.java | 14 +++++++++---- .../constants/UtilConstants.java | 4 ++++ .../utils/SettingsUtils.java | 20 +++++++++++++++++++ 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java index ae5695a..ce7e970 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java @@ -20,5 +20,4 @@ public class AppConstants { static final String SAVED_CONFIG_KEY = "SAVED_CONFIG_KEY"; public static final String GOOGLE_API_KEY = "AIzaSyBAJoK-pu_qnQ0U8EGjM1Zkz_g8oJV4w2g"; public static final String DEF_WIKI_COUNTRY_CODE = "en"; - public static final Double DEF_PLACES_RADIUS = 5000.0; } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PlacesFragment.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PlacesFragment.java index 7d10bb6..206c5aa 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PlacesFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PlacesFragment.java @@ -35,6 +35,7 @@ import pl.tpolgrabia.urbanexplorer.worker.GooglePlacesWorker; import pl.tpolgrabia.urbanexplorerutils.callbacks.StandardLocationListenerCallback; import pl.tpolgrabia.urbanexplorerutils.events.RefreshEvent; import pl.tpolgrabia.urbanexplorerutils.utils.LocationUtils; +import pl.tpolgrabia.urbanexplorerutils.utils.SettingsUtils; import java.io.*; import java.util.ArrayList; @@ -224,8 +225,8 @@ public class PlacesFragment extends Fragment { GooglePlacesRequest request = new GooglePlacesRequest(); request.setLocation(location); - request.setSearchRadius(AppConstants.DEF_PLACES_RADIUS); - request.setSearchItemType(GooglePlacesConstants.PLACES_SEARCH_TYPE); + request.setSearchRadius(SettingsUtils.getDefaultPlacesSearchRadius(getActivity())); + request.setSearchItemType(SettingsUtils.getPlacesSearchCategories(getActivity())); new GooglePlacesWorker(getActivity()).execute(request); } @@ -243,13 +244,18 @@ public class PlacesFragment extends Fragment { return; } + if (getActivity() == null) { + lg.debug("Headless fragment, no activity - no context"); + return; + } + lg.debug("Loading next page"); Location location = LocationUtils.getLastKnownLocation(getActivity()); GooglePlacesRequest request = new GooglePlacesRequest(); request.setLocation(location); - request.setSearchRadius(AppConstants.DEF_PLACES_RADIUS); - request.setSearchItemType(GooglePlacesConstants.PLACES_SEARCH_TYPE); + request.setSearchRadius(SettingsUtils.getDefaultPlacesSearchRadius(getActivity())); + request.setSearchItemType(SettingsUtils.getPlacesSearchCategories(getActivity())); request.setPageToken(nextPageToken); new GooglePlacesWorker(getActivity()).execute(request); } diff --git a/urbanexplorerutils/src/main/java/pl/tpolgrabia/urbanexplorerutils/constants/UtilConstants.java b/urbanexplorerutils/src/main/java/pl/tpolgrabia/urbanexplorerutils/constants/UtilConstants.java index bd9a582..6251e2b 100644 --- a/urbanexplorerutils/src/main/java/pl/tpolgrabia/urbanexplorerutils/constants/UtilConstants.java +++ b/urbanexplorerutils/src/main/java/pl/tpolgrabia/urbanexplorerutils/constants/UtilConstants.java @@ -20,4 +20,8 @@ public class UtilConstants { public static final String PANORAMIO_BULK_SIZE_KEY = "pref_panoramio_bulk_size"; public static final int PANORAMIO_BULK_SIZE_DEF_VALUE = 50; + public static final double GOOGLE_PLACES_STD_UNIT = 1000.0; + public static final String PREF_GOOGLE_PLACES_RADIUS = "pref_gplaces_radius"; + public static final Double DEF_PLACES_RADIUS = 5000.0; + public static final String GOOGLE_PLACES_CATEGORIES_PREF = "pref_gplaces_categories"; } diff --git a/urbanexplorerutils/src/main/java/pl/tpolgrabia/urbanexplorerutils/utils/SettingsUtils.java b/urbanexplorerutils/src/main/java/pl/tpolgrabia/urbanexplorerutils/utils/SettingsUtils.java index 818ac8c..fa93e54 100644 --- a/urbanexplorerutils/src/main/java/pl/tpolgrabia/urbanexplorerutils/utils/SettingsUtils.java +++ b/urbanexplorerutils/src/main/java/pl/tpolgrabia/urbanexplorerutils/utils/SettingsUtils.java @@ -3,10 +3,13 @@ package pl.tpolgrabia.urbanexplorerutils.utils; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import pl.tpolgrabia.urbanexplorerutils.constants.UtilConstants; +import java.util.HashSet; + /** * Created by tpolgrabia on 24.09.16. */ @@ -60,4 +63,21 @@ public class SettingsUtils { lg.debug("Pref wiki search results limit {}", prefWikiResultsLimit); return NumberUtils.safeParseLong(prefWikiResultsLimit); } + + public static Double getDefaultPlacesSearchRadius(Context ctx) { + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(ctx); + try { + return Double.parseDouble(sharedPrefs.getString(UtilConstants.PREF_GOOGLE_PLACES_RADIUS, + UtilConstants.DEF_PLACES_RADIUS.toString())) * UtilConstants.GOOGLE_PLACES_STD_UNIT; + } catch (NumberFormatException e) { + lg.error("Invalid settings for google places search radius", e); + return UtilConstants.DEF_PLACES_RADIUS; + } + } + + public static String getPlacesSearchCategories(Context ctx) { + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(ctx); + return StringUtils.join("|", + sharedPrefs.getStringSet(UtilConstants.GOOGLE_PLACES_CATEGORIES_PREF, new HashSet())); + } }