@tokamakdev/platform-android-arm64 0.1.1-beta.100

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.
@@ -0,0 +1,404 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+
4
+ if [[ $# -ne 3 || $1 != build ]]; then
5
+ echo "usage: entrypoint build INPUT OUTPUT" >&2
6
+ exit 2
7
+ fi
8
+
9
+ # The CLI runs the entrypoint from the pack root.
10
+ pack=$PWD
11
+ input=$2
12
+ output=$3
13
+ metadata=$input/metadata
14
+ app_name=$(cat "$metadata/app-name")
15
+ host=$(cat "$metadata/host")
16
+ identifier=$(cat "$metadata/identifier")
17
+ version=1.0
18
+ if [[ -f "$metadata/version" ]]; then
19
+ version=$(cat "$metadata/version")
20
+ fi
21
+ dev_endpoint=
22
+ dev_session_token=
23
+ if [[ -f "$metadata/dev-endpoint" ]]; then
24
+ dev_endpoint=$(cat "$metadata/dev-endpoint")
25
+ dev_session_token=$(cat "$metadata/dev-session-token")
26
+ fi
27
+ # Development builds are debug builds. Other builds are release builds,
28
+ # which R8 shrinks and a configured keystore signs; without one, the debug
29
+ # key signs them so they install for testing. Gradle reads the keystore
30
+ # settings from the environment.
31
+ variant=release
32
+ task=Release
33
+ if [[ -n "$dev_endpoint" ]]; then
34
+ variant=debug
35
+ task=Debug
36
+ fi
37
+ signing=debug
38
+ if [[ "$variant" == release && -n "${TOKAMAK_ANDROID_KEYSTORE:-}" ]]; then
39
+ if [[ ! -f "$TOKAMAK_ANDROID_KEYSTORE" ]]; then
40
+ echo "Android keystore is missing: $TOKAMAK_ANDROID_KEYSTORE" >&2
41
+ exit 1
42
+ fi
43
+ if [[ -z "${TOKAMAK_ANDROID_KEY_ALIAS:-}" || -z "${TOKAMAK_ANDROID_KEYSTORE_PASSWORD:-}" ]]; then
44
+ echo "an Android keystore needs key-alias and keystore-password" >&2
45
+ exit 1
46
+ fi
47
+ signing=release
48
+ elif [[ "$variant" == release && -n "${TOKAMAK_ANDROID_KEY_ALIAS:-}${TOKAMAK_ANDROID_KEYSTORE_PASSWORD:-}${TOKAMAK_ANDROID_KEY_PASSWORD:-}" ]]; then
49
+ echo "Android key-alias and passwords need a keystore" >&2
50
+ exit 1
51
+ fi
52
+ user_manifest=
53
+ if [[ -n "${TOKAMAK_ANDROID_MANIFEST:-}" ]]; then
54
+ user_manifest=$TOKAMAK_ANDROID_MANIFEST
55
+ if [[ ! -f "$user_manifest" ]]; then
56
+ echo "Android manifest file is missing: $user_manifest" >&2
57
+ exit 1
58
+ fi
59
+ fi
60
+ icon=${TOKAMAK_ANDROID_ICON:-}
61
+ if [[ -n "$icon" && ! -d "$icon" ]]; then
62
+ echo "android.icon must be an Android res directory: $icon" >&2
63
+ exit 1
64
+ fi
65
+ project=$(dirname "$output")/.tokamak
66
+ app=$project/app
67
+ native=$app/src/main/jniLibs/arm64-v8a
68
+ assets=$app/src/main/assets/app
69
+ kotlin=$app/src/main/kotlin/com/tokamak/runtime
70
+ plugin_api=$project/tokamak-plugin
71
+ plugin_modules=$project/plugins
72
+ # The oldest Android API level the app supports.
73
+ min_sdk=$(<"$pack/build/min-sdk")
74
+
75
+ # The Android NDK's compiler, which links the runtime into the app.
76
+ android_linker() {
77
+ local ndk=${ANDROID_NDK_HOME:-}
78
+ local sdk_root=${ANDROID_HOME:-${ANDROID_SDK_ROOT:-}}
79
+ if [[ ! -d "$ndk" && -n "$sdk_root" && -d "$sdk_root/ndk" ]]; then
80
+ ndk=$(find "$sdk_root/ndk" -mindepth 1 -maxdepth 1 -type d -print | sort | tail -n 1)
81
+ fi
82
+ local compiler
83
+ for compiler in "$ndk"/toolchains/llvm/prebuilt/*/bin/aarch64-linux-android"$min_sdk"-clang; do
84
+ if [[ -x "$compiler" ]]; then
85
+ printf '%s' "$compiler"
86
+ return
87
+ fi
88
+ done
89
+ echo "Android builds link the tokamak runtime with the Android NDK, which is not installed." >&2
90
+ echo "Install it with Android Studio's SDK Manager (SDK Tools > NDK) or \`sdkmanager \"ndk;29.0.14206865\"\`, or set ANDROID_NDK_HOME." >&2
91
+ exit 1
92
+ }
93
+
94
+ # Link the runtime into libtokamak.so, exporting the JNI functions and the
95
+ # entry points of the runtime parts the app links.
96
+ link_runtime() {
97
+ local runtime=$pack/lib/TokamakRuntime
98
+ local exports=$project/tokamak-exports.map
99
+ local compiler libraries
100
+ compiler=$(android_linker)
101
+ {
102
+ printf '{\n global:\n Java_*;\n'
103
+ sed 's/.*/ &;/' "$metadata/exported-symbols"
104
+ printf ' local:\n *;\n};\n'
105
+ } > "$exports"
106
+ read -r -a libraries <<< "$(< "$runtime/link-libraries")"
107
+ "$compiler" -shared -o "$native/libtokamak.so" \
108
+ -Wl,-soname,libtokamak.so \
109
+ -Wl,--whole-archive "$runtime/libtokamak.a" -Wl,--no-whole-archive \
110
+ -Wl,--version-script="$exports" \
111
+ -Wl,--gc-sections -Wl,--as-needed -Wl,--eh-frame-hdr \
112
+ -Wl,-z,noexecstack -Wl,-z,relro,-z,now -Wl,-z,max-page-size=16384 \
113
+ -Wl,--strip-all \
114
+ "${libraries[@]}"
115
+ }
116
+
117
+ # The signing configuration for a keystore that signs release builds.
118
+ release_signing() {
119
+ if [[ "$signing" != release ]]; then
120
+ return
121
+ fi
122
+ cat <<'EOF'
123
+ signingConfigs {
124
+ release {
125
+ storeFile file(System.getenv('TOKAMAK_ANDROID_KEYSTORE'))
126
+ storePassword System.getenv('TOKAMAK_ANDROID_KEYSTORE_PASSWORD')
127
+ keyAlias System.getenv('TOKAMAK_ANDROID_KEY_ALIAS')
128
+ keyPassword System.getenv('TOKAMAK_ANDROID_KEY_PASSWORD') ?: System.getenv('TOKAMAK_ANDROID_KEYSTORE_PASSWORD')
129
+ }
130
+ }
131
+
132
+ EOF
133
+ }
134
+
135
+ xml_escape() {
136
+ printf '%s' "$1" |
137
+ sed -e 's/&/\&amp;/g' \
138
+ -e 's/</\&lt;/g' \
139
+ -e 's/>/\&gt;/g' \
140
+ -e 's/"/\&quot;/g' \
141
+ -e "s/'/\&apos;/g"
142
+ }
143
+
144
+ rm -rf "$app/src/main/assets" "$app/src/main/jniLibs" "$app/src/main/kotlin" "$app/src/main/res" "$app/user" "$plugin_api" "$plugin_modules" "$output"
145
+ mkdir -p "$native" "$assets" "$kotlin" "$app/src/main/res" "$plugin_api/src/main/kotlin"
146
+ cp -R "$input/app/." "$assets/"
147
+ link_runtime
148
+ cp -R "$pack/native-shell/app/." "$kotlin/"
149
+ cp -R "$pack/native-shell/plugin/." "$plugin_api/src/main/kotlin/"
150
+ cat > "$plugin_api/build.gradle" <<'EOF'
151
+ plugins {
152
+ id 'com.android.library'
153
+ id 'org.jetbrains.kotlin.android'
154
+ }
155
+
156
+ android {
157
+ namespace 'com.tokamak.runtime.plugin'
158
+ }
159
+ EOF
160
+
161
+ has_icon=false
162
+ if [[ -n "$icon" ]]; then
163
+ cp -R "$icon/." "$app/src/main/res/"
164
+ shopt -s nullglob
165
+ launcher_resources=("$app/src/main/res"/mipmap*/ic_launcher.*)
166
+ if (( ${#launcher_resources[@]} == 0 )); then
167
+ echo "Android icon resources must define mipmap/ic_launcher" >&2
168
+ exit 1
169
+ fi
170
+ has_icon=true
171
+ fi
172
+
173
+ # Each plugin is a library module, so its manifest merges below the app's and
174
+ # its dependencies stay its own. Its class is a Kotlin class name in a package,
175
+ # which the registry creates, and its dependencies are Maven coordinates.
176
+ kotlin_class='^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)+$'
177
+ maven_coordinate='^[A-Za-z0-9._-]+:[A-Za-z0-9._-]+:[A-Za-z0-9._-]+$'
178
+ registry=$kotlin/TokamakPluginRegistry.kt
179
+ printf '%s\n\n%s\n%s\n' 'package com.tokamak.runtime' 'internal fun tokamakPlugins(app: TokamakApplication): List<TokamakPlugin> =' ' listOf(' > "$registry"
180
+ shopt -s nullglob
181
+ raw_names=" "
182
+ for plugin in "$input/plugins"/*/; do
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(app.host("%s")),\n' "$class" "$plugin_id" >> "$registry"
190
+ module=$plugin_modules/$plugin_id
191
+ mkdir -p "$module/src/main/kotlin"
192
+ for source in "$plugin/sources"/*; do
193
+ case "$source" in
194
+ *.kt)
195
+ cp "$source" "$module/src/main/kotlin/$(basename "$source")"
196
+ ;;
197
+ *)
198
+ echo "Android plugin source must be Kotlin: $source" >&2
199
+ exit 1
200
+ ;;
201
+ esac
202
+ done
203
+ if [[ -f "$plugin/manifest" ]]; then
204
+ cp "$plugin/manifest" "$module/src/main/AndroidManifest.xml"
205
+ fi
206
+ for resource in "$plugin/raw"/*; do
207
+ [[ -f "$resource" ]] || continue
208
+ name=${resource##*/}
209
+ name=${name#*-}
210
+ # Android names a raw resource without its extension, across every plugin.
211
+ if [[ "$raw_names" == *" ${name%%.*} "* ]]; then
212
+ echo "plugin '$plugin_id' raw resource '$name' has the name of another plugin resource" >&2
213
+ exit 1
214
+ fi
215
+ raw_names+=" ${name%%.*} "
216
+ mkdir -p "$module/src/main/res/raw"
217
+ cp "$resource" "$module/src/main/res/raw/$name"
218
+ done
219
+ {
220
+ printf '%s\n' 'plugins {' " id 'com.android.library'" " id 'org.jetbrains.kotlin.android'"
221
+ printf '%s\n\n' '}'
222
+ printf '%s\n' 'android {'
223
+ printf " namespace 'tokamak.plugins.%s'\n" "${plugin_id//-/_}"
224
+ printf '%s\n\n' '}'
225
+ printf '%s\n' 'dependencies {' " implementation project(':tokamak-plugin')"
226
+ for dependency in "$plugin/dependencies"/*; do
227
+ coordinate=$(cat "$dependency")
228
+ if [[ ! "$coordinate" =~ $maven_coordinate ]]; then
229
+ echo "plugin '$plugin_id' has invalid android dependency '$coordinate'; use group:artifact:version" >&2
230
+ exit 1
231
+ fi
232
+ printf " implementation '%s'\n" "$coordinate"
233
+ done
234
+ printf '%s\n' '}'
235
+ } > "$module/build.gradle"
236
+ done
237
+ printf '%s\n' ' )' >> "$registry"
238
+
239
+ mkdir -p "$project/app/src/main"
240
+ cat > "$project/settings.gradle" <<'EOF'
241
+ pluginManagement {
242
+ repositories {
243
+ google()
244
+ mavenCentral()
245
+ gradlePluginPortal()
246
+ }
247
+ }
248
+ dependencyResolutionManagement {
249
+ repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
250
+ repositories {
251
+ google()
252
+ mavenCentral()
253
+ }
254
+ }
255
+ rootProject.name = 'tokamak'
256
+ include ':app', ':tokamak-plugin'
257
+ EOF
258
+ for plugin in "$input/plugins"/*/; do
259
+ printf "include ':plugins:%s'\n" "$(basename "$plugin")" >> "$project/settings.gradle"
260
+ done
261
+
262
+ cat > "$project/build.gradle" <<EOF
263
+ plugins {
264
+ id 'com.android.application' version '8.7.3' apply false
265
+ id 'com.android.library' version '8.7.3' apply false
266
+ id 'org.jetbrains.kotlin.android' version '2.1.0' apply false
267
+ }
268
+
269
+ subprojects {
270
+ plugins.withId('com.android.library') {
271
+ android {
272
+ compileSdk 35
273
+ defaultConfig {
274
+ minSdk $min_sdk
275
+ }
276
+ sourceSets {
277
+ main {
278
+ kotlin.srcDirs += 'src/main/kotlin'
279
+ }
280
+ }
281
+ compileOptions {
282
+ sourceCompatibility JavaVersion.VERSION_17
283
+ targetCompatibility JavaVersion.VERSION_17
284
+ }
285
+ }
286
+ }
287
+ plugins.withId('org.jetbrains.kotlin.android') {
288
+ kotlin {
289
+ jvmToolchain(17)
290
+ }
291
+ }
292
+ }
293
+ EOF
294
+
295
+ printf '%s\n' 'android.useAndroidX=true' > "$project/gradle.properties"
296
+ cat > "$project/app/build.gradle" <<EOF
297
+ plugins {
298
+ id 'com.android.application'
299
+ id 'org.jetbrains.kotlin.android'
300
+ }
301
+
302
+ android {
303
+ namespace 'com.tokamak.runtime'
304
+ compileSdk 35
305
+
306
+ defaultConfig {
307
+ applicationId '$identifier'
308
+ minSdk $min_sdk
309
+ targetSdk 35
310
+ versionCode 1
311
+ versionName providers.gradleProperty('tokamak.version').get()
312
+ }
313
+
314
+ androidResources {
315
+ ignoreAssetsPattern = ''
316
+ }
317
+
318
+ sourceSets {
319
+ main {
320
+ kotlin.srcDirs += 'src/main/kotlin'
321
+ }
322
+ }
323
+
324
+ compileOptions {
325
+ sourceCompatibility JavaVersion.VERSION_17
326
+ targetCompatibility JavaVersion.VERSION_17
327
+ }
328
+
329
+ lint {
330
+ checkOnly 'NewApi'
331
+ abortOnError true
332
+ checkDependencies true
333
+ }
334
+
335
+ $(release_signing)
336
+ buildTypes {
337
+ release {
338
+ minifyEnabled true
339
+ proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'tokamak-rules.pro'
340
+ signingConfig signingConfigs.$signing
341
+ }
342
+ }
343
+ }
344
+
345
+ dependencies {
346
+ implementation 'androidx.lifecycle:lifecycle-process:2.11.0'
347
+ implementation 'androidx.webkit:webkit:1.12.1'
348
+ implementation project(':tokamak-plugin')
349
+ EOF
350
+ for plugin in "$input/plugins"/*/; do
351
+ printf " implementation project(':plugins:%s')\n" "$(basename "$plugin")" >> "$project/app/build.gradle"
352
+ done
353
+ printf '%s\n' '}' >> "$project/app/build.gradle"
354
+
355
+ if [[ -n "$user_manifest" ]]; then
356
+ mkdir -p "$app/user"
357
+ cp "$user_manifest" "$app/user/AndroidManifest.xml"
358
+ cat >> "$project/app/build.gradle" <<'EOF'
359
+
360
+ androidComponents {
361
+ onVariants(selector().all()) { variant ->
362
+ variant.sources.manifests.addStaticManifestFile(file('user/AndroidManifest.xml').path)
363
+ }
364
+ }
365
+ EOF
366
+ fi
367
+
368
+ manifest=$app/src/main/AndroidManifest.xml
369
+ {
370
+ printf '%s\n' \
371
+ '<manifest xmlns:android="http://schemas.android.com/apk/res/android">' \
372
+ ' <uses-permission android:name="android.permission.INTERNET" />'
373
+ icon_attribute=
374
+ if [[ "$has_icon" == true ]]; then
375
+ icon_attribute=' android:icon="@mipmap/ic_launcher"'
376
+ fi
377
+ printf ' <application android:name=".TokamakApplication" android:label="%s"%s android:usesCleartextTraffic="false">\n' "$(xml_escape "$app_name")" "$icon_attribute"
378
+ printf ' <meta-data android:name="tokamak.host" android:value="%s" />\n' "$host"
379
+ if [[ -n "$dev_endpoint" ]]; then
380
+ printf ' <meta-data android:name="tokamak.dev.endpoint" android:value="%s" />\n' "$dev_endpoint"
381
+ printf ' <meta-data android:name="tokamak.dev.session-token" android:value="%s" />\n' "$dev_session_token"
382
+ fi
383
+ printf '%s\n' \
384
+ ' <activity android:name=".TokamakActivity" android:exported="true" android:launchMode="singleTask" android:theme="@android:style/Theme.Material.NoActionBar" android:configChanges="colorMode|density|fontScale|keyboard|keyboardHidden|layoutDirection|locale|mcc|mnc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|touchscreen|uiMode">' \
385
+ ' <intent-filter>' \
386
+ ' <action android:name="android.intent.action.MAIN" />' \
387
+ ' <category android:name="android.intent.category.LAUNCHER" />' \
388
+ ' </intent-filter>' \
389
+ ' </activity>' \
390
+ ' </application>' \
391
+ '</manifest>'
392
+ } > "$manifest"
393
+
394
+ # R8 keeps every name, so class names in errors and stack traces stay real,
395
+ # and the methods the runtime calls from native code.
396
+ printf '%s\n' \
397
+ '-dontobfuscate' \
398
+ '-keep interface com.tokamak.runtime.TokamakRuntime$Plugins { *; }' \
399
+ '-keepclassmembers class * implements com.tokamak.runtime.TokamakRuntime$Plugins { *; }' \
400
+ > "$app/tokamak-rules.pro"
401
+
402
+ gradle --no-daemon "-Ptokamak.version=$version" ":app:lint$task" ":app:assemble$task" --project-dir "$project"
403
+ mkdir -p "$(dirname "$output")"
404
+ cp "$app/build/outputs/apk/$variant/app-$variant.apk" "$output"
@@ -0,0 +1 @@
1
+ 31
@@ -0,0 +1,4 @@
1
+ [diffend] Oversized file quarantined before diffing.
2
+ name: package/android-arm64/lib/TokamakRuntime/libtokamak.a
3
+ size: 34954724 bytes
4
+ sha256: 0c1c17cf934017821355ef967869fe867e2f863a59c30644be2e41ca37470ade
@@ -0,0 +1 @@
1
+ -ldl -llog -lunwind -ldl -lm -lc -llog
@@ -0,0 +1,163 @@
1
+ package com.tokamak.runtime
2
+
3
+ import android.app.Activity
4
+ import android.content.Intent
5
+ import android.os.Bundle
6
+ import android.util.Log
7
+ import android.view.ViewGroup
8
+ import android.view.WindowInsets
9
+ import android.webkit.WebView
10
+ import android.widget.FrameLayout
11
+ import androidx.webkit.WebViewFeature
12
+
13
+ private const val TAG = "tokamak"
14
+ private const val FAILED =
15
+ "<!doctype html><title>tokamak</title><h1>App failed to start</h1><p>See logcat for details.</p>"
16
+ private const val UNSUPPORTED =
17
+ "<!doctype html><title>tokamak</title><h1>Unsupported WebView</h1>" +
18
+ "<p>This device's WebView cannot serve the app's secure origin.</p>"
19
+
20
+ /** The tokamak window: shows the app's WebView over the process's runtime. */
21
+ class TokamakActivity : Activity() {
22
+ private val tokamak: TokamakApplication
23
+ get() = application as TokamakApplication
24
+ private lateinit var webView: WebView
25
+ private lateinit var pluginBridge: TokamakPluginBridge
26
+ private var runtime: TokamakRuntime? = null
27
+ private var proxyPort: Int? = null
28
+ private var restoreGeneration = 0L
29
+ private var destroyed = false
30
+
31
+ override fun onCreate(savedInstanceState: Bundle?) {
32
+ super.onCreate(savedInstanceState)
33
+ tokamak.activity = this
34
+ webView = WebView(this).apply {
35
+ settings.javaScriptEnabled = true
36
+ settings.domStorageEnabled = true
37
+ settings.setSupportMultipleWindows(false)
38
+ }
39
+ webView.webChromeClient = TokamakWebChromeClient(tokamak, tokamak.appHost)
40
+ pluginBridge = TokamakPluginBridge(this, tokamak.appHost, tokamak.plugins)
41
+ if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER)) {
42
+ pluginBridge.install(webView)
43
+ }
44
+ setContentView(webViewContainer())
45
+ // A recreated activity or one reopened from Recents carries an intent plugins have had.
46
+ val launchedFromHistory = intent.flags and Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY != 0
47
+ if (savedInstanceState == null && !launchedFromHistory) deliver(intent)
48
+ if (!proxyIsSupported()) {
49
+ show(UNSUPPORTED)
50
+ return
51
+ }
52
+ Thread(::startRuntime, "tokamak-startup").start()
53
+ }
54
+
55
+ override fun onNewIntent(intent: Intent) {
56
+ super.onNewIntent(intent)
57
+ setIntent(intent)
58
+ deliver(intent)
59
+ }
60
+
61
+ override fun onResume() {
62
+ super.onResume()
63
+ restoreGeneration += 1
64
+ val generation = restoreGeneration
65
+ runtime?.let { restoreGateway(it, generation) }
66
+ }
67
+
68
+ override fun onDestroy() {
69
+ destroyed = true
70
+ restoreGeneration += 1
71
+ if (tokamak.activity === this) tokamak.activity = null
72
+ if (isFinishing) tokamak.permissionRequests.cancel()
73
+ TokamakProxy.release(this)
74
+ pluginBridge.close()
75
+ webView.stopLoading()
76
+ webView.destroy()
77
+ super.onDestroy()
78
+ }
79
+
80
+ override fun onRequestPermissionsResult(
81
+ requestCode: Int,
82
+ permissions: Array<out String>,
83
+ grantResults: IntArray,
84
+ ) {
85
+ super.onRequestPermissionsResult(requestCode, permissions, grantResults)
86
+ tokamak.permissionRequests.onRequestPermissionsResult(requestCode)
87
+ }
88
+
89
+ private fun deliver(intent: Intent) {
90
+ tokamak.plugins.values.forEach { it.onIntent(intent) }
91
+ }
92
+
93
+ private fun startRuntime() {
94
+ val started = runCatching { tokamak.runtime }
95
+ runOnUiThread { finishStart(started) }
96
+ }
97
+
98
+ private fun finishStart(started: Result<TokamakRuntime>) {
99
+ if (destroyed) return
100
+ val runtime = started.getOrElse { error ->
101
+ Log.e(TAG, "tokamak failed to start", error)
102
+ show(FAILED)
103
+ return
104
+ }
105
+ this.runtime = runtime
106
+ proxyPort = runtime.port
107
+ TokamakProxy.acquire(this, tokamak.appHost, runtime.port)
108
+ }
109
+
110
+ private fun restoreGateway(runtime: TokamakRuntime, generation: Long) {
111
+ Thread {
112
+ val result = runCatching { runtime.restoreGateway() }
113
+ runOnUiThread {
114
+ if (
115
+ destroyed ||
116
+ this.runtime !== runtime ||
117
+ generation != restoreGeneration
118
+ ) return@runOnUiThread
119
+ result
120
+ .onSuccess { port ->
121
+ if (port == runtime.port && port != proxyPort) {
122
+ proxyPort = port
123
+ TokamakProxy.acquire(this, tokamak.appHost, port)
124
+ }
125
+ }
126
+ .onFailure { error ->
127
+ Log.w(TAG, "tokamak gateway could not recover", error)
128
+ }
129
+ }
130
+ }.start()
131
+ }
132
+
133
+ internal fun proxyReady() {
134
+ if (destroyed) return
135
+ val runtime = runtime ?: return
136
+ webView.webViewClient =
137
+ TokamakWebViewClient(this, tokamak.appHost, runtime, pluginBridge)
138
+ webView.loadUrl("https://${tokamak.appHost}/")
139
+ }
140
+
141
+ private fun proxyIsSupported(): Boolean =
142
+ WebViewFeature.isFeatureSupported(WebViewFeature.PROXY_OVERRIDE) &&
143
+ WebViewFeature.isFeatureSupported(WebViewFeature.PROXY_OVERRIDE_REVERSE_BYPASS) &&
144
+ WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER)
145
+
146
+ private fun show(html: String) = webView.loadData(html, "text/html", "utf-8")
147
+
148
+ private fun webViewContainer(): FrameLayout =
149
+ FrameLayout(this).apply {
150
+ addView(
151
+ webView,
152
+ FrameLayout.LayoutParams(
153
+ ViewGroup.LayoutParams.MATCH_PARENT,
154
+ ViewGroup.LayoutParams.MATCH_PARENT,
155
+ ),
156
+ )
157
+ setOnApplyWindowInsetsListener { view, insets ->
158
+ val bars = insets.getInsets(WindowInsets.Type.systemBars())
159
+ view.setPadding(bars.left, bars.top, bars.right, bars.bottom)
160
+ insets
161
+ }
162
+ }
163
+ }