@bugsee/react-native 0.0.0-stage → 7.0.0-beta2

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 (250) hide show
  1. package/BugseeReactNative.podspec +95 -0
  2. package/LICENSE +21 -0
  3. package/README.md +557 -2
  4. package/android/build.gradle +162 -0
  5. package/android/consumer-rules.pro +2 -0
  6. package/android/src/main/AndroidManifest.xml +33 -0
  7. package/android/src/main/java/com/bugsee/reactnative/AppearanceBridge.java +82 -0
  8. package/android/src/main/java/com/bugsee/reactnative/AttributeBridge.java +209 -0
  9. package/android/src/main/java/com/bugsee/reactnative/BreadcrumbFilterBridge.java +443 -0
  10. package/android/src/main/java/com/bugsee/reactnative/BridgeJson.java +168 -0
  11. package/android/src/main/java/com/bugsee/reactnative/BugseeModule.java +1869 -0
  12. package/android/src/main/java/com/bugsee/reactnative/BugseeOptionEnums.java +136 -0
  13. package/android/src/main/java/com/bugsee/reactnative/BugseePackage.java +42 -0
  14. package/android/src/main/java/com/bugsee/reactnative/BugseeReactNativeWrapper.java +200 -0
  15. package/android/src/main/java/com/bugsee/reactnative/BugseeStatusMapper.java +66 -0
  16. package/android/src/main/java/com/bugsee/reactnative/BugseeTokens.java +22 -0
  17. package/android/src/main/java/com/bugsee/reactnative/ConsoleEchoDedup.java +202 -0
  18. package/android/src/main/java/com/bugsee/reactnative/CreatedReports.java +127 -0
  19. package/android/src/main/java/com/bugsee/reactnative/DataRequestBridge.java +375 -0
  20. package/android/src/main/java/com/bugsee/reactnative/ExceptionBridge.java +88 -0
  21. package/android/src/main/java/com/bugsee/reactnative/ExceptionExecutors.java +33 -0
  22. package/android/src/main/java/com/bugsee/reactnative/LogFilterBridge.java +375 -0
  23. package/android/src/main/java/com/bugsee/reactnative/NetworkEvents.java +227 -0
  24. package/android/src/main/java/com/bugsee/reactnative/NetworkFilterBridge.java +498 -0
  25. package/android/src/main/java/com/bugsee/reactnative/PluginEndpoint.java +48 -0
  26. package/android/src/main/java/com/bugsee/reactnative/ReactNativeWebException.java +19 -0
  27. package/android/src/main/java/com/bugsee/reactnative/ReactNativeWrapperInitProvider.java +61 -0
  28. package/android/src/main/java/com/bugsee/reactnative/ReactRootOriginTracker.java +867 -0
  29. package/android/src/main/java/com/bugsee/reactnative/ReportArgs.java +58 -0
  30. package/android/src/main/java/com/bugsee/reactnative/ReportHandlerBridge.java +336 -0
  31. package/android/src/main/java/com/bugsee/reactnative/ReportHandlerDeadlines.java +57 -0
  32. package/android/src/main/java/com/bugsee/reactnative/ReportOps.java +287 -0
  33. package/android/src/main/java/com/bugsee/reactnative/SecureRectanglePulls.java +113 -0
  34. package/android/src/main/java/com/bugsee/reactnative/SecureRectangleStore.java +692 -0
  35. package/android/src/main/java/com/bugsee/reactnative/SpanHandles.java +143 -0
  36. package/android/src/main/java/com/bugsee/reactnative/WrapperChannelHolder.java +137 -0
  37. package/android/src/main/java/com/bugsee/reactnative/WrapperEventBus.java +110 -0
  38. package/android/src/main/java/com/bugsee/reactnative/WrapperRegistrar.java +54 -0
  39. package/app.plugin.js +4 -0
  40. package/ios/BGSRNConsoleCapture.h +52 -0
  41. package/ios/BGSRNConsoleCapture.mm +344 -0
  42. package/ios/BugseeModule.h +25 -0
  43. package/ios/BugseeModule.mm +2728 -0
  44. package/ios/Package.swift +71 -0
  45. package/ios/Support/Package.resolved +14 -0
  46. package/ios/Support/Package.swift +44 -0
  47. package/ios/Support/Sources/BugseeRNSupport/BGSRNAttributes.m +52 -0
  48. package/ios/Support/Sources/BugseeRNSupport/BGSRNCreatedReportOps.m +236 -0
  49. package/ios/Support/Sources/BugseeRNSupport/BGSRNCreatedReports.m +184 -0
  50. package/ios/Support/Sources/BugseeRNSupport/BGSRNDataRequestBridge.m +431 -0
  51. package/ios/Support/Sources/BugseeRNSupport/BGSRNErrorMessage.m +40 -0
  52. package/ios/Support/Sources/BugseeRNSupport/BGSRNEventBus.m +70 -0
  53. package/ios/Support/Sources/BugseeRNSupport/BGSRNExceptions.m +63 -0
  54. package/ios/Support/Sources/BugseeRNSupport/BGSRNGuardedEmit.mm +32 -0
  55. package/ios/Support/Sources/BugseeRNSupport/BGSRNJSON.m +40 -0
  56. package/ios/Support/Sources/BugseeRNSupport/BGSRNLaunchHold.m +147 -0
  57. package/ios/Support/Sources/BugseeRNSupport/BGSRNMainThread.m +23 -0
  58. package/ios/Support/Sources/BugseeRNSupport/BGSRNNetworkEvents.m +159 -0
  59. package/ios/Support/Sources/BugseeRNSupport/BGSRNNetworkFilter.m +155 -0
  60. package/ios/Support/Sources/BugseeRNSupport/BGSRNPluginEndpoint.m +21 -0
  61. package/ios/Support/Sources/BugseeRNSupport/BGSRNReactRootOriginTracker.m +63 -0
  62. package/ios/Support/Sources/BugseeRNSupport/BGSRNReactWindow.m +251 -0
  63. package/ios/Support/Sources/BugseeRNSupport/BGSRNReportArgs.m +47 -0
  64. package/ios/Support/Sources/BugseeRNSupport/BGSRNReportDeadlines.m +8 -0
  65. package/ios/Support/Sources/BugseeRNSupport/BGSRNReportHandlerBridge.m +308 -0
  66. package/ios/Support/Sources/BugseeRNSupport/BGSRNReportOps.m +345 -0
  67. package/ios/Support/Sources/BugseeRNSupport/BGSRNSecureRectangles.m +508 -0
  68. package/ios/Support/Sources/BugseeRNSupport/BGSRNSettleOnce.m +62 -0
  69. package/ios/Support/Sources/BugseeRNSupport/BGSRNSpanHandles.m +136 -0
  70. package/ios/Support/Sources/BugseeRNSupport/BGSRNStatusMapper.m +18 -0
  71. package/ios/Support/Sources/BugseeRNSupport/BGSRNTokens.m +9 -0
  72. package/ios/Support/Sources/BugseeRNSupport/BGSRNValues.m +5 -0
  73. package/ios/Support/Sources/BugseeRNSupport/BGSRNWrapper.m +54 -0
  74. package/ios/Support/Sources/BugseeRNSupport/BGSRNWrapperChannelHolder.m +59 -0
  75. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNAttributes.h +63 -0
  76. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNCreatedReportOps.h +51 -0
  77. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNCreatedReports.h +81 -0
  78. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNDataRequestBridge.h +118 -0
  79. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNErrorMessage.h +30 -0
  80. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNEventBus.h +45 -0
  81. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNExceptions.h +31 -0
  82. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNGuardedEmit.h +31 -0
  83. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNJSON.h +25 -0
  84. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNLaunchHold.h +86 -0
  85. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNMainThread.h +32 -0
  86. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNNetworkEvents.h +28 -0
  87. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNNetworkFilter.h +28 -0
  88. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNPluginEndpoint.h +17 -0
  89. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReactRootOriginTracker.h +48 -0
  90. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReactWindow.h +135 -0
  91. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportArgs.h +17 -0
  92. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportDeadlines.h +37 -0
  93. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportHandlerBridge.h +94 -0
  94. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportOps.h +86 -0
  95. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNSecureRectangles.h +198 -0
  96. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNSettleOnce.h +25 -0
  97. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNSpanHandles.h +49 -0
  98. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNStatusMapper.h +26 -0
  99. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNTokens.h +14 -0
  100. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNValues.h +18 -0
  101. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNWrapper.h +40 -0
  102. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNWrapperChannelHolder.h +74 -0
  103. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNAttributesTests.m +194 -0
  104. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNCreatedReportOpsTests.m +274 -0
  105. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNCreatedReportsTests.m +240 -0
  106. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNDataRequestBridgeTests.m +685 -0
  107. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNErrorMessageTests.m +102 -0
  108. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNEventBusTests.m +136 -0
  109. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNExceptionsTests.m +68 -0
  110. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNFakeReport.h +41 -0
  111. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNFakeReport.m +166 -0
  112. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNGuardedEmitTests.mm +85 -0
  113. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNJSONTests.m +281 -0
  114. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNLaunchHoldTests.m +230 -0
  115. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNMainThreadTests.m +97 -0
  116. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNNetworkEventsTests.m +99 -0
  117. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNNetworkFilterTests.m +134 -0
  118. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNPluginEndpointTests.m +38 -0
  119. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReactRootOriginTrackerTests.m +210 -0
  120. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReactWindowTests.m +548 -0
  121. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportArgsTests.m +51 -0
  122. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportDeadlinesTests.m +22 -0
  123. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportHandlerBridgeTests.m +382 -0
  124. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportOpsTests.m +295 -0
  125. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNSecureRectanglesTests.m +568 -0
  126. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNSettleOnceTests.m +134 -0
  127. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNSpanHandlesTests.m +153 -0
  128. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNStatusMapperTests.m +81 -0
  129. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNValuesTests.m +32 -0
  130. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNWrapperChannelHolderTests.m +205 -0
  131. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNWrapperTests.m +66 -0
  132. package/ios/react-native-spm-prefix.h +10 -0
  133. package/native-versions.json +10 -0
  134. package/package.json +62 -3
  135. package/plugin/build/bundle-phase.d.ts +41 -0
  136. package/plugin/build/bundle-phase.js +116 -0
  137. package/plugin/build/bundle-phase.js.map +1 -0
  138. package/plugin/build/dsym-script.d.ts +32 -0
  139. package/plugin/build/dsym-script.js +159 -0
  140. package/plugin/build/dsym-script.js.map +1 -0
  141. package/plugin/build/endpoint.d.ts +30 -0
  142. package/plugin/build/endpoint.js +69 -0
  143. package/plugin/build/endpoint.js.map +1 -0
  144. package/plugin/build/gradle.d.ts +65 -0
  145. package/plugin/build/gradle.js +1066 -0
  146. package/plugin/build/gradle.js.map +1 -0
  147. package/plugin/build/index.d.ts +85 -0
  148. package/plugin/build/index.js +217 -0
  149. package/plugin/build/index.js.map +1 -0
  150. package/plugin/build/manifest.d.ts +48 -0
  151. package/plugin/build/manifest.js +69 -0
  152. package/plugin/build/manifest.js.map +1 -0
  153. package/plugin/build/native-versions.baked.json +4 -0
  154. package/plugin/build/native-versions.d.ts +10 -0
  155. package/plugin/build/native-versions.js +27 -0
  156. package/plugin/build/native-versions.js.map +1 -0
  157. package/plugin/build/pbx-string.d.ts +8 -0
  158. package/plugin/build/pbx-string.js +31 -0
  159. package/plugin/build/pbx-string.js.map +1 -0
  160. package/plugin/build/podfile.d.ts +3 -0
  161. package/plugin/build/podfile.js +28 -0
  162. package/plugin/build/podfile.js.map +1 -0
  163. package/plugin/build/properties.d.ts +19 -0
  164. package/plugin/build/properties.js +41 -0
  165. package/plugin/build/properties.js.map +1 -0
  166. package/plugin/build/scheme.d.ts +20 -0
  167. package/plugin/build/scheme.js +107 -0
  168. package/plugin/build/scheme.js.map +1 -0
  169. package/react-native.config.js +26 -0
  170. package/scripts/bugsee-sourcemaps.gradle +201 -0
  171. package/scripts/bugsee-xcode.sh +83 -0
  172. package/scripts/compose-then-inject.js +113 -0
  173. package/scripts/hermes-sourcemaps.js +634 -0
  174. package/scripts/hermesc-preserve-js.cmd +6 -0
  175. package/scripts/hermesc-preserve-js.js +200 -0
  176. package/scripts/hermesc-preserve-js.sh +7 -0
  177. package/src/NativeBugsee.ts +622 -0
  178. package/src/apm/notify.ts +58 -0
  179. package/src/apm/span.ts +403 -0
  180. package/src/apm/types.ts +57 -0
  181. package/src/appearance/report.ts +171 -0
  182. package/src/architecture.ts +48 -0
  183. package/src/attributes/errors.ts +39 -0
  184. package/src/attributes/validate.ts +150 -0
  185. package/src/breadcrumbs/add.ts +58 -0
  186. package/src/breadcrumbs/data.ts +33 -0
  187. package/src/breadcrumbs/filter.ts +215 -0
  188. package/src/breadcrumbs/types.ts +64 -0
  189. package/src/bridge/json.ts +110 -0
  190. package/src/console/capture.ts +180 -0
  191. package/src/console/dedup.ts +175 -0
  192. package/src/data/validate.ts +259 -0
  193. package/src/errorName.ts +32 -0
  194. package/src/exceptions/ErrorBoundary.tsx +92 -0
  195. package/src/exceptions/RootErrorReporter.tsx +61 -0
  196. package/src/exceptions/debugIds.ts +118 -0
  197. package/src/exceptions/handlers.ts +236 -0
  198. package/src/exceptions/options.ts +85 -0
  199. package/src/exceptions/payload.ts +456 -0
  200. package/src/exceptions/report.ts +170 -0
  201. package/src/exceptions/sha1.ts +148 -0
  202. package/src/exceptions/stack.ts +355 -0
  203. package/src/index.ts +962 -0
  204. package/src/logs/filter.ts +90 -0
  205. package/src/logs/log.ts +36 -0
  206. package/src/network/add.ts +165 -0
  207. package/src/network/filter.ts +198 -0
  208. package/src/options/AndroidLaunchOptions.ts +78 -0
  209. package/src/options/BugseeLaunchOptions.ts +190 -0
  210. package/src/options/IOSLaunchOptions.ts +57 -0
  211. package/src/options/android-options-manifest.json +646 -0
  212. package/src/options/createDefaultLaunchOptions.ts +29 -0
  213. package/src/options/endpoint.ts +58 -0
  214. package/src/options/enums.ts +104 -0
  215. package/src/options/ios-enums.json +19 -0
  216. package/src/options/keys.ts +39 -0
  217. package/src/options/option-enums.json +34 -0
  218. package/src/options/option-keys.json +107 -0
  219. package/src/report/BugseeReport.ts +264 -0
  220. package/src/report/CreatedReport.ts +127 -0
  221. package/src/report/dispatcher.ts +95 -0
  222. package/src/report/errors.ts +48 -0
  223. package/src/report/fields.ts +40 -0
  224. package/src/report/types.ts +157 -0
  225. package/src/report/validate.ts +197 -0
  226. package/src/secure/BugseeSecure.tsx +202 -0
  227. package/src/secure/ancestry.ts +622 -0
  228. package/src/secure/drivers.ts +342 -0
  229. package/src/secure/followEvents.ts +57 -0
  230. package/src/secure/hold.ts +99 -0
  231. package/src/secure/measureLoop.ts +323 -0
  232. package/src/secure/nativeAnimation.ts +1005 -0
  233. package/src/secure/rectangles.ts +93 -0
  234. package/src/secure/registry.ts +237 -0
  235. package/src/secure/surface.ts +83 -0
  236. package/src/secure/tracker.ts +723 -0
  237. package/src/secure/transition.ts +488 -0
  238. package/src/secure/unit.ts +22 -0
  239. package/src/status.ts +16 -0
  240. package/src/version.ts +9 -0
  241. package/src/viewtree/anchor.tsx +105 -0
  242. package/src/viewtree/constants.ts +20 -0
  243. package/src/viewtree/fiber.ts +251 -0
  244. package/src/viewtree/requests.ts +347 -0
  245. package/src/viewtree/walk.ts +902 -0
  246. package/src/wrapper/channel.ts +33 -0
  247. package/src/wrapper/collect.ts +52 -0
  248. package/src/wrapper/events.ts +45 -0
  249. package/src/wrapper/identity.ts +54 -0
  250. package/src/wrapper/lifecycle-events.ts +89 -0
@@ -0,0 +1,692 @@
1
+ package com.bugsee.reactnative;
2
+
3
+ import android.util.Log;
4
+
5
+ import androidx.annotation.NonNull;
6
+ import androidx.annotation.Nullable;
7
+
8
+ import java.util.Arrays;
9
+ import java.util.Map;
10
+ import java.util.TreeMap;
11
+ import java.util.concurrent.ConcurrentHashMap;
12
+
13
+ /**
14
+ * The regions the app has asked Bugsee not to record, in the form the SDK
15
+ * pulls them.
16
+ *
17
+ * <p>The SDK does not subscribe to changes; it asks, 2-3 times a second, for
18
+ * a packed buffer {@code [version, count, l, t, r, b, ...]} and re-reads the
19
+ * rectangles only when the version differs from the one it last saw. Two
20
+ * properties follow, and both are load-bearing:
21
+ *
22
+ * <ul>
23
+ * <li>any change to the set MUST move the version. A stale version is a
24
+ * privacy defect in the dangerous direction: the SDK goes on redacting
25
+ * the region the app has stopped considering secret, and — worse —
26
+ * stops redacting nothing, so a newly secret region is recorded in the
27
+ * clear until something else happens to move the version.</li>
28
+ * <li>a no-op write must NOT move it, or the SDK re-reads on every frame.</li>
29
+ * </ul>
30
+ *
31
+ * <p>The version is kept per display because the SDK's freshness comparison is
32
+ * per display: a change on one screen must not invalidate another's.
33
+ *
34
+ * <h2>Surfaces</h2>
35
+ *
36
+ * <p>JS measures with {@code measureInWindow}, which Fabric makes relative to
37
+ * the measured node's nearest {@code RootNodeKind} ancestor
38
+ * ({@code LayoutableShadowNode.cpp} stops the ancestor walk there;
39
+ * {@code ModalHostViewShadowNode} sets that trait). The activity React root
40
+ * and a {@code <Modal>}'s {@code DialogRootViewGroup} are different surfaces:
41
+ * each rectangle is stored under the surface it was measured in, and served
42
+ * translated by <em>that</em> surface's display origin (see {@link
43
+ * ReactRootOriginTracker#surfaceOrigin}). A single origin for the whole
44
+ * display cannot serve both.
45
+ *
46
+ * <p>Fails closed. A surface whose origin has not been read yet, or whose
47
+ * root was forgotten ({@link #forgetOrigin}), serves its rectangles as one
48
+ * rectangle covering the whole display: an unknown origin must not place a
49
+ * secure region anywhere it could miss. That includes the main surface, so
50
+ * the very first publish masks the whole display until the tracker's first
51
+ * read, a fraction of a second. While no React root can be found at all,
52
+ * the main surface's rectangles stay one whole-display rectangle: there is
53
+ * no origin to read, and the store will not guess one (ruled, N8).
54
+ *
55
+ * <p>A {@code <Modal>} surface belongs to one JS runtime: its key is a React
56
+ * tag that the next runtime does not know. A reload drops every surface but
57
+ * the main one ({@link #claimRuntime}, {@link #releaseRuntime}), so a Modal
58
+ * that was open when the old runtime went away does not mask the display
59
+ * for the rest of the process. A claim also empties the main surface's
60
+ * rectangles, keeping its origin: they were the old runtime's, and its
61
+ * clearing write is ignored once stale (below), so without this a new tree
62
+ * that never publishes on the main surface would leave them masking until
63
+ * the process dies. The new runtime's first publish is then the only main
64
+ * set.
65
+ *
66
+ * <p>The claim also gates every lane write a runtime makes: the module's
67
+ * publish ({@link #publishForRuntime}) and its tracker's origin writes
68
+ * ({@link #setOriginForRuntime}, {@link #forgetOriginForRuntime}, {@link
69
+ * #dropSurfaceIfEmptyForRuntime}). A write whose claim is no longer current
70
+ * is ignored, under the same lock as the claim. A reload can construct the
71
+ * new module before the old one is invalidated, and Fabric numbers React
72
+ * tags from 1 again, so the old runtime's late writes would otherwise put
73
+ * back a lane the claim dropped or clear the new runtime's lane on the same
74
+ * key, uncovering its Modal. The display size is not a runtime's state: it
75
+ * is recorded by whoever reads it.
76
+ *
77
+ * <p>Every served rectangle is clamped to the display's real size once it
78
+ * is known ({@link #setDisplayBounds}). The SDK's native video mask clamps a
79
+ * rectangle's rows to the frame but not its right edge to a row, so a
80
+ * rectangle wider than the frame would black out the start of the next row
81
+ * on every row: it must never be handed one.
82
+ *
83
+ * <h2>Threading</h2>
84
+ *
85
+ * <p>Writes arrive on the JS thread; the pull happens on a Bugsee background
86
+ * thread (on Apple, the main thread). Every published value is therefore an
87
+ * immutable snapshot, swapped in whole. The SDK's own header is explicit that
88
+ * a buffer rewritten by another thread while it reads is a use-after-free, so
89
+ * nothing here ever hands out, or retains, an array a caller can still write
90
+ * through.
91
+ */
92
+ final class SecureRectangleStore {
93
+
94
+ private static final String TAG = "BugseeRN";
95
+
96
+ /** Four coordinates per rectangle: left, top, right, bottom. */
97
+ private static final int COORDINATES_PER_RECTANGLE = 4;
98
+
99
+ /** Precedes the rectangles in every buffer: the version and the count. */
100
+ private static final int HEADER_INTS = 2;
101
+
102
+ /**
103
+ * The version a display reports before anything is secured. Any value
104
+ * works as long as it is stable; the SDK only ever compares it against
105
+ * what it saw last.
106
+ */
107
+ private static final int INITIAL_VERSION = 1;
108
+
109
+ private static final int[] NO_COORDINATES = new int[0];
110
+
111
+ /**
112
+ * The activity React root's surface key. Legacy {@link #set}/{@link
113
+ * #setOrigin} writers that do not name a surface land here; a {@code
114
+ * <Modal>}'s {@code DialogRootViewGroup} uses a different key.
115
+ */
116
+ static final int MAIN_SURFACE = 0;
117
+
118
+ /**
119
+ * The bounds a display serves a fail-closed lane as when its real size
120
+ * has not been recorded ({@link #setDisplayBounds}): bigger than any
121
+ * phone or tablet panel, and small enough that a renderer that does not
122
+ * clip a rectangle's right edge per row still stays cheap.
123
+ */
124
+ static final int FALLBACK_DISPLAY_SIZE = 16384;
125
+
126
+ /** One surface's raw measurements and the origin that places them. */
127
+ private static final class Lane {
128
+ /** As JS published them: relative to this surface's own origin. */
129
+ final int[] raw;
130
+ final int originX;
131
+ final int originY;
132
+ /** Whether {@link #originX}/{@link #originY} were read (and still hold). */
133
+ final boolean originKnown;
134
+
135
+ Lane(@NonNull final int[] raw, final int originX, final int originY, final boolean originKnown) {
136
+ this.raw = raw;
137
+ this.originX = originX;
138
+ this.originY = originY;
139
+ this.originKnown = originKnown;
140
+ }
141
+
142
+ @NonNull
143
+ Lane withRaw(@NonNull final int[] newRaw) {
144
+ return new Lane(newRaw, originX, originY, originKnown);
145
+ }
146
+
147
+ @NonNull
148
+ Lane withOrigin(final int newOriginX, final int newOriginY) {
149
+ return new Lane(raw, newOriginX, newOriginY, true);
150
+ }
151
+
152
+ @NonNull
153
+ Lane withOriginUnknown() {
154
+ return new Lane(raw, originX, originY, false);
155
+ }
156
+ }
157
+
158
+ /** One immutable published set for a display. Replaced wholesale. */
159
+ private static final class Snapshot {
160
+ final int version;
161
+ /** Surface key → lane. TreeMap so the served order is stable. */
162
+ final TreeMap<Integer, Lane> lanes;
163
+ /** The display's real size {@code {width, height}} in pixels, or {@code null}. */
164
+ @Nullable
165
+ final int[] bounds;
166
+ /** What the SDK is served: every lane in surface-key order, clamped to {@link #bounds}. */
167
+ final int[] coordinates;
168
+
169
+ Snapshot(final int version, @NonNull final TreeMap<Integer, Lane> lanes, @Nullable final int[] bounds) {
170
+ this.version = version;
171
+ // Defensive copies: callers must not retain a mutable reference.
172
+ this.lanes = new TreeMap<>(lanes);
173
+ this.bounds = bounds == null ? null : bounds.clone();
174
+ this.coordinates = serve(this.lanes, this.bounds);
175
+ }
176
+
177
+ @NonNull
178
+ Snapshot withVersion(final int newVersion) {
179
+ return new Snapshot(newVersion, lanes, bounds);
180
+ }
181
+
182
+ @NonNull
183
+ Snapshot withLanes(@NonNull final TreeMap<Integer, Lane> nextLanes) {
184
+ return new Snapshot(0, nextLanes, bounds);
185
+ }
186
+
187
+ @NonNull
188
+ Snapshot withBounds(@NonNull final int[] nextBounds) {
189
+ return new Snapshot(0, lanes, nextBounds);
190
+ }
191
+
192
+ /**
193
+ * A lane whose origin is unknown serves the whole display; any other
194
+ * serves its rectangles moved by its origin. Every rectangle is then
195
+ * clamped to the display, and one left with no area is dropped: a
196
+ * renderer is never handed a rectangle that runs off the panel.
197
+ */
198
+ @NonNull
199
+ private static int[] serve(@NonNull final TreeMap<Integer, Lane> lanes, @Nullable final int[] bounds) {
200
+ final int width = bounds == null ? FALLBACK_DISPLAY_SIZE : bounds[0];
201
+ final int height = bounds == null ? FALLBACK_DISPLAY_SIZE : bounds[1];
202
+ int total = 0;
203
+ for (final Lane lane : lanes.values()) {
204
+ total += lane.raw.length == 0 ? 0 : lane.originKnown ? lane.raw.length : COORDINATES_PER_RECTANGLE;
205
+ }
206
+ if (total == 0) {
207
+ return NO_COORDINATES;
208
+ }
209
+ final int[] served = new int[total];
210
+ int at = 0;
211
+ for (final Lane lane : lanes.values()) {
212
+ if (lane.raw.length == 0) {
213
+ continue;
214
+ }
215
+ if (!lane.originKnown) {
216
+ served[at++] = 0;
217
+ served[at++] = 0;
218
+ served[at++] = width;
219
+ served[at++] = height;
220
+ continue;
221
+ }
222
+ final int[] moved = translate(lane.raw, lane.originX, lane.originY);
223
+ for (int i = 0; i < moved.length; i += COORDINATES_PER_RECTANGLE) {
224
+ if (bounds == null) {
225
+ System.arraycopy(moved, i, served, at, COORDINATES_PER_RECTANGLE);
226
+ at += COORDINATES_PER_RECTANGLE;
227
+ continue;
228
+ }
229
+ final int left = clamp(moved[i], width);
230
+ final int top = clamp(moved[i + 1], height);
231
+ final int right = clamp(moved[i + 2], width);
232
+ final int bottom = clamp(moved[i + 3], height);
233
+ if (right <= left || bottom <= top) {
234
+ continue;
235
+ }
236
+ served[at++] = left;
237
+ served[at++] = top;
238
+ served[at++] = right;
239
+ served[at++] = bottom;
240
+ }
241
+ }
242
+ return at == served.length ? served : Arrays.copyOf(served, at);
243
+ }
244
+
245
+ private static int clamp(final int value, final int max) {
246
+ return Math.max(0, Math.min(value, max));
247
+ }
248
+ }
249
+
250
+ /** What a display serves before anything is secured or located. */
251
+ private static final Snapshot EMPTY = new Snapshot(INITIAL_VERSION, new TreeMap<>(), null);
252
+
253
+ /**
254
+ * The process-wide set of secured regions.
255
+ *
256
+ * <p>Deliberately not owned by a wrapper instance. The wrapper object is
257
+ * replaced mid-session — the init provider registers one before launch and
258
+ * setWrapperInfo swaps in the fully-populated one once the bridge is up —
259
+ * and the regions an app has marked secret must not be forgotten when that
260
+ * happens. Losing them would leave the app recorded unredacted until it
261
+ * next happened to re-publish, which is the failure that matters most here.
262
+ */
263
+ private static final SecureRectangleStore SHARED = new SecureRectangleStore();
264
+
265
+ @NonNull
266
+ static SecureRectangleStore shared() {
267
+ return SHARED;
268
+ }
269
+
270
+ private final Map<Integer, Snapshot> byDisplay = new ConcurrentHashMap<>();
271
+
272
+ /**
273
+ * Publishes {@code coordinates} as the secure set for {@code display}'s
274
+ * main surface, as a flat list of four-int rectangles. Other surfaces on
275
+ * the same display are left alone.
276
+ *
277
+ * <p>The version moves only when the served set actually differs, so an
278
+ * app that re-publishes an unchanged layout on every render costs the SDK
279
+ * nothing.
280
+ */
281
+ void set(final int display, @Nullable final int[] coordinates) {
282
+ set(display, MAIN_SURFACE, coordinates);
283
+ }
284
+
285
+ /**
286
+ * Publishes {@code coordinates} for one surface on {@code display}. Other
287
+ * surfaces keep their rectangles and origins.
288
+ */
289
+ void set(final int display, final int surface, @Nullable final int[] coordinates) {
290
+ if (coordinates == null) {
291
+ throw new IllegalArgumentException(
292
+ "secure rectangles cannot be null; pass an empty array to clear them");
293
+ }
294
+ if (coordinates.length % COORDINATES_PER_RECTANGLE != 0) {
295
+ throw new IllegalArgumentException(
296
+ "secure rectangles need 4 coordinates each (left, top, right, bottom), got "
297
+ + coordinates.length);
298
+ }
299
+
300
+ // Copy on the way in as well as out: the caller keeps its array and a
301
+ // later write through it would edit a snapshot the SDK is reading.
302
+ final int[] raw = coordinates.clone();
303
+ update(display, previous -> withLane(previous, surface, lane -> lane.withRaw(raw)));
304
+ }
305
+
306
+ /**
307
+ * {@link #set}, for the TurboModule: a malformed list is logged and
308
+ * dropped instead of thrown. {@code setSecureRectangles} is a void method,
309
+ * so an exception out of it has no promise to reject and would take the
310
+ * host app down; the previous set stays published.
311
+ *
312
+ * @return whether {@code coordinates} was published
313
+ */
314
+ boolean publishOrLog(final int display, @Nullable final int[] coordinates) {
315
+ return publishOrLog(display, MAIN_SURFACE, coordinates);
316
+ }
317
+
318
+ boolean publishOrLog(final int display, final int surface, @Nullable final int[] coordinates) {
319
+ return publishForRuntime(currentClaim(), display, surface, coordinates);
320
+ }
321
+
322
+ /**
323
+ * {@link #publishOrLog}, written by the module holding {@code claim}: ignored
324
+ * when a newer runtime has claimed the store since. What the TurboModule
325
+ * calls.
326
+ *
327
+ * @return whether {@code coordinates} was published
328
+ */
329
+ synchronized boolean publishForRuntime(final int claim, final int display, final int surface,
330
+ @Nullable final int[] coordinates) {
331
+ if (!isCurrentLocked(claim)) {
332
+ return false;
333
+ }
334
+ try {
335
+ set(display, surface, coordinates);
336
+ return true;
337
+ } catch (IllegalArgumentException e) {
338
+ Log.e(TAG, "setSecureRectangles rejected; the previous set stays published: "
339
+ + e.getClass().getName());
340
+ return false;
341
+ }
342
+ }
343
+
344
+ /**
345
+ * Records where the main React root's viewport origin sits on {@code
346
+ * display}, in display pixels, and serves that surface's rectangles moved
347
+ * by it. Other surfaces keep their own origins.
348
+ *
349
+ * <p>JS measures with {@code measureInWindow}, which React Native makes
350
+ * relative to the root's viewport offset
351
+ * ({@code RootViewUtil.getViewportOffset}): the root's location in its
352
+ * window, less the status-bar and cutout insets when edge-to-edge is off.
353
+ * The SDK wants display-relative pixels. Unconverted, every rectangle
354
+ * lands that many pixels up and left of the view it covers, and a strip of
355
+ * each secure region is recorded in the clear.
356
+ *
357
+ * <p>The origin is an integer, so adding it after JS has rounded each edge
358
+ * outwards (floor left/top, ceil right/bottom, after scaling) is exactly
359
+ * the same as rounding after the offset: {@code floor(x) + k == floor(x + k)}
360
+ * for integer {@code k}.
361
+ */
362
+ void setOrigin(final int display, final int originX, final int originY) {
363
+ setOrigin(display, MAIN_SURFACE, originX, originY);
364
+ }
365
+
366
+ /**
367
+ * Records where one surface's viewport origin sits on {@code display}.
368
+ * Only that surface's rectangles move; a {@code <Modal>} and the activity
369
+ * root must not share one origin.
370
+ */
371
+ void setOrigin(final int display, final int surface, final int originX, final int originY) {
372
+ update(display, previous -> withLane(previous, surface,
373
+ lane -> lane.withOrigin(originX, originY)));
374
+ }
375
+
376
+ /**
377
+ * {@link #setOrigin(int, int, int, int)}, written by the module holding
378
+ * {@code claim}: ignored when a newer runtime has claimed the store since.
379
+ * An old runtime's dialog root must not place the new runtime's Modal.
380
+ */
381
+ synchronized void setOriginForRuntime(final int claim, final int display, final int surface,
382
+ final int originX, final int originY) {
383
+ if (isCurrentLocked(claim)) {
384
+ setOrigin(display, surface, originX, originY);
385
+ }
386
+ }
387
+
388
+ /**
389
+ * Records {@code display}'s real size in pixels: what a fail-closed lane
390
+ * serves, and what every rectangle is clamped to.
391
+ */
392
+ void setDisplayBounds(final int display, final int width, final int height) {
393
+ if (width <= 0 || height <= 0) {
394
+ return;
395
+ }
396
+ update(display, previous -> previous.bounds != null
397
+ && previous.bounds[0] == width && previous.bounds[1] == height
398
+ ? previous
399
+ : previous.withBounds(new int[] { width, height }));
400
+ }
401
+
402
+ /** The current JS runtime's claim; see {@link #claimRuntime}. Guarded by {@code this}. */
403
+ private int runtime;
404
+
405
+ /**
406
+ * The claim a runtime holds now. For writers that belong to no runtime
407
+ * of their own (tests, single-runtime callers); a module writes with the
408
+ * claim {@link #claimRuntime} gave it.
409
+ */
410
+ synchronized int currentClaim() {
411
+ return runtime;
412
+ }
413
+
414
+ /**
415
+ * Whether {@code claim} is still the current runtime's. A stale one is
416
+ * logged at debug, numbers only. Caller holds {@code this}.
417
+ */
418
+ private boolean isCurrentLocked(final int claim) {
419
+ if (claim == runtime) {
420
+ return true;
421
+ }
422
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
423
+ Log.d(TAG, "secure write ignored: runtime claim " + claim
424
+ + " is stale, current " + runtime);
425
+ }
426
+ return false;
427
+ }
428
+
429
+ /**
430
+ * A new JS runtime's module is starting: drops every surface but the main
431
+ * one, whose keys the new runtime cannot know, empties the main one's
432
+ * rectangles (keeping its origin), and returns its claim. Synchronised
433
+ * with {@link #releaseRuntime} so an old module released after the new
434
+ * one started cannot drop the new runtime's surfaces.
435
+ */
436
+ synchronized int claimRuntime() {
437
+ runtime++;
438
+ dropNonMainSurfaces();
439
+ emptyMainSurface();
440
+ return runtime;
441
+ }
442
+
443
+ /**
444
+ * Empties the main surface's rectangles on every display, keeping its
445
+ * origin: the old runtime's set must not outlive it, and a new runtime
446
+ * that never publishes on the main surface would otherwise inherit it.
447
+ */
448
+ private void emptyMainSurface() {
449
+ for (final Integer display : byDisplay.keySet()) {
450
+ update(display, previous -> {
451
+ final Lane main = previous.lanes.get(MAIN_SURFACE);
452
+ if (main == null || main.raw.length == 0) {
453
+ return previous;
454
+ }
455
+ final TreeMap<Integer, Lane> nextLanes = new TreeMap<>(previous.lanes);
456
+ nextLanes.put(MAIN_SURFACE, main.withRaw(NO_COORDINATES));
457
+ return previous.withLanes(nextLanes);
458
+ });
459
+ }
460
+ }
461
+
462
+ /**
463
+ * The module holding {@code claim} is gone. Drops every surface but the
464
+ * main one, unless a newer runtime has already claimed the store.
465
+ */
466
+ synchronized void releaseRuntime(final int claim) {
467
+ if (claim == runtime) {
468
+ dropNonMainSurfaces();
469
+ }
470
+ }
471
+
472
+ /** How many surfaces other than the main one hold rectangles, on every display. */
473
+ int nonMainSurfacesWithRectangles() {
474
+ int count = 0;
475
+ for (final Snapshot snapshot : byDisplay.values()) {
476
+ for (final java.util.Map.Entry<Integer, Lane> entry : snapshot.lanes.entrySet()) {
477
+ if (entry.getKey() != MAIN_SURFACE && entry.getValue().raw.length > 0) {
478
+ count++;
479
+ }
480
+ }
481
+ }
482
+ return count;
483
+ }
484
+
485
+ /** Drops every surface but the main one, on every display. */
486
+ private void dropNonMainSurfaces() {
487
+ for (final Integer display : byDisplay.keySet()) {
488
+ update(display, previous -> {
489
+ if (previous.lanes.isEmpty()
490
+ || (previous.lanes.size() == 1 && previous.lanes.containsKey(MAIN_SURFACE))) {
491
+ return previous;
492
+ }
493
+ final TreeMap<Integer, Lane> nextLanes = new TreeMap<>();
494
+ final Lane main = previous.lanes.get(MAIN_SURFACE);
495
+ if (main != null) {
496
+ nextLanes.put(MAIN_SURFACE, main);
497
+ }
498
+ return previous.withLanes(nextLanes);
499
+ });
500
+ }
501
+ }
502
+
503
+ /**
504
+ * {@link #forgetOrigin}, by the module holding {@code claim}: ignored when
505
+ * a newer runtime has claimed the store since.
506
+ */
507
+ synchronized void forgetOriginForRuntime(final int claim, final int surface) {
508
+ if (isCurrentLocked(claim)) {
509
+ forgetOrigin(surface);
510
+ }
511
+ }
512
+
513
+ /**
514
+ * {@link #dropSurfaceIfEmpty}, by the module holding {@code claim}: ignored
515
+ * when a newer runtime has claimed the store since.
516
+ */
517
+ synchronized void dropSurfaceIfEmptyForRuntime(final int claim, final int surface) {
518
+ if (isCurrentLocked(claim)) {
519
+ dropSurfaceIfEmpty(surface);
520
+ }
521
+ }
522
+
523
+ /** Whether {@code display}'s real size has been recorded. */
524
+ boolean hasDisplayBounds(final int display) {
525
+ final Snapshot current = byDisplay.get(display);
526
+ return current != null && current.bounds != null;
527
+ }
528
+
529
+ /**
530
+ * {@code surface}'s root is gone: its origin no longer holds, so every
531
+ * display serves its rectangles as the whole display again until a new
532
+ * origin is read. An empty lane is unaffected.
533
+ */
534
+ void forgetOrigin(final int surface) {
535
+ for (final Integer display : byDisplay.keySet()) {
536
+ update(display, previous -> {
537
+ final Lane lane = previous.lanes.get(surface);
538
+ if (lane == null || !lane.originKnown) {
539
+ return previous;
540
+ }
541
+ final TreeMap<Integer, Lane> nextLanes = new TreeMap<>(previous.lanes);
542
+ nextLanes.put(surface, lane.withOriginUnknown());
543
+ return previous.withLanes(nextLanes);
544
+ });
545
+ }
546
+ }
547
+
548
+ /** Whether {@code surface} holds rectangles on any display. */
549
+ boolean hasRectangles(final int surface) {
550
+ for (final Snapshot snapshot : byDisplay.values()) {
551
+ final Lane lane = snapshot.lanes.get(surface);
552
+ if (lane != null && lane.raw.length > 0) {
553
+ return true;
554
+ }
555
+ }
556
+ return false;
557
+ }
558
+
559
+ /**
560
+ * Forgets {@code surface} on every display where it holds no rectangles:
561
+ * its root is gone, and a later root may reuse the key. A surface that
562
+ * still holds rectangles stays (fails closed): only its owner clearing
563
+ * them removes the redaction.
564
+ */
565
+ void dropSurfaceIfEmpty(final int surface) {
566
+ if (surface == MAIN_SURFACE) {
567
+ return;
568
+ }
569
+ for (final Integer display : byDisplay.keySet()) {
570
+ update(display, previous -> {
571
+ final Lane lane = previous.lanes.get(surface);
572
+ if (lane == null || lane.raw.length > 0) {
573
+ return previous;
574
+ }
575
+ final TreeMap<Integer, Lane> nextLanes = new TreeMap<>(previous.lanes);
576
+ nextLanes.remove(surface);
577
+ return previous.withLanes(nextLanes);
578
+ });
579
+ }
580
+ }
581
+
582
+ /** Whether {@code surface} has a lane on any display. Tests and the tracker read it. */
583
+ boolean hasSurface(final int surface) {
584
+ for (final Snapshot snapshot : byDisplay.values()) {
585
+ if (snapshot.lanes.containsKey(surface)) {
586
+ return true;
587
+ }
588
+ }
589
+ return false;
590
+ }
591
+
592
+ /**
593
+ * {@code locationOnScreen} of a React root less React Native's viewport
594
+ * offset for it: the display-pixel position of the origin
595
+ * {@code measureInWindow} measures from on that surface.
596
+ */
597
+ @NonNull
598
+ static int[] displayOrigin(@NonNull final int[] locationOnScreen,
599
+ final int viewportX, final int viewportY) {
600
+ return new int[] { locationOnScreen[0] - viewportX, locationOnScreen[1] - viewportY };
601
+ }
602
+
603
+ private interface LaneChange {
604
+ @NonNull
605
+ Lane apply(@NonNull Lane previous);
606
+ }
607
+
608
+ private interface Change {
609
+ /** The next state, built from the current one; its version is ignored. */
610
+ @NonNull
611
+ Snapshot apply(@NonNull Snapshot previous);
612
+ }
613
+
614
+ @NonNull
615
+ private static Snapshot withLane(@NonNull final Snapshot previous, final int surface,
616
+ @NonNull final LaneChange change) {
617
+ final TreeMap<Integer, Lane> nextLanes = new TreeMap<>(previous.lanes);
618
+ final Lane prior = nextLanes.get(surface);
619
+ final Lane base = prior == null ? new Lane(NO_COORDINATES, 0, 0, false) : prior;
620
+ nextLanes.put(surface, change.apply(base));
621
+ return previous.withLanes(nextLanes);
622
+ }
623
+
624
+ /**
625
+ * The version moves only when the SERVED set differs, so an app that
626
+ * re-publishes an unchanged layout, or a layout pass that re-reports the
627
+ * same origin, costs the SDK nothing.
628
+ *
629
+ * <p>compute() rather than get-then-put: two writes racing on one display
630
+ * (JS thread, UI thread) would otherwise both read the old version and
631
+ * publish the same new one, so the second change would be invisible to
632
+ * the SDK.
633
+ */
634
+ private void update(final int display, @NonNull final Change change) {
635
+ byDisplay.compute(display, (key, current) -> {
636
+ final Snapshot previous = current == null ? EMPTY : current;
637
+ final Snapshot next = change.apply(previous);
638
+ final int version = Arrays.equals(previous.coordinates, next.coordinates)
639
+ ? previous.version
640
+ : nextVersion(previous.version);
641
+ return next.withVersion(version);
642
+ });
643
+ }
644
+
645
+ /** Every rectangle moved by the origin, saturating rather than wrapping. */
646
+ @NonNull
647
+ private static int[] translate(@NonNull final int[] raw, final int dx, final int dy) {
648
+ final int[] moved = new int[raw.length];
649
+ for (int i = 0; i < raw.length; i++) {
650
+ moved[i] = saturatedAdd(raw[i], i % 2 == 0 ? dx : dy);
651
+ }
652
+ return moved;
653
+ }
654
+
655
+ private static int saturatedAdd(final int a, final int b) {
656
+ final long sum = (long) a + b;
657
+ if (sum > Integer.MAX_VALUE) return Integer.MAX_VALUE;
658
+ if (sum < Integer.MIN_VALUE) return Integer.MIN_VALUE;
659
+ return (int) sum;
660
+ }
661
+
662
+ /**
663
+ * The buffer for {@code display}: {@code [version, count, l, t, r, b, ...]}.
664
+ * A display nothing has secured reports an empty set rather than nothing at
665
+ * all, so the SDK always has a version to compare against.
666
+ */
667
+ @NonNull
668
+ int[] snapshot(final int display) {
669
+ final Snapshot current = byDisplay.get(display);
670
+ if (current == null) {
671
+ return new int[] { INITIAL_VERSION, 0 };
672
+ }
673
+
674
+ final int[] packed = new int[HEADER_INTS + current.coordinates.length];
675
+ packed[0] = current.version;
676
+ packed[1] = current.coordinates.length / COORDINATES_PER_RECTANGLE;
677
+ System.arraycopy(current.coordinates, 0, packed, HEADER_INTS,
678
+ current.coordinates.length);
679
+ return packed;
680
+ }
681
+
682
+ /**
683
+ * Steps the version, skipping the value that means "nothing secured yet".
684
+ * Wrapping matters only in theory — it takes 2^31 changes — but landing
685
+ * back on the initial version would make a real set look identical to the
686
+ * empty one to an SDK that had only ever seen the latter.
687
+ */
688
+ private static int nextVersion(final int current) {
689
+ final int next = current + 1;
690
+ return next == INITIAL_VERSION ? INITIAL_VERSION + 1 : next;
691
+ }
692
+ }