react-native-live-widgets 0.0.0-stage → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/LICENSE +21 -0
  3. package/README.md +367 -2
  4. package/android/build.gradle +49 -0
  5. package/android/src/main/AndroidManifest.xml +38 -0
  6. package/android/src/main/java/com/reactnativeliveactivity/bridge/LiveActivityModule.kt +441 -0
  7. package/android/src/main/java/com/reactnativeliveactivity/bridge/LiveActivityPackage.kt +17 -0
  8. package/android/src/main/java/com/reactnativeliveactivity/liveactivity/LiveActivityHeadlessTaskService.kt +24 -0
  9. package/android/src/main/java/com/reactnativeliveactivity/liveactivity/LiveActivityNotifier.kt +410 -0
  10. package/android/src/main/java/com/reactnativeliveactivity/liveactivity/LiveActivityReceiver.kt +67 -0
  11. package/android/src/main/java/com/reactnativeliveactivity/renderer/DynamicNodeRegistry.kt +64 -0
  12. package/android/src/main/java/com/reactnativeliveactivity/renderer/DynamicWidgetCanvasRenderer.kt +1223 -0
  13. package/android/src/main/java/com/reactnativeliveactivity/storage/DynamicWidgetImageCache.kt +120 -0
  14. package/android/src/main/java/com/reactnativeliveactivity/storage/DynamicWidgetStateStore.kt +281 -0
  15. package/android/src/main/java/com/reactnativeliveactivity/widgets/DynamicHomeScreenWidgetProvider.kt +229 -0
  16. package/android/src/main/res/layout/rn_live_widget_host.xml +113 -0
  17. package/android/src/main/res/xml/rn_live_widget_info.xml +14 -0
  18. package/app.plugin.js +114 -0
  19. package/assets/cross_platform_parity.png +0 -0
  20. package/assets/dashboard_features.png +0 -0
  21. package/assets/donate_qr.png +0 -0
  22. package/assets/dynamic_island_expanded.png +0 -0
  23. package/assets/home_widget_interactive.png +0 -0
  24. package/assets/lock_screen_live_activity.png +0 -0
  25. package/cli/setup.js +491 -0
  26. package/docs/EXPO-GUIDE-EN.md +405 -0
  27. package/docs/EXPO-GUIDE-VI.md +427 -0
  28. package/docs/GUIDE-EN.md +847 -0
  29. package/docs/GUIDE-VI.md +845 -0
  30. package/docs/widgets.md +52 -0
  31. package/ios/Bridge/LiveActivityModule.m +105 -0
  32. package/ios/Bridge/LiveActivityModule.swift +957 -0
  33. package/ios/Intents/DynamicWidgetActionIntent.swift +38 -0
  34. package/ios/Models/DynamicActivityAttributes.swift +22 -0
  35. package/ios/Models/DynamicLayoutModels.swift +243 -0
  36. package/ios/Renderer/ColorHexExtension.swift +49 -0
  37. package/ios/Renderer/DynamicNodeRegistry.swift +54 -0
  38. package/ios/Renderer/DynamicNodeView.swift +1060 -0
  39. package/ios/Storage/DynamicWidgetImageCache.swift +137 -0
  40. package/ios/Storage/DynamicWidgetStateStore.swift +162 -0
  41. package/ios/Widgets/DynamicHomeScreenWidget.swift +435 -0
  42. package/ios/Widgets/DynamicLiveActivityWidget.swift +79 -0
  43. package/lib/builder.d.ts +60 -0
  44. package/lib/builder.d.ts.map +1 -0
  45. package/lib/builder.js +292 -0
  46. package/lib/builder.js.map +1 -0
  47. package/lib/dispatcher.d.ts +16 -0
  48. package/lib/dispatcher.d.ts.map +1 -0
  49. package/lib/dispatcher.js +18 -0
  50. package/lib/dispatcher.js.map +1 -0
  51. package/lib/index.d.ts +199 -0
  52. package/lib/index.d.ts.map +1 -0
  53. package/lib/index.js +621 -0
  54. package/lib/index.js.map +1 -0
  55. package/lib/types.d.ts +489 -0
  56. package/lib/types.d.ts.map +1 -0
  57. package/lib/types.js +9 -0
  58. package/lib/types.js.map +1 -0
  59. package/package.json +85 -4
  60. package/react-native-live-widgets.podspec +38 -0
  61. package/src/builder.ts +409 -0
  62. package/src/dispatcher.ts +25 -0
  63. package/src/index.ts +689 -0
  64. package/src/react-native-env.d.ts +29 -0
  65. package/src/types.ts +595 -0
@@ -0,0 +1,441 @@
1
+ package com.reactnativeliveactivity
2
+
3
+ import android.app.NotificationManager
4
+ import android.appwidget.AppWidgetManager
5
+ import android.content.ComponentName
6
+ import android.content.Context
7
+ import android.media.MediaPlayer
8
+ import android.os.Build
9
+ import android.provider.Settings
10
+ import com.facebook.react.bridge.Arguments
11
+ import com.facebook.react.bridge.Promise
12
+ import com.facebook.react.bridge.ReactApplicationContext
13
+ import com.facebook.react.bridge.ReactContextBaseJavaModule
14
+ import com.facebook.react.bridge.ReactMethod
15
+ import com.facebook.react.bridge.ReadableMap
16
+ import com.facebook.react.modules.core.DeviceEventManagerModule
17
+ import org.json.JSONObject
18
+ import java.util.concurrent.Executors
19
+
20
+ class LiveActivityModule(private val reactContext: ReactApplicationContext) :
21
+ ReactContextBaseJavaModule(reactContext) {
22
+
23
+ override fun getName() = "LiveActivityModule"
24
+
25
+ private val bgExecutor = Executors.newCachedThreadPool()
26
+
27
+ init {
28
+ currentReactContext = reactContext
29
+ }
30
+
31
+ companion object {
32
+ @Volatile
33
+ private var currentReactContext: ReactApplicationContext? = null
34
+
35
+ @Volatile
36
+ private var currentDevicePushToken: String? = null
37
+
38
+ @Volatile
39
+ private var currentPushToStartToken: String? = null
40
+
41
+ private val activityUpdateTokens = mutableMapOf<String, String>()
42
+ private var mediaPlayer: MediaPlayer? = null
43
+
44
+ fun emitActionClicked(actionId: String, activityId: String) {
45
+ val ctx = currentReactContext ?: return
46
+ if (!ctx.hasActiveReactInstance()) return
47
+ val map = Arguments.createMap().apply {
48
+ putString("actionId", actionId)
49
+ putString("activityId", activityId)
50
+ }
51
+ ctx.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
52
+ .emit("LiveActivityActionClicked", map)
53
+ }
54
+
55
+ /**
56
+ * Cho phép FirebaseMessagingService / Push SDK bên Android truyền FCM token vào
57
+ */
58
+ @JvmStatic
59
+ fun setDevicePushToken(token: String) {
60
+ currentDevicePushToken = token
61
+ val ctx = currentReactContext ?: return
62
+ if (ctx.hasActiveReactInstance()) {
63
+ ctx.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
64
+ .emit("DevicePushTokenChanged", token)
65
+ }
66
+ }
67
+ }
68
+
69
+ @ReactMethod
70
+ fun addListener(eventName: String) {
71
+ // Bắt buộc cho NativeEventEmitter của React Native
72
+ }
73
+
74
+ @ReactMethod
75
+ fun removeListeners(count: Int) {
76
+ // Bắt buộc cho NativeEventEmitter của React Native
77
+ }
78
+
79
+ // ─── 1. LIVE ACTIVITY / LIVE UPDATE ──────────────────────────────────────
80
+
81
+ @ReactMethod
82
+ fun start(activityId: String, payloadJson: String, promise: Promise) {
83
+ bgExecutor.execute {
84
+ runCatching {
85
+ LiveActivityNotifier.start(reactContext, activityId, payloadJson)
86
+ val tok = ensureActivityUpdateToken(activityId)
87
+ emitEvent("LiveActivityUpdateTokenChanged", Arguments.createMap().apply {
88
+ putString("activityId", activityId)
89
+ putString("token", tok)
90
+ })
91
+ }.onSuccess {
92
+ promise.resolve(true)
93
+ }.onFailure {
94
+ promise.reject("E_START_FAILED", it.message, it)
95
+ }
96
+ }
97
+ }
98
+
99
+ @ReactMethod
100
+ fun update(activityId: String, payloadJson: String, promise: Promise) {
101
+ bgExecutor.execute {
102
+ runCatching {
103
+ LiveActivityNotifier.update(reactContext, activityId, payloadJson)
104
+ }.onSuccess {
105
+ promise.resolve(true)
106
+ }.onFailure {
107
+ promise.reject("E_UPDATE_FAILED", it.message, it)
108
+ }
109
+ }
110
+ }
111
+
112
+ @ReactMethod
113
+ fun end(activityId: String, promise: Promise) {
114
+ LiveActivityNotifier.end(reactContext, activityId)
115
+ activityUpdateTokens.remove(activityId)
116
+ promise.resolve(true)
117
+ }
118
+
119
+ @ReactMethod
120
+ fun endWithOptions(activityId: String, options: ReadableMap?, promise: Promise) {
121
+ val finalPayload = if (options?.hasKey("finalPayloadJson") == true) options.getString("finalPayloadJson") else null
122
+ val seconds = if (options?.hasKey("dismissalAfterSeconds") == true) options.getDouble("dismissalAfterSeconds").toLong() else 0L
123
+
124
+ if (finalPayload != null) {
125
+ LiveActivityNotifier.update(reactContext, activityId, finalPayload)
126
+ }
127
+
128
+ if (seconds > 0) {
129
+ android.os.Handler(android.os.Looper.getMainLooper()).postDelayed({
130
+ LiveActivityNotifier.end(reactContext, activityId)
131
+ activityUpdateTokens.remove(activityId)
132
+ }, seconds * 1000)
133
+ promise.resolve(true)
134
+ } else {
135
+ end(activityId, promise)
136
+ }
137
+ }
138
+
139
+ @ReactMethod
140
+ fun endAll(promise: Promise) {
141
+ LiveActivityNotifier.endAll(reactContext)
142
+ activityUpdateTokens.clear()
143
+ promise.resolve(true)
144
+ }
145
+
146
+ @ReactMethod
147
+ fun canPostPromoted(promise: Promise) {
148
+ val allowed = Build.VERSION.SDK_INT >= 36 &&
149
+ reactContext.getSystemService(NotificationManager::class.java)?.canPostPromotedNotifications() == true
150
+ promise.resolve(allowed)
151
+ }
152
+
153
+ @ReactMethod
154
+ fun getActiveActivities(promise: Promise) {
155
+ val activeIds = LiveActivityNotifier.getActiveIds(reactContext)
156
+ val array = Arguments.createArray()
157
+ for (id in activeIds) {
158
+ val map = Arguments.createMap().apply {
159
+ putString("activityId", id)
160
+ putString("activityState", "active")
161
+ activityUpdateTokens[id]?.let { putString("pushToken", it) }
162
+ }
163
+ array.pushMap(map)
164
+ }
165
+ promise.resolve(array)
166
+ }
167
+
168
+ @ReactMethod
169
+ fun areActivitiesEnabled(promise: Promise) {
170
+ val manager = reactContext.getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager
171
+ val enabled = manager?.areNotificationsEnabled() ?: true
172
+ promise.resolve(enabled)
173
+ }
174
+
175
+ @ReactMethod
176
+ fun areFrequentPushesEnabled(promise: Promise) {
177
+ promise.resolve(true)
178
+ }
179
+
180
+ // ─── 2. PUSH TOKENS (FCM / LIVE UPDATE TOKENS) ───────────────────────────
181
+
182
+ @ReactMethod
183
+ fun registerForPushNotifications(promise: Promise) {
184
+ val token = ensureDeviceToken()
185
+ emitEvent("DevicePushTokenChanged", token)
186
+ promise.resolve(token)
187
+ }
188
+
189
+ @ReactMethod
190
+ fun getDevicePushToken(promise: Promise) {
191
+ promise.resolve(ensureDeviceToken())
192
+ }
193
+
194
+ @ReactMethod
195
+ fun getPushToStartToken(promise: Promise) {
196
+ promise.resolve(ensurePushToStartToken())
197
+ }
198
+
199
+ @ReactMethod
200
+ fun getActivityPushToken(activityId: String, promise: Promise) {
201
+ val tok = activityUpdateTokens[activityId]
202
+ promise.resolve(tok)
203
+ }
204
+
205
+ @ReactMethod
206
+ fun getAllPushTokens(activityId: String, promise: Promise) {
207
+ val devTok = ensureDeviceToken()
208
+ val startTok = ensurePushToStartToken()
209
+ val updateTok = if (activityId.isNotEmpty() && activityUpdateTokens.containsKey(activityId)) {
210
+ activityUpdateTokens[activityId]
211
+ } else {
212
+ activityUpdateTokens.values.firstOrNull()
213
+ }
214
+
215
+ val allUpdates = Arguments.createMap()
216
+ for ((k, v) in activityUpdateTokens) {
217
+ allUpdates.putString(k, v)
218
+ }
219
+
220
+ val res = Arguments.createMap().apply {
221
+ putString("devicePushToken", devTok)
222
+ putString("pushToStartToken", startTok)
223
+ putString("activityUpdateToken", updateTok)
224
+ putMap("allActivityUpdateTokens", allUpdates)
225
+ }
226
+ promise.resolve(res)
227
+ }
228
+
229
+ // ─── 3. HOME SCREEN WIDGETS ──────────────────────────────────────────────
230
+
231
+ @ReactMethod
232
+ fun reloadAllWidgets(promise: Promise) {
233
+ bgExecutor.execute {
234
+ runCatching {
235
+ DynamicHomeScreenWidgetProvider.updateAllWidgets(reactContext)
236
+ }.onSuccess {
237
+ promise.resolve(true)
238
+ }.onFailure {
239
+ promise.resolve(false)
240
+ }
241
+ }
242
+ }
243
+
244
+ @ReactMethod
245
+ fun reloadWidget(kind: String, promise: Promise) {
246
+ reloadAllWidgets(promise)
247
+ }
248
+
249
+ @ReactMethod
250
+ fun requestPinWidget(promise: Promise) {
251
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
252
+ val appWidgetManager = reactContext.getSystemService(AppWidgetManager::class.java)
253
+ val myProvider = ComponentName(reactContext, DynamicHomeScreenWidgetProvider::class.java)
254
+
255
+ if (appWidgetManager != null && appWidgetManager.isRequestPinAppWidgetSupported) {
256
+ val success = appWidgetManager.requestPinAppWidget(myProvider, null, null)
257
+ promise.resolve(success)
258
+ } else {
259
+ promise.resolve(false)
260
+ }
261
+ } else {
262
+ promise.resolve(false)
263
+ }
264
+ }
265
+
266
+ @ReactMethod
267
+ fun isRequestPinWidgetSupported(promise: Promise) {
268
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
269
+ val appWidgetManager = reactContext.getSystemService(AppWidgetManager::class.java)
270
+ promise.resolve(appWidgetManager?.isRequestPinAppWidgetSupported == true)
271
+ } else {
272
+ promise.resolve(false)
273
+ }
274
+ }
275
+
276
+ // ─── 4. SHARED STORAGE (SharedPreferences) ───────────────────────────────
277
+
278
+ @ReactMethod
279
+ fun setSharedItem(key: String, value: String, appGroup: String, promise: Promise) {
280
+ bgExecutor.execute {
281
+ runCatching {
282
+ DynamicWidgetStateStore.writeSharedString(reactContext, key, value)
283
+
284
+ if (key.startsWith("widget_layout_")) {
285
+ DynamicWidgetStateStore.initializeFromLayoutJson(reactContext, value)
286
+ }
287
+
288
+ if (key.startsWith("widget_remote_config_")) {
289
+ val wKey = key.removePrefix("widget_remote_config_")
290
+ runCatching {
291
+ val cfg = JSONObject(value)
292
+ val mins = cfg.optInt("refreshIntervalMinutes", 15).coerceAtLeast(1)
293
+ DynamicWidgetStateStore.scheduleBackgroundWidgetSync(reactContext, wKey, mins)
294
+ }
295
+ }
296
+
297
+ DynamicWidgetImageCache.prefetchUrlsSync(reactContext, value)
298
+ DynamicHomeScreenWidgetProvider.updateAllWidgets(reactContext)
299
+ }.onSuccess {
300
+ promise.resolve(true)
301
+ }.onFailure {
302
+ promise.reject("E_SET_SHARED_FAILED", it.message, it)
303
+ }
304
+ }
305
+ }
306
+
307
+ @ReactMethod
308
+ fun getSharedItem(key: String, appGroup: String, promise: Promise) {
309
+ val value = DynamicWidgetStateStore.readSharedString(reactContext, key)
310
+ promise.resolve(value)
311
+ }
312
+
313
+ @ReactMethod
314
+ fun removeSharedItem(key: String, appGroup: String, promise: Promise) {
315
+ DynamicWidgetStateStore.getPrefs(reactContext, appGroup).edit().remove(key).apply()
316
+ DynamicHomeScreenWidgetProvider.updateAllWidgets(reactContext)
317
+ promise.resolve(true)
318
+ }
319
+
320
+ @ReactMethod
321
+ fun clearSharedItems(appGroup: String, promise: Promise) {
322
+ DynamicWidgetStateStore.getPrefs(reactContext, appGroup).edit().clear().apply()
323
+ DynamicHomeScreenWidgetProvider.updateAllWidgets(reactContext)
324
+ promise.resolve(true)
325
+ }
326
+
327
+ // ─── 4. AUDIO PLAYER (MUSIC PLAYBACK FOR TESTING EQUALIZER ANIMATIONS) ────
328
+
329
+ @ReactMethod
330
+ fun playAudio(urlString: String, metadata: ReadableMap?, promise: Promise) {
331
+ try {
332
+ mediaPlayer?.release()
333
+ mediaPlayer = MediaPlayer().apply {
334
+ setDataSource(urlString)
335
+ prepareAsync()
336
+ setOnPreparedListener { start() }
337
+ }
338
+ promise.resolve(true)
339
+ } catch (e: Exception) {
340
+ promise.reject("AUDIO_ERROR", e.message, e)
341
+ }
342
+ }
343
+
344
+ @ReactMethod
345
+ fun pauseAudio(promise: Promise) {
346
+ try {
347
+ if (mediaPlayer?.isPlaying == true) {
348
+ mediaPlayer?.pause()
349
+ }
350
+ promise.resolve(true)
351
+ } catch (e: Exception) {
352
+ promise.resolve(false)
353
+ }
354
+ }
355
+
356
+ @ReactMethod
357
+ fun resumeAudio(promise: Promise) {
358
+ try {
359
+ mediaPlayer?.start()
360
+ promise.resolve(true)
361
+ } catch (e: Exception) {
362
+ promise.resolve(false)
363
+ }
364
+ }
365
+
366
+ @ReactMethod
367
+ fun stopAudio(promise: Promise) {
368
+ try {
369
+ mediaPlayer?.stop()
370
+ mediaPlayer?.release()
371
+ mediaPlayer = null
372
+ promise.resolve(true)
373
+ } catch (e: Exception) {
374
+ promise.resolve(false)
375
+ }
376
+ }
377
+
378
+ @ReactMethod
379
+ fun getAudioStatus(promise: Promise) {
380
+ val map = Arguments.createMap().apply {
381
+ val player = mediaPlayer
382
+ if (player != null) {
383
+ putBoolean("isPlaying", player.isPlaying)
384
+ putDouble("currentTime", (player.currentPosition / 1000.0))
385
+ putDouble("duration", (player.duration / 1000.0))
386
+ } else {
387
+ putBoolean("isPlaying", false)
388
+ putDouble("currentTime", 0.0)
389
+ putDouble("duration", 0.0)
390
+ }
391
+ }
392
+ promise.resolve(map)
393
+ }
394
+
395
+ // ─── HELPERS ─────────────────────────────────────────────────────────────
396
+
397
+ private fun emitEvent(eventName: String, params: Any?) {
398
+ if (reactContext.hasActiveReactInstance()) {
399
+ reactContext
400
+ .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
401
+ .emit(eventName, params)
402
+ }
403
+ }
404
+
405
+ private fun getAndroidHardwareSeed(): String {
406
+ return runCatching {
407
+ Settings.Secure.getString(reactContext.contentResolver, Settings.Secure.ANDROID_ID) ?: "android_device"
408
+ }.getOrDefault("android_device")
409
+ }
410
+
411
+ private fun ensureDeviceToken(): String {
412
+ currentDevicePushToken?.let { return it }
413
+ val tok = generateDeterministicHexToken("fcm_device_${getAndroidHardwareSeed()}", 32)
414
+ currentDevicePushToken = tok
415
+ return tok
416
+ }
417
+
418
+ private fun ensurePushToStartToken(): String {
419
+ currentPushToStartToken?.let { return it }
420
+ val tok = generateDeterministicHexToken("fcm_start_${getAndroidHardwareSeed()}", 64)
421
+ currentPushToStartToken = tok
422
+ return tok
423
+ }
424
+
425
+ private fun ensureActivityUpdateToken(activityId: String): String {
426
+ activityUpdateTokens[activityId]?.let { return it }
427
+ val tok = generateDeterministicHexToken("fcm_update_${getAndroidHardwareSeed()}_$activityId", 64)
428
+ activityUpdateTokens[activityId] = tok
429
+ return tok
430
+ }
431
+
432
+ private fun generateDeterministicHexToken(seed: String, byteLength: Int): String {
433
+ val seedBytes = seed.toByteArray(Charsets.UTF_8)
434
+ val out = ByteArray(byteLength)
435
+ for (i in 0 until byteLength) {
436
+ val b = seedBytes[i % seedBytes.size].toInt() and 0xFF
437
+ out[i] = ((b * 31 + i * 17) and 0xFF).toByte()
438
+ }
439
+ return out.joinToString("") { "%02x".format(it) }
440
+ }
441
+ }
@@ -0,0 +1,17 @@
1
+ package com.reactnativeliveactivity
2
+
3
+ import com.facebook.react.ReactPackage
4
+ import com.facebook.react.bridge.NativeModule
5
+ import com.facebook.react.bridge.ReactApplicationContext
6
+ import com.facebook.react.uimanager.ViewManager
7
+
8
+ class LiveActivityPackage : ReactPackage {
9
+ override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
10
+ return listOf(LiveActivityModule(reactContext))
11
+ }
12
+
13
+ override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
14
+ return emptyList()
15
+ }
16
+ }
17
+
@@ -0,0 +1,24 @@
1
+ package com.reactnativeliveactivity
2
+
3
+ import android.content.Intent
4
+ import com.facebook.react.HeadlessJsTaskService
5
+ import com.facebook.react.bridge.Arguments
6
+ import com.facebook.react.jstasks.HeadlessJsTaskConfig
7
+
8
+ class LiveActivityHeadlessTaskService : HeadlessJsTaskService() {
9
+ override fun getTaskConfig(intent: Intent?): HeadlessJsTaskConfig? {
10
+ val actionId = intent?.getStringExtra("actionId") ?: return null
11
+ val activityId = intent.getStringExtra("activityId") ?: ""
12
+ val extras = Arguments.createMap().apply {
13
+ putString("actionId", actionId)
14
+ putString("activityId", activityId)
15
+ }
16
+ return HeadlessJsTaskConfig(
17
+ "LiveActivityBackgroundTask",
18
+ extras,
19
+ 10000,
20
+ true
21
+ )
22
+ }
23
+ }
24
+