Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package coredevices.pebble.firmware

import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import androidx.core.app.NotificationCompat
import coredevices.util.R
import io.rebble.libpebblecommon.connection.AppContext

actual fun postWatchFullyChargedNotification(appContext: AppContext, watchName: String) {
val context = appContext.context
context.createBatteryNotificationChannel()
val builder = NotificationCompat.Builder(context, BATTERY_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Watch Fully Charged")
.setContentText("$watchName is fully charged")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true)
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(WATCH_FULLY_CHARGED_NOTIFICATION_ID, builder.build())
}

private const val BATTERY_CHANNEL_ID = "battery_fully_charged_channel"
private const val WATCH_FULLY_CHARGED_NOTIFICATION_ID = 1001

private fun Context.createBatteryNotificationChannel() {
val channel = NotificationChannel(
BATTERY_CHANNEL_ID,
"Battery",
NotificationManager.IMPORTANCE_DEFAULT
).apply {
description = "Watch fully charged notifications"
}
val manager = getSystemService(NotificationManager::class.java)
manager.createNotificationChannel(channel)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import coredevices.database.WeatherLocationDao
import coredevices.database.insertDefaultWeatherLocationOnce
import coredevices.firestore.UsersDao
import coredevices.pebble.firmware.FirmwareUpdateUiTracker
import coredevices.pebble.firmware.postWatchFullyChargedNotification
import coredevices.pebble.services.AppstoreSourceInitializer
import coredevices.pebble.services.AnalyticsHeartbeatQueue
import coredevices.pebble.services.MemfaultChunkQueue
Expand All @@ -18,6 +19,7 @@ import coredevices.util.DoneInitialOnboarding
import coredevices.util.PermissionRequester
import dev.gitlive.firebase.Firebase
import dev.gitlive.firebase.crashlytics.crashlytics
import io.rebble.libpebblecommon.connection.AppContext
import io.rebble.libpebblecommon.connection.BleDiscoveredPebbleDevice
import io.rebble.libpebblecommon.connection.CommonConnectedDevice
import io.rebble.libpebblecommon.connection.ConnectedPebble
Expand All @@ -28,6 +30,7 @@ import io.rebble.libpebblecommon.connection.FirmwareUpdateCheckResult
import io.rebble.libpebblecommon.connection.KnownPebbleDevice
import io.rebble.libpebblecommon.connection.LibPebble
import io.rebble.libpebblecommon.connection.PebbleDevice
import io.rebble.libpebblecommon.connection.PebbleIdentifier
import io.rebble.libpebblecommon.connection.endpointmanager.FirmwareUpdater
import io.rebble.libpebblecommon.metadata.WatchHardwarePlatform
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -58,6 +61,7 @@ class PebbleAppDelegate(
private val memfaultChunkQueue: MemfaultChunkQueue,
private val analyticsHeartbeatQueue: AnalyticsHeartbeatQueue,
private val pebbleWebServices: PebbleWebServices,
private val appContext: AppContext,
) {
private val logger = Logger.withTag("PebbleAppDelegate")

Expand Down Expand Up @@ -159,6 +163,25 @@ class PebbleAppDelegate(
}
}
}
GlobalScope.launch {
val batteryLevels = mutableMapOf<PebbleIdentifier, Int?>()
val notified100 = mutableMapOf<PebbleIdentifier, Boolean>()
libPebble.watches.collect { watches ->
watches.filterIsInstance<CommonConnectedDevice>().forEach { watch ->
val level = watch.batteryLevel
val prevLevel = batteryLevels[watch.identifier]
if (level == 100 && prevLevel != null && prevLevel < 100
&& notified100[watch.identifier] != true) {
postWatchFullyChargedNotification(appContext, watch.name)
notified100[watch.identifier] = true
}
if (level != null && level <= 90 && notified100[watch.identifier] == true) {
notified100[watch.identifier] = false
}
batteryLevels[watch.identifier] = level
}
}
}
GlobalScope.launch {
libPebble.watches.flatMapLatest { watches ->
val flows = watches.filterIsInstance<ConnectedPebble.PKJS>()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package coredevices.pebble.firmware

import io.rebble.libpebblecommon.connection.AppContext

expect fun postWatchFullyChargedNotification(appContext: AppContext, watchName: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package coredevices.pebble.firmware

import io.rebble.libpebblecommon.connection.AppContext
import platform.UserNotifications.UNMutableNotificationContent
import platform.UserNotifications.UNNotificationRequest
import platform.UserNotifications.UNTimeIntervalNotificationTrigger
import platform.UserNotifications.UNUserNotificationCenter

actual fun postWatchFullyChargedNotification(appContext: AppContext, watchName: String) {
val content = UNMutableNotificationContent()
content.setTitle("Watch Fully Charged")
content.setBody("$watchName is fully charged")
val request = UNNotificationRequest.requestWithIdentifier(
"watch-fully-charged",
content,
UNTimeIntervalNotificationTrigger.triggerWithTimeInterval(1.0, false)
)
UNUserNotificationCenter.currentNotificationCenter().addNotificationRequest(request, null)
}
Loading