@tokamakdev/platform-android-arm64 0.1.1-beta.44 → 0.1.1-beta.45

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.
@@ -29,6 +29,17 @@ if [[ -n "$dev_endpoint" && -z "$dev_session_token" ]] || [[ -z "$dev_endpoint"
29
29
  echo "development endpoint and session token must be provided together" >&2
30
30
  exit 1
31
31
  fi
32
+ user_manifest=
33
+ if [[ -n "${TOKAMAK_ANDROID_MANIFEST:-}" ]]; then
34
+ user_manifest=$TOKAMAK_ANDROID_MANIFEST
35
+ if [[ "$user_manifest" != /* ]]; then
36
+ user_manifest=$(cat "$metadata/project-dir")/$user_manifest
37
+ fi
38
+ if [[ ! -f "$user_manifest" ]]; then
39
+ echo "android-manifest file is missing: $user_manifest" >&2
40
+ exit 1
41
+ fi
42
+ fi
32
43
  project=$(dirname "$output")/.tokamak
33
44
  app=$project/app
34
45
  native=$app/src/main/jniLibs/arm64-v8a
@@ -46,7 +57,7 @@ xml_escape() {
46
57
  -e "s/'/\'/g"
47
58
  }
48
59
 
49
- rm -rf "$app/src/main/assets" "$app/src/main/jniLibs" "$app/src/main/kotlin" "$app/src/main/res" "$output"
60
+ rm -rf "$app/src/main/assets" "$app/src/main/jniLibs" "$app/src/main/kotlin" "$app/src/main/res" "$app/user" "$output"
50
61
  mkdir -p "$native" "$assets" "$kotlin" "$plugins_root" "$app/src/main/res"
51
62
  cp -R "$input/app/." "$assets/"
52
63
  cp "$input/runtime/libtokamak.so" "$native/libtokamak.so"
@@ -146,6 +157,11 @@ android {
146
157
  sourceCompatibility JavaVersion.VERSION_17
147
158
  targetCompatibility JavaVersion.VERSION_17
148
159
  }
160
+
161
+ lint {
162
+ checkOnly 'NewApi'
163
+ abortOnError true
164
+ }
149
165
  }
150
166
 
151
167
  kotlin {
@@ -157,6 +173,19 @@ dependencies {
157
173
  }
158
174
  EOF
159
175
 
176
+ if [[ -n "$user_manifest" ]]; then
177
+ mkdir -p "$app/user"
178
+ cp "$user_manifest" "$app/user/AndroidManifest.xml"
179
+ cat >> "$project/app/build.gradle" <<'EOF'
180
+
181
+ androidComponents {
182
+ onVariants(selector().all()) { variant ->
183
+ variant.sources.manifests.addStaticManifestFile(file('user/AndroidManifest.xml').path)
184
+ }
185
+ }
186
+ EOF
187
+ fi
188
+
160
189
  registry=$kotlin/TokamakPluginRegistry.kt
161
190
  {
162
191
  printf '%s\n\n' 'package com.tokamak.runtime'
@@ -174,13 +203,13 @@ registry=$kotlin/TokamakPluginRegistry.kt
174
203
  manifest=$app/src/main/AndroidManifest.xml
175
204
  {
176
205
  printf '%s\n' '<manifest xmlns:android="http://schemas.android.com/apk/res/android">'
177
- printf '%s\n' ' <uses-permission android:name="android.permission.INTERNET" />'
178
- for plugin in "$input/plugins"/*; do
179
- [[ -d "$plugin" ]] || continue
180
- for permission in "$plugin/permissions"/*; do
181
- [[ -f "$permission" ]] || continue
182
- printf ' <uses-permission android:name="%s" />\n' "$(cat "$permission")"
206
+ {
207
+ printf '%s\n' android.permission.INTERNET
208
+ for permission in "$input/plugins"/*/permissions/*; do
209
+ printf '%s\n' "$(cat "$permission")"
183
210
  done
211
+ } | sort -u | while IFS= read -r permission; do
212
+ printf ' <uses-permission android:name="%s" />\n' "$permission"
184
213
  done
185
214
  icon_attribute=
186
215
  if [[ "$has_icon" == true ]]; then
@@ -202,6 +231,6 @@ manifest=$app/src/main/AndroidManifest.xml
202
231
  printf '%s\n' '</manifest>'
203
232
  } > "$manifest"
204
233
 
205
- gradle --no-daemon :app:assembleDebug --project-dir "$project"
234
+ gradle --no-daemon :app:lintDebug :app:assembleDebug --project-dir "$project"
206
235
  mkdir -p "$(dirname "$output")"
207
236
  cp "$app/build/outputs/apk/debug/app-debug.apk" "$output"
@@ -26,6 +26,7 @@ private const val UNSUPPORTED =
26
26
  class TokamakActivity : Activity() {
27
27
  private lateinit var webView: WebView
28
28
  private lateinit var pluginBridge: TokamakPluginBridge
29
+ private lateinit var chromeClient: TokamakWebChromeClient
29
30
  private var runtime: TokamakRuntime? = null
30
31
  private var proxyPort: Int? = null
31
32
  private var restoreGeneration = 0L
@@ -38,6 +39,8 @@ class TokamakActivity : Activity() {
38
39
  settings.domStorageEnabled = true
39
40
  settings.setSupportMultipleWindows(false)
40
41
  }
42
+ chromeClient = TokamakWebChromeClient(this, appHost())
43
+ webView.webChromeClient = chromeClient
41
44
  pluginBridge = TokamakPluginBridge(this, appHost(), tokamakPlugins(this))
42
45
  if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER)) {
43
46
  pluginBridge.install(webView)
@@ -76,6 +79,7 @@ class TokamakActivity : Activity() {
76
79
  grantResults: IntArray,
77
80
  ) {
78
81
  super.onRequestPermissionsResult(requestCode, permissions, grantResults)
82
+ chromeClient.onRequestPermissionsResult(requestCode)
79
83
  pluginBridge.onRequestPermissionsResult(requestCode, permissions, grantResults)
80
84
  }
81
85
 
@@ -0,0 +1,9 @@
1
+ package com.tokamak.runtime
2
+
3
+ import android.net.Uri
4
+
5
+ /** Whether this URI is on the app origin, `https://<appHost>`. */
6
+ internal fun Uri.isAppOrigin(appHost: String): Boolean =
7
+ scheme.equals("https", ignoreCase = true) &&
8
+ host.equals(appHost, ignoreCase = true) &&
9
+ (port == -1 || port == 443)
@@ -91,14 +91,7 @@ internal class TokamakPluginBridge(
91
91
  isMainFrame: Boolean,
92
92
  replyProxy: JavaScriptReplyProxy,
93
93
  ) {
94
- if (
95
- !isMainFrame ||
96
- sourceOrigin.scheme != "https" ||
97
- sourceOrigin.host != host ||
98
- (sourceOrigin.port != -1 && sourceOrigin.port != 443)
99
- ) {
100
- return
101
- }
94
+ if (!isMainFrame || !sourceOrigin.isAppOrigin(host)) return
102
95
  val request = runCatching { JSONObject(message.data ?: return) }.getOrNull() ?: return
103
96
  val session = request.optString("session")
104
97
  if (session.isEmpty()) return
@@ -158,6 +151,13 @@ internal class TokamakPluginBridge(
158
151
  key: RequestKey,
159
152
  result: Result<Any?>,
160
153
  done: Boolean,
154
+ ) = activity.runOnUiThread { deliver(replyProxy, key, result, done) }
155
+
156
+ private fun deliver(
157
+ replyProxy: JavaScriptReplyProxy,
158
+ key: RequestKey,
159
+ result: Result<Any?>,
160
+ done: Boolean,
161
161
  ) {
162
162
  if (activeSession != key.session) return
163
163
  val error = result.exceptionOrNull()
@@ -176,8 +176,6 @@ internal class TokamakPluginBridge(
176
176
  JSONObject().put("name", name).put("message", error.message ?: name),
177
177
  )
178
178
  }
179
- activity.runOnUiThread {
180
- if (activeSession == key.session) replyProxy.postMessage(response.toString())
181
- }
179
+ replyProxy.postMessage(response.toString())
182
180
  }
183
181
  }
@@ -0,0 +1,69 @@
1
+ package com.tokamak.runtime
2
+
3
+ import android.Manifest
4
+ import android.app.Activity
5
+ import android.content.pm.PackageManager
6
+ import android.webkit.PermissionRequest
7
+ import android.webkit.WebChromeClient
8
+
9
+ private const val MEDIA_PERMISSION_REQUEST = 0x3ED1
10
+
11
+ private val RESOURCE_PERMISSIONS =
12
+ mapOf(
13
+ PermissionRequest.RESOURCE_VIDEO_CAPTURE to Manifest.permission.CAMERA,
14
+ PermissionRequest.RESOURCE_AUDIO_CAPTURE to Manifest.permission.RECORD_AUDIO,
15
+ )
16
+
17
+ /** Grants the app origin's media capture requests once the app holds their permissions. */
18
+ internal class TokamakWebChromeClient(
19
+ private val activity: Activity,
20
+ private val host: String,
21
+ ) : WebChromeClient() {
22
+ private val waiting = mutableListOf<PermissionRequest>()
23
+
24
+ /** The permissions the open permission dialog asks for; empty while none is open. */
25
+ private var asking = emptySet<String>()
26
+
27
+ override fun onPermissionRequest(request: PermissionRequest) {
28
+ val supported = request.resources.all(RESOURCE_PERMISSIONS::containsKey)
29
+ if (!request.origin.isAppOrigin(host) || !supported) {
30
+ request.deny()
31
+ return
32
+ }
33
+ if (missingPermissions(request).isEmpty()) {
34
+ request.grant(request.resources)
35
+ return
36
+ }
37
+ waiting += request
38
+ if (asking.isEmpty()) askForMissingPermissions()
39
+ }
40
+
41
+ override fun onPermissionRequestCanceled(request: PermissionRequest) {
42
+ waiting -= request
43
+ }
44
+
45
+ fun onRequestPermissionsResult(requestCode: Int) {
46
+ if (requestCode != MEDIA_PERMISSION_REQUEST) return
47
+ val answered = waiting.filter { asking.containsAll(missingPermissions(it)) }
48
+ waiting -= answered
49
+ answered.forEach(::decide)
50
+ askForMissingPermissions()
51
+ }
52
+
53
+ private fun askForMissingPermissions() {
54
+ asking = waiting.flatMap(::missingPermissions).toSet()
55
+ if (asking.isNotEmpty()) {
56
+ activity.requestPermissions(asking.toTypedArray(), MEDIA_PERMISSION_REQUEST)
57
+ }
58
+ }
59
+
60
+ private fun decide(request: PermissionRequest) {
61
+ if (missingPermissions(request).isEmpty()) request.grant(request.resources) else request.deny()
62
+ }
63
+
64
+ private fun missingPermissions(request: PermissionRequest): List<String> =
65
+ request.resources.map(RESOURCE_PERMISSIONS::getValue).filterNot(::isGranted)
66
+
67
+ private fun isGranted(permission: String): Boolean =
68
+ activity.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED
69
+ }
@@ -27,7 +27,7 @@ internal class TokamakWebViewClient(
27
27
  override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
28
28
  pluginBridge.close()
29
29
  val uri = Uri.parse(url)
30
- if (!isAppOrigin(uri)) {
30
+ if (!uri.isAppOrigin(host)) {
31
31
  view.stopLoading()
32
32
  openExternal(uri)
33
33
  }
@@ -37,7 +37,7 @@ internal class TokamakWebViewClient(
37
37
  view: WebView,
38
38
  request: WebResourceRequest,
39
39
  ): Boolean {
40
- if (!request.isForMainFrame || isAppOrigin(request.url)) return false
40
+ if (!request.isForMainFrame || request.url.isAppOrigin(host)) return false
41
41
  openExternal(request.url)
42
42
  return true
43
43
  }
@@ -67,11 +67,6 @@ internal class TokamakWebViewClient(
67
67
  request.proceed(privateKey, arrayOf(certificate))
68
68
  }
69
69
 
70
- private fun isAppOrigin(uri: Uri): Boolean =
71
- uri.scheme.equals("https", ignoreCase = true) &&
72
- uri.host?.equals(host, ignoreCase = true) == true &&
73
- (uri.port == -1 || uri.port == 443)
74
-
75
70
  private fun openExternal(uri: Uri) {
76
71
  runCatching {
77
72
  activity.startActivity(Intent(Intent.ACTION_VIEW, uri))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tokamakdev/platform-android-arm64",
3
- "version": "0.1.1-beta.44",
3
+ "version": "0.1.1-beta.45",
4
4
  "description": "The tokamak platform pack for android-arm64",
5
5
  "repository": {
6
6
  "type": "git",