This commit is contained in:
y20k 2020-09-02 16:00:08 +02:00
parent cdcd925930
commit 2776f00eaa
No known key found for this signature in database
GPG key ID: 824D4259F41FAFF6
4 changed files with 21 additions and 16 deletions

View file

@ -52,7 +52,7 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
// todo remove after testing finished // todo: remove after testing finished
if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
StrictMode.setVmPolicy( StrictMode.setVmPolicy(
VmPolicy.Builder() VmPolicy.Builder()

View file

@ -138,6 +138,7 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener, MapOverlayHelpe
super.onStop() super.onStop()
// unbind from TrackerService // unbind from TrackerService
activity?.unbindService(connection) activity?.unbindService(connection)
handleServiceUnbind()
} }
@ -201,6 +202,17 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener, MapOverlayHelpe
} }
/* Handles state when service is being unbound */
private fun handleServiceUnbind() {
bound = false
// unregister listener for changes in shared preferences
PreferenceManager.getDefaultSharedPreferences(activity as Context).unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
// stop receiving location updates
handler.removeCallbacks(periodicLocationRequestRunnable)
}
/* Starts / pauses tracking and toggles the recording sub menu_bottom_navigation */ /* Starts / pauses tracking and toggles the recording sub menu_bottom_navigation */
private fun handleTrackingManagementMenu() { private fun handleTrackingManagementMenu() {
when (trackingState) { when (trackingState) {
@ -285,11 +297,8 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener, MapOverlayHelpe
handler.postDelayed(periodicLocationRequestRunnable, 0) handler.postDelayed(periodicLocationRequestRunnable, 0)
} }
override fun onServiceDisconnected(arg0: ComponentName) { override fun onServiceDisconnected(arg0: ComponentName) {
bound = false // service has crashed, or was killed by the system
// unregister listener for changes in shared preferences handleServiceUnbind()
PreferenceManager.getDefaultSharedPreferences(activity as Context).unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
// stop receiving location updates
handler.removeCallbacks(periodicLocationRequestRunnable)
} }
} }
/* /*

View file

@ -247,11 +247,6 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
/* Stop tracking location */ /* Stop tracking location */
fun stopTracking() { fun stopTracking() {
// stop receiving location updates - if app is not bound (= visible)
if (!bound) {
removeGpsLocationListener()
removeGpsLocationListener()
}
// save state // save state
track.recordingStop = GregorianCalendar.getInstance().time track.recordingStop = GregorianCalendar.getInstance().time
trackingState = Keys.STATE_TRACKING_STOPPED trackingState = Keys.STATE_TRACKING_STOPPED
@ -327,6 +322,7 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
// check if already registered // check if already registered
if (!gpsLocationListenerRegistered) { if (!gpsLocationListenerRegistered) {
// check if Network provider is available // check if Network provider is available
gpsProviderActive = LocationHelper.isGpsEnabled(locationManager)
if (gpsProviderActive) { if (gpsProviderActive) {
// check for location permission // check for location permission
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
@ -351,6 +347,7 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
// check if already registered // check if already registered
if (!networkLocationListenerRegistered) { if (!networkLocationListenerRegistered) {
// check if Network provider is available // check if Network provider is available
networkProviderActive = LocationHelper.isNetworkEnabled(locationManager)
if (networkProviderActive && !gpsOnly) { if (networkProviderActive && !gpsOnly) {
// check for location permission // check for location permission
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

View file

@ -207,14 +207,13 @@ data class MapFragmentLayoutHolder(private var context: Context, private var mar
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_DENIED) { if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_DENIED) {
// CASE: Location permission not granted // CASE: Location permission not granted
locationErrorBar.setText(R.string.snackbar_message_location_permission_denied) locationErrorBar.setText(R.string.snackbar_message_location_permission_denied)
locationErrorBar.show() if (!locationErrorBar.isShown) locationErrorBar.show()
} else if (!gpsProviderActive && !networkProviderActive) { } else if (!gpsProviderActive && !networkProviderActive) {
// CASE: Location setting is off // CASE: Location setting is off
locationErrorBar.setText(R.string.snackbar_message_location_offline) locationErrorBar.setText(R.string.snackbar_message_location_offline)
locationErrorBar.show() if (!locationErrorBar.isShown) locationErrorBar.show()
} else if (locationErrorBar.isShown) { } else {
// CASE: Snackbar is visible but unnecessary if (locationErrorBar.isShown) locationErrorBar.dismiss()
locationErrorBar.dismiss()
} }
} }