@blotch/angular-lynx-native 22.3.1 → 22.3.3
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.
- package/README.md +47 -15
- package/dist/android/project.d.ts +114 -1
- package/dist/android/project.d.ts.map +1 -1
- package/dist/android/project.js +440 -5
- package/dist/eject.d.ts +15 -1
- package/dist/eject.d.ts.map +1 -1
- package/dist/eject.js +14 -2
- package/dist/features.d.ts +70 -0
- package/dist/features.d.ts.map +1 -0
- package/dist/features.js +115 -0
- package/dist/files.d.ts +24 -0
- package/dist/files.d.ts.map +1 -0
- package/dist/files.js +41 -0
- package/dist/generate.d.ts +34 -3
- package/dist/generate.d.ts.map +1 -1
- package/dist/generate.js +78 -19
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -2
- package/dist/ios/build.d.ts.map +1 -1
- package/dist/ios/build.js +8 -2
- package/dist/ios/frameworks.d.ts +6 -5
- package/dist/ios/frameworks.d.ts.map +1 -1
- package/dist/ios/frameworks.js +46 -15
- package/dist/ios/project.d.ts +192 -11
- package/dist/ios/project.d.ts.map +1 -1
- package/dist/ios/project.js +823 -33
- package/dist/options.d.ts +208 -1
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +154 -0
- package/dist/permissions.d.ts +18 -1
- package/dist/permissions.d.ts.map +1 -1
- package/dist/permissions.js +79 -4
- package/dist/plugins.d.ts +245 -0
- package/dist/plugins.d.ts.map +1 -0
- package/dist/plugins.js +1244 -0
- package/package.json +1 -1
- package/templates/android/README.md +46 -5
- package/templates/android/angular-lynx.properties +2 -0
- package/templates/android/app/build.gradle.kts +78 -11
- package/templates/android/app/src/main/AndroidManifest.xml +19 -3
- package/templates/android/app/src/main/java/dev/angularlynx/shell/AngularLynxPlugins.kt +13 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/AudioRecorderModule.kt +520 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/BarcodeScannerModule.kt +144 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/BiometricsModule.kt +433 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/BitmapPresenter.kt +98 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/BitmapRendererContext.kt +175 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/BluetoothModule.kt +1288 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/CalendarModule.kt +127 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/Canvas2DContext.kt +1557 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/Canvas2DOps.kt +63 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasFilter.kt +357 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasUI.kt +4 -3
- package/templates/android/app/src/main/java/dev/angularlynx/shell/EdgeToEdgeOverlay.kt +24 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/EngineThreadHint.kt +156 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/FilePickerModule.kt +263 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/GeolocationModule.kt +557 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/HapticsModule.kt +82 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/HostElementUI.kt +331 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ImagePickerModule.kt +143 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ImagePipeline.kt +83 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/MainActivity.kt +523 -9
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ModuleBytes.kt +58 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/MotionModule.kt +332 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/NetworkStatus.kt +121 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/NfcModule.kt +274 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/PermissionRequests.kt +35 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/PhotoLibraryModule.kt +355 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ShareTargetModule.kt +234 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellApplication.kt +7 -8
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellModule.kt +58 -4
- package/templates/android/app/src/main/java/dev/angularlynx/shell/StoreReviewModule.kt +66 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/SystemFontFamilies.kt +68 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/TrackingModule.kt +74 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/VideoFrameTarget.kt +429 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/VideoUI.kt +676 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLContext.kt +128 -13
- package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLModule.kt +493 -65
- package/templates/android/app/src/main/java/dev/angularlynx/shell/WifiModule.kt +429 -0
- package/templates/android/app/src/main/res/values/biometrics.xml +9 -0
- package/templates/android/app/src/main/res/values/share_target.xml +11 -0
- package/templates/android/app/src/main/res/xml/backup_rules.xml +5 -3
- package/templates/android/app/src/main/res/xml/data_extraction_rules.xml +6 -3
- package/templates/android/app/src/main/res/xml/share_paths.xml +7 -2
- package/templates/android/build.gradle.kts +1 -1
- package/templates/android/gradle/wrapper/gradle-wrapper.properties +2 -2
- package/templates/ios/App/AngularLynxBitmapRendererContext.m +188 -0
- package/templates/ios/App/AngularLynxCanvas2DContext.m +2467 -0
- package/templates/ios/App/AngularLynxCanvas2DOps.h +61 -0
- package/templates/ios/App/AngularLynxPlugins.swift +22 -0
- package/templates/ios/App/AngularLynxVideo.h +37 -0
- package/templates/ios/App/AngularLynxVideo.m +435 -0
- package/templates/ios/App/AngularLynxWebGL.h +69 -7
- package/templates/ios/App/AngularLynxWebGL.m +538 -29
- package/templates/ios/App/AngularLynxWebGLContext.m +148 -46
- package/templates/ios/App/AnimationFrameRate.swift +66 -0
- package/templates/ios/App/AppDelegate.swift +37 -3
- package/templates/ios/App/AppleSignInButton.swift +106 -0
- package/templates/ios/App/AppleSignInModule.swift +222 -0
- package/templates/ios/App/AudioRecorderModule.swift +433 -0
- package/templates/ios/App/BarcodeScannerModule.swift +284 -0
- package/templates/ios/App/BiometricsModule.swift +256 -0
- package/templates/ios/App/BluetoothModule.swift +1086 -0
- package/templates/ios/App/CalendarModule.swift +158 -0
- package/templates/ios/App/FilePickerModule.swift +240 -0
- package/templates/ios/App/GeolocationModule.swift +455 -0
- package/templates/ios/App/HapticsModule.swift +90 -0
- package/templates/ios/App/HostElementUI.swift +270 -0
- package/templates/ios/App/ImagePickerModule.swift +160 -1
- package/templates/ios/App/Info.plist +2 -0
- package/templates/ios/App/LynxViewController.swift +274 -9
- package/templates/ios/App/MotionModule.swift +430 -0
- package/templates/ios/App/NetworkStatus.swift +41 -0
- package/templates/ios/App/NfcModule.swift +346 -0
- package/templates/ios/App/PhotoLibraryModule.swift +169 -0
- package/templates/ios/App/ShareTargetModule.swift +150 -0
- package/templates/ios/App/ShellModule.swift +17 -0
- package/templates/ios/App/StoreReviewModule.swift +61 -0
- package/templates/ios/App/TrackingModule.swift +121 -0
- package/templates/ios/App/WifiModule.swift +237 -0
- package/templates/ios/App/XElementRegistry.swift +13 -3
- package/templates/ios/ShareExtension/Info.plist +42 -0
- package/templates/ios/ShareExtension/ShareViewController.swift +387 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
package dev.angularlynx.shell
|
|
2
|
+
|
|
3
|
+
import com.lynx.react.bridge.JavaOnlyArray
|
|
4
|
+
import com.lynx.react.bridge.ReadableArray
|
|
5
|
+
import java.nio.ByteBuffer
|
|
6
|
+
import java.nio.ByteOrder
|
|
7
|
+
|
|
8
|
+
// How bytes cross a module call, both ways: the WebGL module's buffers and
|
|
9
|
+
// images, and the files AssetsModule reads. Here, not in WebGLModule.kt, as
|
|
10
|
+
// an app that never draws on a canvas is built without that file
|
|
11
|
+
// (@blotch/angular-lynx-native's features.ts) and still has AssetsModule.
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The bytes the runtime sends as 32-bit words, with the byte count last (see
|
|
15
|
+
* toWords in webgl-bridge.ts). Android runs little-endian, so the words land
|
|
16
|
+
* in memory as the bytes were.
|
|
17
|
+
*/
|
|
18
|
+
internal fun bytesFromWords(words: ReadableArray): ByteBuffer {
|
|
19
|
+
val count = words.size()
|
|
20
|
+
if (count == 0) return ByteBuffer.allocateDirect(0)
|
|
21
|
+
val byteLength = words.getDouble(count - 1).toLong().toInt()
|
|
22
|
+
val buffer = ByteBuffer.allocateDirect((count - 1) * 4).order(ByteOrder.LITTLE_ENDIAN)
|
|
23
|
+
for (i in 0 until count - 1) buffer.putInt(words.getDouble(i).toLong().toInt())
|
|
24
|
+
buffer.rewind()
|
|
25
|
+
buffer.limit(minOf(byteLength, (count - 1) * 4))
|
|
26
|
+
return buffer
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Bytes as words for the trip back, the byte count last. */
|
|
30
|
+
internal fun wordsFromBytes(bytes: ByteBuffer, length: Int): JavaOnlyArray {
|
|
31
|
+
val words = JavaOnlyArray()
|
|
32
|
+
val source = bytes.duplicate().order(ByteOrder.LITTLE_ENDIAN)
|
|
33
|
+
source.rewind()
|
|
34
|
+
val wordCount = (length + 3) / 4
|
|
35
|
+
for (i in 0 until wordCount) {
|
|
36
|
+
var word = 0L
|
|
37
|
+
for (b in 0 until 4) {
|
|
38
|
+
val index = i * 4 + b
|
|
39
|
+
if (index < length) word = word or ((source.get(index).toLong() and 0xff) shl (8 * b))
|
|
40
|
+
}
|
|
41
|
+
words.pushDouble(word.toDouble())
|
|
42
|
+
}
|
|
43
|
+
words.pushInt(length)
|
|
44
|
+
return words
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
internal fun flipRows(pixels: ByteBuffer, stride: Int, height: Int) {
|
|
48
|
+
val top = ByteArray(stride)
|
|
49
|
+
val bottom = ByteArray(stride)
|
|
50
|
+
for (row in 0 until height / 2) {
|
|
51
|
+
val other = height - 1 - row
|
|
52
|
+
pixels.position(row * stride); pixels.get(top)
|
|
53
|
+
pixels.position(other * stride); pixels.get(bottom)
|
|
54
|
+
pixels.position(row * stride); pixels.put(bottom)
|
|
55
|
+
pixels.position(other * stride); pixels.put(top)
|
|
56
|
+
}
|
|
57
|
+
pixels.rewind()
|
|
58
|
+
}
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
package dev.angularlynx.shell
|
|
2
|
+
|
|
3
|
+
import android.app.Activity
|
|
4
|
+
import android.app.Application
|
|
5
|
+
import android.content.Context
|
|
6
|
+
import android.hardware.Sensor
|
|
7
|
+
import android.hardware.SensorEvent
|
|
8
|
+
import android.hardware.SensorEventListener
|
|
9
|
+
import android.hardware.SensorManager
|
|
10
|
+
import android.os.Bundle
|
|
11
|
+
import android.os.Handler
|
|
12
|
+
import android.os.HandlerThread
|
|
13
|
+
import android.os.SystemClock
|
|
14
|
+
import com.lynx.jsbridge.LynxContextModule
|
|
15
|
+
import com.lynx.jsbridge.LynxMethod
|
|
16
|
+
import com.lynx.react.bridge.Callback
|
|
17
|
+
import com.lynx.react.bridge.JavaOnlyArray
|
|
18
|
+
import com.lynx.react.bridge.JavaOnlyMap
|
|
19
|
+
import com.lynx.tasm.behavior.LynxContext
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The native module behind the runtime's `LynxMotion`, which calls it as
|
|
23
|
+
* `NativeModules.AngularLynxMotion` (from the background thread, where
|
|
24
|
+
* NativeModules lives). MainActivity registers it with itself as the param,
|
|
25
|
+
* so the sensors stop while the activity is paused.
|
|
26
|
+
*
|
|
27
|
+
* `start(sensor, interval)` runs a sensor, sampling every `interval`
|
|
28
|
+
* milliseconds, or changes how often; `stop(sensor)` stops it; and
|
|
29
|
+
* `availability` calls back with which sensors the phone has. The sensors
|
|
30
|
+
* are `acceleration` (`TYPE_ACCELEROMETER`, in m/s² with gravity), `rotation`
|
|
31
|
+
* (`TYPE_GYROSCOPE`, in rad/s), `orientation` (`TYPE_GAME_ROTATION_VECTOR`,
|
|
32
|
+
* which leaves the magnetometer out, as a rotation matrix), `heading`
|
|
33
|
+
* (`TYPE_ROTATION_VECTOR`'s azimuth, which uses it) and `shake`, which this
|
|
34
|
+
* module hears from the accelerometer itself, so a shake costs the runtime
|
|
35
|
+
* one event rather than 50 samples a second. None needs a permission: the
|
|
36
|
+
* runtime asks for no more than 200 Hz, past which Android 12 wants
|
|
37
|
+
* HIGH_SAMPLING_RATE_SENSORS.
|
|
38
|
+
*
|
|
39
|
+
* The samples go to the runtime in the `angularLynxMotion` global event, one
|
|
40
|
+
* map with a flat array of numbers for each sensor that sampled (see
|
|
41
|
+
* `MotionBatch` in the runtime's motion.ts), at most once every 16 ms: each
|
|
42
|
+
* event is work for Lynx's engine thread, where Angular runs.
|
|
43
|
+
*
|
|
44
|
+
* The generator leaves this file, and the lines in MainActivity that
|
|
45
|
+
* register it, out of an app whose angular.json doesn't set `motion`.
|
|
46
|
+
*/
|
|
47
|
+
class MotionModule : LynxContextModule {
|
|
48
|
+
// Lynx creates a module through whichever of its constructors it finds.
|
|
49
|
+
constructor(context: LynxContext) : super(context) {
|
|
50
|
+
activity = null
|
|
51
|
+
watchLifecycle()
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
constructor(context: LynxContext, param: Any?) : super(context, param) {
|
|
55
|
+
activity = param as? Activity
|
|
56
|
+
watchLifecycle()
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private val activity: Activity?
|
|
60
|
+
|
|
61
|
+
/** Everything below belongs to this thread, where the sensors call back. */
|
|
62
|
+
private val thread = HandlerThread("AngularLynxMotion").apply { start() }
|
|
63
|
+
private val handler = Handler(thread.looper)
|
|
64
|
+
private val sensors = mLynxContext.getSystemService(Context.SENSOR_SERVICE) as SensorManager
|
|
65
|
+
|
|
66
|
+
/** The interval each running sensor was started at, in microseconds. */
|
|
67
|
+
private val requested = HashMap<String, Int>()
|
|
68
|
+
/** The rate each piece of hardware was registered at, to change it only on a change. */
|
|
69
|
+
private val registered = HashMap<Int, Int>()
|
|
70
|
+
/** When each sensor's last sample was kept, in milliseconds. */
|
|
71
|
+
private val lastSample = HashMap<String, Double>()
|
|
72
|
+
private val batch = HashMap<String, MutableList<Double>>()
|
|
73
|
+
private var flushScheduled = false
|
|
74
|
+
private var lastFlush = 0L
|
|
75
|
+
private var paused = false
|
|
76
|
+
private var shakes = ShakeDetector()
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The epoch time of the phone's start, read once: each sample's time is
|
|
80
|
+
* its elapsed realtime from there, so the times only go forward, and
|
|
81
|
+
* spacing and shakes go by them, even when the system's clock is changed.
|
|
82
|
+
* They stay milliseconds since the epoch, as the runtime's `Date.now()`.
|
|
83
|
+
*/
|
|
84
|
+
private val epochAtBootMs = System.currentTimeMillis() - SystemClock.elapsedRealtimeNanos() / 1_000_000.0
|
|
85
|
+
private val rotation = FloatArray(9)
|
|
86
|
+
private val angles = FloatArray(3)
|
|
87
|
+
|
|
88
|
+
private val listener =
|
|
89
|
+
object : SensorEventListener {
|
|
90
|
+
override fun onSensorChanged(event: SensorEvent) = sample(event)
|
|
91
|
+
|
|
92
|
+
override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) = Unit
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
private val lifecycle =
|
|
96
|
+
object : Application.ActivityLifecycleCallbacks {
|
|
97
|
+
override fun onActivityResumed(resumed: Activity) {
|
|
98
|
+
if (resumed === activity) handler.post { paused = false; apply() }
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
override fun onActivityPaused(paused: Activity) {
|
|
102
|
+
if (paused === activity) {
|
|
103
|
+
handler.post {
|
|
104
|
+
this@MotionModule.paused = true
|
|
105
|
+
// Samples from before the pause aren't sent after
|
|
106
|
+
// it, and aren't half of a shake with one after it
|
|
107
|
+
// either.
|
|
108
|
+
batch.clear()
|
|
109
|
+
shakes = ShakeDetector()
|
|
110
|
+
apply()
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
override fun onActivityCreated(created: Activity, state: Bundle?) = Unit
|
|
116
|
+
|
|
117
|
+
override fun onActivityStarted(started: Activity) = Unit
|
|
118
|
+
|
|
119
|
+
override fun onActivityStopped(stopped: Activity) = Unit
|
|
120
|
+
|
|
121
|
+
override fun onActivitySaveInstanceState(saved: Activity, state: Bundle) = Unit
|
|
122
|
+
|
|
123
|
+
override fun onActivityDestroyed(destroyed: Activity) = Unit
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Stops the sensors while the activity is paused, where Lynx pauses the
|
|
128
|
+
* runtime and nobody sees what they'd measure, and the battery would pay
|
|
129
|
+
* for them.
|
|
130
|
+
*/
|
|
131
|
+
private fun watchLifecycle() {
|
|
132
|
+
(mLynxContext.applicationContext as? Application)?.registerActivityLifecycleCallbacks(lifecycle)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
@LynxMethod
|
|
136
|
+
fun start(sensor: String, interval: Double) {
|
|
137
|
+
handler.post {
|
|
138
|
+
requested[sensor] = (maxOf(interval, 5.0) * 1000).toInt()
|
|
139
|
+
apply()
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
@LynxMethod
|
|
144
|
+
fun stop(sensor: String) {
|
|
145
|
+
handler.post {
|
|
146
|
+
requested.remove(sensor)
|
|
147
|
+
lastSample.remove(sensor)
|
|
148
|
+
// Samples still waiting for the next event were for the watches
|
|
149
|
+
// that just stopped: a shake heard before a new listener came
|
|
150
|
+
// mustn't reach it.
|
|
151
|
+
batch.remove(sensor)
|
|
152
|
+
// A shake begun for the last listener can't be finished for the next.
|
|
153
|
+
if (sensor == "shake") shakes = ShakeDetector()
|
|
154
|
+
apply()
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Calls back with which sensors the phone has. */
|
|
159
|
+
@LynxMethod
|
|
160
|
+
fun availability(callback: Callback) {
|
|
161
|
+
callback.invoke(
|
|
162
|
+
JavaOnlyMap().apply {
|
|
163
|
+
putBoolean("acceleration", sensors.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null)
|
|
164
|
+
putBoolean("rotation", sensors.getDefaultSensor(Sensor.TYPE_GYROSCOPE) != null)
|
|
165
|
+
putBoolean("orientation", sensors.getDefaultSensor(Sensor.TYPE_GAME_ROTATION_VECTOR) != null)
|
|
166
|
+
putBoolean("heading", sensors.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR) != null)
|
|
167
|
+
},
|
|
168
|
+
)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
override fun destroy() {
|
|
172
|
+
(mLynxContext.applicationContext as? Application)?.unregisterActivityLifecycleCallbacks(lifecycle)
|
|
173
|
+
handler.post {
|
|
174
|
+
requested.clear()
|
|
175
|
+
apply()
|
|
176
|
+
thread.quitSafely()
|
|
177
|
+
}
|
|
178
|
+
super.destroy()
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Registers each sensor at the shortest interval anything asks of it, or not at all. */
|
|
182
|
+
private fun apply() {
|
|
183
|
+
val wanted = if (paused) emptyMap() else requested
|
|
184
|
+
// The accelerometer serves the acceleration and the shakes.
|
|
185
|
+
val accelerometer =
|
|
186
|
+
listOfNotNull(wanted["acceleration"], wanted["shake"]?.let { SHAKE_INTERVAL_US }).minOrNull()
|
|
187
|
+
set(Sensor.TYPE_ACCELEROMETER, accelerometer)
|
|
188
|
+
set(Sensor.TYPE_GYROSCOPE, wanted["rotation"])
|
|
189
|
+
set(Sensor.TYPE_GAME_ROTATION_VECTOR, wanted["orientation"])
|
|
190
|
+
set(Sensor.TYPE_ROTATION_VECTOR, wanted["heading"])
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
private fun set(type: Int, interval: Int?) {
|
|
194
|
+
if (registered[type] == interval) return
|
|
195
|
+
val sensor = sensors.getDefaultSensor(type) ?: return
|
|
196
|
+
val previous = registered.remove(type)
|
|
197
|
+
if (previous != null) sensors.unregisterListener(listener, sensor)
|
|
198
|
+
if (interval == null) return
|
|
199
|
+
// The interval is a hint: Android may sample faster, which keep()
|
|
200
|
+
// throttles. Recorded only when Android took it, so a later start()
|
|
201
|
+
// tries again rather than finding it already registered. If Android
|
|
202
|
+
// refuses the new rate, the watches that were running keep the old
|
|
203
|
+
// one rather than getting nothing.
|
|
204
|
+
if (sensors.registerListener(listener, sensor, interval, handler)) {
|
|
205
|
+
registered[type] = interval
|
|
206
|
+
} else if (previous != null && sensors.registerListener(listener, sensor, previous, handler)) {
|
|
207
|
+
registered[type] = previous
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
private fun sample(event: SensorEvent) {
|
|
212
|
+
val t = milliseconds(event.timestamp)
|
|
213
|
+
val v = event.values
|
|
214
|
+
when (event.sensor.type) {
|
|
215
|
+
Sensor.TYPE_ACCELEROMETER -> {
|
|
216
|
+
val (x, y, z) = Triple(v[0].toDouble(), v[1].toDouble(), v[2].toDouble())
|
|
217
|
+
if (keep("acceleration", t)) add("acceleration", t, x, y, z)
|
|
218
|
+
if (requested.containsKey("shake") && shakes.add(t, x, y, z)) add("shake", t)
|
|
219
|
+
}
|
|
220
|
+
Sensor.TYPE_GYROSCOPE ->
|
|
221
|
+
if (keep("rotation", t)) add("rotation", t, v[0].toDouble(), v[1].toDouble(), v[2].toDouble())
|
|
222
|
+
Sensor.TYPE_GAME_ROTATION_VECTOR ->
|
|
223
|
+
if (keep("orientation", t)) {
|
|
224
|
+
// Turns a vector in the phone's frame into the earth's,
|
|
225
|
+
// row by row: the matrix the runtime reads.
|
|
226
|
+
SensorManager.getRotationMatrixFromVector(rotation, v)
|
|
227
|
+
add("orientation", t, *rotation.map { it.toDouble() }.toDoubleArray())
|
|
228
|
+
}
|
|
229
|
+
Sensor.TYPE_ROTATION_VECTOR ->
|
|
230
|
+
if (keep("heading", t)) {
|
|
231
|
+
SensorManager.getRotationMatrixFromVector(rotation, v)
|
|
232
|
+
SensorManager.getOrientation(rotation, angles)
|
|
233
|
+
// The azimuth: where the top of the phone points, in
|
|
234
|
+
// radians clockwise from magnetic north (π/2 when it
|
|
235
|
+
// points east), from -π to π, as degrees from 0 to 360.
|
|
236
|
+
val heading = (Math.toDegrees(angles[0].toDouble()) + 360) % 360
|
|
237
|
+
// The estimated accuracy, in radians, where the sensor
|
|
238
|
+
// gives one, and -1 where it doesn't. Android has no true
|
|
239
|
+
// north without the phone's location, so that's -1.
|
|
240
|
+
val accuracy = if (v.size > 4 && v[4] >= 0) Math.toDegrees(v[4].toDouble()) else -1.0
|
|
241
|
+
add("heading", t, heading, -1.0, accuracy)
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Whether a sensor's sample at `t` is due, by the interval it was started
|
|
248
|
+
* at, allowing a little early for the sensor's jitter. Marks it kept.
|
|
249
|
+
*/
|
|
250
|
+
private fun keep(sensor: String, t: Double): Boolean {
|
|
251
|
+
val interval = requested[sensor] ?: return false
|
|
252
|
+
val last = lastSample[sensor]
|
|
253
|
+
// The runtime's own rule (`due()` in motion.ts): a sample up to a
|
|
254
|
+
// quarter of the interval early, 4 ms at most, counts. A wider one
|
|
255
|
+
// here would pass samples the runtime then skips, halving a watch's
|
|
256
|
+
// rate.
|
|
257
|
+
val ms = interval / 1000.0
|
|
258
|
+
if (last != null && t - last < ms - minOf(4.0, ms / 4)) return false
|
|
259
|
+
lastSample[sensor] = t
|
|
260
|
+
return true
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** Adds a sample to the next event, which goes at most every 16 ms. */
|
|
264
|
+
private fun add(sensor: String, vararg values: Double) {
|
|
265
|
+
batch.getOrPut(sensor) { ArrayList() }.addAll(values.toList())
|
|
266
|
+
if (flushScheduled) return
|
|
267
|
+
flushScheduled = true
|
|
268
|
+
val wait = maxOf(0L, lastFlush + FLUSH_INTERVAL_MS - SystemClock.uptimeMillis())
|
|
269
|
+
handler.postDelayed({ flush() }, wait)
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
private fun flush() {
|
|
273
|
+
flushScheduled = false
|
|
274
|
+
lastFlush = SystemClock.uptimeMillis()
|
|
275
|
+
if (batch.isEmpty()) return
|
|
276
|
+
val map = JavaOnlyMap()
|
|
277
|
+
for ((sensor, values) in batch) {
|
|
278
|
+
map.putArray(sensor, JavaOnlyArray().apply { values.forEach { pushDouble(it) } })
|
|
279
|
+
}
|
|
280
|
+
batch.clear()
|
|
281
|
+
mLynxContext.sendGlobalEvent(EVENT, JavaOnlyArray().apply { pushMap(map) })
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* A sensor event's time (nanoseconds of elapsed realtime) in milliseconds
|
|
286
|
+
* since the epoch, as the runtime's `Date.now()` counts, from the epoch
|
|
287
|
+
* time of the phone's start read once (`epochAtBootMs`).
|
|
288
|
+
*/
|
|
289
|
+
private fun milliseconds(timestamp: Long): Double = epochAtBootMs + timestamp / 1_000_000.0
|
|
290
|
+
|
|
291
|
+
companion object {
|
|
292
|
+
const val NAME = "AngularLynxMotion"
|
|
293
|
+
const val EVENT = "angularLynxMotion"
|
|
294
|
+
|
|
295
|
+
/** The least time between events, about a frame. */
|
|
296
|
+
private const val FLUSH_INTERVAL_MS = 16L
|
|
297
|
+
|
|
298
|
+
/** How often the accelerometer samples for shakes: the runtime's `SHAKE.intervalMs`. */
|
|
299
|
+
private const val SHAKE_INTERVAL_US = 20_000
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Hears a shake in accelerometer samples: the runtime's `ShakeDetector`
|
|
305
|
+
* (`motion/motion-math.ts`), after Square's Seismic. A sample accelerates
|
|
306
|
+
* past 13 m/s², and the phone is shaking when three quarters of the samples
|
|
307
|
+
* of the last half second do, over a quarter of a second or more.
|
|
308
|
+
*/
|
|
309
|
+
private class ShakeDetector {
|
|
310
|
+
private val times = ArrayDeque<Double>()
|
|
311
|
+
private val fast = ArrayDeque<Boolean>()
|
|
312
|
+
private var accelerating = 0
|
|
313
|
+
private var quietUntil = Double.NEGATIVE_INFINITY
|
|
314
|
+
|
|
315
|
+
fun add(t: Double, x: Double, y: Double, z: Double): Boolean {
|
|
316
|
+
val accelerates = x * x + y * y + z * z > 13.0 * 13.0
|
|
317
|
+
times.addLast(t)
|
|
318
|
+
fast.addLast(accelerates)
|
|
319
|
+
if (accelerates) accelerating++
|
|
320
|
+
while (times.isNotEmpty() && times.first() < t - 500) {
|
|
321
|
+
times.removeFirst()
|
|
322
|
+
if (fast.removeFirst()) accelerating--
|
|
323
|
+
}
|
|
324
|
+
if (t < quietUntil) return false
|
|
325
|
+
if (times.size < 4 || t - times.first() < 250 || accelerating * 4 < times.size * 3) return false
|
|
326
|
+
times.clear()
|
|
327
|
+
fast.clear()
|
|
328
|
+
accelerating = 0
|
|
329
|
+
quietUntil = t + 750
|
|
330
|
+
return true
|
|
331
|
+
}
|
|
332
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
package dev.angularlynx.shell
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.net.ConnectivityManager
|
|
5
|
+
import android.net.Network
|
|
6
|
+
import android.net.NetworkCapabilities
|
|
7
|
+
import android.os.Build
|
|
8
|
+
import android.os.Handler
|
|
9
|
+
import android.os.Looper
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Whether the phone has a connection to the internet, for LynxNetwork, which
|
|
13
|
+
* MainActivity sends in the `online` global prop. Lynx tells the app's code
|
|
14
|
+
* nothing about the connection, and has no `navigator.onLine`, so the shell
|
|
15
|
+
* watches it itself.
|
|
16
|
+
*
|
|
17
|
+
* Online while the default network can reach the internet and Android has
|
|
18
|
+
* checked that it does (NET_CAPABILITY_VALIDATED): a Wi-Fi network behind a
|
|
19
|
+
* captive portal, or one with no way out, counts as offline, as the status
|
|
20
|
+
* bar's "!" shows. It needs ACCESS_NETWORK_STATE, a permission Android grants
|
|
21
|
+
* at install without asking.
|
|
22
|
+
*
|
|
23
|
+
* [onChange] is called on the main thread each time [online] changes, and
|
|
24
|
+
* only then: a new network with the same answer (Wi-Fi to cellular) would
|
|
25
|
+
* only make the app run its change detection for nothing. [close] stops it.
|
|
26
|
+
*/
|
|
27
|
+
class NetworkStatus(context: Context, private val onChange: (Boolean) -> Unit) : AutoCloseable {
|
|
28
|
+
private val connectivity = context.getSystemService(ConnectivityManager::class.java)
|
|
29
|
+
|
|
30
|
+
// The callbacks come on a ConnectivityManager thread, and the LynxView
|
|
31
|
+
// takes its updates on the main one. The overload that takes a Handler
|
|
32
|
+
// needs Android 8, above the app's minSdk, so each callback posts.
|
|
33
|
+
private val main = Handler(Looper.getMainLooper())
|
|
34
|
+
|
|
35
|
+
// The default network the callbacks last named, on the main thread. A
|
|
36
|
+
// switch to another one can report the old one lost after the new one is
|
|
37
|
+
// available, which mustn't take the app offline.
|
|
38
|
+
private var current: Network? = null
|
|
39
|
+
|
|
40
|
+
// Set by close(), on the main thread. A callback already running on the
|
|
41
|
+
// ConnectivityManager thread as the callback is unregistered can still
|
|
42
|
+
// post after close() cleared the queue, and its update mustn't reach a
|
|
43
|
+
// LynxView that has gone.
|
|
44
|
+
private var closed = false
|
|
45
|
+
|
|
46
|
+
private val callback = object : ConnectivityManager.NetworkCallback() {
|
|
47
|
+
override fun onAvailable(network: Network) {
|
|
48
|
+
// Android 8 and later always follow onAvailable with
|
|
49
|
+
// onCapabilitiesChanged, which answers for the network. Android 7
|
|
50
|
+
// may not, so a network that is already validated as it becomes
|
|
51
|
+
// the default would otherwise never take the app online. Its
|
|
52
|
+
// capabilities are read once the main thread runs the post, not in
|
|
53
|
+
// the callback, where a ConnectivityManager call can race the
|
|
54
|
+
// update it is reporting.
|
|
55
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) return
|
|
56
|
+
main.post {
|
|
57
|
+
current = network
|
|
58
|
+
update(isOnline(connectivity.getNetworkCapabilities(network)))
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
override fun onCapabilitiesChanged(network: Network, capabilities: NetworkCapabilities) {
|
|
63
|
+
val online = isOnline(capabilities)
|
|
64
|
+
main.post {
|
|
65
|
+
current = network
|
|
66
|
+
update(online)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
override fun onLost(network: Network) {
|
|
71
|
+
main.post {
|
|
72
|
+
if (current == null || current == network) {
|
|
73
|
+
current = null
|
|
74
|
+
update(false)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
init {
|
|
81
|
+
// Registered before the first read, so no change can fall between the
|
|
82
|
+
// two: Android reports the default network to a new callback, but says
|
|
83
|
+
// nothing when there is none, so a network lost after the read and
|
|
84
|
+
// before the registration would leave the app online until the next
|
|
85
|
+
// change. A loss after the registration reaches the callback, whose
|
|
86
|
+
// posts run after this.
|
|
87
|
+
connectivity.registerDefaultNetworkCallback(callback)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Whether the phone is online now. Read synchronously from the active
|
|
92
|
+
* network as this starts, so the first global props, which go out from
|
|
93
|
+
* onCreate before any callback, are already right.
|
|
94
|
+
*/
|
|
95
|
+
var online: Boolean = isOnline(connectivity.getNetworkCapabilities(connectivity.activeNetwork))
|
|
96
|
+
private set
|
|
97
|
+
|
|
98
|
+
private fun update(online: Boolean) {
|
|
99
|
+
if (closed || this.online == online) return
|
|
100
|
+
this.online = online
|
|
101
|
+
onChange(online)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Stops watching, as the activity goes. Android caps how many callbacks
|
|
106
|
+
* an app can hold, and keeps each one, and what it reaches, until it's
|
|
107
|
+
* unregistered.
|
|
108
|
+
*/
|
|
109
|
+
override fun close() {
|
|
110
|
+
closed = true
|
|
111
|
+
connectivity.unregisterNetworkCallback(callback)
|
|
112
|
+
main.removeCallbacksAndMessages(null)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
private companion object {
|
|
116
|
+
fun isOnline(capabilities: NetworkCapabilities?): Boolean =
|
|
117
|
+
capabilities != null &&
|
|
118
|
+
capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) &&
|
|
119
|
+
capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
|
|
120
|
+
}
|
|
121
|
+
}
|