aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-android/app/src/main/kotlin/org/meshbay/client/shell/EngineCheck.kt
blob: 6382182a02df9d24cb2fb0aa8ce662d2e790d685 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package org.meshbay.client.shell

import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.view.Gravity
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import androidx.webkit.WebViewCompat
import androidx.webkit.WebViewFeature

/**
 * The engine floor, checked before the page is loaded (design O6: verified,
 * not assumed).
 *
 * The WebView updates through the store independently of Android, so the floor
 * is a Chromium version, not an API level. What binds it: Ed25519 and X25519 in
 * WebCrypto (the handshake, chat and the group envelope) — Chromium 137 — and
 * the two androidx.webkit features the bridge is built on. Measured present on
 * WebView 145 (spike S-1); the floor itself still has to be confirmed on the
 * oldest real device to be supported.
 */
object EngineCheck {
    const val MIN_CHROMIUM = 137

    fun problem(context: Context): String? {
        if (!WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER) ||
            !WebViewFeature.isFeatureSupported(WebViewFeature.DOCUMENT_START_SCRIPT)) {
            return "This device's Android System WebView is too old for MeshBay."
        }
        val version = WebViewCompat.getCurrentWebViewPackage(context)?.versionName ?: return null
        val major = version.substringBefore('.').toIntOrNull() ?: return null
        return if (major < MIN_CHROMIUM) {
            "MeshBay needs Android System WebView $MIN_CHROMIUM or newer; this device has $version."
        } else null
    }

    fun screen(context: Context, problem: String): View = LinearLayout(context).apply {
        orientation = LinearLayout.VERTICAL
        gravity = Gravity.CENTER
        setPadding(48, 48, 48, 48)
        addView(TextView(context).apply { text = problem; textSize = 18f; gravity = Gravity.CENTER })
        addView(Button(context).apply {
            text = "Update Android System WebView"
            setOnClickListener {
                val id = "com.google.android.webview"
                try { context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$id"))) }
                catch (e: ActivityNotFoundException) {
                    context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$id")))
                }
            }
        })
    }
}