@tokamakdev/platform-android-arm64 0.1.1-beta.65 → 0.1.1-beta.70
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/android-arm64/build/entrypoint +24 -24
- package/android-arm64/lib/TokamakRuntime/libtokamak.a +2 -2
- package/android-arm64/native-shell/app/TokamakActivity.kt +3 -7
- package/android-arm64/native-shell/app/TokamakApplication.kt +11 -2
- package/android-arm64/native-shell/app/TokamakPermissions.kt +49 -0
- package/android-arm64/native-shell/app/TokamakPluginBridge.kt +10 -6
- package/android-arm64/native-shell/app/TokamakRuntime.kt +2 -2
- package/android-arm64/native-shell/app/TokamakWebChromeClient.kt +6 -26
- package/android-arm64/native-shell/plugin/TokamakOwnerAuthentication.kt +76 -0
- package/android-arm64/native-shell/plugin/TokamakPlugin.kt +20 -10
- package/android-arm64/native-shell/plugin/TokamakPluginArguments.kt +23 -0
- package/android-arm64/platform-pack.json +6 -0
- package/package.json +1 -1
|
@@ -172,14 +172,24 @@ if [[ -d "$icons" ]]; then
|
|
|
172
172
|
fi
|
|
173
173
|
|
|
174
174
|
# Each plugin is a library module, so its manifest merges below the app's and
|
|
175
|
-
# its dependencies stay its own.
|
|
175
|
+
# its dependencies stay its own. Its class is a Kotlin class name in a package,
|
|
176
|
+
# which the registry creates, and its dependencies are Maven coordinates.
|
|
177
|
+
kotlin_class='^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)+$'
|
|
178
|
+
maven_coordinate='^[A-Za-z0-9._-]+:[A-Za-z0-9._-]+:[A-Za-z0-9._-]+$'
|
|
179
|
+
registry=$kotlin/TokamakPluginRegistry.kt
|
|
180
|
+
printf '%s\n\n%s\n%s\n' 'package com.tokamak.runtime' 'internal fun tokamakPlugins(host: TokamakHost): List<TokamakPlugin> =' ' listOf(' > "$registry"
|
|
176
181
|
shopt -s nullglob
|
|
177
182
|
for plugin in "$input/plugins"/*/; do
|
|
178
183
|
plugin_id=$(basename "$plugin")
|
|
184
|
+
class=$(cat "$plugin/class" 2>/dev/null || true)
|
|
185
|
+
if [[ ! "$class" =~ $kotlin_class ]]; then
|
|
186
|
+
echo "plugin '$plugin_id' has invalid android class '$class'" >&2
|
|
187
|
+
exit 1
|
|
188
|
+
fi
|
|
189
|
+
printf ' %s(host),\n' "$class" >> "$registry"
|
|
179
190
|
module=$plugin_modules/$plugin_id
|
|
180
191
|
mkdir -p "$module/src/main/kotlin"
|
|
181
192
|
for source in "$plugin/sources"/*; do
|
|
182
|
-
[[ -f "$source" ]] || continue
|
|
183
193
|
case "$source" in
|
|
184
194
|
*.kt)
|
|
185
195
|
cp "$source" "$module/src/main/kotlin/$(basename "$source")"
|
|
@@ -190,8 +200,8 @@ for plugin in "$input/plugins"/*/; do
|
|
|
190
200
|
;;
|
|
191
201
|
esac
|
|
192
202
|
done
|
|
193
|
-
if [[ -f "$plugin/
|
|
194
|
-
cp "$plugin/
|
|
203
|
+
if [[ -f "$plugin/manifest" ]]; then
|
|
204
|
+
cp "$plugin/manifest" "$module/src/main/AndroidManifest.xml"
|
|
195
205
|
fi
|
|
196
206
|
{
|
|
197
207
|
printf '%s\n' 'plugins {' " id 'com.android.library'" " id 'org.jetbrains.kotlin.android'"
|
|
@@ -201,11 +211,17 @@ for plugin in "$input/plugins"/*/; do
|
|
|
201
211
|
printf '%s\n\n' '}'
|
|
202
212
|
printf '%s\n' 'dependencies {' " implementation project(':tokamak-plugin')"
|
|
203
213
|
for dependency in "$plugin/dependencies"/*; do
|
|
204
|
-
|
|
214
|
+
coordinate=$(cat "$dependency")
|
|
215
|
+
if [[ ! "$coordinate" =~ $maven_coordinate ]]; then
|
|
216
|
+
echo "plugin '$plugin_id' has invalid android dependency '$coordinate'; use group:artifact:version" >&2
|
|
217
|
+
exit 1
|
|
218
|
+
fi
|
|
219
|
+
printf " implementation '%s'\n" "$coordinate"
|
|
205
220
|
done
|
|
206
221
|
printf '%s\n' '}'
|
|
207
222
|
} > "$module/build.gradle"
|
|
208
223
|
done
|
|
224
|
+
printf '%s\n' ' )' >> "$registry"
|
|
209
225
|
|
|
210
226
|
mkdir -p "$project/app/src/main"
|
|
211
227
|
cat > "$project/settings.gradle" <<'EOF'
|
|
@@ -335,27 +351,11 @@ androidComponents {
|
|
|
335
351
|
EOF
|
|
336
352
|
fi
|
|
337
353
|
|
|
338
|
-
registry=$kotlin/TokamakPluginRegistry.kt
|
|
339
|
-
{
|
|
340
|
-
printf '%s\n\n' 'package com.tokamak.runtime'
|
|
341
|
-
printf '%s\n' 'internal fun tokamakPlugins(host: TokamakHost): List<TokamakPlugin> =' ' listOf('
|
|
342
|
-
for plugin in "$input/plugins"/*/; do
|
|
343
|
-
printf ' %s(host),\n' "$(cat "$plugin/class")"
|
|
344
|
-
done
|
|
345
|
-
printf '%s\n' ' )'
|
|
346
|
-
} > "$registry"
|
|
347
|
-
|
|
348
354
|
manifest=$app/src/main/AndroidManifest.xml
|
|
349
355
|
{
|
|
350
|
-
printf '%s\n'
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
for permission in "$input/plugins"/*/permissions/*; do
|
|
354
|
-
printf '%s\n' "$(cat "$permission")"
|
|
355
|
-
done
|
|
356
|
-
} | sort -u | while IFS= read -r permission; do
|
|
357
|
-
printf ' <uses-permission android:name="%s" />\n' "$permission"
|
|
358
|
-
done
|
|
356
|
+
printf '%s\n' \
|
|
357
|
+
'<manifest xmlns:android="http://schemas.android.com/apk/res/android">' \
|
|
358
|
+
' <uses-permission android:name="android.permission.INTERNET" />'
|
|
359
359
|
icon_attribute=
|
|
360
360
|
if [[ "$has_icon" == true ]]; then
|
|
361
361
|
icon_attribute=' android:icon="@mipmap/ic_launcher"'
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
[diffend] Oversized file quarantined before diffing.
|
|
2
2
|
name: package/android-arm64/lib/TokamakRuntime/libtokamak.a
|
|
3
|
-
size:
|
|
4
|
-
sha256:
|
|
3
|
+
size: 35083712 bytes
|
|
4
|
+
sha256: 13dcc6cf33f124508edea37c86bc194eabf1f3577062b8a3b5f7466904787987
|
|
@@ -23,7 +23,6 @@ class TokamakActivity : Activity() {
|
|
|
23
23
|
get() = application as TokamakApplication
|
|
24
24
|
private lateinit var webView: WebView
|
|
25
25
|
private lateinit var pluginBridge: TokamakPluginBridge
|
|
26
|
-
private lateinit var chromeClient: TokamakWebChromeClient
|
|
27
26
|
private var runtime: TokamakRuntime? = null
|
|
28
27
|
private var proxyPort: Int? = null
|
|
29
28
|
private var restoreGeneration = 0L
|
|
@@ -37,8 +36,7 @@ class TokamakActivity : Activity() {
|
|
|
37
36
|
settings.domStorageEnabled = true
|
|
38
37
|
settings.setSupportMultipleWindows(false)
|
|
39
38
|
}
|
|
40
|
-
|
|
41
|
-
webView.webChromeClient = chromeClient
|
|
39
|
+
webView.webChromeClient = TokamakWebChromeClient(tokamak, tokamak.appHost)
|
|
42
40
|
pluginBridge = TokamakPluginBridge(this, tokamak.appHost, tokamak.plugins)
|
|
43
41
|
if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER)) {
|
|
44
42
|
pluginBridge.install(webView)
|
|
@@ -71,6 +69,7 @@ class TokamakActivity : Activity() {
|
|
|
71
69
|
destroyed = true
|
|
72
70
|
restoreGeneration += 1
|
|
73
71
|
if (tokamak.activity === this) tokamak.activity = null
|
|
72
|
+
if (isFinishing) tokamak.permissionRequests.cancel()
|
|
74
73
|
TokamakProxy.release(this)
|
|
75
74
|
pluginBridge.close()
|
|
76
75
|
webView.stopLoading()
|
|
@@ -84,10 +83,7 @@ class TokamakActivity : Activity() {
|
|
|
84
83
|
grantResults: IntArray,
|
|
85
84
|
) {
|
|
86
85
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
|
87
|
-
|
|
88
|
-
tokamak.plugins.values.forEach {
|
|
89
|
-
it.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
|
90
|
-
}
|
|
86
|
+
tokamak.permissionRequests.onRequestPermissionsResult(requestCode)
|
|
91
87
|
}
|
|
92
88
|
|
|
93
89
|
private fun deliver(intent: Intent) {
|
|
@@ -4,6 +4,7 @@ import android.app.Activity
|
|
|
4
4
|
import android.app.Application
|
|
5
5
|
import android.content.Context
|
|
6
6
|
import android.content.pm.PackageManager
|
|
7
|
+
import android.os.SystemClock
|
|
7
8
|
import java.io.File
|
|
8
9
|
|
|
9
10
|
private const val HOST_METADATA = "tokamak.host"
|
|
@@ -30,6 +31,8 @@ class TokamakApplication : Application(), TokamakHost {
|
|
|
30
31
|
}
|
|
31
32
|
}
|
|
32
33
|
|
|
34
|
+
internal val permissionRequests = TokamakPermissions(this)
|
|
35
|
+
|
|
33
36
|
/** The running runtime, started on first use. Blocks while it starts. */
|
|
34
37
|
internal val runtime: TokamakRuntime by lazy(::startRuntime)
|
|
35
38
|
|
|
@@ -38,11 +41,17 @@ class TokamakApplication : Application(), TokamakHost {
|
|
|
38
41
|
requireNotNull(metadata()?.getString(HOST_METADATA)) { "$HOST_METADATA is required" }
|
|
39
42
|
}
|
|
40
43
|
|
|
41
|
-
override fun call(name: String, body: String, timeoutMillis: Long): String
|
|
42
|
-
|
|
44
|
+
override fun call(name: String, body: String, timeoutMillis: Long): String {
|
|
45
|
+
val deadline = SystemClock.elapsedRealtime() + timeoutMillis
|
|
46
|
+
val started = runtime
|
|
47
|
+
return started.call(name, body, deadline - SystemClock.elapsedRealtime())
|
|
48
|
+
}
|
|
43
49
|
|
|
44
50
|
override fun plugin(id: String): TokamakPlugin? = plugins[id]
|
|
45
51
|
|
|
52
|
+
override fun requestPermissions(permissions: Set<String>, callback: (Map<String, Boolean>) -> Unit) =
|
|
53
|
+
permissionRequests.request(permissions, callback)
|
|
54
|
+
|
|
46
55
|
private fun startRuntime(): TokamakRuntime {
|
|
47
56
|
val metadata = metadata()
|
|
48
57
|
val endpoint = metadata?.getString(DEV_ENDPOINT_METADATA)
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
package com.tokamak.runtime
|
|
2
|
+
|
|
3
|
+
import android.content.pm.PackageManager
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Asks for runtime permissions in the app's activity one request at a time, as Android cancels a
|
|
7
|
+
* request made while another shows. Use it on the main thread.
|
|
8
|
+
*/
|
|
9
|
+
internal class TokamakPermissions(private val host: TokamakHost) {
|
|
10
|
+
private class Request(val permissions: Set<String>, val callback: (Map<String, Boolean>) -> Unit)
|
|
11
|
+
|
|
12
|
+
private val queue = ArrayDeque<Request>()
|
|
13
|
+
|
|
14
|
+
/** Identifies the request showing, the head of [queue]. */
|
|
15
|
+
private var requestCode = 0
|
|
16
|
+
|
|
17
|
+
fun request(permissions: Set<String>, callback: (Map<String, Boolean>) -> Unit) {
|
|
18
|
+
queue.addLast(Request(permissions, callback))
|
|
19
|
+
if (queue.size == 1) askNext()
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
fun onRequestPermissionsResult(requestCode: Int) {
|
|
23
|
+
if (requestCode != this.requestCode || queue.isEmpty()) return
|
|
24
|
+
val answered = queue.removeFirst()
|
|
25
|
+
askNext()
|
|
26
|
+
answer(answered)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Ends every request, answering with the permissions' current state. */
|
|
30
|
+
fun cancel() {
|
|
31
|
+
requestCode += 1
|
|
32
|
+
val cancelled = queue.toList()
|
|
33
|
+
queue.clear()
|
|
34
|
+
cancelled.forEach(::answer)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
private fun askNext() {
|
|
38
|
+
val request = queue.firstOrNull() ?: return
|
|
39
|
+
val activity = host.activity ?: return cancel()
|
|
40
|
+
requestCode += 1
|
|
41
|
+
activity.requestPermissions(request.permissions.toTypedArray(), requestCode)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private fun answer(request: Request) =
|
|
45
|
+
request.callback(request.permissions.associateWith(::isGranted))
|
|
46
|
+
|
|
47
|
+
private fun isGranted(permission: String): Boolean =
|
|
48
|
+
host.context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED
|
|
49
|
+
}
|
|
@@ -76,10 +76,14 @@ internal class TokamakPluginBridge(
|
|
|
76
76
|
}
|
|
77
77
|
val arguments = request.opt("arguments")
|
|
78
78
|
val reply: TokamakPluginReply = { result -> send(replyProxy, key, result, isCall) }
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
79
|
+
try {
|
|
80
|
+
if (isCall) {
|
|
81
|
+
plugin.call(method, arguments, reply)
|
|
82
|
+
} else {
|
|
83
|
+
cancellations[key] = plugin.subscribe(method, arguments, reply)
|
|
84
|
+
}
|
|
85
|
+
} catch (error: Exception) {
|
|
86
|
+
send(replyProxy, key, Result.failure(error), true)
|
|
83
87
|
}
|
|
84
88
|
}
|
|
85
89
|
|
|
@@ -113,10 +117,10 @@ internal class TokamakPluginBridge(
|
|
|
113
117
|
if (error == null) {
|
|
114
118
|
response.put("value", JSONObject.wrap(result.getOrNull()))
|
|
115
119
|
} else {
|
|
116
|
-
val name = (error as? TokamakPluginError)?.errorName ?: "
|
|
120
|
+
val name = (error as? TokamakPluginError)?.errorName ?: "OperationError"
|
|
117
121
|
response.put(
|
|
118
122
|
"error",
|
|
119
|
-
JSONObject().put("name", name).put("message", error.message ?: name),
|
|
123
|
+
JSONObject().put("name", name).put("message", error.message ?: error.javaClass.name),
|
|
120
124
|
)
|
|
121
125
|
}
|
|
122
126
|
replyProxy.postMessage(response.toString())
|
|
@@ -15,8 +15,8 @@ internal class TokamakRuntime private constructor(private val handle: Long) {
|
|
|
15
15
|
fun resume() = nativeResume(handle)
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
|
-
* Posts the JSON [body] to the Worker's `/tokamak/<name>` endpoint,
|
|
19
|
-
* responds 200 or [timeoutMillis] passes, and returns the response body.
|
|
18
|
+
* Posts the JSON [body] to the Worker's `/tokamak/<name>` endpoint, retrying a failed post,
|
|
19
|
+
* blocking until it responds 200 or [timeoutMillis] passes, and returns the response body.
|
|
20
20
|
*/
|
|
21
21
|
fun call(name: String, body: String, timeoutMillis: Long): String =
|
|
22
22
|
nativeCall(handle, name, body, timeoutMillis)
|
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
package com.tokamak.runtime
|
|
2
2
|
|
|
3
3
|
import android.Manifest
|
|
4
|
-
import android.app.Activity
|
|
5
4
|
import android.content.pm.PackageManager
|
|
6
5
|
import android.webkit.PermissionRequest
|
|
7
6
|
import android.webkit.WebChromeClient
|
|
8
7
|
|
|
9
|
-
private const val MEDIA_PERMISSION_REQUEST = 0x3ED1
|
|
10
|
-
|
|
11
8
|
private val RESOURCE_PERMISSIONS =
|
|
12
9
|
mapOf(
|
|
13
10
|
PermissionRequest.RESOURCE_VIDEO_CAPTURE to Manifest.permission.CAMERA,
|
|
@@ -16,13 +13,10 @@ private val RESOURCE_PERMISSIONS =
|
|
|
16
13
|
|
|
17
14
|
/** Grants the app origin's media capture requests once the app holds their permissions. */
|
|
18
15
|
internal class TokamakWebChromeClient(
|
|
19
|
-
private val
|
|
16
|
+
private val tokamak: TokamakHost,
|
|
20
17
|
private val host: String,
|
|
21
18
|
) : WebChromeClient() {
|
|
22
|
-
private val waiting =
|
|
23
|
-
|
|
24
|
-
/** The permissions the open permission dialog asks for; empty while none is open. */
|
|
25
|
-
private var asking = emptySet<String>()
|
|
19
|
+
private val waiting = mutableSetOf<PermissionRequest>()
|
|
26
20
|
|
|
27
21
|
override fun onPermissionRequest(request: PermissionRequest) {
|
|
28
22
|
val supported = request.resources.all(RESOURCE_PERMISSIONS::containsKey)
|
|
@@ -30,33 +24,19 @@ internal class TokamakWebChromeClient(
|
|
|
30
24
|
request.deny()
|
|
31
25
|
return
|
|
32
26
|
}
|
|
33
|
-
|
|
27
|
+
val missing = missingPermissions(request)
|
|
28
|
+
if (missing.isEmpty()) {
|
|
34
29
|
request.grant(request.resources)
|
|
35
30
|
return
|
|
36
31
|
}
|
|
37
32
|
waiting += request
|
|
38
|
-
if (
|
|
33
|
+
tokamak.requestPermissions(missing.toSet()) { if (waiting.remove(request)) decide(request) }
|
|
39
34
|
}
|
|
40
35
|
|
|
41
36
|
override fun onPermissionRequestCanceled(request: PermissionRequest) {
|
|
42
37
|
waiting -= request
|
|
43
38
|
}
|
|
44
39
|
|
|
45
|
-
fun onRequestPermissionsResult(requestCode: Int) {
|
|
46
|
-
if (requestCode != MEDIA_PERMISSION_REQUEST) return
|
|
47
|
-
val answered = waiting.filter { asking.containsAll(missingPermissions(it)) }
|
|
48
|
-
waiting -= answered
|
|
49
|
-
answered.forEach(::decide)
|
|
50
|
-
askForMissingPermissions()
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
private fun askForMissingPermissions() {
|
|
54
|
-
asking = waiting.flatMap(::missingPermissions).toSet()
|
|
55
|
-
if (asking.isNotEmpty()) {
|
|
56
|
-
activity.requestPermissions(asking.toTypedArray(), MEDIA_PERMISSION_REQUEST)
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
40
|
private fun decide(request: PermissionRequest) {
|
|
61
41
|
if (missingPermissions(request).isEmpty()) request.grant(request.resources) else request.deny()
|
|
62
42
|
}
|
|
@@ -65,5 +45,5 @@ internal class TokamakWebChromeClient(
|
|
|
65
45
|
request.resources.map(RESOURCE_PERMISSIONS::getValue).filterNot(::isGranted)
|
|
66
46
|
|
|
67
47
|
private fun isGranted(permission: String): Boolean =
|
|
68
|
-
|
|
48
|
+
tokamak.context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED
|
|
69
49
|
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
package com.tokamak.runtime
|
|
2
|
+
|
|
3
|
+
import android.hardware.biometrics.BiometricManager
|
|
4
|
+
import android.hardware.biometrics.BiometricManager.Authenticators
|
|
5
|
+
import android.hardware.biometrics.BiometricPrompt
|
|
6
|
+
import android.os.CancellationSignal
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Throws unless the device owner can authenticate with [authenticators]: `NotSupportedError`
|
|
10
|
+
* without the hardware, otherwise `InvalidStateError`.
|
|
11
|
+
*/
|
|
12
|
+
fun TokamakHost.requireOwnerAuthentication(authenticators: Int) {
|
|
13
|
+
when (context.getSystemService(BiometricManager::class.java).canAuthenticate(authenticators)) {
|
|
14
|
+
BiometricManager.BIOMETRIC_SUCCESS -> Unit
|
|
15
|
+
BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE ->
|
|
16
|
+
throw TokamakPluginError.notSupported("This device cannot perform the requested authentication")
|
|
17
|
+
else -> throw TokamakPluginError("InvalidStateError", "The requested authentication is not set up on this device")
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Shows the system prompt, titled [title], for the device owner to authenticate with
|
|
23
|
+
* [authenticators], authorising [crypto] when given. Calls [succeeded] once they do; otherwise
|
|
24
|
+
* [reply] receives the failure. Call it on the main thread.
|
|
25
|
+
*/
|
|
26
|
+
fun TokamakHost.authenticateOwner(
|
|
27
|
+
title: CharSequence,
|
|
28
|
+
authenticators: Int,
|
|
29
|
+
crypto: BiometricPrompt.CryptoObject?,
|
|
30
|
+
reply: TokamakPluginReply,
|
|
31
|
+
succeeded: () -> Unit,
|
|
32
|
+
) {
|
|
33
|
+
val activity =
|
|
34
|
+
activity ?: throw TokamakPluginError(
|
|
35
|
+
"InvalidStateError",
|
|
36
|
+
"The app has no activity to show the authentication prompt in",
|
|
37
|
+
)
|
|
38
|
+
val builder = BiometricPrompt.Builder(activity).setTitle(title).setAllowedAuthenticators(authenticators)
|
|
39
|
+
if ((authenticators and Authenticators.DEVICE_CREDENTIAL) == 0) {
|
|
40
|
+
builder.setNegativeButton(activity.getString(android.R.string.cancel), activity.mainExecutor) { _, _ ->
|
|
41
|
+
reply(Result.failure(TokamakPluginError("NotAllowedError", "Authentication was cancelled")))
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
val prompt = builder.build()
|
|
45
|
+
val callback = PromptCallback(reply, succeeded)
|
|
46
|
+
if (crypto == null) {
|
|
47
|
+
prompt.authenticate(CancellationSignal(), activity.mainExecutor, callback)
|
|
48
|
+
} else {
|
|
49
|
+
prompt.authenticate(crypto, CancellationSignal(), activity.mainExecutor, callback)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
private class PromptCallback(
|
|
54
|
+
private val reply: TokamakPluginReply,
|
|
55
|
+
private val succeeded: () -> Unit,
|
|
56
|
+
) : BiometricPrompt.AuthenticationCallback() {
|
|
57
|
+
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) = succeeded()
|
|
58
|
+
|
|
59
|
+
override fun onAuthenticationError(code: Int, message: CharSequence) =
|
|
60
|
+
reply(Result.failure(TokamakPluginError(authenticationErrorName(code), message.toString())))
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private fun authenticationErrorName(code: Int): String =
|
|
64
|
+
when (code) {
|
|
65
|
+
BiometricPrompt.BIOMETRIC_ERROR_CANCELED,
|
|
66
|
+
BiometricPrompt.BIOMETRIC_ERROR_USER_CANCELED,
|
|
67
|
+
BiometricPrompt.BIOMETRIC_ERROR_TIMEOUT,
|
|
68
|
+
BiometricPrompt.BIOMETRIC_ERROR_LOCKOUT,
|
|
69
|
+
BiometricPrompt.BIOMETRIC_ERROR_LOCKOUT_PERMANENT,
|
|
70
|
+
-> "NotAllowedError"
|
|
71
|
+
BiometricPrompt.BIOMETRIC_ERROR_NO_BIOMETRICS,
|
|
72
|
+
BiometricPrompt.BIOMETRIC_ERROR_NO_DEVICE_CREDENTIAL,
|
|
73
|
+
-> "InvalidStateError"
|
|
74
|
+
BiometricPrompt.BIOMETRIC_ERROR_HW_NOT_PRESENT -> "NotSupportedError"
|
|
75
|
+
else -> "OperationError"
|
|
76
|
+
}
|
|
@@ -4,19 +4,27 @@ import android.app.Activity
|
|
|
4
4
|
import android.content.Context
|
|
5
5
|
import android.content.Intent
|
|
6
6
|
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* A failure a plugin reports to the page as a `DOMException` named [errorName]. Other exceptions
|
|
9
|
+
* reach the page as an `OperationError`.
|
|
10
|
+
*/
|
|
8
11
|
class TokamakPluginError(
|
|
9
12
|
val errorName: String,
|
|
10
13
|
message: String,
|
|
11
14
|
) : Exception(message) {
|
|
12
15
|
companion object {
|
|
13
16
|
fun notSupported(message: String) = TokamakPluginError("NotSupportedError", message)
|
|
17
|
+
|
|
18
|
+
fun typeError(message: String) = TokamakPluginError("TypeError", message)
|
|
14
19
|
}
|
|
15
20
|
}
|
|
16
21
|
|
|
17
22
|
typealias TokamakPluginReply = (Result<Any?>) -> Unit
|
|
18
23
|
|
|
19
|
-
/**
|
|
24
|
+
/**
|
|
25
|
+
* A native plugin, created once per process with the app's [TokamakHost]. A [call] or [subscribe]
|
|
26
|
+
* that throws replies with the exception.
|
|
27
|
+
*/
|
|
20
28
|
interface TokamakPlugin {
|
|
21
29
|
val id: String
|
|
22
30
|
|
|
@@ -33,12 +41,6 @@ interface TokamakPlugin {
|
|
|
33
41
|
return {}
|
|
34
42
|
}
|
|
35
43
|
|
|
36
|
-
fun onRequestPermissionsResult(
|
|
37
|
-
requestCode: Int,
|
|
38
|
-
permissions: Array<out String>,
|
|
39
|
-
grantResults: IntArray,
|
|
40
|
-
) = Unit
|
|
41
|
-
|
|
42
44
|
/** Receives the intent that started the app's activity, and each intent it receives later. */
|
|
43
45
|
fun onIntent(intent: Intent) = Unit
|
|
44
46
|
}
|
|
@@ -51,10 +53,18 @@ interface TokamakHost {
|
|
|
51
53
|
/** The app's activity, or null while it has none. */
|
|
52
54
|
val activity: Activity?
|
|
53
55
|
|
|
56
|
+
/**
|
|
57
|
+
* Asks the user for [permissions] in the app's activity once earlier requests finish, then
|
|
58
|
+
* passes [callback] whether each is granted. Without an activity, answers at once. Call it on
|
|
59
|
+
* the main thread; [callback] runs on the main thread.
|
|
60
|
+
*/
|
|
61
|
+
fun requestPermissions(permissions: Set<String>, callback: (Map<String, Boolean>) -> Unit)
|
|
62
|
+
|
|
54
63
|
/**
|
|
55
64
|
* Posts the JSON [body] to the Worker's `/tokamak/<name>` endpoint and returns the
|
|
56
|
-
* response body. Starts the runtime when it is not running. Blocks,
|
|
57
|
-
* thread; throws unless the Worker responds 200 within
|
|
65
|
+
* response body, retrying a failed post. Starts the runtime when it is not running. Blocks,
|
|
66
|
+
* so call it off the main thread; throws unless the Worker responds 200 within
|
|
67
|
+
* [timeoutMillis], which includes runtime startup.
|
|
58
68
|
*/
|
|
59
69
|
fun call(name: String, body: String, timeoutMillis: Long): String
|
|
60
70
|
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
package com.tokamak.runtime
|
|
2
|
+
|
|
3
|
+
import org.json.JSONObject
|
|
4
|
+
|
|
5
|
+
/** [arguments] as an object; throws a `TypeError` otherwise. */
|
|
6
|
+
fun requireObject(arguments: Any?): JSONObject =
|
|
7
|
+
arguments as? JSONObject ?: throw TokamakPluginError.typeError("arguments must be an object")
|
|
8
|
+
|
|
9
|
+
/** The string [name]; throws a `TypeError` otherwise. */
|
|
10
|
+
fun JSONObject.requireString(name: String): String =
|
|
11
|
+
optionalString(name) ?: throw TokamakPluginError.typeError("$name must be a string")
|
|
12
|
+
|
|
13
|
+
/** The string [name], or null when it is absent or null; throws a `TypeError` otherwise. */
|
|
14
|
+
fun JSONObject.optionalString(name: String): String? =
|
|
15
|
+
when (val value = opt(name)) {
|
|
16
|
+
null, JSONObject.NULL -> null
|
|
17
|
+
is String -> value
|
|
18
|
+
else -> throw TokamakPluginError.typeError("$name must be a string")
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The boolean [name]; throws a `TypeError` otherwise. */
|
|
22
|
+
fun JSONObject.requireBoolean(name: String): Boolean =
|
|
23
|
+
opt(name) as? Boolean ?: throw TokamakPluginError.typeError("$name must be a boolean")
|