AndroidManifest.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. var fs = require('fs');
  18. var xml = require('cordova-common').xmlHelpers;
  19. var DEFAULT_ORIENTATION = 'default';
  20. /** Wraps an AndroidManifest file */
  21. function AndroidManifest (path) {
  22. this.path = path;
  23. this.doc = xml.parseElementtreeSync(path);
  24. if (this.doc.getroot().tag !== 'manifest') {
  25. throw new Error('AndroidManifest at ' + path + ' has incorrect root node name (expected "manifest")');
  26. }
  27. }
  28. AndroidManifest.prototype.getVersionName = function () {
  29. return this.doc.getroot().attrib['android:versionName'];
  30. };
  31. AndroidManifest.prototype.setVersionName = function (versionName) {
  32. this.doc.getroot().attrib['android:versionName'] = versionName;
  33. return this;
  34. };
  35. AndroidManifest.prototype.getVersionCode = function () {
  36. return this.doc.getroot().attrib['android:versionCode'];
  37. };
  38. AndroidManifest.prototype.setVersionCode = function (versionCode) {
  39. this.doc.getroot().attrib['android:versionCode'] = versionCode;
  40. return this;
  41. };
  42. AndroidManifest.prototype.getPackageId = function () {
  43. return this.doc.getroot().attrib['package'];
  44. };
  45. AndroidManifest.prototype.setPackageId = function (pkgId) {
  46. this.doc.getroot().attrib['package'] = pkgId;
  47. return this;
  48. };
  49. AndroidManifest.prototype.getActivity = function () {
  50. var activity = this.doc.getroot().find('./application/activity');
  51. return {
  52. getName: function () {
  53. return activity.attrib['android:name'];
  54. },
  55. setName: function (name) {
  56. if (!name) {
  57. delete activity.attrib['android:name'];
  58. } else {
  59. activity.attrib['android:name'] = name;
  60. }
  61. return this;
  62. },
  63. getOrientation: function () {
  64. return activity.attrib['android:screenOrientation'];
  65. },
  66. setOrientation: function (orientation) {
  67. if (!orientation || orientation.toLowerCase() === DEFAULT_ORIENTATION) {
  68. delete activity.attrib['android:screenOrientation'];
  69. } else {
  70. activity.attrib['android:screenOrientation'] = orientation;
  71. }
  72. return this;
  73. },
  74. getLaunchMode: function () {
  75. return activity.attrib['android:launchMode'];
  76. },
  77. setLaunchMode: function (launchMode) {
  78. if (!launchMode) {
  79. delete activity.attrib['android:launchMode'];
  80. } else {
  81. activity.attrib['android:launchMode'] = launchMode;
  82. }
  83. return this;
  84. }
  85. };
  86. };
  87. AndroidManifest.prototype.getDebuggable = function () {
  88. return this.doc.getroot().find('./application').attrib['android:debuggable'] === 'true';
  89. };
  90. AndroidManifest.prototype.setDebuggable = function (value) {
  91. var application = this.doc.getroot().find('./application');
  92. if (value) {
  93. application.attrib['android:debuggable'] = 'true';
  94. } else {
  95. // The default value is "false", so we can remove attribute at all.
  96. delete application.attrib['android:debuggable'];
  97. }
  98. return this;
  99. };
  100. /**
  101. * Writes manifest to disk syncronously. If filename is specified, then manifest
  102. * will be written to that file
  103. *
  104. * @param {String} [destPath] File to write manifest to. If omitted,
  105. * manifest will be written to file it has been read from.
  106. */
  107. AndroidManifest.prototype.write = function (destPath) {
  108. fs.writeFileSync(destPath || this.path, this.doc.write({ indent: 4 }), 'utf-8');
  109. };
  110. module.exports = AndroidManifest;