1"use strict"; 2 3Object.defineProperty(exports, "__esModule", { 4 value: true 5}); 6exports.appendScheme = appendScheme; 7exports.ensureManifestHasValidIntentFilter = ensureManifestHasValidIntentFilter; 8exports.getScheme = getScheme; 9exports.getSchemesFromManifest = getSchemesFromManifest; 10exports.hasScheme = hasScheme; 11exports.removeScheme = removeScheme; 12exports.setScheme = setScheme; 13exports.withScheme = void 0; 14function _androidPlugins() { 15 const data = require("../plugins/android-plugins"); 16 _androidPlugins = function () { 17 return data; 18 }; 19 return data; 20} 21function _warnings() { 22 const data = require("../utils/warnings"); 23 _warnings = function () { 24 return data; 25 }; 26 return data; 27} 28const withScheme = (0, _androidPlugins().createAndroidManifestPlugin)(setScheme, 'withScheme'); 29exports.withScheme = withScheme; 30function getScheme(config) { 31 if (Array.isArray(config.scheme)) { 32 const validate = value => typeof value === 'string'; 33 return config.scheme.filter(validate); 34 } else if (typeof config.scheme === 'string') { 35 return [config.scheme]; 36 } 37 return []; 38} 39 40// This plugin used to remove the unused schemes but this is unpredictable because other plugins could add schemes. 41// The only way to reliably remove schemes from the project is to nuke the file and regenerate the code (`npx expo prebuild --clean`). 42// Regardless, having extra schemes isn't a fatal issue and therefore a tolerable compromise is to just add new schemes that aren't currently present. 43function setScheme(config, androidManifest) { 44 var _config$android, _config$android2; 45 const schemes = [...getScheme(config), 46 // @ts-ignore: TODO: android.scheme is an unreleased -- harder to add to turtle v1. 47 ...getScheme((_config$android = config.android) !== null && _config$android !== void 0 ? _config$android : {})]; 48 // Add the package name to the list of schemes for easier Google auth and parity with Turtle v1. 49 if ((_config$android2 = config.android) !== null && _config$android2 !== void 0 && _config$android2.package) { 50 schemes.push(config.android.package); 51 } 52 if (schemes.length === 0) { 53 return androidManifest; 54 } 55 if (!ensureManifestHasValidIntentFilter(androidManifest)) { 56 (0, _warnings().addWarningAndroid)('scheme', `Cannot add schemes because the provided manifest does not have a valid Activity with \`android:launchMode="singleTask"\``, 'https://expo.fyi/setup-android-uri-scheme'); 57 return androidManifest; 58 } 59 60 // Get the current schemes and remove them from the list of schemes to add. 61 const currentSchemes = getSchemesFromManifest(androidManifest); 62 for (const uri of currentSchemes) { 63 const index = schemes.indexOf(uri); 64 if (index > -1) schemes.splice(index, 1); 65 } 66 67 // Now add all of the remaining schemes. 68 for (const uri of schemes) { 69 androidManifest = appendScheme(uri, androidManifest); 70 } 71 return androidManifest; 72} 73function isValidRedirectIntentFilter({ 74 actions, 75 categories 76}) { 77 return actions.includes('android.intent.action.VIEW') && !categories.includes('android.intent.category.LAUNCHER'); 78} 79function propertiesFromIntentFilter(intentFilter) { 80 var _intentFilter$action$, _intentFilter$action, _intentFilter$categor, _intentFilter$categor2, _intentFilter$data$fi, _intentFilter$data, _intentFilter$data$fi2; 81 const actions = (_intentFilter$action$ = intentFilter === null || intentFilter === void 0 ? void 0 : (_intentFilter$action = intentFilter.action) === null || _intentFilter$action === void 0 ? void 0 : _intentFilter$action.map(data => { 82 var _data$$; 83 return data === null || data === void 0 ? void 0 : (_data$$ = data.$) === null || _data$$ === void 0 ? void 0 : _data$$['android:name']; 84 })) !== null && _intentFilter$action$ !== void 0 ? _intentFilter$action$ : []; 85 const categories = (_intentFilter$categor = intentFilter === null || intentFilter === void 0 ? void 0 : (_intentFilter$categor2 = intentFilter.category) === null || _intentFilter$categor2 === void 0 ? void 0 : _intentFilter$categor2.map(data => { 86 var _data$$2; 87 return data === null || data === void 0 ? void 0 : (_data$$2 = data.$) === null || _data$$2 === void 0 ? void 0 : _data$$2['android:name']; 88 })) !== null && _intentFilter$categor !== void 0 ? _intentFilter$categor : []; 89 const data = (_intentFilter$data$fi = intentFilter === null || intentFilter === void 0 ? void 0 : (_intentFilter$data = intentFilter.data) === null || _intentFilter$data === void 0 ? void 0 : (_intentFilter$data$fi2 = _intentFilter$data.filter(data => { 90 var _data$$3; 91 return data === null || data === void 0 ? void 0 : (_data$$3 = data.$) === null || _data$$3 === void 0 ? void 0 : _data$$3['android:scheme']; 92 })) === null || _intentFilter$data$fi2 === void 0 ? void 0 : _intentFilter$data$fi2.map(data => { 93 var _data$$4, _data$$5; 94 return { 95 scheme: data === null || data === void 0 ? void 0 : (_data$$4 = data.$) === null || _data$$4 === void 0 ? void 0 : _data$$4['android:scheme'], 96 host: data === null || data === void 0 ? void 0 : (_data$$5 = data.$) === null || _data$$5 === void 0 ? void 0 : _data$$5['android:host'] 97 }; 98 })) !== null && _intentFilter$data$fi !== void 0 ? _intentFilter$data$fi : []; 99 return { 100 actions, 101 categories, 102 data 103 }; 104} 105function getSingleTaskIntentFilters(androidManifest) { 106 if (!Array.isArray(androidManifest.manifest.application)) return []; 107 let outputSchemes = []; 108 for (const application of androidManifest.manifest.application) { 109 const { 110 activity 111 } = application; 112 // @ts-ignore 113 const activities = Array.isArray(activity) ? activity : [activity]; 114 const singleTaskActivities = activities.filter(activity => { 115 var _activity$$; 116 return (activity === null || activity === void 0 ? void 0 : (_activity$$ = activity.$) === null || _activity$$ === void 0 ? void 0 : _activity$$['android:launchMode']) === 'singleTask'; 117 }); 118 for (const activity of singleTaskActivities) { 119 const intentFilters = activity['intent-filter']; 120 outputSchemes = outputSchemes.concat(intentFilters); 121 } 122 } 123 return outputSchemes; 124} 125function getSchemesFromManifest(androidManifest, requestedHost = null) { 126 const outputSchemes = []; 127 const singleTaskIntentFilters = getSingleTaskIntentFilters(androidManifest); 128 for (const intentFilter of singleTaskIntentFilters) { 129 const properties = propertiesFromIntentFilter(intentFilter); 130 if (isValidRedirectIntentFilter(properties) && properties.data) { 131 for (const { 132 scheme, 133 host 134 } of properties.data) { 135 if (requestedHost === null || !host || host === requestedHost) { 136 outputSchemes.push(scheme); 137 } 138 } 139 } 140 } 141 return outputSchemes; 142} 143function ensureManifestHasValidIntentFilter(androidManifest) { 144 if (!Array.isArray(androidManifest.manifest.application)) { 145 return false; 146 } 147 for (const application of androidManifest.manifest.application) { 148 for (const activity of application.activity || []) { 149 var _activity$$2; 150 if ((activity === null || activity === void 0 ? void 0 : (_activity$$2 = activity.$) === null || _activity$$2 === void 0 ? void 0 : _activity$$2['android:launchMode']) === 'singleTask') { 151 for (const intentFilter of activity['intent-filter'] || []) { 152 // Parse valid intent filters... 153 const properties = propertiesFromIntentFilter(intentFilter); 154 if (isValidRedirectIntentFilter(properties)) { 155 return true; 156 } 157 } 158 if (!activity['intent-filter']) { 159 activity['intent-filter'] = []; 160 } 161 activity['intent-filter'].push({ 162 action: [{ 163 $: { 164 'android:name': 'android.intent.action.VIEW' 165 } 166 }], 167 category: [{ 168 $: { 169 'android:name': 'android.intent.category.DEFAULT' 170 } 171 }, { 172 $: { 173 'android:name': 'android.intent.category.BROWSABLE' 174 } 175 }] 176 }); 177 return true; 178 } 179 } 180 } 181 return false; 182} 183function hasScheme(scheme, androidManifest) { 184 const schemes = getSchemesFromManifest(androidManifest); 185 return schemes.includes(scheme); 186} 187function appendScheme(scheme, androidManifest) { 188 if (!Array.isArray(androidManifest.manifest.application)) { 189 return androidManifest; 190 } 191 for (const application of androidManifest.manifest.application) { 192 for (const activity of application.activity || []) { 193 var _activity$$3; 194 if ((activity === null || activity === void 0 ? void 0 : (_activity$$3 = activity.$) === null || _activity$$3 === void 0 ? void 0 : _activity$$3['android:launchMode']) === 'singleTask') { 195 for (const intentFilter of activity['intent-filter'] || []) { 196 const properties = propertiesFromIntentFilter(intentFilter); 197 if (isValidRedirectIntentFilter(properties)) { 198 if (!intentFilter.data) intentFilter.data = []; 199 intentFilter.data.push({ 200 $: { 201 'android:scheme': scheme 202 } 203 }); 204 } 205 } 206 break; 207 } 208 } 209 } 210 return androidManifest; 211} 212function removeScheme(scheme, androidManifest) { 213 if (!Array.isArray(androidManifest.manifest.application)) { 214 return androidManifest; 215 } 216 for (const application of androidManifest.manifest.application) { 217 for (const activity of application.activity || []) { 218 var _activity$$4; 219 if ((activity === null || activity === void 0 ? void 0 : (_activity$$4 = activity.$) === null || _activity$$4 === void 0 ? void 0 : _activity$$4['android:launchMode']) === 'singleTask') { 220 for (const intentFilter of activity['intent-filter'] || []) { 221 // Parse valid intent filters... 222 const properties = propertiesFromIntentFilter(intentFilter); 223 if (isValidRedirectIntentFilter(properties)) { 224 for (const dataKey in (intentFilter === null || intentFilter === void 0 ? void 0 : intentFilter.data) || []) { 225 var _intentFilter$data2, _data$$6; 226 const data = (_intentFilter$data2 = intentFilter.data) === null || _intentFilter$data2 === void 0 ? void 0 : _intentFilter$data2[dataKey]; 227 if ((data === null || data === void 0 ? void 0 : (_data$$6 = data.$) === null || _data$$6 === void 0 ? void 0 : _data$$6['android:scheme']) === scheme) { 228 var _intentFilter$data3; 229 (_intentFilter$data3 = intentFilter.data) === null || _intentFilter$data3 === void 0 ? true : delete _intentFilter$data3[dataKey]; 230 } 231 } 232 } 233 } 234 break; 235 } 236 } 237 } 238 return androidManifest; 239} 240//# sourceMappingURL=Scheme.js.map