@replayd/capacitor-android 0.1.10 → 0.1.11

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.
@@ -3,6 +3,7 @@ package com.replayd.capacitor
3
3
  import android.app.ActivityManager
4
4
  import android.app.ApplicationExitInfo
5
5
  import android.content.Context
6
+ import android.content.SharedPreferences
6
7
  import android.os.Build
7
8
  import com.getcapacitor.JSArray
8
9
  import com.getcapacitor.JSObject
@@ -10,6 +11,7 @@ import com.getcapacitor.Plugin
10
11
  import com.getcapacitor.PluginCall
11
12
  import com.getcapacitor.PluginMethod
12
13
  import com.getcapacitor.annotation.CapacitorPlugin
14
+ import org.json.JSONArray
13
15
  import org.json.JSONObject
14
16
  import java.io.File
15
17
 
@@ -17,7 +19,8 @@ import java.io.File
17
19
  * T-154: native crashes and ANRs for replayd. An uncaught Java/Kotlin exception is written to app storage by a handler
18
20
  * chained before the previous one (≤ 64 KB each, ≤ 5 kept); on API 30+ the system's ApplicationExitInfo adds ANRs and
19
21
  * native crashes. The SDK's Capacitor chunk calls `pending()` on the next launch and sends them as error events,
20
- * linked to the session id it last reported with `setSession()`. Nothing leaves the device from here.
22
+ * linked to the session that was current when they happened (a bounded timeline of the ids reported with
23
+ * `setSession()`; an exit older than the timeline has no sid). Nothing leaves the device from here.
21
24
  */
22
25
  @CapacitorPlugin(name = "ReplaydNative")
23
26
  class ReplaydNativePlugin : Plugin() {
@@ -28,7 +31,7 @@ class ReplaydNativePlugin : Plugin() {
28
31
  @PluginMethod
29
32
  fun setSession(call: PluginCall) {
30
33
  val sid = call.getString("sid")
31
- if (sid != null && sid.length <= 64) CrashStore.prefs(context).edit().putString(CrashStore.SID, sid).apply()
34
+ if (sid != null && sid.length <= 64) CrashStore.recordSession(context, sid, System.currentTimeMillis())
32
35
  call.resolve()
33
36
  }
34
37
 
@@ -54,8 +57,11 @@ class ReplaydNativePlugin : Plugin() {
54
57
  .filter { it.timestamp > seen && (it.reason == ApplicationExitInfo.REASON_ANR || it.reason == ApplicationExitInfo.REASON_CRASH_NATIVE) }
55
58
  .take(5)
56
59
  if (list.isNotEmpty()) prefs.edit().putLong(CrashStore.EXIT_SEEN, list.maxOf { it.timestamp }).apply()
57
- val sid = prefs.getString(CrashStore.SID, null)
60
+ // the SDK reports the new launch's session before asking for these, so each exit is linked by its own
61
+ // timestamp to the session interval it happened in (unknown → null), never to the current session
62
+ val timeline = CrashStore.sessions(prefs)
58
63
  return list.map { info ->
64
+ val sid = CrashStore.sidAt(timeline, info.timestamp)
59
65
  val anr = info.reason == ApplicationExitInfo.REASON_ANR
60
66
  val trace = try {
61
67
  info.traceInputStream?.use { s -> String(s.readBytes().take(CrashStore.MAX_BYTES).toByteArray()) }
@@ -88,12 +94,47 @@ class ReplaydNativePlugin : Plugin() {
88
94
  internal object CrashStore {
89
95
  const val SID = "sid"
90
96
  const val EXIT_SEEN = "exit_seen"
97
+ const val SESSIONS = "sessions"
91
98
  const val MAX_BYTES = 64 * 1024
92
99
  private const val MAX_FILES = 5
100
+ private const val MAX_SESSIONS = 32
93
101
  @Volatile private var installed = false
94
102
 
95
103
  fun prefs(ctx: Context) = ctx.getSharedPreferences("replayd", Context.MODE_PRIVATE)
96
104
 
105
+ /** Session intervals, oldest first: (sid, first time it was reported). Bounded to [MAX_SESSIONS]. */
106
+ fun sessions(p: SharedPreferences): List<Pair<String, Long>> {
107
+ return try {
108
+ val a = JSONArray(p.getString(SESSIONS, null) ?: return emptyList())
109
+ (0 until a.length()).mapNotNull { i ->
110
+ val e = a.optJSONArray(i)
111
+ val s = e?.optString(0, "")
112
+ if (e == null || s.isNullOrEmpty() || s.length > 64) null else s to e.optLong(1, 0L)
113
+ }
114
+ } catch (_: Throwable) {
115
+ emptyList()
116
+ }
117
+ }
118
+
119
+ /** The session that was current at [at], or null when the timeline does not reach back that far. */
120
+ fun sidAt(timeline: List<Pair<String, Long>>, at: Long): String? = timeline.lastOrNull { it.second <= at }?.first
121
+
122
+ /** Remembers the current session: [SID] for crash files, and a new interval when the id changed. */
123
+ @Synchronized
124
+ fun recordSession(ctx: Context, sid: String, now: Long) {
125
+ val p = prefs(ctx)
126
+ val list = sessions(p).toMutableList()
127
+ val e = p.edit().putString(SID, sid)
128
+ if (list.lastOrNull()?.first != sid) {
129
+ list.add(sid to now)
130
+ while (list.size > MAX_SESSIONS) list.removeAt(0)
131
+ val a = JSONArray()
132
+ for ((s, t) in list) a.put(JSONArray().put(s).put(t))
133
+ e.putString(SESSIONS, a.toString())
134
+ }
135
+ e.apply()
136
+ }
137
+
97
138
  private fun dir(ctx: Context) = File(ctx.filesDir, "replayd-crashes").apply { mkdirs() }
98
139
 
99
140
  fun install(ctx: Context) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@replayd/capacitor-android",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "description": "Capacitor Android plugin for replayd: native (Java/Kotlin) crashes and ANRs, reported on the next launch",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -25,12 +25,12 @@
25
25
  }
26
26
  },
27
27
  "peerDependencies": {
28
- "@replayd/core": "^0.1.10"
28
+ "@replayd/core": "^0.1.11"
29
29
  },
30
30
  "devDependencies": {
31
- "@replayd/core": "0.1.10"
31
+ "@replayd/core": "0.1.11"
32
32
  },
33
- "gitHead": "5016777ab966b8b263944744b00b83fd7612aad9",
33
+ "gitHead": "3d25e9b1b327f080b8b47af7edb36056c0613ad8",
34
34
  "scripts": {
35
35
  "build": "node ../../scripts/build.mjs build.json && tsc -p tsconfig.build.json"
36
36
  }