This repository has been archived by the owner on Nov 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanifestWriter.js
316 lines (261 loc) · 8.84 KB
/
manifestWriter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**
Class injects plugin preferences into AndroidManifest.xml file.
*/
var path = require('path');
var xmlHelper = require('../xmlHelper.js');
module.exports = {
writePreferences: writePreferences
};
// region Public API
/**
* Inject preferences into AndroidManifest.xml file.
*
* @param {Object} cordovaContext - cordova context object
* @param {Object} pluginPreferences - plugin preferences as JSON object; already parsed
*/
function writePreferences(cordovaContext, pluginPreferences) {
var pathToManifest = path.join(cordovaContext.opts.projectRoot, 'platforms', 'android', 'app', 'src', 'main', 'AndroidManifest.xml');
var manifestSource = xmlHelper.readXmlAsJson(pathToManifest);
var cleanManifest;
var updatedManifest;
// remove old intent-filters
cleanManifest = removeOldOptions(manifestSource);
// inject intent-filters based on plugin preferences
updatedManifest = injectOptions(cleanManifest, pluginPreferences);
// save new version of the AndroidManifest
xmlHelper.writeJsonAsXml(updatedManifest, pathToManifest);
}
// endregion
// region Manifest cleanup methods
/**
* Remove old intent-filters from the manifest file.
*
* @param {Object} manifestData - manifest content as JSON object
* @return {Object} manifest data without old intent-filters
*/
function removeOldOptions(manifestData) {
var cleanManifest = manifestData;
var activities = manifestData['manifest']['application'][0]['activity'];
activities.forEach(removeIntentFiltersFromActivity);
cleanManifest['manifest']['application'][0]['activity'] = activities;
return cleanManifest;
}
/**
* Remove old intent filters from the given activity.
*
* @param {Object} activity - activity, from which we need to remove intent-filters.
* Changes applied to the passed object.
*/
function removeIntentFiltersFromActivity(activity) {
var oldIntentFilters = activity['intent-filter'];
var newIntentFilters = [];
if (oldIntentFilters == null || oldIntentFilters.length == 0) {
return;
}
oldIntentFilters.forEach(function(intentFilter) {
if (!isIntentFilterForUniversalLinks(intentFilter)) {
newIntentFilters.push(intentFilter);
}
});
activity['intent-filter'] = newIntentFilters;
}
/**
* Check if given intent-filter is for Universal Links.
*
* @param {Object} intentFilter - intent-filter to check
* @return {Boolean} true - if intent-filter for Universal Links; otherwise - false;
*/
function isIntentFilterForUniversalLinks(intentFilter) {
var actions = intentFilter['action'];
var categories = intentFilter['category'];
var data = intentFilter['data'];
return isActionForUniversalLinks(actions) &&
isCategoriesForUniversalLinks(categories) &&
isDataTagForUniversalLinks(data);
}
/**
* Check if actions from the intent-filter corresponds to actions for Universal Links.
*
* @param {Array} actions - list of actions in the intent-filter
* @return {Boolean} true - if action for Universal Links; otherwise - false
*/
function isActionForUniversalLinks(actions) {
// there can be only 1 action
if (actions == null || actions.length != 1) {
return false;
}
var action = actions[0]['$']['android:name'];
return ('android.intent.action.VIEW' === action);
}
/**
* Check if categories in the intent-filter corresponds to categories for Universal Links.
*
* @param {Array} categories - list of categories in the intent-filter
* @return {Boolean} true - if action for Universal Links; otherwise - false
*/
function isCategoriesForUniversalLinks(categories) {
// there can be only 2 categories
if (categories == null || categories.length != 2) {
return false;
}
var isBrowsable = false;
var isDefault = false;
// check intent categories
categories.forEach(function(category) {
var categoryName = category['$']['android:name'];
if (!isBrowsable) {
isBrowsable = 'android.intent.category.BROWSABLE' === categoryName;
}
if (!isDefault) {
isDefault = 'android.intent.category.DEFAULT' === categoryName;
}
});
return isDefault && isBrowsable;
}
/**
* Check if data tag from intent-filter corresponds to data for Universal Links.
*
* @param {Array} data - list of data tags in the intent-filter
* @return {Boolean} true - if data tag for Universal Links; otherwise - false
*/
function isDataTagForUniversalLinks(data) {
// can have only 1 data tag in the intent-filter
if (data == null || data.length != 1) {
return false;
}
var dataHost = data[0]['$']['android:host'];
var dataScheme = data[0]['$']['android:scheme'];
var hostIsSet = dataHost != null && dataHost.length > 0;
var schemeIsSet = dataScheme != null && dataScheme.length > 0;
return hostIsSet && schemeIsSet;
}
// endregion
// region Methods to inject preferences into AndroidManifest.xml file
/**
* Inject options into manifest file.
*
* @param {Object} manifestData - manifest content where preferences should be injected
* @param {Object} pluginPreferences - plugin preferences from config.xml; already parsed
* @return {Object} updated manifest data with corresponding intent-filters
*/
function injectOptions(manifestData, pluginPreferences) {
var changedManifest = manifestData;
var activitiesList = changedManifest['manifest']['application'][0]['activity'];
var launchActivityIndex = getMainLaunchActivityIndex(activitiesList);
var ulIntentFilters = [];
var launchActivity;
if (launchActivityIndex < 0) {
console.warn('Could not find launch activity in the AndroidManifest file. Can\'t inject Universal Links preferences.');
return;
}
// get launch activity
launchActivity = activitiesList[launchActivityIndex];
// generate intent-filters
pluginPreferences.hosts.forEach(function(host) {
host.paths.forEach(function(hostPath) {
ulIntentFilters.push(createIntentFilter(host.name, host.scheme, hostPath));
});
});
// add Universal Links intent-filters to the launch activity
launchActivity['intent-filter'] = launchActivity['intent-filter'].concat(ulIntentFilters);
return changedManifest;
}
/**
* Find index of the applications launcher activity.
*
* @param {Array} activities - list of all activities in the app
* @return {Integer} index of the launch activity; -1 - if none was found
*/
function getMainLaunchActivityIndex(activities) {
var launchActivityIndex = -1;
activities.some(function(activity, index) {
if (isLaunchActivity(activity)) {
launchActivityIndex = index;
return true;
}
return false;
});
return launchActivityIndex;
}
/**
* Check if the given actvity is a launch activity.
*
* @param {Object} activity - activity to check
* @return {Boolean} true - if this is a launch activity; otherwise - false
*/
function isLaunchActivity(activity) {
var intentFilters = activity['intent-filter'];
var isLauncher = false;
if (intentFilters == null || intentFilters.length == 0) {
return false;
}
isLauncher = intentFilters.some(function(intentFilter) {
var action = intentFilter['action'];
var category = intentFilter['category'];
if (action == null || action.length != 1 || category == null || category.length != 1) {
return false;
}
var isMainAction = ('android.intent.action.MAIN' === action[0]['$']['android:name']);
var isLauncherCategory = ('android.intent.category.LAUNCHER' === category[0]['$']['android:name']);
return isMainAction && isLauncherCategory;
});
return isLauncher;
}
/**
* Create JSON object that represent intent-filter for universal link.
*
* @param {String} host - host name
* @param {String} scheme - host scheme
* @param {String} pathName - host path
* @return {Object} intent-filter as a JSON object
*/
function createIntentFilter(host, scheme, pathName) {
var intentFilter = {
'$': {
'android:autoVerify': 'true'
},
'action': [{
'$': {
'android:name': 'android.intent.action.VIEW'
}
}],
'category': [{
'$': {
'android:name': 'android.intent.category.DEFAULT'
}
}, {
'$': {
'android:name': 'android.intent.category.BROWSABLE'
}
}],
'data': [{
'$': {
'android:host': host,
'android:scheme': scheme
}
}]
};
injectPathComponentIntoIntentFilter(intentFilter, pathName);
return intentFilter;
}
/**
* Inject host path into provided intent-filter.
*
* @param {Object} intentFilter - intent-filter object where path component should be injected
* @param {String} pathName - host path to inject
*/
function injectPathComponentIntoIntentFilter(intentFilter, pathName) {
if (pathName == null || pathName === '*') {
return;
}
var attrKey = 'android:path';
if (pathName.indexOf('*') >= 0) {
attrKey = 'android:pathPattern';
pathName = pathName.replace(/\*/g, '.*');
}
if (pathName.indexOf('/') != 0) {
pathName = '/' + pathName;
}
intentFilter['data'][0]['$'][attrKey] = pathName;
}
// endregion