@blotch/angular-lynx-native 22.3.0 → 22.3.2

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 (100) hide show
  1. package/README.md +41 -15
  2. package/dist/android/project.d.ts +90 -1
  3. package/dist/android/project.d.ts.map +1 -1
  4. package/dist/android/project.js +369 -5
  5. package/dist/eject.d.ts +15 -1
  6. package/dist/eject.d.ts.map +1 -1
  7. package/dist/eject.js +13 -2
  8. package/dist/features.d.ts +70 -0
  9. package/dist/features.d.ts.map +1 -0
  10. package/dist/features.js +115 -0
  11. package/dist/generate.d.ts +17 -1
  12. package/dist/generate.d.ts.map +1 -1
  13. package/dist/generate.js +12 -4
  14. package/dist/index.d.ts +1 -0
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +8 -1
  17. package/dist/ios/build.d.ts.map +1 -1
  18. package/dist/ios/build.js +8 -2
  19. package/dist/ios/frameworks.d.ts +6 -5
  20. package/dist/ios/frameworks.d.ts.map +1 -1
  21. package/dist/ios/frameworks.js +46 -15
  22. package/dist/ios/project.d.ts +173 -11
  23. package/dist/ios/project.d.ts.map +1 -1
  24. package/dist/ios/project.js +767 -33
  25. package/dist/options.d.ts +143 -2
  26. package/dist/options.d.ts.map +1 -1
  27. package/dist/options.js +127 -0
  28. package/dist/permissions.d.ts +18 -1
  29. package/dist/permissions.d.ts.map +1 -1
  30. package/dist/permissions.js +63 -3
  31. package/package.json +1 -1
  32. package/templates/android/README.md +37 -2
  33. package/templates/android/angular-lynx.properties +2 -0
  34. package/templates/android/app/build.gradle.kts +70 -1
  35. package/templates/android/app/src/main/AndroidManifest.xml +24 -3
  36. package/templates/android/app/src/main/java/dev/angularlynx/shell/BarcodeScannerModule.kt +144 -0
  37. package/templates/android/app/src/main/java/dev/angularlynx/shell/BiometricsModule.kt +433 -0
  38. package/templates/android/app/src/main/java/dev/angularlynx/shell/BitmapPresenter.kt +98 -0
  39. package/templates/android/app/src/main/java/dev/angularlynx/shell/BitmapRendererContext.kt +175 -0
  40. package/templates/android/app/src/main/java/dev/angularlynx/shell/BluetoothModule.kt +1288 -0
  41. package/templates/android/app/src/main/java/dev/angularlynx/shell/CalendarModule.kt +127 -0
  42. package/templates/android/app/src/main/java/dev/angularlynx/shell/Canvas2DContext.kt +1557 -0
  43. package/templates/android/app/src/main/java/dev/angularlynx/shell/Canvas2DOps.kt +63 -0
  44. package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasFilter.kt +357 -0
  45. package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasUI.kt +4 -3
  46. package/templates/android/app/src/main/java/dev/angularlynx/shell/FilePickerModule.kt +263 -0
  47. package/templates/android/app/src/main/java/dev/angularlynx/shell/HapticsModule.kt +82 -0
  48. package/templates/android/app/src/main/java/dev/angularlynx/shell/MainActivity.kt +376 -6
  49. package/templates/android/app/src/main/java/dev/angularlynx/shell/ModuleBytes.kt +58 -0
  50. package/templates/android/app/src/main/java/dev/angularlynx/shell/MotionModule.kt +332 -0
  51. package/templates/android/app/src/main/java/dev/angularlynx/shell/NfcModule.kt +274 -0
  52. package/templates/android/app/src/main/java/dev/angularlynx/shell/PermissionRequests.kt +25 -0
  53. package/templates/android/app/src/main/java/dev/angularlynx/shell/PhotoLibraryModule.kt +168 -0
  54. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShareTargetModule.kt +234 -0
  55. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellApplication.kt +6 -0
  56. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellModule.kt +47 -3
  57. package/templates/android/app/src/main/java/dev/angularlynx/shell/StoreReviewModule.kt +66 -0
  58. package/templates/android/app/src/main/java/dev/angularlynx/shell/SystemFontFamilies.kt +68 -0
  59. package/templates/android/app/src/main/java/dev/angularlynx/shell/TrackingModule.kt +74 -0
  60. package/templates/android/app/src/main/java/dev/angularlynx/shell/VideoFrameTarget.kt +429 -0
  61. package/templates/android/app/src/main/java/dev/angularlynx/shell/VideoUI.kt +676 -0
  62. package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLContext.kt +128 -13
  63. package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLModule.kt +493 -65
  64. package/templates/android/app/src/main/java/dev/angularlynx/shell/WifiModule.kt +429 -0
  65. package/templates/android/app/src/main/res/values/biometrics.xml +9 -0
  66. package/templates/android/app/src/main/res/values/share_target.xml +11 -0
  67. package/templates/android/app/src/main/res/xml/backup_rules.xml +5 -3
  68. package/templates/android/app/src/main/res/xml/data_extraction_rules.xml +6 -3
  69. package/templates/android/build.gradle.kts +1 -1
  70. package/templates/android/gradle/wrapper/gradle-wrapper.properties +2 -2
  71. package/templates/ios/App/AngularLynxBitmapRendererContext.m +188 -0
  72. package/templates/ios/App/AngularLynxCanvas2DContext.m +2467 -0
  73. package/templates/ios/App/AngularLynxCanvas2DOps.h +61 -0
  74. package/templates/ios/App/AngularLynxVideo.h +37 -0
  75. package/templates/ios/App/AngularLynxVideo.m +435 -0
  76. package/templates/ios/App/AngularLynxWebGL.h +69 -7
  77. package/templates/ios/App/AngularLynxWebGL.m +538 -29
  78. package/templates/ios/App/AngularLynxWebGLContext.m +148 -46
  79. package/templates/ios/App/AppDelegate.swift +34 -2
  80. package/templates/ios/App/AppleSignInButton.swift +106 -0
  81. package/templates/ios/App/AppleSignInModule.swift +222 -0
  82. package/templates/ios/App/BarcodeScannerModule.swift +284 -0
  83. package/templates/ios/App/BiometricsModule.swift +256 -0
  84. package/templates/ios/App/BluetoothModule.swift +1086 -0
  85. package/templates/ios/App/CalendarModule.swift +158 -0
  86. package/templates/ios/App/FilePickerModule.swift +240 -0
  87. package/templates/ios/App/HapticsModule.swift +90 -0
  88. package/templates/ios/App/Info.plist +2 -0
  89. package/templates/ios/App/LynxViewController.swift +210 -6
  90. package/templates/ios/App/MotionModule.swift +430 -0
  91. package/templates/ios/App/NfcModule.swift +346 -0
  92. package/templates/ios/App/PhotoLibraryModule.swift +169 -0
  93. package/templates/ios/App/ShareTargetModule.swift +150 -0
  94. package/templates/ios/App/ShellModule.swift +17 -0
  95. package/templates/ios/App/StoreReviewModule.swift +61 -0
  96. package/templates/ios/App/TrackingModule.swift +121 -0
  97. package/templates/ios/App/WifiModule.swift +237 -0
  98. package/templates/ios/App/XElementRegistry.swift +13 -3
  99. package/templates/ios/ShareExtension/Info.plist +42 -0
  100. package/templates/ios/ShareExtension/ShareViewController.swift +387 -0
@@ -0,0 +1,263 @@
1
+ package dev.angularlynx.shell
2
+
3
+ import android.app.Activity
4
+ import android.content.Context
5
+ import android.content.Intent
6
+ import android.net.Uri
7
+ import android.os.Handler
8
+ import android.os.Looper
9
+ import android.provider.OpenableColumns
10
+ import android.util.Base64
11
+ import android.webkit.MimeTypeMap
12
+ import com.lynx.jsbridge.LynxMethod
13
+ import com.lynx.jsbridge.LynxModule
14
+ import com.lynx.react.bridge.Callback
15
+ import com.lynx.react.bridge.JavaOnlyArray
16
+ import com.lynx.react.bridge.JavaOnlyMap
17
+ import com.lynx.react.bridge.ReadableMap
18
+ import com.lynx.react.bridge.ReadableType
19
+ import java.io.ByteArrayOutputStream
20
+ import java.io.IOException
21
+ import java.io.InputStream
22
+ import java.util.concurrent.Executors
23
+
24
+ /**
25
+ * The native module behind the runtime's `LynxFilePicker`, which calls it as
26
+ * `NativeModules.AngularLynxFilePicker` (from the background thread, where
27
+ * NativeModules lives).
28
+ *
29
+ * Lynx has no `<input type="file">` and no downloads. The activity opens the
30
+ * system's document picker (`ACTION_OPEN_DOCUMENT`) and its save screen
31
+ * (`ACTION_CREATE_DOCUMENT`) instead: Downloads, Drive and the other apps'
32
+ * document providers. Neither needs a permission, since the app reads or
33
+ * writes only the document the person chose.
34
+ *
35
+ * The bytes go back and forth as base64, since the module's callback and the
36
+ * runtime's relay to the main thread carry only what JSON can.
37
+ *
38
+ * The generator leaves this file, and the lines in MainActivity that launch
39
+ * the pickers and register it, out of an app whose angular.json doesn't set
40
+ * `filePicker`.
41
+ */
42
+ class FilePickerModule(context: Context, param: Any?) : LynxModule(context, param) {
43
+ private val host = param as Host
44
+ private val main = Handler(Looper.getMainLooper())
45
+
46
+ interface Host {
47
+ /**
48
+ * Starts [intent] for its result, and calls [done] once with the
49
+ * result code and data. Returns false, without starting it, while
50
+ * another is open.
51
+ */
52
+ fun startDocumentActivity(intent: Intent, done: (Int, Intent?) -> Unit): Boolean
53
+ }
54
+
55
+ /**
56
+ * Opens the document picker and calls back once with `{ files: [{ base64,
57
+ * name, type }] }`, `{ canceled: true }` when the person closes it, or
58
+ * `{ error: "too-large" | "failed", message }`, as the iOS module does.
59
+ */
60
+ @LynxMethod
61
+ fun pick(options: ReadableMap, callback: Callback) {
62
+ val mimeTypes = mimeTypes(options)
63
+ val multiple = options.hasKey("multiple") && options.getBoolean("multiple")
64
+ // JavaScript numbers can arrive as either, so it's read as a double.
65
+ val maxBytes = if (options.hasKey("maxBytes")) options.getDouble("maxBytes").toLong() else Long.MAX_VALUE
66
+ val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
67
+ // Only documents the app can open as a stream of bytes.
68
+ addCategory(Intent.CATEGORY_OPENABLE)
69
+ type = mimeTypes.singleOrNull() ?: "*/*"
70
+ if (mimeTypes.size > 1) putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes.toTypedArray())
71
+ putExtra(Intent.EXTRA_ALLOW_MULTIPLE, multiple)
72
+ }
73
+ start(intent, callback) { resultCode, data ->
74
+ val uris = if (resultCode == Activity.RESULT_OK) picked(data) else emptyList()
75
+ if (uris.isEmpty()) {
76
+ callback.invoke(JavaOnlyMap.from(mapOf("canceled" to true)))
77
+ } else {
78
+ // Reading a large document takes a moment: off the main thread.
79
+ worker.execute { callback.invoke(read(uris, maxBytes)) }
80
+ }
81
+ }
82
+ }
83
+
84
+ /**
85
+ * Lets the person choose where to save `{ base64, name, type }`, writes it
86
+ * there, and calls back once with `{ saved: true }`, `{ saved: false }`
87
+ * when they close the screen, or `{ error: "failed", message }`.
88
+ */
89
+ @LynxMethod
90
+ fun save(file: ReadableMap, callback: Callback) {
91
+ val base64 = file.optString("base64")
92
+ val bytes = try {
93
+ base64?.let { Base64.decode(it, Base64.DEFAULT) }
94
+ } catch (_: IllegalArgumentException) {
95
+ null
96
+ }
97
+ if (bytes == null) {
98
+ callback.invoke(failed("The file to save had no bytes."))
99
+ return
100
+ }
101
+ val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
102
+ addCategory(Intent.CATEGORY_OPENABLE)
103
+ type = file.optString("type") ?: "application/octet-stream"
104
+ putExtra(Intent.EXTRA_TITLE, file.optString("name") ?: "file")
105
+ }
106
+ start(intent, callback) { resultCode, data ->
107
+ val uri = data?.data
108
+ if (resultCode != Activity.RESULT_OK || uri == null) {
109
+ callback.invoke(JavaOnlyMap.from(mapOf("saved" to false)))
110
+ } else {
111
+ worker.execute { callback.invoke(write(uri, bytes)) }
112
+ }
113
+ }
114
+ }
115
+
116
+ /**
117
+ * Starts [intent] from the activity, on the main thread: Lynx calls
118
+ * modules off it.
119
+ */
120
+ private fun start(intent: Intent, callback: Callback, done: (Int, Intent?) -> Unit) {
121
+ main.post {
122
+ val opened = try {
123
+ host.startDocumentActivity(intent, done)
124
+ } catch (_: android.content.ActivityNotFoundException) {
125
+ callback.invoke(failed("This phone has no document picker."))
126
+ return@post
127
+ }
128
+ if (!opened) callback.invoke(failed("A picker is already open."))
129
+ }
130
+ }
131
+
132
+ /**
133
+ * The MIME types for `LynxFilePicker`'s `types`, which are given as an
134
+ * `<input type="file">`'s `accept`: MIME types, a family such as
135
+ * image's (`image/` and a star: written out, it would open a comment
136
+ * inside this one, since Kotlin nests them), or extensions such as
137
+ * `.docx`, which Android's picker
138
+ * doesn't take, so they're looked up. Any document when none is given or
139
+ * known.
140
+ */
141
+ private fun mimeTypes(options: ReadableMap): List<String> {
142
+ if (!options.hasKey("types") || options.getType("types") != ReadableType.Array) return emptyList()
143
+ val types = options.getArray("types")
144
+ return (0 until types.size()).mapNotNull { index ->
145
+ val type = types.getString(index)
146
+ if (type.startsWith(".")) {
147
+ MimeTypeMap.getSingleton().getMimeTypeFromExtension(type.substring(1).lowercase())
148
+ } else {
149
+ type
150
+ }
151
+ }.distinct()
152
+ }
153
+
154
+ /** The documents picked: one in the data, or several in its ClipData. */
155
+ private fun picked(data: Intent?): List<Uri> {
156
+ val clip = data?.clipData
157
+ if (clip != null) return (0 until clip.itemCount).mapNotNull { clip.getItemAt(it).uri }
158
+ return listOfNotNull(data?.data)
159
+ }
160
+
161
+ /**
162
+ * The callback's answer for the picked documents: their bytes, names and
163
+ * types, or too-large when they hold more than the runtime can carry,
164
+ * checked from the sizes their providers give before anything is read,
165
+ * and again as each is read.
166
+ */
167
+ private fun read(uris: List<Uri>, maxBytes: Long): JavaOnlyMap {
168
+ val resolver = mContext.contentResolver
169
+ val documents = uris.map { uri -> Triple(uri, displayName(uri) ?: "file", size(uri)) }
170
+ if (documents.sumOf { it.third ?: 0L } > maxBytes) return tooLarge()
171
+ val files = JavaOnlyArray()
172
+ var remaining = maxBytes
173
+ for ((uri, name, _) in documents) {
174
+ val bytes = try {
175
+ resolver.openInputStream(uri)?.use { readAtMost(it, remaining) }
176
+ } catch (_: IOException) {
177
+ null
178
+ } catch (_: SecurityException) {
179
+ null
180
+ } ?: return failed("$name couldn't be read.")
181
+ // A provider needn't say a document's size, so the check above
182
+ // can pass one of any size: the read stops past what's left of
183
+ // the limit instead of holding the whole document first.
184
+ if (bytes.size > remaining) return tooLarge()
185
+ remaining -= bytes.size
186
+ files.pushMap(
187
+ JavaOnlyMap.from(
188
+ mapOf(
189
+ "base64" to Base64.encodeToString(bytes, Base64.NO_WRAP),
190
+ "name" to name,
191
+ "type" to (resolver.getType(uri) ?: "application/octet-stream"),
192
+ ),
193
+ ),
194
+ )
195
+ }
196
+ return JavaOnlyMap.from(mapOf("files" to files))
197
+ }
198
+
199
+ /**
200
+ * The stream's bytes, up to [limit] and one more: a result longer than
201
+ * [limit] says the document is over it, without reading the rest.
202
+ */
203
+ private fun readAtMost(input: InputStream, limit: Long): ByteArray {
204
+ val output = ByteArrayOutputStream()
205
+ val buffer = ByteArray(64 * 1024)
206
+ while (output.size() <= limit) {
207
+ // Written so a limit of Long.MAX_VALUE can't overflow.
208
+ val left = limit - output.size()
209
+ val wanted = if (left >= buffer.size) buffer.size else (left + 1).toInt()
210
+ val read = input.read(buffer, 0, wanted)
211
+ if (read < 0) break
212
+ output.write(buffer, 0, read)
213
+ }
214
+ return output.toByteArray()
215
+ }
216
+
217
+ private fun tooLarge() =
218
+ JavaOnlyMap.from(mapOf("error" to "too-large", "message" to "The files picked are too large."))
219
+
220
+ private fun write(uri: Uri, bytes: ByteArray): JavaOnlyMap =
221
+ try {
222
+ // "wt" truncates a document the person chose to replace. A
223
+ // provider that can't open it gives no stream.
224
+ val stream = mContext.contentResolver.openOutputStream(uri, "wt")
225
+ if (stream == null) {
226
+ failed("The file couldn't be written.")
227
+ } else {
228
+ stream.use { it.write(bytes) }
229
+ JavaOnlyMap.from(mapOf("saved" to true))
230
+ }
231
+ } catch (error: IOException) {
232
+ failed(error.message ?: "The file couldn't be written.")
233
+ } catch (error: SecurityException) {
234
+ failed(error.message ?: "The file couldn't be written.")
235
+ }
236
+
237
+ /** The document's name, such as `receipt.pdf`. */
238
+ private fun displayName(uri: Uri): String? = column(uri, OpenableColumns.DISPLAY_NAME) { getString(it) }
239
+
240
+ /** The document's size, or null when its provider doesn't say. */
241
+ private fun size(uri: Uri): Long? = column(uri, OpenableColumns.SIZE) { if (isNull(it)) null else getLong(it) }
242
+
243
+ private fun <T> column(uri: Uri, name: String, value: android.database.Cursor.(Int) -> T?): T? =
244
+ try {
245
+ mContext.contentResolver.query(uri, arrayOf(name), null, null, null)
246
+ ?.use { cursor -> if (cursor.moveToFirst()) cursor.value(0) else null }
247
+ } catch (_: RuntimeException) {
248
+ null
249
+ }
250
+
251
+ private fun failed(message: String) =
252
+ JavaOnlyMap.from(mapOf("error" to "failed", "message" to message))
253
+
254
+ private fun ReadableMap.optString(key: String): String? =
255
+ if (hasKey(key) && getType(key) == ReadableType.String) getString(key) else null
256
+
257
+ companion object {
258
+ const val NAME = "AngularLynxFilePicker"
259
+
260
+ /** One thread for reading and writing, so two documents aren't held at once. */
261
+ private val worker = Executors.newSingleThreadExecutor()
262
+ }
263
+ }
@@ -0,0 +1,82 @@
1
+ package dev.angularlynx.shell
2
+
3
+ import android.app.Activity
4
+ import android.content.Context
5
+ import android.os.Build
6
+ import android.os.Handler
7
+ import android.os.Looper
8
+ import android.view.HapticFeedbackConstants
9
+ import com.lynx.jsbridge.LynxMethod
10
+ import com.lynx.jsbridge.LynxModule
11
+ import com.lynx.react.bridge.Callback
12
+ import com.lynx.react.bridge.JavaOnlyMap
13
+
14
+ /**
15
+ * The native module behind the runtime's `LynxHaptics`, which calls it as
16
+ * `NativeModules.AngularLynxHaptics` (from the background thread, where
17
+ * NativeModules lives). MainActivity registers it with itself as the param:
18
+ * the feedback plays on its window.
19
+ *
20
+ * Each method plays the nearest of Android's `HapticFeedbackConstants` with
21
+ * `View.performHapticFeedback()`, as the system's own controls do, and calls
22
+ * back once with `{}`. Unlike `Vibrator`, that needs no VIBRATE permission,
23
+ * and it follows the person's touch feedback setting: Android plays nothing
24
+ * when they've turned it off.
25
+ */
26
+ class HapticsModule(context: Context, param: Any?) : LynxModule(context, param) {
27
+ private val activity = param as Activity
28
+ private val main = Handler(Looper.getMainLooper())
29
+
30
+ // Lynx calls modules off the main thread, and a view's feedback belongs
31
+ // to the UI thread.
32
+ private fun play(feedback: Int, callback: Callback) {
33
+ main.post {
34
+ activity.window?.decorView?.performHapticFeedback(feedback)
35
+ callback.invoke(JavaOnlyMap())
36
+ }
37
+ }
38
+
39
+ /**
40
+ * The runtime's HapticImpactStyle. Android has three weights of tap, so
41
+ * `soft` plays as `light` and `rigid` as `medium`; a name the runtime
42
+ * doesn't send plays as `medium`, as its default does.
43
+ */
44
+ @LynxMethod
45
+ fun impact(style: String, callback: Callback) {
46
+ val feedback =
47
+ when (style) {
48
+ "light", "soft" -> HapticFeedbackConstants.KEYBOARD_TAP
49
+ "heavy" -> HapticFeedbackConstants.LONG_PRESS
50
+ else -> HapticFeedbackConstants.VIRTUAL_KEY
51
+ }
52
+ play(feedback, callback)
53
+ }
54
+
55
+ /** A selection moving, as a picker's detent or a clock's hand ticks. */
56
+ @LynxMethod
57
+ fun selection(callback: Callback) {
58
+ play(HapticFeedbackConstants.CLOCK_TICK, callback)
59
+ }
60
+
61
+ /**
62
+ * The runtime's HapticNotificationType. Android 11 added a confirm and a
63
+ * reject feedback; before it, success plays as a key's tap and the
64
+ * others as a long press.
65
+ */
66
+ @LynxMethod
67
+ fun notification(type: String, callback: Callback) {
68
+ val modern = Build.VERSION.SDK_INT >= Build.VERSION_CODES.R
69
+ val feedback =
70
+ when {
71
+ type == "warning" || type == "error" ->
72
+ if (modern) HapticFeedbackConstants.REJECT else HapticFeedbackConstants.LONG_PRESS
73
+ else ->
74
+ if (modern) HapticFeedbackConstants.CONFIRM else HapticFeedbackConstants.VIRTUAL_KEY
75
+ }
76
+ play(feedback, callback)
77
+ }
78
+
79
+ companion object {
80
+ const val NAME = "AngularLynxHaptics"
81
+ }
82
+ }