2020-01-27 17:22:44 +00:00
|
|
|
/*
|
|
|
|
* TrackingToggleTileService.kt
|
|
|
|
* Implements the TrackingToggleTileService service
|
|
|
|
* A TrackingToggleTileService toggles the recording state from a quick settings tile
|
|
|
|
*
|
|
|
|
* This file is part of
|
|
|
|
* TRACKBOOK - Movement Recorder for Android
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016-20 - Y20K.org
|
|
|
|
* Licensed under the MIT-License
|
|
|
|
* http://opensource.org/licenses/MIT
|
|
|
|
*
|
|
|
|
* Trackbook uses osmdroid - OpenStreetMap-Tools for Android
|
|
|
|
* https://github.com/osmdroid/osmdroid
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
package org.y20k.trackbook
|
|
|
|
|
2020-01-31 20:15:33 +00:00
|
|
|
import android.content.Intent
|
|
|
|
import android.content.SharedPreferences
|
2020-01-27 17:22:44 +00:00
|
|
|
import android.graphics.drawable.Icon
|
2020-01-29 12:37:04 +00:00
|
|
|
import android.os.Build
|
2020-01-27 17:22:44 +00:00
|
|
|
import android.service.quicksettings.Tile
|
|
|
|
import android.service.quicksettings.TileService
|
|
|
|
import androidx.preference.PreferenceManager
|
|
|
|
import org.y20k.trackbook.helpers.LogHelper
|
|
|
|
import org.y20k.trackbook.helpers.PreferencesHelper
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TrackingToggleTileService class
|
|
|
|
*/
|
2020-08-01 00:06:53 +00:00
|
|
|
class TrackingToggleTileService() : TileService() {
|
2020-01-27 17:22:44 +00:00
|
|
|
|
|
|
|
/* Define log tag */
|
|
|
|
private val TAG: String = LogHelper.makeLogTag(TrackingToggleTileService::class.java)
|
|
|
|
|
|
|
|
|
|
|
|
/* Main class variables */
|
|
|
|
private var bound: Boolean = false
|
2020-01-28 16:39:45 +00:00
|
|
|
private var trackingState: Int = Keys.STATE_TRACKING_NOT
|
2020-01-27 17:22:44 +00:00
|
|
|
private lateinit var trackerService: TrackerService
|
|
|
|
|
|
|
|
|
|
|
|
/* Overrides onTileAdded from TileService */
|
|
|
|
override fun onTileAdded() {
|
|
|
|
super.onTileAdded()
|
|
|
|
// get saved tracking state
|
|
|
|
trackingState = PreferencesHelper.loadTrackingState(this)
|
2020-01-31 20:15:33 +00:00
|
|
|
// set up tile
|
|
|
|
updateTile()
|
2020-01-27 17:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Overrides onTileRemoved from TileService */
|
|
|
|
override fun onTileRemoved() {
|
|
|
|
super.onTileRemoved()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-31 22:24:28 +00:00
|
|
|
/* Overrides onStartListening from TileService (tile becomes visible) */
|
2020-01-27 17:22:44 +00:00
|
|
|
override fun onStartListening() {
|
|
|
|
super.onStartListening()
|
2020-01-31 22:24:28 +00:00
|
|
|
// get saved tracking state
|
|
|
|
trackingState = PreferencesHelper.loadTrackingState(this)
|
|
|
|
// set up tile
|
|
|
|
updateTile()
|
|
|
|
// register listener for changes in shared preferences
|
2020-08-01 00:06:53 +00:00
|
|
|
PreferenceManager.getDefaultSharedPreferences(this@TrackingToggleTileService)
|
|
|
|
.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
|
2020-01-27 17:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Overrides onClick from TileService */
|
|
|
|
override fun onClick() {
|
|
|
|
super.onClick()
|
|
|
|
when (trackingState) {
|
2020-01-31 20:15:33 +00:00
|
|
|
Keys.STATE_TRACKING_ACTIVE -> stopTracking()
|
|
|
|
else -> startTracking()
|
2020-01-27 17:22:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-31 22:24:28 +00:00
|
|
|
/* Overrides onStopListening from TileService (tile no longer visible) */
|
2020-01-27 17:22:44 +00:00
|
|
|
override fun onStopListening() {
|
|
|
|
super.onStopListening()
|
2020-01-31 22:24:28 +00:00
|
|
|
// unregister listener for changes in shared preferences
|
2020-08-01 00:06:53 +00:00
|
|
|
PreferenceManager.getDefaultSharedPreferences(this@TrackingToggleTileService)
|
|
|
|
.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener)
|
2020-01-27 17:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Overrides onDestroy from Service */
|
|
|
|
override fun onDestroy() {
|
|
|
|
super.onDestroy()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Update quick settings tile */
|
|
|
|
private fun updateTile() {
|
|
|
|
val tile: Tile = qsTile
|
|
|
|
tile.icon = Icon.createWithResource(this, R.drawable.ic_notification_icon_small_24dp)
|
|
|
|
when (trackingState) {
|
|
|
|
Keys.STATE_TRACKING_ACTIVE -> {
|
|
|
|
tile.label = getString(R.string.quick_settings_tile_title_stop)
|
|
|
|
tile.contentDescription = getString(R.string.descr_quick_settings_tile_title_stop)
|
|
|
|
tile.state = Tile.STATE_ACTIVE
|
|
|
|
}
|
|
|
|
else -> {
|
|
|
|
tile.label = getString(R.string.quick_settings_tile_title_start)
|
|
|
|
tile.contentDescription = getString(R.string.descr_quick_settings_tile_title_start)
|
|
|
|
tile.state = Tile.STATE_INACTIVE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tile.updateTile()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-31 22:24:28 +00:00
|
|
|
/* 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)
|
|
|
|
} else {
|
|
|
|
application.startService(intent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Stop tracking */
|
|
|
|
private fun stopTracking() {
|
|
|
|
val intent = Intent(application, TrackerService::class.java)
|
|
|
|
intent.action = Keys.ACTION_STOP
|
|
|
|
application.startService(intent)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-27 17:22:44 +00:00
|
|
|
/*
|
|
|
|
* Defines the listener for changes in shared preferences
|
|
|
|
*/
|
2020-08-01 00:06:53 +00:00
|
|
|
private val sharedPreferenceChangeListener =
|
|
|
|
SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, key ->
|
|
|
|
when (key) {
|
|
|
|
Keys.PREF_TRACKING_STATE -> {
|
|
|
|
trackingState = PreferencesHelper.loadTrackingState(this)
|
|
|
|
updateTile()
|
|
|
|
}
|
2020-01-27 17:22:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* End of declaration
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2020-08-01 00:06:53 +00:00
|
|
|
}
|