-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLiveUpdateModule.kt
More file actions
214 lines (180 loc) · 8.41 KB
/
LiveUpdateModule.kt
File metadata and controls
214 lines (180 loc) · 8.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package me.tinykitten.trainlcd
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.drawable.Icon
import android.os.Build
import android.os.Bundle
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReadableMap
private fun ReadableMap.optString(key: String, default: String = ""): String =
if (hasKey(key) && !isNull(key)) getString(key) ?: default else default
private fun ReadableMap.optBoolean(key: String, default: Boolean = false): Boolean =
if (hasKey(key) && !isNull(key)) getBoolean(key) else default
private fun ReadableMap.optDouble(key: String, default: Double = 0.0): Double =
if (hasKey(key) && !isNull(key)) getDouble(key) else default
class LiveUpdateModule(reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext) {
companion object {
private const val CHANNEL_ID = "live_update"
private const val NOTIFICATION_ID = 49152
private const val MAX_PROGRESS = 1000
}
override fun getName() = "LiveUpdateModule"
private fun getNotificationManager(): NotificationManager =
reactApplicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
private fun ensureChannel() {
val nm = getNotificationManager()
if (nm.getNotificationChannel(CHANNEL_ID) != null) return
val channel = NotificationChannel(
CHANNEL_ID,
reactApplicationContext.getString(R.string.live_update_channel_name),
NotificationManager.IMPORTANCE_DEFAULT
).apply {
description = reactApplicationContext.getString(R.string.live_update_channel_description)
setShowBadge(false)
lockscreenVisibility = Notification.VISIBILITY_PUBLIC
}
nm.createNotificationChannel(channel)
}
private fun createContentIntent(): PendingIntent? {
val intent = reactApplicationContext.packageManager
.getLaunchIntentForPackage(reactApplicationContext.packageName) ?: return null
return PendingIntent.getActivity(
reactApplicationContext,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
}
@ReactMethod
fun startLiveUpdate(state: ReadableMap?) {
if (Build.VERSION.SDK_INT < 36 || state == null) return
postProgressNotification(state)
}
@ReactMethod
fun updateLiveUpdate(state: ReadableMap?) {
if (Build.VERSION.SDK_INT < 36 || state == null) return
postProgressNotification(state)
}
@ReactMethod
fun stopLiveUpdate() {
getNotificationManager().cancel(NOTIFICATION_ID)
}
private fun createTrackerIcon(color: Int): Icon {
val density = reactApplicationContext.resources.displayMetrics.density
val sizePx = (24 * density).toInt()
val bitmap = Bitmap.createBitmap(sizePx, sizePx, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
val paint = Paint(Paint.ANTI_ALIAS_FLAG)
val cx = sizePx / 2f
val cy = sizePx / 2f
val strokeWidth = 2f * density
val radius = cx - strokeWidth / 2f
paint.style = Paint.Style.FILL
paint.color = color
canvas.drawCircle(cx, cy, radius, paint)
paint.style = Paint.Style.STROKE
paint.color = Color.WHITE
paint.strokeWidth = strokeWidth
canvas.drawCircle(cx, cy, radius, paint)
return Icon.createWithBitmap(bitmap)
}
@Suppress("NewApi")
private fun postProgressNotification(state: ReadableMap) {
ensureChannel()
val stationName = state.optString("stationName")
val nextStationName = state.optString("nextStationName")
val stationNumber = state.optString("stationNumber")
val nextStationNumber = state.optString("nextStationNumber")
val approaching = state.optBoolean("approaching")
val stopped = state.optBoolean("stopped")
val lineName = state.optString("lineName")
val lineColor = state.optString("lineColor", "#000000")
val progress = state.optDouble("progress")
val trainTypeName = state.optString("trainTypeName")
val boundStationName = state.optString("boundStationName")
val boundStationNumber = state.optString("boundStationNumber")
val passingStationName = state.optString("passingStationName")
val passingStationNumber = state.optString("passingStationNumber")
val parsedColor = try {
Color.parseColor(lineColor)
} catch (_: Exception) {
Color.BLACK
}
val progressInt = (progress * MAX_PROGRESS).toInt().coerceIn(0, MAX_PROGRESS)
fun formatStationWithNumber(name: String, number: String): String =
if (number.isNotEmpty()) "$name ($number)" else name
val contentTitle = when {
passingStationName.isNotEmpty() -> reactApplicationContext.getString(R.string.live_update_passing, formatStationWithNumber(passingStationName, passingStationNumber))
stopped -> formatStationWithNumber(stationName, stationNumber)
approaching -> reactApplicationContext.getString(R.string.live_update_approaching, formatStationWithNumber(nextStationName, nextStationNumber))
else -> reactApplicationContext.getString(R.string.live_update_next, formatStationWithNumber(nextStationName, nextStationNumber))
}
val formattedBoundName = run {
val hasSuffix = boundStationName.endsWith("方面")
val baseName = if (hasSuffix) boundStationName.removeSuffix("方面") else boundStationName
val delimiter = if (hasSuffix) "・" else "/"
val names = baseName.split(delimiter)
val numbers = boundStationNumber.split("/")
val formatted = names.mapIndexed { index, name ->
val number = numbers.getOrElse(index) { "" }
formatStationWithNumber(name, number)
}.joinToString(delimiter)
if (hasSuffix) "${formatted} 方面" else formatted
}
val contentText = buildString {
if (trainTypeName.isNotEmpty()) {
append(trainTypeName)
append(" ")
}
append(formattedBoundName)
}
val subText = lineName
val trackerIcon = createTrackerIcon(parsedColor)
val progressStyle = Notification.ProgressStyle()
.setStyledByProgress(true)
.setProgress(progressInt)
.setProgressTrackerIcon(trackerIcon)
.setProgressSegments(
listOf(
Notification.ProgressStyle.Segment(MAX_PROGRESS).setColor(parsedColor)
)
)
.setProgressPoints(
listOf(
Notification.ProgressStyle.Point(0).setColor(parsedColor),
Notification.ProgressStyle.Point(MAX_PROGRESS).setColor(parsedColor)
)
)
val shortCriticalText = when {
passingStationName.isNotEmpty() -> reactApplicationContext.getString(R.string.live_update_passing, passingStationName)
stopped -> stationName
approaching -> reactApplicationContext.getString(R.string.live_update_approaching, nextStationName)
else -> reactApplicationContext.getString(R.string.live_update_next, nextStationName)
}
val builder = Notification.Builder(reactApplicationContext, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification_live_update)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setSubText(subText)
.setStyle(progressStyle)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setShortCriticalText(shortCriticalText)
.addExtras(Bundle().apply {
putBoolean(Notification.EXTRA_REQUEST_PROMOTED_ONGOING, true)
})
createContentIntent()?.let { builder.setContentIntent(it) }
val notification = builder.build()
getNotificationManager().notify(NOTIFICATION_ID, notification)
}
}