quick settings tile was unnecessarily bound to tracker service. better to use an intent to start stop recording
This commit is contained in:
parent
6a989a8fc3
commit
9591f9f8c0
4 changed files with 32 additions and 65 deletions
|
@ -60,7 +60,6 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener {
|
|||
private lateinit var currentBestLocation: Location
|
||||
private lateinit var layout: MapFragmentLayoutHolder
|
||||
private lateinit var trackerService: TrackerService
|
||||
private lateinit var sharedPreferenceChangeListener2:SharedPreferences.OnSharedPreferenceChangeListener
|
||||
|
||||
|
||||
/* Overrides onCreate from Fragment */
|
||||
|
@ -240,31 +239,18 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener {
|
|||
private val sharedPreferenceChangeListener = SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, key ->
|
||||
when (key) {
|
||||
Keys.PREF_TRACKING_STATE -> {
|
||||
if (activity != null) {
|
||||
trackingState = PreferencesHelper.loadTrackingState(activity as Context)
|
||||
layout.updateRecordingButton(trackingState)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
* End of declaration
|
||||
*/
|
||||
|
||||
|
||||
private fun createSharedPreferenceChangeListener(): SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
return SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, key ->
|
||||
when (key) {
|
||||
Keys.PREF_TRACKING_STATE -> {
|
||||
if (isAdded) {
|
||||
trackingState = PreferencesHelper.loadTrackingState(activity as Context)
|
||||
layout.updateRecordingButton(trackingState)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Defines callbacks for service binding, passed to bindService()
|
||||
*/
|
||||
|
@ -275,8 +261,7 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener {
|
|||
trackerService = binder.service
|
||||
bound = true
|
||||
// register listener for changes in shared preferences
|
||||
sharedPreferenceChangeListener2 = createSharedPreferenceChangeListener()
|
||||
PreferenceManager.getDefaultSharedPreferences(activity as Context).registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener2)
|
||||
PreferenceManager.getDefaultSharedPreferences(activity as Context).registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
|
||||
// start listening for location updates
|
||||
handler.removeCallbacks(periodicLocationRequestRunnable)
|
||||
handler.postDelayed(periodicLocationRequestRunnable, 0)
|
||||
|
@ -284,7 +269,7 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener {
|
|||
override fun onServiceDisconnected(arg0: ComponentName) {
|
||||
bound = false
|
||||
// unregister listener for changes in shared preferences
|
||||
PreferenceManager.getDefaultSharedPreferences(activity as Context).unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener2)
|
||||
PreferenceManager.getDefaultSharedPreferences(activity as Context).unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
|
||||
// stop receiving location updates
|
||||
handler.removeCallbacks(periodicLocationRequestRunnable)
|
||||
}
|
||||
|
|
|
@ -186,6 +186,7 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
|
|||
/* Start tracking location */
|
||||
fun startTracking(newTrack: Boolean = true) {
|
||||
if (newTrack) {
|
||||
track = Track()
|
||||
track.recordingStart = GregorianCalendar.getInstance().time
|
||||
track.recordingStop = track.recordingStart
|
||||
track.name = DateTimeHelper.convertToReadableDate(track.recordingStart)
|
||||
|
|
|
@ -17,10 +17,10 @@
|
|||
|
||||
package org.y20k.trackbook
|
||||
|
||||
import android.content.*
|
||||
import android.content.Intent
|
||||
import android.content.SharedPreferences
|
||||
import android.graphics.drawable.Icon
|
||||
import android.os.Build
|
||||
import android.os.IBinder
|
||||
import android.service.quicksettings.Tile
|
||||
import android.service.quicksettings.TileService
|
||||
import androidx.preference.PreferenceManager
|
||||
|
@ -48,6 +48,8 @@ class TrackingToggleTileService(): TileService() {
|
|||
super.onTileAdded()
|
||||
// get saved tracking state
|
||||
trackingState = PreferencesHelper.loadTrackingState(this)
|
||||
// set up tile
|
||||
updateTile()
|
||||
}
|
||||
|
||||
/* Overrides onTileRemoved from TileService */
|
||||
|
@ -59,8 +61,8 @@ class TrackingToggleTileService(): TileService() {
|
|||
/* Overrides onStartListening from TileService */
|
||||
override fun onStartListening() {
|
||||
super.onStartListening()
|
||||
// tile becomes visible - bind tracker service
|
||||
bindService(Intent(this, TrackerService::class.java), connection, Context.BIND_AUTO_CREATE)
|
||||
// tile becomes visible - register listener for changes in shared preferences
|
||||
PreferenceManager.getDefaultSharedPreferences(this@TrackingToggleTileService).registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
|
||||
}
|
||||
|
||||
|
||||
|
@ -68,21 +70,16 @@ class TrackingToggleTileService(): TileService() {
|
|||
override fun onClick() {
|
||||
super.onClick()
|
||||
when (trackingState) {
|
||||
Keys.STATE_TRACKING_ACTIVE -> {
|
||||
trackerService.stopTracking()
|
||||
}
|
||||
else -> {
|
||||
// start service via intent so that it keeps running after unbind
|
||||
startTrackerService()
|
||||
trackerService.startTracking()
|
||||
}
|
||||
Keys.STATE_TRACKING_ACTIVE -> stopTracking()
|
||||
else -> startTracking()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Start tracker service */
|
||||
private fun startTrackerService() {
|
||||
/* Start tracking */
|
||||
private fun startTracking() {
|
||||
val intent = Intent(application, TrackerService::class.java)
|
||||
intent.action = Keys.ACTION_START
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
// ... start service in foreground to prevent it being killed on Oreo
|
||||
application.startForegroundService(intent)
|
||||
|
@ -92,11 +89,19 @@ class TrackingToggleTileService(): TileService() {
|
|||
}
|
||||
|
||||
|
||||
/* Stop tracking */
|
||||
private fun stopTracking() {
|
||||
val intent = Intent(application, TrackerService::class.java)
|
||||
intent.action = Keys.ACTION_STOP
|
||||
application.startService(intent)
|
||||
}
|
||||
|
||||
|
||||
/* Overrides onStopListening from TileService */
|
||||
override fun onStopListening() {
|
||||
super.onStopListening()
|
||||
// tile no longer visible - unbind tracker service
|
||||
unbindService(connection)
|
||||
// tile no longer visible - unregister listener for changes in shared preferences
|
||||
PreferenceManager.getDefaultSharedPreferences(this@TrackingToggleTileService).unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
|
||||
}
|
||||
|
||||
|
||||
|
@ -142,32 +147,6 @@ class TrackingToggleTileService(): TileService() {
|
|||
*/
|
||||
|
||||
|
||||
/*
|
||||
* Defines callbacks for service binding, passed to bindService()
|
||||
*/
|
||||
private val connection = object : ServiceConnection {
|
||||
override fun onServiceConnected(className: ComponentName, service: IBinder) {
|
||||
// We've bound to LocalService, cast the IBinder and get LocalService instance
|
||||
val binder = service as TrackerService.LocalBinder
|
||||
trackerService = binder.service
|
||||
trackingState = trackerService.trackingState
|
||||
bound = true
|
||||
// update state of tile
|
||||
updateTile()
|
||||
// register listener for changes in shared preferences
|
||||
PreferenceManager.getDefaultSharedPreferences(this@TrackingToggleTileService).registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
|
||||
|
||||
}
|
||||
override fun onServiceDisconnected(arg0: ComponentName) {
|
||||
bound = false
|
||||
// unregister listener for changes in shared preferences
|
||||
PreferenceManager.getDefaultSharedPreferences(this@TrackingToggleTileService).unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
|
||||
}
|
||||
}
|
||||
/*
|
||||
* End of declaration
|
||||
*/
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -11,8 +11,10 @@
|
|||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/track_element_list"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="32dp"
|
||||
android:layout_height="0dp"
|
||||
android:layout_marginTop="28dp"
|
||||
android:layout_marginBottom="4dp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
|
Loading…
Reference in a new issue