38
38
39
39
import org .json .JSONException ;
40
40
41
+ import java .util .Collections ;
42
+ import java .util .Map ;
43
+ import java .util .Objects ;
44
+
41
45
/**
42
46
* A convenience class to make using Trusted Web Activities easier. You can extend this class for
43
47
* basic modifications to the behaviour.
@@ -200,8 +204,9 @@ protected void launchTwa() {
200
204
getColorCompat (mMetadata .navigationBarDividerColorDarkId ))
201
205
.build ();
202
206
207
+ Uri launchUrl = getLaunchingUrl ();
203
208
TrustedWebActivityIntentBuilder twaBuilder =
204
- new TrustedWebActivityIntentBuilder (getLaunchingUrl () )
209
+ new TrustedWebActivityIntentBuilder (launchUrl )
205
210
.setToolbarColor (getColorCompat (mMetadata .statusBarColorId ))
206
211
.setNavigationBarColor (getColorCompat (mMetadata .navigationBarColorId ))
207
212
.setNavigationBarDividerColor (
@@ -212,6 +217,12 @@ protected void launchTwa() {
212
217
.setDisplayMode (getDisplayMode ())
213
218
.setScreenOrientation (mMetadata .screenOrientation );
214
219
220
+ // TODO When available
221
+ // Uri intentUrl = getIntent().getData();
222
+ // if (!launchUrl.equals(intentUrl)) {
223
+ // twaBuilder.setOriginalLaunchUrl(launchUrlBundle.getOriginalUrl());
224
+ // }
225
+
215
226
if (mMetadata .additionalTrustedOrigins != null ) {
216
227
twaBuilder .setAdditionalTrustedOrigins (mMetadata .additionalTrustedOrigins );
217
228
}
@@ -330,6 +341,23 @@ protected void onSaveInstanceState(Bundle outState) {
330
341
outState .putBoolean (BROWSER_WAS_LAUNCHED_KEY , mBrowserWasLaunched );
331
342
}
332
343
344
+ /**
345
+ * Override this to enable Protocol Handler support.
346
+ * Keys of this map are data schemes, e.g. "bitcoin", "irc", "xmpp", "web+coffee", and values
347
+ * are templates that will be used to construct the full URL. The template must contain a "%s"
348
+ * token and be an absolute location with http/https scheme and the same origin as the TWA.
349
+ *
350
+ * An example valid entry in the map would be:
351
+ * ["web+coffee"] -> ["https://coffee.com/?type=%s"]
352
+ * This would result in a link "web+coffee://latte" being converted to
353
+ * "https://coffee.com/?type=latte".
354
+ *
355
+ * {@see https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps/Manifest/Reference/protocol_handlers}
356
+ */
357
+ protected Map <String , Uri > getProtocolHandlers () {
358
+ return Collections .emptyMap ();
359
+ }
360
+
333
361
@ Override
334
362
public void onEnterAnimationComplete () {
335
363
super .onEnterAnimationComplete ();
@@ -347,18 +375,27 @@ public void onEnterAnimationComplete() {
347
375
* Override this for special handling (such as ignoring or sanitising data from the Intent).
348
376
*/
349
377
protected Uri getLaunchingUrl () {
350
- Uri uri = getIntent ().getData ();
351
- if (uri != null ) {
352
- Log .d (TAG , "Using URL from Intent (" + uri + ")." );
353
- return uri ;
354
- }
355
-
356
- if (mMetadata .defaultUrl != null ) {
357
- Log .d (TAG , "Using URL from Manifest (" + mMetadata .defaultUrl + ")." );
358
- return Uri .parse (mMetadata .defaultUrl );
378
+ Uri baseUrl = Uri .parse (mMetadata .defaultUrl );
379
+
380
+ Uri intentUrl = getIntent ().getData ();
381
+
382
+ if (intentUrl != null ) {
383
+ Map <String , Uri > protocolHandlers = getProtocolHandlers ();
384
+ String scheme = intentUrl .getScheme ();
385
+ if (protocolHandlers .containsKey (scheme )) {
386
+ String format = Objects .requireNonNull (protocolHandlers .get (scheme )).toString ();
387
+ String target = intentUrl .toString ().replace (scheme + "://" , "" );
388
+ Uri targetUrl = Uri .parse (String .format (format , target ));
389
+ Log .d (TAG , "Using protocol handler url: " + targetUrl );
390
+ return targetUrl ;
391
+ }
392
+
393
+ Log .d (TAG , "Using url from Intent: " + intentUrl );
394
+ return intentUrl ;
359
395
}
360
396
361
- return Uri .parse ("https://www.example.com/" );
397
+ Log .d (TAG , "Using url from Manifest: " + baseUrl );
398
+ return baseUrl ;
362
399
}
363
400
364
401
/**
0 commit comments