|
| 1 | +@file:OptIn(androidx.compose.material3.ExperimentalMaterial3Api::class) |
| 2 | + |
| 3 | +package me.kavishdevar.librepods.services.notifications |
| 4 | + |
| 5 | +import android.app.NotificationChannel |
| 6 | +import android.app.NotificationManager |
| 7 | +import android.app.PendingIntent |
| 8 | +import android.content.Context |
| 9 | +import android.content.Intent |
| 10 | +import androidx.core.app.NotificationCompat |
| 11 | +import androidx.core.graphics.drawable.IconCompat |
| 12 | +import me.kavishdevar.librepods.MainActivity |
| 13 | +import me.kavishdevar.librepods.R |
| 14 | +import me.kavishdevar.librepods.data.Battery |
| 15 | +import me.kavishdevar.librepods.data.BatteryComponent |
| 16 | +import me.kavishdevar.librepods.data.BatteryStatus |
| 17 | + |
| 18 | +object LiveUpdateNotification { |
| 19 | + const val CHANNEL_ID = "airpods_live_update" |
| 20 | + const val NOTIF_ID_MAIN = 4 |
| 21 | + const val NOTIF_ID_LOW_LEFT = 5 |
| 22 | + const val NOTIF_ID_LOW_RIGHT = 6 |
| 23 | + const val NOTIF_ID_LOW_CASE = 7 |
| 24 | + const val NOTIF_ID_CASE_OPEN = 8 |
| 25 | + |
| 26 | + private val state = LiveUpdateState() |
| 27 | + |
| 28 | + fun resetState() { |
| 29 | + state.reset() |
| 30 | + } |
| 31 | + |
| 32 | + fun ensureChannel(context: Context) { |
| 33 | + val nm = context.getSystemService(NotificationManager::class.java) |
| 34 | + if (nm.getNotificationChannel(CHANNEL_ID) != null) return |
| 35 | + val channel = NotificationChannel( |
| 36 | + CHANNEL_ID, |
| 37 | + context.getString(R.string.live_update_channel_name), |
| 38 | + NotificationManager.IMPORTANCE_HIGH |
| 39 | + ).apply { |
| 40 | + description = context.getString(R.string.live_update_channel_description) |
| 41 | + enableVibration(false) |
| 42 | + setSound(null, null) |
| 43 | + } |
| 44 | + nm.createNotificationChannel(channel) |
| 45 | + } |
| 46 | + |
| 47 | + fun show( |
| 48 | + context: Context, |
| 49 | + airpodsName: String, |
| 50 | + batteryList: List<Battery>, |
| 51 | + headsUp: Boolean |
| 52 | + ) { |
| 53 | + val nm = context.getSystemService(NotificationManager::class.java) |
| 54 | + val builder = buildMain(context, airpodsName, batteryList, headsUp) |
| 55 | + nm.notify(NOTIF_ID_MAIN, builder.build()) |
| 56 | + } |
| 57 | + |
| 58 | + fun update( |
| 59 | + context: Context, |
| 60 | + airpodsName: String, |
| 61 | + batteryList: List<Battery> |
| 62 | + ) { |
| 63 | + val nm = context.getSystemService(NotificationManager::class.java) |
| 64 | + val builder = buildMain(context, airpodsName, batteryList, headsUp = false) |
| 65 | + nm.notify(NOTIF_ID_MAIN, builder.build()) |
| 66 | + } |
| 67 | + |
| 68 | + fun checkLowBattery(context: Context, batteryList: List<Battery>) { |
| 69 | + val nm = context.getSystemService(NotificationManager::class.java) |
| 70 | + val pi = mainActivityPendingIntent(context) |
| 71 | + |
| 72 | + batteryList.forEach { battery -> |
| 73 | + if (battery.status == BatteryStatus.DISCONNECTED) return@forEach |
| 74 | + if (!state.shouldFireLowBattery(battery.component, battery.level)) return@forEach |
| 75 | + |
| 76 | + val (titleRes, notifId) = when (battery.component) { |
| 77 | + BatteryComponent.LEFT -> R.string.live_update_low_battery_left to NOTIF_ID_LOW_LEFT |
| 78 | + BatteryComponent.RIGHT -> R.string.live_update_low_battery_right to NOTIF_ID_LOW_RIGHT |
| 79 | + BatteryComponent.CASE -> R.string.live_update_low_battery_case to NOTIF_ID_LOW_CASE |
| 80 | + else -> return@forEach |
| 81 | + } |
| 82 | + |
| 83 | + val notif = NotificationCompat.Builder(context, CHANNEL_ID) |
| 84 | + .setSmallIcon(R.drawable.airpods) |
| 85 | + .setContentTitle(context.getString(titleRes, battery.level)) |
| 86 | + .setContentText(context.getString(R.string.live_update_low_battery_body)) |
| 87 | + .setContentIntent(pi) |
| 88 | + .setPriority(NotificationCompat.PRIORITY_HIGH) |
| 89 | + .setAutoCancel(true) |
| 90 | + .setOnlyAlertOnce(false) |
| 91 | + .build() |
| 92 | + nm.notify(notifId, notif) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + fun showCaseOpenReminder(context: Context, batteryList: List<Battery>) { |
| 97 | + val caseBattery = batteryList.firstOrNull { it.component == BatteryComponent.CASE } |
| 98 | + ?: return |
| 99 | + if (caseBattery.status == BatteryStatus.DISCONNECTED) return |
| 100 | + if (!state.shouldFireCaseOpenReminder(caseBattery.level)) return |
| 101 | + |
| 102 | + val nm = context.getSystemService(NotificationManager::class.java) |
| 103 | + val notif = NotificationCompat.Builder(context, CHANNEL_ID) |
| 104 | + .setSmallIcon(R.drawable.airpods) |
| 105 | + .setContentTitle(context.getString(R.string.live_update_case_open_title)) |
| 106 | + .setContentText(context.getString(R.string.live_update_case_open_body, caseBattery.level)) |
| 107 | + .setContentIntent(mainActivityPendingIntent(context)) |
| 108 | + .setPriority(NotificationCompat.PRIORITY_HIGH) |
| 109 | + .setAutoCancel(true) |
| 110 | + .setOnlyAlertOnce(false) |
| 111 | + .build() |
| 112 | + nm.notify(NOTIF_ID_CASE_OPEN, notif) |
| 113 | + } |
| 114 | + |
| 115 | + fun resetCaseOpenReminderOnLidClose() { |
| 116 | + state.resetCaseOpenReminder() |
| 117 | + } |
| 118 | + |
| 119 | + fun headsUpOnListeningModeChange( |
| 120 | + context: Context, |
| 121 | + airpodsName: String, |
| 122 | + batteryList: List<Battery>, |
| 123 | + newMode: Byte |
| 124 | + ) { |
| 125 | + if (!state.shouldFireListeningModeChange(newMode)) return |
| 126 | + show(context, airpodsName, batteryList, headsUp = true) |
| 127 | + } |
| 128 | + |
| 129 | + fun cancelAll(context: Context) { |
| 130 | + val nm = context.getSystemService(NotificationManager::class.java) |
| 131 | + nm.cancel(NOTIF_ID_MAIN) |
| 132 | + nm.cancel(NOTIF_ID_LOW_LEFT) |
| 133 | + nm.cancel(NOTIF_ID_LOW_RIGHT) |
| 134 | + nm.cancel(NOTIF_ID_LOW_CASE) |
| 135 | + nm.cancel(NOTIF_ID_CASE_OPEN) |
| 136 | + } |
| 137 | + |
| 138 | + private fun buildMain( |
| 139 | + context: Context, |
| 140 | + airpodsName: String, |
| 141 | + batteryList: List<Battery>, |
| 142 | + headsUp: Boolean |
| 143 | + ): NotificationCompat.Builder { |
| 144 | + val left = batteryList.firstOrNull { it.component == BatteryComponent.LEFT } |
| 145 | + val right = batteryList.firstOrNull { it.component == BatteryComponent.RIGHT } |
| 146 | + val case = batteryList.firstOrNull { it.component == BatteryComponent.CASE } |
| 147 | + |
| 148 | + val lowestEar = listOfNotNull(left, right) |
| 149 | + .filter { it.status != BatteryStatus.DISCONNECTED } |
| 150 | + .minByOrNull { it.level } |
| 151 | + ?.level ?: 0 |
| 152 | + |
| 153 | + val expandedBody = buildString { |
| 154 | + left?.takeIf { it.status != BatteryStatus.DISCONNECTED }?.let { |
| 155 | + append("L ") |
| 156 | + if (it.status == BatteryStatus.CHARGING) append("⚡") |
| 157 | + append("${it.level}%") |
| 158 | + } |
| 159 | + right?.takeIf { it.status != BatteryStatus.DISCONNECTED }?.let { |
| 160 | + if (isNotEmpty()) append(" · ") |
| 161 | + append("R ") |
| 162 | + if (it.status == BatteryStatus.CHARGING) append("⚡") |
| 163 | + append("${it.level}%") |
| 164 | + } |
| 165 | + case?.takeIf { it.status != BatteryStatus.DISCONNECTED }?.let { |
| 166 | + if (isNotEmpty()) append(" · ") |
| 167 | + append("Case ") |
| 168 | + if (it.status == BatteryStatus.CHARGING) append("⚡") |
| 169 | + append("${it.level}%") |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + val progressStyle = NotificationCompat.ProgressStyle() |
| 174 | + .setProgress(lowestEar) |
| 175 | + .setProgressTrackerIcon( |
| 176 | + IconCompat.createWithResource(context, R.drawable.airpods) |
| 177 | + ) |
| 178 | + |
| 179 | + return NotificationCompat.Builder(context, CHANNEL_ID) |
| 180 | + .setSmallIcon(R.drawable.airpods) |
| 181 | + .setContentTitle(airpodsName) |
| 182 | + .setContentText(if (expandedBody.isNotEmpty()) expandedBody else "$lowestEar%") |
| 183 | + .setStyle(progressStyle) |
| 184 | + .setOngoing(true) |
| 185 | + .setContentIntent(mainActivityPendingIntent(context)) |
| 186 | + .setPriority(if (headsUp) NotificationCompat.PRIORITY_HIGH else NotificationCompat.PRIORITY_DEFAULT) |
| 187 | + .setOnlyAlertOnce(!headsUp) |
| 188 | + .setRequestPromotedOngoing(true) |
| 189 | + } |
| 190 | + |
| 191 | + private fun mainActivityPendingIntent(context: Context): PendingIntent { |
| 192 | + return PendingIntent.getActivity( |
| 193 | + context, |
| 194 | + 0, |
| 195 | + Intent(context, MainActivity::class.java), |
| 196 | + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE |
| 197 | + ) |
| 198 | + } |
| 199 | +} |
0 commit comments