@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.
- package/README.md +41 -15
- package/dist/android/project.d.ts +90 -1
- package/dist/android/project.d.ts.map +1 -1
- package/dist/android/project.js +369 -5
- package/dist/eject.d.ts +15 -1
- package/dist/eject.d.ts.map +1 -1
- package/dist/eject.js +13 -2
- package/dist/features.d.ts +70 -0
- package/dist/features.d.ts.map +1 -0
- package/dist/features.js +115 -0
- package/dist/generate.d.ts +17 -1
- package/dist/generate.d.ts.map +1 -1
- package/dist/generate.js +12 -4
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -1
- 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 +173 -11
- package/dist/ios/project.d.ts.map +1 -1
- package/dist/ios/project.js +767 -33
- package/dist/options.d.ts +143 -2
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +127 -0
- package/dist/permissions.d.ts +18 -1
- package/dist/permissions.d.ts.map +1 -1
- package/dist/permissions.js +63 -3
- package/package.json +1 -1
- package/templates/android/README.md +37 -2
- package/templates/android/angular-lynx.properties +2 -0
- package/templates/android/app/build.gradle.kts +70 -1
- package/templates/android/app/src/main/AndroidManifest.xml +24 -3
- 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/FilePickerModule.kt +263 -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/MainActivity.kt +376 -6
- 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/NfcModule.kt +274 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/PermissionRequests.kt +25 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/PhotoLibraryModule.kt +168 -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 +6 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellModule.kt +47 -3
- 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/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/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/AppDelegate.swift +34 -2
- package/templates/ios/App/AppleSignInButton.swift +106 -0
- package/templates/ios/App/AppleSignInModule.swift +222 -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/HapticsModule.swift +90 -0
- package/templates/ios/App/Info.plist +2 -0
- package/templates/ios/App/LynxViewController.swift +210 -6
- package/templates/ios/App/MotionModule.swift +430 -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
|
@@ -60,6 +60,11 @@ android {
|
|
|
60
60
|
// its insets listener). A properties file written before the option
|
|
61
61
|
// existed has no line for it, and gets the default.
|
|
62
62
|
resValue("bool", "edge_to_edge", appConfig.getProperty("edgeToEdge") ?: "true")
|
|
63
|
+
// Whether MainActivity sets the LynxView's font scale from the
|
|
64
|
+
// system's font size, and the largest it takes (0 for no cap). Read
|
|
65
|
+
// with the same default for a properties file written before them.
|
|
66
|
+
resValue("bool", "text_scaling", appConfig.getProperty("textScaling") ?: "true")
|
|
67
|
+
resValue("string", "max_text_scale", appConfig.getProperty("maxTextScale") ?: "0")
|
|
63
68
|
}
|
|
64
69
|
|
|
65
70
|
signingConfigs {
|
|
@@ -95,6 +100,20 @@ android {
|
|
|
95
100
|
} else {
|
|
96
101
|
signingConfigs.getByName("debug")
|
|
97
102
|
}
|
|
103
|
+
// A build signed with the upload key is for Google Play, which
|
|
104
|
+
// rejects an app whose 64-bit native libraries can't load on a
|
|
105
|
+
// device with 16 KB memory pages. Fresco's x86_64 libraries, at
|
|
106
|
+
// the version Lynx's image service is written against, are
|
|
107
|
+
// aligned for 4 KB pages only (its arm64 ones are fine), so the
|
|
108
|
+
// Play build ships the ARM ABIs alone. That leaves out only x86
|
|
109
|
+
// devices (emulators and a few Chromebooks); 32-bit x86 goes
|
|
110
|
+
// too, as no phone needs it. Builds signed with the debug key
|
|
111
|
+
// keep every ABI, so their APK still runs on an x86_64 emulator.
|
|
112
|
+
if (hasReleaseSigning) {
|
|
113
|
+
ndk {
|
|
114
|
+
abiFilters += listOf("arm64-v8a", "armeabi-v7a")
|
|
115
|
+
}
|
|
116
|
+
}
|
|
98
117
|
}
|
|
99
118
|
}
|
|
100
119
|
|
|
@@ -143,6 +162,33 @@ dependencies {
|
|
|
143
162
|
// from every format the Photo Picker hands back, the same way on every
|
|
144
163
|
// Android version.
|
|
145
164
|
implementation("androidx.exifinterface:exifinterface:1.4.2")
|
|
165
|
+
// BiometricPrompt, which BiometricsModule shows: the same prompt from
|
|
166
|
+
// Android 7 up, with the fingerprint dialog on versions before Android 9's
|
|
167
|
+
// own. Only an app with permissions.biometrics keeps it, with the module.
|
|
168
|
+
// angular-lynx:biometrics
|
|
169
|
+
implementation("androidx.biometric:biometric:1.1.0")
|
|
170
|
+
// /angular-lynx:biometrics
|
|
171
|
+
// Google's code scanner, which BarcodeScannerModule opens: it runs in
|
|
172
|
+
// Google Play services, which shows the camera, so the app needs no
|
|
173
|
+
// CAMERA permission and ships no ML Kit model. Only an app whose
|
|
174
|
+
// angular.json sets permissions.camera keeps these lines and the module
|
|
175
|
+
// (@blotch/angular-lynx-native's android/project.ts).
|
|
176
|
+
// angular-lynx:barcode-scanner
|
|
177
|
+
implementation("com.google.android.gms:play-services-code-scanner:16.1.0")
|
|
178
|
+
// /angular-lynx:barcode-scanner
|
|
179
|
+
// Google Play's In-App Review API, which StoreReviewModule asks for the
|
|
180
|
+
// rating prompt. Only an app whose angular.json sets storeReview keeps
|
|
181
|
+
// these lines (@blotch/angular-lynx-native's android/project.ts).
|
|
182
|
+
// angular-lynx:store-review
|
|
183
|
+
implementation("com.google.android.play:review:2.0.2")
|
|
184
|
+
// /angular-lynx:store-review
|
|
185
|
+
// Google Play's advertising ID library, which TrackingModule asks whether
|
|
186
|
+
// the person has opted out of personalized ads or deleted their ID. It
|
|
187
|
+
// also declares AD_ID, which the manifest does too. Only an app whose
|
|
188
|
+
// angular.json sets permissions.tracking keeps these lines.
|
|
189
|
+
// angular-lynx:tracking
|
|
190
|
+
implementation("com.google.android.gms:play-services-ads-identifier:18.3.0")
|
|
191
|
+
// /angular-lynx:tracking
|
|
146
192
|
|
|
147
193
|
// The Lynx engine, its JavaScript runtime (PrimJS) and its JS SDK.
|
|
148
194
|
implementation("org.lynxsdk.lynx:lynx:$lynxVersion")
|
|
@@ -161,6 +207,12 @@ dependencies {
|
|
|
161
207
|
implementation("com.facebook.fresco:animated-webp:$frescoVersion")
|
|
162
208
|
implementation("com.facebook.fresco:webpsupport:$frescoVersion")
|
|
163
209
|
implementation("com.facebook.fresco:animated-base:$frescoVersion")
|
|
210
|
+
// Fresco 2.3 loads its native libraries with SoLoader 0.9, which can
|
|
211
|
+
// crash on devices that run 64-bit code only, and Google Play holds back
|
|
212
|
+
// an app that ships it. 0.10.5 fixes that and keeps the API Fresco 2.x
|
|
213
|
+
// calls (React Native ships the two together), so Gradle's conflict
|
|
214
|
+
// resolution moves Fresco onto it.
|
|
215
|
+
implementation("com.facebook.soloader:soloader:0.13.0")
|
|
164
216
|
implementation("com.squareup.okhttp3:okhttp:4.12.1")
|
|
165
217
|
|
|
166
218
|
// XElement: <input>, <textarea>, <overlay>, <svg>, <blur-view>,
|
|
@@ -175,8 +227,25 @@ dependencies {
|
|
|
175
227
|
exclude(group = "org.lynxsdk.lynx", module = "xelement-markdown")
|
|
176
228
|
}
|
|
177
229
|
// The umbrella declares its modules for runtime only. EdgeToEdgeOverlay
|
|
178
|
-
// extends <overlay>'s classes, so that module is compiled against too
|
|
230
|
+
// extends <overlay>'s classes, so that module is compiled against too,
|
|
231
|
+
// as is <video>'s.
|
|
179
232
|
implementation("org.lynxsdk.lynx:xelement-overlay:$lynxVersion")
|
|
233
|
+
// VideoUI extends <video>'s classes and reads its media3 PlayerView, at
|
|
234
|
+
// the media3 version xelement-video depends on.
|
|
235
|
+
implementation("org.lynxsdk.lynx:xelement-video:$lynxVersion")
|
|
236
|
+
implementation("androidx.media3:media3-ui:1.0.2")
|
|
237
|
+
implementation("androidx.media3:media3-common:1.0.2")
|
|
238
|
+
// cspell:words servalsvg libserval
|
|
239
|
+
// ServalSVG, the engine <svg> draws with. xelement-svg asks for 0.0.2,
|
|
240
|
+
// whose AAR has libserval_svg.so for arm64-v8a, armeabi-v7a and x86 but
|
|
241
|
+
// not x86_64, so on an x86_64 emulator every <svg> is blank and Lynx's
|
|
242
|
+
// SVG thread throws UnsatisfiedLinkError. 0.2.4 ships all four ABIs,
|
|
243
|
+
// 16 KB-aligned on 64-bit, and keeps the Java API xelement-svg calls;
|
|
244
|
+
// Gradle's conflict resolution moves xelement-svg onto it. It's also the
|
|
245
|
+
// version the iOS frameworks link (build-lynx-ios-frameworks.sh), so
|
|
246
|
+
// both platforms draw with the same engine. Drop this once xelement-svg
|
|
247
|
+
// depends on a release with x86_64.
|
|
248
|
+
implementation("org.lynxsdk.lynx:servalsvg:0.2.4")
|
|
180
249
|
// XElement's modules depend on appcompat and material 1.0.0, which bring
|
|
181
250
|
// vectordrawable 1.0.0 and vectordrawable-animated 1.0.0. Those two share
|
|
182
251
|
// the namespace androidx.vectordrawable, and AGP 9's manifest merger
|
|
@@ -4,6 +4,16 @@
|
|
|
4
4
|
|
|
5
5
|
<!-- fetch() and XMLHttpRequest go through Lynx's HTTP service. -->
|
|
6
6
|
<uses-permission android:name="android.permission.INTERNET" />
|
|
7
|
+
|
|
8
|
+
<!--
|
|
9
|
+
A fixed screenOrientation makes Android imply that the device needs a
|
|
10
|
+
screen in that orientation, and Google Play then hides the app from
|
|
11
|
+
devices that don't report one (some Chromebooks, TVs and headsets).
|
|
12
|
+
The app runs on them anyway, letterboxed or rotated, so neither
|
|
13
|
+
orientation is a requirement.
|
|
14
|
+
-->
|
|
15
|
+
<uses-feature android:name="android.hardware.screen.portrait" android:required="false" />
|
|
16
|
+
<uses-feature android:name="android.hardware.screen.landscape" android:required="false" />
|
|
7
17
|
<!-- angular-lynx:permissions -->
|
|
8
18
|
|
|
9
19
|
<!--
|
|
@@ -15,11 +25,17 @@
|
|
|
15
25
|
<!--
|
|
16
26
|
Auto Backup leaves out LynxSecureStorage's file (SecureStorageModule):
|
|
17
27
|
dataExtractionRules from Android 12, fullBackupContent before.
|
|
28
|
+
|
|
29
|
+
enableOnBackInvokedCallback opts in to predictive back on Android 13
|
|
30
|
+
to 15, which Android 16 turns on for an app that targets API 36:
|
|
31
|
+
without it, Android 14 sends MainActivity's back handler only the
|
|
32
|
+
finished gesture, so a stack can't move its page with the finger.
|
|
18
33
|
-->
|
|
19
34
|
<application
|
|
20
35
|
android:name=".ShellApplication"
|
|
21
36
|
android:allowBackup="true"
|
|
22
37
|
android:dataExtractionRules="@xml/data_extraction_rules"
|
|
38
|
+
android:enableOnBackInvokedCallback="true"
|
|
23
39
|
android:fullBackupContent="@xml/backup_rules"
|
|
24
40
|
android:icon="@mipmap/ic_launcher"
|
|
25
41
|
android:roundIcon="@mipmap/ic_launcher_round"
|
|
@@ -31,13 +47,13 @@
|
|
|
31
47
|
<!--
|
|
32
48
|
One activity holding one LynxView; Angular Router does all navigation
|
|
33
49
|
inside it. configChanges keeps the activity (and the running Angular
|
|
34
|
-
app) alive on rotation, resizing
|
|
35
|
-
restarting it; the LynxView re-lays itself out. It starts with the
|
|
50
|
+
app) alive on rotation, resizing, dark-mode switches and a new font
|
|
51
|
+
size instead of restarting it; the LynxView re-lays itself out. It starts with the
|
|
36
52
|
splash screen's theme, which installSplashScreen() replaces.
|
|
37
53
|
-->
|
|
38
54
|
<activity
|
|
39
55
|
android:name=".MainActivity"
|
|
40
|
-
android:configChanges="orientation|screenSize|screenLayout|smallestScreenSize|keyboard|keyboardHidden|navigation|uiMode"
|
|
56
|
+
android:configChanges="orientation|screenSize|screenLayout|smallestScreenSize|keyboard|keyboardHidden|navigation|uiMode|fontScale"
|
|
41
57
|
android:exported="true"
|
|
42
58
|
android:launchMode="singleTask"
|
|
43
59
|
android:screenOrientation="${screenOrientation}"
|
|
@@ -48,6 +64,11 @@
|
|
|
48
64
|
<category android:name="android.intent.category.LAUNCHER" />
|
|
49
65
|
</intent-filter>
|
|
50
66
|
<!-- angular-lynx:deep-links -->
|
|
67
|
+
<!--
|
|
68
|
+
angular.json's shareTarget: the app in other apps' share
|
|
69
|
+
sheets, for its MIME types (ShareTargetModule.kt).
|
|
70
|
+
-->
|
|
71
|
+
<!-- angular-lynx:share-target -->
|
|
51
72
|
<!--
|
|
52
73
|
angular.json's shortcuts, in the menu a long press on the
|
|
53
74
|
app's icon opens. Android reads them from the launcher
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
package dev.angularlynx.shell
|
|
2
|
+
|
|
3
|
+
import android.app.Activity
|
|
4
|
+
import android.content.Context
|
|
5
|
+
import android.os.Handler
|
|
6
|
+
import android.os.Looper
|
|
7
|
+
import com.google.android.gms.common.ConnectionResult
|
|
8
|
+
import com.google.android.gms.common.GoogleApiAvailability
|
|
9
|
+
import com.google.mlkit.common.MlKitException
|
|
10
|
+
import com.google.mlkit.vision.barcode.common.Barcode
|
|
11
|
+
import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions
|
|
12
|
+
import com.google.mlkit.vision.codescanner.GmsBarcodeScanning
|
|
13
|
+
import com.lynx.jsbridge.LynxMethod
|
|
14
|
+
import com.lynx.jsbridge.LynxModule
|
|
15
|
+
import com.lynx.react.bridge.Callback
|
|
16
|
+
import com.lynx.react.bridge.JavaOnlyMap
|
|
17
|
+
import com.lynx.react.bridge.ReadableMap
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The native module behind the runtime's `LynxBarcodeScanner`, which calls it
|
|
21
|
+
* as `NativeModules.AngularLynxBarcodeScanner` (from the background thread,
|
|
22
|
+
* where NativeModules lives). MainActivity registers it with itself as the
|
|
23
|
+
* param: the scanner opens over it.
|
|
24
|
+
*
|
|
25
|
+
* It opens Google's code scanner, which runs in Google Play services rather
|
|
26
|
+
* than the app: Play services shows the camera and hands back only the code
|
|
27
|
+
* read, so the app needs no CAMERA permission and draws no camera screen.
|
|
28
|
+
* A phone without Play services has no scanner, and a scan reports
|
|
29
|
+
* `unavailable`; Play services downloads the scanner the first time an app
|
|
30
|
+
* asks for it, and a scan before that's done reports `unavailable` too.
|
|
31
|
+
*
|
|
32
|
+
* Only an app whose angular.json sets `permissions.camera` has this file and
|
|
33
|
+
* the Play services library behind it (app/build.gradle.kts's
|
|
34
|
+
* `angular-lynx:barcode-scanner` lines), as only it has the iOS module. So
|
|
35
|
+
* MainActivity looks it up by name, and nothing else may name the class.
|
|
36
|
+
*/
|
|
37
|
+
class BarcodeScannerModule(context: Context, param: Any?) : LynxModule(context, param) {
|
|
38
|
+
private val activity = param as Activity
|
|
39
|
+
private val main = Handler(Looper.getMainLooper())
|
|
40
|
+
|
|
41
|
+
/** Whether a scan is open: Play services runs one at a time. */
|
|
42
|
+
private var scanning = false
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Opens the scanner and calls back once with `{ format, value }`, or
|
|
46
|
+
* `{ error, message }`, where `error` is `canceled`, `unavailable`,
|
|
47
|
+
* `denied` or `failed`, as the iOS module does.
|
|
48
|
+
*/
|
|
49
|
+
@LynxMethod
|
|
50
|
+
fun scan(request: ReadableMap, callback: Callback) {
|
|
51
|
+
val names = request.getArray("formats")?.let { formats ->
|
|
52
|
+
(0 until formats.size()).map { formats.getString(it) }
|
|
53
|
+
} ?: emptyList()
|
|
54
|
+
// Lynx calls modules off the main thread, and the scanner starts an
|
|
55
|
+
// activity.
|
|
56
|
+
main.post { start(names, callback) }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private fun start(names: List<String>, callback: Callback) {
|
|
60
|
+
if (scanning) {
|
|
61
|
+
callback.invoke(error("failed", "A scan is already open."))
|
|
62
|
+
return
|
|
63
|
+
}
|
|
64
|
+
if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(activity) !=
|
|
65
|
+
ConnectionResult.SUCCESS
|
|
66
|
+
) {
|
|
67
|
+
callback.invoke(error("unavailable", "The barcode scanner needs Google Play services."))
|
|
68
|
+
return
|
|
69
|
+
}
|
|
70
|
+
val formats = names.mapNotNull { FORMATS[it] }.distinct()
|
|
71
|
+
val options = GmsBarcodeScannerOptions.Builder()
|
|
72
|
+
.apply {
|
|
73
|
+
if (formats.isNotEmpty()) {
|
|
74
|
+
setBarcodeFormats(formats.first(), *formats.drop(1).toIntArray())
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
// Zooms in on a code that's far away, as the system camera does.
|
|
78
|
+
.enableAutoZoom()
|
|
79
|
+
.build()
|
|
80
|
+
scanning = true
|
|
81
|
+
val done = { result: JavaOnlyMap ->
|
|
82
|
+
scanning = false
|
|
83
|
+
callback.invoke(result)
|
|
84
|
+
}
|
|
85
|
+
GmsBarcodeScanning.getClient(activity, options)
|
|
86
|
+
.startScan()
|
|
87
|
+
.addOnSuccessListener { barcode ->
|
|
88
|
+
val format = NAMES[barcode.format]
|
|
89
|
+
val value = barcode.rawValue ?: barcode.displayValue
|
|
90
|
+
if (format == null || value == null) {
|
|
91
|
+
done(error("failed", "The scanner read a code it couldn't hand over."))
|
|
92
|
+
} else {
|
|
93
|
+
done(JavaOnlyMap.from(mapOf("format" to format, "value" to value)))
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
.addOnCanceledListener { done(error("canceled", "The scan was canceled.")) }
|
|
97
|
+
.addOnFailureListener { failure ->
|
|
98
|
+
val code = (failure as? MlKitException)?.errorCode
|
|
99
|
+
done(
|
|
100
|
+
when (code) {
|
|
101
|
+
MlKitException.CODE_SCANNER_CANCELLED ->
|
|
102
|
+
error("canceled", "The scan was canceled.")
|
|
103
|
+
// Play services shows the camera, so it's Play
|
|
104
|
+
// services, not the app, that the person refused it
|
|
105
|
+
// to.
|
|
106
|
+
MlKitException.CODE_SCANNER_CAMERA_PERMISSION_NOT_GRANTED ->
|
|
107
|
+
error("denied", "Google Play services isn't allowed to use the camera.")
|
|
108
|
+
MlKitException.UNAVAILABLE, MlKitException.CODE_SCANNER_UNAVAILABLE ->
|
|
109
|
+
error(
|
|
110
|
+
"unavailable",
|
|
111
|
+
"Google Play services has no barcode scanner yet. It downloads one the first time an app asks: try again shortly.",
|
|
112
|
+
)
|
|
113
|
+
else -> error("failed", failure.message ?: "The scanner could not read a code.")
|
|
114
|
+
},
|
|
115
|
+
)
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private fun error(code: String, message: String) =
|
|
120
|
+
JavaOnlyMap.from(mapOf("error" to code, "message" to message))
|
|
121
|
+
|
|
122
|
+
companion object {
|
|
123
|
+
const val NAME = "AngularLynxBarcodeScanner"
|
|
124
|
+
|
|
125
|
+
/** The runtime's BarcodeFormat names as ML Kit's formats. */
|
|
126
|
+
private val FORMATS = mapOf(
|
|
127
|
+
"qr" to Barcode.FORMAT_QR_CODE,
|
|
128
|
+
"aztec" to Barcode.FORMAT_AZTEC,
|
|
129
|
+
"dataMatrix" to Barcode.FORMAT_DATA_MATRIX,
|
|
130
|
+
"pdf417" to Barcode.FORMAT_PDF417,
|
|
131
|
+
"ean13" to Barcode.FORMAT_EAN_13,
|
|
132
|
+
"ean8" to Barcode.FORMAT_EAN_8,
|
|
133
|
+
"upcA" to Barcode.FORMAT_UPC_A,
|
|
134
|
+
"upcE" to Barcode.FORMAT_UPC_E,
|
|
135
|
+
"code39" to Barcode.FORMAT_CODE_39,
|
|
136
|
+
"code93" to Barcode.FORMAT_CODE_93,
|
|
137
|
+
"code128" to Barcode.FORMAT_CODE_128,
|
|
138
|
+
"itf" to Barcode.FORMAT_ITF,
|
|
139
|
+
"codabar" to Barcode.FORMAT_CODABAR,
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
private val NAMES = FORMATS.entries.associate { (name, format) -> format to name }
|
|
143
|
+
}
|
|
144
|
+
}
|