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 context = preferenceManager.context
|
||||||
val screen = preferenceManager.createPreferenceScreen(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
|
// set up "Restrict to GPS" preference
|
||||||
val preferenceGpsOnly: SwitchPreferenceCompat = SwitchPreferenceCompat(activity as Context)
|
val preferenceGpsOnly: SwitchPreferenceCompat = SwitchPreferenceCompat(activity as Context)
|
||||||
preferenceGpsOnly.title = getString(R.string.pref_gps_only_title)
|
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.summaryOff = getString(R.string.pref_gps_only_summary_gps_and_network)
|
||||||
preferenceGpsOnly.setDefaultValue(false)
|
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
|
// set up "Accuracy Threshold" preference
|
||||||
val preferenceAccuracyThreshold: SeekBarPreference = SeekBarPreference(activity as Context)
|
val preferenceAccuracyThreshold: SeekBarPreference = SeekBarPreference(activity as Context)
|
||||||
preferenceAccuracyThreshold.title = getString(R.string.pref_accuracy_threshold_title)
|
preferenceAccuracyThreshold.title = getString(R.string.pref_accuracy_threshold_title)
|
||||||
|
@ -98,8 +98,8 @@ class SettingsFragment : PreferenceFragmentCompat() {
|
||||||
|
|
||||||
// setup preference screen
|
// setup preference screen
|
||||||
screen.addPreference(preferenceCategoryGeneral)
|
screen.addPreference(preferenceCategoryGeneral)
|
||||||
screen.addPreference(preferenceImperialMeasurementUnits)
|
|
||||||
screen.addPreference(preferenceGpsOnly)
|
screen.addPreference(preferenceGpsOnly)
|
||||||
|
screen.addPreference(preferenceImperialMeasurementUnits)
|
||||||
screen.addPreference(preferenceCategoryAdvanced)
|
screen.addPreference(preferenceCategoryAdvanced)
|
||||||
screen.addPreference(preferenceAccuracyThreshold)
|
screen.addPreference(preferenceAccuracyThreshold)
|
||||||
screen.addPreference(preferenceResetAdvanced)
|
screen.addPreference(preferenceResetAdvanced)
|
||||||
|
|
|
@ -82,6 +82,9 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
|
||||||
/* Overrides onCreate from Service */
|
/* Overrides onCreate from Service */
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
|
gpsOnly = PreferencesHelper.loadGpsOnly(this)
|
||||||
|
useImperial = PreferencesHelper.loadUseImperialUnits(this)
|
||||||
|
locationAccuracyThreshold = PreferencesHelper.loadAccuracyThreshold(this)
|
||||||
locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
||||||
sensorManager = this.getSystemService(Context.SENSOR_SERVICE) as SensorManager
|
sensorManager = this.getSystemService(Context.SENSOR_SERVICE) as SensorManager
|
||||||
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||||
|
@ -90,8 +93,6 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
|
||||||
networkProviderActive = LocationHelper.isNetworkEnabled(locationManager)
|
networkProviderActive = LocationHelper.isNetworkEnabled(locationManager)
|
||||||
gpsLocationListener = createLocationListener()
|
gpsLocationListener = createLocationListener()
|
||||||
networkLocationListener = createLocationListener()
|
networkLocationListener = createLocationListener()
|
||||||
useImperial = PreferencesHelper.loadUseImperialUnits(this)
|
|
||||||
locationAccuracyThreshold = PreferencesHelper.loadAccuracyThreshold(this)
|
|
||||||
trackingState = PreferencesHelper.loadTrackingState(this)
|
trackingState = PreferencesHelper.loadTrackingState(this)
|
||||||
currentBestLocation = LocationHelper.getLastKnownLocation(this)
|
currentBestLocation = LocationHelper.getLastKnownLocation(this)
|
||||||
track = FileHelper.readTrack(this, FileHelper.getTempFileUri(this))
|
track = FileHelper.readTrack(this, FileHelper.getTempFileUri(this))
|
||||||
|
@ -127,7 +128,8 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
|
||||||
|
|
||||||
/* Overrides onBind from Service */
|
/* Overrides onBind from Service */
|
||||||
override fun onBind(p0: Intent?): IBinder? {
|
override fun onBind(p0: Intent?): IBinder? {
|
||||||
addLocationListeners()
|
addGpsLocationListener()
|
||||||
|
addNetworkLocationListener()
|
||||||
return binder
|
return binder
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,7 +141,8 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
|
||||||
if (trackingState == Keys.STATE_TRACKING_ACTIVE) stopTracking()
|
if (trackingState == Keys.STATE_TRACKING_ACTIVE) stopTracking()
|
||||||
stopForeground(true)
|
stopForeground(true)
|
||||||
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
|
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
|
||||||
removeLocationListeners()
|
removeGpsLocationListener()
|
||||||
|
removeNetworkLocationListener()
|
||||||
backgroundJob.cancel()
|
backgroundJob.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -265,14 +268,14 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Adds location listeners to location manager */
|
/* Adds a GPS location listener to location manager */
|
||||||
private fun addLocationListeners() {
|
private fun addGpsLocationListener() {
|
||||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
||||||
if (gpsProviderActive) {
|
if (gpsProviderActive) {
|
||||||
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0f,gpsLocationListener)
|
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0f,gpsLocationListener)
|
||||||
}
|
LogHelper.v(TAG, "Added GPS location listener.")
|
||||||
if (networkProviderActive) {
|
} else {
|
||||||
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0f,networkLocationListener)
|
LogHelper.w(TAG, "Unable to add GPS location listener.")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
LogHelper.w(TAG, "Unable to request device location. Permission is not granted.")
|
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 */
|
/* Adds a Network location listener to location manager */
|
||||||
private fun removeLocationListeners() {
|
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)
|
locationManager.removeUpdates(gpsLocationListener)
|
||||||
locationManager.removeUpdates(networkLocationListener)
|
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,14 +341,26 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
|
||||||
/*
|
/*
|
||||||
* Defines the listener for changes in shared preferences
|
* Defines the listener for changes in shared preferences
|
||||||
*/
|
*/
|
||||||
val sharedPreferenceChangeListener = object : SharedPreferences.OnSharedPreferenceChangeListener {
|
private val sharedPreferenceChangeListener = SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, key ->
|
||||||
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
|
|
||||||
when (key) {
|
when (key) {
|
||||||
Keys.PREF_GPS_ONLY -> gpsOnly = PreferencesHelper.loadGpsOnly(this@TrackerService)
|
|
||||||
Keys.PREF_USE_IMPERIAL_UNITS -> useImperial = PreferencesHelper.loadUseImperialUnits(this@TrackerService)
|
Keys.PREF_GPS_ONLY -> {
|
||||||
Keys.PREF_LOCATION_ACCURACY_THRESHOLD -> locationAccuracyThreshold = PreferencesHelper.loadAccuracyThreshold(this@TrackerService)
|
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
|
* End of declaration
|
||||||
|
|
Loading…
Reference in a new issue