Skip to content

Commit

Permalink
Add: AppUsageEventCollector
Browse files Browse the repository at this point in the history
  • Loading branch information
highjun committed Sep 26, 2024
1 parent 096faa1 commit e3134cf
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package kaist.iclab.field_tracker
import kaist.iclab.tracker.collectors.BatteryCollector
import kaist.iclab.tracker.collectors.LocationCollector
import kaist.iclab.tracker.collectors.TestCollector
import kaist.iclab.tracker.collectors.controller.CollectorControllerInterface
import kaist.iclab.tracker.controller.CollectorControllerInterface
import kaist.iclab.tracker.database.DatabaseInterface
import kaist.iclab.tracker.permission.PermissionManagerInterface
import kaist.iclab.field_tracker.ui.AbstractMainViewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.app.Application
import kaist.iclab.tracker.collectors.BatteryCollector
import kaist.iclab.tracker.collectors.LocationCollector
import kaist.iclab.tracker.collectors.TestCollector
import kaist.iclab.tracker.collectors.controller.CollectorControllerInterface
import kaist.iclab.tracker.controller.CollectorControllerInterface
import kaist.iclab.tracker.Tracker
import org.koin.android.ext.android.get
import org.koin.android.ext.koin.androidContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package kaist.iclab.field_tracker.ui
import android.util.Log
import kotlinx.coroutines.flow.MutableStateFlow

class MainViewModelFakeImpl() : AbstractMainViewModel() {
class MainViewModelFakeImpl() : AbstractMainViewModel() {
companion object{
const val TAG = "MainViewModelFakeImpl"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package kaist.iclab.field_tracker.ui

import android.util.Log
import androidx.lifecycle.viewModelScope
import kaist.iclab.tracker.collectors.controller.CollectorControllerInterface
import kaist.iclab.tracker.controller.CollectorControllerInterface
import kaist.iclab.tracker.database.DatabaseInterface
import kaist.iclab.tracker.permission.PermissionManagerInterface
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package kaist.iclab.lab_galaxywatch_tracker

import kaist.iclab.tracker.Tracker
import kaist.iclab.tracker.collectors.controller.CollectorControllerInterface
import kaist.iclab.tracker.controller.CollectorControllerInterface
import kaist.iclab.tracker.database.DatabaseInterface
import kaist.iclab.tracker.permission.PermissionManagerInterface
import kaist.iclab.lab_galaxywatch_tracker.data.WearableDataCollector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package kaist.iclab.lab_galaxywatch_tracker

import android.os.Bundle
import androidx.activity.compose.setContent
import kaist.iclab.tracker.collectors.controller.CollectorController
import kaist.iclab.tracker.controller.CollectorController
import kaist.iclab.tracker.permission.PermissionActivity
import kaist.iclab.lab_galaxywatch_tracker.data.WearableDataCollector
import kaist.iclab.lab_galaxywatch_tracker.ui.MainScreen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package kaist.iclab.lab_galaxywatch_tracker.ui

import android.util.Log
import androidx.lifecycle.viewModelScope
import kaist.iclab.tracker.collectors.controller.CollectorControllerInterface
import kaist.iclab.tracker.controller.CollectorControllerInterface
import kaist.iclab.tracker.database.DatabaseInterface
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
Expand Down
2 changes: 1 addition & 1 deletion tracker-library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<application>
<!-- android:foregroundServiceType="specialUse|camera|microphone|location|connectedDevice"-->
<service
android:name=".collectors.controller.CollectorService"
android:name=".controller.CollectorService"
android:foregroundServiceType="dataSync|specialUse|location"
android:exported="true">
</service>
Expand Down
6 changes: 3 additions & 3 deletions tracker-library/src/main/java/kaist/iclab/tracker/Tracker.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package kaist.iclab.tracker

import android.content.Context
import kaist.iclab.tracker.collectors.controller.CollectorController
import kaist.iclab.tracker.collectors.controller.CollectorControllerInterface
import kaist.iclab.tracker.collectors.controller.CollectorService
import kaist.iclab.tracker.controller.CollectorController
import kaist.iclab.tracker.controller.CollectorControllerInterface
import kaist.iclab.tracker.controller.CollectorService
import kaist.iclab.tracker.database.CouchbaseDBImpl
import kaist.iclab.tracker.database.DatabaseInterface
import kaist.iclab.tracker.permission.PermissionManagerImpl
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package kaist.iclab.tracker.collectors

import android.Manifest
import android.app.usage.UsageEvents
import android.app.usage.UsageStatsManager
import android.content.Context
import android.os.Build
import android.util.Log
import kaist.iclab.tracker.database.DatabaseInterface
import kaist.iclab.tracker.triggers.AlarmTrigger
import java.util.concurrent.TimeUnit
import kotlin.math.max


class AppUsageEventCollector(
override val context: Context,
override val database: DatabaseInterface
):AbstractCollector(context, database) {
companion object {
const val NAME = "APP_USAGE_EVENT"
const val TAG = "AppUsageEventCollector"
const val ACTION_REQUEST = "kaist.iclab.tracker.ACTION_APP_USAGE_EVENT_REQUEST"
const val ACTION_CODE_REQUEST = 0x2
}

override val NAME: String
get() = Companion.NAME

override val permissions: Array<String> = arrayOf(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) Manifest.permission.PACKAGE_USAGE_STATS else null
).filterNotNull().toTypedArray()

var trigger: AlarmTrigger? = null

var lastTimeDataWritten:Long = 0L

fun listener() {
val currTime = System.currentTimeMillis()
val prevTime = max(
currTime - TimeUnit.HOURS.toMillis(12),
lastTimeDataWritten
)
val usageStatManager =
context.getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager
val events = usageStatManager.queryEvents(prevTime, currTime)
while (events.hasNextEvent()) {
val event = UsageEvents.Event()
events.getNextEvent(event)
event.toMap().let {
Log.d(TAG, "EVENT $it")
}
}
}

fun UsageEvents.Event.toMap(): Map<String, Any> {
val data = mutableMapOf(
"timestamp" to this.timeStamp,
"package_name" to this.packageName,
"class_name" to this.className,
"event_type" to this.eventType,
// "configuration" to this.configuration, change it if there is need
)
/*TODO: getExtras parsing which is added on API Level 35*/
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
data["app_stand_by_bucket"] = this.appStandbyBucket
}
return data
}

/* Check whether the system allow to collect data
* In case of sensor malfunction or broken, it would not be available.*/
override fun isAvailable() = true


/* Start collector to collect data
* */
override fun start(){
trigger = AlarmTrigger(
context,
ACTION_REQUEST,
ACTION_CODE_REQUEST,
60000L
) {
listener()
}
trigger?.register()
}

/* Stop collector to stop collecting data
* */
override fun stop(){
trigger?.unregister()
trigger = null
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kaist.iclab.tracker.collectors.controller
package kaist.iclab.tracker.controller

import android.content.Context
import android.content.Intent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kaist.iclab.tracker.collectors.controller
package kaist.iclab.tracker.controller

import kaist.iclab.tracker.collectors.AbstractCollector
import kaist.iclab.tracker.permission.PermissionManagerInterface
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kaist.iclab.tracker.collectors.controller
package kaist.iclab.tracker.controller

import android.app.Notification
import android.app.NotificationChannel
Expand Down Expand Up @@ -40,7 +40,6 @@ class CollectorService(): Service() {
}
}


override fun onBind(intent: Intent?): IBinder? = null
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.d(TAG, "onStartCommand")
Expand All @@ -62,6 +61,14 @@ class CollectorService(): Service() {

return super.onStartCommand(intent, flags, startId)
}
override fun onDestroy() {
super.onDestroy()
Tracker.getDatabase().log("SERVICE_STOPPED")
Tracker.getCollectorController().collectors.forEach {
it.stop()
}
Tracker.getDatabase().update(CollectorController.RUNNING_COLLECTION, mapOf("running" to false, "timestamp" to System.currentTimeMillis()))
}

fun run() {
Log.d(TAG, "run")
Expand All @@ -70,14 +77,5 @@ class CollectorService(): Service() {
it.start()
}
Tracker.getDatabase().update(CollectorController.RUNNING_COLLECTION, mapOf("running" to true, "timestamp" to System.currentTimeMillis()))

}
override fun onDestroy() {
super.onDestroy()
Tracker.getDatabase().log("SERVICE_STOPPED")
Tracker.getCollectorController().collectors.forEach {
it.stop()
}
Tracker.getDatabase().update(CollectorController.RUNNING_COLLECTION, mapOf("running" to false, "timestamp" to System.currentTimeMillis()))
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package kaist.iclab.lab_galaxywatch_tracker

import kaist.iclab.tracker.Tracker
import kaist.iclab.tracker.collectors.controller.CollectorControllerInterface
import kaist.iclab.tracker.controller.CollectorControllerInterface
import kaist.iclab.tracker.database.DatabaseInterface
import kaist.iclab.tracker.permission.PermissionManagerInterface
import kaist.iclab.lab_galaxywatch_tracker.data.collector.ACCCollector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package kaist.iclab.lab_galaxywatch_tracker

import android.os.Bundle
import androidx.activity.compose.setContent
import kaist.iclab.tracker.collectors.controller.CollectorController
import kaist.iclab.tracker.controller.CollectorController
import kaist.iclab.tracker.permission.PermissionActivity
import kaist.iclab.lab_galaxywatch_tracker.data.collector.ACCCollector
import kaist.iclab.lab_galaxywatch_tracker.data.collector.HRCollector
Expand Down

0 comments on commit e3134cf

Please sign in to comment.