[O] Split notification
This commit is contained in:
@@ -18,7 +18,6 @@ import org.hydev.wearsync.bles.BluetoothHandler.Companion.ble
|
|||||||
import org.hydev.wearsync.bles.decoders.BatteryDecoder
|
import org.hydev.wearsync.bles.decoders.BatteryDecoder
|
||||||
import org.hydev.wearsync.bles.decoders.HeartRateDecoder
|
import org.hydev.wearsync.bles.decoders.HeartRateDecoder
|
||||||
import org.hydev.wearsync.bles.decoders.IDecoder
|
import org.hydev.wearsync.bles.decoders.IDecoder
|
||||||
import timber.log.Timber
|
|
||||||
|
|
||||||
|
|
||||||
class MyService : Service()
|
class MyService : Service()
|
||||||
@@ -36,7 +35,7 @@ class MyService : Service()
|
|||||||
|
|
||||||
registerReceiver(mBatInfoReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
|
registerReceiver(mBatInfoReceiver, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
|
||||||
|
|
||||||
startForeground()
|
startForeground(NOTIF_ID, "Just started!".toNotif())
|
||||||
return super.onStartCommand(intent, flags, startId)
|
return super.onStartCommand(intent, flags, startId)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,39 +45,29 @@ class MyService : Service()
|
|||||||
observe<HeartRateDecoder>()
|
observe<HeartRateDecoder>()
|
||||||
}
|
}
|
||||||
|
|
||||||
private inline fun <reified T : IDecoder<out Any>> observe() {
|
private inline fun <reified T : IDecoder<out Any>> observe() = ble.observeAny<T> { add(it) }
|
||||||
ble.observeAny<T> {
|
|
||||||
scope.launch {
|
private fun add(it: Any) = scope.launch {
|
||||||
runCatching {
|
runCatching {
|
||||||
println("Adding ${it.javaClass.simpleName} to influxdb")
|
println("Adding ${it.javaClass.simpleName} to influxdb")
|
||||||
influx add it
|
influx add it
|
||||||
}.orTrace()
|
}.orTrace()
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private val mBatInfoReceiver: BroadcastReceiver = object : BroadcastReceiver()
|
private val mBatInfoReceiver = object : BroadcastReceiver() {
|
||||||
{
|
override fun onReceive(ctxt: Context, intent: Intent) { add(intent.batteryInfo(bm)) }
|
||||||
override fun onReceive(ctxt: Context, intent: Intent) {
|
|
||||||
val bi = intent.batteryInfo(bm)
|
|
||||||
Timber.d("Battery info recorded: $bi")
|
|
||||||
scope.launch { runCatching { influx add bi }.orTrace() }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun startForeground()
|
private val intent = PendingIntent.getActivity(this, 0, intent<MainActivity>(),
|
||||||
{
|
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
|
||||||
val notificationIntent = Intent(this, MainActivity::class.java)
|
|
||||||
val pendingIntent = PendingIntent.getActivity(this, 0,
|
private fun String.toNotif() = NotificationCompat.Builder(this@MyService, NOTIF_CHANNEL_ID)
|
||||||
notificationIntent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
|
.setSmallIcon(R.drawable.ic_watch_24)
|
||||||
startForeground(NOTIF_ID, NotificationCompat.Builder(this, NOTIF_CHANNEL_ID)
|
.setContentIntent(intent)
|
||||||
.setOngoing(true)
|
.setContentTitle("🐱 Running!")
|
||||||
.setSmallIcon(R.drawable.ic_watch_24)
|
.setOngoing(true)
|
||||||
.setContentTitle("🐱 Running!")
|
.setSubText(this)
|
||||||
.setContentIntent(pendingIntent)
|
.build()
|
||||||
.build()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object
|
companion object
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user