1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const xml2js_1 = require("xml2js");
|
4 | function getScheme(config) {
|
5 | return typeof config.scheme === 'string' ? config.scheme : null;
|
6 | }
|
7 | exports.getScheme = getScheme;
|
8 | async function setScheme(config, manifestDocument) {
|
9 | let scheme = getScheme(config);
|
10 | if (!scheme) {
|
11 | return manifestDocument;
|
12 | }
|
13 | let mainActivity = manifestDocument.manifest.application[0].activity.filter((e) => e['$']['android:name'] === '.MainActivity');
|
14 | const schemeTag = `<data android:scheme="${scheme}"/>`;
|
15 | const intentFiltersXML = `
|
16 | <intent-filter>
|
17 | <action android:name="android.intent.action.VIEW"/>
|
18 | <category android:name="android.intent.category.DEFAULT"/>
|
19 | <category android:name="android.intent.category.BROWSABLE"/>
|
20 | ${schemeTag}
|
21 | </intent-filter>`;
|
22 | const parser = new xml2js_1.Parser();
|
23 | const intentFiltersJSON = await parser.parseStringPromise(intentFiltersXML);
|
24 | if ('intent-filter' in mainActivity[0]) {
|
25 | mainActivity[0]['intent-filter'] = mainActivity[0]['intent-filter'].concat(intentFiltersJSON['intent-filter']);
|
26 | }
|
27 | else {
|
28 | mainActivity[0]['intent-filter'] = intentFiltersJSON['intent-filter'];
|
29 | }
|
30 | return manifestDocument;
|
31 | }
|
32 | exports.setScheme = setScheme;
|
33 | function isValidRedirectIntentFilter({ actions, categories, schemes }) {
|
34 | return (actions.includes('android.intent.action.VIEW') &&
|
35 | !categories.includes('android.intent.category.LAUNCHER'));
|
36 | }
|
37 | function propertiesFromIntentFilter(intentFilter) {
|
38 | var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
39 | const actions = (_c = (_b = (_a = intentFilter) === null || _a === void 0 ? void 0 : _a.action) === null || _b === void 0 ? void 0 : _b.map((data) => { var _a, _b; return (_b = (_a = data) === null || _a === void 0 ? void 0 : _a['$']) === null || _b === void 0 ? void 0 : _b['android:name']; }), (_c !== null && _c !== void 0 ? _c : []));
|
40 | const categories = (_f = (_e = (_d = intentFilter) === null || _d === void 0 ? void 0 : _d.category) === null || _e === void 0 ? void 0 : _e.map((data) => { var _a, _b; return (_b = (_a = data) === null || _a === void 0 ? void 0 : _a['$']) === null || _b === void 0 ? void 0 : _b['android:name']; }), (_f !== null && _f !== void 0 ? _f : []));
|
41 | const schemes = (_j = (_h = (_g = intentFilter) === null || _g === void 0 ? void 0 : _g.data) === null || _h === void 0 ? void 0 : _h.map((data) => { var _a, _b; return (_b = (_a = data) === null || _a === void 0 ? void 0 : _a['$']) === null || _b === void 0 ? void 0 : _b['android:scheme']; }), (_j !== null && _j !== void 0 ? _j : []));
|
42 | return {
|
43 | schemes,
|
44 | actions,
|
45 | categories,
|
46 | };
|
47 | }
|
48 | function getSingleTaskIntentFilters(manifestDocument) {
|
49 | if (!Array.isArray(manifestDocument.manifest.application))
|
50 | return [];
|
51 | let outputSchemes = [];
|
52 | for (let application of manifestDocument.manifest.application) {
|
53 | const { activity } = application;
|
54 | let activities = Array.isArray(activity) ? activity : [activity];
|
55 | const singleTaskActivities = activities.filter(activity => { var _a, _b; return ((_b = (_a = activity) === null || _a === void 0 ? void 0 : _a['$']) === null || _b === void 0 ? void 0 : _b['android:launchMode']) === 'singleTask'; });
|
56 | for (const activity of singleTaskActivities) {
|
57 | const intentFilters = activity['intent-filter'];
|
58 | outputSchemes = outputSchemes.concat(intentFilters);
|
59 | }
|
60 | }
|
61 | return outputSchemes;
|
62 | }
|
63 | function getSchemesFromManifest(manifestDocument) {
|
64 | let outputSchemes = [];
|
65 | const singleTaskIntentFilters = getSingleTaskIntentFilters(manifestDocument);
|
66 | for (const intentFilter of singleTaskIntentFilters) {
|
67 | const properties = propertiesFromIntentFilter(intentFilter);
|
68 | if (isValidRedirectIntentFilter(properties)) {
|
69 | outputSchemes.push(properties);
|
70 | }
|
71 | }
|
72 | return outputSchemes.reduce((prev, { schemes }) => [...prev, ...schemes], []);
|
73 | }
|
74 | exports.getSchemesFromManifest = getSchemesFromManifest;
|
75 | function ensureManifestHasValidIntentFilter(manifestDocument) {
|
76 | var _a, _b;
|
77 | if (!Array.isArray(manifestDocument.manifest.application))
|
78 | return false;
|
79 | for (let application of manifestDocument.manifest.application) {
|
80 | for (let activity of application.activity) {
|
81 | if (((_b = (_a = activity) === null || _a === void 0 ? void 0 : _a['$']) === null || _b === void 0 ? void 0 : _b['android:launchMode']) === 'singleTask') {
|
82 | for (let intentFilter of activity['intent-filter']) {
|
83 |
|
84 | const properties = propertiesFromIntentFilter(intentFilter);
|
85 | if (isValidRedirectIntentFilter(properties)) {
|
86 | return true;
|
87 | }
|
88 | }
|
89 | activity['intent-filter'].push({
|
90 | action: [{ $: { 'android:name': 'android.intent.action.VIEW' } }],
|
91 | category: [
|
92 | { $: { 'android:name': 'android.intent.category.DEFAULT' } },
|
93 | { $: { 'android:name': 'android.intent.category.BROWSABLE' } },
|
94 | ],
|
95 | });
|
96 | return true;
|
97 | }
|
98 | }
|
99 | }
|
100 | return false;
|
101 | }
|
102 | exports.ensureManifestHasValidIntentFilter = ensureManifestHasValidIntentFilter;
|
103 | function hasScheme(scheme, manifestDocument) {
|
104 | const schemes = getSchemesFromManifest(manifestDocument);
|
105 | return schemes.includes(scheme);
|
106 | }
|
107 | exports.hasScheme = hasScheme;
|
108 | function appendScheme(scheme, manifestDocument) {
|
109 | var _a, _b;
|
110 | if (!Array.isArray(manifestDocument.manifest.application))
|
111 | return manifestDocument;
|
112 | for (let application of manifestDocument.manifest.application) {
|
113 | for (let activity of application.activity) {
|
114 | if (((_b = (_a = activity) === null || _a === void 0 ? void 0 : _a['$']) === null || _b === void 0 ? void 0 : _b['android:launchMode']) === 'singleTask') {
|
115 | for (let intentFilter of activity['intent-filter']) {
|
116 | const properties = propertiesFromIntentFilter(intentFilter);
|
117 | if (isValidRedirectIntentFilter(properties)) {
|
118 | if (!intentFilter.data)
|
119 | intentFilter.data = [];
|
120 | intentFilter.data.push({
|
121 | $: { 'android:scheme': scheme },
|
122 | });
|
123 | }
|
124 | }
|
125 | break;
|
126 | }
|
127 | }
|
128 | }
|
129 | return manifestDocument;
|
130 | }
|
131 | exports.appendScheme = appendScheme;
|
132 | function removeScheme(scheme, manifestDocument) {
|
133 | var _a, _b, _c, _d, _e;
|
134 | if (!Array.isArray(manifestDocument.manifest.application))
|
135 | return manifestDocument;
|
136 | for (let application of manifestDocument.manifest.application) {
|
137 | for (let activity of application.activity) {
|
138 | if (((_b = (_a = activity) === null || _a === void 0 ? void 0 : _a['$']) === null || _b === void 0 ? void 0 : _b['android:launchMode']) === 'singleTask') {
|
139 | for (let intentFilter of activity['intent-filter']) {
|
140 |
|
141 | const properties = propertiesFromIntentFilter(intentFilter);
|
142 | if (isValidRedirectIntentFilter(properties)) {
|
143 | for (let dataKey in (_c = intentFilter) === null || _c === void 0 ? void 0 : _c.data) {
|
144 | let data = intentFilter.data[dataKey];
|
145 | if (((_e = (_d = data) === null || _d === void 0 ? void 0 : _d['$']) === null || _e === void 0 ? void 0 : _e['android:scheme']) === scheme) {
|
146 | delete intentFilter.data[dataKey];
|
147 | }
|
148 | }
|
149 | }
|
150 | }
|
151 | break;
|
152 | }
|
153 | }
|
154 | }
|
155 | return manifestDocument;
|
156 | }
|
157 | exports.removeScheme = removeScheme;
|
158 |
|
\ | No newline at end of file |