@tokamakdev/platform-android-arm64 0.1.1-beta.161 → 0.1.1-beta.162

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.
@@ -200,6 +200,8 @@ if [[ -n "$icon" && ! -d "$icon" ]]; then
200
200
  echo "android.icon must be an Android res directory: $icon" >&2
201
201
  exit 1
202
202
  fi
203
+ # tok passes every splash setting when the app has a splash.
204
+ splash=${TOKAMAK_ANDROID_SPLASH_WIDTH:-}
203
205
  project=$(dirname "$output")/.tokamak
204
206
  app=$project/app
205
207
  native=$app/src/main/jniLibs/arm64-v8a
@@ -299,6 +301,55 @@ if [[ -n "$icon" ]]; then
299
301
  has_icon=true
300
302
  fi
301
303
 
304
+ # The splash images are assets the activity decodes, and its colours make a
305
+ # theme, from which Android 12 and later draws the system splash without an
306
+ # icon.
307
+ theme=@android:style/Theme.Material.NoActionBar
308
+ splash_metadata=
309
+ if [[ -n "$splash" ]]; then
310
+ theme=@style/Tokamak.Splash
311
+ splash_assets=$app/src/main/assets/tokamak
312
+ res=$app/src/main/res
313
+ mkdir -p "$splash_assets" "$res/values" "$res/values-night" "$res/values-v31"
314
+ splash_metadata+=" <meta-data android:name=\"tokamak.splash.width\" android:value=\"$splash\" />"$'\n'
315
+ splash_metadata+=" <meta-data android:name=\"tokamak.splash.background\" android:value=\"$TOKAMAK_ANDROID_SPLASH_BACKGROUND\" />"$'\n'
316
+ splash_metadata+=" <meta-data android:name=\"tokamak.splash.background-dark\" android:value=\"$TOKAMAK_ANDROID_SPLASH_BACKGROUND_DARK\" />"$'\n'
317
+ for key in image image-dark; do
318
+ variable=TOKAMAK_ANDROID_SPLASH_$(printf '%s' "$key" | tr 'a-z-' 'A-Z_')
319
+ image=${!variable:-}
320
+ [[ -n "$image" ]] || continue
321
+ name=splash-$key.$(printf '%s' "${image##*.}" | tr 'A-Z' 'a-z')
322
+ # A dark image that is the light one shares its file.
323
+ if [[ "$key" == image-dark && "$image" == "${TOKAMAK_ANDROID_SPLASH_IMAGE:-}" ]]; then
324
+ name=splash-image.${name##*.}
325
+ fi
326
+ cp "$image" "$splash_assets/$name"
327
+ splash_metadata+=" <meta-data android:name=\"tokamak.splash.$key\" android:value=\"tokamak/$name\" />"$'\n'
328
+ done
329
+ cat > "$res/values/tokamak_splash.xml" <<EOF
330
+ <resources>
331
+ <color name="tokamak_splash_background">$TOKAMAK_ANDROID_SPLASH_BACKGROUND</color>
332
+ <style name="Tokamak.Splash" parent="@android:style/Theme.Material.NoActionBar">
333
+ <item name="android:windowBackground">@color/tokamak_splash_background</item>
334
+ </style>
335
+ </resources>
336
+ EOF
337
+ cat > "$res/values-night/tokamak_splash.xml" <<EOF
338
+ <resources>
339
+ <color name="tokamak_splash_background">$TOKAMAK_ANDROID_SPLASH_BACKGROUND_DARK</color>
340
+ </resources>
341
+ EOF
342
+ cat > "$res/values-v31/tokamak_splash.xml" <<'EOF'
343
+ <resources>
344
+ <style name="Tokamak.Splash" parent="@android:style/Theme.Material.NoActionBar">
345
+ <item name="android:windowBackground">@color/tokamak_splash_background</item>
346
+ <item name="android:windowSplashScreenBackground">@color/tokamak_splash_background</item>
347
+ <item name="android:windowSplashScreenAnimatedIcon">@android:color/transparent</item>
348
+ </style>
349
+ </resources>
350
+ EOF
351
+ fi
352
+
302
353
  # Each plugin's Gradle module is a library module of the app, so its manifest
303
354
  # merges below the app's and its dependencies resolve with the app's. Its class
304
355
  # is a Kotlin class name in a package, which the registry creates. The
@@ -484,12 +535,13 @@ manifest=$app/src/main/AndroidManifest.xml
484
535
  fi
485
536
  printf ' <application android:name=".TokamakApplication" android:label="%s"%s>\n' "$(xml_escape "$app_name")" "$icon_attribute"
486
537
  printf ' <meta-data android:name="tokamak.host" android:value="%s" />\n' "$host"
538
+ printf '%s' "$splash_metadata"
487
539
  if [[ -n "$dev_endpoint" ]]; then
488
540
  printf ' <meta-data android:name="tokamak.dev.endpoint" android:value="%s" />\n' "$dev_endpoint"
489
541
  printf ' <meta-data android:name="tokamak.dev.session-token" android:value="%s" />\n' "$dev_session_token"
490
542
  fi
491
543
  printf '%s\n' \
492
- ' <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">' \
544
+ " <activity android:name=\".TokamakActivity\" android:exported=\"true\" android:launchMode=\"singleTask\" android:theme=\"$theme\" android:configChanges=\"colorMode|density|fontScale|keyboard|keyboardHidden|layoutDirection|locale|mcc|mnc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|touchscreen|uiMode\">" \
493
545
  ' <intent-filter>' \
494
546
  ' <action android:name="android.intent.action.MAIN" />' \
495
547
  ' <category android:name="android.intent.category.LAUNCHER" />' \
@@ -27,6 +27,7 @@ class TokamakActivity : Activity() {
27
27
  private var proxyPort: Int? = null
28
28
  private var restoreGeneration = 0L
29
29
  private var destroyed = false
30
+ private var splash: TokamakSplash? = null
30
31
 
31
32
  override fun onCreate(savedInstanceState: Bundle?) {
32
33
  super.onCreate(savedInstanceState)
@@ -38,6 +39,7 @@ class TokamakActivity : Activity() {
38
39
  }
39
40
  webView.webChromeClient = TokamakWebChromeClient(tokamak, tokamak.appHost)
40
41
  setContentView(webViewContainer())
42
+ splash = TokamakSplash.show(this, tokamak.metadata())
41
43
  // A recreated activity or one reopened from Recents carries an intent plugins have had.
42
44
  val launchedFromHistory = intent.flags and Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY != 0
43
45
  if (savedInstanceState == null && !launchedFromHistory) deliver(intent)
@@ -127,7 +129,7 @@ class TokamakActivity : Activity() {
127
129
  private fun load(runtime: TokamakRuntime) {
128
130
  if (destroyed) return
129
131
  webView.webViewClient =
130
- TokamakWebViewClient(this, tokamak.appHost, runtime)
132
+ TokamakWebViewClient(this, tokamak.appHost, runtime, ::pageFinished)
131
133
  webView.loadUrl("https://${tokamak.appHost}/")
132
134
  }
133
135
 
@@ -135,7 +137,23 @@ class TokamakActivity : Activity() {
135
137
  WebViewFeature.isFeatureSupported(WebViewFeature.PROXY_OVERRIDE) &&
136
138
  WebViewFeature.isFeatureSupported(WebViewFeature.PROXY_OVERRIDE_REVERSE_BYPASS)
137
139
 
138
- private fun show(html: String) = webView.loadData(html, "text/html", "utf-8")
140
+ private fun show(html: String) {
141
+ removeSplash()
142
+ webView.loadData(html, "text/html", "utf-8")
143
+ }
144
+
145
+ /** Removes the splash at the WebView's next frame, which shows the loaded page. */
146
+ private fun pageFinished() {
147
+ if (splash == null) return
148
+ webView.postVisualStateCallback(0, object : WebView.VisualStateCallback() {
149
+ override fun onComplete(requestId: Long) = removeSplash()
150
+ })
151
+ }
152
+
153
+ private fun removeSplash() {
154
+ splash?.remove()
155
+ splash = null
156
+ }
139
157
 
140
158
  private fun webViewContainer(): FrameLayout =
141
159
  FrameLayout(this).apply {
@@ -216,7 +216,7 @@ class TokamakApplication : Application(), TokamakPlugins {
216
216
  return runtime
217
217
  }
218
218
 
219
- private fun metadata() =
219
+ internal fun metadata() =
220
220
  packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA).metaData
221
221
 
222
222
  /** Copy the packaged app out of the APK once per install, so the runtime can read it as files. */
@@ -0,0 +1,91 @@
1
+ package com.tokamak.runtime
2
+
3
+ import android.app.Activity
4
+ import android.content.res.AssetManager
5
+ import android.graphics.ImageDecoder
6
+ import android.graphics.drawable.AnimatedImageDrawable
7
+ import android.graphics.drawable.Drawable
8
+ import android.os.Bundle
9
+ import android.util.Log
10
+ import android.view.Gravity
11
+ import android.view.View
12
+ import android.view.ViewGroup
13
+ import android.view.ViewTreeObserver
14
+ import android.widget.FrameLayout
15
+ import android.widget.ImageView
16
+ import kotlin.math.roundToInt
17
+
18
+ /**
19
+ * The app's splash screen: its image centred on its background colour, shown
20
+ * over the WebView from launch until the app's first page has loaded. The
21
+ * build names the images, width and colours in the manifest's meta-data.
22
+ */
23
+ internal class TokamakSplash private constructor(private val view: View) {
24
+ fun remove() {
25
+ (view.parent as? ViewGroup)?.removeView(view)
26
+ }
27
+
28
+ companion object {
29
+ /** Shows the splash `metadata` describes over `activity`, or returns null when the app has none. */
30
+ fun show(activity: Activity, metadata: Bundle?): TokamakSplash? {
31
+ if (metadata?.containsKey("tokamak.splash.width") != true) return null
32
+ val suffix = if (activity.resources.configuration.isNightModeActive) "-dark" else ""
33
+ val view = FrameLayout(activity).apply {
34
+ setBackgroundColor(metadata.getInt("tokamak.splash.background$suffix"))
35
+ importantForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
36
+ // Touches stop here rather than reaching the page beneath.
37
+ isClickable = true
38
+ }
39
+ activity.addContentView(
40
+ view,
41
+ ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT),
42
+ )
43
+ val image = metadata.getString("tokamak.splash.image$suffix")
44
+ if (image != null) {
45
+ val density = activity.resources.displayMetrics.density
46
+ addImage(activity, view, image, (metadata.getInt("tokamak.splash.width") * density).roundToInt())
47
+ }
48
+ return TokamakSplash(view)
49
+ }
50
+
51
+ /**
52
+ * Decodes the asset `image` `width` pixels wide on a worker thread and
53
+ * centres it in `view`. The window's first frame waits for the decode,
54
+ * so it shows the image, and Android 12 and later keeps its system
55
+ * splash until then.
56
+ */
57
+ private fun addImage(activity: Activity, view: FrameLayout, image: String, width: Int) {
58
+ var decoded = false
59
+ val content = activity.findViewById<View>(android.R.id.content)
60
+ content.viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
61
+ override fun onPreDraw(): Boolean {
62
+ if (decoded) content.viewTreeObserver.removeOnPreDrawListener(this)
63
+ return decoded
64
+ }
65
+ })
66
+ Thread({
67
+ val drawable = runCatching { decode(activity.assets, image, width) }
68
+ .onFailure { Log.w("tokamak", "tokamak could not decode the splash image $image", it) }
69
+ .getOrNull()
70
+ activity.runOnUiThread {
71
+ decoded = true
72
+ if (drawable == null) return@runOnUiThread
73
+ val imageView = ImageView(activity).apply {
74
+ adjustViewBounds = true
75
+ setImageDrawable(drawable)
76
+ }
77
+ view.addView(imageView, FrameLayout.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER))
78
+ // A GIF plays with the loop count stored in the file.
79
+ (drawable as? AnimatedImageDrawable)?.start()
80
+ }
81
+ }, "tokamak-splash").start()
82
+ }
83
+
84
+ /** The asset `image`, decoded `width` pixels wide at its aspect ratio. */
85
+ private fun decode(assets: AssetManager, image: String, width: Int): Drawable =
86
+ ImageDecoder.decodeDrawable(ImageDecoder.createSource(assets, image)) { decoder, info, _ ->
87
+ val height = width.toLong() * info.size.height / info.size.width
88
+ decoder.setTargetSize(width, height.toInt().coerceAtLeast(1))
89
+ }
90
+ }
91
+ }
@@ -17,11 +17,15 @@ import java.security.cert.CertificateFactory
17
17
  import java.security.cert.X509Certificate
18
18
  import java.security.spec.PKCS8EncodedKeySpec
19
19
 
20
- /** Answers the app origin's TLS challenges with the runtime's decision. */
20
+ /**
21
+ * Answers the app origin's TLS challenges with the runtime's decision, and
22
+ * calls `pageFinished` as each page finishes loading or fails to.
23
+ */
21
24
  internal class TokamakWebViewClient(
22
25
  private val activity: Activity,
23
26
  private val host: String,
24
27
  private val runtime: TokamakRuntime,
28
+ private val pageFinished: () -> Unit,
25
29
  ) : WebViewClient() {
26
30
  override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
27
31
  val uri = Uri.parse(url)
@@ -31,6 +35,8 @@ internal class TokamakWebViewClient(
31
35
  }
32
36
  }
33
37
 
38
+ override fun onPageFinished(view: WebView, url: String) = pageFinished()
39
+
34
40
  override fun shouldOverrideUrlLoading(
35
41
  view: WebView,
36
42
  request: WebResourceRequest,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tokamakdev/platform-android-arm64",
3
- "version": "0.1.1-beta.161",
3
+ "version": "0.1.1-beta.162",
4
4
  "description": "The tokamak platform pack for android-arm64",
5
5
  "repository": {
6
6
  "type": "git",