1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.getAndroidModFileProviders = getAndroidModFileProviders;
7exports.sortAndroidManifest = sortAndroidManifest;
8exports.withAndroidBaseMods = withAndroidBaseMods;
9function _fs() {
10  const data = require("fs");
11  _fs = function () {
12    return data;
13  };
14  return data;
15}
16function _path() {
17  const data = _interopRequireDefault(require("path"));
18  _path = function () {
19    return data;
20  };
21  return data;
22}
23function _android() {
24  const data = require("../android");
25  _android = function () {
26    return data;
27  };
28  return data;
29}
30function _XML() {
31  const data = require("../utils/XML");
32  _XML = function () {
33    return data;
34  };
35  return data;
36}
37function _sortObject() {
38  const data = require("../utils/sortObject");
39  _sortObject = function () {
40    return data;
41  };
42  return data;
43}
44function _createBaseMod() {
45  const data = require("./createBaseMod");
46  _createBaseMod = function () {
47    return data;
48  };
49  return data;
50}
51function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
52const {
53  readFile,
54  writeFile
55} = _fs().promises;
56function getAndroidManifestTemplate(config) {
57  var _config$android$packa, _config$android;
58  // Keep in sync with https://github.com/expo/expo/blob/master/templates/expo-template-bare-minimum/android/app/src/main/AndroidManifest.xml
59  // TODO: Read from remote template when possible
60  return (0, _XML().parseXMLAsync)(`
61  <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="${(_config$android$packa = (_config$android = config.android) === null || _config$android === void 0 ? void 0 : _config$android.package) !== null && _config$android$packa !== void 0 ? _config$android$packa : 'com.placeholder.appid'}">
62
63    <uses-permission android:name="android.permission.INTERNET"/>
64    <!-- OPTIONAL PERMISSIONS, REMOVE WHATEVER YOU DO NOT NEED -->
65    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
66    <!-- These require runtime permissions on M -->
67    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
68    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
69    <!-- END OPTIONAL PERMISSIONS -->
70
71    <queries>
72      <!-- Support checking for http(s) links via the Linking API -->
73      <intent>
74        <action android:name="android.intent.action.VIEW" />
75        <category android:name="android.intent.category.BROWSABLE" />
76        <data android:scheme="https" />
77      </intent>
78    </queries>
79
80    <application
81      android:name=".MainApplication"
82      android:label="@string/app_name"
83      android:icon="@mipmap/ic_launcher"
84      android:roundIcon="@mipmap/ic_launcher_round"
85      android:allowBackup="false"
86      android:theme="@style/AppTheme"
87      android:usesCleartextTraffic="true"
88    >
89      <meta-data android:name="expo.modules.updates.EXPO_UPDATE_URL" android:value="YOUR-APP-URL-HERE"/>
90      <meta-data android:name="expo.modules.updates.EXPO_SDK_VERSION" android:value="YOUR-APP-SDK-VERSION-HERE"/>
91      <activity
92        android:name=".MainActivity"
93        android:label="@string/app_name"
94        android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode"
95        android:launchMode="singleTask"
96        android:windowSoftInputMode="adjustResize"
97        android:theme="@style/Theme.App.SplashScreen"
98      >
99        <intent-filter>
100          <action android:name="android.intent.action.MAIN"/>
101          <category android:name="android.intent.category.LAUNCHER"/>
102        </intent-filter>
103      </activity>
104      <activity android:name="com.facebook.react.devsupport.DevSettingsActivity"/>
105    </application>
106  </manifest>
107  `);
108}
109function sortAndroidManifest(obj) {
110  if (obj.manifest) {
111    // Reverse sort so application is last and permissions are first
112    obj.manifest = (0, _sortObject().sortObject)(obj.manifest, _sortObject().reverseSortString);
113    if (Array.isArray(obj.manifest['uses-permission'])) {
114      // Sort permissions alphabetically
115      obj.manifest['uses-permission'].sort((a, b) => {
116        if (a.$['android:name'] < b.$['android:name']) return -1;
117        if (a.$['android:name'] > b.$['android:name']) return 1;
118        return 0;
119      });
120    }
121    if (Array.isArray(obj.manifest.application)) {
122      // reverse sort applications so activity is towards the end and meta-data is towards the front.
123      obj.manifest.application = obj.manifest.application.map(application => {
124        application = (0, _sortObject().sortObjWithOrder)(application, ['meta-data', 'service', 'activity']);
125        if (Array.isArray(application['meta-data'])) {
126          // Sort metadata alphabetically
127          application['meta-data'].sort((a, b) => {
128            if (a.$['android:name'] < b.$['android:name']) return -1;
129            if (a.$['android:name'] > b.$['android:name']) return 1;
130            return 0;
131          });
132        }
133        return application;
134      });
135    }
136  }
137  return obj;
138}
139const defaultProviders = {
140  dangerous: (0, _createBaseMod().provider)({
141    getFilePath() {
142      return '';
143    },
144    async read() {
145      return {
146        filePath: '',
147        modResults: {}
148      };
149    },
150    async write() {}
151  }),
152  // Append a rule to supply gradle.properties data to mods on `mods.android.gradleProperties`
153  manifest: (0, _createBaseMod().provider)({
154    isIntrospective: true,
155    getFilePath({
156      modRequest: {
157        platformProjectRoot
158      }
159    }) {
160      return _path().default.join(platformProjectRoot, 'app/src/main/AndroidManifest.xml');
161    },
162    async read(filePath, config) {
163      try {
164        return await _android().Manifest.readAndroidManifestAsync(filePath);
165      } catch (error) {
166        if (!config.modRequest.introspect) {
167          throw error;
168        }
169      }
170      return await getAndroidManifestTemplate(config);
171    },
172    async write(filePath, {
173      modResults,
174      modRequest: {
175        introspect
176      }
177    }) {
178      if (introspect) return;
179      await _android().Manifest.writeAndroidManifestAsync(filePath, sortAndroidManifest(modResults));
180    }
181  }),
182  // Append a rule to supply gradle.properties data to mods on `mods.android.gradleProperties`
183  gradleProperties: (0, _createBaseMod().provider)({
184    isIntrospective: true,
185    getFilePath({
186      modRequest: {
187        platformProjectRoot
188      }
189    }) {
190      return _path().default.join(platformProjectRoot, 'gradle.properties');
191    },
192    async read(filePath, config) {
193      try {
194        return await _android().Properties.parsePropertiesFile(await readFile(filePath, 'utf8'));
195      } catch (error) {
196        if (!config.modRequest.introspect) {
197          throw error;
198        }
199      }
200      return [];
201    },
202    async write(filePath, {
203      modResults,
204      modRequest: {
205        introspect
206      }
207    }) {
208      if (introspect) return;
209      await writeFile(filePath, _android().Properties.propertiesListToString(modResults));
210    }
211  }),
212  // Append a rule to supply strings.xml data to mods on `mods.android.strings`
213  strings: (0, _createBaseMod().provider)({
214    isIntrospective: true,
215    async getFilePath({
216      modRequest: {
217        projectRoot,
218        introspect
219      }
220    }) {
221      try {
222        return await _android().Strings.getProjectStringsXMLPathAsync(projectRoot);
223      } catch (error) {
224        if (!introspect) {
225          throw error;
226        }
227      }
228      return '';
229    },
230    async read(filePath, config) {
231      try {
232        return await _android().Resources.readResourcesXMLAsync({
233          path: filePath
234        });
235      } catch (error) {
236        if (!config.modRequest.introspect) {
237          throw error;
238        }
239      }
240      return {
241        resources: {}
242      };
243    },
244    async write(filePath, {
245      modResults,
246      modRequest: {
247        introspect
248      }
249    }) {
250      if (introspect) return;
251      await (0, _XML().writeXMLAsync)({
252        path: filePath,
253        xml: modResults
254      });
255    }
256  }),
257  colors: (0, _createBaseMod().provider)({
258    isIntrospective: true,
259    async getFilePath({
260      modRequest: {
261        projectRoot,
262        introspect
263      }
264    }) {
265      try {
266        return await _android().Colors.getProjectColorsXMLPathAsync(projectRoot);
267      } catch (error) {
268        if (!introspect) {
269          throw error;
270        }
271      }
272      return '';
273    },
274    async read(filePath, {
275      modRequest: {
276        introspect
277      }
278    }) {
279      try {
280        return await _android().Resources.readResourcesXMLAsync({
281          path: filePath
282        });
283      } catch (error) {
284        if (!introspect) {
285          throw error;
286        }
287      }
288      return {
289        resources: {}
290      };
291    },
292    async write(filePath, {
293      modResults,
294      modRequest: {
295        introspect
296      }
297    }) {
298      if (introspect) return;
299      await (0, _XML().writeXMLAsync)({
300        path: filePath,
301        xml: modResults
302      });
303    }
304  }),
305  colorsNight: (0, _createBaseMod().provider)({
306    isIntrospective: true,
307    async getFilePath({
308      modRequest: {
309        projectRoot,
310        introspect
311      }
312    }) {
313      try {
314        return await _android().Colors.getProjectColorsXMLPathAsync(projectRoot, {
315          kind: 'values-night'
316        });
317      } catch (error) {
318        if (!introspect) {
319          throw error;
320        }
321      }
322      return '';
323    },
324    async read(filePath, config) {
325      try {
326        return await _android().Resources.readResourcesXMLAsync({
327          path: filePath
328        });
329      } catch (error) {
330        if (!config.modRequest.introspect) {
331          throw error;
332        }
333      }
334      return {
335        resources: {}
336      };
337    },
338    async write(filePath, {
339      modResults,
340      modRequest: {
341        introspect
342      }
343    }) {
344      if (introspect) return;
345      await (0, _XML().writeXMLAsync)({
346        path: filePath,
347        xml: modResults
348      });
349    }
350  }),
351  styles: (0, _createBaseMod().provider)({
352    isIntrospective: true,
353    async getFilePath({
354      modRequest: {
355        projectRoot,
356        introspect
357      }
358    }) {
359      try {
360        return await _android().Styles.getProjectStylesXMLPathAsync(projectRoot);
361      } catch (error) {
362        if (!introspect) {
363          throw error;
364        }
365      }
366      return '';
367    },
368    async read(filePath, config) {
369      var _styles$resources$$;
370      let styles = {
371        resources: {}
372      };
373      try {
374        // Adds support for `tools:x`
375        styles = await _android().Resources.readResourcesXMLAsync({
376          path: filePath,
377          fallback: `<?xml version="1.0" encoding="utf-8"?><resources xmlns:tools="http://schemas.android.com/tools"></resources>`
378        });
379      } catch (error) {
380        if (!config.modRequest.introspect) {
381          throw error;
382        }
383      }
384
385      // Ensure support for tools is added...
386      if (!styles.resources.$) {
387        styles.resources.$ = {};
388      }
389      if (!((_styles$resources$$ = styles.resources.$) !== null && _styles$resources$$ !== void 0 && _styles$resources$$['xmlns:tools'])) {
390        styles.resources.$['xmlns:tools'] = 'http://schemas.android.com/tools';
391      }
392      return styles;
393    },
394    async write(filePath, {
395      modResults,
396      modRequest: {
397        introspect
398      }
399    }) {
400      if (introspect) return;
401      await (0, _XML().writeXMLAsync)({
402        path: filePath,
403        xml: modResults
404      });
405    }
406  }),
407  projectBuildGradle: (0, _createBaseMod().provider)({
408    getFilePath({
409      modRequest: {
410        projectRoot
411      }
412    }) {
413      return _android().Paths.getProjectBuildGradleFilePath(projectRoot);
414    },
415    async read(filePath) {
416      return _android().Paths.getFileInfo(filePath);
417    },
418    async write(filePath, {
419      modResults: {
420        contents
421      }
422    }) {
423      await writeFile(filePath, contents);
424    }
425  }),
426  settingsGradle: (0, _createBaseMod().provider)({
427    getFilePath({
428      modRequest: {
429        projectRoot
430      }
431    }) {
432      return _android().Paths.getSettingsGradleFilePath(projectRoot);
433    },
434    async read(filePath) {
435      return _android().Paths.getFileInfo(filePath);
436    },
437    async write(filePath, {
438      modResults: {
439        contents
440      }
441    }) {
442      await writeFile(filePath, contents);
443    }
444  }),
445  appBuildGradle: (0, _createBaseMod().provider)({
446    getFilePath({
447      modRequest: {
448        projectRoot
449      }
450    }) {
451      return _android().Paths.getAppBuildGradleFilePath(projectRoot);
452    },
453    async read(filePath) {
454      return _android().Paths.getFileInfo(filePath);
455    },
456    async write(filePath, {
457      modResults: {
458        contents
459      }
460    }) {
461      await writeFile(filePath, contents);
462    }
463  }),
464  mainActivity: (0, _createBaseMod().provider)({
465    getFilePath({
466      modRequest: {
467        projectRoot
468      }
469    }) {
470      return _android().Paths.getProjectFilePath(projectRoot, 'MainActivity');
471    },
472    async read(filePath) {
473      return _android().Paths.getFileInfo(filePath);
474    },
475    async write(filePath, {
476      modResults: {
477        contents
478      }
479    }) {
480      await writeFile(filePath, contents);
481    }
482  }),
483  mainApplication: (0, _createBaseMod().provider)({
484    getFilePath({
485      modRequest: {
486        projectRoot
487      }
488    }) {
489      return _android().Paths.getProjectFilePath(projectRoot, 'MainApplication');
490    },
491    async read(filePath) {
492      return _android().Paths.getFileInfo(filePath);
493    },
494    async write(filePath, {
495      modResults: {
496        contents
497      }
498    }) {
499      await writeFile(filePath, contents);
500    }
501  })
502};
503function withAndroidBaseMods(config, {
504  providers,
505  ...props
506} = {}) {
507  return (0, _createBaseMod().withGeneratedBaseMods)(config, {
508    ...props,
509    platform: 'android',
510    providers: providers !== null && providers !== void 0 ? providers : getAndroidModFileProviders()
511  });
512}
513function getAndroidModFileProviders() {
514  return defaultProviders;
515}
516//# sourceMappingURL=withAndroidBaseMods.js.map