@cometchat/chat-uikit-react-native 5.5.4 → 5.7.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 (215) hide show
  1. package/android/src/main/java/com/reactnativecometchatuikit/CometChatKeyboardModule.kt +101 -0
  2. package/android/src/main/java/com/reactnativecometchatuikit/CometchatUiKitPackage.java +1 -0
  3. package/android/src/main/java/com/reactnativecometchatuikit/FileManager.java +78 -3
  4. package/android/src/main/java/com/reactnativecometchatuikit/KeyboardDirectionWatcher.kt +64 -0
  5. package/android/src/main/java/com/reactnativecometchatuikit/KeyboardLanguageDirection.kt +44 -0
  6. package/android/src/main/java/com/reactnativecometchatuikit/RichTextEditorView.kt +287 -49
  7. package/android/src/main/java/com/reactnativecometchatuikit/SafeFileName.java +78 -0
  8. package/ios/CCLocalFileGuard.h +19 -0
  9. package/ios/CCLocalFileGuard.m +34 -0
  10. package/ios/CometChatKeyboardModule.m +9 -0
  11. package/ios/CometChatKeyboardModule.swift +71 -0
  12. package/ios/FileManager.m +70 -6
  13. package/ios/KeyboardLanguageDirection.swift +24 -0
  14. package/ios/RichTextEditorView.swift +253 -51
  15. package/ios/RichTextEditorViewManager.m +1 -0
  16. package/ios/VideoManager.m +1 -1
  17. package/package.json +18 -3
  18. package/src/CometChatAIAssistantChatHistory/CometChatAIAssistantChatHistory.tsx +41 -57
  19. package/src/CometChatAIAssistantChatHistory/Skeleton.tsx +4 -1
  20. package/src/CometChatAIAssistantChatHistory/style.ts +5 -2
  21. package/src/CometChatCompactMessageComposer/CometChatCompactMessageComposer.tsx +145 -63
  22. package/src/CometChatCompactMessageComposer/styles.ts +7 -7
  23. package/src/CometChatConversations/CometChatConversations.tsx +276 -147
  24. package/src/CometChatConversations/Skeleton.tsx +4 -1
  25. package/src/CometChatConversations/style.ts +1 -1
  26. package/src/CometChatGroupMembers/CometChatGroupMembers.tsx +39 -18
  27. package/src/CometChatGroupMembers/Skeleton.tsx +4 -1
  28. package/src/CometChatGroupMembers/style.ts +6 -4
  29. package/src/CometChatGroups/CometChatGroups.tsx +74 -58
  30. package/src/CometChatGroups/GroupsStyle.ts +1 -1
  31. package/src/CometChatGroups/Skeleton.tsx +4 -1
  32. package/src/CometChatMessageComposer/CometChatMessageComposer.tsx +190 -96
  33. package/src/CometChatMessageComposer/styles.tsx +1 -1
  34. package/src/CometChatMessageHeader/CometChatMessageHeader.tsx +28 -15
  35. package/src/CometChatMessageHeader/listners.ts +76 -56
  36. package/src/CometChatMessageInformation/CometChatMessageInformation.tsx +8 -1
  37. package/src/CometChatMessageInformation/Skeleton.tsx +4 -1
  38. package/src/CometChatMessageList/CometChatMessageList.tsx +1465 -313
  39. package/src/CometChatMessageList/SwipeToReplyView.tsx +61 -0
  40. package/src/CometChatMessageList/style.ts +32 -32
  41. package/src/CometChatMessageList/swipeToReply.ts +25 -0
  42. package/src/CometChatNotificationFeed/CometChatNotificationFeed.tsx +81 -10
  43. package/src/CometChatNotificationFeed/style.ts +1 -1
  44. package/src/CometChatNotificationFeed/utils.ts +17 -18
  45. package/src/CometChatPinnedMessages/CometChatPinnedMessages.tsx +82 -3
  46. package/src/CometChatPinnedMessages/style.ts +5 -5
  47. package/src/CometChatRichTextEditor/RichTextEditorViewNativeComponent.ts +2 -0
  48. package/src/CometChatRichTextEditor/index.tsx +3 -0
  49. package/src/CometChatSavedMessages/CometChatSavedMessages.tsx +83 -15
  50. package/src/CometChatSavedMessages/style.ts +3 -3
  51. package/src/CometChatSearch/CometChatSearch.tsx +391 -136
  52. package/src/CometChatSearch/Skeleton.tsx +4 -1
  53. package/src/CometChatSearch/searchLabels.ts +26 -0
  54. package/src/CometChatSearch/style.ts +7 -7
  55. package/src/CometChatThreadHeader/CometChatThreadHeader.tsx +34 -8
  56. package/src/CometChatThreadHeader/Header.tsx +2 -2
  57. package/src/CometChatUsers/CometChatUsers.tsx +10 -1
  58. package/src/CometChatUsers/Skeleton.tsx +4 -1
  59. package/src/CometChatUsers/style.ts +1 -1
  60. package/src/calls/CometChatCallButtons/CometChatCallButtons.tsx +27 -1
  61. package/src/calls/CometChatCallLogs/CometChatCallLogs.tsx +16 -4
  62. package/src/calls/CometChatCallLogs/Skeleton.tsx +4 -1
  63. package/src/calls/CometChatIncomingCall/CometChatIncomingCall.tsx +11 -3
  64. package/src/calls/CometChatOutgoingCall/CometChatOutgoingCall.tsx +10 -2
  65. package/src/extensions/CollaborativeBubble/style.ts +4 -4
  66. package/src/extensions/CollaborativeDocument/CollaborativeDocumentExtensionDecorator.tsx +1 -1
  67. package/src/extensions/CollaborativeWhiteboard/CollaborativeWhiteboardExtensionDecorator.tsx +1 -1
  68. package/src/extensions/LinkPreview/LinkPreviewBubble.tsx +11 -6
  69. package/src/extensions/LinkPreview/style.ts +6 -6
  70. package/src/extensions/Polls/Header.tsx +1 -1
  71. package/src/extensions/Polls/Polls.tsx +25 -12
  72. package/src/extensions/Polls/PollsBubble.tsx +10 -6
  73. package/src/extensions/Polls/style.ts +4 -4
  74. package/src/extensions/Stickers/CometChatStickerKeyboard/CometChatStickerKeyboard.tsx +2 -2
  75. package/src/extensions/Stickers/CometChatStickerKeyboard/Skeleton.tsx +1 -1
  76. package/src/extensions/Stickers/CometChatStickerKeyboard/style.ts +1 -1
  77. package/src/extensions/Stickers/StickersExtensionDecorator.tsx +4 -2
  78. package/src/index.ts +11 -1
  79. package/src/shared/CometChatUiKit/CometChatUIKit.ts +102 -2
  80. package/src/shared/CometChatUiKit/UIKitSettings.ts +28 -0
  81. package/src/shared/events/CometChatUIEventHandler/Listener.ts +10 -0
  82. package/src/shared/events/index.ts +4 -0
  83. package/src/shared/events/offline.ts +28 -0
  84. package/src/shared/formatters/CometChatMentionsFormatter/CometChatMentionsFormatter.tsx +18 -13
  85. package/src/shared/formatters/CometChatRichTextFormatter/index.tsx +15 -7
  86. package/src/shared/formatters/CometChatTextFormatter.ts +11 -0
  87. package/src/shared/formatters/CometChatUrlsFormatter/index.tsx +7 -4
  88. package/src/shared/framework/MessageDataSource.tsx +51 -108
  89. package/src/shared/helper/LocalizedDateHelper.ts +64 -4
  90. package/src/shared/helper/helperFunctions.ts +71 -4
  91. package/src/shared/helper/keyboardDirection.ts +54 -0
  92. package/src/shared/helper/layoutDirection.ts +205 -0
  93. package/src/shared/helper/pluralRules.ts +67 -0
  94. package/src/shared/helper/textDirection.ts +118 -0
  95. package/src/shared/icons/Icon.tsx +37 -2
  96. package/src/shared/icons/components/format-list-numbered-rtl.tsx +15 -0
  97. package/src/shared/index.ts +2 -0
  98. package/src/shared/offline/OfflineHub.ts +651 -0
  99. package/src/shared/offline/OfflineMediaBubble.tsx +49 -0
  100. package/src/shared/offline/OfflineViews.tsx +222 -0
  101. package/src/shared/offline/conversationPins.ts +137 -0
  102. package/src/shared/offline/conversations.ts +325 -0
  103. package/src/shared/offline/frameProbe.ts +24 -0
  104. package/src/shared/offline/gate.ts +94 -0
  105. package/src/shared/offline/groupActions.ts +81 -0
  106. package/src/shared/offline/identity.ts +162 -0
  107. package/src/shared/offline/index.ts +76 -0
  108. package/src/shared/offline/media.ts +247 -0
  109. package/src/shared/offline/notices.ts +84 -0
  110. package/src/shared/offline/reconcile.ts +296 -0
  111. package/src/shared/offline/searchOutcome.ts +62 -0
  112. package/src/shared/offline/send.ts +430 -0
  113. package/src/shared/offline/stateContract.ts +85 -0
  114. package/src/shared/offline/threadReplies.ts +125 -0
  115. package/src/shared/offline/twoPhase.ts +372 -0
  116. package/src/shared/offline/unsentActions.ts +168 -0
  117. package/src/shared/offline/useOfflineMediaUri.ts +87 -0
  118. package/src/shared/offline/useOfflineReconnect.ts +25 -0
  119. package/src/shared/offline/useOfflineSessionEnded.ts +22 -0
  120. package/src/shared/offline/useOfflineUploadTray.ts +76 -0
  121. package/src/shared/offline/usePresenceHidden.ts +33 -0
  122. package/src/shared/resources/CometChatLocalizeNew/CometChatLocalizationHelper.ts +2 -0
  123. package/src/shared/resources/CometChatLocalizeNew/resources/ar/translation.json +720 -0
  124. package/src/shared/resources/CometChatLocalizeNew/resources/de/translation.json +74 -3
  125. package/src/shared/resources/CometChatLocalizeNew/resources/en/translation.json +75 -4
  126. package/src/shared/resources/CometChatLocalizeNew/resources/es/translation.json +74 -3
  127. package/src/shared/resources/CometChatLocalizeNew/resources/fr/translation.json +75 -4
  128. package/src/shared/resources/CometChatLocalizeNew/resources/hi/translation.json +74 -3
  129. package/src/shared/resources/CometChatLocalizeNew/resources/hu/translation.json +74 -3
  130. package/src/shared/resources/CometChatLocalizeNew/resources/it/translation.json +74 -3
  131. package/src/shared/resources/CometChatLocalizeNew/resources/ja/translation.json +74 -3
  132. package/src/shared/resources/CometChatLocalizeNew/resources/ko/translation.json +74 -3
  133. package/src/shared/resources/CometChatLocalizeNew/resources/lt/translation.json +82 -3
  134. package/src/shared/resources/CometChatLocalizeNew/resources/ms/translation.json +74 -3
  135. package/src/shared/resources/CometChatLocalizeNew/resources/nl/translation.json +74 -3
  136. package/src/shared/resources/CometChatLocalizeNew/resources/pt/translation.json +74 -3
  137. package/src/shared/resources/CometChatLocalizeNew/resources/ru/translation.json +90 -3
  138. package/src/shared/resources/CometChatLocalizeNew/resources/sv/translation.json +74 -3
  139. package/src/shared/resources/CometChatLocalizeNew/resources/tr/translation.json +74 -3
  140. package/src/shared/resources/CometChatLocalizeNew/resources/zh/translation.json +74 -3
  141. package/src/shared/resources/CometChatLocalizeNew/resources/zh-tw/translation.json +74 -3
  142. package/src/shared/services/UploadQueueService.ts +106 -1
  143. package/src/shared/utils/CometChatMessagePreview/CometChatMessagePreview.tsx +13 -12
  144. package/src/shared/utils/CometChatMessagePreview/style.ts +1 -1
  145. package/src/shared/utils/MessageReceiptUtils.ts +8 -0
  146. package/src/shared/utils/MessageUtils.tsx +14 -14
  147. package/src/shared/utils/PermissionUtil.ts +16 -5
  148. package/src/shared/utils/SavedMessagesHelper.ts +18 -1
  149. package/src/shared/utils/attachmentLimits.ts +3 -2
  150. package/src/shared/utils/catchUpCursor.ts +36 -0
  151. package/src/shared/utils/conversationUtils.ts +3 -5
  152. package/src/shared/utils/failedMessageRetry.ts +73 -0
  153. package/src/shared/utils/fileIconFontSize.ts +10 -0
  154. package/src/shared/utils/fileSizeLabel.ts +16 -0
  155. package/src/shared/utils/groupActionText.ts +75 -0
  156. package/src/shared/utils/messageIdentity.ts +38 -0
  157. package/src/shared/views/CometChatAIAssistantMessageBubble/CometChatAIAssistantMessageBubble.tsx +12 -8
  158. package/src/shared/views/CometChatAttachmentPreview/CometChatAttachmentPreview.style.ts +1 -1
  159. package/src/shared/views/CometChatAttachmentPreview/CometChatAttachmentPreviewItem.tsx +5 -3
  160. package/src/shared/views/CometChatAttachmentPreview/CometChatAttachmentTile.tsx +22 -17
  161. package/src/shared/views/CometChatAttachmentViewer/CometChatAttachmentViewer.tsx +45 -12
  162. package/src/shared/views/CometChatAudioBubble/CometChatAudioBubble.tsx +16 -10
  163. package/src/shared/views/CometChatAudiosBubble/CometChatAudiosBubble.tsx +20 -8
  164. package/src/shared/views/CometChatBadge/CometChatBadge.tsx +8 -4
  165. package/src/shared/views/CometChatBottomSheet/CometChatBottomSheet.tsx +6 -2
  166. package/src/shared/views/CometChatComposerErrorBanner.tsx +3 -1
  167. package/src/shared/views/CometChatConfirmDialog/CometChatConfirmDialog.tsx +8 -3
  168. package/src/shared/views/CometChatConversationStarter/CometChatConversationStarter.tsx +2 -2
  169. package/src/shared/views/CometChatEmojiKeyboard/CometChatEmojiKeyboard.tsx +5 -3
  170. package/src/shared/views/CometChatEmojiKeyboard/emojis.ts +8 -8
  171. package/src/shared/views/CometChatEmojiKeyboard/style.ts +2 -2
  172. package/src/shared/views/CometChatFileBubble/CometChatFileBubble.tsx +13 -27
  173. package/src/shared/views/CometChatFilesBubble/CometChatFilesBubble.tsx +65 -15
  174. package/src/shared/views/CometChatImageViewerModal/ImageViewerModal.tsx +7 -3
  175. package/src/shared/views/CometChatImagesBubble/CometChatImagesBubble.tsx +56 -25
  176. package/src/shared/views/CometChatInlineAudioRecorder/CometChatInlineAudioRecorder.tsx +41 -21
  177. package/src/shared/views/CometChatInlineAudioRecorder/useAudioRecorder.ts +7 -3
  178. package/src/shared/views/CometChatLinkConfirmPopup/CometChatLinkConfirmPopup.tsx +7 -3
  179. package/src/shared/views/CometChatList/CometChatList.tsx +478 -21
  180. package/src/shared/views/CometChatList/Header.tsx +5 -2
  181. package/src/shared/views/CometChatList/sectionLetter.ts +44 -0
  182. package/src/shared/views/CometChatList/styles.ts +7 -7
  183. package/src/shared/views/CometChatListItem/CometChatListItem.tsx +6 -4
  184. package/src/shared/views/CometChatMediaRecorder/CometChatAudioPlayer.tsx +16 -10
  185. package/src/shared/views/CometChatMediaRecorder/Timer.tsx +5 -1
  186. package/src/shared/views/CometChatMediaViewer/CometChatMediaViewer.tsx +43 -16
  187. package/src/shared/views/CometChatMessageBubble/CometChatMessageBubble.tsx +24 -4
  188. package/src/shared/views/CometChatMessageInput/CometChatMessageInput.tsx +19 -2
  189. package/src/shared/views/CometChatNewMessageIndicator/CometChatNewMessageIndicator.tsx +3 -2
  190. package/src/shared/views/CometChatReactionList/CometChatReactionList.tsx +59 -12
  191. package/src/shared/views/CometChatReactionList/ReactionListStyle.tsx +2 -2
  192. package/src/shared/views/CometChatReactionList/Skeleton.tsx +4 -1
  193. package/src/shared/views/CometChatReactions/CometChatReactions.tsx +6 -2
  194. package/src/shared/views/CometChatReportDialog/CometChatReportDialog.tsx +7 -3
  195. package/src/shared/views/CometChatReportDialog/style.ts +1 -1
  196. package/src/shared/views/CometChatSmartReplies/CometChatSmartReplies.tsx +1 -1
  197. package/src/shared/views/CometChatStatusIndicator/CometChatStatusIndicator.tsx +4 -0
  198. package/src/shared/views/CometChatStreamMessageBubble/CometChatStreamMessageBubble.tsx +20 -13
  199. package/src/shared/views/CometChatSuggestionList/CometChatSuggestionList.tsx +3 -2
  200. package/src/shared/views/CometChatSuggestionList/Skeleton.tsx +1 -1
  201. package/src/shared/views/CometChatTextBubble/CometChatTextBubble.tsx +29 -3
  202. package/src/shared/views/CometChatTooltipMenu/CometChatTooltipMenu.tsx +20 -30
  203. package/src/shared/views/CometChatTooltipMenu/tooltipPosition.ts +43 -0
  204. package/src/shared/views/CometChatVideoBubble/style.ts +2 -2
  205. package/src/shared/views/CometChatVideoPlayer/CometChatVideoPlayer.tsx +4 -1
  206. package/src/shared/views/CometChatVideosBubble/CometChatVideosBubble.tsx +58 -24
  207. package/src/shared/views/CometChatVoiceNoteBubble/CometChatVoiceNoteBubble.tsx +24 -12
  208. package/src/shared/views/SenderLabel.tsx +41 -0
  209. package/src/theme/default/default.ts +1 -1
  210. package/src/theme/type.ts +17 -0
  211. package/android/.gradle/7.4/checksums/checksums.lock +0 -0
  212. package/android/.gradle/7.4/fileChanges/last-build.bin +0 -0
  213. package/android/.gradle/7.4/fileHashes/fileHashes.lock +0 -0
  214. package/android/.gradle/7.4/gc.properties +0 -0
  215. package/android/.gradle/vcs-1/gc.properties +0 -0
@@ -0,0 +1,101 @@
1
+ package com.reactnativecometchatuikit
2
+
3
+ import android.os.Handler
4
+ import android.os.Looper
5
+ import com.facebook.react.bridge.Arguments
6
+ import com.facebook.react.bridge.LifecycleEventListener
7
+ import com.facebook.react.bridge.Promise
8
+ import com.facebook.react.bridge.ReactApplicationContext
9
+ import com.facebook.react.bridge.ReactContextBaseJavaModule
10
+ import com.facebook.react.bridge.ReactMethod
11
+ import com.facebook.react.modules.core.DeviceEventManagerModule
12
+
13
+ /**
14
+ * ENG-57281: the keyboard's direction for the standard composer (a React Native TextInput), which
15
+ * cannot see the keyboard from JS. JS listens only while the input is focused with no letter yet.
16
+ */
17
+ class CometChatKeyboardModule(reactContext: ReactApplicationContext)
18
+ : ReactContextBaseJavaModule(reactContext), LifecycleEventListener {
19
+
20
+ override fun getName() = "CometChatKeyboardModule"
21
+
22
+ companion object {
23
+ const val EVENT = "keyboardDirectionChange"
24
+ }
25
+
26
+ /** The keyboard's direction ("rtl"/"ltr"/null). A seam so tests can stand in a keyboard. */
27
+ internal var readDirection: () -> String? = { KeyboardLanguageDirection.current(reactApplicationContext) }
28
+
29
+ private val main = Handler(Looper.getMainLooper())
30
+ // Touched on the main thread only.
31
+ private var listeners = 0
32
+ private var paused = false
33
+
34
+ // Polls only while JS listens (a focused standard composer with no letter yet) and the app is in
35
+ // the foreground.
36
+ private val watcher = KeyboardDirectionWatcher(
37
+ post = { task, delay -> main.postDelayed(task, delay) },
38
+ cancel = { main.removeCallbacks(it) },
39
+ read = { when (readDirection()) { "rtl" -> true; "ltr" -> false; else -> null } },
40
+ shouldPoll = { listeners > 0 && !paused },
41
+ onChange = { rtl -> emit(if (rtl) "rtl" else "ltr") },
42
+ )
43
+
44
+ // After the fields above: registering can call onHostResume straight away.
45
+ init {
46
+ reactContext.addLifecycleEventListener(this)
47
+ }
48
+
49
+ private fun emit(direction: String) {
50
+ if (!reactApplicationContext.hasActiveReactInstance()) return
51
+ val body = Arguments.createMap().apply { putString("direction", direction) }
52
+ reactApplicationContext
53
+ .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
54
+ .emit(EVENT, body)
55
+ }
56
+
57
+ @ReactMethod
58
+ fun getKeyboardDirection(promise: Promise) {
59
+ try {
60
+ promise.resolve(readDirection())
61
+ } catch (e: Exception) {
62
+ promise.reject("ERROR", e.message)
63
+ }
64
+ }
65
+
66
+ // NativeEventEmitter calls these when JS adds and removes listeners.
67
+ @ReactMethod
68
+ fun addListener(eventName: String) {
69
+ main.post {
70
+ if (listeners++ == 0) watcher.prime()
71
+ }
72
+ }
73
+
74
+ @ReactMethod
75
+ fun removeListeners(count: Int) {
76
+ main.post {
77
+ listeners = (listeners - count).coerceAtLeast(0)
78
+ watcher.schedule()
79
+ }
80
+ }
81
+
82
+ // Backgrounded: no polling. Back: resume if JS still listens.
83
+ override fun onHostPause() {
84
+ main.post { paused = true; watcher.schedule() }
85
+ }
86
+
87
+ override fun onHostResume() {
88
+ main.post { paused = false; watcher.schedule() }
89
+ }
90
+
91
+ override fun onHostDestroy() {
92
+ main.post { paused = true; watcher.schedule() }
93
+ }
94
+
95
+ override fun invalidate() {
96
+ // Also stops add/resume tasks already queued with main.post from restarting the poll.
97
+ watcher.invalidate()
98
+ reactApplicationContext.removeLifecycleEventListener(this)
99
+ super.invalidate()
100
+ }
101
+ }
@@ -27,6 +27,7 @@ public class CometchatUiKitPackage implements ReactPackage {
27
27
  modules.add(new TimeZoneCodeManager(reactContext));
28
28
  modules.add(new ProximityModule(reactContext));
29
29
  modules.add(new CometChatClipboardModule(reactContext));
30
+ modules.add(new CometChatKeyboardModule(reactContext));
30
31
  return modules;
31
32
  }
32
33
 
@@ -47,6 +47,7 @@ import com.facebook.react.bridge.ReactApplicationContext;
47
47
  import com.facebook.react.bridge.ReactContextBaseJavaModule;
48
48
  import com.facebook.react.bridge.ReactMethod;
49
49
  import com.facebook.react.bridge.ReadableMap;
50
+ import com.facebook.react.bridge.UiThreadUtil;
50
51
  import com.facebook.react.bridge.WritableMap;
51
52
  import com.facebook.react.bridge.WritableArray;
52
53
  import com.facebook.react.bridge.WritableNativeMap;
@@ -306,7 +307,11 @@ public class FileManager extends ReactContextBaseJavaModule {
306
307
  if (fileName == null) {
307
308
  fileName = String.valueOf(System.currentTimeMillis());
308
309
  }
309
- File destFile = new File(dir, fileName);
310
+ String safeName = SafeFileName.of(fileName);
311
+ if (safeName == null) {
312
+ throw new IOException("unsafe file name");
313
+ }
314
+ File destFile = new File(dir, safeName);
310
315
  Uri copyPath = copyFile(context, uri, destFile);
311
316
  map.putString(FIELD_FILE_COPY_URI, copyPath.toString());
312
317
  } catch (Exception e) {
@@ -445,10 +450,19 @@ public class FileManager extends ReactContextBaseJavaModule {
445
450
  }
446
451
 
447
452
  public static void downloadFileInNewThread(Context context, String fileUrl, String fileName, String mimeType) {
448
- File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName);
453
+ String safeName = SafeFileName.of(fileName);
454
+ if (safeName == null) {
455
+ handleDownloadFailure(context);
456
+ return;
457
+ }
458
+ File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), safeName);
449
459
  Thread downloadThread = new Thread(() -> {
450
460
  try {
451
- if (!file.exists()) {
461
+ if (!file.exists() && SafeFileName.isFileUri(fileUrl)) {
462
+ // A file already on the device: copied only from the offline media cache, no fetch.
463
+ copyLocalFile(context, fileUrl, file);
464
+ handleDownloadSuccess(context, mimeType, file);
465
+ } else if (!file.exists()) {
452
466
  URL url = new URL(fileUrl);
453
467
  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
454
468
  connection.connect();
@@ -485,6 +499,37 @@ public class FileManager extends ReactContextBaseJavaModule {
485
499
  downloadThread.start();
486
500
  }
487
501
 
502
+ /**
503
+ * Where the offline Chat SDK keeps media (MediaFileStore): `<dataDir>/no_backup/cometchat/media`
504
+ * (the folder op-sqlite reports as ANDROID_FILES_PATH, one level up, then `no_backup`), or
505
+ * `<files>/cometchat/media` without op-sqlite and before the SDK's one-time move to no_backup.
506
+ * Its database (`no_backup/databases`) and every other private file are outside both.
507
+ */
508
+ private static File[] offlineMediaRoots(Context context) {
509
+ File files = context.getFilesDir();
510
+ return new File[] {
511
+ new File(files.getParentFile(), "no_backup/cometchat/media"),
512
+ new File(files, "cometchat/media"),
513
+ };
514
+ }
515
+
516
+ private static void copyLocalFile(Context context, String fileUri, File dest) throws IOException {
517
+ // A file:// source is the message's data: copy it only when it resolves inside the offline
518
+ // media cache, never any other file the app can read.
519
+ File source = SafeFileName.confinedLocalFile(fileUri, offlineMediaRoots(context));
520
+ if (source == null) {
521
+ throw new IOException("source is not in the offline media cache");
522
+ }
523
+ try (InputStream in = new FileInputStream(source); OutputStream out = new FileOutputStream(dest)) {
524
+ byte[] buffer = new byte[4096];
525
+ int read;
526
+ while ((read = in.read(buffer)) != -1) {
527
+ out.write(buffer, 0, read);
528
+ }
529
+ out.flush();
530
+ }
531
+ }
532
+
488
533
  private static void handleDownloadSuccess(Context context, String mimeType, final File file) {
489
534
  ((Activity) context).runOnUiThread(() -> {
490
535
  shareFile(mimeType, context, file);
@@ -1805,12 +1850,31 @@ public class FileManager extends ReactContextBaseJavaModule {
1805
1850
  @ReactMethod
1806
1851
  public void checkAndDownload(String url, String name, Callback callback) {
1807
1852
  try {
1853
+ name = SafeFileName.of(name);
1854
+ if (name == null) {
1855
+ callback.invoke("{\"success\": false, \"error\": \"Invalid file name\"}");
1856
+ return;
1857
+ }
1808
1858
  if (fileExists(name)) {
1809
1859
  // Already downloaded — open it and report done
1810
1860
  openFileWithOption(name, ignored -> {});
1811
1861
  callback.invoke("{\"success\": true, \"alreadyExists\": true}");
1812
1862
  return;
1813
1863
  }
1864
+ if (SafeFileName.isFileUri(url)) {
1865
+ // A file already on the device: copied into Downloads only from the offline media cache.
1866
+ File dest = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), name);
1867
+ try {
1868
+ copyLocalFile(getReactApplicationContext(), url, dest);
1869
+ } catch (IOException e) {
1870
+ Log.w(TAG, "checkAndDownload: refused a local file outside the offline media cache");
1871
+ UiThreadUtil.runOnUiThread(() -> Toast.makeText(getReactApplicationContext(), "File download failed.", Toast.LENGTH_SHORT).show());
1872
+ callback.invoke("{\"success\": false, \"error\": \"File is not available\"}");
1873
+ return;
1874
+ }
1875
+ callback.invoke("{\"success\": true, \"alreadyExists\": false}");
1876
+ return;
1877
+ }
1814
1878
  Long downloadId = downloadFile(name, url);
1815
1879
  if (downloadId == null) {
1816
1880
  callback.invoke("{\"success\": false, \"error\": \"Download could not be started\"}");
@@ -1837,6 +1901,11 @@ public class FileManager extends ReactContextBaseJavaModule {
1837
1901
  @ReactMethod
1838
1902
  public void openFileWithOption(String fileName, Callback callback) {
1839
1903
  try {
1904
+ fileName = SafeFileName.of(fileName);
1905
+ if (fileName == null) {
1906
+ callback.invoke("{\"success\": false, \"error\": \"Invalid file name\"}");
1907
+ return;
1908
+ }
1840
1909
  // Get the Downloads directory path
1841
1910
  File downloadsDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
1842
1911
  File file = new File(downloadsDir, fileName);
@@ -1910,12 +1979,18 @@ public class FileManager extends ReactContextBaseJavaModule {
1910
1979
  }
1911
1980
 
1912
1981
  private boolean fileExists(String fileName) {
1982
+ fileName = SafeFileName.of(fileName);
1983
+ if (fileName == null) return false;
1913
1984
  File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName);
1914
1985
  return f.exists();
1915
1986
  }
1916
1987
 
1917
1988
  private Long downloadFile(String fileName, String url){
1918
1989
  try {
1990
+ fileName = SafeFileName.of(fileName);
1991
+ if (fileName == null) {
1992
+ return null;
1993
+ }
1919
1994
  if (fileExists(fileName)) {
1920
1995
  return null;
1921
1996
  }
@@ -0,0 +1,64 @@
1
+ package com.reactnativecometchatuikit
2
+
3
+ /**
4
+ * ENG-57281: re-reads the keyboard language's direction on a timer, for putting an empty composer's
5
+ * caret on that side. Shared by RichTextEditorView (compact composer) and CometChatKeyboardModule
6
+ * (standard composer); each caller decides when polling is wanted ([shouldPoll]) and posts on its
7
+ * own thread ([post] / [cancel]).
8
+ *
9
+ * [read] gives true = right-to-left, false = left-to-right, null = no language (emoji, voice): no
10
+ * language keeps the last direction, so opening the emoji keyboard moves nothing.
11
+ */
12
+ internal class KeyboardDirectionWatcher(
13
+ private val post: (Runnable, Long) -> Unit,
14
+ private val cancel: (Runnable) -> Unit,
15
+ private val read: () -> Boolean?,
16
+ private val shouldPoll: () -> Boolean,
17
+ private val onChange: (Boolean) -> Unit,
18
+ ) {
19
+ companion object {
20
+ const val POLL_MS = 500L
21
+ }
22
+
23
+ /** The last direction seen; null until a language is reported. */
24
+ var last: Boolean? = null
25
+ private set
26
+
27
+ private var invalidated = false
28
+ private val tick = Runnable { refresh() }
29
+
30
+ /** Reads now; a new language calls [onChange]. Then polls on while [shouldPoll]. */
31
+ fun refresh() {
32
+ if (invalidated) return
33
+ val now = read()
34
+ if (now != null && now != last) {
35
+ last = now
36
+ onChange(now)
37
+ }
38
+ schedule()
39
+ }
40
+
41
+ /** Takes the current reading (null included) as the baseline without [onChange], then polls. */
42
+ fun prime() {
43
+ if (invalidated) return
44
+ last = read()
45
+ schedule()
46
+ }
47
+
48
+ // ponytail: Android sends no event when the user switches keyboard languages, so this polls every
49
+ // POLL_MS, only while the caller's shouldPoll holds (a focused composer whose caret paragraph has
50
+ // no letter yet, in the foreground). Ceiling: a switch shows up to POLL_MS late; drop the poll if
51
+ // Android ever reports IME subtype changes to the app.
52
+ fun schedule() {
53
+ cancel(tick)
54
+ if (!invalidated && shouldPoll()) post(tick, POLL_MS)
55
+ }
56
+
57
+ fun stop() = cancel(tick)
58
+
59
+ /** For good: nothing reads or polls after this, even a task the caller had already queued. */
60
+ fun invalidate() {
61
+ invalidated = true
62
+ cancel(tick)
63
+ }
64
+ }
@@ -0,0 +1,44 @@
1
+ package com.reactnativecometchatuikit
2
+
3
+ import android.content.Context
4
+ import android.text.TextUtils
5
+ import android.view.View
6
+ import android.view.inputmethod.InputMethodManager
7
+ import java.util.Locale
8
+
9
+ /**
10
+ * ENG-57281: the direction of the current keyboard's language, for putting an empty composer's caret
11
+ * on that side. Used by RichTextEditorView (compact composer) and CometChatKeyboardModule (standard
12
+ * composer).
13
+ */
14
+ internal object KeyboardLanguageDirection {
15
+ /**
16
+ * true = right-to-left, false = left-to-right, null = the keyboard gave no language (emoji,
17
+ * voice, a keyboard that reports none). Takes a BCP-47 tag ("ar-EG") or a legacy locale ("ar_EG").
18
+ */
19
+ fun isRtl(language: String?): Boolean? {
20
+ val tag = language?.trim()?.replace('_', '-')
21
+ if (tag.isNullOrEmpty()) return null
22
+ val locale = Locale.forLanguageTag(tag)
23
+ if (locale.language.isNullOrEmpty()) return null // "und", or not a language tag
24
+ return TextUtils.getLayoutDirectionFromLocale(locale) == View.LAYOUT_DIRECTION_RTL
25
+ }
26
+
27
+ /**
28
+ * The current keyboard's language: its subtype's language tag, else the subtype's locale string.
29
+ * Android sends no event when the user switches keyboard languages, so callers poll this.
30
+ */
31
+ fun currentLanguage(context: Context): String? {
32
+ val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager ?: return null
33
+ val subtype = try { imm.currentInputMethodSubtype } catch (e: Exception) { null } ?: return null
34
+ return subtype.languageTag.takeUnless { it.isNullOrEmpty() }
35
+ ?: @Suppress("DEPRECATION") subtype.locale.takeUnless { it.isNullOrEmpty() }
36
+ }
37
+
38
+ /** "rtl" / "ltr" / null for JS. */
39
+ fun current(context: Context): String? = when (isRtl(currentLanguage(context))) {
40
+ true -> "rtl"
41
+ false -> "ltr"
42
+ null -> null
43
+ }
44
+ }