Skip to content

Commit f6f17c6

Browse files
Protocol Handler support
1 parent 65e5fc1 commit f6f17c6

File tree

1 file changed

+48
-11
lines changed

1 file changed

+48
-11
lines changed

androidbrowserhelper/src/main/java/com/google/androidbrowserhelper/trusted/LauncherActivity.java

+48-11
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838

3939
import org.json.JSONException;
4040

41+
import java.util.Collections;
42+
import java.util.Map;
43+
import java.util.Objects;
44+
4145
/**
4246
* A convenience class to make using Trusted Web Activities easier. You can extend this class for
4347
* basic modifications to the behaviour.
@@ -200,8 +204,9 @@ protected void launchTwa() {
200204
getColorCompat(mMetadata.navigationBarDividerColorDarkId))
201205
.build();
202206

207+
Uri launchUrl = getLaunchingUrl();
203208
TrustedWebActivityIntentBuilder twaBuilder =
204-
new TrustedWebActivityIntentBuilder(getLaunchingUrl())
209+
new TrustedWebActivityIntentBuilder(launchUrl)
205210
.setToolbarColor(getColorCompat(mMetadata.statusBarColorId))
206211
.setNavigationBarColor(getColorCompat(mMetadata.navigationBarColorId))
207212
.setNavigationBarDividerColor(
@@ -212,6 +217,12 @@ protected void launchTwa() {
212217
.setDisplayMode(getDisplayMode())
213218
.setScreenOrientation(mMetadata.screenOrientation);
214219

220+
// TODO When available
221+
// Uri intentUrl = getIntent().getData();
222+
// if (!launchUrl.equals(intentUrl)) {
223+
// twaBuilder.setOriginalLaunchUrl(launchUrlBundle.getOriginalUrl());
224+
// }
225+
215226
if (mMetadata.additionalTrustedOrigins != null) {
216227
twaBuilder.setAdditionalTrustedOrigins(mMetadata.additionalTrustedOrigins);
217228
}
@@ -330,6 +341,23 @@ protected void onSaveInstanceState(Bundle outState) {
330341
outState.putBoolean(BROWSER_WAS_LAUNCHED_KEY, mBrowserWasLaunched);
331342
}
332343

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+
333361
@Override
334362
public void onEnterAnimationComplete() {
335363
super.onEnterAnimationComplete();
@@ -347,18 +375,27 @@ public void onEnterAnimationComplete() {
347375
* Override this for special handling (such as ignoring or sanitising data from the Intent).
348376
*/
349377
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;
359395
}
360396

361-
return Uri.parse("https://www.example.com/");
397+
Log.d(TAG, "Using url from Manifest: " + baseUrl);
398+
return baseUrl;
362399
}
363400

364401
/**

0 commit comments

Comments
 (0)