Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

prepare.js 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /**
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. /* eslint no-useless-escape: 0 */
  18. var Q = require('q');
  19. var fs = require('fs');
  20. var path = require('path');
  21. var shell = require('shelljs');
  22. var events = require('cordova-common').events;
  23. var AndroidManifest = require('./AndroidManifest');
  24. var checkReqs = require('./check_reqs');
  25. var xmlHelpers = require('cordova-common').xmlHelpers;
  26. var CordovaError = require('cordova-common').CordovaError;
  27. var ConfigParser = require('cordova-common').ConfigParser;
  28. var FileUpdater = require('cordova-common').FileUpdater;
  29. var PlatformJson = require('cordova-common').PlatformJson;
  30. var PlatformMunger = require('cordova-common').ConfigChanges.PlatformMunger;
  31. var PluginInfoProvider = require('cordova-common').PluginInfoProvider;
  32. const GradlePropertiesParser = require('./config/GradlePropertiesParser');
  33. module.exports.prepare = function (cordovaProject, options) {
  34. var self = this;
  35. var platformJson = PlatformJson.load(this.locations.root, this.platform);
  36. var munger = new PlatformMunger(this.platform, this.locations.root, platformJson, new PluginInfoProvider());
  37. this._config = updateConfigFilesFrom(cordovaProject.projectConfig, munger, this.locations);
  38. // Get the min SDK version from config.xml
  39. const minSdkVersion = this._config.getPreference('android-minSdkVersion', 'android');
  40. const maxSdkVersion = this._config.getPreference('android-maxSdkVersion', 'android');
  41. const targetSdkVersion = this._config.getPreference('android-targetSdkVersion', 'android');
  42. let gradlePropertiesUserConfig = {};
  43. if (minSdkVersion) gradlePropertiesUserConfig.cdvMinSdkVersion = minSdkVersion;
  44. if (maxSdkVersion) gradlePropertiesUserConfig.cdvMaxSdkVersion = maxSdkVersion;
  45. if (targetSdkVersion) gradlePropertiesUserConfig.cdvTargetSdkVersion = targetSdkVersion;
  46. let gradlePropertiesParser = new GradlePropertiesParser(this.locations.root);
  47. gradlePropertiesParser.configure(gradlePropertiesUserConfig);
  48. // Update own www dir with project's www assets and plugins' assets and js-files
  49. return Q.when(updateWww(cordovaProject, this.locations)).then(function () {
  50. // update project according to config.xml changes.
  51. return updateProjectAccordingTo(self._config, self.locations);
  52. }).then(function () {
  53. updateIcons(cordovaProject, path.relative(cordovaProject.root, self.locations.res));
  54. updateSplashes(cordovaProject, path.relative(cordovaProject.root, self.locations.res));
  55. updateFileResources(cordovaProject, path.relative(cordovaProject.root, self.locations.root));
  56. }).then(function () {
  57. events.emit('verbose', 'Prepared android project successfully');
  58. });
  59. };
  60. module.exports.clean = function (options) {
  61. // A cordovaProject isn't passed into the clean() function, because it might have
  62. // been called from the platform shell script rather than the CLI. Check for the
  63. // noPrepare option passed in by the non-CLI clean script. If that's present, or if
  64. // there's no config.xml found at the project root, then don't clean prepared files.
  65. var projectRoot = path.resolve(this.root, '../..');
  66. if ((options && options.noPrepare) || !fs.existsSync(this.locations.configXml) ||
  67. !fs.existsSync(this.locations.configXml)) {
  68. return Q();
  69. }
  70. var projectConfig = new ConfigParser(this.locations.configXml);
  71. var self = this;
  72. return Q().then(function () {
  73. cleanWww(projectRoot, self.locations);
  74. cleanIcons(projectRoot, projectConfig, path.relative(projectRoot, self.locations.res));
  75. cleanSplashes(projectRoot, projectConfig, path.relative(projectRoot, self.locations.res));
  76. cleanFileResources(projectRoot, projectConfig, path.relative(projectRoot, self.locations.root));
  77. });
  78. };
  79. /**
  80. * Updates config files in project based on app's config.xml and config munge,
  81. * generated by plugins.
  82. *
  83. * @param {ConfigParser} sourceConfig A project's configuration that will
  84. * be merged into platform's config.xml
  85. * @param {ConfigChanges} configMunger An initialized ConfigChanges instance
  86. * for this platform.
  87. * @param {Object} locations A map of locations for this platform
  88. *
  89. * @return {ConfigParser} An instance of ConfigParser, that
  90. * represents current project's configuration. When returned, the
  91. * configuration is already dumped to appropriate config.xml file.
  92. */
  93. function updateConfigFilesFrom (sourceConfig, configMunger, locations) {
  94. events.emit('verbose', 'Generating platform-specific config.xml from defaults for android at ' + locations.configXml);
  95. // First cleanup current config and merge project's one into own
  96. // Overwrite platform config.xml with defaults.xml.
  97. shell.cp('-f', locations.defaultConfigXml, locations.configXml);
  98. // Then apply config changes from global munge to all config files
  99. // in project (including project's config)
  100. configMunger.reapply_global_munge().save_all();
  101. events.emit('verbose', 'Merging project\'s config.xml into platform-specific android config.xml');
  102. // Merge changes from app's config.xml into platform's one
  103. var config = new ConfigParser(locations.configXml);
  104. xmlHelpers.mergeXml(sourceConfig.doc.getroot(),
  105. config.doc.getroot(), 'android', /* clobber= */true);
  106. config.write();
  107. return config;
  108. }
  109. /**
  110. * Logs all file operations via the verbose event stream, indented.
  111. */
  112. function logFileOp (message) {
  113. events.emit('verbose', ' ' + message);
  114. }
  115. /**
  116. * Updates platform 'www' directory by replacing it with contents of
  117. * 'platform_www' and app www. Also copies project's overrides' folder into
  118. * the platform 'www' folder
  119. *
  120. * @param {Object} cordovaProject An object which describes cordova project.
  121. * @param {Object} destinations An object that contains destination
  122. * paths for www files.
  123. */
  124. function updateWww (cordovaProject, destinations) {
  125. var sourceDirs = [
  126. path.relative(cordovaProject.root, cordovaProject.locations.www),
  127. path.relative(cordovaProject.root, destinations.platformWww)
  128. ];
  129. // If project contains 'merges' for our platform, use them as another overrides
  130. var merges_path = path.join(cordovaProject.root, 'merges', 'android');
  131. if (fs.existsSync(merges_path)) {
  132. events.emit('verbose', 'Found "merges/android" folder. Copying its contents into the android project.');
  133. sourceDirs.push(path.join('merges', 'android'));
  134. }
  135. var targetDir = path.relative(cordovaProject.root, destinations.www);
  136. events.emit(
  137. 'verbose', 'Merging and updating files from [' + sourceDirs.join(', ') + '] to ' + targetDir);
  138. FileUpdater.mergeAndUpdateDir(
  139. sourceDirs, targetDir, { rootDir: cordovaProject.root }, logFileOp);
  140. }
  141. /**
  142. * Cleans all files from the platform 'www' directory.
  143. */
  144. function cleanWww (projectRoot, locations) {
  145. var targetDir = path.relative(projectRoot, locations.www);
  146. events.emit('verbose', 'Cleaning ' + targetDir);
  147. // No source paths are specified, so mergeAndUpdateDir() will clear the target directory.
  148. FileUpdater.mergeAndUpdateDir(
  149. [], targetDir, { rootDir: projectRoot, all: true }, logFileOp);
  150. }
  151. /**
  152. * Updates project structure and AndroidManifest according to project's configuration.
  153. *
  154. * @param {ConfigParser} platformConfig A project's configuration that will
  155. * be used to update project
  156. * @param {Object} locations A map of locations for this platform
  157. */
  158. function updateProjectAccordingTo (platformConfig, locations) {
  159. // Update app name by editing res/values/strings.xml
  160. var strings = xmlHelpers.parseElementtreeSync(locations.strings);
  161. var name = platformConfig.name();
  162. strings.find('string[@name="app_name"]').text = name.replace(/\'/g, '\\\'');
  163. var shortName = platformConfig.shortName && platformConfig.shortName();
  164. if (shortName && shortName !== name) {
  165. strings.find('string[@name="launcher_name"]').text = shortName.replace(/\'/g, '\\\'');
  166. }
  167. fs.writeFileSync(locations.strings, strings.write({ indent: 4 }), 'utf-8');
  168. events.emit('verbose', 'Wrote out android application name "' + name + '" to ' + locations.strings);
  169. // Java packages cannot support dashes
  170. var androidPkgName = (platformConfig.android_packageName() || platformConfig.packageName()).replace(/-/g, '_');
  171. var manifest = new AndroidManifest(locations.manifest);
  172. var manifestId = manifest.getPackageId();
  173. manifest.getActivity()
  174. .setOrientation(platformConfig.getPreference('orientation'))
  175. .setLaunchMode(findAndroidLaunchModePreference(platformConfig));
  176. manifest.setVersionName(platformConfig.version())
  177. .setVersionCode(platformConfig.android_versionCode() || default_versionCode(platformConfig.version()))
  178. .setPackageId(androidPkgName)
  179. .write();
  180. // Java file paths shouldn't be hard coded
  181. var javaPattern = path.join(locations.javaSrc, manifestId.replace(/\./g, '/'), '*.java');
  182. var java_files = shell.ls(javaPattern).filter(function (f) {
  183. return shell.grep(/extends\s+CordovaActivity/g, f);
  184. });
  185. if (java_files.length === 0) {
  186. throw new CordovaError('No Java files found that extend CordovaActivity.');
  187. } else if (java_files.length > 1) {
  188. events.emit('log', 'Multiple candidate Java files that extend CordovaActivity found. Guessing at the first one, ' + java_files[0]);
  189. }
  190. var destFile = path.join(locations.root, 'app', 'src', 'main', 'java', androidPkgName.replace(/\./g, '/'), path.basename(java_files[0]));
  191. shell.mkdir('-p', path.dirname(destFile));
  192. shell.sed(/package [\w\.]*;/, 'package ' + androidPkgName + ';', java_files[0]).to(destFile);
  193. events.emit('verbose', 'Wrote out Android package name "' + androidPkgName + '" to ' + destFile);
  194. var removeOrigPkg = checkReqs.isWindows() || checkReqs.isDarwin() ?
  195. manifestId.toUpperCase() !== androidPkgName.toUpperCase() :
  196. manifestId !== androidPkgName;
  197. if (removeOrigPkg) {
  198. // If package was name changed we need to remove old java with main activity
  199. shell.rm('-Rf', java_files[0]);
  200. // remove any empty directories
  201. var currentDir = path.dirname(java_files[0]);
  202. var sourcesRoot = path.resolve(locations.root, 'src');
  203. while (currentDir !== sourcesRoot) {
  204. if (fs.existsSync(currentDir) && fs.readdirSync(currentDir).length === 0) {
  205. fs.rmdirSync(currentDir);
  206. currentDir = path.resolve(currentDir, '..');
  207. } else {
  208. break;
  209. }
  210. }
  211. }
  212. }
  213. // Consturct the default value for versionCode as
  214. // PATCH + MINOR * 100 + MAJOR * 10000
  215. // see http://developer.android.com/tools/publishing/versioning.html
  216. function default_versionCode (version) {
  217. var nums = version.split('-')[0].split('.');
  218. var versionCode = 0;
  219. if (+nums[0]) {
  220. versionCode += +nums[0] * 10000;
  221. }
  222. if (+nums[1]) {
  223. versionCode += +nums[1] * 100;
  224. }
  225. if (+nums[2]) {
  226. versionCode += +nums[2];
  227. }
  228. events.emit('verbose', 'android-versionCode not found in config.xml. Generating a code based on version in config.xml (' + version + '): ' + versionCode);
  229. return versionCode;
  230. }
  231. function getImageResourcePath (resourcesDir, type, density, name, sourceName) {
  232. if (/\.9\.png$/.test(sourceName)) {
  233. name = name.replace(/\.png$/, '.9.png');
  234. }
  235. var resourcePath = path.join(resourcesDir, (density ? type + '-' + density : type), name);
  236. return resourcePath;
  237. }
  238. function getAdaptiveImageResourcePath (resourcesDir, type, density, name, sourceName) {
  239. if (/\.9\.png$/.test(sourceName)) {
  240. name = name.replace(/\.png$/, '.9.png');
  241. }
  242. var resourcePath = path.join(resourcesDir, (density ? type + '-' + density + '-v26' : type), name);
  243. return resourcePath;
  244. }
  245. function updateSplashes (cordovaProject, platformResourcesDir) {
  246. var resources = cordovaProject.projectConfig.getSplashScreens('android');
  247. // if there are "splash" elements in config.xml
  248. if (resources.length === 0) {
  249. events.emit('verbose', 'This app does not have splash screens defined');
  250. return;
  251. }
  252. var resourceMap = mapImageResources(cordovaProject.root, platformResourcesDir, 'drawable', 'screen.png');
  253. var hadMdpi = false;
  254. resources.forEach(function (resource) {
  255. if (!resource.density) {
  256. return;
  257. }
  258. if (resource.density === 'mdpi') {
  259. hadMdpi = true;
  260. }
  261. var targetPath = getImageResourcePath(
  262. platformResourcesDir, 'drawable', resource.density, 'screen.png', path.basename(resource.src));
  263. resourceMap[targetPath] = resource.src;
  264. });
  265. // There's no "default" drawable, so assume default == mdpi.
  266. if (!hadMdpi && resources.defaultResource) {
  267. var targetPath = getImageResourcePath(
  268. platformResourcesDir, 'drawable', 'mdpi', 'screen.png', path.basename(resources.defaultResource.src));
  269. resourceMap[targetPath] = resources.defaultResource.src;
  270. }
  271. events.emit('verbose', 'Updating splash screens at ' + platformResourcesDir);
  272. FileUpdater.updatePaths(
  273. resourceMap, { rootDir: cordovaProject.root }, logFileOp);
  274. }
  275. function cleanSplashes (projectRoot, projectConfig, platformResourcesDir) {
  276. var resources = projectConfig.getSplashScreens('android');
  277. if (resources.length > 0) {
  278. var resourceMap = mapImageResources(projectRoot, platformResourcesDir, 'drawable', 'screen.png');
  279. events.emit('verbose', 'Cleaning splash screens at ' + platformResourcesDir);
  280. // No source paths are specified in the map, so updatePaths() will delete the target files.
  281. FileUpdater.updatePaths(
  282. resourceMap, { rootDir: projectRoot, all: true }, logFileOp);
  283. }
  284. }
  285. function updateIcons (cordovaProject, platformResourcesDir) {
  286. let icons = cordovaProject.projectConfig.getIcons('android');
  287. // Skip if there are no app defined icons in config.xml
  288. if (icons.length === 0) {
  289. events.emit('verbose', 'This app does not have launcher icons defined');
  290. return;
  291. }
  292. // 1. loop icons determin if there is an error in the setup.
  293. // 2. during initial loop, also setup for legacy support.
  294. let errorMissingAttributes = [];
  295. let errorLegacyIconNeeded = [];
  296. let hasAdaptive = false;
  297. icons.forEach((icon, key) => {
  298. if (
  299. (icon.background && !icon.foreground)
  300. || (!icon.background && icon.foreground)
  301. || (!icon.background && !icon.foreground && !icon.src)
  302. ) {
  303. errorMissingAttributes.push(icon.density ? icon.density : 'size=' + (icon.height || icon.width));
  304. }
  305. if (icon.foreground) {
  306. hasAdaptive = true;
  307. if (
  308. !icon.src
  309. && (
  310. icon.foreground.startsWith('@color')
  311. || path.extname(path.basename(icon.foreground)) === '.xml'
  312. )
  313. ) {
  314. errorLegacyIconNeeded.push(icon.density ? icon.density : 'size=' + (icon.height || icon.width));
  315. } else if (!icon.src) {
  316. icons[key].src = icon.foreground;
  317. }
  318. }
  319. });
  320. let errorMessage = [];
  321. if (errorMissingAttributes.length > 0) {
  322. errorMessage.push('One of the following attributes are set but missing the other for the density type: ' + errorMissingAttributes.join(', ') + '. Please ensure that all require attributes are defined.');
  323. }
  324. if (errorLegacyIconNeeded.length > 0) {
  325. errorMessage.push('For the following icons with the density of: ' + errorLegacyIconNeeded.join(', ') + ', adaptive foreground with a defined color or vector can not be used as a standard fallback icon for older Android devices. To support older Android environments, please provide a value for the src attribute.');
  326. }
  327. if (errorMessage.length > 0) {
  328. throw new CordovaError(errorMessage.join(' '));
  329. }
  330. let resourceMap = Object.assign(
  331. {},
  332. mapImageResources(cordovaProject.root, platformResourcesDir, 'mipmap', 'ic_launcher.png'),
  333. mapImageResources(cordovaProject.root, platformResourcesDir, 'mipmap', 'ic_launcher_foreground.png'),
  334. mapImageResources(cordovaProject.root, platformResourcesDir, 'mipmap', 'ic_launcher_background.png'),
  335. mapImageResources(cordovaProject.root, platformResourcesDir, 'mipmap', 'ic_launcher_foreground.xml'),
  336. mapImageResources(cordovaProject.root, platformResourcesDir, 'mipmap', 'ic_launcher_background.xml'),
  337. mapImageResources(cordovaProject.root, platformResourcesDir, 'mipmap', 'ic_launcher.xml')
  338. );
  339. let preparedIcons = prepareIcons(icons);
  340. if (hasAdaptive) {
  341. resourceMap = updateIconResourceForAdaptive(preparedIcons, resourceMap, platformResourcesDir);
  342. }
  343. resourceMap = updateIconResourceForLegacy(preparedIcons, resourceMap, platformResourcesDir);
  344. events.emit('verbose', 'Updating icons at ' + platformResourcesDir);
  345. FileUpdater.updatePaths(resourceMap, { rootDir: cordovaProject.root }, logFileOp);
  346. }
  347. function updateIconResourceForAdaptive (preparedIcons, resourceMap, platformResourcesDir) {
  348. let android_icons = preparedIcons.android_icons;
  349. let default_icon = preparedIcons.default_icon;
  350. // The source paths for icons and splashes are relative to
  351. // project's config.xml location, so we use it as base path.
  352. let background;
  353. let foreground;
  354. let targetPathBackground;
  355. let targetPathForeground;
  356. for (let density in android_icons) {
  357. let backgroundVal = '@mipmap/ic_launcher_background';
  358. let foregroundVal = '@mipmap/ic_launcher_foreground';
  359. background = android_icons[density].background;
  360. foreground = android_icons[density].foreground;
  361. if (background.startsWith('@color')) {
  362. // Colors Use Case
  363. backgroundVal = background; // Example: @color/background_foobar_1
  364. } else if (path.extname(path.basename(background)) === '.xml') {
  365. // Vector Use Case
  366. targetPathBackground = getAdaptiveImageResourcePath(platformResourcesDir, 'mipmap', density, 'ic_launcher_background.xml', path.basename(android_icons[density].background));
  367. resourceMap[targetPathBackground] = android_icons[density].background;
  368. } else if (path.extname(path.basename(background)) === '.png') {
  369. // Images Use Case
  370. targetPathBackground = getAdaptiveImageResourcePath(platformResourcesDir, 'mipmap', density, 'ic_launcher_background.png', path.basename(android_icons[density].background));
  371. resourceMap[targetPathBackground] = android_icons[density].background;
  372. }
  373. if (foreground.startsWith('@color')) {
  374. // Colors Use Case
  375. foregroundVal = foreground;
  376. } else if (path.extname(path.basename(foreground)) === '.xml') {
  377. // Vector Use Case
  378. targetPathForeground = getAdaptiveImageResourcePath(platformResourcesDir, 'mipmap', density, 'ic_launcher_foreground.xml', path.basename(android_icons[density].foreground));
  379. resourceMap[targetPathForeground] = android_icons[density].foreground;
  380. } else if (path.extname(path.basename(foreground)) === '.png') {
  381. // Images Use Case
  382. targetPathForeground = getAdaptiveImageResourcePath(platformResourcesDir, 'mipmap', density, 'ic_launcher_foreground.png', path.basename(android_icons[density].foreground));
  383. resourceMap[targetPathForeground] = android_icons[density].foreground;
  384. }
  385. // create an XML for DPI and set color
  386. const icLauncherTemplate = `<?xml version="1.0" encoding="utf-8"?>
  387. <adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
  388. <background android:drawable="` + backgroundVal + `" />
  389. <foreground android:drawable="` + foregroundVal + `" />
  390. </adaptive-icon>`;
  391. let launcherXmlPath = path.join(platformResourcesDir, 'mipmap-' + density + '-v26', 'ic_launcher.xml');
  392. // Remove the XML from the resourceMap so the file does not get removed.
  393. delete resourceMap[launcherXmlPath];
  394. fs.writeFileSync(path.resolve(launcherXmlPath), icLauncherTemplate);
  395. }
  396. // There's no "default" drawable, so assume default == mdpi.
  397. if (default_icon && !android_icons.mdpi) {
  398. let defaultTargetPathBackground;
  399. let defaultTargetPathForeground;
  400. if (background.startsWith('@color')) {
  401. // Colors Use Case
  402. targetPathBackground = default_icon.background;
  403. } else if (path.extname(path.basename(background)) === '.xml') {
  404. // Vector Use Case
  405. defaultTargetPathBackground = getAdaptiveImageResourcePath(platformResourcesDir, 'mipmap', 'mdpi', 'ic_launcher_background.xml', path.basename(default_icon.background));
  406. resourceMap[defaultTargetPathBackground] = default_icon.background;
  407. } else if (path.extname(path.basename(background)) === '.png') {
  408. // Images Use Case
  409. defaultTargetPathBackground = getAdaptiveImageResourcePath(platformResourcesDir, 'mipmap', 'mdpi', 'ic_launcher_background.png', path.basename(default_icon.background));
  410. resourceMap[defaultTargetPathBackground] = default_icon.background;
  411. }
  412. if (foreground.startsWith('@color')) {
  413. // Colors Use Case
  414. targetPathForeground = default_icon.foreground;
  415. } else if (path.extname(path.basename(foreground)) === '.xml') {
  416. // Vector Use Case
  417. defaultTargetPathForeground = getAdaptiveImageResourcePath(platformResourcesDir, 'mipmap', 'mdpi', 'ic_launcher_foreground.xml', path.basename(default_icon.foreground));
  418. resourceMap[defaultTargetPathForeground] = default_icon.foreground;
  419. } else if (path.extname(path.basename(foreground)) === '.png') {
  420. // Images Use Case
  421. defaultTargetPathForeground = getAdaptiveImageResourcePath(platformResourcesDir, 'mipmap', 'mdpi', 'ic_launcher_foreground.png', path.basename(default_icon.foreground));
  422. resourceMap[defaultTargetPathForeground] = default_icon.foreground;
  423. }
  424. }
  425. return resourceMap;
  426. }
  427. function updateIconResourceForLegacy (preparedIcons, resourceMap, platformResourcesDir) {
  428. let android_icons = preparedIcons.android_icons;
  429. let default_icon = preparedIcons.default_icon;
  430. // The source paths for icons and splashes are relative to
  431. // project's config.xml location, so we use it as base path.
  432. for (var density in android_icons) {
  433. var targetPath = getImageResourcePath(platformResourcesDir, 'mipmap', density, 'ic_launcher.png', path.basename(android_icons[density].src));
  434. resourceMap[targetPath] = android_icons[density].src;
  435. }
  436. // There's no "default" drawable, so assume default == mdpi.
  437. if (default_icon && !android_icons.mdpi) {
  438. var defaultTargetPath = getImageResourcePath(platformResourcesDir, 'mipmap', 'mdpi', 'ic_launcher.png', path.basename(default_icon.src));
  439. resourceMap[defaultTargetPath] = default_icon.src;
  440. }
  441. return resourceMap;
  442. }
  443. function prepareIcons (icons) {
  444. // http://developer.android.com/design/style/iconography.html
  445. const SIZE_TO_DENSITY_MAP = {
  446. 36: 'ldpi',
  447. 48: 'mdpi',
  448. 72: 'hdpi',
  449. 96: 'xhdpi',
  450. 144: 'xxhdpi',
  451. 192: 'xxxhdpi'
  452. };
  453. let android_icons = {};
  454. let default_icon;
  455. // find the best matching icon for a given density or size
  456. // @output android_icons
  457. var parseIcon = function (icon, icon_size) {
  458. // do I have a platform icon for that density already
  459. var density = icon.density || SIZE_TO_DENSITY_MAP[icon_size];
  460. if (!density) {
  461. // invalid icon defition ( or unsupported size)
  462. return;
  463. }
  464. var previous = android_icons[density];
  465. if (previous && previous.platform) {
  466. return;
  467. }
  468. android_icons[density] = icon;
  469. };
  470. // iterate over all icon elements to find the default icon and call parseIcon
  471. for (var i = 0; i < icons.length; i++) {
  472. var icon = icons[i];
  473. var size = icon.width;
  474. if (!size) {
  475. size = icon.height;
  476. }
  477. if (!size && !icon.density) {
  478. if (default_icon) {
  479. let found = {};
  480. let favor = {};
  481. // populating found icon.
  482. if (icon.background && icon.foreground) {
  483. found.background = icon.background;
  484. found.foreground = icon.foreground;
  485. }
  486. if (icon.src) {
  487. found.src = icon.src;
  488. }
  489. if (default_icon.background && default_icon.foreground) {
  490. favor.background = default_icon.background;
  491. favor.foreground = default_icon.foreground;
  492. }
  493. if (default_icon.src) {
  494. favor.src = default_icon.src;
  495. }
  496. events.emit('verbose', 'Found extra default icon: ' + JSON.stringify(found) + ' and ignoring in favor of ' + JSON.stringify(favor) + '.');
  497. } else {
  498. default_icon = icon;
  499. }
  500. } else {
  501. parseIcon(icon, size);
  502. }
  503. }
  504. return {
  505. android_icons: android_icons,
  506. default_icon: default_icon
  507. };
  508. }
  509. function cleanIcons (projectRoot, projectConfig, platformResourcesDir) {
  510. var icons = projectConfig.getIcons('android');
  511. // Skip if there are no app defined icons in config.xml
  512. if (icons.length === 0) {
  513. events.emit('verbose', 'This app does not have launcher icons defined');
  514. return;
  515. }
  516. let resourceMap = Object.assign(
  517. {},
  518. mapImageResources(projectRoot, platformResourcesDir, 'mipmap', 'ic_launcher.png'),
  519. mapImageResources(projectRoot, platformResourcesDir, 'mipmap', 'ic_launcher_foreground.png'),
  520. mapImageResources(projectRoot, platformResourcesDir, 'mipmap', 'ic_launcher_background.png'),
  521. mapImageResources(projectRoot, platformResourcesDir, 'mipmap', 'ic_launcher_foreground.xml'),
  522. mapImageResources(projectRoot, platformResourcesDir, 'mipmap', 'ic_launcher_background.xml'),
  523. mapImageResources(projectRoot, platformResourcesDir, 'mipmap', 'ic_launcher.xml')
  524. );
  525. events.emit('verbose', 'Cleaning icons at ' + platformResourcesDir);
  526. // No source paths are specified in the map, so updatePaths() will delete the target files.
  527. FileUpdater.updatePaths(resourceMap, { rootDir: projectRoot, all: true }, logFileOp);
  528. }
  529. /**
  530. * Gets a map containing resources of a specified name from all drawable folders in a directory.
  531. */
  532. function mapImageResources (rootDir, subDir, type, resourceName) {
  533. var pathMap = {};
  534. shell.ls(path.join(rootDir, subDir, type + '-*')).forEach(function (drawableFolder) {
  535. var imagePath = path.join(subDir, path.basename(drawableFolder), resourceName);
  536. pathMap[imagePath] = null;
  537. });
  538. return pathMap;
  539. }
  540. function updateFileResources (cordovaProject, platformDir) {
  541. var files = cordovaProject.projectConfig.getFileResources('android');
  542. // if there are resource-file elements in config.xml
  543. if (files.length === 0) {
  544. events.emit('verbose', 'This app does not have additional resource files defined');
  545. return;
  546. }
  547. var resourceMap = {};
  548. files.forEach(function (res) {
  549. var targetPath = path.join(platformDir, res.target);
  550. resourceMap[targetPath] = res.src;
  551. });
  552. events.emit('verbose', 'Updating resource files at ' + platformDir);
  553. FileUpdater.updatePaths(
  554. resourceMap, { rootDir: cordovaProject.root }, logFileOp);
  555. }
  556. function cleanFileResources (projectRoot, projectConfig, platformDir) {
  557. var files = projectConfig.getFileResources('android', true);
  558. if (files.length > 0) {
  559. events.emit('verbose', 'Cleaning resource files at ' + platformDir);
  560. var resourceMap = {};
  561. files.forEach(function (res) {
  562. var filePath = path.join(platformDir, res.target);
  563. resourceMap[filePath] = null;
  564. });
  565. FileUpdater.updatePaths(
  566. resourceMap, {
  567. rootDir: projectRoot, all: true }, logFileOp);
  568. }
  569. }
  570. /**
  571. * Gets and validates 'AndroidLaunchMode' prepference from config.xml. Returns
  572. * preference value and warns if it doesn't seems to be valid
  573. *
  574. * @param {ConfigParser} platformConfig A configParser instance for
  575. * platform.
  576. *
  577. * @return {String} Preference's value from config.xml or
  578. * default value, if there is no such preference. The default value is
  579. * 'singleTop'
  580. */
  581. function findAndroidLaunchModePreference (platformConfig) {
  582. var launchMode = platformConfig.getPreference('AndroidLaunchMode');
  583. if (!launchMode) {
  584. // Return a default value
  585. return 'singleTop';
  586. }
  587. var expectedValues = ['standard', 'singleTop', 'singleTask', 'singleInstance'];
  588. var valid = expectedValues.indexOf(launchMode) >= 0;
  589. if (!valid) {
  590. // Note: warn, but leave the launch mode as developer wanted, in case the list of options changes in the future
  591. events.emit('warn', 'Unrecognized value for AndroidLaunchMode preference: ' +
  592. launchMode + '. Expected values are: ' + expectedValues.join(', '));
  593. }
  594. return launchMode;
  595. }