Skip to content

Commit

Permalink
Merge pull request #42 from ActivityWatch/dev/getLastEvent-limit
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare authored Jun 22, 2020
2 parents 81e838b + 003a818 commit 5c67f5d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class RustInterface constructor(context: Context? = null) {
private external fun setDataDir(path: String)
external fun getBuckets(): String
external fun createBucket(bucket: String): String
external fun getEvents(bucket_id: String): String
external fun getEvents(bucket_id: String, limit: Int): String
external fun heartbeat(bucket_id: String, event: String, pulsetime: Double): String

fun sayHello(to: String): String {
Expand Down Expand Up @@ -93,8 +93,7 @@ class RustInterface constructor(context: Context? = null) {

fun getEventsJSON(bucket_id: String, limit: Int = 0): JSONArray {
// TODO: Handle errors
// FIXME: Use limit (will have major performance benefits)
val result = getEvents(bucket_id)
val result = getEvents(bucket_id, limit)
return try {
JSONArray(result)
} catch(e: JSONException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ class UsageStatsWatcher constructor(val context: Context) {
}

private fun getLastEvent(): JSONObject? {
// FIXME: For some reason doesn't return last event, always 2h behind (so probably a timezone issue)
val events = ri.getEventsJSON(bucket_id, limit=1)
return if (events.length() > 0) {
//Log.d(TAG, events[0].toString())
return if (events.length() == 1) {
//Log.d(TAG, "Last event: ${events[0]}")
events[0] as JSONObject
} else {
Log.w(TAG, "More or less than one event was retrieved when trying to get last event, actual length: ${events.length()}")
null
}
}
Expand Down Expand Up @@ -155,7 +155,7 @@ class UsageStatsWatcher constructor(val context: Context) {
nextEvent@ while(usageEvents.hasNextEvent()) {
val event = UsageEvents.Event()
usageEvents.getNextEvent(event)
if(event.eventType !in arrayListOf(UsageEvents.Event.MOVE_TO_FOREGROUND, UsageEvents.Event.MOVE_TO_BACKGROUND, UsageEvents.Event.SCREEN_INTERACTIVE, UsageEvents.Event.SCREEN_NON_INTERACTIVE)) {
if(event.eventType !in arrayListOf(UsageEvents.Event.ACTIVITY_RESUMED, UsageEvents.Event.ACTIVITY_PAUSED, UsageEvents.Event.SCREEN_INTERACTIVE, UsageEvents.Event.SCREEN_NON_INTERACTIVE)) {
// Not sure which events are triggered here, so we use a (probably safe) fallback
//Log.d(TAG, "Rare eventType: ${event.eventType}, skipping")
continue@nextEvent
Expand All @@ -164,12 +164,12 @@ class UsageStatsWatcher constructor(val context: Context) {
val awEvent = Event.fromUsageEvent(event, context, includeClassname = true)
val pulsetime: Double
when(event.eventType) {
UsageEvents.Event.MOVE_TO_FOREGROUND, UsageEvents.Event.SCREEN_INTERACTIVE -> {
UsageEvents.Event.ACTIVITY_RESUMED, UsageEvents.Event.SCREEN_INTERACTIVE -> {
// MOVE_TO_FOREGROUND: New Activity was opened
// SCREEN_INTERACTIVE: Screen just became interactive, user was previously therefore not active on the device
pulsetime = 1.0
}
UsageEvents.Event.MOVE_TO_BACKGROUND, UsageEvents.Event.SCREEN_NON_INTERACTIVE -> {
UsageEvents.Event.ACTIVITY_PAUSED, UsageEvents.Event.SCREEN_NON_INTERACTIVE -> {
// MOVE_TO_BACKGROUND: Activity was moved to background
// SCREEN_NOT_INTERACTIVE: Screen locked/turned off, user is therefore now AFK, and this is the last event
pulsetime = 24 * 60 * 60.0 // 24h, we will assume events should never grow longer than that
Expand Down

0 comments on commit 5c67f5d

Please sign in to comment.