Skip to content

Commit

Permalink
ADDED
Browse files Browse the repository at this point in the history
  • Loading branch information
highjun committed Oct 31, 2024
1 parent 9a0e5fb commit eacaeb1
Show file tree
Hide file tree
Showing 27 changed files with 640 additions and 619 deletions.
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ plugins {
alias(libs.plugins.androidApplication) apply false
alias(libs.plugins.jetbrainsKotlinAndroid) apply false
alias(libs.plugins.androidLibrary) apply false
// alias(libs.plugins.realm) apply false
}
2 changes: 2 additions & 0 deletions field-smartphone/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,7 @@ dependencies {
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material.icons.extended)

implementation(libs.couchbase)

implementation(project(":tracker-library"))
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,63 @@
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.controller.CollectorControllerInterface
import kaist.iclab.tracker.database.DatabaseInterface
import kaist.iclab.tracker.permission.PermissionManagerInterface
import kaist.iclab.field_tracker.ui.AbstractMainViewModel
import kaist.iclab.field_tracker.ui.MainViewModelImpl
import kaist.iclab.tracker.Tracker
import org.koin.core.module.dsl.singleOf
import kaist.iclab.tracker.collectors.*
import kaist.iclab.tracker.controller.CollectorControllerInterface
import kaist.iclab.tracker.database.DatabaseInterface
import kaist.iclab.tracker.database.TempDBImpl
import kaist.iclab.tracker.permission.PermissionManagerInterface
import org.koin.android.ext.koin.androidContext
import org.koin.core.module.dsl.viewModel
import org.koin.core.module.dsl.viewModelOf
import org.koin.core.module.dsl.singleOf
import org.koin.dsl.module


val appModule = module {
single<DatabaseInterface> {
Tracker.getDatabase()
}
single<CollectorControllerInterface> {
Tracker.getCollectorController()
}
single<PermissionManagerInterface> {
Tracker.getPermissionManager()
}
// singleOf(::TestCollector)
// singleOf(::BatteryCollector)
single<DatabaseInterface> {
TempDBImpl(androidContext())
}

singleOf(::ActivityTransitionCollector)
singleOf(::AmbientLightCollector)
singleOf(::AppUsageLogCollector)
singleOf(::BatteryCollector)
singleOf(::CallLogCollector)
singleOf(::DataTrafficStatCollector)
singleOf(::LocationCollector)
singleOf(::MessageLogCollector)
singleOf(::NotificationCollector)
singleOf(::ScreenCollector)
singleOf(::UserInteractionCollector)
singleOf(::WiFiScanCollector)

viewModelOf(::MainViewModelImpl)
viewModel<AbstractMainViewModel>{
get<MainViewModelImpl>()
viewModel<AbstractMainViewModel> {
val collectors = mapOf(
"ActivityTransitionCollector" to get<ActivityTransitionCollector>(),
"AmbientLightCollector" to get<AmbientLightCollector>(),
"AppUsageLogCollector" to get<AppUsageLogCollector>(),
"BatteryCollector" to get<BatteryCollector>(),
"CallLogCollector" to get<CallLogCollector>(),
"DataTrafficStatCollector" to get<DataTrafficStatCollector>(),
"LocationCollector" to get<LocationCollector>(),
"MessageLogCollector" to get<MessageLogCollector>(),
"NotificationCollector" to get<NotificationCollector>(),
"ScreenCollector" to get<ScreenCollector>(),
"UserInteractionCollector" to get<UserInteractionCollector>(),
"WiFiScanCollector" to get<WiFiScanCollector>()
)
MainViewModelImpl(
get(),
get(),
get(),
collectors
)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package kaist.iclab.field_tracker

import android.Manifest
import android.os.Build
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
Expand All @@ -9,6 +11,7 @@ import androidx.compose.ui.Modifier
import kaist.iclab.tracker.permission.PermissionActivity
import kaist.iclab.field_tracker.ui.MainScreen
import kaist.iclab.field_tracker.ui.theme.TrackerTheme
import kaist.iclab.tracker.Tracker
import org.koin.androidx.compose.KoinAndroidContext

class MainActivity : PermissionActivity() {
Expand All @@ -27,5 +30,10 @@ class MainActivity : PermissionActivity() {
}
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
Tracker.getPermissionManager().request(
arrayOf(Manifest.permission.POST_NOTIFICATIONS)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package kaist.iclab.field_tracker

import android.app.Application
import android.util.Log
import kaist.iclab.tracker.Tracker
import kaist.iclab.tracker.collectors.BatteryCollector
import kaist.iclab.tracker.collectors.LocationCollector
import kaist.iclab.tracker.collectors.TestCollector
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
import org.koin.android.ext.koin.androidLogger
Expand All @@ -27,14 +25,18 @@ class MainApplication: Application(){

fun setupCollector() {
val collectorController = get<CollectorControllerInterface>()
get<BatteryCollector>().listener = { data ->
Log.d("BatteryCollector", "Battery data: $data")
// Do something with battery data
}
// collectorController.addCollector(get<TestCollector>())
// collectorController.addCollector(get<BatteryCollector>())
val locationCollector = get<LocationCollector>()
locationCollector.listener = { data ->
Log.d("LocationCollector", "Location data: $data")
}
collectorController.addCollector(locationCollector)
collectorController.enable(locationCollector.NAME, get())
// val locationCollector = get<LocationCollector>()
// locationCollector.listener = { data ->
// Log.d("LocationCollector", "Location data: $data")
// }
// collectorController.addCollector(locationCollector)
// collectorController.enable(locationCollector.NAME, get())
// val filter: Filter = { data ->
// data + ("custom" to "data")
// }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//package kaist.iclab.tracker.database
//
//import android.content.Context
//import android.util.Log
//import com.couchbase.lite.CouchbaseLite
//import com.couchbase.lite.DataSource
//import com.couchbase.lite.Database
//import com.couchbase.lite.Expression
//import com.couchbase.lite.MutableDocument
//import com.couchbase.lite.Ordering
//import com.couchbase.lite.Query
//import com.couchbase.lite.QueryBuilder
//import com.couchbase.lite.SelectResult
//import com.couchbase.lite.collectionChangeFlow
//import kotlinx.coroutines.flow.Flow
//import kotlinx.coroutines.flow.map
//import kotlinx.coroutines.flow.onStart
//
//class CouchbaseDBImpl(
// context: Context,
//) : DatabaseInterface {
//
// companion object {
// const val TAG = "CouchbaseDBImpl"
// const val LOG_COLLECTION = "LOG"
// const val DB = "tracker"
// }
//
// private val database: Database by lazy {
// Database(DB)
// }
//
// init {
// // Initialize Couchbase Lite
// CouchbaseLite.init(context)
// }
//
// override fun insert(collectionName: String, data: Map<String, Any>): String {
// val document = MutableDocument(data)
// val collection = getCollection(collectionName)
// collection.save(document)
// return document.id
// }
//
// override fun update(collectionName: String, data: Map<String, Any>) {
// val collection = getCollection(collectionName)
// val id = collection.indexes.firstOrNull()
// val document = MutableDocument(id, data)
// collection.save(document)
// Log.d(TAG, "$collectionName Updated: ${document.toMap()}")
// }
//
// override fun sync() {
// TODO("Not yet implemented")
// }
//
// override fun deleteAll() {
// database.delete()
// }
//
// override fun getAllDocs(collectionName: String): List<Map<String, Any>> {
// val collection = getCollection(collectionName)
// val query: Query = QueryBuilder.select(SelectResult.all())
// .from(DataSource.collection(collection))
// return query.execute().allResults().map { it.toMap() }
// }
//
// override fun getDocsFlow(collectionName: String): Flow<List<Map<String, Any>>> {
// val collection = getCollection(collectionName)
// return collection.collectionChangeFlow().map {
// getAllDocs(collectionName)
// }.onStart {
// emit(getAllDocs(collectionName))
// }
// }
//
// override fun getLastDoc(collectionName: String): Map<String, Any> {
// val collection = getCollection(collectionName)
// val query: Query = QueryBuilder.select(SelectResult.all())
// .from(DataSource.collection(collection))
// .orderBy(Ordering.property("timestamp").descending())
// .limit(Expression.intValue(1))
// return query.execute().firstOrNull()?.getDictionary(0)?.toMap() ?: mapOf()
// }
//
// override fun getLastDocFlow(collectionName: String): Flow<Map<String, Any>> {
// val collection = getCollection(collectionName)
// return collection.collectionChangeFlow().map {
// getLastDoc(collectionName)
// }.onStart {
// emit(getLastDoc(collectionName))
// }
// }
//
// override fun log(message: String) {
// insert(
// LOG_COLLECTION,
// mapOf("timestamp" to System.currentTimeMillis(), "message" to message)
// )
// }
//
// private fun getCollection(collectionName: String): com.couchbase.lite.Collection {
// val collection = database.getCollection(collectionName)
// ?: database.createCollection(collectionName)
// return collection
// }
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package kaist.iclab.tracker.database

import kotlinx.coroutines.flow.Flow

interface DatabaseInterface {
// fun insert(collectionName: String, data: Map<String, Any>): String
// fun update(collectionName: String, data: Map<String, Any>)
//
// fun sync()
// fun deleteAll()
//
// fun getDocsFlow(collectionName: String): Flow<List<Map<String, Any>>>
// fun getAllDocs(collectionName: String): List<Map<String,Any>>
//
// fun getLastDocFlow(collectionName: String): Flow<Map<String, Any>>
// fun getLastDoc(collectionName: String): Map<String, Any>
//
// /* Function to Log some messages*/
// fun log(message: String)

/* Enable/Disable Collector */
fun updateConfig(name: String, value: Boolean)
fun getConfigFlow(): Flow<Map<String,Boolean>>
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//package dev.iclab.tracker.database
//package kaist.iclab.tracker.database
//
//import android.util.Log
//import dev.iclab.tracker.Util
//import dev.iclab.tracker.collectors.AbstractCollector
//import dev.iclab.tracker.collectors.TestCollector
//
//class FakeDBImpl : DatabaseInterface {
// companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package kaist.iclab.tracker.database

import android.content.Context
import com.couchbase.lite.CouchbaseLite
import com.couchbase.lite.Database
import com.couchbase.lite.MutableDocument
import com.couchbase.lite.collectionChangeFlow
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart

class TempDBImpl(context: Context): DatabaseInterface {
companion object {
const val DB = "CONFIG"
}
private val database: Database by lazy {
Database(DB)
}

init {
CouchbaseLite.init(context)
}

override fun updateConfig(name: String, value: Boolean) {
val collection = getCollection()
val document = collection.getDocument(DB)?.toMutable() ?: MutableDocument(DB)
document.setBoolean(name, value)
collection.save(document)
}

override fun getConfigFlow(): Flow<Map<String, Boolean>> {
val collection = getCollection()
return collection.collectionChangeFlow().map {
val document = collection.getDocument(DB)
document?.toMap()?.filterValues { it is Boolean }?.mapValues { it.value as Boolean } ?: emptyMap()
}.onStart {
val document = collection.getDocument(DB)
emit(document?.toMap()?.filterValues { it is Boolean }?.mapValues { it.value as Boolean } ?: emptyMap())
}
}

private fun getCollection(): com.couchbase.lite.Collection {
val collection = database.getCollection(DB)
?: database.createCollection(DB)
return collection
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kaist.iclab.field_tracker.ui

import androidx.lifecycle.ViewModel
import kaist.iclab.tracker.collectors.AbstractCollector
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
Expand All @@ -10,10 +11,10 @@ abstract class AbstractMainViewModel: ViewModel() {
val isRunningState: StateFlow<Boolean>
get() = _isRunningState.asStateFlow()

abstract val collectorList: List<String>
abstract val _collectorConfigState: MutableStateFlow<Map<String, Boolean>>
val collectorConfigState: StateFlow<Map<String, Boolean>>
get() = _collectorConfigState.asStateFlow()
abstract val collectorMap: Map<String, AbstractCollector>
abstract val _enabledCollectors: MutableStateFlow<Map<String, Boolean>>
val enabledCollectors: StateFlow<Map<String, Boolean>>
get() = _enabledCollectors.asStateFlow()

abstract fun start()
abstract fun stop()
Expand Down
Loading

0 comments on commit eacaeb1

Please sign in to comment.