commonroutesServer/www/platforms/android/cordova/lib/AndroidManifest.js
www/platforms/android/cordova/lib/AndroidManifest.jsRaw
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
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18*/
19
20var fs = require('fs');
21var et = require('elementtree');
22var xml= require('cordova-common').xmlHelpers;
23
24var DEFAULT_ORIENTATION = 'default';
25
26/** Wraps an AndroidManifest file */
27function AndroidManifest(path) {
28 this.path = path;
29 this.doc = xml.parseElementtreeSync(path);
30 if (this.doc.getroot().tag !== 'manifest') {
31 throw new Error('AndroidManifest at ' + path + ' has incorrect root node name (expected "manifest")');
32 }
33}
34
35AndroidManifest.prototype.getVersionName = function() {
36 return this.doc.getroot().attrib['android:versionName'];
37};
38
39AndroidManifest.prototype.setVersionName = function(versionName) {
40 this.doc.getroot().attrib['android:versionName'] = versionName;
41 return this;
42};
43
44AndroidManifest.prototype.getVersionCode = function() {
45 return this.doc.getroot().attrib['android:versionCode'];
46};
47
48AndroidManifest.prototype.setVersionCode = function(versionCode) {
49 this.doc.getroot().attrib['android:versionCode'] = versionCode;
50 return this;
51};
52
53AndroidManifest.prototype.getPackageId = function() {
54 /*jshint -W069 */
55 return this.doc.getroot().attrib['package'];
56 /*jshint +W069 */
57};
58
59AndroidManifest.prototype.setPackageId = function(pkgId) {
60 /*jshint -W069 */
61 this.doc.getroot().attrib['package'] = pkgId;
62 /*jshint +W069 */
63 return this;
64};
65
66AndroidManifest.prototype.getActivity = function() {
67 var activity = this.doc.getroot().find('./application/activity');
68 return {
69 getName: function () {
70 return activity.attrib['android:name'];
71 },
72 setName: function (name) {
73 if (!name) {
74 delete activity.attrib['android:name'];
75 } else {
76 activity.attrib['android:name'] = name;
77 }
78 return this;
79 },
80 getOrientation: function () {
81 return activity.attrib['android:screenOrientation'];
82 },
83 setOrientation: function (orientation) {
84 if (!orientation || orientation.toLowerCase() === DEFAULT_ORIENTATION) {
85 delete activity.attrib['android:screenOrientation'];
86 } else {
87 activity.attrib['android:screenOrientation'] = orientation;
88 }
89 return this;
90 },
91 getLaunchMode: function () {
92 return activity.attrib['android:launchMode'];
93 },
94 setLaunchMode: function (launchMode) {
95 if (!launchMode) {
96 delete activity.attrib['android:launchMode'];
97 } else {
98 activity.attrib['android:launchMode'] = launchMode;
99 }
100 return this;
101 }
102 };
103};
104
105['minSdkVersion', 'maxSdkVersion', 'targetSdkVersion']
106.forEach(function(sdkPrefName) {
107 // Copy variable reference to avoid closure issues
108 var prefName = sdkPrefName;
109
110 AndroidManifest.prototype['get' + capitalize(prefName)] = function() {
111 var usesSdk = this.doc.getroot().find('./uses-sdk');
112 return usesSdk && usesSdk.attrib['android:' + prefName];
113 };
114
115 AndroidManifest.prototype['set' + capitalize(prefName)] = function(prefValue) {
116 var usesSdk = this.doc.getroot().find('./uses-sdk');
117
118 if (!usesSdk && prefValue) { // if there is no required uses-sdk element, we should create it first
119 usesSdk = new et.Element('uses-sdk');
120 this.doc.getroot().append(usesSdk);
121 }
122
123 if (prefValue) {
124 usesSdk.attrib['android:' + prefName] = prefValue;
125 }
126
127 return this;
128 };
129});
130
131AndroidManifest.prototype.getDebuggable = function() {
132 return this.doc.getroot().find('./application').attrib['android:debuggable'] === 'true';
133};
134
135AndroidManifest.prototype.setDebuggable = function(value) {
136 var application = this.doc.getroot().find('./application');
137 if (value) {
138 application.attrib['android:debuggable'] = 'true';
139 } else {
140 // The default value is "false", so we can remove attribute at all.
141 delete application.attrib['android:debuggable'];
142 }
143 return this;
144};
145
146/**
147 * Writes manifest to disk syncronously. If filename is specified, then manifest
148 * will be written to that file
149 *
150 * @param {String} [destPath] File to write manifest to. If omitted,
151 * manifest will be written to file it has been read from.
152 */
153AndroidManifest.prototype.write = function(destPath) {
154 fs.writeFileSync(destPath || this.path, this.doc.write({indent: 4}), 'utf-8');
155};
156
157module.exports = AndroidManifest;
158
159function capitalize (str) {
160 return str.charAt(0).toUpperCase() + str.slice(1);
161}