Skip to content

Commit 530ecfe

Browse files
Protocol Handler support
1 parent 9ba50ce commit 530ecfe

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

packages/core/src/lib/TwaManifest.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {fetchUtils} from './FetchUtils';
2121
import {findSuitableIcon, generatePackageId, validateNotEmpty} from './util';
2222
import Color = require('color');
2323
import {ConsoleLog} from './Log';
24-
import {ShareTarget, WebManifestIcon, WebManifestJson} from './types/WebManifest';
24+
import {ProtocolHandler, ShareTarget, WebManifestIcon, WebManifestJson} from './types/WebManifest';
2525
import {ShortcutInfo} from './ShortcutInfo';
2626
import {AppsFlyerConfig} from './features/AppsFlyerFeature';
2727
import {LocationDelegationConfig} from './features/LocationDelegationFeature';
@@ -169,6 +169,7 @@ export class TwaManifest {
169169
serviceAccountJsonFile: string | undefined;
170170
additionalTrustedOrigins: string[];
171171
retainedBundles: number[];
172+
protocolHandlers?: ProtocolHandler[];
172173

173174
private static log = new ConsoleLog('twa-manifest');
174175

@@ -219,6 +220,7 @@ export class TwaManifest {
219220
this.serviceAccountJsonFile = data.serviceAccountJsonFile;
220221
this.additionalTrustedOrigins = data.additionalTrustedOrigins || [];
221222
this.retainedBundles = data.retainedBundles || [];
223+
this.protocolHandlers = data.protocolHandlers;
222224
}
223225

224226
/**
@@ -343,6 +345,7 @@ export class TwaManifest {
343345
shareTarget: TwaManifest.verifyShareTarget(webManifestUrl, webManifest.share_target),
344346
orientation: asOrientation(webManifest.orientation) || DEFAULT_ORIENTATION,
345347
fullScopeUrl: fullScopeUrl.toString(),
348+
protocolHandlers: webManifest.protocol_handlers,
346349
});
347350
return twaManifest;
348351
}
@@ -479,6 +482,12 @@ export class TwaManifest {
479482
oldTwaManifestJson.iconUrl!, webManifest.icons!, 'monochrome', MIN_NOTIFICATION_ICON_SIZE,
480483
webManifestUrl);
481484

485+
let mergedProtocolHandlers = new Map<String, String>();
486+
oldTwaManifest.protocolHandlers?.forEach(handler => mergedProtocolHandlers.set(handler.protocol, handler.url));
487+
if (!(fieldsToIgnore.includes('protocol_handlers'))) {
488+
webManifest.protocol_handlers?.forEach(handler => mergedProtocolHandlers.set(handler.protocol, handler.url));
489+
}
490+
482491
const fullStartUrl: URL = new URL(webManifest['start_url'] || '/', webManifestUrl);
483492
const fullScopeUrl: URL = new URL(webManifest['scope'] || '.', webManifestUrl);
484493

@@ -503,6 +512,7 @@ export class TwaManifest {
503512
maskableIconUrl: maskableIconUrl || oldTwaManifestJson.maskableIconUrl,
504513
monochromeIconUrl: monochromeIconUrl || oldTwaManifestJson.monochromeIconUrl,
505514
shortcuts: shortcuts,
515+
protocolHandlers: Array.from(mergedProtocolHandlers.entries()).map(([protocol, url]) => ({protocol, url} as ProtocolHandler))
506516
});
507517
return twaManifest;
508518
}
@@ -558,6 +568,7 @@ export interface TwaManifestJson {
558568
serviceAccountJsonFile?: string;
559569
additionalTrustedOrigins?: string[];
560570
retainedBundles?: number[];
571+
protocolHandlers?: ProtocolHandler[];
561572
}
562573

563574
export interface SigningKeyInfo {

packages/core/src/lib/types/WebManifest.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ export interface WebManifestShortcutJson {
3030
icons?: Array<WebManifestIcon>;
3131
}
3232

33+
export interface ProtocolHandler {
34+
protocol: string;
35+
url: string;
36+
}
37+
3338
type WebManifestDisplayMode = 'browser' | 'minimal-ui' | 'standalone' | 'fullscreen';
3439

3540
export type OrientationLock = 'any' | 'natural' | 'landscape'| 'portrait' | 'portrait-primary'|
@@ -67,4 +72,5 @@ export interface WebManifestJson {
6772
shortcuts?: Array<WebManifestShortcutJson>;
6873
share_target?: ShareTarget;
6974
orientation?: OrientationLock;
75+
protocol_handlers?: Array<ProtocolHandler>;
7076
}

packages/core/template_project/app/src/main/AndroidManifest.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,15 @@
207207
android:host="<%= additionalOrigin %>"/>
208208
</intent-filter>
209209
<% } %>
210+
211+
<% for (const protocolHandler of protocolHandlers) { %>
212+
<intent-filter>
213+
<action android:name="android.intent.action.VIEW"/>
214+
<category android:name="android.intent.category.DEFAULT" />
215+
<category android:name="android.intent.category.BROWSABLE"/>
216+
<data android:scheme="<%= protocolHandler.protocol %>" />
217+
</intent-filter>
218+
<% } %>
210219
</activity>
211220

212221
<activity android:name="com.google.androidbrowserhelper.trusted.FocusActivity" />

packages/core/template_project/app/src/main/java/LauncherActivity.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import android.os.Build;
2121
import android.os.Bundle;
2222

23+
import java.util.HashMap;
24+
import java.util.Map;
25+
2326
<% for(const imp of launcherActivity.imports) { %>
2427
import <%= imp %>;
2528
<% } %>
@@ -48,6 +51,12 @@ protected void onCreate(Bundle savedInstanceState) {
4851
}
4952
}
5053

54+
private final Map<String, String> mProtocolHandlers = new HashMap<String, String>() {{
55+
<% for(const protocol of protocolHandlers) { %>
56+
put("<%= protocol.protocol %>", "<%= protocol.url %>");
57+
<% } %>
58+
}};
59+
5160
@Override
5261
protected Uri getLaunchingUrl() {
5362
// Get the original launch Url.
@@ -57,6 +66,15 @@ protected Uri getLaunchingUrl() {
5766
<%= code %>
5867
<% } %>
5968

69+
/* Protocol Handler support */
70+
String scheme = uri.getScheme();
71+
if (mProtocolHandlers.containsKey(scheme)) {
72+
String target = uri.toString().replace(scheme + "://", "");
73+
String format = mProtocolHandlers.get(scheme);
74+
String baseUrl = getString(R.string.launchUrl);
75+
return Uri.parse(baseUrl + String.format(format, target));
76+
}
77+
6078
return uri;
6179
}
6280
}

0 commit comments

Comments
 (0)