expo-app-blocker 0.1.66 → 0.1.68

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.
@@ -39,6 +39,7 @@ public class ExpoAppBlockerModule: Module {
39
39
  private let maxUsageSteps = 60
40
40
  private let pendingUnlockKey = "appBlocker.pendingUnlock.v1"
41
41
  private let pendingInterceptsKey = "appBlocker.pendingIntercepts.v1"
42
+ private let shieldInvokeCountKey = "appBlocker.shieldInvokeCount.v1"
42
43
  private let minimumTemporaryUnlockMinutes = 1
43
44
  private var didLoadPersistedConfig = false
44
45
 
@@ -203,14 +204,43 @@ public class ExpoAppBlockerModule: Module {
203
204
 
204
205
  Function("drainPendingIntercepts") { () -> [[String: Any]] in
205
206
  guard let defaults = self.sharedDefaults else { return [] }
206
- let queue = defaults.array(forKey: self.pendingInterceptsKey) as? [[String: Any]] ?? []
207
- if !queue.isEmpty {
207
+ // Refresh the cached suite so writes made by the (separate) shield
208
+ // extension process are visible to this long-lived app process.
209
+ defaults.synchronize()
210
+ var queue: [[String: Any]] = []
211
+ if let json = defaults.string(forKey: self.pendingInterceptsKey),
212
+ let data = json.data(using: .utf8),
213
+ let parsed = try? JSONSerialization.jsonObject(with: data) as? [[String: Any]] {
214
+ queue = parsed
215
+ }
216
+ if defaults.string(forKey: self.pendingInterceptsKey) != nil {
208
217
  defaults.removeObject(forKey: self.pendingInterceptsKey)
209
218
  defaults.synchronize()
210
219
  }
220
+ let invokes = defaults.integer(forKey: self.shieldInvokeCountKey)
221
+ NSLog("[appblocker] drainPendingIntercepts: returning \(queue.count) shieldInvokes=\(invokes)")
211
222
  return queue
212
223
  }
213
224
 
225
+ // TEMP diagnostic: surfaces whether the shield extension is being
226
+ // invoked at all (shieldInvokeCount) and the current queue depth,
227
+ // without clearing anything.
228
+ Function("debugInterceptState") { () -> [String: Any] in
229
+ guard let defaults = self.sharedDefaults else { return ["available": false] }
230
+ defaults.synchronize()
231
+ var depth = 0
232
+ if let json = defaults.string(forKey: self.pendingInterceptsKey),
233
+ let data = json.data(using: .utf8),
234
+ let parsed = try? JSONSerialization.jsonObject(with: data) as? [[String: Any]] {
235
+ depth = parsed.count
236
+ }
237
+ return [
238
+ "available": true,
239
+ "shieldInvokeCount": defaults.integer(forKey: self.shieldInvokeCountKey),
240
+ "queueDepth": depth,
241
+ ]
242
+ }
243
+
214
244
  Function("isAppBlocked") { (bundleIdentifier: String) -> Bool in
215
245
  self.ensureLoadedPersistedConfig()
216
246
  guard let config = self.currentBlockConfig else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expo-app-blocker",
3
- "version": "0.1.66",
3
+ "version": "0.1.68",
4
4
  "description": "Expo module for cross-platform app blocking. Android: UsageStatsManager + Overlay. iOS: Screen Time API (FamilyControls + ManagedSettings + DeviceActivity).",
5
5
  "main": "src/index.ts",
6
6
  "types": "src/index.ts",
package/src/index.ts CHANGED
@@ -244,6 +244,16 @@ export function drainPendingIntercepts(): PendingIntercept[] {
244
244
  return NativeModule.drainPendingIntercepts() ?? [];
245
245
  }
246
246
 
247
+ /**
248
+ * TEMP diagnostic: returns `{ shieldInvokeCount, queueDepth }` (iOS) without
249
+ * clearing the queue. `shieldInvokeCount` proves whether the shield
250
+ * extension is being invoked by the OS at all.
251
+ */
252
+ export function debugInterceptState(): Record<string, unknown> {
253
+ if (Platform.OS !== "ios" && Platform.OS !== "android") return {};
254
+ return (NativeModule.debugInterceptState?.() as Record<string, unknown>) ?? {};
255
+ }
256
+
247
257
  export function addPendingUnlockListener(
248
258
  handler: () => void
249
259
  ): { remove: () => void } | null {
@@ -49,22 +49,54 @@ class ShieldConfigurationExtension: ShieldConfigurationDataSource {
49
49
  // those bursts into one logical block event.
50
50
  private let pendingInterceptsKey = "appBlocker.pendingIntercepts.v1"
51
51
  private let lastInterceptTsKey = "appBlocker.lastInterceptTs.v1"
52
+ private let shieldInvokeCountKey = "appBlocker.shieldInvokeCount.v1"
52
53
  private let interceptDebounceMs: Double = 2_000
53
54
  private let maxPendingIntercepts = 200
54
55
 
55
56
  private func recordIntercept(appName: String) {
56
- guard let defaults = UserDefaults(suiteName: appGroupIdentifier) else { return }
57
+ guard let defaults = UserDefaults(suiteName: appGroupIdentifier) else {
58
+ NSLog("[appblocker] recordIntercept: no app-group defaults")
59
+ return
60
+ }
61
+ // The extension is a short-lived process; pull a fresh view before
62
+ // read-modify-write so we don't clobber the app's drain.
63
+ defaults.synchronize()
64
+
65
+ // Diagnostic: count every shield render, before any debounce/branch,
66
+ // so the app can confirm the extension is actually being invoked.
67
+ let invokes = defaults.integer(forKey: shieldInvokeCountKey) + 1
68
+ defaults.set(invokes, forKey: shieldInvokeCountKey)
69
+
57
70
  let nowMs = Date().timeIntervalSince1970 * 1000.0
58
71
  let lastMs = defaults.double(forKey: lastInterceptTsKey)
59
- if lastMs > 0, (nowMs - lastMs) < interceptDebounceMs { return }
72
+ if lastMs > 0, (nowMs - lastMs) < interceptDebounceMs {
73
+ defaults.synchronize()
74
+ NSLog("[appblocker] recordIntercept: debounced (invokes=\(invokes))")
75
+ return
76
+ }
60
77
 
61
- var queue = defaults.array(forKey: pendingInterceptsKey) as? [[String: Any]] ?? []
78
+ // Persist as a JSON string the most robust representation to carry a
79
+ // nested array across the extension→app process boundary via the App
80
+ // Group (a raw [[String: Any]] does not reliably survive).
81
+ var queue: [[String: Any]] = []
82
+ if let json = defaults.string(forKey: pendingInterceptsKey),
83
+ let data = json.data(using: .utf8),
84
+ let parsed = try? JSONSerialization.jsonObject(with: data) as? [[String: Any]] {
85
+ queue = parsed
86
+ }
62
87
  queue.append(["appName": appName, "interceptedAt": nowMs])
63
88
  if queue.count > maxPendingIntercepts {
64
89
  queue = Array(queue.suffix(maxPendingIntercepts))
65
90
  }
66
- defaults.set(queue, forKey: pendingInterceptsKey)
91
+ if let data = try? JSONSerialization.data(withJSONObject: queue),
92
+ let json = String(data: data, encoding: .utf8) {
93
+ defaults.set(json, forKey: pendingInterceptsKey)
94
+ }
67
95
  defaults.set(nowMs, forKey: lastInterceptTsKey)
96
+ // Force a flush to cfprefsd — the OS may suspend/kill the extension
97
+ // immediately after rendering, before an unflushed write persists.
98
+ defaults.synchronize()
99
+ NSLog("[appblocker] recordIntercept: queued \(appName), depth=\(queue.count) invokes=\(invokes)")
68
100
  }
69
101
 
70
102
  private func makeConfig(appName: String) -> ShieldConfiguration {