Wired up the GPS-only switch
This commit is contained in:
parent
5e11184562
commit
882a02eb42
3 changed files with 73 additions and 28 deletions
|
@ -52,14 +52,6 @@ class SettingsFragment : PreferenceFragmentCompat() {
|
|||
val context = preferenceManager.context
|
||||
val screen = preferenceManager.createPreferenceScreen(context)
|
||||
|
||||
// set up "Enable Imperial Measurements" preference
|
||||
val preferenceImperialMeasurementUnits: SwitchPreferenceCompat = SwitchPreferenceCompat(activity as Context)
|
||||
preferenceImperialMeasurementUnits.title = getString(R.string.pref_imperial_measurement_units_title)
|
||||
preferenceImperialMeasurementUnits.key = Keys.PREF_USE_IMPERIAL_UNITS
|
||||
preferenceImperialMeasurementUnits.summaryOn = getString(R.string.pref_imperial_measurement_units_summary_imperial)
|
||||
preferenceImperialMeasurementUnits.summaryOff = getString(R.string.pref_imperial_measurement_units_summary_metric)
|
||||
preferenceImperialMeasurementUnits.setDefaultValue(LengthUnitHelper.useImperialUnits())
|
||||
|
||||
// set up "Restrict to GPS" preference
|
||||
val preferenceGpsOnly: SwitchPreferenceCompat = SwitchPreferenceCompat(activity as Context)
|
||||
preferenceGpsOnly.title = getString(R.string.pref_gps_only_title)
|
||||
|
@ -68,6 +60,14 @@ class SettingsFragment : PreferenceFragmentCompat() {
|
|||
preferenceGpsOnly.summaryOff = getString(R.string.pref_gps_only_summary_gps_and_network)
|
||||
preferenceGpsOnly.setDefaultValue(false)
|
||||
|
||||
// set up "Use Imperial Measurements" preference
|
||||
val preferenceImperialMeasurementUnits: SwitchPreferenceCompat = SwitchPreferenceCompat(activity as Context)
|
||||
preferenceImperialMeasurementUnits.title = getString(R.string.pref_imperial_measurement_units_title)
|
||||
preferenceImperialMeasurementUnits.key = Keys.PREF_USE_IMPERIAL_UNITS
|
||||
preferenceImperialMeasurementUnits.summaryOn = getString(R.string.pref_imperial_measurement_units_summary_imperial)
|
||||
preferenceImperialMeasurementUnits.summaryOff = getString(R.string.pref_imperial_measurement_units_summary_metric)
|
||||
preferenceImperialMeasurementUnits.setDefaultValue(LengthUnitHelper.useImperialUnits())
|
||||
|
||||
// set up "Accuracy Threshold" preference
|
||||
val preferenceAccuracyThreshold: SeekBarPreference = SeekBarPreference(activity as Context)
|
||||
preferenceAccuracyThreshold.title = getString(R.string.pref_accuracy_threshold_title)
|
||||
|
@ -98,8 +98,8 @@ class SettingsFragment : PreferenceFragmentCompat() {
|
|||
|
||||
// setup preference screen
|
||||
screen.addPreference(preferenceCategoryGeneral)
|
||||
screen.addPreference(preferenceImperialMeasurementUnits)
|
||||
screen.addPreference(preferenceGpsOnly)
|
||||
screen.addPreference(preferenceImperialMeasurementUnits)
|
||||
screen.addPreference(preferenceCategoryAdvanced)
|
||||
screen.addPreference(preferenceAccuracyThreshold)
|
||||
screen.addPreference(preferenceResetAdvanced)
|
||||
|
|
|
@ -82,6 +82,9 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
|
|||
/* Overrides onCreate from Service */
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
gpsOnly = PreferencesHelper.loadGpsOnly(this)
|
||||
useImperial = PreferencesHelper.loadUseImperialUnits(this)
|
||||
locationAccuracyThreshold = PreferencesHelper.loadAccuracyThreshold(this)
|
||||
locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
||||
sensorManager = this.getSystemService(Context.SENSOR_SERVICE) as SensorManager
|
||||
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
|
@ -90,8 +93,6 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
|
|||
networkProviderActive = LocationHelper.isNetworkEnabled(locationManager)
|
||||
gpsLocationListener = createLocationListener()
|
||||
networkLocationListener = createLocationListener()
|
||||
useImperial = PreferencesHelper.loadUseImperialUnits(this)
|
||||
locationAccuracyThreshold = PreferencesHelper.loadAccuracyThreshold(this)
|
||||
trackingState = PreferencesHelper.loadTrackingState(this)
|
||||
currentBestLocation = LocationHelper.getLastKnownLocation(this)
|
||||
track = FileHelper.readTrack(this, FileHelper.getTempFileUri(this))
|
||||
|
@ -127,7 +128,8 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
|
|||
|
||||
/* Overrides onBind from Service */
|
||||
override fun onBind(p0: Intent?): IBinder? {
|
||||
addLocationListeners()
|
||||
addGpsLocationListener()
|
||||
addNetworkLocationListener()
|
||||
return binder
|
||||
}
|
||||
|
||||
|
@ -139,7 +141,8 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
|
|||
if (trackingState == Keys.STATE_TRACKING_ACTIVE) stopTracking()
|
||||
stopForeground(true)
|
||||
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
|
||||
removeLocationListeners()
|
||||
removeGpsLocationListener()
|
||||
removeNetworkLocationListener()
|
||||
backgroundJob.cancel()
|
||||
}
|
||||
|
||||
|
@ -265,14 +268,14 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
|
|||
}
|
||||
|
||||
|
||||
/* Adds location listeners to location manager */
|
||||
private fun addLocationListeners() {
|
||||
/* Adds a GPS location listener to location manager */
|
||||
private fun addGpsLocationListener() {
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
||||
if (gpsProviderActive) {
|
||||
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0f,gpsLocationListener)
|
||||
}
|
||||
if (networkProviderActive) {
|
||||
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0f,networkLocationListener)
|
||||
LogHelper.v(TAG, "Added GPS location listener.")
|
||||
} else {
|
||||
LogHelper.w(TAG, "Unable to add GPS location listener.")
|
||||
}
|
||||
} else {
|
||||
LogHelper.w(TAG, "Unable to request device location. Permission is not granted.")
|
||||
|
@ -280,10 +283,40 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
|
|||
}
|
||||
|
||||
|
||||
/* Removes location listeners from location manager */
|
||||
private fun removeLocationListeners() {
|
||||
locationManager.removeUpdates(gpsLocationListener)
|
||||
locationManager.removeUpdates(networkLocationListener)
|
||||
/* Adds a Network location listener to location manager */
|
||||
private fun addNetworkLocationListener() {
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
||||
if (networkProviderActive && !gpsOnly) {
|
||||
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0f,networkLocationListener)
|
||||
LogHelper.v(TAG, "Added Network location listener.")
|
||||
} else {
|
||||
LogHelper.w(TAG, "Unable to add Network location listener.")
|
||||
}
|
||||
} else {
|
||||
LogHelper.w(TAG, "Unable to request device location. Permission is not granted.")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Adds location listeners to location manager */
|
||||
private fun removeGpsLocationListener() {
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
||||
locationManager.removeUpdates(gpsLocationListener)
|
||||
LogHelper.v(TAG, "Removed GPS location listener.")
|
||||
} else {
|
||||
LogHelper.w(TAG, "Unable to remove GPS location listener. Location permission is needed.")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Adds location listeners to location manager */
|
||||
private fun removeNetworkLocationListener() {
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
||||
locationManager.removeUpdates(gpsLocationListener)
|
||||
LogHelper.v(TAG, "Removed Network location listener.")
|
||||
} else {
|
||||
LogHelper.w(TAG, "Unable to remove Network location listener. Location permission is needed.")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -308,15 +341,27 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
|
|||
/*
|
||||
* Defines the listener for changes in shared preferences
|
||||
*/
|
||||
val sharedPreferenceChangeListener = object : SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
|
||||
private val sharedPreferenceChangeListener = SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, key ->
|
||||
when (key) {
|
||||
Keys.PREF_GPS_ONLY -> gpsOnly = PreferencesHelper.loadGpsOnly(this@TrackerService)
|
||||
Keys.PREF_USE_IMPERIAL_UNITS -> useImperial = PreferencesHelper.loadUseImperialUnits(this@TrackerService)
|
||||
Keys.PREF_LOCATION_ACCURACY_THRESHOLD -> locationAccuracyThreshold = PreferencesHelper.loadAccuracyThreshold(this@TrackerService)
|
||||
|
||||
Keys.PREF_GPS_ONLY -> {
|
||||
gpsOnly = PreferencesHelper.loadGpsOnly(this@TrackerService)
|
||||
when (gpsOnly) {
|
||||
true -> removeNetworkLocationListener()
|
||||
false -> addNetworkLocationListener()
|
||||
}
|
||||
}
|
||||
|
||||
Keys.PREF_USE_IMPERIAL_UNITS -> {
|
||||
useImperial = PreferencesHelper.loadUseImperialUnits(this@TrackerService)
|
||||
}
|
||||
|
||||
Keys.PREF_LOCATION_ACCURACY_THRESHOLD -> {
|
||||
locationAccuracyThreshold = PreferencesHelper.loadAccuracyThreshold(this@TrackerService)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
* End of declaration
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue