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

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.
@@ -176,7 +176,7 @@ fi
176
176
  kotlin_class='^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)+$'
177
177
  maven_coordinate='^[A-Za-z0-9._-]+:[A-Za-z0-9._-]+:[A-Za-z0-9._-]+$'
178
178
  registry=$kotlin/TokamakPluginRegistry.kt
179
- printf '%s\n\n%s\n%s\n' 'package com.tokamak.runtime' 'internal fun tokamakPlugins(host: TokamakHost): List<TokamakPlugin> =' ' listOf(' > "$registry"
179
+ printf '%s\n\n%s\n%s\n' 'package com.tokamak.runtime' 'internal fun tokamakPlugins(app: TokamakApplication): List<TokamakPlugin> =' ' listOf(' > "$registry"
180
180
  shopt -s nullglob
181
181
  for plugin in "$input/plugins"/*/; do
182
182
  plugin_id=$(basename "$plugin")
@@ -185,7 +185,7 @@ for plugin in "$input/plugins"/*/; do
185
185
  echo "plugin '$plugin_id' has invalid android class '$class'" >&2
186
186
  exit 1
187
187
  fi
188
- printf ' %s(host),\n' "$class" >> "$registry"
188
+ printf ' %s(app.host("%s")),\n' "$class" "$plugin_id" >> "$registry"
189
189
  module=$plugin_modules/$plugin_id
190
190
  mkdir -p "$module/src/main/kotlin"
191
191
  for source in "$plugin/sources"/*; do
@@ -329,6 +329,7 @@ $(release_signing)
329
329
  }
330
330
 
331
331
  dependencies {
332
+ implementation 'androidx.lifecycle:lifecycle-process:2.11.0'
332
333
  implementation 'androidx.webkit:webkit:1.12.1'
333
334
  implementation project(':tokamak-plugin')
334
335
  EOF
@@ -1,4 +1,4 @@
1
1
  [diffend] Oversized file quarantined before diffing.
2
2
  name: package/android-arm64/lib/TokamakRuntime/libtokamak.a
3
- size: 34546724 bytes
4
- sha256: 19aa516c49b2eaa9aa16329273ec5e4fdaae07abb20978a6838ff22096380d7d
3
+ size: 34737932 bytes
4
+ sha256: 5220b289d10f2cea26812c03040e92ed90a4b63b08cc1b2aa641673ea23a60b5
@@ -5,22 +5,28 @@ import android.app.Application
5
5
  import android.content.Context
6
6
  import android.content.pm.PackageManager
7
7
  import android.os.SystemClock
8
+ import android.util.Log
9
+ import androidx.lifecycle.DefaultLifecycleObserver
10
+ import androidx.lifecycle.LifecycleOwner
11
+ import androidx.lifecycle.ProcessLifecycleOwner
8
12
  import java.io.File
13
+ import java.util.concurrent.Executors
9
14
 
10
15
  private const val HOST_METADATA = "tokamak.host"
11
16
  private const val DEV_ENDPOINT_METADATA = "tokamak.dev.endpoint"
12
17
  private const val DEV_SESSION_TOKEN_METADATA = "tokamak.dev.session-token"
13
18
 
19
+ /** How long the Worker has for `resume` and `suspend`. */
20
+ private const val LIFECYCLE_DEADLINE_MILLIS = 10_000L
21
+
14
22
  /**
15
23
  * The tokamak process. Owns the runtime and native plugins, which outlive any activity, so a
16
24
  * plugin can run Worker code when the system starts the app in the background.
17
25
  */
18
- class TokamakApplication : Application(), TokamakHost {
19
- override val context: Context
20
- get() = this
21
-
26
+ class TokamakApplication : Application(), TokamakPlugins {
27
+ /** The app's activity, or null while it has none. */
22
28
  @Volatile
23
- override var activity: Activity? = null
29
+ var activity: Activity? = null
24
30
 
25
31
  /** Every plugin, by ID. */
26
32
  val plugins: Map<String, TokamakPlugin> by lazy {
@@ -41,25 +47,69 @@ class TokamakApplication : Application(), TokamakHost {
41
47
  requireNotNull(metadata()?.getString(HOST_METADATA)) { "$HOST_METADATA is required" }
42
48
  }
43
49
 
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())
50
+ /** Delivers `resume` and `suspend` one at a time, in order. */
51
+ private val lifecycle = Executors.newSingleThreadExecutor()
52
+
53
+ override fun onCreate() {
54
+ super.onCreate()
55
+ ProcessLifecycleOwner.get().lifecycle.addObserver(
56
+ object : DefaultLifecycleObserver {
57
+ override fun onStart(owner: LifecycleOwner) = moved(foreground = true)
58
+
59
+ override fun onStop(owner: LifecycleOwner) = moved(foreground = false)
60
+ },
61
+ )
48
62
  }
49
63
 
50
64
  override fun plugin(id: String): TokamakPlugin? = plugins[id]
51
65
 
52
- override fun requestPermissions(permissions: Set<String>, callback: (Map<String, Boolean>) -> Unit) =
53
- permissionRequests.request(permissions, callback)
66
+ /** The host the plugin [id] sees, whose events the Worker's listeners receive as `<id>.<name>`. */
67
+ internal fun host(id: String): TokamakHost =
68
+ object : TokamakHost {
69
+ override val context: Context
70
+ get() = this@TokamakApplication
71
+
72
+ override val activity: Activity?
73
+ get() = this@TokamakApplication.activity
74
+
75
+ override fun requestPermissions(
76
+ permissions: Set<String>,
77
+ callback: (Map<String, Boolean>) -> Unit,
78
+ ) = permissionRequests.request(permissions, callback)
79
+
80
+ override fun emit(name: String, event: String, timeoutMillis: Long): String =
81
+ emitEvent("$id.$name", event, timeoutMillis)
82
+ }
83
+
84
+ /**
85
+ * Delivers the event [name] with the JSON [event] to the Worker's listeners and returns their
86
+ * reply as JSON. Starts the runtime when it is not running. Blocks.
87
+ */
88
+ private fun emitEvent(name: String, event: String, timeoutMillis: Long): String {
89
+ val deadline = SystemClock.elapsedRealtime() + timeoutMillis
90
+ val started = runtime
91
+ return started.emit(name, event, deadline - SystemClock.elapsedRealtime())
92
+ }
93
+
94
+ /** Delivers `resume` when the app moves into the foreground and `suspend` when it moves out. */
95
+ private fun moved(foreground: Boolean) {
96
+ val name = if (foreground) "resume" else "suspend"
97
+ lifecycle.execute {
98
+ runCatching { emitEvent(name, "{}", LIFECYCLE_DEADLINE_MILLIS) }
99
+ .onFailure { Log.w("tokamak", "tokamak $name failed", it) }
100
+ }
101
+ }
54
102
 
103
+ /** Starts the runtime, which delivers `start` in the foreground when an activity started it. */
55
104
  private fun startRuntime(): TokamakRuntime {
56
105
  val metadata = metadata()
57
106
  val endpoint = metadata?.getString(DEV_ENDPOINT_METADATA)
58
107
  val sessionToken = metadata?.getString(DEV_SESSION_TOKEN_METADATA)
108
+ val foreground = activity != null
59
109
  return if (!endpoint.isNullOrEmpty() && !sessionToken.isNullOrEmpty()) {
60
- TokamakRuntime.startDevelopment(stateDir(), appHost, endpoint, sessionToken)
110
+ TokamakRuntime.startDevelopment(stateDir(), appHost, endpoint, sessionToken, foreground)
61
111
  } else {
62
- TokamakRuntime.start(unpackApp(), stateDir(), storageDir(), appHost)
112
+ TokamakRuntime.start(unpackApp(), stateDir(), storageDir(), appHost, foreground)
63
113
  }
64
114
  }
65
115
 
@@ -6,7 +6,7 @@ import android.content.pm.PackageManager
6
6
  * Asks for runtime permissions in the app's activity one request at a time, as Android cancels a
7
7
  * request made while another shows. Use it on the main thread.
8
8
  */
9
- internal class TokamakPermissions(private val host: TokamakHost) {
9
+ internal class TokamakPermissions(private val app: TokamakApplication) {
10
10
  private class Request(val permissions: Set<String>, val callback: (Map<String, Boolean>) -> Unit)
11
11
 
12
12
  private val queue = ArrayDeque<Request>()
@@ -36,7 +36,7 @@ internal class TokamakPermissions(private val host: TokamakHost) {
36
36
 
37
37
  private fun askNext() {
38
38
  val request = queue.firstOrNull() ?: return
39
- val activity = host.activity ?: return cancel()
39
+ val activity = app.activity ?: return cancel()
40
40
  requestCode += 1
41
41
  activity.requestPermissions(request.permissions.toTypedArray(), requestCode)
42
42
  }
@@ -45,5 +45,5 @@ internal class TokamakPermissions(private val host: TokamakHost) {
45
45
  request.callback(request.permissions.associateWith(::isGranted))
46
46
 
47
47
  private fun isGranted(permission: String): Boolean =
48
- host.context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED
48
+ app.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED
49
49
  }
@@ -11,11 +11,11 @@ internal class TokamakRuntime private constructor(private val handle: Long) {
11
11
  fun restoreGateway(): Int = nativeRestoreGateway(handle)
12
12
 
13
13
  /**
14
- * Posts the JSON [body] to the Worker's `/tokamak/<name>` endpoint, retrying a failed post,
15
- * blocking until it responds 200 or [timeoutMillis] passes, and returns the response body.
14
+ * Delivers the event [name] with the JSON [event] to the Worker's listeners, blocking until
15
+ * they return or [timeoutMillis] passes, and returns their reply as JSON.
16
16
  */
17
- fun call(name: String, body: String, timeoutMillis: Long): String =
18
- nativeCall(handle, name, body, timeoutMillis)
17
+ fun emit(name: String, event: String, timeoutMillis: Long): String =
18
+ nativeEmit(handle, name, event, timeoutMillis)
19
19
 
20
20
  /**
21
21
  * The authority a server certificate for [host] must chain to, or null
@@ -35,23 +35,32 @@ internal class TokamakRuntime private constructor(private val handle: Long) {
35
35
  System.loadLibrary("tokamak")
36
36
  }
37
37
 
38
- /** Start the runtime, throwing when it cannot serve the app. */
38
+ /**
39
+ * Start the runtime, which delivers `start` with [foreground], throwing when it cannot
40
+ * serve the app.
41
+ */
39
42
  fun start(
40
43
  packagedDir: File,
41
44
  stateDir: File,
42
45
  storageDir: File,
43
46
  host: String,
47
+ foreground: Boolean,
44
48
  ): TokamakRuntime =
45
- TokamakRuntime(nativeStart(packagedDir.path, stateDir.path, storageDir.path, host))
49
+ TokamakRuntime(
50
+ nativeStart(packagedDir.path, stateDir.path, storageDir.path, host, foreground),
51
+ )
46
52
 
47
- /** Start the runtime against a host development server. */
53
+ /** Start the runtime against a host development server, delivering `start` with [foreground]. */
48
54
  fun startDevelopment(
49
55
  stateDir: File,
50
56
  host: String,
51
57
  endpoint: String,
52
58
  sessionToken: String,
59
+ foreground: Boolean,
53
60
  ): TokamakRuntime =
54
- TokamakRuntime(nativeStartDevelopment(stateDir.path, host, endpoint, sessionToken))
61
+ TokamakRuntime(
62
+ nativeStartDevelopment(stateDir.path, host, endpoint, sessionToken, foreground),
63
+ )
55
64
 
56
65
  @JvmStatic
57
66
  private external fun nativeStart(
@@ -59,6 +68,7 @@ internal class TokamakRuntime private constructor(private val handle: Long) {
59
68
  stateDir: String,
60
69
  storageDir: String,
61
70
  host: String,
71
+ foreground: Boolean,
62
72
  ): Long
63
73
 
64
74
  @JvmStatic
@@ -67,6 +77,7 @@ internal class TokamakRuntime private constructor(private val handle: Long) {
67
77
  host: String,
68
78
  endpoint: String,
69
79
  sessionToken: String,
80
+ foreground: Boolean,
70
81
  ): Long
71
82
 
72
83
  @JvmStatic
@@ -76,10 +87,10 @@ internal class TokamakRuntime private constructor(private val handle: Long) {
76
87
  private external fun nativeRestoreGateway(handle: Long): Int
77
88
 
78
89
  @JvmStatic
79
- private external fun nativeCall(
90
+ private external fun nativeEmit(
80
91
  handle: Long,
81
92
  name: String,
82
- body: String,
93
+ event: String,
83
94
  timeoutMillis: Long,
84
95
  ): String
85
96
 
@@ -13,7 +13,7 @@ private val RESOURCE_PERMISSIONS =
13
13
 
14
14
  /** Grants the app origin's media capture requests once the app holds their permissions. */
15
15
  internal class TokamakWebChromeClient(
16
- private val tokamak: TokamakHost,
16
+ private val tokamak: TokamakApplication,
17
17
  private val host: String,
18
18
  ) : WebChromeClient() {
19
19
  private val waiting = mutableSetOf<PermissionRequest>()
@@ -30,7 +30,7 @@ internal class TokamakWebChromeClient(
30
30
  return
31
31
  }
32
32
  waiting += request
33
- tokamak.requestPermissions(missing.toSet()) { if (waiting.remove(request)) decide(request) }
33
+ tokamak.permissionRequests.request(missing.toSet()) { if (waiting.remove(request)) decide(request) }
34
34
  }
35
35
 
36
36
  override fun onPermissionRequestCanceled(request: PermissionRequest) {
@@ -45,5 +45,5 @@ internal class TokamakWebChromeClient(
45
45
  request.resources.map(RESOURCE_PERMISSIONS::getValue).filterNot(::isGranted)
46
46
 
47
47
  private fun isGranted(permission: String): Boolean =
48
- tokamak.context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED
48
+ tokamak.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED
49
49
  }
@@ -45,7 +45,7 @@ interface TokamakPlugin {
45
45
  fun onIntent(intent: Intent) = Unit
46
46
  }
47
47
 
48
- /** The app as a plugin sees it: its context, its activity, and its Worker. */
48
+ /** The app as one plugin sees it: its context, its activity, and its Worker. */
49
49
  interface TokamakHost {
50
50
  /** The application context. */
51
51
  val context: Context
@@ -61,17 +61,19 @@ interface TokamakHost {
61
61
  fun requestPermissions(permissions: Set<String>, callback: (Map<String, Boolean>) -> Unit)
62
62
 
63
63
  /**
64
- * Posts the JSON [body] to the Worker's `/tokamak/<name>` endpoint and returns the
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.
64
+ * Delivers the plugin's event [name], which the Worker's listeners receive as
65
+ * `<plugin>.<name>`, with the JSON [event], and returns their reply as JSON, `null` when none
66
+ * replies. Starts the runtime when it is not running. Blocks, so call it off the main thread;
67
+ * throws unless the listeners return within [timeoutMillis], which includes runtime startup.
68
68
  */
69
- fun call(name: String, body: String, timeoutMillis: Long): String
69
+ fun emit(name: String, event: String, timeoutMillis: Long): String
70
+ }
70
71
 
72
+ /** The app's plugins. The app's [android.app.Application] is one. */
73
+ interface TokamakPlugins {
71
74
  /** The plugin with [id], or null when the app does not include it. */
72
75
  fun plugin(id: String): TokamakPlugin?
73
76
  }
74
77
 
75
- /** The host of the app this context belongs to. */
76
- val Context.tokamakHost: TokamakHost
77
- get() = applicationContext as TokamakHost
78
+ /** The plugin with [id] in the app this context belongs to, or null when the app does not include it. */
79
+ fun Context.tokamakPlugin(id: String): TokamakPlugin? = (applicationContext as TokamakPlugins).plugin(id)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tokamakdev/platform-android-arm64",
3
- "version": "0.1.1-beta.82",
3
+ "version": "0.1.1-beta.83",
4
4
  "description": "The tokamak platform pack for android-arm64",
5
5
  "repository": {
6
6
  "type": "git",