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 currentBestLocation: Location
|
||||||
private lateinit var layout: MapFragmentLayoutHolder
|
private lateinit var layout: MapFragmentLayoutHolder
|
||||||
private lateinit var trackerService: TrackerService
|
private lateinit var trackerService: TrackerService
|
||||||
private lateinit var sharedPreferenceChangeListener2:SharedPreferences.OnSharedPreferenceChangeListener
|
|
||||||
|
|
||||||
|
|
||||||
/* Overrides onCreate from Fragment */
|
/* Overrides onCreate from Fragment */
|
||||||
|
@ -240,8 +239,10 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener {
|
||||||
private val sharedPreferenceChangeListener = SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, key ->
|
private val sharedPreferenceChangeListener = SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, key ->
|
||||||
when (key) {
|
when (key) {
|
||||||
Keys.PREF_TRACKING_STATE -> {
|
Keys.PREF_TRACKING_STATE -> {
|
||||||
trackingState = PreferencesHelper.loadTrackingState(activity as Context)
|
if (activity != null) {
|
||||||
layout.updateRecordingButton(trackingState)
|
trackingState = PreferencesHelper.loadTrackingState(activity as Context)
|
||||||
|
layout.updateRecordingButton(trackingState)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -250,21 +251,6 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
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()
|
* Defines callbacks for service binding, passed to bindService()
|
||||||
*/
|
*/
|
||||||
|
@ -275,8 +261,7 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener {
|
||||||
trackerService = binder.service
|
trackerService = binder.service
|
||||||
bound = true
|
bound = true
|
||||||
// register listener for changes in shared preferences
|
// register listener for changes in shared preferences
|
||||||
sharedPreferenceChangeListener2 = createSharedPreferenceChangeListener()
|
PreferenceManager.getDefaultSharedPreferences(activity as Context).registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
|
||||||
PreferenceManager.getDefaultSharedPreferences(activity as Context).registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener2)
|
|
||||||
// start listening for location updates
|
// start listening for location updates
|
||||||
handler.removeCallbacks(periodicLocationRequestRunnable)
|
handler.removeCallbacks(periodicLocationRequestRunnable)
|
||||||
handler.postDelayed(periodicLocationRequestRunnable, 0)
|
handler.postDelayed(periodicLocationRequestRunnable, 0)
|
||||||
|
@ -284,7 +269,7 @@ class MapFragment : Fragment(), YesNoDialog.YesNoDialogListener {
|
||||||
override fun onServiceDisconnected(arg0: ComponentName) {
|
override fun onServiceDisconnected(arg0: ComponentName) {
|
||||||
bound = false
|
bound = false
|
||||||
// unregister listener for changes in shared preferences
|
// 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
|
// stop receiving location updates
|
||||||
handler.removeCallbacks(periodicLocationRequestRunnable)
|
handler.removeCallbacks(periodicLocationRequestRunnable)
|
||||||
}
|
}
|
||||||
|
|
|
@ -186,6 +186,7 @@ class TrackerService(): Service(), CoroutineScope, SensorEventListener {
|
||||||
/* Start tracking location */
|
/* Start tracking location */
|
||||||
fun startTracking(newTrack: Boolean = true) {
|
fun startTracking(newTrack: Boolean = true) {
|
||||||
if (newTrack) {
|
if (newTrack) {
|
||||||
|
track = Track()
|
||||||
track.recordingStart = GregorianCalendar.getInstance().time
|
track.recordingStart = GregorianCalendar.getInstance().time
|
||||||
track.recordingStop = track.recordingStart
|
track.recordingStop = track.recordingStart
|
||||||
track.name = DateTimeHelper.convertToReadableDate(track.recordingStart)
|
track.name = DateTimeHelper.convertToReadableDate(track.recordingStart)
|
||||||
|
|
|
@ -17,10 +17,10 @@
|
||||||
|
|
||||||
package org.y20k.trackbook
|
package org.y20k.trackbook
|
||||||
|
|
||||||
import android.content.*
|
import android.content.Intent
|
||||||
|
import android.content.SharedPreferences
|
||||||
import android.graphics.drawable.Icon
|
import android.graphics.drawable.Icon
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.IBinder
|
|
||||||
import android.service.quicksettings.Tile
|
import android.service.quicksettings.Tile
|
||||||
import android.service.quicksettings.TileService
|
import android.service.quicksettings.TileService
|
||||||
import androidx.preference.PreferenceManager
|
import androidx.preference.PreferenceManager
|
||||||
|
@ -48,6 +48,8 @@ class TrackingToggleTileService(): TileService() {
|
||||||
super.onTileAdded()
|
super.onTileAdded()
|
||||||
// get saved tracking state
|
// get saved tracking state
|
||||||
trackingState = PreferencesHelper.loadTrackingState(this)
|
trackingState = PreferencesHelper.loadTrackingState(this)
|
||||||
|
// set up tile
|
||||||
|
updateTile()
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Overrides onTileRemoved from TileService */
|
/* Overrides onTileRemoved from TileService */
|
||||||
|
@ -59,8 +61,8 @@ class TrackingToggleTileService(): TileService() {
|
||||||
/* Overrides onStartListening from TileService */
|
/* Overrides onStartListening from TileService */
|
||||||
override fun onStartListening() {
|
override fun onStartListening() {
|
||||||
super.onStartListening()
|
super.onStartListening()
|
||||||
// tile becomes visible - bind tracker service
|
// tile becomes visible - register listener for changes in shared preferences
|
||||||
bindService(Intent(this, TrackerService::class.java), connection, Context.BIND_AUTO_CREATE)
|
PreferenceManager.getDefaultSharedPreferences(this@TrackingToggleTileService).registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -68,21 +70,16 @@ class TrackingToggleTileService(): TileService() {
|
||||||
override fun onClick() {
|
override fun onClick() {
|
||||||
super.onClick()
|
super.onClick()
|
||||||
when (trackingState) {
|
when (trackingState) {
|
||||||
Keys.STATE_TRACKING_ACTIVE -> {
|
Keys.STATE_TRACKING_ACTIVE -> stopTracking()
|
||||||
trackerService.stopTracking()
|
else -> startTracking()
|
||||||
}
|
|
||||||
else -> {
|
|
||||||
// start service via intent so that it keeps running after unbind
|
|
||||||
startTrackerService()
|
|
||||||
trackerService.startTracking()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Start tracker service */
|
/* Start tracking */
|
||||||
private fun startTrackerService() {
|
private fun startTracking() {
|
||||||
val intent = Intent(application, TrackerService::class.java)
|
val intent = Intent(application, TrackerService::class.java)
|
||||||
|
intent.action = Keys.ACTION_START
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
// ... start service in foreground to prevent it being killed on Oreo
|
// ... start service in foreground to prevent it being killed on Oreo
|
||||||
application.startForegroundService(intent)
|
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 */
|
/* Overrides onStopListening from TileService */
|
||||||
override fun onStopListening() {
|
override fun onStopListening() {
|
||||||
super.onStopListening()
|
super.onStopListening()
|
||||||
// tile no longer visible - unbind tracker service
|
// tile no longer visible - unregister listener for changes in shared preferences
|
||||||
unbindService(connection)
|
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
|
<androidx.recyclerview.widget.RecyclerView
|
||||||
android:id="@+id/track_element_list"
|
android:id="@+id/track_element_list"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="0dp"
|
||||||
android:layout_marginTop="32dp"
|
android:layout_marginTop="28dp"
|
||||||
|
android:layout_marginBottom="4dp"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toTopOf="parent" />
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
Loading…
Reference in a new issue