expo-twa-bridge 0.1.0 → 0.1.1
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.
- package/CHANGELOG.md +14 -0
- package/README.md +83 -32
- package/android/build.gradle +2 -2
- package/android/src/main/AndroidManifest.xml +10 -2
- package/android/src/main/java/expo/modules/twabridge/ExpoTwaBridgeModule.kt +90 -13
- package/android/src/main/java/expo/modules/twabridge/TwaProviderResolution.kt +42 -15
- package/android/src/main/java/expo/modules/twabridge/TwaWebViewActivity.kt +406 -0
- package/android/src/main/res/values/styles.xml +7 -0
- package/build/ExpoTwaBridge.errors.d.ts +3 -2
- package/build/ExpoTwaBridge.errors.js +3 -2
- package/build/ExpoTwaBridge.types.d.ts +23 -4
- package/build/ExpoTwaBridgeModule.d.ts +8 -2
- package/build/ExpoTwaBridgeModule.js +9 -3
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.1 (2026-11-23)
|
|
4
|
+
|
|
5
|
+
- Add an optional fullscreen WebView fallback: `fallback: 'webview'`. It is
|
|
6
|
+
used only for browser/session/launch availability failures, loads the
|
|
7
|
+
requested HTTPS URL in a dedicated Activity, hides both Android system bars
|
|
8
|
+
in immersive mode, and handles WebView history with the Android back button.
|
|
9
|
+
Strict Digital Asset Links failures and timeouts still never fall back.
|
|
10
|
+
- The WebView fallback has no PostMessage channel. When `postMessage` is
|
|
11
|
+
requested and the WebView fallback opens, `onChannelError` reports
|
|
12
|
+
`ERR_TWA_POSTMESSAGE_UNAVAILABLE` and `postMessage()` rejects with
|
|
13
|
+
`ERR_TWA_POSTMESSAGE_NOT_READY`.
|
|
14
|
+
- `launch()` now resolves `{ mode: 'webview', verified: false }` when the
|
|
15
|
+
WebView fallback opens. `fallback: 'custom-tabs'` remains the default.
|
|
16
|
+
|
|
3
17
|
## 0.1.0 (2026-11-22)
|
|
4
18
|
|
|
5
19
|
- Initial release.
|
package/README.md
CHANGED
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
# expo-twa-bridge
|
|
2
2
|
|
|
3
3
|
A strict-first Expo native module for opening HTTPS websites in an Android
|
|
4
|
-
Trusted Web Activity (TWA) through Chrome
|
|
4
|
+
Trusted Web Activity (TWA) through Chrome.
|
|
5
5
|
|
|
6
6
|
The package launches a genuine Trusted Web Activity. With
|
|
7
7
|
`verification: 'strict'` it requires Digital Asset Links verification to pass
|
|
8
8
|
before the browser is launched; with the default `verification: 'permissive'`
|
|
9
9
|
it launches the TWA intent and lets Chrome perform its own verification, so
|
|
10
10
|
Chrome decides whether to show the URL fullscreen or with browser UI. If the
|
|
11
|
-
browser cannot host a TWA, it
|
|
12
|
-
with the browser chrome visible
|
|
13
|
-
|
|
14
|
-
activity, and a failed verification
|
|
11
|
+
browser cannot host a TWA, it can instead open the URL in Android Custom Tabs
|
|
12
|
+
with the browser chrome visible (the default), or in a dedicated fullscreen
|
|
13
|
+
WebView Activity when `fallback: 'webview'` is explicitly configured. It never
|
|
14
|
+
uses Expo WebBrowser or a regular browser activity, and a failed verification
|
|
15
|
+
is never reported as verified.
|
|
15
16
|
|
|
16
17
|
It can also open a bidirectional `postMessage` channel on the **same**
|
|
17
18
|
`CustomTabsSession` used to launch the TWA (and, on the Custom Tabs fallback,
|
|
18
|
-
the same session is used there).
|
|
19
|
+
the same session is used there). The WebView fallback has no PostMessage
|
|
20
|
+
channel.
|
|
19
21
|
|
|
20
22
|
## Installation
|
|
21
23
|
|
|
@@ -59,8 +61,9 @@ await ExpoTwaBridge.launch({
|
|
|
59
61
|
// Extra HTTPS origins that should also be shown without browser controls.
|
|
60
62
|
additionalTrustedOrigins: ['https://app.example.com'],
|
|
61
63
|
// Optional: when the browser cannot host a TWA, fall back to Android Custom
|
|
62
|
-
// Tabs (chrome visible)
|
|
63
|
-
|
|
64
|
+
// Tabs (chrome visible, the default) or to a dedicated fullscreen WebView.
|
|
65
|
+
// 'none' rejects instead. The WebView fallback has no PostMessage channel.
|
|
66
|
+
fallback: 'custom-tabs', // or 'webview'
|
|
64
67
|
// Optional: pin the browser package that hosts the TWA (e.g. Chrome).
|
|
65
68
|
// browserPackage: 'com.android.chrome',
|
|
66
69
|
// Optional: splash image shown by Chrome while the TWA loads. `image` accepts
|
|
@@ -85,7 +88,7 @@ const { mode, verified } = await ExpoTwaBridge.launch({
|
|
|
85
88
|
url: 'https://example.com',
|
|
86
89
|
verification: 'strict',
|
|
87
90
|
});
|
|
88
|
-
// mode: 'twa' | 'custom-tabs'
|
|
91
|
+
// mode: 'twa' | 'custom-tabs' | 'webview'
|
|
89
92
|
// verified: true only for a strict-mode TWA launch
|
|
90
93
|
```
|
|
91
94
|
|
|
@@ -96,7 +99,7 @@ It throws a `TwaLaunchError` with one of the following codes:
|
|
|
96
99
|
| `ERR_TWA_NO_ACTIVITY` | No foreground Android `Activity` to launch from. |
|
|
97
100
|
| `ERR_TWA_INVALID_URL` | The URL is not an absolute HTTPS URL. |
|
|
98
101
|
| `ERR_TWA_INVALID_DISPLAY_MODE` | `displayMode` is not `immersive` or `default`. |
|
|
99
|
-
| `ERR_TWA_INVALID_FALLBACK_MODE` | `fallback` is not `custom-tabs` or `none`.
|
|
102
|
+
| `ERR_TWA_INVALID_FALLBACK_MODE` | `fallback` is not `custom-tabs`, `webview`, or `none`. |
|
|
100
103
|
| `ERR_TWA_INVALID_VERIFICATION_MODE` | `verification` is not `strict` or `permissive`. |
|
|
101
104
|
| `ERR_TWA_INVALID_SPLASH_SCREEN` | `splashScreen.image` could not be resolved to an image URI. |
|
|
102
105
|
| `ERR_TWA_INVALID_ORIGIN` | An additional origin is not an absolute HTTPS origin. |
|
|
@@ -106,12 +109,12 @@ It throws a `TwaLaunchError` with one of the following codes:
|
|
|
106
109
|
| `ERR_TWA_VERIFICATION_FAILED` | `strict` mode: Digital Asset Links verification failed for the origin. |
|
|
107
110
|
| `ERR_TWA_VERIFICATION_UNAVAILABLE` | `strict` mode: the browser rejected the verification request. |
|
|
108
111
|
| `ERR_TWA_VERIFICATION_TIMEOUT` | `strict` mode: the browser did not finish verification in time. |
|
|
109
|
-
| `ERR_TWA_FALLBACK_FAILED` | The Custom Tabs fallback could not be launched.
|
|
112
|
+
| `ERR_TWA_FALLBACK_FAILED` | The Custom Tabs or WebView fallback could not be launched. |
|
|
110
113
|
| `ERR_TWA_LAUNCH_FAILED` | Session setup or the TWA launch itself failed. |
|
|
111
114
|
| `ERR_TWA_POSTMESSAGE_INVALID_ORIGIN` | A PostMessage origin is not an absolute HTTPS origin. |
|
|
112
115
|
| `ERR_TWA_POSTMESSAGE_VERIFICATION_FAILED` | `use_as_origin` Digital Asset Links check failed. |
|
|
113
|
-
| `ERR_TWA_POSTMESSAGE_UNAVAILABLE` | Browser rejected the channel
|
|
114
|
-
| `ERR_TWA_POSTMESSAGE_NOT_READY` | `postMessage()` called before the channel is ready.
|
|
116
|
+
| `ERR_TWA_POSTMESSAGE_UNAVAILABLE` | Browser rejected the channel, or the WebView fallback is active. |
|
|
117
|
+
| `ERR_TWA_POSTMESSAGE_NOT_READY` | `postMessage()` called before the channel is ready, or on the WebView fallback. |
|
|
115
118
|
| `ERR_TWA_POSTMESSAGE_FAILED` | The browser rejected the message. |
|
|
116
119
|
| `ERR_TWA_POSTMESSAGE_TIMEOUT` | The channel did not become ready in time. |
|
|
117
120
|
| `ERR_TWA_UNSUPPORTED` | Called on a platform other than Android. |
|
|
@@ -190,15 +193,36 @@ When the browser cannot host a TWA because of a browser or environment problem
|
|
|
190
193
|
`fallback` is `'custom-tabs'` (the default), the same URL is opened in Android
|
|
191
194
|
Custom Tabs with the browser chrome visible, and the promise resolves with
|
|
192
195
|
`{ mode: 'custom-tabs', verified: false }`. Set `fallback: 'none'` to reject
|
|
193
|
-
instead. PostMessage keeps working on
|
|
196
|
+
instead. PostMessage keeps working on this fallback through the same Custom Tabs
|
|
194
197
|
session.
|
|
195
198
|
|
|
199
|
+
When `fallback` is `'webview'`, those same availability failures open the URL
|
|
200
|
+
in a dedicated fullscreen Android WebView Activity instead, and the promise
|
|
201
|
+
resolves with `{ mode: 'webview', verified: false }`. The WebView fallback:
|
|
202
|
+
|
|
203
|
+
- loads the original requested HTTPS URL;
|
|
204
|
+
- honors `displayMode`: `immersive` hides both Android system bars, `default`
|
|
205
|
+
keeps them visible;
|
|
206
|
+
- keeps HTTPS navigation inside the WebView, blocks cleartext `http://`
|
|
207
|
+
navigation, cancels TLS errors, and shows an in-Activity error message when
|
|
208
|
+
the main frame fails to load;
|
|
209
|
+
- uses the Android back button for WebView history before finishing the
|
|
210
|
+
Activity; and
|
|
211
|
+
- disables file/content access and never exposes a JavaScript interface.
|
|
212
|
+
|
|
213
|
+
The WebView fallback has **no** PostMessage channel. If `postMessage` was
|
|
214
|
+
requested, `onChannelError` reports `ERR_TWA_POSTMESSAGE_UNAVAILABLE` and
|
|
215
|
+
`postMessage()` rejects with `ERR_TWA_POSTMESSAGE_NOT_READY`. Custom Tabs or a
|
|
216
|
+
TWA is required for PostMessage. Use `fallback: 'none'` to reject instead of
|
|
217
|
+
opening any fallback.
|
|
218
|
+
|
|
196
219
|
In `strict` mode, Digital Asset Links verification failures and timeouts
|
|
197
220
|
**never** fall back — the promise always rejects with
|
|
198
|
-
`ERR_TWA_VERIFICATION_FAILED` / `ERR_TWA_VERIFICATION_TIMEOUT` and no browser
|
|
199
|
-
is opened. In `permissive` mode verification cannot reject the
|
|
200
|
-
real browser/launch failures use the fallback policy.
|
|
201
|
-
WebBrowser
|
|
221
|
+
`ERR_TWA_VERIFICATION_FAILED` / `ERR_TWA_VERIFICATION_TIMEOUT` and no browser
|
|
222
|
+
UI or WebView is opened. In `permissive` mode verification cannot reject the
|
|
223
|
+
launch, so only real browser/launch failures use the fallback policy. Expo
|
|
224
|
+
WebBrowser and a regular browser activity are never used; the WebView is used
|
|
225
|
+
only when `fallback: 'webview'` is configured.
|
|
202
226
|
|
|
203
227
|
An explicit `browserPackage` is never silently replaced: if it is missing or
|
|
204
228
|
cannot host a TWA, the configured `fallback` policy applies.
|
|
@@ -213,7 +237,7 @@ const { mode, verified } = await ExpoTwaBridge.launch({
|
|
|
213
237
|
url: 'https://example.com',
|
|
214
238
|
verification: 'strict',
|
|
215
239
|
});
|
|
216
|
-
// mode: 'twa' | 'custom-tabs'
|
|
240
|
+
// mode: 'twa' | 'custom-tabs' | 'webview'
|
|
217
241
|
// verified: true only when strict verification passed and a TWA opened
|
|
218
242
|
|
|
219
243
|
if (mode === 'twa' && verified) {
|
|
@@ -223,19 +247,23 @@ if (mode === 'twa' && verified) {
|
|
|
223
247
|
// A TWA intent opened, but verification was not confirmed by this module
|
|
224
248
|
// (permissive mode). Chrome decided how to present the URL.
|
|
225
249
|
onTwaOpened();
|
|
226
|
-
} else {
|
|
250
|
+
} else if (mode === 'custom-tabs') {
|
|
227
251
|
// The Custom Tabs fallback opened the URL with the browser UI visible.
|
|
228
252
|
onFallbackOpened();
|
|
253
|
+
} else {
|
|
254
|
+
// The fullscreen WebView fallback opened the URL. It is not a verified TWA
|
|
255
|
+
// and has no PostMessage channel.
|
|
256
|
+
onWebViewFallbackOpened();
|
|
229
257
|
}
|
|
230
258
|
```
|
|
231
259
|
|
|
232
|
-
`verified` is always `false` in `permissive` mode and for `custom-tabs`
|
|
233
|
-
results, so an unverified navigation is never reported as verified.
|
|
260
|
+
`verified` is always `false` in `permissive` mode and for `custom-tabs` and
|
|
261
|
+
`webview` results, so an unverified navigation is never reported as verified.
|
|
234
262
|
|
|
235
|
-
Whenever `mode` is `'custom-tabs'`, the
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
must never be opened.
|
|
263
|
+
Whenever `mode` is `'custom-tabs'` or `'webview'`, the session is **not** a
|
|
264
|
+
verified fullscreen TWA — the fallback was used only because the browser could
|
|
265
|
+
not host a TWA. The WebView fallback is fullscreen but still unverified. Set
|
|
266
|
+
`fallback: 'none'` if a non-TWA surface must never be opened.
|
|
239
267
|
|
|
240
268
|
## Fullscreen display modes
|
|
241
269
|
|
|
@@ -245,15 +273,17 @@ and is the default. It hides both browser controls and the Android system bars.
|
|
|
245
273
|
`default` maps to `TrustedWebActivityDisplayMode.DefaultMode`, which keeps the
|
|
246
274
|
system bars visible.
|
|
247
275
|
|
|
248
|
-
|
|
249
|
-
`TrustedWebActivityIntentBuilder.setDisplayMode()
|
|
250
|
-
|
|
276
|
+
For a TWA this is the AndroidX TWA display-mode API, passed through
|
|
277
|
+
`TrustedWebActivityIntentBuilder.setDisplayMode()`. The WebView fallback honors
|
|
278
|
+
the same setting with the equivalent immersive system-bar flags in its
|
|
279
|
+
dedicated Activity.
|
|
251
280
|
|
|
252
281
|
## Splash screen
|
|
253
282
|
|
|
254
283
|
Pass `splashScreen` to show an image while Chrome loads the Trusted Web
|
|
255
284
|
Activity. This covers the moment where Chrome's own Digital Asset Links check
|
|
256
|
-
would otherwise flash the URL bar before switching to fullscreen.
|
|
285
|
+
would otherwise flash the URL bar before switching to fullscreen. The splash
|
|
286
|
+
screen is a TWA feature; the WebView fallback ignores it.
|
|
257
287
|
|
|
258
288
|
```ts
|
|
259
289
|
await ExpoTwaBridge.launch({
|
|
@@ -286,6 +316,17 @@ PostMessage runs over the existing TWA session and requires browser
|
|
|
286
316
|
verification with the `delegate_permission/common.use_as_origin` Digital Asset
|
|
287
317
|
Links relation.
|
|
288
318
|
|
|
319
|
+
### WebView fallback
|
|
320
|
+
|
|
321
|
+
PostMessage is not available with `fallback: 'webview'`. The WebView Activity
|
|
322
|
+
does not create a `CustomTabsSession`, does not inject a JavaScript interface,
|
|
323
|
+
and does not emulate the channel. When the WebView fallback is used,
|
|
324
|
+
`onChannelError` reports `ERR_TWA_POSTMESSAGE_UNAVAILABLE` and `postMessage()`
|
|
325
|
+
rejects with `ERR_TWA_POSTMESSAGE_NOT_READY`. Use `fallback: 'custom-tabs'` or a
|
|
326
|
+
TWA when PostMessage is required. A secure WebView PostMessage implementation
|
|
327
|
+
would have to be added explicitly; this package deliberately does not expose
|
|
328
|
+
one.
|
|
329
|
+
|
|
289
330
|
`additionalTrustedOrigins` controls which origins the **TWA may navigate to**
|
|
290
331
|
(each must pass `handle_all_urls` verification — this module verifies them in
|
|
291
332
|
`strict` mode and Chrome verifies them itself in `permissive` mode); it does
|
|
@@ -324,8 +365,9 @@ await ExpoTwaBridge.postMessage('PING');
|
|
|
324
365
|
```
|
|
325
366
|
|
|
326
367
|
`postMessage()` rejects with `ERR_TWA_POSTMESSAGE_NOT_READY` until
|
|
327
|
-
`addChannelReadyListener` has fired
|
|
328
|
-
can be layered on top with
|
|
368
|
+
`addChannelReadyListener` has fired, and whenever the WebView fallback opened
|
|
369
|
+
the URL. Messages are raw strings; typed envelopes can be layered on top with
|
|
370
|
+
`JSON.stringify` / `JSON.parse`.
|
|
329
371
|
|
|
330
372
|
### Native → Web and Web → Native
|
|
331
373
|
|
|
@@ -407,6 +449,9 @@ effectively exposed to the page's code.
|
|
|
407
449
|
- TWA PostMessage additionally requires a browser that supports the TWA
|
|
408
450
|
PostMessage API (for example Chrome 115+). Verifying the TWA does not imply
|
|
409
451
|
the browser also supports PostMessage.
|
|
452
|
+
- `fallback: 'webview'` requires an Android System WebView provider (normally
|
|
453
|
+
installed on Android; Chrome or another browser can provide it). The WebView
|
|
454
|
+
fallback does not require a TWA-capable browser.
|
|
410
455
|
- The page must be served over HTTPS.
|
|
411
456
|
- For a verified fullscreen TWA (and for `verification: 'strict'`), the website
|
|
412
457
|
must verify the app through
|
|
@@ -440,6 +485,9 @@ and the result is `verified: false`. PostMessage verification failures are
|
|
|
440
485
|
reported through `onChannelError` as
|
|
441
486
|
`ERR_TWA_POSTMESSAGE_VERIFICATION_FAILED` in both modes.
|
|
442
487
|
|
|
488
|
+
Strict verification failures and timeouts never use `fallback: 'custom-tabs'`
|
|
489
|
+
or `fallback: 'webview'`; only browser/session/launch availability failures do.
|
|
490
|
+
|
|
443
491
|
1. Fetch the file from the device's network:
|
|
444
492
|
`curl https://example.com/.well-known/assetlinks.json`.
|
|
445
493
|
2. Check the JSON contains the correct package, certificate SHA-256, and
|
|
@@ -481,6 +529,9 @@ Common causes of `ERR_TWA_VERIFICATION_FAILED`:
|
|
|
481
529
|
There is no automatic reconnection; relaunch the TWA after a service
|
|
482
530
|
disconnect.
|
|
483
531
|
- Messages sent before `onChannelReady` are rejected, not queued.
|
|
532
|
+
- The WebView fallback has no PostMessage channel and is never reported as a
|
|
533
|
+
verified TWA. It is used only for browser/session/launch availability
|
|
534
|
+
failures, never for verification failures.
|
|
484
535
|
- Permissive mode does not make Chrome trust an origin; Chrome still verifies
|
|
485
536
|
the URL and may keep browser UI. Use `verification: 'strict'` when a verified
|
|
486
537
|
fullscreen TWA is mandatory.
|
package/android/build.gradle
CHANGED
|
@@ -4,13 +4,13 @@ plugins {
|
|
|
4
4
|
}
|
|
5
5
|
|
|
6
6
|
group = 'expo.modules.twabridge'
|
|
7
|
-
version = '0.1.
|
|
7
|
+
version = '0.1.1'
|
|
8
8
|
|
|
9
9
|
android {
|
|
10
10
|
namespace "expo.modules.twabridge"
|
|
11
11
|
defaultConfig {
|
|
12
12
|
versionCode 1
|
|
13
|
-
versionName "0.1.
|
|
13
|
+
versionName "0.1.1"
|
|
14
14
|
}
|
|
15
15
|
lintOptions {
|
|
16
16
|
abortOnError false
|
|
@@ -1,2 +1,10 @@
|
|
|
1
|
-
<manifest>
|
|
2
|
-
|
|
1
|
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
|
2
|
+
<application>
|
|
3
|
+
<activity
|
|
4
|
+
android:name="expo.modules.twabridge.TwaWebViewActivity"
|
|
5
|
+
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout|uiMode"
|
|
6
|
+
android:exported="false"
|
|
7
|
+
android:theme="@style/Theme.ExpoTwaBridge.WebView"
|
|
8
|
+
android:windowSoftInputMode="adjustResize" />
|
|
9
|
+
</application>
|
|
10
|
+
</manifest>
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
package expo.modules.twabridge
|
|
2
2
|
|
|
3
3
|
import android.app.Activity
|
|
4
|
+
import android.content.Intent
|
|
4
5
|
import android.graphics.Color
|
|
5
6
|
import android.net.Uri
|
|
6
7
|
import android.util.Log
|
|
@@ -66,12 +67,13 @@ class ExpoTwaBridgeModule : Module() {
|
|
|
66
67
|
)
|
|
67
68
|
return@AsyncFunction
|
|
68
69
|
}
|
|
70
|
+
val immersive = mode is TrustedWebActivityDisplayMode.ImmersiveMode
|
|
69
71
|
|
|
70
72
|
val fallbackPolicy = FallbackPolicy.fromValue(fallback)
|
|
71
73
|
if (fallbackPolicy == null) {
|
|
72
74
|
promise.reject(
|
|
73
75
|
"ERR_TWA_INVALID_FALLBACK_MODE",
|
|
74
|
-
"fallback must be either \"custom-tabs\" or \"none\".",
|
|
76
|
+
"fallback must be either \"custom-tabs\", \"webview\" or \"none\".",
|
|
75
77
|
null
|
|
76
78
|
)
|
|
77
79
|
return@AsyncFunction
|
|
@@ -174,11 +176,31 @@ class ExpoTwaBridgeModule : Module() {
|
|
|
174
176
|
fallbackPackage = providerPackage,
|
|
175
177
|
postMessageConfig = postMessageConfig,
|
|
176
178
|
promise = promise,
|
|
177
|
-
allowFallbackFromTwa = false
|
|
179
|
+
allowFallbackFromTwa = false,
|
|
180
|
+
immersive = immersive
|
|
178
181
|
)
|
|
179
182
|
)
|
|
180
183
|
}
|
|
181
184
|
|
|
185
|
+
LaunchStrategy.WEBVIEW -> {
|
|
186
|
+
Log.i(TAG, "Launching the WebView fallback: url=$url")
|
|
187
|
+
|
|
188
|
+
launcher = null
|
|
189
|
+
if (!launchWebViewFallback(activity, uri, immersive)) {
|
|
190
|
+
promise.reject(
|
|
191
|
+
"ERR_TWA_FALLBACK_FAILED",
|
|
192
|
+
"The WebView fallback could not be launched.",
|
|
193
|
+
null
|
|
194
|
+
)
|
|
195
|
+
return@runOnUiThread
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (postMessageConfig != null) {
|
|
199
|
+
emitWebViewPostMessageUnavailable()
|
|
200
|
+
}
|
|
201
|
+
promise.resolve(LaunchSurface.WEBVIEW.mode)
|
|
202
|
+
}
|
|
203
|
+
|
|
182
204
|
LaunchStrategy.TWA -> {
|
|
183
205
|
if (providerPackage == null) {
|
|
184
206
|
promise.reject(
|
|
@@ -210,7 +232,8 @@ class ExpoTwaBridgeModule : Module() {
|
|
|
210
232
|
fallbackPackage = providerPackage,
|
|
211
233
|
postMessageConfig = postMessageConfig,
|
|
212
234
|
promise = promise,
|
|
213
|
-
allowFallbackFromTwa = true
|
|
235
|
+
allowFallbackFromTwa = true,
|
|
236
|
+
immersive = immersive
|
|
214
237
|
)
|
|
215
238
|
)
|
|
216
239
|
}
|
|
@@ -264,8 +287,8 @@ class ExpoTwaBridgeModule : Module() {
|
|
|
264
287
|
|
|
265
288
|
/**
|
|
266
289
|
* Builds the launcher callback for a surface. A fallback-eligible TWA
|
|
267
|
-
* failure is retried once with
|
|
268
|
-
*
|
|
290
|
+
* failure is retried once with the configured fallback surface; verification
|
|
291
|
+
* failures are never retried.
|
|
269
292
|
*/
|
|
270
293
|
private fun createCallback(
|
|
271
294
|
activity: Activity,
|
|
@@ -275,7 +298,8 @@ class ExpoTwaBridgeModule : Module() {
|
|
|
275
298
|
fallbackPackage: String?,
|
|
276
299
|
postMessageConfig: PostMessageConfig?,
|
|
277
300
|
promise: Promise,
|
|
278
|
-
allowFallbackFromTwa: Boolean
|
|
301
|
+
allowFallbackFromTwa: Boolean,
|
|
302
|
+
immersive: Boolean
|
|
279
303
|
): StrictTwaLauncher.Callback =
|
|
280
304
|
object : StrictTwaLauncher.Callback {
|
|
281
305
|
override fun onLaunched() {
|
|
@@ -288,22 +312,46 @@ class ExpoTwaBridgeModule : Module() {
|
|
|
288
312
|
cause: Throwable?,
|
|
289
313
|
fallbackEligible: Boolean
|
|
290
314
|
) {
|
|
291
|
-
val
|
|
292
|
-
fallbackEligible
|
|
293
|
-
|
|
294
|
-
|
|
315
|
+
val strategy = if (allowFallbackFromTwa) {
|
|
316
|
+
resolveFallbackStrategy(fallback, fallbackEligible)
|
|
317
|
+
} else {
|
|
318
|
+
null
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
val canFallbackToCustomTabs =
|
|
322
|
+
strategy == LaunchStrategy.CUSTOM_TABS && fallbackPackage != null
|
|
323
|
+
val canFallbackToWebView = strategy == LaunchStrategy.WEBVIEW
|
|
295
324
|
|
|
296
|
-
if (!
|
|
325
|
+
if (!canFallbackToCustomTabs && !canFallbackToWebView) {
|
|
297
326
|
promise.reject(code, message, cause)
|
|
298
327
|
return
|
|
299
328
|
}
|
|
300
329
|
|
|
330
|
+
if (canFallbackToWebView) {
|
|
331
|
+
Log.i(TAG, "$code: $message — falling back to the WebView.")
|
|
332
|
+
launcher?.destroy()
|
|
333
|
+
launcher = null
|
|
334
|
+
if (!launchWebViewFallback(activity, uri, immersive)) {
|
|
335
|
+
promise.reject(
|
|
336
|
+
"ERR_TWA_FALLBACK_FAILED",
|
|
337
|
+
"The WebView fallback could not be launched.",
|
|
338
|
+
cause
|
|
339
|
+
)
|
|
340
|
+
return
|
|
341
|
+
}
|
|
342
|
+
if (postMessageConfig != null) {
|
|
343
|
+
emitWebViewPostMessageUnavailable()
|
|
344
|
+
}
|
|
345
|
+
promise.resolve(LaunchSurface.WEBVIEW.mode)
|
|
346
|
+
return
|
|
347
|
+
}
|
|
348
|
+
|
|
301
349
|
Log.i(TAG, "$code: $message — falling back to Android Custom Tabs.")
|
|
302
350
|
|
|
303
351
|
val fallbackLauncher = StrictTwaLauncher(activity)
|
|
304
352
|
launcher = fallbackLauncher
|
|
305
353
|
fallbackLauncher.launchCustomTabs(
|
|
306
|
-
fallbackPackage
|
|
354
|
+
fallbackPackage!!,
|
|
307
355
|
uri,
|
|
308
356
|
postMessageConfig?.sourceOrigin,
|
|
309
357
|
postMessageConfig?.targetOrigin,
|
|
@@ -315,7 +363,8 @@ class ExpoTwaBridgeModule : Module() {
|
|
|
315
363
|
fallbackPackage = fallbackPackage,
|
|
316
364
|
postMessageConfig = postMessageConfig,
|
|
317
365
|
promise = promise,
|
|
318
|
-
allowFallbackFromTwa = false
|
|
366
|
+
allowFallbackFromTwa = false,
|
|
367
|
+
immersive = immersive
|
|
319
368
|
)
|
|
320
369
|
)
|
|
321
370
|
}
|
|
@@ -340,6 +389,34 @@ class ExpoTwaBridgeModule : Module() {
|
|
|
340
389
|
}
|
|
341
390
|
}
|
|
342
391
|
|
|
392
|
+
private fun launchWebViewFallback(
|
|
393
|
+
activity: Activity,
|
|
394
|
+
uri: Uri,
|
|
395
|
+
immersive: Boolean
|
|
396
|
+
): Boolean =
|
|
397
|
+
try {
|
|
398
|
+
val intent = Intent(activity, TwaWebViewActivity::class.java).apply {
|
|
399
|
+
putExtra(TwaWebViewActivity.EXTRA_URL, uri.toString())
|
|
400
|
+
putExtra(TwaWebViewActivity.EXTRA_IMMERSIVE, immersive)
|
|
401
|
+
}
|
|
402
|
+
activity.startActivity(intent)
|
|
403
|
+
true
|
|
404
|
+
} catch (error: Exception) {
|
|
405
|
+
Log.e(TAG, "The WebView fallback could not be launched.", error)
|
|
406
|
+
false
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
private fun emitWebViewPostMessageUnavailable() {
|
|
410
|
+
sendEvent(
|
|
411
|
+
"onChannelError",
|
|
412
|
+
mapOf(
|
|
413
|
+
"code" to "ERR_TWA_POSTMESSAGE_UNAVAILABLE",
|
|
414
|
+
"message" to "PostMessage is not available in the WebView fallback. " +
|
|
415
|
+
"Use fallback: \"custom-tabs\" or launch a Trusted Web Activity to use PostMessage."
|
|
416
|
+
)
|
|
417
|
+
)
|
|
418
|
+
}
|
|
419
|
+
|
|
343
420
|
private fun parseDisplayMode(value: String): TrustedWebActivityDisplayMode? =
|
|
344
421
|
when (value.lowercase()) {
|
|
345
422
|
DISPLAY_MODE_IMMERSIVE -> TrustedWebActivityDisplayMode.ImmersiveMode(
|
|
@@ -13,6 +13,7 @@ internal const val TWA_SERVICE_CATEGORY =
|
|
|
13
13
|
|
|
14
14
|
internal enum class FallbackPolicy(val value: String) {
|
|
15
15
|
CUSTOM_TABS("custom-tabs"),
|
|
16
|
+
WEBVIEW("webview"),
|
|
16
17
|
NONE("none");
|
|
17
18
|
|
|
18
19
|
companion object {
|
|
@@ -23,7 +24,8 @@ internal enum class FallbackPolicy(val value: String) {
|
|
|
23
24
|
|
|
24
25
|
internal enum class LaunchSurface(val mode: String) {
|
|
25
26
|
TWA("twa"),
|
|
26
|
-
CUSTOM_TABS("custom-tabs")
|
|
27
|
+
CUSTOM_TABS("custom-tabs"),
|
|
28
|
+
WEBVIEW("webview")
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
/**
|
|
@@ -37,7 +39,7 @@ internal data class ProviderCapabilities(
|
|
|
37
39
|
val isExplicit: Boolean
|
|
38
40
|
)
|
|
39
41
|
|
|
40
|
-
internal enum class LaunchStrategy { TWA, CUSTOM_TABS, ERROR }
|
|
42
|
+
internal enum class LaunchStrategy { TWA, CUSTOM_TABS, WEBVIEW, ERROR }
|
|
41
43
|
|
|
42
44
|
internal data class LaunchDecision(
|
|
43
45
|
val strategy: LaunchStrategy,
|
|
@@ -59,20 +61,26 @@ internal fun decideLaunchStrategy(
|
|
|
59
61
|
): LaunchDecision {
|
|
60
62
|
if (capabilities.isExplicit) {
|
|
61
63
|
if (!capabilities.installed) {
|
|
62
|
-
return
|
|
63
|
-
LaunchStrategy.
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
"
|
|
67
|
-
|
|
64
|
+
return when (fallback) {
|
|
65
|
+
FallbackPolicy.WEBVIEW -> LaunchDecision(LaunchStrategy.WEBVIEW)
|
|
66
|
+
else -> LaunchDecision(
|
|
67
|
+
LaunchStrategy.ERROR,
|
|
68
|
+
"ERR_TWA_BROWSER_UNAVAILABLE",
|
|
69
|
+
"The requested browser package \"${capabilities.packageName}\" is not installed. " +
|
|
70
|
+
"No other browser is selected automatically when browserPackage is set."
|
|
71
|
+
)
|
|
72
|
+
}
|
|
68
73
|
}
|
|
69
74
|
if (!capabilities.supportsCustomTabs) {
|
|
70
|
-
return
|
|
71
|
-
LaunchStrategy.
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
"
|
|
75
|
-
|
|
75
|
+
return when (fallback) {
|
|
76
|
+
FallbackPolicy.WEBVIEW -> LaunchDecision(LaunchStrategy.WEBVIEW)
|
|
77
|
+
else -> LaunchDecision(
|
|
78
|
+
LaunchStrategy.ERROR,
|
|
79
|
+
"ERR_TWA_BROWSER_UNAVAILABLE",
|
|
80
|
+
"The requested browser package \"${capabilities.packageName}\" does not expose an " +
|
|
81
|
+
"Android Custom Tabs service, so it cannot host a Trusted Web Activity."
|
|
82
|
+
)
|
|
83
|
+
}
|
|
76
84
|
}
|
|
77
85
|
}
|
|
78
86
|
|
|
@@ -83,12 +91,13 @@ internal fun decideLaunchStrategy(
|
|
|
83
91
|
if (capabilities.supportsCustomTabs && capabilities.packageName != null) {
|
|
84
92
|
return when (fallback) {
|
|
85
93
|
FallbackPolicy.CUSTOM_TABS -> LaunchDecision(LaunchStrategy.CUSTOM_TABS)
|
|
94
|
+
FallbackPolicy.WEBVIEW -> LaunchDecision(LaunchStrategy.WEBVIEW)
|
|
86
95
|
FallbackPolicy.NONE -> LaunchDecision(
|
|
87
96
|
LaunchStrategy.ERROR,
|
|
88
97
|
"ERR_TWA_NO_BROWSER",
|
|
89
98
|
"The available browser \"${capabilities.packageName}\" does not support Trusted Web " +
|
|
90
99
|
"Activities. Install a TWA-capable browser (for example Chrome) or use " +
|
|
91
|
-
"fallback: \"custom-tabs\"."
|
|
100
|
+
"fallback: \"custom-tabs\" or \"webview\"."
|
|
92
101
|
)
|
|
93
102
|
}
|
|
94
103
|
}
|
|
@@ -100,6 +109,7 @@ internal fun decideLaunchStrategy(
|
|
|
100
109
|
"No browser supporting Android Custom Tabs is installed, so neither a Trusted Web " +
|
|
101
110
|
"Activity nor the Custom Tabs fallback can be launched."
|
|
102
111
|
)
|
|
112
|
+
FallbackPolicy.WEBVIEW -> LaunchDecision(LaunchStrategy.WEBVIEW)
|
|
103
113
|
FallbackPolicy.NONE -> LaunchDecision(
|
|
104
114
|
LaunchStrategy.ERROR,
|
|
105
115
|
"ERR_TWA_NO_BROWSER",
|
|
@@ -109,6 +119,23 @@ internal fun decideLaunchStrategy(
|
|
|
109
119
|
}
|
|
110
120
|
}
|
|
111
121
|
|
|
122
|
+
/**
|
|
123
|
+
* Chooses the retry surface for a fallback-eligible TWA launch failure.
|
|
124
|
+
*
|
|
125
|
+
* Verification and configuration failures are never fallback-eligible, so this
|
|
126
|
+
* returns null for them regardless of the configured policy. `none` never
|
|
127
|
+
* falls back.
|
|
128
|
+
*/
|
|
129
|
+
internal fun resolveFallbackStrategy(
|
|
130
|
+
fallback: FallbackPolicy,
|
|
131
|
+
fallbackEligible: Boolean
|
|
132
|
+
): LaunchStrategy? = when {
|
|
133
|
+
!fallbackEligible -> null
|
|
134
|
+
fallback == FallbackPolicy.CUSTOM_TABS -> LaunchStrategy.CUSTOM_TABS
|
|
135
|
+
fallback == FallbackPolicy.WEBVIEW -> LaunchStrategy.WEBVIEW
|
|
136
|
+
else -> null
|
|
137
|
+
}
|
|
138
|
+
|
|
112
139
|
internal object TwaProviderResolver {
|
|
113
140
|
|
|
114
141
|
fun resolve(packageManager: PackageManager, requestedPackage: String?): ProviderCapabilities {
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
package expo.modules.twabridge
|
|
2
|
+
|
|
3
|
+
import android.annotation.SuppressLint
|
|
4
|
+
import android.app.Activity
|
|
5
|
+
import android.content.Intent
|
|
6
|
+
import android.content.res.Configuration
|
|
7
|
+
import android.graphics.Bitmap
|
|
8
|
+
import android.graphics.Color
|
|
9
|
+
import android.net.Uri
|
|
10
|
+
import android.net.http.SslError
|
|
11
|
+
import android.os.Build
|
|
12
|
+
import android.os.Bundle
|
|
13
|
+
import android.util.Log
|
|
14
|
+
import android.view.View
|
|
15
|
+
import android.view.ViewGroup
|
|
16
|
+
import android.view.WindowManager
|
|
17
|
+
import android.webkit.RenderProcessGoneDetail
|
|
18
|
+
import android.webkit.SslErrorHandler
|
|
19
|
+
import android.webkit.WebResourceError
|
|
20
|
+
import android.webkit.WebResourceRequest
|
|
21
|
+
import android.webkit.WebResourceResponse
|
|
22
|
+
import android.webkit.WebSettings
|
|
23
|
+
import android.webkit.WebView
|
|
24
|
+
import android.webkit.WebViewClient
|
|
25
|
+
import android.widget.FrameLayout
|
|
26
|
+
import android.widget.TextView
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Dedicated WebView surface used only by the explicit `fallback: 'webview'`
|
|
30
|
+
* policy. It loads the same HTTPS URL as the TWA, hides the Android system
|
|
31
|
+
* bars in immersive mode, handles Android back navigation through the WebView
|
|
32
|
+
* history, and never exposes a JavaScript interface.
|
|
33
|
+
*/
|
|
34
|
+
class TwaWebViewActivity : Activity() {
|
|
35
|
+
|
|
36
|
+
companion object {
|
|
37
|
+
const val EXTRA_URL = "expo.modules.twabridge.TwaWebViewActivity.EXTRA_URL"
|
|
38
|
+
const val EXTRA_IMMERSIVE =
|
|
39
|
+
"expo.modules.twabridge.TwaWebViewActivity.EXTRA_IMMERSIVE"
|
|
40
|
+
|
|
41
|
+
private const val STATE_URL = "expo.modules.twabridge.TwaWebViewActivity.STATE_URL"
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private var webView: WebView? = null
|
|
45
|
+
private var errorView: TextView? = null
|
|
46
|
+
private var immersive = true
|
|
47
|
+
|
|
48
|
+
override fun onCreate(savedInstanceState: Bundle?) {
|
|
49
|
+
super.onCreate(savedInstanceState)
|
|
50
|
+
|
|
51
|
+
val requestedUrl = intent.getStringExtra(EXTRA_URL)
|
|
52
|
+
if (requestedUrl == null || parseHttpsUrl(requestedUrl) == null) {
|
|
53
|
+
Log.w(TAG, "The WebView fallback received a non-HTTPS URL; finishing.")
|
|
54
|
+
finish()
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
immersive = intent.getBooleanExtra(EXTRA_IMMERSIVE, true)
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
setupContent(savedInstanceState, requestedUrl)
|
|
62
|
+
} catch (error: Exception) {
|
|
63
|
+
Log.e(TAG, "The WebView fallback could not be initialized.", error)
|
|
64
|
+
showFatalError(
|
|
65
|
+
"WebView unavailable",
|
|
66
|
+
error.message ?: "The Android WebView could not be initialized."
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private fun setupContent(savedInstanceState: Bundle?, requestedUrl: String) {
|
|
72
|
+
val root = FrameLayout(this)
|
|
73
|
+
val error = TextView(this).apply {
|
|
74
|
+
setBackgroundColor(Color.WHITE)
|
|
75
|
+
setTextColor(Color.DKGRAY)
|
|
76
|
+
textSize = 16f
|
|
77
|
+
gravity = android.view.Gravity.CENTER
|
|
78
|
+
setPadding(48, 48, 48, 48)
|
|
79
|
+
visibility = View.GONE
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
val web = WebView(this)
|
|
83
|
+
webView = web
|
|
84
|
+
configure(web)
|
|
85
|
+
|
|
86
|
+
root.addView(
|
|
87
|
+
web,
|
|
88
|
+
FrameLayout.LayoutParams(
|
|
89
|
+
FrameLayout.LayoutParams.MATCH_PARENT,
|
|
90
|
+
FrameLayout.LayoutParams.MATCH_PARENT
|
|
91
|
+
)
|
|
92
|
+
)
|
|
93
|
+
root.addView(
|
|
94
|
+
error,
|
|
95
|
+
FrameLayout.LayoutParams(
|
|
96
|
+
FrameLayout.LayoutParams.MATCH_PARENT,
|
|
97
|
+
FrameLayout.LayoutParams.MATCH_PARENT
|
|
98
|
+
)
|
|
99
|
+
)
|
|
100
|
+
setContentView(root)
|
|
101
|
+
errorView = error
|
|
102
|
+
SystemBarsCompat.apply(this, immersive)
|
|
103
|
+
|
|
104
|
+
BackCompat.register(this)
|
|
105
|
+
|
|
106
|
+
val restored = savedInstanceState?.let {
|
|
107
|
+
try {
|
|
108
|
+
web.restoreState(it)
|
|
109
|
+
} catch (error: Exception) {
|
|
110
|
+
Log.w(TAG, "Could not restore the WebView state.", error)
|
|
111
|
+
null
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (restored == null) {
|
|
115
|
+
val restoreUrl = savedInstanceState?.getString(STATE_URL) ?: requestedUrl
|
|
116
|
+
web.loadUrl(restoreUrl)
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
@SuppressLint("SetJavaScriptEnabled")
|
|
121
|
+
@Suppress("DEPRECATION")
|
|
122
|
+
private fun configure(web: WebView) {
|
|
123
|
+
web.settings.apply {
|
|
124
|
+
javaScriptEnabled = true
|
|
125
|
+
domStorageEnabled = true
|
|
126
|
+
databaseEnabled = true
|
|
127
|
+
allowFileAccess = false
|
|
128
|
+
allowContentAccess = false
|
|
129
|
+
allowFileAccessFromFileURLs = false
|
|
130
|
+
allowUniversalAccessFromFileURLs = false
|
|
131
|
+
javaScriptCanOpenWindowsAutomatically = false
|
|
132
|
+
setSupportMultipleWindows(false)
|
|
133
|
+
mediaPlaybackRequiresUserGesture = true
|
|
134
|
+
mixedContentMode = WebSettings.MIXED_CONTENT_NEVER_ALLOW
|
|
135
|
+
setGeolocationEnabled(false)
|
|
136
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
137
|
+
safeBrowsingEnabled = true
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
web.webViewClient = BridgeWebViewClient()
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
@Suppress("DEPRECATION", "OVERRIDE_DEPRECATION")
|
|
144
|
+
override fun onBackPressed() {
|
|
145
|
+
handleBack()
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private fun handleBack() {
|
|
149
|
+
val web = webView
|
|
150
|
+
if (web != null && web.canGoBack()) {
|
|
151
|
+
web.goBack()
|
|
152
|
+
} else {
|
|
153
|
+
finish()
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
override fun onWindowFocusChanged(hasFocus: Boolean) {
|
|
158
|
+
super.onWindowFocusChanged(hasFocus)
|
|
159
|
+
if (hasFocus && immersive) {
|
|
160
|
+
SystemBarsCompat.apply(this, immersive)
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
override fun onConfigurationChanged(newConfig: Configuration) {
|
|
165
|
+
super.onConfigurationChanged(newConfig)
|
|
166
|
+
if (immersive) {
|
|
167
|
+
SystemBarsCompat.apply(this, immersive)
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
override fun onPause() {
|
|
172
|
+
webView?.onPause()
|
|
173
|
+
super.onPause()
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
override fun onResume() {
|
|
177
|
+
super.onResume()
|
|
178
|
+
webView?.onResume()
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
override fun onSaveInstanceState(outState: Bundle) {
|
|
182
|
+
super.onSaveInstanceState(outState)
|
|
183
|
+
webView?.saveState(outState)
|
|
184
|
+
webView?.url?.let { outState.putString(STATE_URL, it) }
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
override fun onDestroy() {
|
|
188
|
+
webView?.let { web ->
|
|
189
|
+
(web.parent as? ViewGroup)?.removeView(web)
|
|
190
|
+
web.stopLoading()
|
|
191
|
+
web.loadUrl("about:blank")
|
|
192
|
+
web.clearHistory()
|
|
193
|
+
web.removeAllViews()
|
|
194
|
+
web.destroy()
|
|
195
|
+
}
|
|
196
|
+
webView = null
|
|
197
|
+
errorView = null
|
|
198
|
+
super.onDestroy()
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
private fun showError(title: String, detail: String) {
|
|
202
|
+
val view = errorView ?: return
|
|
203
|
+
view.text = "$title\n\n$detail"
|
|
204
|
+
view.visibility = View.VISIBLE
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
private fun hideError() {
|
|
208
|
+
errorView?.visibility = View.GONE
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
private fun showFatalError(title: String, detail: String) {
|
|
212
|
+
val view = TextView(this).apply {
|
|
213
|
+
setBackgroundColor(Color.WHITE)
|
|
214
|
+
setTextColor(Color.DKGRAY)
|
|
215
|
+
textSize = 16f
|
|
216
|
+
gravity = android.view.Gravity.CENTER
|
|
217
|
+
setPadding(48, 48, 48, 48)
|
|
218
|
+
text = "$title\n\n$detail"
|
|
219
|
+
}
|
|
220
|
+
setContentView(view)
|
|
221
|
+
SystemBarsCompat.apply(this, immersive)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
private inner class BridgeWebViewClient : WebViewClient() {
|
|
225
|
+
override fun shouldOverrideUrlLoading(
|
|
226
|
+
view: WebView,
|
|
227
|
+
request: WebResourceRequest
|
|
228
|
+
): Boolean = handleNavigation(request.url)
|
|
229
|
+
|
|
230
|
+
@Deprecated("Deprecated in Java")
|
|
231
|
+
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean =
|
|
232
|
+
handleNavigation(Uri.parse(url))
|
|
233
|
+
|
|
234
|
+
override fun onPageStarted(
|
|
235
|
+
view: WebView,
|
|
236
|
+
url: String?,
|
|
237
|
+
favicon: Bitmap?
|
|
238
|
+
) {
|
|
239
|
+
hideError()
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
override fun onReceivedError(
|
|
243
|
+
view: WebView,
|
|
244
|
+
request: WebResourceRequest,
|
|
245
|
+
error: WebResourceError
|
|
246
|
+
) {
|
|
247
|
+
if (request.isForMainFrame) {
|
|
248
|
+
showError(
|
|
249
|
+
"Page could not be loaded",
|
|
250
|
+
error.description?.toString() ?: "The WebView reported a loading error."
|
|
251
|
+
)
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
override fun onReceivedHttpError(
|
|
256
|
+
view: WebView,
|
|
257
|
+
request: WebResourceRequest,
|
|
258
|
+
errorResponse: WebResourceResponse
|
|
259
|
+
) {
|
|
260
|
+
if (request.isForMainFrame) {
|
|
261
|
+
showError(
|
|
262
|
+
"Page could not be loaded",
|
|
263
|
+
"The server returned HTTP ${errorResponse.statusCode}."
|
|
264
|
+
)
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
override fun onReceivedSslError(
|
|
269
|
+
view: WebView,
|
|
270
|
+
handler: SslErrorHandler,
|
|
271
|
+
error: SslError
|
|
272
|
+
) {
|
|
273
|
+
handler.cancel()
|
|
274
|
+
showError(
|
|
275
|
+
"Secure connection failed",
|
|
276
|
+
"The WebView could not verify the site's TLS certificate."
|
|
277
|
+
)
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
override fun onRenderProcessGone(
|
|
281
|
+
view: WebView,
|
|
282
|
+
detail: RenderProcessGoneDetail
|
|
283
|
+
): Boolean {
|
|
284
|
+
Log.e(
|
|
285
|
+
TAG,
|
|
286
|
+
"The WebView renderer process stopped (didCrash=${detail.didCrash()})."
|
|
287
|
+
)
|
|
288
|
+
(view.parent as? ViewGroup)?.removeView(view)
|
|
289
|
+
view.destroy()
|
|
290
|
+
if (webView === view) {
|
|
291
|
+
webView = null
|
|
292
|
+
}
|
|
293
|
+
showError(
|
|
294
|
+
"Page could not be displayed",
|
|
295
|
+
"The WebView renderer stopped unexpectedly. Press back to leave."
|
|
296
|
+
)
|
|
297
|
+
return true
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Keeps HTTPS navigation inside the WebView, blocks insecure or local
|
|
303
|
+
* schemes, and hands other schemes to the system.
|
|
304
|
+
*/
|
|
305
|
+
private fun handleNavigation(uri: Uri): Boolean =
|
|
306
|
+
when (uri.scheme?.lowercase()) {
|
|
307
|
+
"https", "blob" -> false
|
|
308
|
+
"http" -> {
|
|
309
|
+
showError(
|
|
310
|
+
"Insecure navigation blocked",
|
|
311
|
+
"The WebView fallback only opens HTTPS URLs."
|
|
312
|
+
)
|
|
313
|
+
true
|
|
314
|
+
}
|
|
315
|
+
"file", "content", "javascript", "data" -> true
|
|
316
|
+
else -> {
|
|
317
|
+
try {
|
|
318
|
+
startActivity(Intent(Intent.ACTION_VIEW, uri))
|
|
319
|
+
} catch (error: Exception) {
|
|
320
|
+
Log.w(TAG, "Could not open external URI: $uri", error)
|
|
321
|
+
}
|
|
322
|
+
true
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Keeps API 30+ window-inset classes out of the main Activity so they are
|
|
328
|
+
* only loaded on devices that provide them.
|
|
329
|
+
*/
|
|
330
|
+
private object SystemBarsCompat {
|
|
331
|
+
fun apply(activity: TwaWebViewActivity, immersive: Boolean) {
|
|
332
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
|
333
|
+
Api30.apply(activity, immersive)
|
|
334
|
+
} else {
|
|
335
|
+
Legacy.apply(activity, immersive)
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
if (immersive && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
|
339
|
+
activity.window.attributes = activity.window.attributes.apply {
|
|
340
|
+
layoutInDisplayCutoutMode =
|
|
341
|
+
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
private object Api30 {
|
|
347
|
+
@Suppress("DEPRECATION")
|
|
348
|
+
fun apply(activity: TwaWebViewActivity, immersive: Boolean) {
|
|
349
|
+
val window = activity.window
|
|
350
|
+
window.setDecorFitsSystemWindows(!immersive)
|
|
351
|
+
|
|
352
|
+
val bars = android.view.WindowInsets.Type.statusBars() or
|
|
353
|
+
android.view.WindowInsets.Type.navigationBars()
|
|
354
|
+
// Read the controller from the decor view, which also forces the decor
|
|
355
|
+
// to exist before PhoneWindow.getInsetsController() is reached.
|
|
356
|
+
window.decorView.windowInsetsController?.apply {
|
|
357
|
+
if (immersive) {
|
|
358
|
+
hide(bars)
|
|
359
|
+
systemBarsBehavior =
|
|
360
|
+
android.view.WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
|
|
361
|
+
} else {
|
|
362
|
+
show(bars)
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
private object Legacy {
|
|
369
|
+
@Suppress("DEPRECATION")
|
|
370
|
+
fun apply(activity: TwaWebViewActivity, immersive: Boolean) {
|
|
371
|
+
activity.window.decorView.systemUiVisibility = if (immersive) {
|
|
372
|
+
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
|
|
373
|
+
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
|
|
374
|
+
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
|
|
375
|
+
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
|
|
376
|
+
View.SYSTEM_UI_FLAG_FULLSCREEN or
|
|
377
|
+
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
|
|
378
|
+
} else {
|
|
379
|
+
View.SYSTEM_UI_FLAG_VISIBLE
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Registers the API 33+ predictive-back callback without loading that class
|
|
387
|
+
* on older devices. `onBackPressed()` remains the fallback path.
|
|
388
|
+
*/
|
|
389
|
+
private object BackCompat {
|
|
390
|
+
fun register(activity: TwaWebViewActivity) {
|
|
391
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
392
|
+
Api33.register(activity)
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
private object Api33 {
|
|
397
|
+
fun register(activity: TwaWebViewActivity) {
|
|
398
|
+
activity.onBackInvokedDispatcher.registerOnBackInvokedCallback(
|
|
399
|
+
android.window.OnBackInvokedDispatcher.PRIORITY_DEFAULT
|
|
400
|
+
) {
|
|
401
|
+
activity.handleBack()
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="utf-8"?>
|
|
2
|
+
<resources>
|
|
3
|
+
<style name="Theme.ExpoTwaBridge.WebView" parent="@android:style/Theme.Material.NoActionBar">
|
|
4
|
+
<item name="android:windowNoTitle">true</item>
|
|
5
|
+
<item name="android:windowBackground">@android:color/white</item>
|
|
6
|
+
</style>
|
|
7
|
+
</resources>
|
|
@@ -9,8 +9,9 @@ export type TwaErrorCode = 'ERR_TWA_NO_ACTIVITY' | 'ERR_TWA_INVALID_URL' | 'ERR_
|
|
|
9
9
|
*
|
|
10
10
|
* `ERR_TWA_NO_BROWSER`, `ERR_TWA_BROWSER_UNAVAILABLE`,
|
|
11
11
|
* `ERR_TWA_CUSTOM_TABS_UNAVAILABLE` and `ERR_TWA_FALLBACK_FAILED` describe
|
|
12
|
-
* browser/environment problems; with `fallback: 'custom-tabs'`
|
|
13
|
-
* retried
|
|
12
|
+
* browser/environment problems; with `fallback: 'custom-tabs'` or
|
|
13
|
+
* `fallback: 'webview'` the launch is retried on that surface before these
|
|
14
|
+
* are reported. Verification failures and timeouts never fall back.
|
|
14
15
|
*/
|
|
15
16
|
export declare class TwaLaunchError extends Error {
|
|
16
17
|
readonly code: TwaErrorCode;
|
|
@@ -12,8 +12,9 @@ exports.toTwaLaunchError = toTwaLaunchError;
|
|
|
12
12
|
*
|
|
13
13
|
* `ERR_TWA_NO_BROWSER`, `ERR_TWA_BROWSER_UNAVAILABLE`,
|
|
14
14
|
* `ERR_TWA_CUSTOM_TABS_UNAVAILABLE` and `ERR_TWA_FALLBACK_FAILED` describe
|
|
15
|
-
* browser/environment problems; with `fallback: 'custom-tabs'`
|
|
16
|
-
* retried
|
|
15
|
+
* browser/environment problems; with `fallback: 'custom-tabs'` or
|
|
16
|
+
* `fallback: 'webview'` the launch is retried on that surface before these
|
|
17
|
+
* are reported. Verification failures and timeouts never fall back.
|
|
17
18
|
*/
|
|
18
19
|
class TwaLaunchError extends Error {
|
|
19
20
|
code;
|
|
@@ -2,8 +2,17 @@ export type TwaDisplayMode = 'default' | 'immersive';
|
|
|
2
2
|
/**
|
|
3
3
|
* What to do when a Trusted Web Activity cannot be launched because of a
|
|
4
4
|
* browser or environment problem.
|
|
5
|
+
*
|
|
6
|
+
* - `custom-tabs` (default): open the URL in Android Custom Tabs with the
|
|
7
|
+
* browser chrome visible.
|
|
8
|
+
* - `webview`: open the URL in a dedicated fullscreen Android WebView
|
|
9
|
+
* Activity. The WebView fallback has no PostMessage channel.
|
|
10
|
+
* - `none`: reject with a descriptive error.
|
|
11
|
+
*
|
|
12
|
+
* Digital Asset Links verification failures and timeouts never use any
|
|
13
|
+
* fallback, regardless of this setting.
|
|
5
14
|
*/
|
|
6
|
-
export type TwaFallbackMode = 'custom-tabs' | 'none';
|
|
15
|
+
export type TwaFallbackMode = 'custom-tabs' | 'webview' | 'none';
|
|
7
16
|
/**
|
|
8
17
|
* How the module treats Digital Asset Links verification before opening the
|
|
9
18
|
* URL.
|
|
@@ -12,15 +21,15 @@ export type TwaVerificationMode = 'strict' | 'permissive';
|
|
|
12
21
|
/**
|
|
13
22
|
* Surface that actually opened the URL.
|
|
14
23
|
*/
|
|
15
|
-
export type TwaLaunchMode = 'twa' | 'custom-tabs';
|
|
24
|
+
export type TwaLaunchMode = 'twa' | 'custom-tabs' | 'webview';
|
|
16
25
|
export type TwaLaunchResult = {
|
|
17
26
|
mode: TwaLaunchMode;
|
|
18
27
|
/**
|
|
19
28
|
* `true` only when this module confirmed Digital Asset Links verification
|
|
20
29
|
* before opening a fullscreen Trusted Web Activity.
|
|
21
30
|
*
|
|
22
|
-
* Always `false` in `permissive` mode and whenever `mode` is `custom-tabs
|
|
23
|
-
* an unverified navigation is never reported as verified.
|
|
31
|
+
* Always `false` in `permissive` mode and whenever `mode` is `custom-tabs`
|
|
32
|
+
* or `webview`: an unverified navigation is never reported as verified.
|
|
24
33
|
*/
|
|
25
34
|
verified: boolean;
|
|
26
35
|
};
|
|
@@ -88,6 +97,11 @@ export type TwaLaunchOptions = {
|
|
|
88
97
|
* - `custom-tabs` (default): open the same URL in Android Custom Tabs with
|
|
89
98
|
* the browser chrome visible. PostMessage keeps working through the same
|
|
90
99
|
* Custom Tabs session.
|
|
100
|
+
* - `webview`: open the same URL in a dedicated fullscreen Android WebView
|
|
101
|
+
* Activity. It is used only for browser/session/launch availability
|
|
102
|
+
* failures. PostMessage is unavailable on this surface: when it was
|
|
103
|
+
* requested, `onChannelError` reports
|
|
104
|
+
* `ERR_TWA_POSTMESSAGE_UNAVAILABLE`.
|
|
91
105
|
* - `none`: reject with a descriptive error.
|
|
92
106
|
*
|
|
93
107
|
* Digital Asset Links verification failures and timeouts never fall back —
|
|
@@ -130,6 +144,11 @@ export type TwaLaunchOptions = {
|
|
|
130
144
|
*
|
|
131
145
|
* The channel is requested after the first navigation finishes. Listen for
|
|
132
146
|
* `onChannelReady` before calling `postMessage()`.
|
|
147
|
+
*
|
|
148
|
+
* PostMessage is not available in the WebView fallback. If the WebView
|
|
149
|
+
* fallback is used, `onChannelError` reports
|
|
150
|
+
* `ERR_TWA_POSTMESSAGE_UNAVAILABLE` and `postMessage()` rejects with
|
|
151
|
+
* `ERR_TWA_POSTMESSAGE_NOT_READY`.
|
|
133
152
|
*/
|
|
134
153
|
postMessage?: TwaPostMessageOptions;
|
|
135
154
|
};
|
|
@@ -17,7 +17,11 @@ export declare const ExpoTwaBridge: {
|
|
|
17
17
|
*
|
|
18
18
|
* When the TWA cannot launch because of a browser or environment problem and
|
|
19
19
|
* `fallback` is `'custom-tabs'` (the default), the URL is opened in Android
|
|
20
|
-
* Custom Tabs instead and the result is `{ mode: 'custom-tabs' }`.
|
|
20
|
+
* Custom Tabs instead and the result is `{ mode: 'custom-tabs' }`. With
|
|
21
|
+
* `fallback: 'webview'`, the URL is opened in a dedicated fullscreen WebView
|
|
22
|
+
* Activity instead and the result is `{ mode: 'webview' }`. The WebView
|
|
23
|
+
* fallback has no PostMessage channel; `onChannelError` reports
|
|
24
|
+
* `ERR_TWA_POSTMESSAGE_UNAVAILABLE` when PostMessage was requested.
|
|
21
25
|
*
|
|
22
26
|
* @throws {TwaLaunchError} in `strict` mode with
|
|
23
27
|
* `ERR_TWA_VERIFICATION_FAILED` / `ERR_TWA_VERIFICATION_TIMEOUT` when the
|
|
@@ -29,7 +33,9 @@ export declare const ExpoTwaBridge: {
|
|
|
29
33
|
* channel. The channel must be ready; listen for `onChannelReady` first.
|
|
30
34
|
*
|
|
31
35
|
* The channel is backed by the browser session, so it also works when the
|
|
32
|
-
* Custom Tabs fallback opened the URL.
|
|
36
|
+
* Custom Tabs fallback opened the URL. It is never available when the
|
|
37
|
+
* WebView fallback opened the URL; in that case this rejects with
|
|
38
|
+
* `ERR_TWA_POSTMESSAGE_NOT_READY`.
|
|
33
39
|
*/
|
|
34
40
|
postMessage(message: string): Promise<void>;
|
|
35
41
|
/**
|
|
@@ -7,7 +7,7 @@ const ExpoTwaBridge_errors_1 = require("./ExpoTwaBridge.errors");
|
|
|
7
7
|
Object.defineProperty(exports, "TwaLaunchError", { enumerable: true, get: function () { return ExpoTwaBridge_errors_1.TwaLaunchError; } });
|
|
8
8
|
const ExpoTwaBridgeModule = (0, expo_1.requireNativeModule)('ExpoTwaBridge');
|
|
9
9
|
function parseLaunchMode(value) {
|
|
10
|
-
if (value === 'twa' || value === 'custom-tabs') {
|
|
10
|
+
if (value === 'twa' || value === 'custom-tabs' || value === 'webview') {
|
|
11
11
|
return value;
|
|
12
12
|
}
|
|
13
13
|
throw new ExpoTwaBridge_errors_1.TwaLaunchError('ERR_TWA_LAUNCH_FAILED', `The native module returned an unknown launch mode "${value}".`);
|
|
@@ -43,7 +43,11 @@ exports.ExpoTwaBridge = {
|
|
|
43
43
|
*
|
|
44
44
|
* When the TWA cannot launch because of a browser or environment problem and
|
|
45
45
|
* `fallback` is `'custom-tabs'` (the default), the URL is opened in Android
|
|
46
|
-
* Custom Tabs instead and the result is `{ mode: 'custom-tabs' }`.
|
|
46
|
+
* Custom Tabs instead and the result is `{ mode: 'custom-tabs' }`. With
|
|
47
|
+
* `fallback: 'webview'`, the URL is opened in a dedicated fullscreen WebView
|
|
48
|
+
* Activity instead and the result is `{ mode: 'webview' }`. The WebView
|
|
49
|
+
* fallback has no PostMessage channel; `onChannelError` reports
|
|
50
|
+
* `ERR_TWA_POSTMESSAGE_UNAVAILABLE` when PostMessage was requested.
|
|
47
51
|
*
|
|
48
52
|
* @throws {TwaLaunchError} in `strict` mode with
|
|
49
53
|
* `ERR_TWA_VERIFICATION_FAILED` / `ERR_TWA_VERIFICATION_TIMEOUT` when the
|
|
@@ -86,7 +90,9 @@ exports.ExpoTwaBridge = {
|
|
|
86
90
|
* channel. The channel must be ready; listen for `onChannelReady` first.
|
|
87
91
|
*
|
|
88
92
|
* The channel is backed by the browser session, so it also works when the
|
|
89
|
-
* Custom Tabs fallback opened the URL.
|
|
93
|
+
* Custom Tabs fallback opened the URL. It is never available when the
|
|
94
|
+
* WebView fallback opened the URL; in that case this rejects with
|
|
95
|
+
* `ERR_TWA_POSTMESSAGE_NOT_READY`.
|
|
90
96
|
*/
|
|
91
97
|
async postMessage(message) {
|
|
92
98
|
try {
|