Skip to content

Commit c099203

Browse files
committed
refactor(notification): centralize Android log tags and tighten pending action typing
1 parent 9b2f6f0 commit c099203

6 files changed

Lines changed: 85 additions & 51 deletions

File tree

plugins/notification/android/src/main/java/ChannelManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class ChannelManager(private var context: Context) {
8484
notificationChannel.lightColor = Color.parseColor(lightColor)
8585
} catch (ex: IllegalArgumentException) {
8686
Logger.error(
87-
Logger.tags("NotificationChannel"),
87+
NOTIFICATION_CHANNEL_LOG_TAGS,
8888
"Invalid color provided for light color.",
8989
null
9090
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2+
// SPDX-License-Identifier: Apache-2.0
3+
// SPDX-License-Identifier: MIT
4+
5+
package app.tauri.notification
6+
7+
import app.tauri.Logger
8+
9+
val NOTIFICATION_LOG_TAGS = Logger.tags("Notification")
10+
val NOTIFICATION_CHANNEL_LOG_TAGS = Logger.tags("NotificationChannel")

plugins/notification/android/src/main/java/NotificationPlugin.kt

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) {
138138
if (droppedExpired > 0) {
139139
rebuildPendingActionEventKeysLocked()
140140
Logger.debug(
141-
Logger.tags("Notification"),
141+
NOTIFICATION_LOG_TAGS,
142142
"Dropped expired pending actionPerformed events=$droppedExpired"
143143
)
144144
}
@@ -183,7 +183,7 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) {
183183
val key = event.optString("key").ifEmpty { buildActionEventKey(payload) }
184184
if (pendingActionEventKeys.contains(key)) {
185185
Logger.debug(
186-
Logger.tags("Notification"),
186+
NOTIFICATION_LOG_TAGS,
187187
"Skipping duplicate restored actionPerformed event key=$key"
188188
)
189189
continue
@@ -193,12 +193,12 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) {
193193
pendingActionEventKeys.add(key)
194194
}
195195
Logger.debug(
196-
Logger.tags("Notification"),
196+
NOTIFICATION_LOG_TAGS,
197197
"Restored pending actionPerformed events=${pendingActionEvents.size}"
198198
)
199199
} catch (error: Throwable) {
200200
Logger.error(
201-
Logger.tags("Notification"),
201+
NOTIFICATION_LOG_TAGS,
202202
"Failed to restore pending actionPerformed events",
203203
error
204204
)
@@ -221,7 +221,7 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) {
221221

222222
super.load(webView)
223223
this.webView = webView
224-
Logger.debug(Logger.tags("Notification"), "Plugin load started")
224+
Logger.debug(NOTIFICATION_LOG_TAGS, "Plugin load started")
225225
synchronized(this) {
226226
pendingActionEvents.clear()
227227
pendingActionEventKeys.clear()
@@ -241,7 +241,7 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) {
241241
this.manager = manager
242242

243243
notificationManager = activity.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
244-
Logger.debug(Logger.tags("Notification"), "Plugin load complete; awaiting notification intents")
244+
Logger.debug(NOTIFICATION_LOG_TAGS, "Plugin load complete; awaiting notification intents")
245245

246246
val intent = activity.intent
247247
intent?.let {
@@ -251,21 +251,21 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) {
251251

252252
override fun onNewIntent(intent: Intent) {
253253
super.onNewIntent(intent)
254-
Logger.debug(Logger.tags("Notification"), "onNewIntent received action=${intent.action}")
254+
Logger.debug(NOTIFICATION_LOG_TAGS, "onNewIntent received action=${intent.action}")
255255
onIntent(intent)
256256
}
257257

258258
fun onIntent(intent: Intent) {
259259
if (Intent.ACTION_MAIN != intent.action) {
260-
Logger.debug(Logger.tags("Notification"), "Ignoring intent action=${intent.action}")
260+
Logger.debug(NOTIFICATION_LOG_TAGS, "Ignoring intent action=${intent.action}")
261261
return
262262
}
263-
Logger.debug(Logger.tags("Notification"), "Processing ACTION_MAIN intent for notification action")
263+
Logger.debug(NOTIFICATION_LOG_TAGS, "Processing ACTION_MAIN intent for notification action")
264264
val dataJson = manager.handleNotificationActionPerformed(intent, notificationStorage)
265265
if (dataJson != null) {
266266
dispatchActionPerformed(dataJson)
267267
} else {
268-
Logger.debug(Logger.tags("Notification"), "No action payload extracted from intent")
268+
Logger.debug(NOTIFICATION_LOG_TAGS, "No action payload extracted from intent")
269269
}
270270
}
271271

@@ -277,7 +277,7 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) {
277277
// Without this key check, the same action can be enqueued twice across reload boundaries.
278278
if (pendingActionEventKeys.contains(key)) {
279279
Logger.debug(
280-
Logger.tags("Notification"),
280+
NOTIFICATION_LOG_TAGS,
281281
"Skipping duplicate queued actionPerformed event key=$key"
282282
)
283283
return
@@ -286,21 +286,21 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) {
286286
pendingActionEventKeys.add(key)
287287
persistPendingActionEventsLocked()
288288
Logger.debug(
289-
Logger.tags("Notification"),
289+
NOTIFICATION_LOG_TAGS,
290290
"Queued actionPerformed event; listener not ready (pending=${pendingActionEvents.size})"
291291
)
292292
return
293293
}
294294
}
295-
Logger.debug(Logger.tags("Notification"), "Dispatching actionPerformed event immediately")
295+
Logger.debug(NOTIFICATION_LOG_TAGS, "Dispatching actionPerformed event immediately")
296296
trigger("actionPerformed", payload)
297297
}
298298

299299
@Command
300300
fun show(invoke: Invoke) {
301301
val notification = invoke.parseArgs(Notification::class.java)
302302
Logger.debug(
303-
Logger.tags("Notification"),
303+
NOTIFICATION_LOG_TAGS,
304304
"show called id=${notification.id} title=${notification.title} actionTypeId=${notification.actionTypeId} hasSchedule=${notification.schedule != null}"
305305
)
306306
val id = manager.schedule(notification)
@@ -312,29 +312,29 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) {
312312
fun batch(invoke: Invoke) {
313313
val args = invoke.parseArgs(BatchArgs::class.java)
314314
Logger.debug(
315-
Logger.tags("Notification"),
315+
NOTIFICATION_LOG_TAGS,
316316
"batch called notifications=${args.notifications.size}"
317317
)
318318

319319
val ids = manager.schedule(args.notifications)
320320
notificationStorage.appendNotifications(args.notifications)
321-
Logger.debug(Logger.tags("Notification"), "batch scheduled ids=$ids")
321+
Logger.debug(NOTIFICATION_LOG_TAGS, "batch scheduled ids=$ids")
322322

323323
invoke.resolveObject(ids)
324324
}
325325

326326
@Command
327327
fun cancel(invoke: Invoke) {
328328
val args = invoke.parseArgs(CancelArgs::class.java)
329-
Logger.debug(Logger.tags("Notification"), "cancel called notifications=${args.notifications}")
329+
Logger.debug(NOTIFICATION_LOG_TAGS, "cancel called notifications=${args.notifications}")
330330
manager.cancel(args.notifications)
331331
invoke.resolve()
332332
}
333333

334334
@Command
335335
fun removeActive(invoke: Invoke) {
336336
val args = invoke.parseArgs(RemoveActiveArgs::class.java)
337-
Logger.debug(Logger.tags("Notification"), "removeActive called notifications=${args.notifications.size}")
337+
Logger.debug(NOTIFICATION_LOG_TAGS, "removeActive called notifications=${args.notifications.size}")
338338

339339
if (args.notifications.isEmpty()) {
340340
notificationManager.cancelAll()
@@ -354,7 +354,7 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) {
354354
@Command
355355
fun getPending(invoke: Invoke) {
356356
val notifications= notificationStorage.getSavedNotifications()
357-
Logger.debug(Logger.tags("Notification"), "getPending returning count=${notifications.size}")
357+
Logger.debug(NOTIFICATION_LOG_TAGS, "getPending returning count=${notifications.size}")
358358
val result = Notification.buildNotificationPendingList(notifications)
359359
invoke.resolveObject(result)
360360
}
@@ -363,7 +363,7 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) {
363363
fun registerActionTypes(invoke: Invoke) {
364364
val args = invoke.parseArgs(RegisterActionTypesArgs::class.java)
365365
Logger.debug(
366-
Logger.tags("Notification"),
366+
NOTIFICATION_LOG_TAGS,
367367
"registerActionTypes called types=${args.types.size}"
368368
)
369369
notificationStorage.writeActionGroup(args.types)
@@ -385,7 +385,7 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) {
385385
persistPendingActionEventsLocked()
386386
}
387387
Logger.debug(
388-
Logger.tags("Notification"),
388+
NOTIFICATION_LOG_TAGS,
389389
"Action listener marked ready; drained pending actionPerformed events=$drainedCount"
390390
)
391391
invoke.resolveObject(pending)

plugins/notification/android/src/main/java/TauriNotificationManager.kt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ class TauriNotificationManager(
5454
data: Intent,
5555
notificationStorage: NotificationStorage
5656
): JSObject? {
57-
Logger.debug(Logger.tags("Notification"), "Notification received: " + data.dataString)
57+
Logger.debug(NOTIFICATION_LOG_TAGS, "Notification received: " + data.dataString)
5858
val notificationId =
5959
data.getIntExtra(NOTIFICATION_INTENT_KEY, Int.MIN_VALUE)
6060
if (notificationId == Int.MIN_VALUE) {
61-
Logger.debug(Logger.tags("Notification"), "Activity started without notification attached")
61+
Logger.debug(NOTIFICATION_LOG_TAGS, "Activity started without notification attached")
6262
return null
6363
}
6464
val savedNotification = notificationStorage.getSavedNotification(notificationId.toString())
@@ -71,7 +71,7 @@ class TauriNotificationManager(
7171
dataJson.put("inputValue", input?.toString())
7272
val menuAction = data.getStringExtra(ACTION_INTENT_KEY)
7373
Logger.debug(
74-
Logger.tags("Notification"),
74+
NOTIFICATION_LOG_TAGS,
7575
"Action performed id=$notificationId actionId=$menuAction removable=$isRemovable"
7676
)
7777
dismissVisibleNotification(notificationId)
@@ -92,7 +92,7 @@ class TauriNotificationManager(
9292
request.put("actionTypeId", savedNotification.actionTypeId)
9393
}
9494
Logger.debug(
95-
Logger.tags("Notification"),
95+
NOTIFICATION_LOG_TAGS,
9696
"Recovered missing notification metadata from storage id=$notificationId actionTypeId=${savedNotification?.actionTypeId}"
9797
)
9898
} else if (!request.has("id")) {
@@ -145,7 +145,7 @@ class TauriNotificationManager(
145145
fun schedule(notification: Notification): Int {
146146
val notificationManager = NotificationManagerCompat.from(context)
147147
Logger.debug(
148-
Logger.tags("Notification"),
148+
NOTIFICATION_LOG_TAGS,
149149
"Scheduling notification id=${notification.id} actionTypeId=${notification.actionTypeId} hasSchedule=${notification.schedule != null}"
150150
)
151151
return trigger(notificationManager, notification)
@@ -154,13 +154,13 @@ class TauriNotificationManager(
154154
fun schedule(notifications: List<Notification>): List<Int> {
155155
val ids = mutableListOf<Int>()
156156
val notificationManager = NotificationManagerCompat.from(context)
157-
Logger.debug(Logger.tags("Notification"), "Scheduling batch notifications count=${notifications.size}")
157+
Logger.debug(NOTIFICATION_LOG_TAGS, "Scheduling batch notifications count=${notifications.size}")
158158

159159
for (notification in notifications) {
160160
val id = trigger(notificationManager, notification)
161161
ids.add(id)
162162
}
163-
Logger.debug(Logger.tags("Notification"), "Scheduled batch notification ids=$ids")
163+
Logger.debug(NOTIFICATION_LOG_TAGS, "Scheduled batch notification ids=$ids")
164164

165165
return ids
166166
}
@@ -339,7 +339,7 @@ class TauriNotificationManager(
339339
val schedule = notification.schedule
340340
intent.putExtra(NOTIFICATION_IS_REMOVABLE_KEY, schedule == null || schedule.isRemovable())
341341
Logger.debug(
342-
Logger.tags("Notification"),
342+
NOTIFICATION_LOG_TAGS,
343343
"Built action intent notificationId=${notification.id} action=$action removable=${schedule == null || schedule.isRemovable()}"
344344
)
345345
return intent
@@ -370,7 +370,7 @@ class TauriNotificationManager(
370370
when (schedule) {
371371
is NotificationSchedule.At -> {
372372
if (schedule.date.time < Date().time) {
373-
Logger.error(Logger.tags("Notification"), "Scheduled time must be *after* current time", null)
373+
Logger.error(NOTIFICATION_LOG_TAGS, "Scheduled time must be *after* current time", null)
374374
return
375375
}
376376
if (schedule.repeating) {
@@ -388,7 +388,7 @@ class TauriNotificationManager(
388388
setExactIfPossible(alarmManager, schedule, trigger, pendingIntent)
389389
val sdf = SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
390390
Logger.debug(
391-
Logger.tags("Notification"),
391+
NOTIFICATION_LOG_TAGS,
392392
"notification " + request.id + " will next fire at " + sdf.format(Date(trigger))
393393
)
394394
}
@@ -494,7 +494,7 @@ class NotificationDismissReceiver : BroadcastReceiver() {
494494
val intExtra =
495495
intent.getIntExtra(NOTIFICATION_INTENT_KEY, Int.MIN_VALUE)
496496
if (intExtra == Int.MIN_VALUE) {
497-
Logger.error(Logger.tags("Notification"), "Invalid notification dismiss operation", null)
497+
Logger.error(NOTIFICATION_LOG_TAGS, "Invalid notification dismiss operation", null)
498498
return
499499
}
500500
val isRemovable =
@@ -524,7 +524,7 @@ class TimedNotificationPublisher : BroadcastReceiver() {
524524
notification?.`when` = System.currentTimeMillis()
525525
val id = intent.getIntExtra(NOTIFICATION_INTENT_KEY, Int.MIN_VALUE)
526526
if (id == Int.MIN_VALUE) {
527-
Logger.error(Logger.tags("Notification"), "No valid id supplied", null)
527+
Logger.error(NOTIFICATION_LOG_TAGS, "No valid id supplied", null)
528528
}
529529
val storage = NotificationStorage(context, ObjectMapper())
530530

@@ -564,7 +564,7 @@ class TimedNotificationPublisher : BroadcastReceiver() {
564564
}
565565
val sdf = SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
566566
Logger.debug(
567-
Logger.tags("Notification"),
567+
NOTIFICATION_LOG_TAGS,
568568
"notification " + id + " will next fire at " + sdf.format(Date(trigger))
569569
)
570570
return true

0 commit comments

Comments
 (0)