@tokamakdev/platform-android-arm64 0.1.1-beta.83 → 0.1.1-beta.84

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.
@@ -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
- printf '%s\n' '-dontobfuscate' > "$app/tokamak-rules.pro"
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: 34737932 bytes
4
- sha256: 5220b289d10f2cea26812c03040e92ed90a4b63b08cc1b2aa641673ea23a60b5
3
+ size: 34943420 bytes
4
+ sha256: fb4b9ea1b76fd473b5b0f9c48f885e8a975f388d2a8ef634b8d530fb511bc9bb
@@ -11,6 +11,7 @@ import androidx.lifecycle.LifecycleOwner
11
11
  import androidx.lifecycle.ProcessLifecycleOwner
12
12
  import java.io.File
13
13
  import java.util.concurrent.Executors
14
+ import org.json.JSONTokener
14
15
 
15
16
  private const val HOST_METADATA = "tokamak.host"
16
17
  private const val DEV_ENDPOINT_METADATA = "tokamak.dev.endpoint"
@@ -39,8 +40,32 @@ class TokamakApplication : Application(), TokamakPlugins {
39
40
 
40
41
  internal val permissionRequests = TokamakPermissions(this)
41
42
 
43
+ private val started = lazy(::startRuntime)
44
+
42
45
  /** The running runtime, started on first use. Blocks while it starts. */
43
- internal val runtime: TokamakRuntime by lazy(::startRuntime)
46
+ internal val runtime: TokamakRuntime by started
47
+
48
+ /** Whether the app is in the foreground, as the lifecycle last reported. Main thread only. */
49
+ private var foreground = false
50
+
51
+ /** The Worker's calls and subscriptions on the plugins. Main thread only. */
52
+ private val workerRequests by lazy {
53
+ TokamakPluginRequests<Long>(plugins) { id, result -> runtime.reply(id, result.toString()) }
54
+ }
55
+
56
+ /** The plugins as the runtime calls them, on its own threads. */
57
+ private val workerPlugins =
58
+ object : TokamakRuntime.Plugins {
59
+ override fun call(id: Long, plugin: String, method: String, arguments: String) =
60
+ mainExecutor.execute { workerRequests.run(id, false, plugin, method, parse(arguments)) }
61
+
62
+ override fun subscribe(id: Long, plugin: String, method: String, arguments: String) =
63
+ mainExecutor.execute { workerRequests.run(id, true, plugin, method, parse(arguments)) }
64
+
65
+ override fun unsubscribe(id: Long) = mainExecutor.execute { workerRequests.cancel(id) }
66
+
67
+ private fun parse(arguments: String): Any? = JSONTokener(arguments).nextValue()
68
+ }
44
69
 
45
70
  /** The host of the app origin, `https://<appHost>`. */
46
71
  val appHost: String by lazy {
@@ -72,6 +97,15 @@ class TokamakApplication : Application(), TokamakPlugins {
72
97
  override val activity: Activity?
73
98
  get() = this@TokamakApplication.activity
74
99
 
100
+ override val isInForeground: Boolean
101
+ get() = foreground
102
+
103
+ override fun requireForegroundActivity(): Activity =
104
+ this@TokamakApplication.requireForegroundActivity()
105
+
106
+ override fun wouldPrompt(permission: String): Boolean =
107
+ permissionRequests.wouldPrompt(permission)
108
+
75
109
  override fun requestPermissions(
76
110
  permissions: Set<String>,
77
111
  callback: (Map<String, Boolean>) -> Unit,
@@ -81,6 +115,10 @@ class TokamakApplication : Application(), TokamakPlugins {
81
115
  emitEvent("$id.$name", event, timeoutMillis)
82
116
  }
83
117
 
118
+ /** The activity to show UI in; throws `NeedsUIError` while the app is in the background. */
119
+ internal fun requireForegroundActivity(): Activity =
120
+ activity?.takeIf { foreground } ?: throw TokamakPluginError.needsUI()
121
+
84
122
  /**
85
123
  * Delivers the event [name] with the JSON [event] to the Worker's listeners and returns their
86
124
  * reply as JSON. Starts the runtime when it is not running. Blocks.
@@ -91,26 +129,41 @@ class TokamakApplication : Application(), TokamakPlugins {
91
129
  return started.emit(name, event, deadline - SystemClock.elapsedRealtime())
92
130
  }
93
131
 
94
- /** Delivers `resume` when the app moves into the foreground and `suspend` when it moves out. */
132
+ /** Records the app moving into or out of the foreground, once the runtime has started. */
95
133
  private fun moved(foreground: Boolean) {
134
+ this.foreground = foreground
135
+ if (started.isInitialized()) record(runtime)
136
+ }
137
+
138
+ /** Reports [foreground] to [runtime], delivering `resume` or `suspend` when it changed. */
139
+ private fun record(runtime: TokamakRuntime) {
140
+ if (!runtime.setForeground(foreground)) return
96
141
  val name = if (foreground) "resume" else "suspend"
97
142
  lifecycle.execute {
98
- runCatching { emitEvent(name, "{}", LIFECYCLE_DEADLINE_MILLIS) }
143
+ runCatching { runtime.emit(name, "{}", LIFECYCLE_DEADLINE_MILLIS) }
99
144
  .onFailure { Log.w("tokamak", "tokamak $name failed", it) }
100
145
  }
101
146
  }
102
147
 
103
- /** Starts the runtime, which delivers `start` in the foreground when an activity started it. */
148
+ /**
149
+ * Starts the runtime, which delivers `start` in the foreground when an activity started it,
150
+ * then records where the app has moved since.
151
+ */
104
152
  private fun startRuntime(): TokamakRuntime {
105
153
  val metadata = metadata()
106
154
  val endpoint = metadata?.getString(DEV_ENDPOINT_METADATA)
107
155
  val sessionToken = metadata?.getString(DEV_SESSION_TOKEN_METADATA)
108
- val foreground = activity != null
109
- return if (!endpoint.isNullOrEmpty() && !sessionToken.isNullOrEmpty()) {
110
- TokamakRuntime.startDevelopment(stateDir(), appHost, endpoint, sessionToken, foreground)
111
- } else {
112
- TokamakRuntime.start(unpackApp(), stateDir(), storageDir(), appHost, foreground)
113
- }
156
+ val startsInForeground = activity != null
157
+ val runtime =
158
+ if (!endpoint.isNullOrEmpty() && !sessionToken.isNullOrEmpty()) {
159
+ TokamakRuntime.startDevelopment(
160
+ stateDir(), appHost, endpoint, sessionToken, startsInForeground, workerPlugins,
161
+ )
162
+ } else {
163
+ TokamakRuntime.start(unpackApp(), stateDir(), storageDir(), appHost, startsInForeground, workerPlugins)
164
+ }
165
+ mainExecutor.execute { record(runtime) }
166
+ return runtime
114
167
  }
115
168
 
116
169
  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. Use it on the main thread.
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
- queue.addLast(Request(permissions, callback))
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
- private val plugins: Map<String, TokamakPlugin>,
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 cancellations = mutableMapOf<RequestKey, () -> Unit>()
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
- val cancelAll = cancellations.values.toList()
34
- cancellations.clear()
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" -> cancellations.remove(key)?.invoke()
60
- "call", "subscribe" -> dispatch(request, type == "call", key, replyProxy)
61
- else -> sendNotSupported(replyProxy, key, "Plugin operation is not supported")
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
- private fun dispatch(
66
- request: JSONObject,
67
- isCall: Boolean,
68
- key: RequestKey,
69
- replyProxy: JavaScriptReplyProxy,
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,15 @@ 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
+ /** Passes a plugin's JSON [result] to the Worker's call or subscription [id]. */
40
+ fun reply(id: Long, result: String) = nativeReply(handle, id, result)
41
+
20
42
  /**
21
43
  * The authority a server certificate for [host] must chain to, or null
22
44
  * when tokamak does not vouch for the host.
@@ -36,8 +58,8 @@ internal class TokamakRuntime private constructor(private val handle: Long) {
36
58
  }
37
59
 
38
60
  /**
39
- * Start the runtime, which delivers `start` with [foreground], throwing when it cannot
40
- * serve the app.
61
+ * Start the runtime, which delivers `start` with [foreground] and whose Worker calls
62
+ * [plugins], throwing when it cannot serve the app.
41
63
  */
42
64
  fun start(
43
65
  packagedDir: File,
@@ -45,21 +67,26 @@ internal class TokamakRuntime private constructor(private val handle: Long) {
45
67
  storageDir: File,
46
68
  host: String,
47
69
  foreground: Boolean,
70
+ plugins: Plugins,
48
71
  ): TokamakRuntime =
49
72
  TokamakRuntime(
50
- nativeStart(packagedDir.path, stateDir.path, storageDir.path, host, foreground),
73
+ nativeStart(packagedDir.path, stateDir.path, storageDir.path, host, foreground, plugins),
51
74
  )
52
75
 
53
- /** Start the runtime against a host development server, delivering `start` with [foreground]. */
76
+ /**
77
+ * Start the runtime against a host development server, delivering `start` with
78
+ * [foreground], whose development Worker calls [plugins].
79
+ */
54
80
  fun startDevelopment(
55
81
  stateDir: File,
56
82
  host: String,
57
83
  endpoint: String,
58
84
  sessionToken: String,
59
85
  foreground: Boolean,
86
+ plugins: Plugins,
60
87
  ): TokamakRuntime =
61
88
  TokamakRuntime(
62
- nativeStartDevelopment(stateDir.path, host, endpoint, sessionToken, foreground),
89
+ nativeStartDevelopment(stateDir.path, host, endpoint, sessionToken, foreground, plugins),
63
90
  )
64
91
 
65
92
  @JvmStatic
@@ -69,6 +96,7 @@ internal class TokamakRuntime private constructor(private val handle: Long) {
69
96
  storageDir: String,
70
97
  host: String,
71
98
  foreground: Boolean,
99
+ plugins: Plugins,
72
100
  ): Long
73
101
 
74
102
  @JvmStatic
@@ -78,8 +106,15 @@ internal class TokamakRuntime private constructor(private val handle: Long) {
78
106
  endpoint: String,
79
107
  sessionToken: String,
80
108
  foreground: Boolean,
109
+ plugins: Plugins,
81
110
  ): Long
82
111
 
112
+ @JvmStatic
113
+ private external fun nativeSetForeground(handle: Long, foreground: Boolean): Boolean
114
+
115
+ @JvmStatic
116
+ private external fun nativeReply(handle: Long, id: Long, result: String)
117
+
83
118
  @JvmStatic
84
119
  private external fun nativePort(handle: Long): Int
85
120
 
@@ -30,7 +30,12 @@ internal class TokamakWebChromeClient(
30
30
  return
31
31
  }
32
32
  waiting += request
33
- tokamak.permissionRequests.request(missing.toSet()) { if (waiting.remove(request)) decide(request) }
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. Call it on the main thread.
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. Without an activity, answers at once. Call it on
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)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tokamakdev/platform-android-arm64",
3
- "version": "0.1.1-beta.83",
3
+ "version": "0.1.1-beta.84",
4
4
  "description": "The tokamak platform pack for android-arm64",
5
5
  "repository": {
6
6
  "type": "git",