Skip to content

Commit

Permalink
Fixed WeatherMapApi client to ktor and minor updates to other plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
this-Aditya committed Nov 5, 2024
1 parent 9dd931d commit 5e7188f
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import java.util.*
import java.util.concurrent.TimeUnit
import java.util.regex.Pattern
import android.os.Bundle
import org.radarbase.android.util.CoroutineTaskExecutor

class PhoneLogManager(context: PhoneLogService) : AbstractSourceManager<PhoneLogService, BaseSourceState>(context) {
private val callTopic: DataCache<ObservationKey, PhoneCall> = createCache(
Expand All @@ -60,6 +61,8 @@ class PhoneLogManager(context: PhoneLogService) : AbstractSourceManager<PhoneLog
private var lastSmsTimestamp: Long = 0
private var lastCallTimestamp: Long = 0

private val logsExecutor: CoroutineTaskExecutor = CoroutineTaskExecutor(this::class.simpleName!!)

init {
name = service.getString(R.string.phoneLogServiceDisplayName)
logProcessor = OfflineProcessor(context) {
Expand Down Expand Up @@ -91,6 +94,7 @@ class PhoneLogManager(context: PhoneLogService) : AbstractSourceManager<PhoneLog
.apply()
}
}
logsExecutor.start()

status = SourceStatusListener.Status.CONNECTED
}
Expand All @@ -101,7 +105,7 @@ class PhoneLogManager(context: PhoneLogService) : AbstractSourceManager<PhoneLog
logger.info("Call and SMS log: listener activated and set to a period of {} {}", period, unit)
}

private fun processSmsLog() {
private suspend fun processSmsLog() {
val newSmsTimestamp = processDb(Telephony.Sms.CONTENT_URI, SMS_COLUMNS, Telephony.Sms.DATE, lastSmsTimestamp) {
val date = getLong(getColumnIndexOrThrow(Telephony.Sms.DATE))

Expand All @@ -126,7 +130,7 @@ class PhoneLogManager(context: PhoneLogService) : AbstractSourceManager<PhoneLog
}
}

private fun processCallLog() {
private suspend fun processCallLog() {
val newLastCallTimestamp = processDb(CallLog.Calls.CONTENT_URI, CALL_COLUMNS, CallLog.Calls.DATE, lastCallTimestamp) {
val date = getLong(getColumnIndexOrThrow(CallLog.Calls.DATE))

Expand Down Expand Up @@ -203,7 +207,7 @@ class PhoneLogManager(context: PhoneLogService) : AbstractSourceManager<PhoneLog
return lastTimestamp
}

private fun processNumberUnreadSms() {
private suspend fun processNumberUnreadSms() {
val where = Telephony.Sms.READ + " = 0"
try {
db.query(Telephony.Sms.CONTENT_URI, ID_COLUMNS, where, null, null)?.use { c ->
Expand All @@ -225,16 +229,20 @@ class PhoneLogManager(context: PhoneLogService) : AbstractSourceManager<PhoneLog
CallLog.Calls.MISSED_TYPE -> PhoneCallType.MISSED
else -> PhoneCallType.UNKNOWN
}

send(callTopic, PhoneCall(
eventTimestamp,
currentTime,
duration,
targetKey,
type,
targetIsContact,
phoneNumber == null,
target.length))
logsExecutor.execute {
send(
callTopic, PhoneCall(
eventTimestamp,
currentTime,
duration,
targetKey,
type,
targetIsContact,
phoneNumber == null,
target.length
)
)
}
}

private fun sendPhoneSms(eventTimestamp: Double, target: String, typeCode: Int, message: String, targetIsContact: Boolean) {
Expand All @@ -255,21 +263,27 @@ class PhoneLogManager(context: PhoneLogService) : AbstractSourceManager<PhoneLog

// Only incoming messages are associated with a contact. For outgoing we don't know
val sendFromContact: Boolean? = if (type == PhoneSmsType.INCOMING) targetIsContact else null

send(smsTopic, PhoneSms(
eventTimestamp,
currentTime,
targetKey,
type,
length,
sendFromContact,
phoneNumber == null,
target.length))
logsExecutor.execute {
send(
smsTopic, PhoneSms(
eventTimestamp,
currentTime,
targetKey,
type,
length,
sendFromContact,
phoneNumber == null,
target.length
)
)
}
}

private fun sendNumberUnreadSms(numberUnread: Int) {
val time = currentTime
send(smsUnreadTopic, PhoneSmsUnread(time, time, numberUnread))
logsExecutor.execute {
send(smsUnreadTopic, PhoneSmsUnread(time, time, numberUnread))
}
}

/**
Expand Down Expand Up @@ -307,7 +321,9 @@ class PhoneLogManager(context: PhoneLogService) : AbstractSourceManager<PhoneLog
}

override fun onClose() {
logProcessor.stop()
logsExecutor.stop {
logProcessor.stop()
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import android.app.usage.UsageEvents.Event.*
import android.app.usage.UsageStatsManager
import android.content.*
import android.os.Build
import kotlinx.coroutines.SupervisorJob
import org.radarbase.android.data.DataCache
import org.radarbase.android.source.AbstractSourceManager
import org.radarbase.android.source.BaseSourceState
import org.radarbase.android.source.SourceStatusListener
import org.radarbase.android.util.CoroutineTaskExecutor
import org.radarbase.android.util.OfflineProcessor
import org.radarcns.kafka.ObservationKey
import org.radarcns.passive.phone.PhoneInteractionState
Expand Down Expand Up @@ -53,6 +55,8 @@ class PhoneUsageManager(context: PhoneUsageService) : AbstractSourceManager<Phon
private var lastEventType: Int = 0
private var lastEventIsSent: Boolean = false

private val usagesTaskExecutor = CoroutineTaskExecutor(this::class.simpleName!!)

init {
name = service.getString(R.string.phoneUsageServiceDisplayName)
this.usageStatsManager = context.getSystemService(Context.USAGE_STATS_SERVICE) as? UsageStatsManager
Expand Down Expand Up @@ -101,7 +105,7 @@ class PhoneUsageManager(context: PhoneUsageService) : AbstractSourceManager<Phon
})

phoneUsageProcessor.start()

usagesTaskExecutor.start(SupervisorJob())
status = SourceStatusListener.Status.CONNECTED
}

Expand All @@ -115,8 +119,9 @@ class PhoneUsageManager(context: PhoneUsageService) : AbstractSourceManager<Phon
}

val time = currentTime
send(userInteractionTopic, PhoneUserInteraction(time, time, state))

usagesTaskExecutor.execute {
send(userInteractionTopic, PhoneUserInteraction(time, time, state))
}
// Save the last user interaction state. Value shutdown is used to register boot.
preferences.edit()
.putString(LAST_USER_INTERACTION, action)
Expand All @@ -133,7 +138,7 @@ class PhoneUsageManager(context: PhoneUsageService) : AbstractSourceManager<Phon
logger.info("Usage event alarm activated and set to a period of {} seconds", interval)
}

private fun processUsageEvents() {
private suspend fun processUsageEvents() {
usageStatsManager ?: return

// Get events from previous event to now or from a fixed history
Expand Down Expand Up @@ -182,7 +187,9 @@ class PhoneUsageManager(context: PhoneUsageService) : AbstractSourceManager<Phon

val time = lastTimestamp / 1000.0
val value = PhoneUsageEvent(time, currentTime, lastPackageName, null, null, usageEventType)
send(usageEventTopic, value)
usagesTaskExecutor.execute {
send(usageEventTopic, value)
}

if (logger.isDebugEnabled) {
logger.debug("Event: [{}] {}\n\t{}", lastEventType, lastPackageName, Date(lastTimestamp))
Expand Down Expand Up @@ -218,7 +225,9 @@ class PhoneUsageManager(context: PhoneUsageService) : AbstractSourceManager<Phon

override fun onClose() {
if (phoneUsageProcessor.isStarted) {
phoneUsageProcessor.stop()
usagesTaskExecutor.stop {
phoneUsageProcessor.stop()
}
service.unregisterReceiver(phoneStateReceiver)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ import android.content.SharedPreferences
import android.content.pm.PackageManager
import android.os.Build
import androidx.core.app.ActivityCompat
import kotlinx.coroutines.SupervisorJob
import org.radarbase.android.data.DataCache
import org.radarbase.android.source.AbstractSourceManager
import org.radarbase.android.source.BaseSourceState
import org.radarbase.android.source.SourceStatusListener
import org.radarbase.android.util.BluetoothStateReceiver.Companion.bluetoothAdapter
import org.radarbase.android.util.BluetoothStateReceiver.Companion.hasBluetoothPermission
import org.radarbase.android.util.CoroutineTaskExecutor
import org.radarbase.android.util.HashGenerator
import org.radarbase.android.util.OfflineProcessor
import org.radarcns.kafka.ObservationKey
Expand All @@ -56,6 +58,8 @@ class PhoneBluetoothManager(service: PhoneBluetoothService) : AbstractSourceMana
PhoneBluetoothDeviceScanned()
)

private val bluetoothTaskExecutor = CoroutineTaskExecutor(this::class.simpleName!!)

private var bluetoothBroadcastReceiver: BroadcastReceiver? = null
private val hashGenerator: HashGenerator = HashGenerator(service, "bluetooth_devices")
private val preferences: SharedPreferences
Expand Down Expand Up @@ -88,10 +92,11 @@ class PhoneBluetoothManager(service: PhoneBluetoothService) : AbstractSourceMana
status = SourceStatusListener.Status.READY
register()
processor.start()
bluetoothTaskExecutor.start(SupervisorJob())
status = SourceStatusListener.Status.CONNECTED
}

private fun processBluetoothDevices() {
private suspend fun processBluetoothDevices() {
val bluetoothAdapter = service.bluetoothAdapter
if (bluetoothAdapter == null) {
logger.error("Bluetooth is not available.")
Expand Down Expand Up @@ -139,21 +144,24 @@ class PhoneBluetoothManager(service: PhoneBluetoothService) : AbstractSourceMana

pairedDevices.forEach { bd ->
val mac = bd.address
val hash = hashGenerator.createHashByteBuffer(mac + "$hashSaltReference")
val hash =
hashGenerator.createHashByteBuffer(mac + "$hashSaltReference")

send(bluetoothScannedTopic, scannedTopicBuilder.apply {
bluetoothTaskExecutor.execute {
send(bluetoothScannedTopic, scannedTopicBuilder.apply {
this.macAddressHash = hash
this.pairedState = bd.bondState.toPairedState()
this.hashSaltReference = hashSaltReference
}.build())
}

send(bluetoothScannedTopic, scannedTopicBuilder.apply {
this.macAddressHash = macAddressHash
this.pairedState = device.bondState.toPairedState()
this.hashSaltReference = hashSaltReference
}.build())

}
bluetoothTaskExecutor.execute {
send(bluetoothScannedTopic, scannedTopicBuilder.apply {
this.macAddressHash = macAddressHash
this.pairedState = device.bondState.toPairedState()
this.hashSaltReference = hashSaltReference
}.build())
}
}

BluetoothAdapter.ACTION_DISCOVERY_FINISHED -> {
Expand All @@ -164,8 +172,13 @@ class PhoneBluetoothManager(service: PhoneBluetoothService) : AbstractSourceMana

if (!isClosed) {
val time = currentTime
send(bluetoothDevicesTopic, PhoneBluetoothDevices(
time, time, bondedDevices, numberOfDevices, true))
bluetoothTaskExecutor.execute {
send(
bluetoothDevicesTopic, PhoneBluetoothDevices(
time, time, bondedDevices, numberOfDevices, true
)
)
}
}
}
}
Expand All @@ -182,7 +195,9 @@ class PhoneBluetoothManager(service: PhoneBluetoothService) : AbstractSourceMana
}

override fun onClose() {
processor.stop()
bluetoothTaskExecutor.stop {
processor.stop()
}
bluetoothBroadcastReceiver?.let {
try {
service.unregisterReceiver(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.radarbase.android.data.DataCache
import org.radarbase.android.source.AbstractSourceManager
import org.radarbase.android.source.BaseSourceState
import org.radarbase.android.source.SourceStatusListener
import org.radarbase.android.util.CoroutineTaskExecutor
import org.radarbase.android.util.OfflineProcessor
import org.radarcns.kafka.ObservationKey
import org.radarcns.passive.phone.PhoneContactList
Expand All @@ -43,6 +44,8 @@ class PhoneContactListManager(service: PhoneContactsListService) : AbstractSourc
private val db: ContentResolver = service.contentResolver
private var savedContactLookups: Set<String> = emptySet()

private val contactsTaskExecutor = CoroutineTaskExecutor(this::class.simpleName!!)

init {
name = service.getString(R.string.contact_list)
processor = OfflineProcessor(service) {
Expand All @@ -51,6 +54,7 @@ class PhoneContactListManager(service: PhoneContactsListService) : AbstractSourc
requestName = ACTION_UPDATE_CONTACTS_LIST
wake = false
}
contactsTaskExecutor.start()
}

override fun start(acceptableIds: Set<String>) {
Expand Down Expand Up @@ -112,7 +116,9 @@ class PhoneContactListManager(service: PhoneContactsListService) : AbstractSourc
}

override fun onClose() {
processor.stop()
contactsTaskExecutor.stop {
processor.stop()
}
}

private fun makeQuery(
Expand All @@ -136,7 +142,7 @@ class PhoneContactListManager(service: PhoneContactsListService) : AbstractSourc
}
}

private fun processContacts() {
private suspend fun processContacts() {
val newContactLookups = queryContacts() ?: return

var added: Int? = null
Expand Down
Loading

0 comments on commit 5e7188f

Please sign in to comment.