From 48e5b9378f188c3331235b027de35db65bd114ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20P=C3=B3=C5=82grabia?= Date: Wed, 14 Sep 2016 22:15:24 +0200 Subject: [PATCH] Added settings for gps update frequenciy policies. --- .../urbanexplorer/AppConstants.java | 2 ++ .../urbanexplorer/MainActivity.java | 33 +++++++++++++++---- .../urbanexplorer/fragments/HomeFragment.java | 2 +- .../urbanexplorer/utils/NumberUtils.java | 14 ++++++++ app/src/main/res/xml/urban_expl_settings.xml | 21 ++++++++++++ 5 files changed, 65 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java index e318b97..b05c5ef 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java @@ -9,4 +9,6 @@ public class AppConstants { public static final float MIN_DISTANCE = 100; public static final float PAMNORAMIO_DEF_RADIUSX = 0.05f; public static final float PAMNORAMIO_DEF_RADIUSY = 0.05f; + public static final long GPS_LOCATION_UPDATE_FREQ = 15000; + public static final float GPS_LOCATION_DISTANCE_FREQ = MIN_DISTANCE; } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java index 7b5785f..97388b5 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java @@ -2,9 +2,11 @@ package pl.tpolgrabia.urbanexplorer; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.location.Location; import android.location.LocationManager; import android.os.Bundle; +import android.preference.PreferenceManager; import android.provider.Settings; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; @@ -30,6 +32,7 @@ import pl.tpolgrabia.urbanexplorer.fragments.WikiLocationsFragment; import pl.tpolgrabia.urbanexplorer.handlers.SwipeHandler; import pl.tpolgrabia.urbanexplorer.utils.ImageLoaderUtils; import pl.tpolgrabia.urbanexplorer.utils.LocationUtils; +import pl.tpolgrabia.urbanexplorer.utils.NumberUtils; import pl.tpolgrabia.urbanexplorer.views.CustomInterceptor; import pl.tpolgrabia.urbanexplorer.views.SwipeFrameLayout; @@ -221,13 +224,33 @@ public class MainActivity extends ActionBarActivity { if (locationProvider != null) { LocationManager locationService = (LocationManager)getSystemService(LOCATION_SERVICE); locationService.requestLocationUpdates(locationProvider, - AppConstants.MIN_TIME, - AppConstants.MIN_DISTANCE, + fetchGpsUpdateFreq(), + fetchGpsDistanceFreq(), locationCallback); locationServicesActivated = true; } } + private Float fetchGpsDistanceFreq() { + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); + String prefDistanceUpdateFreq = sharedPreferences.getString( + "pref_gps_distance_freq", + String.valueOf(AppConstants.GPS_LOCATION_DISTANCE_FREQ)); + + Log.d(CLASS_TAG, "Pref GPS distance update frequency " + prefDistanceUpdateFreq); + return NumberUtils.safeParseFloat(prefDistanceUpdateFreq); + } + + private Long fetchGpsUpdateFreq() { + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); + String prefGpsUpdateFreq = sharedPreferences.getString( + "pref_gps_update_freq", + String.valueOf(AppConstants.GPS_LOCATION_UPDATE_FREQ)); + + Log.d(CLASS_TAG, "Pref GPS location update frequency " + prefGpsUpdateFreq); + return NumberUtils.safeParseLong(prefGpsUpdateFreq); + } + @Override protected void onPause() { super.onPause(); @@ -251,10 +274,8 @@ public class MainActivity extends ActionBarActivity { case LOCATION_SETTINGS_REQUEST_ID: String locationProvider = LocationUtils.getDefaultLocation(this); if (locationProvider == null) { - // sadly, nothing to do except from notifing user that program is not enable working -// Toast.makeText(this, "Sorry location services are not working." + -// " Program cannot work properly - check location settings to allow program working correctly", -// Toast.LENGTH_LONG).show(); + + // launching settings activity to allow the user switching on location service Intent intent = new Intent(this, SettingsActivity.class); startActivity(intent); 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 b8a88a1..e8bcfaf 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java @@ -289,7 +289,7 @@ public class HomeFragment extends Fragment { final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); final String pref_panoramio_radiusy = sharedPreferences.getString( "pref_panoramio_radiusy", - String.valueOf(AppConstants.PAMNORAMIO_DEF_RADIUSX)); + String.valueOf(AppConstants.PAMNORAMIO_DEF_RADIUSY)); Log.d(CLASS_TAG, "Panoramio radiusy pref equals " + pref_panoramio_radiusy); return Double.parseDouble( pref_panoramio_radiusy); 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 2e4b74b..4de8350 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/NumberUtils.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/NumberUtils.java @@ -32,4 +32,18 @@ public class NumberUtils { return null; } } + + public static Float safeParseFloat(String s) { + if (s == null || "".equals(s.trim())) { + return null; + } + + String trimmed = s.trim(); + + try { + return Double.parseDouble(trimmed); + } catch (NumberFormatException e) { + return null; + } + } } diff --git a/app/src/main/res/xml/urban_expl_settings.xml b/app/src/main/res/xml/urban_expl_settings.xml index da3aefd..f628945 100644 --- a/app/src/main/res/xml/urban_expl_settings.xml +++ b/app/src/main/res/xml/urban_expl_settings.xml @@ -41,4 +41,25 @@ + + + + + + + + +