@tokamakdev/platform-android-arm64 0.1.1-beta.83 → 0.1.1-beta.85
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 +7 -2
- package/android-arm64/lib/TokamakRuntime/libtokamak.a +2 -2
- package/android-arm64/native-shell/app/TokamakApplication.kt +68 -9
- package/android-arm64/native-shell/app/TokamakPermissions.kt +31 -2
- package/android-arm64/native-shell/app/TokamakPluginBridge.kt +21 -70
- package/android-arm64/native-shell/app/TokamakPluginRequests.kt +57 -0
- package/android-arm64/native-shell/app/TokamakRuntime.kt +47 -5
- package/android-arm64/native-shell/app/TokamakWebChromeClient.kt +6 -1
- package/android-arm64/native-shell/plugin/TokamakOwnerAuthentication.kt +3 -6
- package/android-arm64/native-shell/plugin/TokamakPlugin.kt +20 -1
- package/package.json +1 -1
|
@@ -377,8 +377,13 @@ manifest=$app/src/main/AndroidManifest.xml
|
|
|
377
377
|
'</manifest>'
|
|
378
378
|
} > "$manifest"
|
|
379
379
|
|
|
380
|
-
# R8 keeps every name, so class names in errors and stack traces stay real
|
|
381
|
-
|
|
380
|
+
# R8 keeps every name, so class names in errors and stack traces stay real,
|
|
381
|
+
# and the methods the runtime calls from native code.
|
|
382
|
+
printf '%s\n' \
|
|
383
|
+
'-dontobfuscate' \
|
|
384
|
+
'-keep interface com.tokamak.runtime.TokamakRuntime$Plugins { *; }' \
|
|
385
|
+
'-keepclassmembers class * implements com.tokamak.runtime.TokamakRuntime$Plugins { *; }' \
|
|
386
|
+
> "$app/tokamak-rules.pro"
|
|
382
387
|
|
|
383
388
|
gradle --no-daemon "-Ptokamak.version=$version" ":app:lint$task" ":app:assemble$task" --project-dir "$project"
|
|
384
389
|
mkdir -p "$(dirname "$output")"
|
|
@@ -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: 34943892 bytes
|
|
4
|
+
sha256: 7b9a432c222a974f34900d4051fa8e802483cc30cdc042cd3d65ff6207652ede
|
|
@@ -7,10 +7,12 @@ import android.content.pm.PackageManager
|
|
|
7
7
|
import android.os.SystemClock
|
|
8
8
|
import android.util.Log
|
|
9
9
|
import androidx.lifecycle.DefaultLifecycleObserver
|
|
10
|
+
import androidx.lifecycle.Lifecycle
|
|
10
11
|
import androidx.lifecycle.LifecycleOwner
|
|
11
12
|
import androidx.lifecycle.ProcessLifecycleOwner
|
|
12
13
|
import java.io.File
|
|
13
14
|
import java.util.concurrent.Executors
|
|
15
|
+
import org.json.JSONTokener
|
|
14
16
|
|
|
15
17
|
private const val HOST_METADATA = "tokamak.host"
|
|
16
18
|
private const val DEV_ENDPOINT_METADATA = "tokamak.dev.endpoint"
|
|
@@ -39,8 +41,29 @@ class TokamakApplication : Application(), TokamakPlugins {
|
|
|
39
41
|
|
|
40
42
|
internal val permissionRequests = TokamakPermissions(this)
|
|
41
43
|
|
|
44
|
+
private val started = lazy(::startRuntime)
|
|
45
|
+
|
|
42
46
|
/** The running runtime, started on first use. Blocks while it starts. */
|
|
43
|
-
internal val runtime: TokamakRuntime by
|
|
47
|
+
internal val runtime: TokamakRuntime by started
|
|
48
|
+
|
|
49
|
+
/** The Worker's calls and subscriptions on the plugins. Main thread only. */
|
|
50
|
+
private val workerRequests by lazy {
|
|
51
|
+
TokamakPluginRequests<Long>(plugins) { id, result -> runtime.reply(id, result.toString()) }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The plugins as the runtime calls them, on its own threads. */
|
|
55
|
+
private val workerPlugins =
|
|
56
|
+
object : TokamakRuntime.Plugins {
|
|
57
|
+
override fun call(id: Long, plugin: String, method: String, arguments: String) =
|
|
58
|
+
mainExecutor.execute { workerRequests.run(id, false, plugin, method, parse(arguments)) }
|
|
59
|
+
|
|
60
|
+
override fun subscribe(id: Long, plugin: String, method: String, arguments: String) =
|
|
61
|
+
mainExecutor.execute { workerRequests.run(id, true, plugin, method, parse(arguments)) }
|
|
62
|
+
|
|
63
|
+
override fun unsubscribe(id: Long) = mainExecutor.execute { workerRequests.cancel(id) }
|
|
64
|
+
|
|
65
|
+
private fun parse(arguments: String): Any? = JSONTokener(arguments).nextValue()
|
|
66
|
+
}
|
|
44
67
|
|
|
45
68
|
/** The host of the app origin, `https://<appHost>`. */
|
|
46
69
|
val appHost: String by lazy {
|
|
@@ -72,6 +95,15 @@ class TokamakApplication : Application(), TokamakPlugins {
|
|
|
72
95
|
override val activity: Activity?
|
|
73
96
|
get() = this@TokamakApplication.activity
|
|
74
97
|
|
|
98
|
+
override val isInForeground: Boolean
|
|
99
|
+
get() = this@TokamakApplication.isInForeground
|
|
100
|
+
|
|
101
|
+
override fun requireForegroundActivity(): Activity =
|
|
102
|
+
this@TokamakApplication.requireForegroundActivity()
|
|
103
|
+
|
|
104
|
+
override fun wouldPrompt(permission: String): Boolean =
|
|
105
|
+
permissionRequests.wouldPrompt(permission)
|
|
106
|
+
|
|
75
107
|
override fun requestPermissions(
|
|
76
108
|
permissions: Set<String>,
|
|
77
109
|
callback: (Map<String, Boolean>) -> Unit,
|
|
@@ -81,6 +113,17 @@ class TokamakApplication : Application(), TokamakPlugins {
|
|
|
81
113
|
emitEvent("$id.$name", event, timeoutMillis)
|
|
82
114
|
}
|
|
83
115
|
|
|
116
|
+
/** The activity to show UI in; throws `NeedsUIError` while the app is in the background. */
|
|
117
|
+
internal fun requireForegroundActivity(): Activity =
|
|
118
|
+
activity?.takeIf { isInForeground } ?: throw TokamakPluginError.needsUI()
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Whether the app is in the foreground, as the runtime records it, or until the runtime has
|
|
122
|
+
* started, whether an activity exists to start it in the foreground.
|
|
123
|
+
*/
|
|
124
|
+
private val isInForeground: Boolean
|
|
125
|
+
get() = if (started.isInitialized()) runtime.isForeground else activity != null
|
|
126
|
+
|
|
84
127
|
/**
|
|
85
128
|
* Delivers the event [name] with the JSON [event] to the Worker's listeners and returns their
|
|
86
129
|
* reply as JSON. Starts the runtime when it is not running. Blocks.
|
|
@@ -91,26 +134,42 @@ class TokamakApplication : Application(), TokamakPlugins {
|
|
|
91
134
|
return started.emit(name, event, deadline - SystemClock.elapsedRealtime())
|
|
92
135
|
}
|
|
93
136
|
|
|
94
|
-
/**
|
|
137
|
+
/** Records the app moving into or out of the foreground, once the runtime has started. */
|
|
95
138
|
private fun moved(foreground: Boolean) {
|
|
139
|
+
if (started.isInitialized()) record(runtime, foreground)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Records [foreground] in [runtime], delivering `resume` or `suspend` when it changed. */
|
|
143
|
+
private fun record(runtime: TokamakRuntime, foreground: Boolean) {
|
|
144
|
+
if (!runtime.setForeground(foreground)) return
|
|
96
145
|
val name = if (foreground) "resume" else "suspend"
|
|
97
146
|
lifecycle.execute {
|
|
98
|
-
runCatching {
|
|
147
|
+
runCatching { runtime.emit(name, "{}", LIFECYCLE_DEADLINE_MILLIS) }
|
|
99
148
|
.onFailure { Log.w("tokamak", "tokamak $name failed", it) }
|
|
100
149
|
}
|
|
101
150
|
}
|
|
102
151
|
|
|
103
|
-
/**
|
|
152
|
+
/**
|
|
153
|
+
* Starts the runtime, which delivers `start` in the foreground when an activity started it,
|
|
154
|
+
* then records where the app has moved since.
|
|
155
|
+
*/
|
|
104
156
|
private fun startRuntime(): TokamakRuntime {
|
|
105
157
|
val metadata = metadata()
|
|
106
158
|
val endpoint = metadata?.getString(DEV_ENDPOINT_METADATA)
|
|
107
159
|
val sessionToken = metadata?.getString(DEV_SESSION_TOKEN_METADATA)
|
|
108
|
-
val
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
160
|
+
val startsInForeground = activity != null
|
|
161
|
+
val runtime =
|
|
162
|
+
if (!endpoint.isNullOrEmpty() && !sessionToken.isNullOrEmpty()) {
|
|
163
|
+
TokamakRuntime.startDevelopment(
|
|
164
|
+
stateDir(), appHost, endpoint, sessionToken, startsInForeground, workerPlugins,
|
|
165
|
+
)
|
|
166
|
+
} else {
|
|
167
|
+
TokamakRuntime.start(unpackApp(), stateDir(), storageDir(), appHost, startsInForeground, workerPlugins)
|
|
168
|
+
}
|
|
169
|
+
mainExecutor.execute {
|
|
170
|
+
record(runtime, ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED))
|
|
113
171
|
}
|
|
172
|
+
return runtime
|
|
114
173
|
}
|
|
115
174
|
|
|
116
175
|
private fun metadata() =
|
|
@@ -1,27 +1,46 @@
|
|
|
1
1
|
package com.tokamak.runtime
|
|
2
2
|
|
|
3
|
+
import android.content.Context
|
|
3
4
|
import android.content.pm.PackageManager
|
|
4
5
|
|
|
6
|
+
private const val PREFERENCES = "tokamak.permissions"
|
|
7
|
+
private const val REFUSED = "refused"
|
|
8
|
+
|
|
5
9
|
/**
|
|
6
10
|
* Asks for runtime permissions in the app's activity one request at a time, as Android cancels a
|
|
7
|
-
* request made while another shows.
|
|
11
|
+
* request made while another shows. Remembers the permissions the user has refused for good, as
|
|
12
|
+
* Android reports them the same way as permissions never asked for. Use it on the main thread.
|
|
8
13
|
*/
|
|
9
14
|
internal class TokamakPermissions(private val app: TokamakApplication) {
|
|
10
15
|
private class Request(val permissions: Set<String>, val callback: (Map<String, Boolean>) -> Unit)
|
|
11
16
|
|
|
12
17
|
private val queue = ArrayDeque<Request>()
|
|
18
|
+
private val preferences by lazy { app.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE) }
|
|
13
19
|
|
|
14
20
|
/** Identifies the request showing, the head of [queue]. */
|
|
15
21
|
private var requestCode = 0
|
|
16
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Asks for [permissions], then passes [callback] whether each is granted. Answers at once when
|
|
25
|
+
* none would prompt; throws `NeedsUIError` when one would while the app is in the background.
|
|
26
|
+
*/
|
|
17
27
|
fun request(permissions: Set<String>, callback: (Map<String, Boolean>) -> Unit) {
|
|
18
|
-
|
|
28
|
+
val request = Request(permissions, callback)
|
|
29
|
+
if (permissions.none(::wouldPrompt)) return answer(request)
|
|
30
|
+
app.requireForegroundActivity()
|
|
31
|
+
queue.addLast(request)
|
|
19
32
|
if (queue.size == 1) askNext()
|
|
20
33
|
}
|
|
21
34
|
|
|
35
|
+
/** Whether asking for [permission] shows the system prompt. */
|
|
36
|
+
fun wouldPrompt(permission: String): Boolean =
|
|
37
|
+
!isGranted(permission) &&
|
|
38
|
+
(app.activity?.shouldShowRequestPermissionRationale(permission) == true || permission !in refused())
|
|
39
|
+
|
|
22
40
|
fun onRequestPermissionsResult(requestCode: Int) {
|
|
23
41
|
if (requestCode != this.requestCode || queue.isEmpty()) return
|
|
24
42
|
val answered = queue.removeFirst()
|
|
43
|
+
remember(answered.permissions)
|
|
25
44
|
askNext()
|
|
26
45
|
answer(answered)
|
|
27
46
|
}
|
|
@@ -41,9 +60,19 @@ internal class TokamakPermissions(private val app: TokamakApplication) {
|
|
|
41
60
|
activity.requestPermissions(request.permissions.toTypedArray(), requestCode)
|
|
42
61
|
}
|
|
43
62
|
|
|
63
|
+
/** Records which of the answered [permissions] the user refused for good. */
|
|
64
|
+
private fun remember(permissions: Set<String>) {
|
|
65
|
+
val activity = app.activity ?: return
|
|
66
|
+
val refusedForGood =
|
|
67
|
+
permissions.filter { !isGranted(it) && !activity.shouldShowRequestPermissionRationale(it) }
|
|
68
|
+
preferences.edit().putStringSet(REFUSED, refused() - permissions + refusedForGood).apply()
|
|
69
|
+
}
|
|
70
|
+
|
|
44
71
|
private fun answer(request: Request) =
|
|
45
72
|
request.callback(request.permissions.associateWith(::isGranted))
|
|
46
73
|
|
|
74
|
+
private fun refused(): Set<String> = preferences.getStringSet(REFUSED, null).orEmpty()
|
|
75
|
+
|
|
47
76
|
private fun isGranted(permission: String): Boolean =
|
|
48
77
|
app.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED
|
|
49
78
|
}
|
|
@@ -12,12 +12,13 @@ import org.json.JSONObject
|
|
|
12
12
|
internal class TokamakPluginBridge(
|
|
13
13
|
private val activity: Activity,
|
|
14
14
|
private val host: String,
|
|
15
|
-
|
|
15
|
+
plugins: Map<String, TokamakPlugin>,
|
|
16
16
|
) : WebViewCompat.WebMessageListener {
|
|
17
17
|
private data class RequestKey(val session: String, val id: Int)
|
|
18
18
|
|
|
19
|
-
private val
|
|
19
|
+
private val requests = TokamakPluginRequests<RequestKey>(plugins, ::send)
|
|
20
20
|
private var activeSession: String? = null
|
|
21
|
+
private var replyProxy: JavaScriptReplyProxy? = null
|
|
21
22
|
|
|
22
23
|
fun install(webView: WebView) {
|
|
23
24
|
WebViewCompat.addWebMessageListener(
|
|
@@ -30,9 +31,8 @@ internal class TokamakPluginBridge(
|
|
|
30
31
|
|
|
31
32
|
fun close() {
|
|
32
33
|
activeSession = null
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
cancelAll.forEach { it() }
|
|
34
|
+
replyProxy = null
|
|
35
|
+
requests.cancelAll()
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
override fun onPostMessage(
|
|
@@ -50,79 +50,30 @@ internal class TokamakPluginBridge(
|
|
|
50
50
|
if (type == "reset") {
|
|
51
51
|
close()
|
|
52
52
|
activeSession = session
|
|
53
|
+
this.replyProxy = replyProxy
|
|
53
54
|
return
|
|
54
55
|
}
|
|
55
56
|
val id = request.optInt("id", -1)
|
|
56
57
|
if (id < 0) return
|
|
57
58
|
val key = RequestKey(session, id)
|
|
58
59
|
when (type) {
|
|
59
|
-
"cancel" ->
|
|
60
|
-
"call", "subscribe" ->
|
|
61
|
-
|
|
60
|
+
"cancel" -> requests.cancel(key)
|
|
61
|
+
"call", "subscribe" ->
|
|
62
|
+
requests.run(
|
|
63
|
+
key,
|
|
64
|
+
type == "subscribe",
|
|
65
|
+
request.optString("plugin"),
|
|
66
|
+
request.optString("method"),
|
|
67
|
+
request.opt("arguments"),
|
|
68
|
+
)
|
|
69
|
+
else -> requests.fail(key, TokamakPluginError.notSupported("Plugin operation is not supported"))
|
|
62
70
|
}
|
|
63
71
|
}
|
|
64
72
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
) {
|
|
71
|
-
val plugin = plugins[request.optString("plugin")]
|
|
72
|
-
val method = request.optString("method")
|
|
73
|
-
if (plugin == null || method.isEmpty()) {
|
|
74
|
-
sendNotSupported(replyProxy, key, "Plugin is not supported")
|
|
75
|
-
return
|
|
76
|
-
}
|
|
77
|
-
val arguments = request.opt("arguments")
|
|
78
|
-
val reply: TokamakPluginReply = { result -> send(replyProxy, key, result, isCall) }
|
|
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)
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
private fun sendNotSupported(
|
|
91
|
-
replyProxy: JavaScriptReplyProxy,
|
|
92
|
-
key: RequestKey,
|
|
93
|
-
message: String,
|
|
94
|
-
) = send(replyProxy, key, Result.failure(TokamakPluginError.notSupported(message)), true)
|
|
95
|
-
|
|
96
|
-
private fun send(
|
|
97
|
-
replyProxy: JavaScriptReplyProxy,
|
|
98
|
-
key: RequestKey,
|
|
99
|
-
result: Result<Any?>,
|
|
100
|
-
done: Boolean,
|
|
101
|
-
) = activity.runOnUiThread { deliver(replyProxy, key, result, done) }
|
|
102
|
-
|
|
103
|
-
private fun deliver(
|
|
104
|
-
replyProxy: JavaScriptReplyProxy,
|
|
105
|
-
key: RequestKey,
|
|
106
|
-
result: Result<Any?>,
|
|
107
|
-
done: Boolean,
|
|
108
|
-
) {
|
|
109
|
-
if (activeSession != key.session) return
|
|
110
|
-
val error = result.exceptionOrNull()
|
|
111
|
-
if (done) cancellations.remove(key)?.invoke()
|
|
112
|
-
val response =
|
|
113
|
-
JSONObject()
|
|
114
|
-
.put("session", key.session)
|
|
115
|
-
.put("id", key.id)
|
|
116
|
-
.put("done", done)
|
|
117
|
-
if (error == null) {
|
|
118
|
-
response.put("value", JSONObject.wrap(result.getOrNull()))
|
|
119
|
-
} else {
|
|
120
|
-
val name = (error as? TokamakPluginError)?.errorName ?: "OperationError"
|
|
121
|
-
response.put(
|
|
122
|
-
"error",
|
|
123
|
-
JSONObject().put("name", name).put("message", error.message ?: error.javaClass.name),
|
|
124
|
-
)
|
|
73
|
+
/** Passes [result] to the page that made the request [key], if it is still loaded. */
|
|
74
|
+
private fun send(key: RequestKey, result: JSONObject) =
|
|
75
|
+
activity.runOnUiThread {
|
|
76
|
+
if (activeSession != key.session) return@runOnUiThread
|
|
77
|
+
replyProxy?.postMessage(result.put("session", key.session).put("id", key.id).toString())
|
|
125
78
|
}
|
|
126
|
-
replyProxy.postMessage(response.toString())
|
|
127
|
-
}
|
|
128
79
|
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
package com.tokamak.runtime
|
|
2
|
+
|
|
3
|
+
import org.json.JSONObject
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Runs one caller's calls and subscriptions, the page's or the Worker's, on the app's plugins,
|
|
7
|
+
* keyed by [K]. [send] receives each result, on the thread the plugin replies on, as the JSON
|
|
8
|
+
* object the page's native transport receives without its session and ID. Use it on the main
|
|
9
|
+
* thread.
|
|
10
|
+
*/
|
|
11
|
+
internal class TokamakPluginRequests<K>(
|
|
12
|
+
private val plugins: Map<String, TokamakPlugin>,
|
|
13
|
+
private val send: (K, JSONObject) -> Unit,
|
|
14
|
+
) {
|
|
15
|
+
private val cancellations = mutableMapOf<K, () -> Unit>()
|
|
16
|
+
|
|
17
|
+
/** Calls [method] of [plugin], or subscribes to it when [subscribe]. */
|
|
18
|
+
fun run(key: K, subscribe: Boolean, plugin: String, method: String, arguments: Any?) {
|
|
19
|
+
val target = plugins[plugin]
|
|
20
|
+
if (target == null || method.isEmpty()) {
|
|
21
|
+
fail(key, TokamakPluginError.notSupported("Plugin is not supported"))
|
|
22
|
+
return
|
|
23
|
+
}
|
|
24
|
+
val reply: TokamakPluginReply = { result -> send(key, result(result, done = !subscribe)) }
|
|
25
|
+
try {
|
|
26
|
+
if (subscribe) {
|
|
27
|
+
cancellations[key] = target.subscribe(method, arguments, reply)
|
|
28
|
+
} else {
|
|
29
|
+
target.call(method, arguments, reply)
|
|
30
|
+
}
|
|
31
|
+
} catch (error: Exception) {
|
|
32
|
+
fail(key, error)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Ends the request [key] with [error]. */
|
|
37
|
+
fun fail(key: K, error: Throwable) = send(key, result(Result.failure(error), done = true))
|
|
38
|
+
|
|
39
|
+
/** Ends the subscription [key]. */
|
|
40
|
+
fun cancel(key: K) {
|
|
41
|
+
cancellations.remove(key)?.invoke()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Ends every subscription. */
|
|
45
|
+
fun cancelAll() {
|
|
46
|
+
val cancelAll = cancellations.values.toList()
|
|
47
|
+
cancellations.clear()
|
|
48
|
+
cancelAll.forEach { it() }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
private fun result(result: Result<Any?>, done: Boolean): JSONObject {
|
|
52
|
+
val json = JSONObject().put("done", done)
|
|
53
|
+
val error = result.exceptionOrNull() ?: return json.put("value", JSONObject.wrap(result.getOrNull()))
|
|
54
|
+
val name = (error as? TokamakPluginError)?.errorName ?: "OperationError"
|
|
55
|
+
return json.put("error", JSONObject().put("name", name).put("message", error.message ?: error.javaClass.name))
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -4,6 +4,19 @@ import java.io.File
|
|
|
4
4
|
|
|
5
5
|
/** The tokamak runtime, running for the life of the process. */
|
|
6
6
|
internal class TokamakRuntime private constructor(private val handle: Long) {
|
|
7
|
+
/**
|
|
8
|
+
* The app's plugins, which the Worker calls. The runtime calls these methods on its own
|
|
9
|
+
* threads, and each call or subscription is answered with [reply]: once for a call, and once
|
|
10
|
+
* per value for a subscription.
|
|
11
|
+
*/
|
|
12
|
+
interface Plugins {
|
|
13
|
+
fun call(id: Long, plugin: String, method: String, arguments: String)
|
|
14
|
+
|
|
15
|
+
fun subscribe(id: Long, plugin: String, method: String, arguments: String)
|
|
16
|
+
|
|
17
|
+
fun unsubscribe(id: Long)
|
|
18
|
+
}
|
|
19
|
+
|
|
7
20
|
/** The loopback port the gateway bound. */
|
|
8
21
|
val port: Int
|
|
9
22
|
get() = nativePort(handle)
|
|
@@ -17,6 +30,19 @@ internal class TokamakRuntime private constructor(private val handle: Long) {
|
|
|
17
30
|
fun emit(name: String, event: String, timeoutMillis: Long): String =
|
|
18
31
|
nativeEmit(handle, name, event, timeoutMillis)
|
|
19
32
|
|
|
33
|
+
/**
|
|
34
|
+
* Records whether the app is in the foreground, which the Worker's `getLifecycleStage()`
|
|
35
|
+
* reports from then on, and returns whether that changed.
|
|
36
|
+
*/
|
|
37
|
+
fun setForeground(foreground: Boolean): Boolean = nativeSetForeground(handle, foreground)
|
|
38
|
+
|
|
39
|
+
/** Whether the app is in the foreground, as last recorded. */
|
|
40
|
+
val isForeground: Boolean
|
|
41
|
+
get() = nativeIsForeground(handle)
|
|
42
|
+
|
|
43
|
+
/** Passes a plugin's JSON [result] to the Worker's call or subscription [id]. */
|
|
44
|
+
fun reply(id: Long, result: String) = nativeReply(handle, id, result)
|
|
45
|
+
|
|
20
46
|
/**
|
|
21
47
|
* The authority a server certificate for [host] must chain to, or null
|
|
22
48
|
* when tokamak does not vouch for the host.
|
|
@@ -36,8 +62,8 @@ internal class TokamakRuntime private constructor(private val handle: Long) {
|
|
|
36
62
|
}
|
|
37
63
|
|
|
38
64
|
/**
|
|
39
|
-
* Start the runtime, which delivers `start` with [foreground]
|
|
40
|
-
* serve the app.
|
|
65
|
+
* Start the runtime, which delivers `start` with [foreground] and whose Worker calls
|
|
66
|
+
* [plugins], throwing when it cannot serve the app.
|
|
41
67
|
*/
|
|
42
68
|
fun start(
|
|
43
69
|
packagedDir: File,
|
|
@@ -45,21 +71,26 @@ internal class TokamakRuntime private constructor(private val handle: Long) {
|
|
|
45
71
|
storageDir: File,
|
|
46
72
|
host: String,
|
|
47
73
|
foreground: Boolean,
|
|
74
|
+
plugins: Plugins,
|
|
48
75
|
): TokamakRuntime =
|
|
49
76
|
TokamakRuntime(
|
|
50
|
-
nativeStart(packagedDir.path, stateDir.path, storageDir.path, host, foreground),
|
|
77
|
+
nativeStart(packagedDir.path, stateDir.path, storageDir.path, host, foreground, plugins),
|
|
51
78
|
)
|
|
52
79
|
|
|
53
|
-
/**
|
|
80
|
+
/**
|
|
81
|
+
* Start the runtime against a host development server, delivering `start` with
|
|
82
|
+
* [foreground], whose development Worker calls [plugins].
|
|
83
|
+
*/
|
|
54
84
|
fun startDevelopment(
|
|
55
85
|
stateDir: File,
|
|
56
86
|
host: String,
|
|
57
87
|
endpoint: String,
|
|
58
88
|
sessionToken: String,
|
|
59
89
|
foreground: Boolean,
|
|
90
|
+
plugins: Plugins,
|
|
60
91
|
): TokamakRuntime =
|
|
61
92
|
TokamakRuntime(
|
|
62
|
-
nativeStartDevelopment(stateDir.path, host, endpoint, sessionToken, foreground),
|
|
93
|
+
nativeStartDevelopment(stateDir.path, host, endpoint, sessionToken, foreground, plugins),
|
|
63
94
|
)
|
|
64
95
|
|
|
65
96
|
@JvmStatic
|
|
@@ -69,6 +100,7 @@ internal class TokamakRuntime private constructor(private val handle: Long) {
|
|
|
69
100
|
storageDir: String,
|
|
70
101
|
host: String,
|
|
71
102
|
foreground: Boolean,
|
|
103
|
+
plugins: Plugins,
|
|
72
104
|
): Long
|
|
73
105
|
|
|
74
106
|
@JvmStatic
|
|
@@ -78,8 +110,18 @@ internal class TokamakRuntime private constructor(private val handle: Long) {
|
|
|
78
110
|
endpoint: String,
|
|
79
111
|
sessionToken: String,
|
|
80
112
|
foreground: Boolean,
|
|
113
|
+
plugins: Plugins,
|
|
81
114
|
): Long
|
|
82
115
|
|
|
116
|
+
@JvmStatic
|
|
117
|
+
private external fun nativeSetForeground(handle: Long, foreground: Boolean): Boolean
|
|
118
|
+
|
|
119
|
+
@JvmStatic
|
|
120
|
+
private external fun nativeIsForeground(handle: Long): Boolean
|
|
121
|
+
|
|
122
|
+
@JvmStatic
|
|
123
|
+
private external fun nativeReply(handle: Long, id: Long, result: String)
|
|
124
|
+
|
|
83
125
|
@JvmStatic
|
|
84
126
|
private external fun nativePort(handle: Long): Int
|
|
85
127
|
|
|
@@ -30,7 +30,12 @@ internal class TokamakWebChromeClient(
|
|
|
30
30
|
return
|
|
31
31
|
}
|
|
32
32
|
waiting += request
|
|
33
|
-
|
|
33
|
+
try {
|
|
34
|
+
tokamak.permissionRequests.request(missing.toSet()) { if (waiting.remove(request)) decide(request) }
|
|
35
|
+
} catch (_: TokamakPluginError) {
|
|
36
|
+
waiting -= request
|
|
37
|
+
request.deny()
|
|
38
|
+
}
|
|
34
39
|
}
|
|
35
40
|
|
|
36
41
|
override fun onPermissionRequestCanceled(request: PermissionRequest) {
|
|
@@ -21,7 +21,8 @@ fun TokamakHost.requireOwnerAuthentication(authenticators: Int) {
|
|
|
21
21
|
/**
|
|
22
22
|
* Shows the system prompt, titled [title], for the device owner to authenticate with
|
|
23
23
|
* [authenticators], authorising [crypto] when given. Calls [succeeded] once they do; otherwise
|
|
24
|
-
* [reply] receives the failure.
|
|
24
|
+
* [reply] receives the failure. Throws `NeedsUIError` while the app is in the background. Call it
|
|
25
|
+
* on the main thread.
|
|
25
26
|
*/
|
|
26
27
|
fun TokamakHost.authenticateOwner(
|
|
27
28
|
title: CharSequence,
|
|
@@ -30,11 +31,7 @@ fun TokamakHost.authenticateOwner(
|
|
|
30
31
|
reply: TokamakPluginReply,
|
|
31
32
|
succeeded: () -> Unit,
|
|
32
33
|
) {
|
|
33
|
-
val activity =
|
|
34
|
-
activity ?: throw TokamakPluginError(
|
|
35
|
-
"InvalidStateError",
|
|
36
|
-
"The app has no activity to show the authentication prompt in",
|
|
37
|
-
)
|
|
34
|
+
val activity = requireForegroundActivity()
|
|
38
35
|
val builder = BiometricPrompt.Builder(activity).setTitle(title).setAllowedAuthenticators(authenticators)
|
|
39
36
|
if ((authenticators and Authenticators.DEVICE_CREDENTIAL) == 0) {
|
|
40
37
|
builder.setNegativeButton(activity.getString(android.R.string.cancel), activity.mainExecutor) { _, _ ->
|
|
@@ -16,6 +16,9 @@ class TokamakPluginError(
|
|
|
16
16
|
fun notSupported(message: String) = TokamakPluginError("NotSupportedError", message)
|
|
17
17
|
|
|
18
18
|
fun typeError(message: String) = TokamakPluginError("TypeError", message)
|
|
19
|
+
|
|
20
|
+
/** UI was needed while the app is in the background, where it cannot show any. */
|
|
21
|
+
fun needsUI() = TokamakPluginError("NeedsUIError", "The app cannot show UI while it is in the background")
|
|
19
22
|
}
|
|
20
23
|
}
|
|
21
24
|
|
|
@@ -53,9 +56,25 @@ interface TokamakHost {
|
|
|
53
56
|
/** The app's activity, or null while it has none. */
|
|
54
57
|
val activity: Activity?
|
|
55
58
|
|
|
59
|
+
/** Whether the app is in the foreground, where it can show UI. Read it on the main thread. */
|
|
60
|
+
val isInForeground: Boolean
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The activity to show UI in. Throws `NeedsUIError` while the app is in the background, where
|
|
64
|
+
* it cannot show UI. Call it on the main thread.
|
|
65
|
+
*/
|
|
66
|
+
fun requireForegroundActivity(): Activity
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Whether asking for [permission] shows the system prompt: the app does not hold it, and the
|
|
70
|
+
* user has not refused it for good. Call it on the main thread.
|
|
71
|
+
*/
|
|
72
|
+
fun wouldPrompt(permission: String): Boolean
|
|
73
|
+
|
|
56
74
|
/**
|
|
57
75
|
* Asks the user for [permissions] in the app's activity once earlier requests finish, then
|
|
58
|
-
* passes [callback] whether each is granted.
|
|
76
|
+
* passes [callback] whether each is granted. Answers at once, without UI, when none would
|
|
77
|
+
* prompt; throws `NeedsUIError` when one would while the app is in the background. Call it on
|
|
59
78
|
* the main thread; [callback] runs on the main thread.
|
|
60
79
|
*/
|
|
61
80
|
fun requestPermissions(permissions: Set<String>, callback: (Map<String, Boolean>) -> Unit)
|