Skip to content

Commit

Permalink
Rewrite TrackerService to be more like a state machine.
Browse files Browse the repository at this point in the history
  • Loading branch information
voussoir committed Apr 30, 2023
1 parent 2e5be6a commit 3710755
Show file tree
Hide file tree
Showing 11 changed files with 278 additions and 330 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The goal of this fork is to make 24/7 recording easier. I want to be able to run

trkpt has three states of power management. The states transition like this:

1. **FULL POWER**: receives location updates as fast as Android provides them. A wakelock is used to prevent doze.
1. **FULL POWER**: receives location updates as fast as Android provides them. A wakelock is used to resist doze (though I think some devices will doze anyway because they do not respect the user).

Stay near homepoint for a few minutes → Sleep

Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId 'net.voussoir.trkpt'
minSdkVersion 25
targetSdk 32
versionCode 54
versionName '1.0.3'
versionCode 55
versionName '1.1.0'
resConfigs "en", "da", "de", "fr", "hr", "id", "it", "ja", "nb-rNO", "nl", "pl", "pt-rBR", "ru", "sv", "tr", "zh-rCN"
}

Expand Down
12 changes: 0 additions & 12 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,6 @@
</intent-filter>
</service>

<!-- TRACKING TOGGLE SERVICE SYSTEM QUICK SETTINGS -->
<service
android:name="net.voussoir.trkpt.TrackingToggleTileService"
android:label="@string/quick_settings_tile_title_default"
android:icon="@drawable/ic_notification_icon_small_24dp"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE"
android:exported="true">
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>
</service>

<!-- FILE PROVIDER GPX -->
<provider
android:name="androidx.core.content.FileProvider"
Expand Down
12 changes: 7 additions & 5 deletions app/src/main/java/net/voussoir/trkpt/Keys.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ object Keys {

// args
const val ARG_TRACK_TITLE: String = "ArgTrackTitle"
const val ARG_TRACK_ID: String = "ArgTrackID"
const val ARG_TRACK_DEVICE_ID: String = "ArgTrackDeviceID"
const val ARG_TRACK_START_TIME: String = "ArgTrackStartTime"
const val ARG_TRACK_STOP_TIME: String = "ArgTrackStopTime"
Expand All @@ -67,12 +66,15 @@ object Keys {
const val PREF_MAX_ACCURACY: String = "prefMaxAccuracy"

// states
const val STATE_TRACKING_STOPPED: Int = 0
const val STATE_TRACKING_ACTIVE: Int = 1
const val STATE_STOP: Int = 0
const val STATE_FULL_RECORDING: Int = 1
const val STATE_ARRIVED_AT_HOME: Int = 2
const val STATE_SLEEP: Int = 3
const val STATE_DEAD: Int = 4
const val STATE_MAPVIEW: Int = 5
const val LOCATION_INTERVAL_FULL_POWER: Long = 0
const val LOCATION_INTERVAL_SLEEP: Long = ONE_MINUTE_IN_MILLISECONDS
const val LOCATION_INTERVAL_DEAD: Long = -1
const val LOCATION_INTERVAL_STOP: Long = -2
const val LOCATION_INTERVAL_STOP: Long = -1
const val STATE_THEME_FOLLOW_SYSTEM: String = "stateFollowSystem"
const val STATE_THEME_LIGHT_MODE: String = "stateLightMode"
const val STATE_THEME_DARK_MODE: String = "stateDarkMode"
Expand Down
49 changes: 32 additions & 17 deletions app/src/main/java/net/voussoir/trkpt/MapFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ class MapFragment : Fragment()
{
return@setOnClickListener
}
if (tracker.trackingState == Keys.STATE_TRACKING_ACTIVE)
if (tracker.tracking_state != Keys.STATE_STOP && tracker.tracking_state != Keys.STATE_MAPVIEW)
{
tracker.stopTracking()
tracker.state_mapview()
}
else
{
Expand Down Expand Up @@ -276,10 +276,10 @@ class MapFragment : Fragment()
return
}
saveBestLocationState(tracker.currentBestLocation)
if (bound && tracker.trackingState != Keys.STATE_TRACKING_ACTIVE)
if (bound && (tracker.tracking_state == Keys.STATE_MAPVIEW || tracker.tracking_state == Keys.STATE_STOP))
{
tracker.removeGpsLocationListener()
tracker.removeNetworkLocationListener()
tracker.remove_gps_location_listener()
tracker.remove_network_location_listener()
tracker.trackbook.database.commit()
}
handler.removeCallbacks(redraw_runnable)
Expand Down Expand Up @@ -350,7 +350,7 @@ class MapFragment : Fragment()
}
if (trackerService != null)
{
trackerService!!.startTracking()
trackerService!!.state_full_recording()
}
}

Expand Down Expand Up @@ -413,19 +413,13 @@ class MapFragment : Fragment()
val newMarker: Drawable
val fillcolor: Int
val description: String
if (tracker.listeners_enabled_at == 0L)
{
fillcolor = Color.argb(64, 0, 0, 0)
newMarker = ContextCompat.getDrawable(requireContext(), R.drawable.ic_skull_24dp)!!
description = "No location listeners are enabled"
}
else if (tracker.trackingState == Keys.STATE_TRACKING_ACTIVE && tracker.location_interval == Keys.LOCATION_INTERVAL_DEAD)
if (tracker.tracking_state == Keys.STATE_DEAD)
{
fillcolor = Color.argb(64, 0, 0, 0)
newMarker = ContextCompat.getDrawable(requireContext(), R.drawable.ic_skull_24dp)!!
description = "GPS is struggling; disabled until movement"
}
else if (tracker.trackingState == Keys.STATE_TRACKING_ACTIVE && tracker.location_interval == Keys.LOCATION_INTERVAL_SLEEP)
else if (tracker.tracking_state == Keys.STATE_SLEEP)
{
fillcolor = Color.argb(64, 220, 61, 51)
newMarker = ContextCompat.getDrawable(requireContext(), R.drawable.ic_sleep_24dp)!!
Expand All @@ -437,7 +431,7 @@ class MapFragment : Fragment()
newMarker = ContextCompat.getDrawable(requireContext(), R.drawable.ic_marker_location_black_24dp)!!
description = "GPS tracking at full power"
}
else if (tracker.trackingState == Keys.STATE_TRACKING_ACTIVE)
else if (tracker.tracking_state == Keys.STATE_FULL_RECORDING || tracker.tracking_state == Keys.STATE_ARRIVED_AT_HOME)
{
fillcolor = Color.argb(64, 220, 61, 51)
newMarker = ContextCompat.getDrawable(requireContext(), R.drawable.ic_marker_location_red_24dp)!!
Expand Down Expand Up @@ -591,13 +585,13 @@ class MapFragment : Fragment()
mainButton.text = requireContext().getString(R.string.button_not_ready)
mainButton.icon = null
}
else if (tracker == null || tracker.trackingState == Keys.STATE_TRACKING_STOPPED)
else if (tracker == null || tracker.tracking_state == Keys.STATE_STOP || tracker.tracking_state == Keys.STATE_MAPVIEW)
{
mainButton.setIconResource(R.drawable.ic_fiber_manual_record_inactive_24dp)
mainButton.text = requireContext().getString(R.string.button_start)
mainButton.contentDescription = requireContext().getString(R.string.descr_button_start)
}
else if (tracker.trackingState == Keys.STATE_TRACKING_ACTIVE)
else
{
mainButton.setIconResource(R.drawable.ic_fiber_manual_stop_24dp)
mainButton.text = requireContext().getString(R.string.button_pause)
Expand Down Expand Up @@ -672,11 +666,13 @@ class MapFragment : Fragment()
if (show_debug)
{
map_current_time.text = """
state: ${state_name()}
now: ${iso8601_local_noms(System.currentTimeMillis())}
location: ${iso8601_local_noms(tracker.currentBestLocation.time)}
listeners: ${iso8601_local_noms(tracker.listeners_enabled_at)}
motion: ${iso8601_local_noms(tracker.last_significant_motion)}
watchdog: ${iso8601_local_noms(tracker.last_watchdog)}
home: ${iso8601_local_noms(tracker.arrived_at_home)}
died: ${iso8601_local_noms(tracker.gave_up_at)}
power: ${tracker.device_is_charging}
wakelock: ${tracker.wakelock.isHeld}
Expand All @@ -690,6 +686,25 @@ class MapFragment : Fragment()
mapView.invalidate()
}

fun state_name(): String
{
val tracker = trackerService
if (tracker == null)
{
return "null"
}
return when (tracker.tracking_state)
{
Keys.STATE_STOP -> "stop"
Keys.STATE_FULL_RECORDING -> "recording"
Keys.STATE_ARRIVED_AT_HOME -> "home"
Keys.STATE_SLEEP -> "sleep"
Keys.STATE_DEAD -> "dead"
Keys.STATE_MAPVIEW -> "mapview"
else -> tracker.tracking_state.toString()
}
}

val redraw_runnable: Runnable = object : Runnable
{
override fun run()
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/net/voussoir/trkpt/TrackFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ class TrackFragment : Fragment(), MapListener, YesNoDialog.YesNoDialogListener
mapView.zoomController.setVisibility(org.osmdroid.views.CustomZoomButtonsController.Visibility.NEVER)
if (track.trkpts.size > 0)
{
val first = track.trkpts.first()
controller.setCenter(GeoPoint(first.latitude, first.longitude))
val last = track.trkpts.last()
controller.setCenter(GeoPoint(last.latitude, last.longitude))
}
controller.setZoom(Keys.DEFAULT_ZOOM_LEVEL)

Expand Down
Loading

0 comments on commit 3710755

Please sign in to comment.