@craft-native/android 0.0.436 → 0.0.442

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.
@@ -0,0 +1,156 @@
1
+ package com.craft.runtime
2
+
3
+ import android.content.Context
4
+ import android.hardware.Sensor
5
+ import android.hardware.SensorEvent
6
+ import android.hardware.SensorEventListener
7
+ import android.hardware.SensorManager
8
+ import android.os.Handler
9
+ import android.os.Looper
10
+ import android.os.PowerManager
11
+ import org.json.JSONArray
12
+ import org.json.JSONObject
13
+ import java.io.File
14
+ import java.util.UUID
15
+
16
+ /** Native-only journal: JS suspension cannot drop pressure readings from a route. */
17
+ object CraftRecordingPressure {
18
+ private fun file(context: Context, id: String): File {
19
+ val digest = java.security.MessageDigest.getInstance("SHA-256").digest(id.toByteArray(Charsets.UTF_8))
20
+ return File(context.filesDir, "craft-pressure-${digest.joinToString("") { "%02x".format(it) }}.jsonl")
21
+ }
22
+ fun requireAvailable(context: Context) {
23
+ val config = context.assets.open("craft.config.json").bufferedReader().use { JSONObject(it.readText()) }
24
+ if (!config.optBoolean("enableBarometer") || !config.optBoolean("enableBackgroundLocation"))
25
+ throw CraftRecordingException("CAPABILITY_DISABLED", "Recording pressure requires enableBarometer and enableBackgroundLocation")
26
+ val manager = context.getSystemService(SensorManager::class.java)
27
+ if (manager?.getDefaultSensor(Sensor.TYPE_PRESSURE, true) == null && manager?.getDefaultSensor(Sensor.TYPE_PRESSURE) == null)
28
+ throw CraftRecordingException("UNAVAILABLE", "Barometer is not present")
29
+ }
30
+ // Callers also hold the recording-store lock to serialize owner/ACK changes.
31
+ fun append(context: Context, id: String, event: JSONObject) {
32
+ val bytes = (event.put("recordingId", id).toString() + "\n").toByteArray(Charsets.UTF_8)
33
+ java.io.FileOutputStream(file(context, id), true).use { it.write(bytes); it.fd.sync() }
34
+ }
35
+ fun read(context: Context, id: String): JSONArray {
36
+ val result = JSONArray()
37
+ val source = file(context, id)
38
+ if (id.isEmpty() || !source.exists()) return result
39
+ source.forEachLine { line ->
40
+ if (line.isNotBlank()) {
41
+ val event = JSONObject(line)
42
+ check(event.getString("recordingId") == id) { "Invalid pressure recording owner" }
43
+ result.put(event)
44
+ }
45
+ }
46
+ return result
47
+ }
48
+ fun delete(context: Context, id: String) {
49
+ val source = file(context, id)
50
+ if (source.exists() && !source.delete()) throw CraftRecordingException("RECORDING_STORAGE_ERROR", "Unable to remove acknowledged pressure recording")
51
+ }
52
+ }
53
+
54
+ internal interface CraftRecordingPressureSource {
55
+ fun open(sample: (Float, Long) -> Unit): AutoCloseable
56
+ }
57
+
58
+ private class AndroidRecordingPressureSource(private val context: Context) : CraftRecordingPressureSource {
59
+ override fun open(sample: (Float, Long) -> Unit): AutoCloseable {
60
+ CraftRecordingPressure.requireAvailable(context)
61
+ val manager = context.getSystemService(SensorManager::class.java)
62
+ ?: throw IllegalStateException("Sensor manager unavailable")
63
+ val sensor = manager.getDefaultSensor(Sensor.TYPE_PRESSURE, true)
64
+ ?: manager.getDefaultSensor(Sensor.TYPE_PRESSURE)
65
+ ?: throw IllegalStateException("Barometer unavailable")
66
+ // A non-wake-up sensor cannot deliver continuously during CPU suspend.
67
+ // Only the explicitly requested, unpaused foreground recording owns this lock.
68
+ val wake = if (!sensor.isWakeUpSensor) context.getSystemService(PowerManager::class.java)
69
+ .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "craft:recording-pressure").apply { setReferenceCounted(false) } else null
70
+ val listener = object : SensorEventListener {
71
+ override fun onSensorChanged(event: SensorEvent) { sample(event.values.firstOrNull() ?: Float.NaN, event.timestamp) }
72
+ override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {}
73
+ }
74
+ try {
75
+ wake?.acquire()
76
+ check(manager.registerListener(listener, sensor, 1_000_000, Handler(Looper.getMainLooper()))) { "Pressure registration rejected" }
77
+ } catch (error: Exception) {
78
+ manager.unregisterListener(listener)
79
+ if (wake?.isHeld == true) wake.release()
80
+ throw error
81
+ }
82
+ return AutoCloseable {
83
+ try { manager.unregisterListener(listener) }
84
+ finally { if (wake?.isHeld == true) wake.release() }
85
+ }
86
+ }
87
+ }
88
+
89
+ /** One service-owned segment, independent of native screen and WebView lifetimes. */
90
+ internal class CraftRecordingPressureSession(
91
+ private val context: Context,
92
+ val id: String,
93
+ private val source: CraftRecordingPressureSource = AndroidRecordingPressureSource(context),
94
+ ) {
95
+ val epoch = CraftLocationRecordingStore.pressureEpoch
96
+ private var registration: AutoCloseable? = null
97
+ private var closed = false
98
+ private var started = false
99
+ private var reference: Float? = null
100
+ private var lastTimestamp: Long? = null
101
+ private var segment = UUID.randomUUID().toString()
102
+
103
+ private fun ownsRecording() = epoch == CraftLocationRecordingStore.pressureEpoch && CraftLocationRecordingStore.recordingId(context) == id &&
104
+ CraftLocationRecordingStore.isActive(context) && !CraftLocationRecordingStore.isPaused(context) &&
105
+ CraftLocationRecordingStore.pressureEnabled(context)
106
+
107
+ fun start() = synchronized(CraftLocationRecordingStore) {
108
+ if (closed || started || !ownsRecording()) return@synchronized
109
+ started = true
110
+ try {
111
+ write(JSONObject().put("state", "segment").put("reason", "startedOrRecovered"))
112
+ val acquired = source.open(::sample)
113
+ if (closed) acquired.close() else registration = acquired
114
+ } catch (error: Exception) { fail(error) }
115
+ }
116
+
117
+ private fun write(event: JSONObject) {
118
+ CraftRecordingPressure.append(context, id, event.put("segmentId", segment).put("timestamp", System.currentTimeMillis()))
119
+ }
120
+
121
+ private fun sample(pressure: Float, timestamp: Long) = synchronized(CraftLocationRecordingStore) {
122
+ if (closed || !ownsRecording()) return@synchronized
123
+ try {
124
+ require(pressure.isFinite() && pressure > 0 && timestamp >= 0) { "Invalid pressure sample" }
125
+ val previous = lastTimestamp
126
+ if (previous != null && timestamp <= previous) return@synchronized
127
+ if (previous != null && timestamp - previous < 1_000_000_000L) return@synchronized
128
+ if (previous != null && timestamp - previous > 3_000_000_000L) {
129
+ segment = UUID.randomUUID().toString()
130
+ reference = null
131
+ write(JSONObject().put("state", "segment").put("reason", "sampleGap").put("timestampMs", timestamp / 1_000_000.0))
132
+ }
133
+ val baseline = reference ?: pressure.also { reference = it }
134
+ val altitude = SensorManager.getAltitude(baseline, pressure)
135
+ require(altitude.isFinite()) { "Invalid pressure altitude" }
136
+ write(JSONObject().put("state", "sample").put("pressureHpa", pressure)
137
+ .put("referencePressureHpa", baseline).put("relativeAltitudeMeters", altitude)
138
+ .put("timestampMs", timestamp / 1_000_000.0))
139
+ lastTimestamp = timestamp
140
+ } catch (error: Exception) { fail(error) }
141
+ }
142
+
143
+ private fun fail(error: Exception) {
144
+ close()
145
+ runCatching { write(JSONObject().put("state", "stopped").put("reason", "sensorOrStorageError")) }
146
+ CraftLocationRecordingStore.failPressure(context, id, error.message ?: "Pressure recording failed")
147
+ }
148
+
149
+ fun close() {
150
+ if (closed) return
151
+ closed = true
152
+ runCatching { registration?.close() }
153
+ .onFailure { android.util.Log.e("CraftLocation", "Unable to unregister pressure listener", it) }
154
+ registration = null
155
+ }
156
+ }
@@ -110,20 +110,21 @@ object CraftLocationRecordingStore {
110
110
  if (!acknowledged(context, id)) return
111
111
  val saved = archive(context, id)
112
112
  if (saved.exists() && !saved.delete()) throw CraftRecordingException("RECORDING_STORAGE_ERROR", "Unable to remove acknowledged recording")
113
+ CraftRecordingPressure.delete(context, id)
113
114
  if (file(context).exists() && !file(context).delete()) throw CraftRecordingException("RECORDING_STORAGE_ERROR", "Unable to remove acknowledged samples")
114
- edit(context) { it.remove("id").remove("startedAt").putBoolean("active", false).putBoolean("paused", false) }
115
+ edit(context) { it.remove("id").remove("startedAt").remove("barometer").remove("barometerError").putBoolean("active", false).putBoolean("paused", false) }
115
116
  listener = null
116
117
  }
117
118
 
118
119
  @Synchronized
119
- fun start(context: Context, id: String, startedAt: Long, powerMode: String = "default") {
120
+ fun start(context: Context, id: String, startedAt: Long, powerMode: String = "default", barometer: Boolean = false) {
120
121
  recover(context)
121
122
  if (isActive(context)) throw CraftRecordingException("RECORDING_ALREADY_ACTIVE", "A location recording is already active")
122
123
  if (id == recordingId(context) || archive(context, id).exists() || acknowledged(context, id))
123
124
  throw CraftRecordingException("RECORDING_CONFLICT", "Recording IDs cannot be reused")
124
125
  archiveCompleted(context)
125
126
  file(context).writeText("")
126
- edit(context) { it.putString("id", id).putBoolean("active", true).putBoolean("paused", false).putLong("startedAt", startedAt).putString("powerMode", powerMode).remove("powerSaver") }
127
+ edit(context) { it.putString("id", id).putBoolean("barometer", barometer).remove("barometerError").putBoolean("active", true).putBoolean("paused", false).putLong("startedAt", startedAt).putString("powerMode", powerMode).remove("powerSaver") }
127
128
  }
128
129
 
129
130
  private val POWER_MODES = setOf("default", "saver", "auto")
@@ -187,7 +188,10 @@ object CraftLocationRecordingStore {
187
188
  fun updatePause(context: Context, paused: Boolean, options: JSONObject?): JSONObject {
188
189
  requireOwner(context, options)
189
190
  if (!isActive(context)) throw CraftRecordingException("NO_ACTIVE_RECORDING", "No active location recording")
191
+ if (paused && !isPaused(context)) pressureEvent(context, "paused")
190
192
  commit(context, true, paused)
193
+ pressureEpoch += 1
194
+ changed?.invoke()
191
195
  return state(context)
192
196
  }
193
197
  @Synchronized fun setPaused(context: Context, paused: Boolean) { updatePause(context, paused, null) }
@@ -196,8 +200,10 @@ object CraftLocationRecordingStore {
196
200
  fun finish(context: Context, options: JSONObject?): JSONObject {
197
201
  completed(context, expected(options))?.let { return it }
198
202
  requireOwner(context, options)
203
+ if (isActive(context)) pressureEvent(context, "stopped")
199
204
  val stopped = state(context, includeLocations = true)
200
205
  commit(context, false, false)
206
+ changed?.invoke()
201
207
  listener = null
202
208
  return stopped.put("active", false).put("paused", false).put("effectivePowerMode", "default")
203
209
  }
@@ -213,9 +219,10 @@ object CraftLocationRecordingStore {
213
219
  val marker = File(saved.path.replace("-completed-", "-ack-"))
214
220
  if (!marker.exists()) writeAtomic(marker, id)
215
221
  if (saved.exists() && !saved.delete()) throw CraftRecordingException("RECORDING_STORAGE_ERROR", "Unable to remove completed recording")
222
+ CraftRecordingPressure.delete(context, id)
216
223
  if (current) {
217
224
  if (file(context).exists() && !file(context).delete()) throw CraftRecordingException("RECORDING_STORAGE_ERROR", "Unable to remove recording samples")
218
- edit(context) { it.remove("id").remove("startedAt").putBoolean("active", false).putBoolean("paused", false) }
225
+ edit(context) { it.remove("id").remove("startedAt").remove("barometer").remove("barometerError").putBoolean("active", false).putBoolean("paused", false) }
219
226
  listener = null
220
227
  }
221
228
  return true
@@ -236,7 +243,11 @@ object CraftLocationRecordingStore {
236
243
  if (requested !is String || requested.isBlank()) throw CraftRecordingException("INVALID_ARGUMENT", "recordingId must be a nonempty recording ID")
237
244
  requested
238
245
  } else java.util.UUID.randomUUID().toString()
239
- start(context, id, System.currentTimeMillis(), requestedPowerMode(options, "default"))
246
+ if (options.has("barometer") && options.opt("barometer") !is Boolean)
247
+ throw CraftRecordingException("INVALID_ARGUMENT", "barometer must be boolean")
248
+ val pressure = options.optBoolean("barometer", false)
249
+ if (pressure) CraftRecordingPressure.requireAvailable(context)
250
+ start(context, id, System.currentTimeMillis(), requestedPowerMode(options, "default"), pressure)
240
251
  try { ContextCompat.startForegroundService(context, Intent(context, LocationRecordingService::class.java).setAction(LocationRecordingService.ACTION_START).putExtra("recordingId", id)) }
241
252
  catch (error: Exception) { finish(context, JSONObject().put("expectedId", id)); throw error }
242
253
  state(context)
@@ -267,7 +278,16 @@ object CraftLocationRecordingStore {
267
278
  context.startService(Intent(context, LocationRecordingService::class.java).setAction(LocationRecordingService.ACTION_STOP).putExtra("recordingId", stopped.optString("id")))
268
279
  stopped
269
280
  }
270
- "getRecordingState" -> state(context, options = options)
281
+ "getRecordingState" -> {
282
+ if (options.has("includeBarometerSamples") && options.opt("includeBarometerSamples") !is Boolean)
283
+ throw CraftRecordingException("INVALID_ARGUMENT", "includeBarometerSamples must be boolean")
284
+ state(context, options = options).also { result ->
285
+ if (options.optBoolean("includeBarometerSamples")) {
286
+ val id = result.optString("id")
287
+ result.put("barometerEvents", CraftRecordingPressure.read(context, id))
288
+ }
289
+ }
290
+ }
271
291
  "readRecording" -> read(context, options)
272
292
  "acknowledgeRecording" -> acknowledge(context, options)
273
293
  else -> throw CraftRecordingException("UNKNOWN_ACTION", "Unsupported Location method")
@@ -281,6 +301,19 @@ object CraftLocationRecordingStore {
281
301
  @Synchronized fun isActive(context: Context) = prefs(context).getBoolean("active", false)
282
302
  @Synchronized fun isPaused(context: Context) = prefs(context).getBoolean("paused", false)
283
303
  @Synchronized fun recordingId(context: Context): String? = prefs(context).getString("id", null)
304
+ @Volatile internal var pressureEpoch = 0L
305
+ @Volatile internal var changed: (() -> Unit)? = null
306
+ @Synchronized internal fun pressureEnabled(context: Context) = prefs(context).getBoolean("barometer", false)
307
+ @Synchronized internal fun failPressure(context: Context, id: String, message: String) {
308
+ if (recordingId(context) != id || !isActive(context)) return
309
+ edit(context) { it.putBoolean("active", false).putBoolean("paused", false).putString("barometerError", message) }
310
+ changed?.invoke()
311
+ }
312
+ private fun pressureEvent(context: Context, reason: String) {
313
+ val id = recordingId(context) ?: return
314
+ if (pressureEnabled(context)) CraftRecordingPressure.append(context, id,
315
+ JSONObject().put("state", "stopped").put("reason", reason).put("timestamp", System.currentTimeMillis()))
316
+ }
284
317
  @Volatile var listener: ((android.location.Location, String) -> Unit)? = null
285
318
  @Volatile var nativeListener: ((android.location.Location, String) -> Unit)? = null
286
319
 
@@ -329,7 +362,7 @@ object CraftLocationRecordingStore {
329
362
  @Synchronized
330
363
  fun state(context: Context, includeLocations: Boolean = false, options: JSONObject? = null): JSONObject {
331
364
  completed(context, expected(options))?.let {
332
- if (!includeLocations) it.remove("locations")
365
+ if (!includeLocations) { it.remove("locations"); it.remove("barometerEvents") }
333
366
  return it
334
367
  }
335
368
  requireOwner(context, options)
@@ -343,7 +376,12 @@ object CraftLocationRecordingStore {
343
376
  put("sampleCount", locations.length())
344
377
  put("powerMode", powerMode(context))
345
378
  put("effectivePowerMode", if (isPowerSaver(context)) "saver" else "default")
346
- if (includeLocations) put("locations", locations)
379
+ put("barometer", pressureEnabled(context))
380
+ values.getString("barometerError", null)?.let { put("barometerError", it) }
381
+ if (includeLocations) {
382
+ put("locations", locations)
383
+ if (pressureEnabled(context)) put("barometerEvents", CraftRecordingPressure.read(context, recordingId(context) ?: ""))
384
+ }
347
385
  }
348
386
  }
349
387
  }
@@ -355,9 +393,33 @@ class LocationRecordingService : Service() {
355
393
  private var callbackSaver = false
356
394
  private var serviceStartId = 0
357
395
  private var powerReceiver: BroadcastReceiver? = null
396
+ private var pressure: CraftRecordingPressureSession? = null
397
+ private val mainHandler = android.os.Handler(Looper.getMainLooper())
398
+ private val pressureChanged: () -> Unit = {
399
+ mainHandler.post {
400
+ if (!CraftLocationRecordingStore.isActive(this)) { stopUpdates(); stopSelfResult(serviceStartId) }
401
+ else syncPressure()
402
+ }
403
+ Unit
404
+ }
405
+
406
+ private fun syncPressure() = synchronized(CraftLocationRecordingStore) {
407
+ val id = CraftLocationRecordingStore.recordingId(this)
408
+ val wanted = id != null && CraftLocationRecordingStore.isActive(this) &&
409
+ !CraftLocationRecordingStore.isPaused(this) && CraftLocationRecordingStore.pressureEnabled(this)
410
+ if (wanted && pressure?.id == id && pressure?.epoch == CraftLocationRecordingStore.pressureEpoch) return@synchronized
411
+ pressure?.close()
412
+ pressure = null
413
+ if (wanted) {
414
+ val next = CraftRecordingPressureSession(this, id!!)
415
+ pressure = next
416
+ next.start()
417
+ }
418
+ }
358
419
 
359
420
  override fun onCreate() {
360
421
  super.onCreate()
422
+ CraftLocationRecordingStore.changed = pressureChanged
361
423
  val manager = getSystemService(NotificationManager::class.java)
362
424
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
363
425
  manager.createNotificationChannel(NotificationChannel(CHANNEL, "Activity recording", NotificationManager.IMPORTANCE_LOW))
@@ -417,6 +479,7 @@ class LocationRecordingService : Service() {
417
479
  val saver = CraftLocationRecordingStore.isPowerSaver(this)
418
480
  if (callback != null && callbackRecordingId == id && callbackSaver == saver) {
419
481
  syncPowerReceiver()
482
+ syncPressure()
420
483
  return true
421
484
  }
422
485
  stopUpdates()
@@ -446,6 +509,7 @@ class LocationRecordingService : Service() {
446
509
  callbackRecordingId = id
447
510
  callbackSaver = saver
448
511
  syncPowerReceiver()
512
+ syncPressure()
449
513
  return try {
450
514
  client.requestLocationUpdates(request, nextCallback, Looper.getMainLooper())
451
515
  .addOnFailureListener {
@@ -460,13 +524,15 @@ class LocationRecordingService : Service() {
460
524
  }
461
525
  true
462
526
  } catch (error: Exception) {
463
- callback = null
527
+ stopUpdates()
464
528
  runCatching { CraftLocationRecordingStore.finish(this, JSONObject().put("expectedId", id)) }
465
529
  false
466
530
  }
467
531
  }
468
532
 
469
533
  private fun stopUpdates() {
534
+ pressure?.close()
535
+ pressure = null
470
536
  callback?.let { client.removeLocationUpdates(it) }
471
537
  callback = null
472
538
  callbackRecordingId = null
@@ -504,6 +570,8 @@ class LocationRecordingService : Service() {
504
570
  }
505
571
 
506
572
  override fun onDestroy() {
573
+ if (CraftLocationRecordingStore.changed === pressureChanged) CraftLocationRecordingStore.changed = null
574
+ mainHandler.removeCallbacksAndMessages(null)
507
575
  stopUpdates()
508
576
  super.onDestroy()
509
577
  }
@@ -4563,6 +4563,11 @@ class MainActivity : AppCompatActivity() {
4563
4563
  val pressUnderlayShowHandlers = mutableMapOf<View, String>()
4564
4564
  val pressUnderlayHideHandlers = mutableMapOf<View, String>()
4565
4565
  val longPressHandlers = mutableMapOf<View, String>()
4566
+ val accessibilityActionHandlers = mutableMapOf<View, String>()
4567
+ // TalkBack actions are added by id, so removing them on a reconcile
4568
+ // needs the ids back; without this a renamed action stacks up beside
4569
+ // the old one every render.
4570
+ val accessibilityActionIds = mutableMapOf<View, List<Int>>()
4566
4571
  val scrollHandlers = mutableMapOf<View, String>()
4567
4572
  val scrollBeginHandlers = mutableMapOf<View, String>()
4568
4573
  val scrollEndHandlers = mutableMapOf<View, String>()
@@ -4596,6 +4601,9 @@ class MainActivity : AppCompatActivity() {
4596
4601
  val capabilityTimeouts = mutableMapOf<String, Runnable>()
4597
4602
  val audio = CraftNativeAudio()
4598
4603
  val sounds = CraftNativeSounds(view.context)
4604
+ var motion: CraftNativeMotion? = null
4605
+ var barometer: CraftNativeBarometer? = null
4606
+ var pedometer: CraftNativePedometer? = null
4599
4607
  val soundRequestCancellers = mutableMapOf<String, () -> Unit>()
4600
4608
  /** Snapshot writes awaiting the writer thread; close rejects them once. */
4601
4609
  val pendingSyncResults = mutableSetOf<String>()
@@ -4617,6 +4625,9 @@ class MainActivity : AppCompatActivity() {
4617
4625
  soundRequestCancellers.values.toList().forEach { it() }
4618
4626
  soundRequestCancellers.clear()
4619
4627
  sounds.close()
4628
+ motion?.close()
4629
+ barometer?.close()
4630
+ pedometer?.close()
4620
4631
  // Evaluation completion does not mean its outbound messages have
4621
4632
  // reached the host. Close only after the ordered channel barrier,
4622
4633
  // so onDestroy's final writes run before navigation continues.
@@ -4688,6 +4699,10 @@ class MainActivity : AppCompatActivity() {
4688
4699
  private var hapticsEnabled = false
4689
4700
  private var clipboardEnabled = false
4690
4701
  private var shakeEnabled = false
4702
+ private var motionSensorsEnabled = false
4703
+ private var barometerEnabled = false
4704
+ private var pedometerEnabled = false
4705
+ private var motionForeground = false
4691
4706
  internal var nativeAppState = "inactive"
4692
4707
  private set
4693
4708
  private var deliveredInitialDeepLink = false
@@ -4761,6 +4776,9 @@ class MainActivity : AppCompatActivity() {
4761
4776
  hapticsEnabled = config.optBoolean("enableHaptics", false)
4762
4777
  clipboardEnabled = config.optBoolean("enableClipboard", false)
4763
4778
  shakeEnabled = config.optBoolean("enableShakeGesture", false)
4779
+ motionSensorsEnabled = config.optBoolean("enableMotionSensors", false)
4780
+ barometerEnabled = config.optBoolean("enableBarometer", false)
4781
+ pedometerEnabled = config.optBoolean("enablePedometer", false)
4764
4782
  // Normally started already by its content provider; this covers a
4765
4783
  // process that skipped providers (instrumentation).
4766
4784
  CraftCrashReporter.start(this)
@@ -5153,6 +5171,30 @@ class MainActivity : AppCompatActivity() {
5153
5171
  activeScreen.view.onLayoutChanged = {
5154
5172
  if (activeScreen.active) emitLayoutEvents(activeScreen)
5155
5173
  }
5174
+ if (motionSensorsEnabled && !headless) {
5175
+ activeScreen.motion = CraftNativeMotion(CraftAndroidMotionSource(this)).apply {
5176
+ emit = { detail ->
5177
+ if (activeScreen.active && motionForeground) send(activeScreen, "CRAFT_EVENT",
5178
+ JSONObject().put("event", "craftRawMotionUpdate").put("detail", detail))
5179
+ }
5180
+ }
5181
+ }
5182
+ if (barometerEnabled && !headless) {
5183
+ activeScreen.barometer = CraftNativeBarometer(CraftAndroidMotionSource(this)).apply {
5184
+ emit = { detail ->
5185
+ if (activeScreen.active) send(activeScreen, "CRAFT_EVENT",
5186
+ JSONObject().put("event", "craftBarometerUpdate").put("detail", detail))
5187
+ }
5188
+ }
5189
+ }
5190
+ if (pedometerEnabled && !headless) {
5191
+ activeScreen.pedometer = CraftNativePedometer(CraftAndroidMotionSource(this), capabilities::pedometerPermissionGranted).apply {
5192
+ emit = { detail ->
5193
+ if (activeScreen.active) send(activeScreen, "CRAFT_EVENT",
5194
+ JSONObject().put("event", "craftPedometerUpdate").put("detail", detail))
5195
+ }
5196
+ }
5197
+ }
5156
5198
  activeScreen.sounds.emit = { payload ->
5157
5199
  if (activeScreen.active) send(activeScreen, "SOUND_EVENT", payload)
5158
5200
  }
@@ -5534,6 +5576,9 @@ class MainActivity : AppCompatActivity() {
5534
5576
  private fun capabilityNames(): JSONArray = capabilities.names().apply {
5535
5577
  services.names().forEach { name -> put(name) }
5536
5578
  put("audio")
5579
+ if (motionSensorsEnabled) put("rawMotionSensors")
5580
+ if (barometerEnabled) put("barometer")
5581
+ if (pedometerEnabled) put("pedometer")
5537
5582
  put("viewFocus")
5538
5583
  put("viewMeasure")
5539
5584
  put("viewScroll")
@@ -6952,6 +6997,7 @@ class MainActivity : AppCompatActivity() {
6952
6997
  CraftNativePointers.configure(control, props, style, control is CraftNativeFlexLayout || control is CraftNativeImageBackgroundView ||
6953
6998
  control is CraftNativeKeyboardAvoidingView || control is CraftNativeScrollView || control is CraftNativeRefreshableFlatList || control is CraftNativeModalView)
6954
6999
  applyAccessibility(control, props, type, explicitKey ?: protocolId ?: path)
7000
+ applyAccessibilityActions(screen, control, props, events, longPressDisabled)
6955
7001
  // One element across an animated change (craft.animation.next).
6956
7002
  control.transitionName = craftNativeOptionalString(props.opt("transitionID"))?.takeIf { it.isNotBlank() }
6957
7003
  val layoutHandler = craftNativeEventName(events, "onLayout")
@@ -7854,6 +7900,54 @@ class MainActivity : AppCompatActivity() {
7854
7900
  field.setSelection(minOf(boundedStart, boundedEnd), maxOf(boundedStart, boundedEnd))
7855
7901
  }
7856
7902
 
7903
+ /**
7904
+ * What a hold offers, published where TalkBack can reach it.
7905
+ *
7906
+ * A long click is invisible to a screen reader: nothing announces that it
7907
+ * is there and nothing can perform it, so an action living only behind a
7908
+ * hold does not exist for anyone driving the screen by assistive
7909
+ * technology. A thread that holds a message for Copy and Report is the
7910
+ * case this was written for; without this the honest mitigation is a
7911
+ * permanently visible button under every message.
7912
+ *
7913
+ * Runs after applyAccessibility, which installs its own delegate and
7914
+ * would drop actions added before it.
7915
+ */
7916
+ private fun applyAccessibilityActions(
7917
+ screen: Screen,
7918
+ control: View,
7919
+ props: JSONObject,
7920
+ events: JSONObject,
7921
+ disabled: Boolean,
7922
+ ) {
7923
+ screen.accessibilityActionIds.remove(control)?.forEach { ViewCompat.removeAccessibilityAction(control, it) }
7924
+ val handler = craftNativeEventName(events, "onAccessibilityAction").takeUnless { disabled } ?: ""
7925
+ val authored = props.optJSONArray("accessibilityActions")
7926
+ if (handler.isBlank() || authored == null || authored.length() == 0) {
7927
+ screen.accessibilityActionHandlers.remove(control)
7928
+ return
7929
+ }
7930
+ screen.accessibilityActionHandlers[control] = handler
7931
+ val ids = mutableListOf<Int>()
7932
+ for (index in 0 until authored.length()) {
7933
+ val entry = authored.optJSONObject(index) ?: continue
7934
+ val name = craftNativeOptionalString(entry.opt("name"))?.takeIf { it.isNotBlank() } ?: continue
7935
+ val label = craftNativeOptionalString(entry.opt("label"))?.takeIf { it.isNotBlank() } ?: name
7936
+ ids += ViewCompat.addAccessibilityAction(control, label) { _, _ ->
7937
+ // Read the handler at firing time: a reconcile can rename it
7938
+ // while the action sits in TalkBack's menu.
7939
+ val current = screen.accessibilityActionHandlers[control]
7940
+ if (current.isNullOrBlank()) {
7941
+ false
7942
+ } else {
7943
+ send(screen, "EVENT", JSONObject().put("handlerName", current).put("nativeEvent", JSONObject().put("actionName", name)))
7944
+ true
7945
+ }
7946
+ }
7947
+ }
7948
+ if (ids.isEmpty()) screen.accessibilityActionHandlers.remove(control) else screen.accessibilityActionIds[control] = ids
7949
+ }
7950
+
7857
7951
  private fun applyAccessibility(view: View, props: JSONObject, type: String, fallbackLabel: String) {
7858
7952
  val testId = craftNativeOptionalString(props.opt("testID"))
7859
7953
  view.tag = testId?.takeIf { it.isNotBlank() } ?: fallbackLabel
@@ -8039,6 +8133,37 @@ class MainActivity : AppCompatActivity() {
8039
8133
  val method = payload.optString("method")
8040
8134
  val perform: ((CraftNativeCapabilityResult) -> Unit) -> Unit = if (module == "View") {
8041
8135
  { completion -> performView(screen, method, args, completion) }
8136
+ } else if (module == "Pedometer") {
8137
+ { completion ->
8138
+ val pedometer = screen.pedometer
8139
+ if (!pedometerEnabled) completion(CraftNativeCapabilityResult(code = "CAPABILITY_DISABLED", message = "Pedometer is disabled"))
8140
+ else if (pedometer == null) completion(CraftNativeCapabilityResult(code = "NEEDS_ACTIVITY", message = "Pedometer needs a visible app"))
8141
+ else if (method in listOf("start", "requestPermission") && !motionForeground) completion(CraftNativeCapabilityResult(code = "APP_INACTIVE", message = "Pedometer needs a foreground app"))
8142
+ else if (method == "requestPermission") capabilities.perform(token, payload.optInt("version", CRAFT_NATIVE_CAPABILITY_PROTOCOL_VERSION), module, method, args, completion)
8143
+ else completion(pedometer.perform(method, args))
8144
+ }
8145
+ } else if (module == "Barometer") {
8146
+ { completion ->
8147
+ val barometer = screen.barometer
8148
+ completion(if (!barometerEnabled) {
8149
+ CraftNativeCapabilityResult(code = "CAPABILITY_DISABLED", message = "Barometer is disabled")
8150
+ } else if (barometer == null) {
8151
+ CraftNativeCapabilityResult(code = "NEEDS_ACTIVITY", message = "Barometer needs a visible app")
8152
+ } else if (method == "start" && !motionForeground) {
8153
+ CraftNativeCapabilityResult(code = "APP_INACTIVE", message = "Barometer needs a foreground app")
8154
+ } else barometer.perform(method, args))
8155
+ }
8156
+ } else if (module == "MotionSensors") {
8157
+ { completion ->
8158
+ val motion = screen.motion
8159
+ completion(if (!motionSensorsEnabled) {
8160
+ CraftNativeCapabilityResult(code = "CAPABILITY_DISABLED", message = "Motion sensors are disabled")
8161
+ } else if (motion == null) {
8162
+ CraftNativeCapabilityResult(code = "NEEDS_ACTIVITY", message = "Motion sensors need a visible app")
8163
+ } else if (method == "start" && !motionForeground) {
8164
+ CraftNativeCapabilityResult(code = "APP_INACTIVE", message = "Motion sensors need a foreground app")
8165
+ } else motion.perform(method, args))
8166
+ }
8042
8167
  } else if (module == "Audio") {
8043
8168
  { completion ->
8044
8169
  val id = args.optString(0)
@@ -8290,6 +8415,8 @@ class MainActivity : AppCompatActivity() {
8290
8415
  }
8291
8416
 
8292
8417
  override fun onPause() {
8418
+ motionForeground = false
8419
+ allScreens().forEach { it.motion?.stopAll(); it.barometer?.stop("background"); it.pedometer?.stop("background") }
8293
8420
  disarmShakeGesture()
8294
8421
  if (::services.isInitialized) services.pauseVoice()
8295
8422
  nativeAppState = "inactive"
@@ -8350,13 +8477,14 @@ class MainActivity : AppCompatActivity() {
8350
8477
 
8351
8478
  override fun onResume() {
8352
8479
  super.onResume()
8480
+ motionForeground = true
8353
8481
  armShakeGesture()
8354
8482
  if (::services.isInitialized) services.resumed()
8355
8483
  }
8356
8484
 
8357
8485
  override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
8358
8486
  super.onRequestPermissionsResult(requestCode, permissions, grantResults)
8359
- if (::capabilities.isInitialized) { capabilities.onBluetoothPermissions(requestCode); capabilities.onNotificationPermissions(requestCode) }
8487
+ if (::capabilities.isInitialized) { capabilities.onBluetoothPermissions(requestCode); capabilities.onNotificationPermissions(requestCode); capabilities.onPedometerPermissions(requestCode) }
8360
8488
  if (::services.isInitialized) services.onRequestPermissionsResult(requestCode)
8361
8489
  }
8362
8490