blob: e88ee02eb68577ea24b3eb80d61a37a7bc86bea3 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.content.pm;
18
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import android.content.ComponentName;
20import android.content.Intent;
21import android.content.IntentFilter;
22import android.content.res.AssetManager;
23import android.content.res.Configuration;
24import android.content.res.Resources;
25import android.content.res.TypedArray;
26import android.content.res.XmlResourceParser;
Amith Yamasani742a6712011-05-04 14:49:28 -070027import android.os.Binder;
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -070028import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.os.Bundle;
30import android.os.PatternMatcher;
Amith Yamasani742a6712011-05-04 14:49:28 -070031import android.os.UserId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.util.AttributeSet;
Kenny Root05ca4c92011-09-15 10:36:25 -070033import android.util.Base64;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.util.DisplayMetrics;
Kenny Root05ca4c92011-09-15 10:36:25 -070035import android.util.Log;
Kenny Rootd2d29252011-08-08 11:27:57 -070036import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
Kenny Rootd63f7db2010-09-27 08:07:48 -070039import java.io.BufferedInputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import java.io.File;
41import java.io.IOException;
42import java.io.InputStream;
43import java.lang.ref.WeakReference;
Kenny Root05ca4c92011-09-15 10:36:25 -070044import java.security.KeyFactory;
45import java.security.NoSuchAlgorithmException;
46import java.security.PublicKey;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import java.security.cert.Certificate;
48import java.security.cert.CertificateEncodingException;
Kenny Root05ca4c92011-09-15 10:36:25 -070049import java.security.spec.EncodedKeySpec;
50import java.security.spec.InvalidKeySpecException;
51import java.security.spec.X509EncodedKeySpec;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import java.util.ArrayList;
53import java.util.Enumeration;
Dianne Hackborne639da72012-02-21 15:11:13 -080054import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import java.util.Iterator;
Kenny Root05ca4c92011-09-15 10:36:25 -070056import java.util.List;
Kenny Rootbcc954d2011-08-08 16:19:08 -070057import java.util.jar.Attributes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import java.util.jar.JarEntry;
59import java.util.jar.JarFile;
Kenny Rootd2d29252011-08-08 11:27:57 -070060import java.util.jar.Manifest;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061
Amith Yamasani742a6712011-05-04 14:49:28 -070062import com.android.internal.util.XmlUtils;
63
64import org.xmlpull.v1.XmlPullParser;
65import org.xmlpull.v1.XmlPullParserException;
66
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067/**
68 * Package archive parsing
69 *
70 * {@hide}
71 */
72public class PackageParser {
Kenny Rootd2d29252011-08-08 11:27:57 -070073 private static final boolean DEBUG_JAR = false;
74 private static final boolean DEBUG_PARSER = false;
75 private static final boolean DEBUG_BACKUP = false;
76
Kenny Rootbcc954d2011-08-08 16:19:08 -070077 /** File name in an APK for the Android manifest. */
78 private static final String ANDROID_MANIFEST_FILENAME = "AndroidManifest.xml";
79
Dianne Hackborna96cbb42009-05-13 15:06:13 -070080 /** @hide */
81 public static class NewPermissionInfo {
82 public final String name;
83 public final int sdkVersion;
84 public final int fileVersion;
85
86 public NewPermissionInfo(String name, int sdkVersion, int fileVersion) {
87 this.name = name;
88 this.sdkVersion = sdkVersion;
89 this.fileVersion = fileVersion;
90 }
91 }
92
93 /**
94 * List of new permissions that have been added since 1.0.
95 * NOTE: These must be declared in SDK version order, with permissions
96 * added to older SDKs appearing before those added to newer SDKs.
97 * @hide
98 */
Jaikumar Ganesh45515652009-04-23 15:20:21 -070099 public static final PackageParser.NewPermissionInfo NEW_PERMISSIONS[] =
100 new PackageParser.NewPermissionInfo[] {
San Mehat5a3a77d2009-06-01 09:25:28 -0700101 new PackageParser.NewPermissionInfo(android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700102 android.os.Build.VERSION_CODES.DONUT, 0),
103 new PackageParser.NewPermissionInfo(android.Manifest.permission.READ_PHONE_STATE,
104 android.os.Build.VERSION_CODES.DONUT, 0)
Dianne Hackborna96cbb42009-05-13 15:06:13 -0700105 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106
107 private String mArchiveSourcePath;
108 private String[] mSeparateProcesses;
Dianne Hackbornd2509fd2011-09-12 12:29:43 -0700109 private boolean mOnlyCoreApps;
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -0700110 private static final int SDK_VERSION = Build.VERSION.SDK_INT;
111 private static final String SDK_CODENAME = "REL".equals(Build.VERSION.CODENAME)
112 ? null : Build.VERSION.CODENAME;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113
114 private int mParseError = PackageManager.INSTALL_SUCCEEDED;
115
116 private static final Object mSync = new Object();
117 private static WeakReference<byte[]> mReadBuffer;
118
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -0700119 private static boolean sCompatibilityModeEnabled = true;
120 private static final int PARSE_DEFAULT_INSTALL_LOCATION = PackageInfo.INSTALL_LOCATION_UNSPECIFIED;
Mitsuru Oshima69fff4a2009-07-21 09:51:05 -0700121
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700122 static class ParsePackageItemArgs {
123 final Package owner;
124 final String[] outError;
125 final int nameRes;
126 final int labelRes;
127 final int iconRes;
Adam Powell81cd2e92010-04-21 16:35:18 -0700128 final int logoRes;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700129
130 String tag;
131 TypedArray sa;
132
133 ParsePackageItemArgs(Package _owner, String[] _outError,
Adam Powell81cd2e92010-04-21 16:35:18 -0700134 int _nameRes, int _labelRes, int _iconRes, int _logoRes) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700135 owner = _owner;
136 outError = _outError;
137 nameRes = _nameRes;
138 labelRes = _labelRes;
139 iconRes = _iconRes;
Adam Powell81cd2e92010-04-21 16:35:18 -0700140 logoRes = _logoRes;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700141 }
142 }
143
144 static class ParseComponentArgs extends ParsePackageItemArgs {
145 final String[] sepProcesses;
146 final int processRes;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800147 final int descriptionRes;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700148 final int enabledRes;
149 int flags;
150
151 ParseComponentArgs(Package _owner, String[] _outError,
Adam Powell81cd2e92010-04-21 16:35:18 -0700152 int _nameRes, int _labelRes, int _iconRes, int _logoRes,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800153 String[] _sepProcesses, int _processRes,
154 int _descriptionRes, int _enabledRes) {
Adam Powell81cd2e92010-04-21 16:35:18 -0700155 super(_owner, _outError, _nameRes, _labelRes, _iconRes, _logoRes);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700156 sepProcesses = _sepProcesses;
157 processRes = _processRes;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800158 descriptionRes = _descriptionRes;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700159 enabledRes = _enabledRes;
160 }
161 }
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800162
163 /* Light weight package info.
164 * @hide
165 */
166 public static class PackageLite {
Kenny Root05ca4c92011-09-15 10:36:25 -0700167 public final String packageName;
168 public final int installLocation;
169 public final VerifierInfo[] verifiers;
170
171 public PackageLite(String packageName, int installLocation, List<VerifierInfo> verifiers) {
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800172 this.packageName = packageName;
173 this.installLocation = installLocation;
Kenny Root05ca4c92011-09-15 10:36:25 -0700174 this.verifiers = verifiers.toArray(new VerifierInfo[verifiers.size()]);
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800175 }
176 }
177
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700178 private ParsePackageItemArgs mParseInstrumentationArgs;
179 private ParseComponentArgs mParseActivityArgs;
180 private ParseComponentArgs mParseActivityAliasArgs;
181 private ParseComponentArgs mParseServiceArgs;
182 private ParseComponentArgs mParseProviderArgs;
183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 /** If set to true, we will only allow package files that exactly match
185 * the DTD. Otherwise, we try to get as much from the package as we
186 * can without failing. This should normally be set to false, to
187 * support extensions to the DTD in future versions. */
188 private static final boolean RIGID_PARSER = false;
189
190 private static final String TAG = "PackageParser";
191
192 public PackageParser(String archiveSourcePath) {
193 mArchiveSourcePath = archiveSourcePath;
194 }
195
196 public void setSeparateProcesses(String[] procs) {
197 mSeparateProcesses = procs;
198 }
199
Dianne Hackbornd2509fd2011-09-12 12:29:43 -0700200 public void setOnlyCoreApps(boolean onlyCoreApps) {
201 mOnlyCoreApps = onlyCoreApps;
202 }
203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 private static final boolean isPackageFilename(String name) {
205 return name.endsWith(".apk");
206 }
207
208 /**
209 * Generate and return the {@link PackageInfo} for a parsed package.
210 *
211 * @param p the parsed package.
212 * @param flags indicating which optional information is included.
213 */
214 public static PackageInfo generatePackageInfo(PackageParser.Package p,
Dianne Hackborne639da72012-02-21 15:11:13 -0800215 int gids[], int flags, long firstInstallTime, long lastUpdateTime,
216 HashSet<String> grantedPermissions) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217
Amith Yamasani742a6712011-05-04 14:49:28 -0700218 final int userId = Binder.getOrigCallingUser();
219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 PackageInfo pi = new PackageInfo();
221 pi.packageName = p.packageName;
222 pi.versionCode = p.mVersionCode;
223 pi.versionName = p.mVersionName;
224 pi.sharedUserId = p.mSharedUserId;
225 pi.sharedUserLabel = p.mSharedUserLabel;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800226 pi.applicationInfo = generateApplicationInfo(p, flags);
Suchi Amalapurapu117818e2010-02-09 03:45:40 -0800227 pi.installLocation = p.installLocation;
Dianne Hackborn78d68832010-10-07 01:12:46 -0700228 pi.firstInstallTime = firstInstallTime;
229 pi.lastUpdateTime = lastUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 if ((flags&PackageManager.GET_GIDS) != 0) {
231 pi.gids = gids;
232 }
233 if ((flags&PackageManager.GET_CONFIGURATIONS) != 0) {
234 int N = p.configPreferences.size();
235 if (N > 0) {
236 pi.configPreferences = new ConfigurationInfo[N];
Dianne Hackborn49237342009-08-27 20:08:01 -0700237 p.configPreferences.toArray(pi.configPreferences);
238 }
239 N = p.reqFeatures != null ? p.reqFeatures.size() : 0;
240 if (N > 0) {
241 pi.reqFeatures = new FeatureInfo[N];
242 p.reqFeatures.toArray(pi.reqFeatures);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 }
244 }
245 if ((flags&PackageManager.GET_ACTIVITIES) != 0) {
246 int N = p.activities.size();
247 if (N > 0) {
Dianne Hackborn7eca6872009-09-28 23:57:05 -0700248 if ((flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
249 pi.activities = new ActivityInfo[N];
250 } else {
251 int num = 0;
252 for (int i=0; i<N; i++) {
253 if (p.activities.get(i).info.enabled) num++;
254 }
255 pi.activities = new ActivityInfo[num];
256 }
Suchi Amalapurapud83006c2009-10-28 23:39:46 -0700257 for (int i=0, j=0; i<N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 final Activity activity = p.activities.get(i);
259 if (activity.info.enabled
260 || (flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700261 pi.activities[j++] = generateActivityInfo(p.activities.get(i), flags,
262 userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 }
264 }
265 }
266 }
267 if ((flags&PackageManager.GET_RECEIVERS) != 0) {
268 int N = p.receivers.size();
269 if (N > 0) {
Dianne Hackborn7eca6872009-09-28 23:57:05 -0700270 if ((flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
271 pi.receivers = new ActivityInfo[N];
272 } else {
273 int num = 0;
274 for (int i=0; i<N; i++) {
275 if (p.receivers.get(i).info.enabled) num++;
276 }
277 pi.receivers = new ActivityInfo[num];
278 }
Suchi Amalapurapud83006c2009-10-28 23:39:46 -0700279 for (int i=0, j=0; i<N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 final Activity activity = p.receivers.get(i);
281 if (activity.info.enabled
282 || (flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700283 pi.receivers[j++] = generateActivityInfo(p.receivers.get(i), flags, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 }
285 }
286 }
287 }
288 if ((flags&PackageManager.GET_SERVICES) != 0) {
289 int N = p.services.size();
290 if (N > 0) {
Dianne Hackborn7eca6872009-09-28 23:57:05 -0700291 if ((flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
292 pi.services = new ServiceInfo[N];
293 } else {
294 int num = 0;
295 for (int i=0; i<N; i++) {
296 if (p.services.get(i).info.enabled) num++;
297 }
298 pi.services = new ServiceInfo[num];
299 }
Suchi Amalapurapud83006c2009-10-28 23:39:46 -0700300 for (int i=0, j=0; i<N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 final Service service = p.services.get(i);
302 if (service.info.enabled
303 || (flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700304 pi.services[j++] = generateServiceInfo(p.services.get(i), flags, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 }
306 }
307 }
308 }
309 if ((flags&PackageManager.GET_PROVIDERS) != 0) {
310 int N = p.providers.size();
311 if (N > 0) {
Dianne Hackborn7eca6872009-09-28 23:57:05 -0700312 if ((flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
313 pi.providers = new ProviderInfo[N];
314 } else {
315 int num = 0;
316 for (int i=0; i<N; i++) {
317 if (p.providers.get(i).info.enabled) num++;
318 }
319 pi.providers = new ProviderInfo[num];
320 }
Suchi Amalapurapud83006c2009-10-28 23:39:46 -0700321 for (int i=0, j=0; i<N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 final Provider provider = p.providers.get(i);
323 if (provider.info.enabled
324 || (flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700325 pi.providers[j++] = generateProviderInfo(p.providers.get(i), flags, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 }
327 }
328 }
329 }
330 if ((flags&PackageManager.GET_INSTRUMENTATION) != 0) {
331 int N = p.instrumentation.size();
332 if (N > 0) {
333 pi.instrumentation = new InstrumentationInfo[N];
334 for (int i=0; i<N; i++) {
335 pi.instrumentation[i] = generateInstrumentationInfo(
336 p.instrumentation.get(i), flags);
337 }
338 }
339 }
340 if ((flags&PackageManager.GET_PERMISSIONS) != 0) {
341 int N = p.permissions.size();
342 if (N > 0) {
343 pi.permissions = new PermissionInfo[N];
344 for (int i=0; i<N; i++) {
345 pi.permissions[i] = generatePermissionInfo(p.permissions.get(i), flags);
346 }
347 }
348 N = p.requestedPermissions.size();
349 if (N > 0) {
350 pi.requestedPermissions = new String[N];
Dianne Hackborne639da72012-02-21 15:11:13 -0800351 pi.requestedPermissionsFlags = new int[N];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 for (int i=0; i<N; i++) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800353 final String perm = p.requestedPermissions.get(i);
354 pi.requestedPermissions[i] = perm;
355 if (p.requestedPermissionsRequired.get(i)) {
356 pi.requestedPermissionsFlags[i] |= PackageInfo.REQUESTED_PERMISSION_REQUIRED;
357 }
358 if (grantedPermissions != null && grantedPermissions.contains(perm)) {
359 pi.requestedPermissionsFlags[i] |= PackageInfo.REQUESTED_PERMISSION_GRANTED;
360 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 }
362 }
363 }
364 if ((flags&PackageManager.GET_SIGNATURES) != 0) {
Suchi Amalapurapud83006c2009-10-28 23:39:46 -0700365 int N = (p.mSignatures != null) ? p.mSignatures.length : 0;
366 if (N > 0) {
367 pi.signatures = new Signature[N];
368 System.arraycopy(p.mSignatures, 0, pi.signatures, 0, N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 }
370 }
371 return pi;
372 }
373
374 private Certificate[] loadCertificates(JarFile jarFile, JarEntry je,
375 byte[] readBuffer) {
376 try {
377 // We must read the stream for the JarEntry to retrieve
378 // its certificates.
Kenny Rootd63f7db2010-09-27 08:07:48 -0700379 InputStream is = new BufferedInputStream(jarFile.getInputStream(je));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 while (is.read(readBuffer, 0, readBuffer.length) != -1) {
381 // not using
382 }
383 is.close();
384 return je != null ? je.getCertificates() : null;
385 } catch (IOException e) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700386 Slog.w(TAG, "Exception reading " + je.getName() + " in "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 + jarFile.getName(), e);
Dianne Hackborn6e52b5d2010-04-05 14:33:01 -0700388 } catch (RuntimeException e) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700389 Slog.w(TAG, "Exception reading " + je.getName() + " in "
Dianne Hackborn6e52b5d2010-04-05 14:33:01 -0700390 + jarFile.getName(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 }
392 return null;
393 }
394
Suchi Amalapurapuaf8e9f42010-01-12 10:17:28 -0800395 public final static int PARSE_IS_SYSTEM = 1<<0;
396 public final static int PARSE_CHATTY = 1<<1;
397 public final static int PARSE_MUST_BE_APK = 1<<2;
398 public final static int PARSE_IGNORE_PROCESSES = 1<<3;
399 public final static int PARSE_FORWARD_LOCK = 1<<4;
400 public final static int PARSE_ON_SDCARD = 1<<5;
Dianne Hackborn806da1d2010-03-18 16:50:07 -0700401 public final static int PARSE_IS_SYSTEM_DIR = 1<<6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402
403 public int getParseError() {
404 return mParseError;
405 }
406
Suchi Amalapurapuaf8e9f42010-01-12 10:17:28 -0800407 public Package parsePackage(File sourceFile, String destCodePath,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 DisplayMetrics metrics, int flags) {
409 mParseError = PackageManager.INSTALL_SUCCEEDED;
410
411 mArchiveSourcePath = sourceFile.getPath();
412 if (!sourceFile.isFile()) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700413 Slog.w(TAG, "Skipping dir: " + mArchiveSourcePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 mParseError = PackageManager.INSTALL_PARSE_FAILED_NOT_APK;
415 return null;
416 }
417 if (!isPackageFilename(sourceFile.getName())
418 && (flags&PARSE_MUST_BE_APK) != 0) {
419 if ((flags&PARSE_IS_SYSTEM) == 0) {
420 // We expect to have non-.apk files in the system dir,
421 // so don't warn about them.
Kenny Rootd2d29252011-08-08 11:27:57 -0700422 Slog.w(TAG, "Skipping non-package file: " + mArchiveSourcePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 }
424 mParseError = PackageManager.INSTALL_PARSE_FAILED_NOT_APK;
425 return null;
426 }
427
Kenny Rootd2d29252011-08-08 11:27:57 -0700428 if (DEBUG_JAR)
429 Slog.d(TAG, "Scanning package: " + mArchiveSourcePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430
431 XmlResourceParser parser = null;
432 AssetManager assmgr = null;
Dianne Hackborn3b81bc12011-01-15 11:50:52 -0800433 Resources res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 boolean assetError = true;
435 try {
436 assmgr = new AssetManager();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700437 int cookie = assmgr.addAssetPath(mArchiveSourcePath);
Dianne Hackborn3b81bc12011-01-15 11:50:52 -0800438 if (cookie != 0) {
439 res = new Resources(assmgr, metrics, null);
Dianne Hackborn69cb8752011-05-19 18:13:32 -0700440 assmgr.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Dianne Hackborn3b81bc12011-01-15 11:50:52 -0800441 Build.VERSION.RESOURCES_SDK_INT);
Kenny Rootbcc954d2011-08-08 16:19:08 -0700442 parser = assmgr.openXmlResourceParser(cookie, ANDROID_MANIFEST_FILENAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 assetError = false;
444 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -0700445 Slog.w(TAG, "Failed adding asset path:"+mArchiveSourcePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 }
447 } catch (Exception e) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700448 Slog.w(TAG, "Unable to read AndroidManifest.xml of "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 + mArchiveSourcePath, e);
450 }
Dianne Hackborn3b81bc12011-01-15 11:50:52 -0800451 if (assetError) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 if (assmgr != null) assmgr.close();
453 mParseError = PackageManager.INSTALL_PARSE_FAILED_BAD_MANIFEST;
454 return null;
455 }
456 String[] errorText = new String[1];
457 Package pkg = null;
458 Exception errorException = null;
459 try {
460 // XXXX todo: need to figure out correct configuration.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 pkg = parsePackage(res, parser, flags, errorText);
462 } catch (Exception e) {
463 errorException = e;
464 mParseError = PackageManager.INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION;
465 }
466
467
468 if (pkg == null) {
Dianne Hackbornd2509fd2011-09-12 12:29:43 -0700469 // If we are only parsing core apps, then a null with INSTALL_SUCCEEDED
470 // just means to skip this app so don't make a fuss about it.
471 if (!mOnlyCoreApps || mParseError != PackageManager.INSTALL_SUCCEEDED) {
472 if (errorException != null) {
473 Slog.w(TAG, mArchiveSourcePath, errorException);
474 } else {
475 Slog.w(TAG, mArchiveSourcePath + " (at "
476 + parser.getPositionDescription()
477 + "): " + errorText[0]);
478 }
479 if (mParseError == PackageManager.INSTALL_SUCCEEDED) {
480 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 }
483 parser.close();
484 assmgr.close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 return null;
486 }
487
488 parser.close();
489 assmgr.close();
490
Suchi Amalapurapuaf8e9f42010-01-12 10:17:28 -0800491 // Set code and resource paths
492 pkg.mPath = destCodePath;
493 pkg.mScanPath = mArchiveSourcePath;
494 //pkg.applicationInfo.sourceDir = destCodePath;
495 //pkg.applicationInfo.publicSourceDir = destRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 pkg.mSignatures = null;
497
498 return pkg;
499 }
500
501 public boolean collectCertificates(Package pkg, int flags) {
502 pkg.mSignatures = null;
503
504 WeakReference<byte[]> readBufferRef;
505 byte[] readBuffer = null;
506 synchronized (mSync) {
507 readBufferRef = mReadBuffer;
508 if (readBufferRef != null) {
509 mReadBuffer = null;
510 readBuffer = readBufferRef.get();
511 }
512 if (readBuffer == null) {
513 readBuffer = new byte[8192];
514 readBufferRef = new WeakReference<byte[]>(readBuffer);
515 }
516 }
517
518 try {
519 JarFile jarFile = new JarFile(mArchiveSourcePath);
520
521 Certificate[] certs = null;
522
523 if ((flags&PARSE_IS_SYSTEM) != 0) {
524 // If this package comes from the system image, then we
525 // can trust it... we'll just use the AndroidManifest.xml
526 // to retrieve its signatures, not validating all of the
527 // files.
Kenny Rootbcc954d2011-08-08 16:19:08 -0700528 JarEntry jarEntry = jarFile.getJarEntry(ANDROID_MANIFEST_FILENAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 certs = loadCertificates(jarFile, jarEntry, readBuffer);
530 if (certs == null) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700531 Slog.e(TAG, "Package " + pkg.packageName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 + " has no certificates at entry "
533 + jarEntry.getName() + "; ignoring!");
534 jarFile.close();
535 mParseError = PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
536 return false;
537 }
Kenny Rootd2d29252011-08-08 11:27:57 -0700538 if (DEBUG_JAR) {
539 Slog.i(TAG, "File " + mArchiveSourcePath + ": entry=" + jarEntry
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 + " certs=" + (certs != null ? certs.length : 0));
541 if (certs != null) {
542 final int N = certs.length;
543 for (int i=0; i<N; i++) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700544 Slog.i(TAG, " Public key: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 + certs[i].getPublicKey().getEncoded()
546 + " " + certs[i].getPublicKey());
547 }
548 }
549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -0700551 Enumeration<JarEntry> entries = jarFile.entries();
Kenny Rootbcc954d2011-08-08 16:19:08 -0700552 final Manifest manifest = jarFile.getManifest();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 while (entries.hasMoreElements()) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700554 final JarEntry je = entries.nextElement();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 if (je.isDirectory()) continue;
Kenny Rootd2d29252011-08-08 11:27:57 -0700556
Kenny Rootbcc954d2011-08-08 16:19:08 -0700557 final String name = je.getName();
558
559 if (name.startsWith("META-INF/"))
560 continue;
561
562 if (ANDROID_MANIFEST_FILENAME.equals(name)) {
563 final Attributes attributes = manifest.getAttributes(name);
564 pkg.manifestDigest = ManifestDigest.fromAttributes(attributes);
565 }
566
567 final Certificate[] localCerts = loadCertificates(jarFile, je, readBuffer);
Kenny Rootd2d29252011-08-08 11:27:57 -0700568 if (DEBUG_JAR) {
569 Slog.i(TAG, "File " + mArchiveSourcePath + " entry " + je.getName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 + ": certs=" + certs + " ("
571 + (certs != null ? certs.length : 0) + ")");
572 }
Kenny Rootbcc954d2011-08-08 16:19:08 -0700573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 if (localCerts == null) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700575 Slog.e(TAG, "Package " + pkg.packageName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 + " has no certificates at entry "
577 + je.getName() + "; ignoring!");
578 jarFile.close();
579 mParseError = PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
580 return false;
581 } else if (certs == null) {
582 certs = localCerts;
583 } else {
584 // Ensure all certificates match.
585 for (int i=0; i<certs.length; i++) {
586 boolean found = false;
587 for (int j=0; j<localCerts.length; j++) {
588 if (certs[i] != null &&
589 certs[i].equals(localCerts[j])) {
590 found = true;
591 break;
592 }
593 }
594 if (!found || certs.length != localCerts.length) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700595 Slog.e(TAG, "Package " + pkg.packageName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 + " has mismatched certificates at entry "
597 + je.getName() + "; ignoring!");
598 jarFile.close();
599 mParseError = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;
600 return false;
601 }
602 }
603 }
604 }
605 }
606 jarFile.close();
607
608 synchronized (mSync) {
609 mReadBuffer = readBufferRef;
610 }
611
612 if (certs != null && certs.length > 0) {
613 final int N = certs.length;
614 pkg.mSignatures = new Signature[certs.length];
615 for (int i=0; i<N; i++) {
616 pkg.mSignatures[i] = new Signature(
617 certs[i].getEncoded());
618 }
619 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -0700620 Slog.e(TAG, "Package " + pkg.packageName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 + " has no certificates; ignoring!");
622 mParseError = PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
623 return false;
624 }
625 } catch (CertificateEncodingException e) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700626 Slog.w(TAG, "Exception reading " + mArchiveSourcePath, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 mParseError = PackageManager.INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING;
628 return false;
629 } catch (IOException e) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700630 Slog.w(TAG, "Exception reading " + mArchiveSourcePath, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 mParseError = PackageManager.INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING;
632 return false;
633 } catch (RuntimeException e) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700634 Slog.w(TAG, "Exception reading " + mArchiveSourcePath, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 mParseError = PackageManager.INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION;
636 return false;
637 }
638
639 return true;
640 }
641
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800642 /*
643 * Utility method that retrieves just the package name and install
644 * location from the apk location at the given file path.
645 * @param packageFilePath file location of the apk
646 * @param flags Special parse flags
Kenny Root930d3af2010-07-30 16:52:29 -0700647 * @return PackageLite object with package information or null on failure.
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800648 */
649 public static PackageLite parsePackageLite(String packageFilePath, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 AssetManager assmgr = null;
Kenny Root05ca4c92011-09-15 10:36:25 -0700651 final XmlResourceParser parser;
652 final Resources res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 try {
654 assmgr = new AssetManager();
Dianne Hackborn69cb8752011-05-19 18:13:32 -0700655 assmgr.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Dianne Hackborn3b81bc12011-01-15 11:50:52 -0800656 Build.VERSION.RESOURCES_SDK_INT);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 int cookie = assmgr.addAssetPath(packageFilePath);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700659 if (cookie == 0) {
660 return null;
661 }
662
Kenny Root05ca4c92011-09-15 10:36:25 -0700663 final DisplayMetrics metrics = new DisplayMetrics();
664 metrics.setToDefaults();
665 res = new Resources(assmgr, metrics, null);
Kenny Rootbcc954d2011-08-08 16:19:08 -0700666 parser = assmgr.openXmlResourceParser(cookie, ANDROID_MANIFEST_FILENAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 } catch (Exception e) {
668 if (assmgr != null) assmgr.close();
Kenny Rootd2d29252011-08-08 11:27:57 -0700669 Slog.w(TAG, "Unable to read AndroidManifest.xml of "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 + packageFilePath, e);
671 return null;
672 }
Kenny Root05ca4c92011-09-15 10:36:25 -0700673
674 final AttributeSet attrs = parser;
675 final String errors[] = new String[1];
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800676 PackageLite packageLite = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 try {
Kenny Root05ca4c92011-09-15 10:36:25 -0700678 packageLite = parsePackageLite(res, parser, attrs, flags, errors);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 } catch (IOException e) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700680 Slog.w(TAG, packageFilePath, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 } catch (XmlPullParserException e) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700682 Slog.w(TAG, packageFilePath, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 } finally {
684 if (parser != null) parser.close();
685 if (assmgr != null) assmgr.close();
686 }
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800687 if (packageLite == null) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700688 Slog.e(TAG, "parsePackageLite error: " + errors[0]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 return null;
690 }
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800691 return packageLite;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 }
693
694 private static String validateName(String name, boolean requiresSeparator) {
695 final int N = name.length();
696 boolean hasSep = false;
697 boolean front = true;
698 for (int i=0; i<N; i++) {
699 final char c = name.charAt(i);
700 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
701 front = false;
702 continue;
703 }
704 if (!front) {
705 if ((c >= '0' && c <= '9') || c == '_') {
706 continue;
707 }
708 }
709 if (c == '.') {
710 hasSep = true;
711 front = true;
712 continue;
713 }
714 return "bad character '" + c + "'";
715 }
716 return hasSep || !requiresSeparator
717 ? null : "must have at least one '.' separator";
718 }
719
720 private static String parsePackageName(XmlPullParser parser,
721 AttributeSet attrs, int flags, String[] outError)
722 throws IOException, XmlPullParserException {
723
724 int type;
Kenny Rootd2d29252011-08-08 11:27:57 -0700725 while ((type = parser.next()) != XmlPullParser.START_TAG
726 && type != XmlPullParser.END_DOCUMENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 ;
728 }
729
Kenny Rootd2d29252011-08-08 11:27:57 -0700730 if (type != XmlPullParser.START_TAG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 outError[0] = "No start tag found";
732 return null;
733 }
Kenny Rootd2d29252011-08-08 11:27:57 -0700734 if (DEBUG_PARSER)
735 Slog.v(TAG, "Root element name: '" + parser.getName() + "'");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 if (!parser.getName().equals("manifest")) {
737 outError[0] = "No <manifest> tag";
738 return null;
739 }
740 String pkgName = attrs.getAttributeValue(null, "package");
741 if (pkgName == null || pkgName.length() == 0) {
742 outError[0] = "<manifest> does not specify package";
743 return null;
744 }
745 String nameError = validateName(pkgName, true);
746 if (nameError != null && !"android".equals(pkgName)) {
747 outError[0] = "<manifest> specifies bad package name \""
748 + pkgName + "\": " + nameError;
749 return null;
750 }
751
752 return pkgName.intern();
753 }
754
Kenny Root05ca4c92011-09-15 10:36:25 -0700755 private static PackageLite parsePackageLite(Resources res, XmlPullParser parser,
756 AttributeSet attrs, int flags, String[] outError) throws IOException,
757 XmlPullParserException {
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800758
759 int type;
Kenny Rootd2d29252011-08-08 11:27:57 -0700760 while ((type = parser.next()) != XmlPullParser.START_TAG
761 && type != XmlPullParser.END_DOCUMENT) {
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800762 ;
763 }
764
Kenny Rootd2d29252011-08-08 11:27:57 -0700765 if (type != XmlPullParser.START_TAG) {
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800766 outError[0] = "No start tag found";
767 return null;
768 }
Kenny Rootd2d29252011-08-08 11:27:57 -0700769 if (DEBUG_PARSER)
770 Slog.v(TAG, "Root element name: '" + parser.getName() + "'");
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800771 if (!parser.getName().equals("manifest")) {
772 outError[0] = "No <manifest> tag";
773 return null;
774 }
775 String pkgName = attrs.getAttributeValue(null, "package");
776 if (pkgName == null || pkgName.length() == 0) {
777 outError[0] = "<manifest> does not specify package";
778 return null;
779 }
780 String nameError = validateName(pkgName, true);
781 if (nameError != null && !"android".equals(pkgName)) {
782 outError[0] = "<manifest> specifies bad package name \""
783 + pkgName + "\": " + nameError;
784 return null;
785 }
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -0700786 int installLocation = PARSE_DEFAULT_INSTALL_LOCATION;
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800787 for (int i = 0; i < attrs.getAttributeCount(); i++) {
788 String attr = attrs.getAttributeName(i);
789 if (attr.equals("installLocation")) {
790 installLocation = attrs.getAttributeIntValue(i,
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -0700791 PARSE_DEFAULT_INSTALL_LOCATION);
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800792 break;
793 }
794 }
Kenny Root05ca4c92011-09-15 10:36:25 -0700795
796 // Only search the tree when the tag is directly below <manifest>
797 final int searchDepth = parser.getDepth() + 1;
798
799 final List<VerifierInfo> verifiers = new ArrayList<VerifierInfo>();
800 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
801 && (type != XmlPullParser.END_TAG || parser.getDepth() >= searchDepth)) {
802 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
803 continue;
804 }
805
806 if (parser.getDepth() == searchDepth && "package-verifier".equals(parser.getName())) {
807 final VerifierInfo verifier = parseVerifier(res, parser, attrs, flags, outError);
808 if (verifier != null) {
809 verifiers.add(verifier);
810 }
811 }
812 }
813
814 return new PackageLite(pkgName.intern(), installLocation, verifiers);
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800815 }
816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 /**
818 * Temporary.
819 */
820 static public Signature stringToSignature(String str) {
821 final int N = str.length();
822 byte[] sig = new byte[N];
823 for (int i=0; i<N; i++) {
824 sig[i] = (byte)str.charAt(i);
825 }
826 return new Signature(sig);
827 }
828
829 private Package parsePackage(
830 Resources res, XmlResourceParser parser, int flags, String[] outError)
831 throws XmlPullParserException, IOException {
832 AttributeSet attrs = parser;
833
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700834 mParseInstrumentationArgs = null;
835 mParseActivityArgs = null;
836 mParseServiceArgs = null;
837 mParseProviderArgs = null;
838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 String pkgName = parsePackageName(parser, attrs, flags, outError);
840 if (pkgName == null) {
841 mParseError = PackageManager.INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME;
842 return null;
843 }
844 int type;
845
Dianne Hackbornd2509fd2011-09-12 12:29:43 -0700846 if (mOnlyCoreApps) {
847 boolean core = attrs.getAttributeBooleanValue(null, "coreApp", false);
848 if (!core) {
849 mParseError = PackageManager.INSTALL_SUCCEEDED;
850 return null;
851 }
852 }
853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 final Package pkg = new Package(pkgName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 boolean foundApp = false;
Dianne Hackborn851a5412009-05-08 12:06:44 -0700856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 TypedArray sa = res.obtainAttributes(attrs,
858 com.android.internal.R.styleable.AndroidManifest);
859 pkg.mVersionCode = sa.getInteger(
860 com.android.internal.R.styleable.AndroidManifest_versionCode, 0);
Dianne Hackborncf244ad2010-03-09 15:00:30 -0800861 pkg.mVersionName = sa.getNonConfigurationString(
862 com.android.internal.R.styleable.AndroidManifest_versionName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 if (pkg.mVersionName != null) {
864 pkg.mVersionName = pkg.mVersionName.intern();
865 }
Dianne Hackborncf244ad2010-03-09 15:00:30 -0800866 String str = sa.getNonConfigurationString(
867 com.android.internal.R.styleable.AndroidManifest_sharedUserId, 0);
868 if (str != null && str.length() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 String nameError = validateName(str, true);
870 if (nameError != null && !"android".equals(pkgName)) {
871 outError[0] = "<manifest> specifies bad sharedUserId name \""
872 + str + "\": " + nameError;
873 mParseError = PackageManager.INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID;
874 return null;
875 }
876 pkg.mSharedUserId = str.intern();
877 pkg.mSharedUserLabel = sa.getResourceId(
878 com.android.internal.R.styleable.AndroidManifest_sharedUserLabel, 0);
879 }
880 sa.recycle();
Suchi Amalapurapuaaec7792010-02-25 11:49:43 -0800881
Suchi Amalapurapu117818e2010-02-09 03:45:40 -0800882 pkg.installLocation = sa.getInteger(
883 com.android.internal.R.styleable.AndroidManifest_installLocation,
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -0700884 PARSE_DEFAULT_INSTALL_LOCATION);
Dianne Hackborn54e570f2010-10-04 18:32:32 -0700885 pkg.applicationInfo.installLocation = pkg.installLocation;
886
Dianne Hackborn723738c2009-06-25 19:48:04 -0700887 // Resource boolean are -1, so 1 means we don't know the value.
888 int supportsSmallScreens = 1;
889 int supportsNormalScreens = 1;
890 int supportsLargeScreens = 1;
Dianne Hackborn14cee9f2010-04-23 17:51:26 -0700891 int supportsXLargeScreens = 1;
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700892 int resizeable = 1;
Dianne Hackborn11b822d2009-07-21 20:03:02 -0700893 int anyDensity = 1;
Dianne Hackborn723738c2009-06-25 19:48:04 -0700894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 int outerDepth = parser.getDepth();
Kenny Rootd2d29252011-08-08 11:27:57 -0700896 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
897 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
898 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 continue;
900 }
901
902 String tagName = parser.getName();
903 if (tagName.equals("application")) {
904 if (foundApp) {
905 if (RIGID_PARSER) {
906 outError[0] = "<manifest> has more than one <application>";
907 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
908 return null;
909 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -0700910 Slog.w(TAG, "<manifest> has more than one <application>");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 XmlUtils.skipCurrentTag(parser);
912 continue;
913 }
914 }
915
916 foundApp = true;
917 if (!parseApplication(pkg, res, parser, attrs, flags, outError)) {
918 return null;
919 }
920 } else if (tagName.equals("permission-group")) {
921 if (parsePermissionGroup(pkg, res, parser, attrs, outError) == null) {
922 return null;
923 }
924 } else if (tagName.equals("permission")) {
925 if (parsePermission(pkg, res, parser, attrs, outError) == null) {
926 return null;
927 }
928 } else if (tagName.equals("permission-tree")) {
929 if (parsePermissionTree(pkg, res, parser, attrs, outError) == null) {
930 return null;
931 }
932 } else if (tagName.equals("uses-permission")) {
933 sa = res.obtainAttributes(attrs,
934 com.android.internal.R.styleable.AndroidManifestUsesPermission);
935
Dianne Hackborncf244ad2010-03-09 15:00:30 -0800936 // Note: don't allow this value to be a reference to a resource
937 // that may change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 String name = sa.getNonResourceString(
939 com.android.internal.R.styleable.AndroidManifestUsesPermission_name);
Dianne Hackborne639da72012-02-21 15:11:13 -0800940 boolean required = sa.getBoolean(
941 com.android.internal.R.styleable.AndroidManifestUsesPermission_required, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942
943 sa.recycle();
944
945 if (name != null && !pkg.requestedPermissions.contains(name)) {
Dianne Hackborn854060a2009-07-09 18:14:31 -0700946 pkg.requestedPermissions.add(name.intern());
Dianne Hackborn65696252012-03-05 18:49:21 -0800947 pkg.requestedPermissionsRequired.add(required ? Boolean.TRUE : Boolean.FALSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 }
949
950 XmlUtils.skipCurrentTag(parser);
951
952 } else if (tagName.equals("uses-configuration")) {
953 ConfigurationInfo cPref = new ConfigurationInfo();
954 sa = res.obtainAttributes(attrs,
955 com.android.internal.R.styleable.AndroidManifestUsesConfiguration);
956 cPref.reqTouchScreen = sa.getInt(
957 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqTouchScreen,
958 Configuration.TOUCHSCREEN_UNDEFINED);
959 cPref.reqKeyboardType = sa.getInt(
960 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqKeyboardType,
961 Configuration.KEYBOARD_UNDEFINED);
962 if (sa.getBoolean(
963 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqHardKeyboard,
964 false)) {
965 cPref.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD;
966 }
967 cPref.reqNavigation = sa.getInt(
968 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqNavigation,
969 Configuration.NAVIGATION_UNDEFINED);
970 if (sa.getBoolean(
971 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqFiveWayNav,
972 false)) {
973 cPref.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV;
974 }
975 sa.recycle();
976 pkg.configPreferences.add(cPref);
977
978 XmlUtils.skipCurrentTag(parser);
979
Suchi Amalapurapud299b812009-06-05 10:26:19 -0700980 } else if (tagName.equals("uses-feature")) {
Dianne Hackborn49237342009-08-27 20:08:01 -0700981 FeatureInfo fi = new FeatureInfo();
Suchi Amalapurapud299b812009-06-05 10:26:19 -0700982 sa = res.obtainAttributes(attrs,
983 com.android.internal.R.styleable.AndroidManifestUsesFeature);
Dianne Hackborncf244ad2010-03-09 15:00:30 -0800984 // Note: don't allow this value to be a reference to a resource
985 // that may change.
Dianne Hackborn49237342009-08-27 20:08:01 -0700986 fi.name = sa.getNonResourceString(
987 com.android.internal.R.styleable.AndroidManifestUsesFeature_name);
988 if (fi.name == null) {
989 fi.reqGlEsVersion = sa.getInt(
990 com.android.internal.R.styleable.AndroidManifestUsesFeature_glEsVersion,
991 FeatureInfo.GL_ES_VERSION_UNDEFINED);
992 }
993 if (sa.getBoolean(
994 com.android.internal.R.styleable.AndroidManifestUsesFeature_required,
995 true)) {
996 fi.flags |= FeatureInfo.FLAG_REQUIRED;
997 }
Suchi Amalapurapud299b812009-06-05 10:26:19 -0700998 sa.recycle();
Dianne Hackborn49237342009-08-27 20:08:01 -0700999 if (pkg.reqFeatures == null) {
1000 pkg.reqFeatures = new ArrayList<FeatureInfo>();
1001 }
1002 pkg.reqFeatures.add(fi);
1003
1004 if (fi.name == null) {
1005 ConfigurationInfo cPref = new ConfigurationInfo();
1006 cPref.reqGlEsVersion = fi.reqGlEsVersion;
1007 pkg.configPreferences.add(cPref);
1008 }
Suchi Amalapurapud299b812009-06-05 10:26:19 -07001009
1010 XmlUtils.skipCurrentTag(parser);
1011
Dianne Hackborn851a5412009-05-08 12:06:44 -07001012 } else if (tagName.equals("uses-sdk")) {
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001013 if (SDK_VERSION > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 sa = res.obtainAttributes(attrs,
1015 com.android.internal.R.styleable.AndroidManifestUsesSdk);
1016
Dianne Hackborn851a5412009-05-08 12:06:44 -07001017 int minVers = 0;
1018 String minCode = null;
1019 int targetVers = 0;
1020 String targetCode = null;
1021
1022 TypedValue val = sa.peekValue(
1023 com.android.internal.R.styleable.AndroidManifestUsesSdk_minSdkVersion);
1024 if (val != null) {
1025 if (val.type == TypedValue.TYPE_STRING && val.string != null) {
1026 targetCode = minCode = val.string.toString();
1027 } else {
1028 // If it's not a string, it's an integer.
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001029 targetVers = minVers = val.data;
Dianne Hackborn851a5412009-05-08 12:06:44 -07001030 }
1031 }
1032
1033 val = sa.peekValue(
1034 com.android.internal.R.styleable.AndroidManifestUsesSdk_targetSdkVersion);
1035 if (val != null) {
1036 if (val.type == TypedValue.TYPE_STRING && val.string != null) {
1037 targetCode = minCode = val.string.toString();
1038 } else {
1039 // If it's not a string, it's an integer.
1040 targetVers = val.data;
1041 }
1042 }
1043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 sa.recycle();
1045
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001046 if (minCode != null) {
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001047 if (!minCode.equals(SDK_CODENAME)) {
1048 if (SDK_CODENAME != null) {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001049 outError[0] = "Requires development platform " + minCode
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001050 + " (current platform is " + SDK_CODENAME + ")";
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001051 } else {
1052 outError[0] = "Requires development platform " + minCode
1053 + " but this is a release platform.";
1054 }
1055 mParseError = PackageManager.INSTALL_FAILED_OLDER_SDK;
1056 return null;
1057 }
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001058 } else if (minVers > SDK_VERSION) {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001059 outError[0] = "Requires newer sdk version #" + minVers
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001060 + " (current version is #" + SDK_VERSION + ")";
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001061 mParseError = PackageManager.INSTALL_FAILED_OLDER_SDK;
1062 return null;
1063 }
1064
Dianne Hackborn851a5412009-05-08 12:06:44 -07001065 if (targetCode != null) {
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001066 if (!targetCode.equals(SDK_CODENAME)) {
1067 if (SDK_CODENAME != null) {
Dianne Hackborn851a5412009-05-08 12:06:44 -07001068 outError[0] = "Requires development platform " + targetCode
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001069 + " (current platform is " + SDK_CODENAME + ")";
Dianne Hackborn851a5412009-05-08 12:06:44 -07001070 } else {
1071 outError[0] = "Requires development platform " + targetCode
1072 + " but this is a release platform.";
1073 }
1074 mParseError = PackageManager.INSTALL_FAILED_OLDER_SDK;
1075 return null;
1076 }
1077 // If the code matches, it definitely targets this SDK.
Dianne Hackborna96cbb42009-05-13 15:06:13 -07001078 pkg.applicationInfo.targetSdkVersion
1079 = android.os.Build.VERSION_CODES.CUR_DEVELOPMENT;
1080 } else {
1081 pkg.applicationInfo.targetSdkVersion = targetVers;
Dianne Hackborn851a5412009-05-08 12:06:44 -07001082 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 }
1084
1085 XmlUtils.skipCurrentTag(parser);
1086
Dianne Hackborn723738c2009-06-25 19:48:04 -07001087 } else if (tagName.equals("supports-screens")) {
1088 sa = res.obtainAttributes(attrs,
1089 com.android.internal.R.styleable.AndroidManifestSupportsScreens);
1090
Dianne Hackborndf6e9802011-05-26 14:20:23 -07001091 pkg.applicationInfo.requiresSmallestWidthDp = sa.getInteger(
1092 com.android.internal.R.styleable.AndroidManifestSupportsScreens_requiresSmallestWidthDp,
1093 0);
1094 pkg.applicationInfo.compatibleWidthLimitDp = sa.getInteger(
1095 com.android.internal.R.styleable.AndroidManifestSupportsScreens_compatibleWidthLimitDp,
1096 0);
Dianne Hackborn2762ff32011-06-01 21:27:05 -07001097 pkg.applicationInfo.largestWidthLimitDp = sa.getInteger(
1098 com.android.internal.R.styleable.AndroidManifestSupportsScreens_largestWidthLimitDp,
1099 0);
Dianne Hackborndf6e9802011-05-26 14:20:23 -07001100
Dianne Hackborn723738c2009-06-25 19:48:04 -07001101 // This is a trick to get a boolean and still able to detect
1102 // if a value was actually set.
1103 supportsSmallScreens = sa.getInteger(
1104 com.android.internal.R.styleable.AndroidManifestSupportsScreens_smallScreens,
1105 supportsSmallScreens);
1106 supportsNormalScreens = sa.getInteger(
1107 com.android.internal.R.styleable.AndroidManifestSupportsScreens_normalScreens,
1108 supportsNormalScreens);
1109 supportsLargeScreens = sa.getInteger(
1110 com.android.internal.R.styleable.AndroidManifestSupportsScreens_largeScreens,
1111 supportsLargeScreens);
Dianne Hackborn14cee9f2010-04-23 17:51:26 -07001112 supportsXLargeScreens = sa.getInteger(
1113 com.android.internal.R.styleable.AndroidManifestSupportsScreens_xlargeScreens,
1114 supportsXLargeScreens);
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001115 resizeable = sa.getInteger(
1116 com.android.internal.R.styleable.AndroidManifestSupportsScreens_resizeable,
Dianne Hackborn14cee9f2010-04-23 17:51:26 -07001117 resizeable);
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001118 anyDensity = sa.getInteger(
1119 com.android.internal.R.styleable.AndroidManifestSupportsScreens_anyDensity,
1120 anyDensity);
Dianne Hackborn723738c2009-06-25 19:48:04 -07001121
1122 sa.recycle();
1123
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001124 XmlUtils.skipCurrentTag(parser);
Dianne Hackborn854060a2009-07-09 18:14:31 -07001125
1126 } else if (tagName.equals("protected-broadcast")) {
1127 sa = res.obtainAttributes(attrs,
1128 com.android.internal.R.styleable.AndroidManifestProtectedBroadcast);
1129
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001130 // Note: don't allow this value to be a reference to a resource
1131 // that may change.
Dianne Hackborn854060a2009-07-09 18:14:31 -07001132 String name = sa.getNonResourceString(
1133 com.android.internal.R.styleable.AndroidManifestProtectedBroadcast_name);
1134
1135 sa.recycle();
1136
1137 if (name != null && (flags&PARSE_IS_SYSTEM) != 0) {
1138 if (pkg.protectedBroadcasts == null) {
1139 pkg.protectedBroadcasts = new ArrayList<String>();
1140 }
1141 if (!pkg.protectedBroadcasts.contains(name)) {
1142 pkg.protectedBroadcasts.add(name.intern());
1143 }
1144 }
1145
1146 XmlUtils.skipCurrentTag(parser);
1147
1148 } else if (tagName.equals("instrumentation")) {
1149 if (parseInstrumentation(pkg, res, parser, attrs, outError) == null) {
1150 return null;
1151 }
1152
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001153 } else if (tagName.equals("original-package")) {
1154 sa = res.obtainAttributes(attrs,
1155 com.android.internal.R.styleable.AndroidManifestOriginalPackage);
1156
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001157 String orig =sa.getNonConfigurationString(
1158 com.android.internal.R.styleable.AndroidManifestOriginalPackage_name, 0);
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08001159 if (!pkg.packageName.equals(orig)) {
Dianne Hackbornc1552392010-03-03 16:19:01 -08001160 if (pkg.mOriginalPackages == null) {
1161 pkg.mOriginalPackages = new ArrayList<String>();
1162 pkg.mRealPackage = pkg.packageName;
1163 }
1164 pkg.mOriginalPackages.add(orig);
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08001165 }
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001166
1167 sa.recycle();
1168
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001169 XmlUtils.skipCurrentTag(parser);
1170
1171 } else if (tagName.equals("adopt-permissions")) {
1172 sa = res.obtainAttributes(attrs,
1173 com.android.internal.R.styleable.AndroidManifestOriginalPackage);
1174
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001175 String name = sa.getNonConfigurationString(
1176 com.android.internal.R.styleable.AndroidManifestOriginalPackage_name, 0);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001177
1178 sa.recycle();
1179
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08001180 if (name != null) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001181 if (pkg.mAdoptPermissions == null) {
1182 pkg.mAdoptPermissions = new ArrayList<String>();
1183 }
1184 pkg.mAdoptPermissions.add(name);
1185 }
1186
1187 XmlUtils.skipCurrentTag(parser);
1188
Dianne Hackborna0b46c92010-10-21 15:32:06 -07001189 } else if (tagName.equals("uses-gl-texture")) {
1190 // Just skip this tag
1191 XmlUtils.skipCurrentTag(parser);
1192 continue;
1193
1194 } else if (tagName.equals("compatible-screens")) {
1195 // Just skip this tag
1196 XmlUtils.skipCurrentTag(parser);
1197 continue;
1198
Dianne Hackborn854060a2009-07-09 18:14:31 -07001199 } else if (tagName.equals("eat-comment")) {
1200 // Just skip this tag
1201 XmlUtils.skipCurrentTag(parser);
1202 continue;
1203
1204 } else if (RIGID_PARSER) {
1205 outError[0] = "Bad element under <manifest>: "
1206 + parser.getName();
1207 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1208 return null;
1209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -07001211 Slog.w(TAG, "Unknown element under <manifest>: " + parser.getName()
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -07001212 + " at " + mArchiveSourcePath + " "
1213 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 XmlUtils.skipCurrentTag(parser);
1215 continue;
1216 }
1217 }
1218
1219 if (!foundApp && pkg.instrumentation.size() == 0) {
1220 outError[0] = "<manifest> does not contain an <application> or <instrumentation>";
1221 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_EMPTY;
1222 }
1223
Dianne Hackborna96cbb42009-05-13 15:06:13 -07001224 final int NP = PackageParser.NEW_PERMISSIONS.length;
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001225 StringBuilder implicitPerms = null;
Dianne Hackborna96cbb42009-05-13 15:06:13 -07001226 for (int ip=0; ip<NP; ip++) {
1227 final PackageParser.NewPermissionInfo npi
1228 = PackageParser.NEW_PERMISSIONS[ip];
1229 if (pkg.applicationInfo.targetSdkVersion >= npi.sdkVersion) {
1230 break;
1231 }
1232 if (!pkg.requestedPermissions.contains(npi.name)) {
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001233 if (implicitPerms == null) {
1234 implicitPerms = new StringBuilder(128);
1235 implicitPerms.append(pkg.packageName);
1236 implicitPerms.append(": compat added ");
1237 } else {
1238 implicitPerms.append(' ');
1239 }
1240 implicitPerms.append(npi.name);
Dianne Hackborna96cbb42009-05-13 15:06:13 -07001241 pkg.requestedPermissions.add(npi.name);
Dianne Hackborn65696252012-03-05 18:49:21 -08001242 pkg.requestedPermissionsRequired.add(Boolean.TRUE);
Dianne Hackborna96cbb42009-05-13 15:06:13 -07001243 }
Dianne Hackborn851a5412009-05-08 12:06:44 -07001244 }
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001245 if (implicitPerms != null) {
Kenny Rootd2d29252011-08-08 11:27:57 -07001246 Slog.i(TAG, implicitPerms.toString());
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001247 }
Dianne Hackborn851a5412009-05-08 12:06:44 -07001248
Dianne Hackborn723738c2009-06-25 19:48:04 -07001249 if (supportsSmallScreens < 0 || (supportsSmallScreens > 0
1250 && pkg.applicationInfo.targetSdkVersion
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001251 >= android.os.Build.VERSION_CODES.DONUT)) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07001252 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_SMALL_SCREENS;
1253 }
1254 if (supportsNormalScreens != 0) {
1255 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_NORMAL_SCREENS;
1256 }
1257 if (supportsLargeScreens < 0 || (supportsLargeScreens > 0
1258 && pkg.applicationInfo.targetSdkVersion
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001259 >= android.os.Build.VERSION_CODES.DONUT)) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07001260 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_LARGE_SCREENS;
1261 }
Dianne Hackborn14cee9f2010-04-23 17:51:26 -07001262 if (supportsXLargeScreens < 0 || (supportsXLargeScreens > 0
1263 && pkg.applicationInfo.targetSdkVersion
1264 >= android.os.Build.VERSION_CODES.GINGERBREAD)) {
1265 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_XLARGE_SCREENS;
1266 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001267 if (resizeable < 0 || (resizeable > 0
1268 && pkg.applicationInfo.targetSdkVersion
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001269 >= android.os.Build.VERSION_CODES.DONUT)) {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001270 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_RESIZEABLE_FOR_SCREENS;
1271 }
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001272 if (anyDensity < 0 || (anyDensity > 0
1273 && pkg.applicationInfo.targetSdkVersion
1274 >= android.os.Build.VERSION_CODES.DONUT)) {
1275 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES;
Mitsuru Oshima8d112672009-04-27 12:01:23 -07001276 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07001277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 return pkg;
1279 }
1280
1281 private static String buildClassName(String pkg, CharSequence clsSeq,
1282 String[] outError) {
1283 if (clsSeq == null || clsSeq.length() <= 0) {
1284 outError[0] = "Empty class name in package " + pkg;
1285 return null;
1286 }
1287 String cls = clsSeq.toString();
1288 char c = cls.charAt(0);
1289 if (c == '.') {
1290 return (pkg + cls).intern();
1291 }
1292 if (cls.indexOf('.') < 0) {
1293 StringBuilder b = new StringBuilder(pkg);
1294 b.append('.');
1295 b.append(cls);
1296 return b.toString().intern();
1297 }
1298 if (c >= 'a' && c <= 'z') {
1299 return cls.intern();
1300 }
1301 outError[0] = "Bad class name " + cls + " in package " + pkg;
1302 return null;
1303 }
1304
1305 private static String buildCompoundName(String pkg,
1306 CharSequence procSeq, String type, String[] outError) {
1307 String proc = procSeq.toString();
1308 char c = proc.charAt(0);
1309 if (pkg != null && c == ':') {
1310 if (proc.length() < 2) {
1311 outError[0] = "Bad " + type + " name " + proc + " in package " + pkg
1312 + ": must be at least two characters";
1313 return null;
1314 }
1315 String subName = proc.substring(1);
1316 String nameError = validateName(subName, false);
1317 if (nameError != null) {
1318 outError[0] = "Invalid " + type + " name " + proc + " in package "
1319 + pkg + ": " + nameError;
1320 return null;
1321 }
1322 return (pkg + proc).intern();
1323 }
1324 String nameError = validateName(proc, true);
1325 if (nameError != null && !"system".equals(proc)) {
1326 outError[0] = "Invalid " + type + " name " + proc + " in package "
1327 + pkg + ": " + nameError;
1328 return null;
1329 }
1330 return proc.intern();
1331 }
1332
1333 private static String buildProcessName(String pkg, String defProc,
1334 CharSequence procSeq, int flags, String[] separateProcesses,
1335 String[] outError) {
1336 if ((flags&PARSE_IGNORE_PROCESSES) != 0 && !"system".equals(procSeq)) {
1337 return defProc != null ? defProc : pkg;
1338 }
1339 if (separateProcesses != null) {
1340 for (int i=separateProcesses.length-1; i>=0; i--) {
1341 String sp = separateProcesses[i];
1342 if (sp.equals(pkg) || sp.equals(defProc) || sp.equals(procSeq)) {
1343 return pkg;
1344 }
1345 }
1346 }
1347 if (procSeq == null || procSeq.length() <= 0) {
1348 return defProc;
1349 }
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07001350 return buildCompoundName(pkg, procSeq, "process", outError);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 }
1352
1353 private static String buildTaskAffinityName(String pkg, String defProc,
1354 CharSequence procSeq, String[] outError) {
1355 if (procSeq == null) {
1356 return defProc;
1357 }
1358 if (procSeq.length() <= 0) {
1359 return null;
1360 }
1361 return buildCompoundName(pkg, procSeq, "taskAffinity", outError);
1362 }
1363
1364 private PermissionGroup parsePermissionGroup(Package owner, Resources res,
1365 XmlPullParser parser, AttributeSet attrs, String[] outError)
1366 throws XmlPullParserException, IOException {
1367 PermissionGroup perm = new PermissionGroup(owner);
1368
1369 TypedArray sa = res.obtainAttributes(attrs,
1370 com.android.internal.R.styleable.AndroidManifestPermissionGroup);
1371
1372 if (!parsePackageItemInfo(owner, perm.info, outError,
1373 "<permission-group>", sa,
1374 com.android.internal.R.styleable.AndroidManifestPermissionGroup_name,
1375 com.android.internal.R.styleable.AndroidManifestPermissionGroup_label,
Adam Powell81cd2e92010-04-21 16:35:18 -07001376 com.android.internal.R.styleable.AndroidManifestPermissionGroup_icon,
1377 com.android.internal.R.styleable.AndroidManifestPermissionGroup_logo)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 sa.recycle();
1379 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1380 return null;
1381 }
1382
1383 perm.info.descriptionRes = sa.getResourceId(
1384 com.android.internal.R.styleable.AndroidManifestPermissionGroup_description,
1385 0);
1386
1387 sa.recycle();
1388
1389 if (!parseAllMetaData(res, parser, attrs, "<permission-group>", perm,
1390 outError)) {
1391 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1392 return null;
1393 }
1394
1395 owner.permissionGroups.add(perm);
1396
1397 return perm;
1398 }
1399
1400 private Permission parsePermission(Package owner, Resources res,
1401 XmlPullParser parser, AttributeSet attrs, String[] outError)
1402 throws XmlPullParserException, IOException {
1403 Permission perm = new Permission(owner);
1404
1405 TypedArray sa = res.obtainAttributes(attrs,
1406 com.android.internal.R.styleable.AndroidManifestPermission);
1407
1408 if (!parsePackageItemInfo(owner, perm.info, outError,
1409 "<permission>", sa,
1410 com.android.internal.R.styleable.AndroidManifestPermission_name,
1411 com.android.internal.R.styleable.AndroidManifestPermission_label,
Adam Powell81cd2e92010-04-21 16:35:18 -07001412 com.android.internal.R.styleable.AndroidManifestPermission_icon,
1413 com.android.internal.R.styleable.AndroidManifestPermission_logo)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 sa.recycle();
1415 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1416 return null;
1417 }
1418
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001419 // Note: don't allow this value to be a reference to a resource
1420 // that may change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 perm.info.group = sa.getNonResourceString(
1422 com.android.internal.R.styleable.AndroidManifestPermission_permissionGroup);
1423 if (perm.info.group != null) {
1424 perm.info.group = perm.info.group.intern();
1425 }
1426
1427 perm.info.descriptionRes = sa.getResourceId(
1428 com.android.internal.R.styleable.AndroidManifestPermission_description,
1429 0);
1430
1431 perm.info.protectionLevel = sa.getInt(
1432 com.android.internal.R.styleable.AndroidManifestPermission_protectionLevel,
1433 PermissionInfo.PROTECTION_NORMAL);
1434
1435 sa.recycle();
Dianne Hackborne639da72012-02-21 15:11:13 -08001436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 if (perm.info.protectionLevel == -1) {
1438 outError[0] = "<permission> does not specify protectionLevel";
1439 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1440 return null;
1441 }
Dianne Hackborne639da72012-02-21 15:11:13 -08001442
1443 perm.info.protectionLevel = PermissionInfo.fixProtectionLevel(perm.info.protectionLevel);
1444
1445 if ((perm.info.protectionLevel&PermissionInfo.PROTECTION_MASK_FLAGS) != 0) {
1446 if ((perm.info.protectionLevel&PermissionInfo.PROTECTION_MASK_BASE) !=
1447 PermissionInfo.PROTECTION_SIGNATURE) {
1448 outError[0] = "<permission> protectionLevel specifies a flag but is "
1449 + "not based on signature type";
1450 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1451 return null;
1452 }
1453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454
1455 if (!parseAllMetaData(res, parser, attrs, "<permission>", perm,
1456 outError)) {
1457 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1458 return null;
1459 }
1460
1461 owner.permissions.add(perm);
1462
1463 return perm;
1464 }
1465
1466 private Permission parsePermissionTree(Package owner, Resources res,
1467 XmlPullParser parser, AttributeSet attrs, String[] outError)
1468 throws XmlPullParserException, IOException {
1469 Permission perm = new Permission(owner);
1470
1471 TypedArray sa = res.obtainAttributes(attrs,
1472 com.android.internal.R.styleable.AndroidManifestPermissionTree);
1473
1474 if (!parsePackageItemInfo(owner, perm.info, outError,
1475 "<permission-tree>", sa,
1476 com.android.internal.R.styleable.AndroidManifestPermissionTree_name,
1477 com.android.internal.R.styleable.AndroidManifestPermissionTree_label,
Adam Powell81cd2e92010-04-21 16:35:18 -07001478 com.android.internal.R.styleable.AndroidManifestPermissionTree_icon,
1479 com.android.internal.R.styleable.AndroidManifestPermissionTree_logo)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 sa.recycle();
1481 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1482 return null;
1483 }
1484
1485 sa.recycle();
1486
1487 int index = perm.info.name.indexOf('.');
1488 if (index > 0) {
1489 index = perm.info.name.indexOf('.', index+1);
1490 }
1491 if (index < 0) {
1492 outError[0] = "<permission-tree> name has less than three segments: "
1493 + perm.info.name;
1494 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1495 return null;
1496 }
1497
1498 perm.info.descriptionRes = 0;
1499 perm.info.protectionLevel = PermissionInfo.PROTECTION_NORMAL;
1500 perm.tree = true;
1501
1502 if (!parseAllMetaData(res, parser, attrs, "<permission-tree>", perm,
1503 outError)) {
1504 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1505 return null;
1506 }
1507
1508 owner.permissions.add(perm);
1509
1510 return perm;
1511 }
1512
1513 private Instrumentation parseInstrumentation(Package owner, Resources res,
1514 XmlPullParser parser, AttributeSet attrs, String[] outError)
1515 throws XmlPullParserException, IOException {
1516 TypedArray sa = res.obtainAttributes(attrs,
1517 com.android.internal.R.styleable.AndroidManifestInstrumentation);
1518
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001519 if (mParseInstrumentationArgs == null) {
1520 mParseInstrumentationArgs = new ParsePackageItemArgs(owner, outError,
1521 com.android.internal.R.styleable.AndroidManifestInstrumentation_name,
1522 com.android.internal.R.styleable.AndroidManifestInstrumentation_label,
Adam Powell81cd2e92010-04-21 16:35:18 -07001523 com.android.internal.R.styleable.AndroidManifestInstrumentation_icon,
1524 com.android.internal.R.styleable.AndroidManifestInstrumentation_logo);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001525 mParseInstrumentationArgs.tag = "<instrumentation>";
1526 }
1527
1528 mParseInstrumentationArgs.sa = sa;
1529
1530 Instrumentation a = new Instrumentation(mParseInstrumentationArgs,
1531 new InstrumentationInfo());
1532 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 sa.recycle();
1534 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1535 return null;
1536 }
1537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 String str;
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001539 // Note: don't allow this value to be a reference to a resource
1540 // that may change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 str = sa.getNonResourceString(
1542 com.android.internal.R.styleable.AndroidManifestInstrumentation_targetPackage);
1543 a.info.targetPackage = str != null ? str.intern() : null;
1544
1545 a.info.handleProfiling = sa.getBoolean(
1546 com.android.internal.R.styleable.AndroidManifestInstrumentation_handleProfiling,
1547 false);
1548
1549 a.info.functionalTest = sa.getBoolean(
1550 com.android.internal.R.styleable.AndroidManifestInstrumentation_functionalTest,
1551 false);
1552
1553 sa.recycle();
1554
1555 if (a.info.targetPackage == null) {
1556 outError[0] = "<instrumentation> does not specify targetPackage";
1557 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1558 return null;
1559 }
1560
1561 if (!parseAllMetaData(res, parser, attrs, "<instrumentation>", a,
1562 outError)) {
1563 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1564 return null;
1565 }
1566
1567 owner.instrumentation.add(a);
1568
1569 return a;
1570 }
1571
1572 private boolean parseApplication(Package owner, Resources res,
1573 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
1574 throws XmlPullParserException, IOException {
1575 final ApplicationInfo ai = owner.applicationInfo;
1576 final String pkgName = owner.applicationInfo.packageName;
1577
1578 TypedArray sa = res.obtainAttributes(attrs,
1579 com.android.internal.R.styleable.AndroidManifestApplication);
1580
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001581 String name = sa.getNonConfigurationString(
1582 com.android.internal.R.styleable.AndroidManifestApplication_name, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 if (name != null) {
1584 ai.className = buildClassName(pkgName, name, outError);
1585 if (ai.className == null) {
1586 sa.recycle();
1587 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1588 return false;
1589 }
1590 }
1591
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001592 String manageSpaceActivity = sa.getNonConfigurationString(
1593 com.android.internal.R.styleable.AndroidManifestApplication_manageSpaceActivity, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 if (manageSpaceActivity != null) {
1595 ai.manageSpaceActivityName = buildClassName(pkgName, manageSpaceActivity,
1596 outError);
1597 }
1598
Christopher Tate181fafa2009-05-14 11:12:14 -07001599 boolean allowBackup = sa.getBoolean(
1600 com.android.internal.R.styleable.AndroidManifestApplication_allowBackup, true);
1601 if (allowBackup) {
1602 ai.flags |= ApplicationInfo.FLAG_ALLOW_BACKUP;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001603
Christopher Tate3de55bc2010-03-12 17:28:08 -08001604 // backupAgent, killAfterRestore, and restoreAnyVersion are only relevant
1605 // if backup is possible for the given application.
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001606 String backupAgent = sa.getNonConfigurationString(
1607 com.android.internal.R.styleable.AndroidManifestApplication_backupAgent, 0);
Christopher Tate181fafa2009-05-14 11:12:14 -07001608 if (backupAgent != null) {
1609 ai.backupAgentName = buildClassName(pkgName, backupAgent, outError);
Kenny Rootd2d29252011-08-08 11:27:57 -07001610 if (DEBUG_BACKUP) {
1611 Slog.v(TAG, "android:backupAgent = " + ai.backupAgentName
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001612 + " from " + pkgName + "+" + backupAgent);
1613 }
Christopher Tate5e1ab332009-09-01 20:32:49 -07001614
1615 if (sa.getBoolean(
1616 com.android.internal.R.styleable.AndroidManifestApplication_killAfterRestore,
1617 true)) {
1618 ai.flags |= ApplicationInfo.FLAG_KILL_AFTER_RESTORE;
1619 }
1620 if (sa.getBoolean(
Christopher Tate3dda5182010-02-24 16:06:18 -08001621 com.android.internal.R.styleable.AndroidManifestApplication_restoreAnyVersion,
1622 false)) {
1623 ai.flags |= ApplicationInfo.FLAG_RESTORE_ANY_VERSION;
1624 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001625 }
1626 }
Christopher Tate4a627c72011-04-01 14:43:32 -07001627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 TypedValue v = sa.peekValue(
1629 com.android.internal.R.styleable.AndroidManifestApplication_label);
1630 if (v != null && (ai.labelRes=v.resourceId) == 0) {
1631 ai.nonLocalizedLabel = v.coerceToString();
1632 }
1633
1634 ai.icon = sa.getResourceId(
1635 com.android.internal.R.styleable.AndroidManifestApplication_icon, 0);
Adam Powell81cd2e92010-04-21 16:35:18 -07001636 ai.logo = sa.getResourceId(
1637 com.android.internal.R.styleable.AndroidManifestApplication_logo, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 ai.theme = sa.getResourceId(
Dianne Hackbornb35cd542011-01-04 21:30:53 -08001639 com.android.internal.R.styleable.AndroidManifestApplication_theme, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 ai.descriptionRes = sa.getResourceId(
1641 com.android.internal.R.styleable.AndroidManifestApplication_description, 0);
1642
1643 if ((flags&PARSE_IS_SYSTEM) != 0) {
1644 if (sa.getBoolean(
1645 com.android.internal.R.styleable.AndroidManifestApplication_persistent,
1646 false)) {
1647 ai.flags |= ApplicationInfo.FLAG_PERSISTENT;
1648 }
1649 }
1650
Suchi Amalapurapuaf8e9f42010-01-12 10:17:28 -08001651 if ((flags & PARSE_FORWARD_LOCK) != 0) {
1652 ai.flags |= ApplicationInfo.FLAG_FORWARD_LOCK;
1653 }
1654
1655 if ((flags & PARSE_ON_SDCARD) != 0) {
Suchi Amalapurapu6069beb2010-03-10 09:46:49 -08001656 ai.flags |= ApplicationInfo.FLAG_EXTERNAL_STORAGE;
Suchi Amalapurapuaf8e9f42010-01-12 10:17:28 -08001657 }
1658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 if (sa.getBoolean(
1660 com.android.internal.R.styleable.AndroidManifestApplication_debuggable,
1661 false)) {
1662 ai.flags |= ApplicationInfo.FLAG_DEBUGGABLE;
1663 }
1664
1665 if (sa.getBoolean(
Ben Chengef3f5dd2010-03-29 15:47:26 -07001666 com.android.internal.R.styleable.AndroidManifestApplication_vmSafeMode,
Ben Cheng23085b72010-02-08 16:06:32 -08001667 false)) {
1668 ai.flags |= ApplicationInfo.FLAG_VM_SAFE_MODE;
1669 }
1670
Romain Guy529b60a2010-08-03 18:05:47 -07001671 boolean hardwareAccelerated = sa.getBoolean(
Romain Guy812ccbe2010-06-01 14:07:24 -07001672 com.android.internal.R.styleable.AndroidManifestApplication_hardwareAccelerated,
Dianne Hackborn2d6833b2011-06-24 16:04:19 -07001673 owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.ICE_CREAM_SANDWICH);
Romain Guy812ccbe2010-06-01 14:07:24 -07001674
1675 if (sa.getBoolean(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 com.android.internal.R.styleable.AndroidManifestApplication_hasCode,
1677 true)) {
1678 ai.flags |= ApplicationInfo.FLAG_HAS_CODE;
1679 }
1680
1681 if (sa.getBoolean(
1682 com.android.internal.R.styleable.AndroidManifestApplication_allowTaskReparenting,
1683 false)) {
1684 ai.flags |= ApplicationInfo.FLAG_ALLOW_TASK_REPARENTING;
1685 }
1686
1687 if (sa.getBoolean(
1688 com.android.internal.R.styleable.AndroidManifestApplication_allowClearUserData,
1689 true)) {
1690 ai.flags |= ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA;
1691 }
1692
Dianne Hackbornade3eca2009-05-11 18:54:45 -07001693 if (sa.getBoolean(
1694 com.android.internal.R.styleable.AndroidManifestApplication_testOnly,
Dianne Hackborne7fe35b2009-05-13 10:53:41 -07001695 false)) {
Dianne Hackbornade3eca2009-05-11 18:54:45 -07001696 ai.flags |= ApplicationInfo.FLAG_TEST_ONLY;
1697 }
1698
Jason parksa3cdaa52011-01-13 14:15:43 -06001699 if (sa.getBoolean(
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08001700 com.android.internal.R.styleable.AndroidManifestApplication_largeHeap,
Jason parksa3cdaa52011-01-13 14:15:43 -06001701 false)) {
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08001702 ai.flags |= ApplicationInfo.FLAG_LARGE_HEAP;
Jason parksa3cdaa52011-01-13 14:15:43 -06001703 }
1704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 String str;
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001706 str = sa.getNonConfigurationString(
1707 com.android.internal.R.styleable.AndroidManifestApplication_permission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 ai.permission = (str != null && str.length() > 0) ? str.intern() : null;
1709
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07001710 if (owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.FROYO) {
1711 str = sa.getNonConfigurationString(
1712 com.android.internal.R.styleable.AndroidManifestApplication_taskAffinity, 0);
1713 } else {
1714 // Some older apps have been seen to use a resource reference
1715 // here that on older builds was ignored (with a warning). We
1716 // need to continue to do this for them so they don't break.
1717 str = sa.getNonResourceString(
1718 com.android.internal.R.styleable.AndroidManifestApplication_taskAffinity);
1719 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 ai.taskAffinity = buildTaskAffinityName(ai.packageName, ai.packageName,
1721 str, outError);
1722
1723 if (outError[0] == null) {
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07001724 CharSequence pname;
1725 if (owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.FROYO) {
1726 pname = sa.getNonConfigurationString(
1727 com.android.internal.R.styleable.AndroidManifestApplication_process, 0);
1728 } else {
1729 // Some older apps have been seen to use a resource reference
1730 // here that on older builds was ignored (with a warning). We
1731 // need to continue to do this for them so they don't break.
1732 pname = sa.getNonResourceString(
1733 com.android.internal.R.styleable.AndroidManifestApplication_process);
1734 }
1735 ai.processName = buildProcessName(ai.packageName, null, pname,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 flags, mSeparateProcesses, outError);
1737
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001738 ai.enabled = sa.getBoolean(
1739 com.android.internal.R.styleable.AndroidManifestApplication_enabled, true);
Dianne Hackborn860755f2010-06-03 18:47:52 -07001740
Dianne Hackborn02486b12010-08-26 14:18:37 -07001741 if (false) {
1742 if (sa.getBoolean(
1743 com.android.internal.R.styleable.AndroidManifestApplication_cantSaveState,
1744 false)) {
Dianne Hackborn54e570f2010-10-04 18:32:32 -07001745 ai.flags |= ApplicationInfo.FLAG_CANT_SAVE_STATE;
Dianne Hackborn02486b12010-08-26 14:18:37 -07001746
1747 // A heavy-weight application can not be in a custom process.
1748 // We can do direct compare because we intern all strings.
1749 if (ai.processName != null && ai.processName != ai.packageName) {
1750 outError[0] = "cantSaveState applications can not use custom processes";
1751 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07001752 }
1753 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 }
1755
Adam Powell269248d2011-08-02 10:26:54 -07001756 ai.uiOptions = sa.getInt(
1757 com.android.internal.R.styleable.AndroidManifestApplication_uiOptions, 0);
1758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 sa.recycle();
1760
1761 if (outError[0] != null) {
1762 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1763 return false;
1764 }
1765
1766 final int innerDepth = parser.getDepth();
1767
1768 int type;
Kenny Rootd2d29252011-08-08 11:27:57 -07001769 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1770 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1771 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 continue;
1773 }
1774
1775 String tagName = parser.getName();
1776 if (tagName.equals("activity")) {
Romain Guy529b60a2010-08-03 18:05:47 -07001777 Activity a = parseActivity(owner, res, parser, attrs, flags, outError, false,
1778 hardwareAccelerated);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 if (a == null) {
1780 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1781 return false;
1782 }
1783
1784 owner.activities.add(a);
1785
1786 } else if (tagName.equals("receiver")) {
Romain Guy529b60a2010-08-03 18:05:47 -07001787 Activity a = parseActivity(owner, res, parser, attrs, flags, outError, true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 if (a == null) {
1789 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1790 return false;
1791 }
1792
1793 owner.receivers.add(a);
1794
1795 } else if (tagName.equals("service")) {
1796 Service s = parseService(owner, res, parser, attrs, flags, outError);
1797 if (s == null) {
1798 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1799 return false;
1800 }
1801
1802 owner.services.add(s);
1803
1804 } else if (tagName.equals("provider")) {
1805 Provider p = parseProvider(owner, res, parser, attrs, flags, outError);
1806 if (p == null) {
1807 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1808 return false;
1809 }
1810
1811 owner.providers.add(p);
1812
1813 } else if (tagName.equals("activity-alias")) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001814 Activity a = parseActivityAlias(owner, res, parser, attrs, flags, outError);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 if (a == null) {
1816 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1817 return false;
1818 }
1819
1820 owner.activities.add(a);
1821
1822 } else if (parser.getName().equals("meta-data")) {
1823 // note: application meta-data is stored off to the side, so it can
1824 // remain null in the primary copy (we like to avoid extra copies because
1825 // it can be large)
1826 if ((owner.mAppMetaData = parseMetaData(res, parser, attrs, owner.mAppMetaData,
1827 outError)) == null) {
1828 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1829 return false;
1830 }
1831
1832 } else if (tagName.equals("uses-library")) {
1833 sa = res.obtainAttributes(attrs,
1834 com.android.internal.R.styleable.AndroidManifestUsesLibrary);
1835
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001836 // Note: don't allow this value to be a reference to a resource
1837 // that may change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 String lname = sa.getNonResourceString(
1839 com.android.internal.R.styleable.AndroidManifestUsesLibrary_name);
Dianne Hackborn49237342009-08-27 20:08:01 -07001840 boolean req = sa.getBoolean(
1841 com.android.internal.R.styleable.AndroidManifestUsesLibrary_required,
1842 true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843
1844 sa.recycle();
1845
Dianne Hackborn49237342009-08-27 20:08:01 -07001846 if (lname != null) {
1847 if (req) {
1848 if (owner.usesLibraries == null) {
1849 owner.usesLibraries = new ArrayList<String>();
1850 }
1851 if (!owner.usesLibraries.contains(lname)) {
1852 owner.usesLibraries.add(lname.intern());
1853 }
1854 } else {
1855 if (owner.usesOptionalLibraries == null) {
1856 owner.usesOptionalLibraries = new ArrayList<String>();
1857 }
1858 if (!owner.usesOptionalLibraries.contains(lname)) {
1859 owner.usesOptionalLibraries.add(lname.intern());
1860 }
1861 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 }
1863
1864 XmlUtils.skipCurrentTag(parser);
1865
Dianne Hackborncef65ee2010-09-30 18:27:22 -07001866 } else if (tagName.equals("uses-package")) {
1867 // Dependencies for app installers; we don't currently try to
1868 // enforce this.
1869 XmlUtils.skipCurrentTag(parser);
1870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 } else {
1872 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07001873 Slog.w(TAG, "Unknown element under <application>: " + tagName
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001874 + " at " + mArchiveSourcePath + " "
1875 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 XmlUtils.skipCurrentTag(parser);
1877 continue;
1878 } else {
1879 outError[0] = "Bad element under <application>: " + tagName;
1880 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1881 return false;
1882 }
1883 }
1884 }
1885
1886 return true;
1887 }
1888
1889 private boolean parsePackageItemInfo(Package owner, PackageItemInfo outInfo,
1890 String[] outError, String tag, TypedArray sa,
Adam Powell81cd2e92010-04-21 16:35:18 -07001891 int nameRes, int labelRes, int iconRes, int logoRes) {
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001892 String name = sa.getNonConfigurationString(nameRes, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 if (name == null) {
1894 outError[0] = tag + " does not specify android:name";
1895 return false;
1896 }
1897
1898 outInfo.name
1899 = buildClassName(owner.applicationInfo.packageName, name, outError);
1900 if (outInfo.name == null) {
1901 return false;
1902 }
1903
1904 int iconVal = sa.getResourceId(iconRes, 0);
1905 if (iconVal != 0) {
1906 outInfo.icon = iconVal;
1907 outInfo.nonLocalizedLabel = null;
1908 }
Adam Powell81cd2e92010-04-21 16:35:18 -07001909
1910 int logoVal = sa.getResourceId(logoRes, 0);
1911 if (logoVal != 0) {
1912 outInfo.logo = logoVal;
1913 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914
1915 TypedValue v = sa.peekValue(labelRes);
1916 if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
1917 outInfo.nonLocalizedLabel = v.coerceToString();
1918 }
1919
1920 outInfo.packageName = owner.packageName;
1921
1922 return true;
1923 }
1924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 private Activity parseActivity(Package owner, Resources res,
1926 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError,
Romain Guy529b60a2010-08-03 18:05:47 -07001927 boolean receiver, boolean hardwareAccelerated)
1928 throws XmlPullParserException, IOException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 TypedArray sa = res.obtainAttributes(attrs,
1930 com.android.internal.R.styleable.AndroidManifestActivity);
1931
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001932 if (mParseActivityArgs == null) {
1933 mParseActivityArgs = new ParseComponentArgs(owner, outError,
1934 com.android.internal.R.styleable.AndroidManifestActivity_name,
1935 com.android.internal.R.styleable.AndroidManifestActivity_label,
1936 com.android.internal.R.styleable.AndroidManifestActivity_icon,
Adam Powell81cd2e92010-04-21 16:35:18 -07001937 com.android.internal.R.styleable.AndroidManifestActivity_logo,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001938 mSeparateProcesses,
1939 com.android.internal.R.styleable.AndroidManifestActivity_process,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001940 com.android.internal.R.styleable.AndroidManifestActivity_description,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001941 com.android.internal.R.styleable.AndroidManifestActivity_enabled);
1942 }
1943
1944 mParseActivityArgs.tag = receiver ? "<receiver>" : "<activity>";
1945 mParseActivityArgs.sa = sa;
1946 mParseActivityArgs.flags = flags;
1947
1948 Activity a = new Activity(mParseActivityArgs, new ActivityInfo());
1949 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 sa.recycle();
1951 return null;
1952 }
1953
1954 final boolean setExported = sa.hasValue(
1955 com.android.internal.R.styleable.AndroidManifestActivity_exported);
1956 if (setExported) {
1957 a.info.exported = sa.getBoolean(
1958 com.android.internal.R.styleable.AndroidManifestActivity_exported, false);
1959 }
1960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 a.info.theme = sa.getResourceId(
1962 com.android.internal.R.styleable.AndroidManifestActivity_theme, 0);
1963
Adam Powell269248d2011-08-02 10:26:54 -07001964 a.info.uiOptions = sa.getInt(
1965 com.android.internal.R.styleable.AndroidManifestActivity_uiOptions,
1966 a.info.applicationInfo.uiOptions);
1967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 String str;
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001969 str = sa.getNonConfigurationString(
1970 com.android.internal.R.styleable.AndroidManifestActivity_permission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 if (str == null) {
1972 a.info.permission = owner.applicationInfo.permission;
1973 } else {
1974 a.info.permission = str.length() > 0 ? str.toString().intern() : null;
1975 }
1976
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001977 str = sa.getNonConfigurationString(
1978 com.android.internal.R.styleable.AndroidManifestActivity_taskAffinity, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 a.info.taskAffinity = buildTaskAffinityName(owner.applicationInfo.packageName,
1980 owner.applicationInfo.taskAffinity, str, outError);
1981
1982 a.info.flags = 0;
1983 if (sa.getBoolean(
1984 com.android.internal.R.styleable.AndroidManifestActivity_multiprocess,
1985 false)) {
1986 a.info.flags |= ActivityInfo.FLAG_MULTIPROCESS;
1987 }
1988
1989 if (sa.getBoolean(
1990 com.android.internal.R.styleable.AndroidManifestActivity_finishOnTaskLaunch,
1991 false)) {
1992 a.info.flags |= ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH;
1993 }
1994
1995 if (sa.getBoolean(
1996 com.android.internal.R.styleable.AndroidManifestActivity_clearTaskOnLaunch,
1997 false)) {
1998 a.info.flags |= ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH;
1999 }
2000
2001 if (sa.getBoolean(
2002 com.android.internal.R.styleable.AndroidManifestActivity_noHistory,
2003 false)) {
2004 a.info.flags |= ActivityInfo.FLAG_NO_HISTORY;
2005 }
2006
2007 if (sa.getBoolean(
2008 com.android.internal.R.styleable.AndroidManifestActivity_alwaysRetainTaskState,
2009 false)) {
2010 a.info.flags |= ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE;
2011 }
2012
2013 if (sa.getBoolean(
2014 com.android.internal.R.styleable.AndroidManifestActivity_stateNotNeeded,
2015 false)) {
2016 a.info.flags |= ActivityInfo.FLAG_STATE_NOT_NEEDED;
2017 }
2018
2019 if (sa.getBoolean(
2020 com.android.internal.R.styleable.AndroidManifestActivity_excludeFromRecents,
2021 false)) {
2022 a.info.flags |= ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS;
2023 }
2024
2025 if (sa.getBoolean(
2026 com.android.internal.R.styleable.AndroidManifestActivity_allowTaskReparenting,
2027 (owner.applicationInfo.flags&ApplicationInfo.FLAG_ALLOW_TASK_REPARENTING) != 0)) {
2028 a.info.flags |= ActivityInfo.FLAG_ALLOW_TASK_REPARENTING;
2029 }
2030
Dianne Hackbornffa42482009-09-23 22:20:11 -07002031 if (sa.getBoolean(
2032 com.android.internal.R.styleable.AndroidManifestActivity_finishOnCloseSystemDialogs,
2033 false)) {
2034 a.info.flags |= ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS;
2035 }
2036
Daniel Sandler613dde42010-06-21 13:46:39 -04002037 if (sa.getBoolean(
2038 com.android.internal.R.styleable.AndroidManifestActivity_immersive,
2039 false)) {
2040 a.info.flags |= ActivityInfo.FLAG_IMMERSIVE;
2041 }
Romain Guy529b60a2010-08-03 18:05:47 -07002042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 if (!receiver) {
Romain Guy529b60a2010-08-03 18:05:47 -07002044 if (sa.getBoolean(
2045 com.android.internal.R.styleable.AndroidManifestActivity_hardwareAccelerated,
2046 hardwareAccelerated)) {
2047 a.info.flags |= ActivityInfo.FLAG_HARDWARE_ACCELERATED;
2048 }
2049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 a.info.launchMode = sa.getInt(
2051 com.android.internal.R.styleable.AndroidManifestActivity_launchMode,
2052 ActivityInfo.LAUNCH_MULTIPLE);
2053 a.info.screenOrientation = sa.getInt(
2054 com.android.internal.R.styleable.AndroidManifestActivity_screenOrientation,
2055 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
2056 a.info.configChanges = sa.getInt(
2057 com.android.internal.R.styleable.AndroidManifestActivity_configChanges,
2058 0);
2059 a.info.softInputMode = sa.getInt(
2060 com.android.internal.R.styleable.AndroidManifestActivity_windowSoftInputMode,
2061 0);
2062 } else {
2063 a.info.launchMode = ActivityInfo.LAUNCH_MULTIPLE;
2064 a.info.configChanges = 0;
2065 }
2066
2067 sa.recycle();
2068
Dianne Hackborn54e570f2010-10-04 18:32:32 -07002069 if (receiver && (owner.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07002070 // A heavy-weight application can not have receives in its main process
2071 // We can do direct compare because we intern all strings.
2072 if (a.info.processName == owner.packageName) {
2073 outError[0] = "Heavy-weight applications can not have receivers in main process";
2074 }
2075 }
2076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 if (outError[0] != null) {
2078 return null;
2079 }
2080
2081 int outerDepth = parser.getDepth();
2082 int type;
2083 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2084 && (type != XmlPullParser.END_TAG
2085 || parser.getDepth() > outerDepth)) {
2086 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2087 continue;
2088 }
2089
2090 if (parser.getName().equals("intent-filter")) {
2091 ActivityIntentInfo intent = new ActivityIntentInfo(a);
2092 if (!parseIntent(res, parser, attrs, flags, intent, outError, !receiver)) {
2093 return null;
2094 }
2095 if (intent.countActions() == 0) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002096 Slog.w(TAG, "No actions in intent filter at "
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -07002097 + mArchiveSourcePath + " "
2098 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 } else {
2100 a.intents.add(intent);
2101 }
2102 } else if (parser.getName().equals("meta-data")) {
2103 if ((a.metaData=parseMetaData(res, parser, attrs, a.metaData,
2104 outError)) == null) {
2105 return null;
2106 }
2107 } else {
2108 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002109 Slog.w(TAG, "Problem in package " + mArchiveSourcePath + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 if (receiver) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002111 Slog.w(TAG, "Unknown element under <receiver>: " + parser.getName()
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002112 + " at " + mArchiveSourcePath + " "
2113 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -07002115 Slog.w(TAG, "Unknown element under <activity>: " + parser.getName()
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002116 + " at " + mArchiveSourcePath + " "
2117 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 }
2119 XmlUtils.skipCurrentTag(parser);
2120 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -07002122 if (receiver) {
2123 outError[0] = "Bad element under <receiver>: " + parser.getName();
2124 } else {
2125 outError[0] = "Bad element under <activity>: " + parser.getName();
2126 }
2127 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 }
2130 }
2131
2132 if (!setExported) {
2133 a.info.exported = a.intents.size() > 0;
2134 }
2135
2136 return a;
2137 }
2138
2139 private Activity parseActivityAlias(Package owner, Resources res,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002140 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
2141 throws XmlPullParserException, IOException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 TypedArray sa = res.obtainAttributes(attrs,
2143 com.android.internal.R.styleable.AndroidManifestActivityAlias);
2144
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002145 String targetActivity = sa.getNonConfigurationString(
2146 com.android.internal.R.styleable.AndroidManifestActivityAlias_targetActivity, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 if (targetActivity == null) {
2148 outError[0] = "<activity-alias> does not specify android:targetActivity";
2149 sa.recycle();
2150 return null;
2151 }
2152
2153 targetActivity = buildClassName(owner.applicationInfo.packageName,
2154 targetActivity, outError);
2155 if (targetActivity == null) {
2156 sa.recycle();
2157 return null;
2158 }
2159
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002160 if (mParseActivityAliasArgs == null) {
2161 mParseActivityAliasArgs = new ParseComponentArgs(owner, outError,
2162 com.android.internal.R.styleable.AndroidManifestActivityAlias_name,
2163 com.android.internal.R.styleable.AndroidManifestActivityAlias_label,
2164 com.android.internal.R.styleable.AndroidManifestActivityAlias_icon,
Adam Powell81cd2e92010-04-21 16:35:18 -07002165 com.android.internal.R.styleable.AndroidManifestActivityAlias_logo,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002166 mSeparateProcesses,
2167 0,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08002168 com.android.internal.R.styleable.AndroidManifestActivityAlias_description,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002169 com.android.internal.R.styleable.AndroidManifestActivityAlias_enabled);
2170 mParseActivityAliasArgs.tag = "<activity-alias>";
2171 }
2172
2173 mParseActivityAliasArgs.sa = sa;
2174 mParseActivityAliasArgs.flags = flags;
2175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 Activity target = null;
2177
2178 final int NA = owner.activities.size();
2179 for (int i=0; i<NA; i++) {
2180 Activity t = owner.activities.get(i);
2181 if (targetActivity.equals(t.info.name)) {
2182 target = t;
2183 break;
2184 }
2185 }
2186
2187 if (target == null) {
2188 outError[0] = "<activity-alias> target activity " + targetActivity
2189 + " not found in manifest";
2190 sa.recycle();
2191 return null;
2192 }
2193
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002194 ActivityInfo info = new ActivityInfo();
2195 info.targetActivity = targetActivity;
2196 info.configChanges = target.info.configChanges;
2197 info.flags = target.info.flags;
2198 info.icon = target.info.icon;
Adam Powell81cd2e92010-04-21 16:35:18 -07002199 info.logo = target.info.logo;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002200 info.labelRes = target.info.labelRes;
2201 info.nonLocalizedLabel = target.info.nonLocalizedLabel;
2202 info.launchMode = target.info.launchMode;
2203 info.processName = target.info.processName;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08002204 if (info.descriptionRes == 0) {
2205 info.descriptionRes = target.info.descriptionRes;
2206 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002207 info.screenOrientation = target.info.screenOrientation;
2208 info.taskAffinity = target.info.taskAffinity;
2209 info.theme = target.info.theme;
Dianne Hackborn0836c7c2011-10-20 18:40:23 -07002210 info.softInputMode = target.info.softInputMode;
Adam Powell269248d2011-08-02 10:26:54 -07002211 info.uiOptions = target.info.uiOptions;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002212
2213 Activity a = new Activity(mParseActivityAliasArgs, info);
2214 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 sa.recycle();
2216 return null;
2217 }
2218
2219 final boolean setExported = sa.hasValue(
2220 com.android.internal.R.styleable.AndroidManifestActivityAlias_exported);
2221 if (setExported) {
2222 a.info.exported = sa.getBoolean(
2223 com.android.internal.R.styleable.AndroidManifestActivityAlias_exported, false);
2224 }
2225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 String str;
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002227 str = sa.getNonConfigurationString(
2228 com.android.internal.R.styleable.AndroidManifestActivityAlias_permission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 if (str != null) {
2230 a.info.permission = str.length() > 0 ? str.toString().intern() : null;
2231 }
2232
2233 sa.recycle();
2234
2235 if (outError[0] != null) {
2236 return null;
2237 }
2238
2239 int outerDepth = parser.getDepth();
2240 int type;
2241 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2242 && (type != XmlPullParser.END_TAG
2243 || parser.getDepth() > outerDepth)) {
2244 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2245 continue;
2246 }
2247
2248 if (parser.getName().equals("intent-filter")) {
2249 ActivityIntentInfo intent = new ActivityIntentInfo(a);
2250 if (!parseIntent(res, parser, attrs, flags, intent, outError, true)) {
2251 return null;
2252 }
2253 if (intent.countActions() == 0) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002254 Slog.w(TAG, "No actions in intent filter at "
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -07002255 + mArchiveSourcePath + " "
2256 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 } else {
2258 a.intents.add(intent);
2259 }
2260 } else if (parser.getName().equals("meta-data")) {
2261 if ((a.metaData=parseMetaData(res, parser, attrs, a.metaData,
2262 outError)) == null) {
2263 return null;
2264 }
2265 } else {
2266 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002267 Slog.w(TAG, "Unknown element under <activity-alias>: " + parser.getName()
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002268 + " at " + mArchiveSourcePath + " "
2269 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 XmlUtils.skipCurrentTag(parser);
2271 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07002272 } else {
2273 outError[0] = "Bad element under <activity-alias>: " + parser.getName();
2274 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276 }
2277 }
2278
2279 if (!setExported) {
2280 a.info.exported = a.intents.size() > 0;
2281 }
2282
2283 return a;
2284 }
2285
2286 private Provider parseProvider(Package owner, Resources res,
2287 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
2288 throws XmlPullParserException, IOException {
2289 TypedArray sa = res.obtainAttributes(attrs,
2290 com.android.internal.R.styleable.AndroidManifestProvider);
2291
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002292 if (mParseProviderArgs == null) {
2293 mParseProviderArgs = new ParseComponentArgs(owner, outError,
2294 com.android.internal.R.styleable.AndroidManifestProvider_name,
2295 com.android.internal.R.styleable.AndroidManifestProvider_label,
2296 com.android.internal.R.styleable.AndroidManifestProvider_icon,
Adam Powell81cd2e92010-04-21 16:35:18 -07002297 com.android.internal.R.styleable.AndroidManifestProvider_logo,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002298 mSeparateProcesses,
2299 com.android.internal.R.styleable.AndroidManifestProvider_process,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08002300 com.android.internal.R.styleable.AndroidManifestProvider_description,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002301 com.android.internal.R.styleable.AndroidManifestProvider_enabled);
2302 mParseProviderArgs.tag = "<provider>";
2303 }
2304
2305 mParseProviderArgs.sa = sa;
2306 mParseProviderArgs.flags = flags;
2307
2308 Provider p = new Provider(mParseProviderArgs, new ProviderInfo());
2309 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 sa.recycle();
2311 return null;
2312 }
2313
2314 p.info.exported = sa.getBoolean(
2315 com.android.internal.R.styleable.AndroidManifestProvider_exported, true);
2316
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002317 String cpname = sa.getNonConfigurationString(
2318 com.android.internal.R.styleable.AndroidManifestProvider_authorities, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319
2320 p.info.isSyncable = sa.getBoolean(
2321 com.android.internal.R.styleable.AndroidManifestProvider_syncable,
2322 false);
2323
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002324 String permission = sa.getNonConfigurationString(
2325 com.android.internal.R.styleable.AndroidManifestProvider_permission, 0);
2326 String str = sa.getNonConfigurationString(
2327 com.android.internal.R.styleable.AndroidManifestProvider_readPermission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002328 if (str == null) {
2329 str = permission;
2330 }
2331 if (str == null) {
2332 p.info.readPermission = owner.applicationInfo.permission;
2333 } else {
2334 p.info.readPermission =
2335 str.length() > 0 ? str.toString().intern() : null;
2336 }
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002337 str = sa.getNonConfigurationString(
2338 com.android.internal.R.styleable.AndroidManifestProvider_writePermission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 if (str == null) {
2340 str = permission;
2341 }
2342 if (str == null) {
2343 p.info.writePermission = owner.applicationInfo.permission;
2344 } else {
2345 p.info.writePermission =
2346 str.length() > 0 ? str.toString().intern() : null;
2347 }
2348
2349 p.info.grantUriPermissions = sa.getBoolean(
2350 com.android.internal.R.styleable.AndroidManifestProvider_grantUriPermissions,
2351 false);
2352
2353 p.info.multiprocess = sa.getBoolean(
2354 com.android.internal.R.styleable.AndroidManifestProvider_multiprocess,
2355 false);
2356
2357 p.info.initOrder = sa.getInt(
2358 com.android.internal.R.styleable.AndroidManifestProvider_initOrder,
2359 0);
2360
2361 sa.recycle();
2362
Dianne Hackborn54e570f2010-10-04 18:32:32 -07002363 if ((owner.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07002364 // A heavy-weight application can not have providers in its main process
2365 // We can do direct compare because we intern all strings.
2366 if (p.info.processName == owner.packageName) {
2367 outError[0] = "Heavy-weight applications can not have providers in main process";
2368 return null;
2369 }
2370 }
2371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 if (cpname == null) {
2373 outError[0] = "<provider> does not incude authorities attribute";
2374 return null;
2375 }
2376 p.info.authority = cpname.intern();
2377
2378 if (!parseProviderTags(res, parser, attrs, p, outError)) {
2379 return null;
2380 }
2381
2382 return p;
2383 }
2384
2385 private boolean parseProviderTags(Resources res,
2386 XmlPullParser parser, AttributeSet attrs,
2387 Provider outInfo, String[] outError)
2388 throws XmlPullParserException, IOException {
2389 int outerDepth = parser.getDepth();
2390 int type;
2391 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2392 && (type != XmlPullParser.END_TAG
2393 || parser.getDepth() > outerDepth)) {
2394 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2395 continue;
2396 }
2397
2398 if (parser.getName().equals("meta-data")) {
2399 if ((outInfo.metaData=parseMetaData(res, parser, attrs,
2400 outInfo.metaData, outError)) == null) {
2401 return false;
2402 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 } else if (parser.getName().equals("grant-uri-permission")) {
2405 TypedArray sa = res.obtainAttributes(attrs,
2406 com.android.internal.R.styleable.AndroidManifestGrantUriPermission);
2407
2408 PatternMatcher pa = null;
2409
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002410 String str = sa.getNonConfigurationString(
2411 com.android.internal.R.styleable.AndroidManifestGrantUriPermission_path, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 if (str != null) {
2413 pa = new PatternMatcher(str, PatternMatcher.PATTERN_LITERAL);
2414 }
2415
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002416 str = sa.getNonConfigurationString(
2417 com.android.internal.R.styleable.AndroidManifestGrantUriPermission_pathPrefix, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 if (str != null) {
2419 pa = new PatternMatcher(str, PatternMatcher.PATTERN_PREFIX);
2420 }
2421
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002422 str = sa.getNonConfigurationString(
2423 com.android.internal.R.styleable.AndroidManifestGrantUriPermission_pathPattern, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 if (str != null) {
2425 pa = new PatternMatcher(str, PatternMatcher.PATTERN_SIMPLE_GLOB);
2426 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 sa.recycle();
2429
2430 if (pa != null) {
2431 if (outInfo.info.uriPermissionPatterns == null) {
2432 outInfo.info.uriPermissionPatterns = new PatternMatcher[1];
2433 outInfo.info.uriPermissionPatterns[0] = pa;
2434 } else {
2435 final int N = outInfo.info.uriPermissionPatterns.length;
2436 PatternMatcher[] newp = new PatternMatcher[N+1];
2437 System.arraycopy(outInfo.info.uriPermissionPatterns, 0, newp, 0, N);
2438 newp[N] = pa;
2439 outInfo.info.uriPermissionPatterns = newp;
2440 }
2441 outInfo.info.grantUriPermissions = true;
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002442 } else {
2443 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002444 Slog.w(TAG, "Unknown element under <path-permission>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002445 + parser.getName() + " at " + mArchiveSourcePath + " "
2446 + parser.getPositionDescription());
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002447 XmlUtils.skipCurrentTag(parser);
2448 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07002449 } else {
2450 outError[0] = "No path, pathPrefix, or pathPattern for <path-permission>";
2451 return false;
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002452 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002453 }
2454 XmlUtils.skipCurrentTag(parser);
2455
2456 } else if (parser.getName().equals("path-permission")) {
2457 TypedArray sa = res.obtainAttributes(attrs,
2458 com.android.internal.R.styleable.AndroidManifestPathPermission);
2459
2460 PathPermission pa = null;
2461
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002462 String permission = sa.getNonConfigurationString(
2463 com.android.internal.R.styleable.AndroidManifestPathPermission_permission, 0);
2464 String readPermission = sa.getNonConfigurationString(
2465 com.android.internal.R.styleable.AndroidManifestPathPermission_readPermission, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002466 if (readPermission == null) {
2467 readPermission = permission;
2468 }
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002469 String writePermission = sa.getNonConfigurationString(
2470 com.android.internal.R.styleable.AndroidManifestPathPermission_writePermission, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002471 if (writePermission == null) {
2472 writePermission = permission;
2473 }
2474
2475 boolean havePerm = false;
2476 if (readPermission != null) {
2477 readPermission = readPermission.intern();
2478 havePerm = true;
2479 }
2480 if (writePermission != null) {
Bjorn Bringerte04b1ad2010-02-09 13:56:08 +00002481 writePermission = writePermission.intern();
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002482 havePerm = true;
2483 }
2484
2485 if (!havePerm) {
2486 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002487 Slog.w(TAG, "No readPermission or writePermssion for <path-permission>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002488 + parser.getName() + " at " + mArchiveSourcePath + " "
2489 + parser.getPositionDescription());
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002490 XmlUtils.skipCurrentTag(parser);
2491 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07002492 } else {
2493 outError[0] = "No readPermission or writePermssion for <path-permission>";
2494 return false;
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002495 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002496 }
2497
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002498 String path = sa.getNonConfigurationString(
2499 com.android.internal.R.styleable.AndroidManifestPathPermission_path, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002500 if (path != null) {
2501 pa = new PathPermission(path,
2502 PatternMatcher.PATTERN_LITERAL, readPermission, writePermission);
2503 }
2504
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002505 path = sa.getNonConfigurationString(
2506 com.android.internal.R.styleable.AndroidManifestPathPermission_pathPrefix, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002507 if (path != null) {
2508 pa = new PathPermission(path,
2509 PatternMatcher.PATTERN_PREFIX, readPermission, writePermission);
2510 }
2511
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002512 path = sa.getNonConfigurationString(
2513 com.android.internal.R.styleable.AndroidManifestPathPermission_pathPattern, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002514 if (path != null) {
2515 pa = new PathPermission(path,
2516 PatternMatcher.PATTERN_SIMPLE_GLOB, readPermission, writePermission);
2517 }
2518
2519 sa.recycle();
2520
2521 if (pa != null) {
2522 if (outInfo.info.pathPermissions == null) {
2523 outInfo.info.pathPermissions = new PathPermission[1];
2524 outInfo.info.pathPermissions[0] = pa;
2525 } else {
2526 final int N = outInfo.info.pathPermissions.length;
2527 PathPermission[] newp = new PathPermission[N+1];
2528 System.arraycopy(outInfo.info.pathPermissions, 0, newp, 0, N);
2529 newp[N] = pa;
2530 outInfo.info.pathPermissions = newp;
2531 }
2532 } else {
2533 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002534 Slog.w(TAG, "No path, pathPrefix, or pathPattern for <path-permission>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002535 + parser.getName() + " at " + mArchiveSourcePath + " "
2536 + parser.getPositionDescription());
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002537 XmlUtils.skipCurrentTag(parser);
2538 continue;
2539 }
2540 outError[0] = "No path, pathPrefix, or pathPattern for <path-permission>";
2541 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542 }
2543 XmlUtils.skipCurrentTag(parser);
2544
2545 } else {
2546 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002547 Slog.w(TAG, "Unknown element under <provider>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002548 + parser.getName() + " at " + mArchiveSourcePath + " "
2549 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 XmlUtils.skipCurrentTag(parser);
2551 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07002552 } else {
2553 outError[0] = "Bad element under <provider>: " + parser.getName();
2554 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 }
2557 }
2558 return true;
2559 }
2560
2561 private Service parseService(Package owner, Resources res,
2562 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
2563 throws XmlPullParserException, IOException {
2564 TypedArray sa = res.obtainAttributes(attrs,
2565 com.android.internal.R.styleable.AndroidManifestService);
2566
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002567 if (mParseServiceArgs == null) {
2568 mParseServiceArgs = new ParseComponentArgs(owner, outError,
2569 com.android.internal.R.styleable.AndroidManifestService_name,
2570 com.android.internal.R.styleable.AndroidManifestService_label,
2571 com.android.internal.R.styleable.AndroidManifestService_icon,
Adam Powell81cd2e92010-04-21 16:35:18 -07002572 com.android.internal.R.styleable.AndroidManifestService_logo,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002573 mSeparateProcesses,
2574 com.android.internal.R.styleable.AndroidManifestService_process,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08002575 com.android.internal.R.styleable.AndroidManifestService_description,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002576 com.android.internal.R.styleable.AndroidManifestService_enabled);
2577 mParseServiceArgs.tag = "<service>";
2578 }
2579
2580 mParseServiceArgs.sa = sa;
2581 mParseServiceArgs.flags = flags;
2582
2583 Service s = new Service(mParseServiceArgs, new ServiceInfo());
2584 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585 sa.recycle();
2586 return null;
2587 }
2588
2589 final boolean setExported = sa.hasValue(
2590 com.android.internal.R.styleable.AndroidManifestService_exported);
2591 if (setExported) {
2592 s.info.exported = sa.getBoolean(
2593 com.android.internal.R.styleable.AndroidManifestService_exported, false);
2594 }
2595
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002596 String str = sa.getNonConfigurationString(
2597 com.android.internal.R.styleable.AndroidManifestService_permission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 if (str == null) {
2599 s.info.permission = owner.applicationInfo.permission;
2600 } else {
2601 s.info.permission = str.length() > 0 ? str.toString().intern() : null;
2602 }
2603
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002604 s.info.flags = 0;
2605 if (sa.getBoolean(
2606 com.android.internal.R.styleable.AndroidManifestService_stopWithTask,
2607 false)) {
2608 s.info.flags |= ServiceInfo.FLAG_STOP_WITH_TASK;
2609 }
Dianne Hackborna0c283e2012-02-09 10:47:01 -08002610 if (sa.getBoolean(
2611 com.android.internal.R.styleable.AndroidManifestService_isolatedProcess,
2612 false)) {
2613 s.info.flags |= ServiceInfo.FLAG_ISOLATED_PROCESS;
2614 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 sa.recycle();
2617
Dianne Hackborn54e570f2010-10-04 18:32:32 -07002618 if ((owner.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07002619 // A heavy-weight application can not have services in its main process
2620 // We can do direct compare because we intern all strings.
2621 if (s.info.processName == owner.packageName) {
2622 outError[0] = "Heavy-weight applications can not have services in main process";
2623 return null;
2624 }
2625 }
2626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 int outerDepth = parser.getDepth();
2628 int type;
2629 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2630 && (type != XmlPullParser.END_TAG
2631 || parser.getDepth() > outerDepth)) {
2632 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2633 continue;
2634 }
2635
2636 if (parser.getName().equals("intent-filter")) {
2637 ServiceIntentInfo intent = new ServiceIntentInfo(s);
2638 if (!parseIntent(res, parser, attrs, flags, intent, outError, false)) {
2639 return null;
2640 }
2641
2642 s.intents.add(intent);
2643 } else if (parser.getName().equals("meta-data")) {
2644 if ((s.metaData=parseMetaData(res, parser, attrs, s.metaData,
2645 outError)) == null) {
2646 return null;
2647 }
2648 } else {
2649 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002650 Slog.w(TAG, "Unknown element under <service>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002651 + parser.getName() + " at " + mArchiveSourcePath + " "
2652 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 XmlUtils.skipCurrentTag(parser);
2654 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07002655 } else {
2656 outError[0] = "Bad element under <service>: " + parser.getName();
2657 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 }
2660 }
2661
2662 if (!setExported) {
2663 s.info.exported = s.intents.size() > 0;
2664 }
2665
2666 return s;
2667 }
2668
2669 private boolean parseAllMetaData(Resources res,
2670 XmlPullParser parser, AttributeSet attrs, String tag,
2671 Component outInfo, String[] outError)
2672 throws XmlPullParserException, IOException {
2673 int outerDepth = parser.getDepth();
2674 int type;
2675 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2676 && (type != XmlPullParser.END_TAG
2677 || parser.getDepth() > outerDepth)) {
2678 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2679 continue;
2680 }
2681
2682 if (parser.getName().equals("meta-data")) {
2683 if ((outInfo.metaData=parseMetaData(res, parser, attrs,
2684 outInfo.metaData, outError)) == null) {
2685 return false;
2686 }
2687 } else {
2688 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002689 Slog.w(TAG, "Unknown element under " + tag + ": "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002690 + parser.getName() + " at " + mArchiveSourcePath + " "
2691 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 XmlUtils.skipCurrentTag(parser);
2693 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07002694 } else {
2695 outError[0] = "Bad element under " + tag + ": " + parser.getName();
2696 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 }
2699 }
2700 return true;
2701 }
2702
2703 private Bundle parseMetaData(Resources res,
2704 XmlPullParser parser, AttributeSet attrs,
2705 Bundle data, String[] outError)
2706 throws XmlPullParserException, IOException {
2707
2708 TypedArray sa = res.obtainAttributes(attrs,
2709 com.android.internal.R.styleable.AndroidManifestMetaData);
2710
2711 if (data == null) {
2712 data = new Bundle();
2713 }
2714
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002715 String name = sa.getNonConfigurationString(
2716 com.android.internal.R.styleable.AndroidManifestMetaData_name, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 if (name == null) {
2718 outError[0] = "<meta-data> requires an android:name attribute";
2719 sa.recycle();
2720 return null;
2721 }
2722
Dianne Hackborn854060a2009-07-09 18:14:31 -07002723 name = name.intern();
2724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 TypedValue v = sa.peekValue(
2726 com.android.internal.R.styleable.AndroidManifestMetaData_resource);
2727 if (v != null && v.resourceId != 0) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002728 //Slog.i(TAG, "Meta data ref " + name + ": " + v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 data.putInt(name, v.resourceId);
2730 } else {
2731 v = sa.peekValue(
2732 com.android.internal.R.styleable.AndroidManifestMetaData_value);
Kenny Rootd2d29252011-08-08 11:27:57 -07002733 //Slog.i(TAG, "Meta data " + name + ": " + v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 if (v != null) {
2735 if (v.type == TypedValue.TYPE_STRING) {
2736 CharSequence cs = v.coerceToString();
Dianne Hackborn854060a2009-07-09 18:14:31 -07002737 data.putString(name, cs != null ? cs.toString().intern() : null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 } else if (v.type == TypedValue.TYPE_INT_BOOLEAN) {
2739 data.putBoolean(name, v.data != 0);
2740 } else if (v.type >= TypedValue.TYPE_FIRST_INT
2741 && v.type <= TypedValue.TYPE_LAST_INT) {
2742 data.putInt(name, v.data);
2743 } else if (v.type == TypedValue.TYPE_FLOAT) {
2744 data.putFloat(name, v.getFloat());
2745 } else {
2746 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002747 Slog.w(TAG, "<meta-data> only supports string, integer, float, color, boolean, and resource reference types: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002748 + parser.getName() + " at " + mArchiveSourcePath + " "
2749 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 } else {
2751 outError[0] = "<meta-data> only supports string, integer, float, color, boolean, and resource reference types";
2752 data = null;
2753 }
2754 }
2755 } else {
2756 outError[0] = "<meta-data> requires an android:value or android:resource attribute";
2757 data = null;
2758 }
2759 }
2760
2761 sa.recycle();
2762
2763 XmlUtils.skipCurrentTag(parser);
2764
2765 return data;
2766 }
2767
Kenny Root05ca4c92011-09-15 10:36:25 -07002768 private static VerifierInfo parseVerifier(Resources res, XmlPullParser parser,
2769 AttributeSet attrs, int flags, String[] outError) throws XmlPullParserException,
2770 IOException {
2771 final TypedArray sa = res.obtainAttributes(attrs,
2772 com.android.internal.R.styleable.AndroidManifestPackageVerifier);
2773
2774 final String packageName = sa.getNonResourceString(
2775 com.android.internal.R.styleable.AndroidManifestPackageVerifier_name);
2776
2777 final String encodedPublicKey = sa.getNonResourceString(
2778 com.android.internal.R.styleable.AndroidManifestPackageVerifier_publicKey);
2779
2780 sa.recycle();
2781
2782 if (packageName == null || packageName.length() == 0) {
2783 Slog.i(TAG, "verifier package name was null; skipping");
2784 return null;
2785 } else if (encodedPublicKey == null) {
2786 Slog.i(TAG, "verifier " + packageName + " public key was null; skipping");
2787 }
2788
2789 EncodedKeySpec keySpec;
2790 try {
2791 final byte[] encoded = Base64.decode(encodedPublicKey, Base64.DEFAULT);
2792 keySpec = new X509EncodedKeySpec(encoded);
2793 } catch (IllegalArgumentException e) {
2794 Slog.i(TAG, "Could not parse verifier " + packageName + " public key; invalid Base64");
2795 return null;
2796 }
2797
2798 /* First try the key as an RSA key. */
2799 try {
2800 final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
2801 final PublicKey publicKey = keyFactory.generatePublic(keySpec);
2802 return new VerifierInfo(packageName, publicKey);
2803 } catch (NoSuchAlgorithmException e) {
2804 Log.wtf(TAG, "Could not parse public key because RSA isn't included in build");
2805 return null;
2806 } catch (InvalidKeySpecException e) {
2807 // Not a RSA public key.
2808 }
2809
2810 /* Now try it as a DSA key. */
2811 try {
2812 final KeyFactory keyFactory = KeyFactory.getInstance("DSA");
2813 final PublicKey publicKey = keyFactory.generatePublic(keySpec);
2814 return new VerifierInfo(packageName, publicKey);
2815 } catch (NoSuchAlgorithmException e) {
2816 Log.wtf(TAG, "Could not parse public key because DSA isn't included in build");
2817 return null;
2818 } catch (InvalidKeySpecException e) {
2819 // Not a DSA public key.
2820 }
2821
2822 return null;
2823 }
2824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 private static final String ANDROID_RESOURCES
2826 = "http://schemas.android.com/apk/res/android";
2827
2828 private boolean parseIntent(Resources res,
2829 XmlPullParser parser, AttributeSet attrs, int flags,
2830 IntentInfo outInfo, String[] outError, boolean isActivity)
2831 throws XmlPullParserException, IOException {
2832
2833 TypedArray sa = res.obtainAttributes(attrs,
2834 com.android.internal.R.styleable.AndroidManifestIntentFilter);
2835
2836 int priority = sa.getInt(
2837 com.android.internal.R.styleable.AndroidManifestIntentFilter_priority, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 outInfo.setPriority(priority);
Kenny Root502e9a42011-01-10 13:48:15 -08002839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 TypedValue v = sa.peekValue(
2841 com.android.internal.R.styleable.AndroidManifestIntentFilter_label);
2842 if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
2843 outInfo.nonLocalizedLabel = v.coerceToString();
2844 }
2845
2846 outInfo.icon = sa.getResourceId(
2847 com.android.internal.R.styleable.AndroidManifestIntentFilter_icon, 0);
Adam Powell81cd2e92010-04-21 16:35:18 -07002848
2849 outInfo.logo = sa.getResourceId(
2850 com.android.internal.R.styleable.AndroidManifestIntentFilter_logo, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851
2852 sa.recycle();
2853
2854 int outerDepth = parser.getDepth();
2855 int type;
Kenny Rootd2d29252011-08-08 11:27:57 -07002856 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
2857 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
2858 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 continue;
2860 }
2861
2862 String nodeName = parser.getName();
2863 if (nodeName.equals("action")) {
2864 String value = attrs.getAttributeValue(
2865 ANDROID_RESOURCES, "name");
2866 if (value == null || value == "") {
2867 outError[0] = "No value supplied for <android:name>";
2868 return false;
2869 }
2870 XmlUtils.skipCurrentTag(parser);
2871
2872 outInfo.addAction(value);
2873 } else if (nodeName.equals("category")) {
2874 String value = attrs.getAttributeValue(
2875 ANDROID_RESOURCES, "name");
2876 if (value == null || value == "") {
2877 outError[0] = "No value supplied for <android:name>";
2878 return false;
2879 }
2880 XmlUtils.skipCurrentTag(parser);
2881
2882 outInfo.addCategory(value);
2883
2884 } else if (nodeName.equals("data")) {
2885 sa = res.obtainAttributes(attrs,
2886 com.android.internal.R.styleable.AndroidManifestData);
2887
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002888 String str = sa.getNonConfigurationString(
2889 com.android.internal.R.styleable.AndroidManifestData_mimeType, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 if (str != null) {
2891 try {
2892 outInfo.addDataType(str);
2893 } catch (IntentFilter.MalformedMimeTypeException e) {
2894 outError[0] = e.toString();
2895 sa.recycle();
2896 return false;
2897 }
2898 }
2899
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002900 str = sa.getNonConfigurationString(
2901 com.android.internal.R.styleable.AndroidManifestData_scheme, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 if (str != null) {
2903 outInfo.addDataScheme(str);
2904 }
2905
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002906 String host = sa.getNonConfigurationString(
2907 com.android.internal.R.styleable.AndroidManifestData_host, 0);
2908 String port = sa.getNonConfigurationString(
2909 com.android.internal.R.styleable.AndroidManifestData_port, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 if (host != null) {
2911 outInfo.addDataAuthority(host, port);
2912 }
2913
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002914 str = sa.getNonConfigurationString(
2915 com.android.internal.R.styleable.AndroidManifestData_path, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 if (str != null) {
2917 outInfo.addDataPath(str, PatternMatcher.PATTERN_LITERAL);
2918 }
2919
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002920 str = sa.getNonConfigurationString(
2921 com.android.internal.R.styleable.AndroidManifestData_pathPrefix, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 if (str != null) {
2923 outInfo.addDataPath(str, PatternMatcher.PATTERN_PREFIX);
2924 }
2925
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002926 str = sa.getNonConfigurationString(
2927 com.android.internal.R.styleable.AndroidManifestData_pathPattern, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928 if (str != null) {
2929 outInfo.addDataPath(str, PatternMatcher.PATTERN_SIMPLE_GLOB);
2930 }
2931
2932 sa.recycle();
2933 XmlUtils.skipCurrentTag(parser);
2934 } else if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002935 Slog.w(TAG, "Unknown element under <intent-filter>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002936 + parser.getName() + " at " + mArchiveSourcePath + " "
2937 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 XmlUtils.skipCurrentTag(parser);
2939 } else {
2940 outError[0] = "Bad element under <intent-filter>: " + parser.getName();
2941 return false;
2942 }
2943 }
2944
2945 outInfo.hasDefault = outInfo.hasCategory(Intent.CATEGORY_DEFAULT);
Kenny Rootd2d29252011-08-08 11:27:57 -07002946
2947 if (DEBUG_PARSER) {
2948 final StringBuilder cats = new StringBuilder("Intent d=");
2949 cats.append(outInfo.hasDefault);
2950 cats.append(", cat=");
2951
2952 final Iterator<String> it = outInfo.categoriesIterator();
2953 if (it != null) {
2954 while (it.hasNext()) {
2955 cats.append(' ');
2956 cats.append(it.next());
2957 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 }
Kenny Rootd2d29252011-08-08 11:27:57 -07002959 Slog.d(TAG, cats.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960 }
2961
2962 return true;
2963 }
2964
2965 public final static class Package {
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08002966 public String packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967
2968 // For now we only support one application per package.
2969 public final ApplicationInfo applicationInfo = new ApplicationInfo();
2970
2971 public final ArrayList<Permission> permissions = new ArrayList<Permission>(0);
2972 public final ArrayList<PermissionGroup> permissionGroups = new ArrayList<PermissionGroup>(0);
2973 public final ArrayList<Activity> activities = new ArrayList<Activity>(0);
2974 public final ArrayList<Activity> receivers = new ArrayList<Activity>(0);
2975 public final ArrayList<Provider> providers = new ArrayList<Provider>(0);
2976 public final ArrayList<Service> services = new ArrayList<Service>(0);
2977 public final ArrayList<Instrumentation> instrumentation = new ArrayList<Instrumentation>(0);
2978
2979 public final ArrayList<String> requestedPermissions = new ArrayList<String>();
Dianne Hackborne639da72012-02-21 15:11:13 -08002980 public final ArrayList<Boolean> requestedPermissionsRequired = new ArrayList<Boolean>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981
Dianne Hackborn854060a2009-07-09 18:14:31 -07002982 public ArrayList<String> protectedBroadcasts;
2983
Dianne Hackborn49237342009-08-27 20:08:01 -07002984 public ArrayList<String> usesLibraries = null;
2985 public ArrayList<String> usesOptionalLibraries = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 public String[] usesLibraryFiles = null;
2987
Dianne Hackbornc1552392010-03-03 16:19:01 -08002988 public ArrayList<String> mOriginalPackages = null;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08002989 public String mRealPackage = null;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08002990 public ArrayList<String> mAdoptPermissions = null;
2991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002992 // We store the application meta-data independently to avoid multiple unwanted references
2993 public Bundle mAppMetaData = null;
2994
2995 // If this is a 3rd party app, this is the path of the zip file.
2996 public String mPath;
2997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002998 // The version code declared for this package.
2999 public int mVersionCode;
3000
3001 // The version name declared for this package.
3002 public String mVersionName;
3003
3004 // The shared user id that this package wants to use.
3005 public String mSharedUserId;
3006
3007 // The shared user label that this package wants to use.
3008 public int mSharedUserLabel;
3009
3010 // Signatures that were read from the package.
3011 public Signature mSignatures[];
3012
3013 // For use by package manager service for quick lookup of
3014 // preferred up order.
3015 public int mPreferredOrder = 0;
3016
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07003017 // For use by the package manager to keep track of the path to the
3018 // file an app came from.
3019 public String mScanPath;
3020
3021 // For use by package manager to keep track of where it has done dexopt.
3022 public boolean mDidDexOpt;
3023
Dianne Hackborn46730fc2010-07-24 16:32:42 -07003024 // User set enabled state.
3025 public int mSetEnabled = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
3026
Dianne Hackborne7f97212011-02-24 14:40:20 -08003027 // Whether the package has been stopped.
3028 public boolean mSetStopped = false;
3029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003030 // Additional data supplied by callers.
3031 public Object mExtras;
Kenny Rootdeb11262010-08-02 11:36:21 -07003032
3033 // Whether an operation is currently pending on this package
3034 public boolean mOperationPending;
3035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003036 /*
3037 * Applications hardware preferences
3038 */
3039 public final ArrayList<ConfigurationInfo> configPreferences =
3040 new ArrayList<ConfigurationInfo>();
3041
Dianne Hackborn49237342009-08-27 20:08:01 -07003042 /*
3043 * Applications requested features
3044 */
3045 public ArrayList<FeatureInfo> reqFeatures = null;
3046
Suchi Amalapurapu117818e2010-02-09 03:45:40 -08003047 public int installLocation;
3048
Kenny Rootbcc954d2011-08-08 16:19:08 -07003049 /**
3050 * Digest suitable for comparing whether this package's manifest is the
3051 * same as another.
3052 */
3053 public ManifestDigest manifestDigest;
3054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003055 public Package(String _name) {
3056 packageName = _name;
3057 applicationInfo.packageName = _name;
3058 applicationInfo.uid = -1;
3059 }
3060
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003061 public void setPackageName(String newName) {
3062 packageName = newName;
3063 applicationInfo.packageName = newName;
3064 for (int i=permissions.size()-1; i>=0; i--) {
3065 permissions.get(i).setPackageName(newName);
3066 }
3067 for (int i=permissionGroups.size()-1; i>=0; i--) {
3068 permissionGroups.get(i).setPackageName(newName);
3069 }
3070 for (int i=activities.size()-1; i>=0; i--) {
3071 activities.get(i).setPackageName(newName);
3072 }
3073 for (int i=receivers.size()-1; i>=0; i--) {
3074 receivers.get(i).setPackageName(newName);
3075 }
3076 for (int i=providers.size()-1; i>=0; i--) {
3077 providers.get(i).setPackageName(newName);
3078 }
3079 for (int i=services.size()-1; i>=0; i--) {
3080 services.get(i).setPackageName(newName);
3081 }
3082 for (int i=instrumentation.size()-1; i>=0; i--) {
3083 instrumentation.get(i).setPackageName(newName);
3084 }
3085 }
Dianne Hackborn65696252012-03-05 18:49:21 -08003086
3087 public boolean hasComponentClassName(String name) {
3088 for (int i=activities.size()-1; i>=0; i--) {
3089 if (name.equals(activities.get(i).className)) {
3090 return true;
3091 }
3092 }
3093 for (int i=receivers.size()-1; i>=0; i--) {
3094 if (name.equals(receivers.get(i).className)) {
3095 return true;
3096 }
3097 }
3098 for (int i=providers.size()-1; i>=0; i--) {
3099 if (name.equals(providers.get(i).className)) {
3100 return true;
3101 }
3102 }
3103 for (int i=services.size()-1; i>=0; i--) {
3104 if (name.equals(services.get(i).className)) {
3105 return true;
3106 }
3107 }
3108 for (int i=instrumentation.size()-1; i>=0; i--) {
3109 if (name.equals(instrumentation.get(i).className)) {
3110 return true;
3111 }
3112 }
3113 return false;
3114 }
3115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003116 public String toString() {
3117 return "Package{"
3118 + Integer.toHexString(System.identityHashCode(this))
3119 + " " + packageName + "}";
3120 }
3121 }
3122
3123 public static class Component<II extends IntentInfo> {
3124 public final Package owner;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003125 public final ArrayList<II> intents;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003126 public final String className;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003127 public Bundle metaData;
3128
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003129 ComponentName componentName;
3130 String componentShortName;
3131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 public Component(Package _owner) {
3133 owner = _owner;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003134 intents = null;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003135 className = null;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003136 }
3137
3138 public Component(final ParsePackageItemArgs args, final PackageItemInfo outInfo) {
3139 owner = args.owner;
3140 intents = new ArrayList<II>(0);
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003141 String name = args.sa.getNonConfigurationString(args.nameRes, 0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003142 if (name == null) {
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003143 className = null;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003144 args.outError[0] = args.tag + " does not specify android:name";
3145 return;
3146 }
3147
3148 outInfo.name
3149 = buildClassName(owner.applicationInfo.packageName, name, args.outError);
3150 if (outInfo.name == null) {
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003151 className = null;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003152 args.outError[0] = args.tag + " does not have valid android:name";
3153 return;
3154 }
3155
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003156 className = outInfo.name;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003157
3158 int iconVal = args.sa.getResourceId(args.iconRes, 0);
3159 if (iconVal != 0) {
3160 outInfo.icon = iconVal;
3161 outInfo.nonLocalizedLabel = null;
3162 }
Adam Powell81cd2e92010-04-21 16:35:18 -07003163
3164 int logoVal = args.sa.getResourceId(args.logoRes, 0);
3165 if (logoVal != 0) {
3166 outInfo.logo = logoVal;
3167 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003168
3169 TypedValue v = args.sa.peekValue(args.labelRes);
3170 if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
3171 outInfo.nonLocalizedLabel = v.coerceToString();
3172 }
3173
3174 outInfo.packageName = owner.packageName;
3175 }
3176
3177 public Component(final ParseComponentArgs args, final ComponentInfo outInfo) {
3178 this(args, (PackageItemInfo)outInfo);
3179 if (args.outError[0] != null) {
3180 return;
3181 }
3182
3183 if (args.processRes != 0) {
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07003184 CharSequence pname;
3185 if (owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.FROYO) {
3186 pname = args.sa.getNonConfigurationString(args.processRes, 0);
3187 } else {
3188 // Some older apps have been seen to use a resource reference
3189 // here that on older builds was ignored (with a warning). We
3190 // need to continue to do this for them so they don't break.
3191 pname = args.sa.getNonResourceString(args.processRes);
3192 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003193 outInfo.processName = buildProcessName(owner.applicationInfo.packageName,
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07003194 owner.applicationInfo.processName, pname,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003195 args.flags, args.sepProcesses, args.outError);
3196 }
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08003197
3198 if (args.descriptionRes != 0) {
3199 outInfo.descriptionRes = args.sa.getResourceId(args.descriptionRes, 0);
3200 }
3201
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003202 outInfo.enabled = args.sa.getBoolean(args.enabledRes, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003203 }
3204
3205 public Component(Component<II> clone) {
3206 owner = clone.owner;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003207 intents = clone.intents;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003208 className = clone.className;
3209 componentName = clone.componentName;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003210 componentShortName = clone.componentShortName;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003211 }
3212
3213 public ComponentName getComponentName() {
3214 if (componentName != null) {
3215 return componentName;
3216 }
3217 if (className != null) {
3218 componentName = new ComponentName(owner.applicationInfo.packageName,
3219 className);
3220 }
3221 return componentName;
3222 }
3223
3224 public String getComponentShortName() {
3225 if (componentShortName != null) {
3226 return componentShortName;
3227 }
3228 ComponentName component = getComponentName();
3229 if (component != null) {
3230 componentShortName = component.flattenToShortString();
3231 }
3232 return componentShortName;
3233 }
3234
3235 public void setPackageName(String packageName) {
3236 componentName = null;
3237 componentShortName = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 }
3239 }
3240
3241 public final static class Permission extends Component<IntentInfo> {
3242 public final PermissionInfo info;
3243 public boolean tree;
3244 public PermissionGroup group;
3245
3246 public Permission(Package _owner) {
3247 super(_owner);
3248 info = new PermissionInfo();
3249 }
3250
3251 public Permission(Package _owner, PermissionInfo _info) {
3252 super(_owner);
3253 info = _info;
3254 }
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003255
3256 public void setPackageName(String packageName) {
3257 super.setPackageName(packageName);
3258 info.packageName = packageName;
3259 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260
3261 public String toString() {
3262 return "Permission{"
3263 + Integer.toHexString(System.identityHashCode(this))
3264 + " " + info.name + "}";
3265 }
3266 }
3267
3268 public final static class PermissionGroup extends Component<IntentInfo> {
3269 public final PermissionGroupInfo info;
3270
3271 public PermissionGroup(Package _owner) {
3272 super(_owner);
3273 info = new PermissionGroupInfo();
3274 }
3275
3276 public PermissionGroup(Package _owner, PermissionGroupInfo _info) {
3277 super(_owner);
3278 info = _info;
3279 }
3280
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003281 public void setPackageName(String packageName) {
3282 super.setPackageName(packageName);
3283 info.packageName = packageName;
3284 }
3285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003286 public String toString() {
3287 return "PermissionGroup{"
3288 + Integer.toHexString(System.identityHashCode(this))
3289 + " " + info.name + "}";
3290 }
3291 }
3292
3293 private static boolean copyNeeded(int flags, Package p, Bundle metaData) {
Dianne Hackborn46730fc2010-07-24 16:32:42 -07003294 if (p.mSetEnabled != PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
3295 boolean enabled = p.mSetEnabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
3296 if (p.applicationInfo.enabled != enabled) {
3297 return true;
3298 }
3299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300 if ((flags & PackageManager.GET_META_DATA) != 0
3301 && (metaData != null || p.mAppMetaData != null)) {
3302 return true;
3303 }
3304 if ((flags & PackageManager.GET_SHARED_LIBRARY_FILES) != 0
3305 && p.usesLibraryFiles != null) {
3306 return true;
3307 }
3308 return false;
3309 }
3310
3311 public static ApplicationInfo generateApplicationInfo(Package p, int flags) {
Amith Yamasani742a6712011-05-04 14:49:28 -07003312 return generateApplicationInfo(p, flags, UserId.getUserId(Binder.getCallingUid()));
3313 }
3314
3315 public static ApplicationInfo generateApplicationInfo(Package p, int flags, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 if (p == null) return null;
Amith Yamasani742a6712011-05-04 14:49:28 -07003317 if (!copyNeeded(flags, p, null) && userId == 0) {
Mitsuru Oshima69fff4a2009-07-21 09:51:05 -07003318 // CompatibilityMode is global state. It's safe to modify the instance
3319 // of the package.
3320 if (!sCompatibilityModeEnabled) {
3321 p.applicationInfo.disableCompatibilityMode();
3322 }
Dianne Hackborne7f97212011-02-24 14:40:20 -08003323 if (p.mSetStopped) {
3324 p.applicationInfo.flags |= ApplicationInfo.FLAG_STOPPED;
3325 } else {
3326 p.applicationInfo.flags &= ~ApplicationInfo.FLAG_STOPPED;
3327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003328 return p.applicationInfo;
3329 }
3330
3331 // Make shallow copy so we can store the metadata/libraries safely
3332 ApplicationInfo ai = new ApplicationInfo(p.applicationInfo);
Amith Yamasani742a6712011-05-04 14:49:28 -07003333 if (userId != 0) {
3334 ai.uid = UserId.getUid(userId, ai.uid);
3335 ai.dataDir = PackageManager.getDataDirForUser(userId, ai.packageName);
3336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003337 if ((flags & PackageManager.GET_META_DATA) != 0) {
3338 ai.metaData = p.mAppMetaData;
3339 }
3340 if ((flags & PackageManager.GET_SHARED_LIBRARY_FILES) != 0) {
3341 ai.sharedLibraryFiles = p.usesLibraryFiles;
3342 }
Mitsuru Oshima69fff4a2009-07-21 09:51:05 -07003343 if (!sCompatibilityModeEnabled) {
3344 ai.disableCompatibilityMode();
3345 }
Dianne Hackborne7f97212011-02-24 14:40:20 -08003346 if (p.mSetStopped) {
3347 p.applicationInfo.flags |= ApplicationInfo.FLAG_STOPPED;
3348 } else {
3349 p.applicationInfo.flags &= ~ApplicationInfo.FLAG_STOPPED;
3350 }
John Reck4b7b7cc2011-02-02 11:57:44 -08003351 if (p.mSetEnabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
3352 ai.enabled = true;
Dianne Hackborn0ac30312011-06-17 14:49:23 -07003353 } else if (p.mSetEnabled == PackageManager.COMPONENT_ENABLED_STATE_DISABLED
3354 || p.mSetEnabled == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER) {
John Reck4b7b7cc2011-02-02 11:57:44 -08003355 ai.enabled = false;
3356 }
Dianne Hackborn0ac30312011-06-17 14:49:23 -07003357 ai.enabledSetting = p.mSetEnabled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003358 return ai;
3359 }
3360
3361 public static final PermissionInfo generatePermissionInfo(
3362 Permission p, int flags) {
3363 if (p == null) return null;
3364 if ((flags&PackageManager.GET_META_DATA) == 0) {
3365 return p.info;
3366 }
3367 PermissionInfo pi = new PermissionInfo(p.info);
3368 pi.metaData = p.metaData;
3369 return pi;
3370 }
3371
3372 public static final PermissionGroupInfo generatePermissionGroupInfo(
3373 PermissionGroup pg, int flags) {
3374 if (pg == null) return null;
3375 if ((flags&PackageManager.GET_META_DATA) == 0) {
3376 return pg.info;
3377 }
3378 PermissionGroupInfo pgi = new PermissionGroupInfo(pg.info);
3379 pgi.metaData = pg.metaData;
3380 return pgi;
3381 }
3382
3383 public final static class Activity extends Component<ActivityIntentInfo> {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003384 public final ActivityInfo info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003385
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003386 public Activity(final ParseComponentArgs args, final ActivityInfo _info) {
3387 super(args, _info);
3388 info = _info;
3389 info.applicationInfo = args.owner.applicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003391
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003392 public void setPackageName(String packageName) {
3393 super.setPackageName(packageName);
3394 info.packageName = packageName;
3395 }
3396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397 public String toString() {
3398 return "Activity{"
3399 + Integer.toHexString(System.identityHashCode(this))
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003400 + " " + getComponentShortName() + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 }
3402 }
3403
Amith Yamasani742a6712011-05-04 14:49:28 -07003404 public static final ActivityInfo generateActivityInfo(Activity a, int flags, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003405 if (a == null) return null;
Amith Yamasani742a6712011-05-04 14:49:28 -07003406 if (!copyNeeded(flags, a.owner, a.metaData) && userId == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003407 return a.info;
3408 }
3409 // Make shallow copies so we can store the metadata safely
3410 ActivityInfo ai = new ActivityInfo(a.info);
3411 ai.metaData = a.metaData;
Amith Yamasani742a6712011-05-04 14:49:28 -07003412 ai.applicationInfo = generateApplicationInfo(a.owner, flags, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413 return ai;
3414 }
3415
3416 public final static class Service extends Component<ServiceIntentInfo> {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003417 public final ServiceInfo info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003418
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003419 public Service(final ParseComponentArgs args, final ServiceInfo _info) {
3420 super(args, _info);
3421 info = _info;
3422 info.applicationInfo = args.owner.applicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003423 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003424
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003425 public void setPackageName(String packageName) {
3426 super.setPackageName(packageName);
3427 info.packageName = packageName;
3428 }
3429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 public String toString() {
3431 return "Service{"
3432 + Integer.toHexString(System.identityHashCode(this))
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003433 + " " + getComponentShortName() + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 }
3435 }
3436
Amith Yamasani742a6712011-05-04 14:49:28 -07003437 public static final ServiceInfo generateServiceInfo(Service s, int flags, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 if (s == null) return null;
Amith Yamasani37ce3a82012-02-06 12:04:42 -08003439 if (!copyNeeded(flags, s.owner, s.metaData)
3440 && userId == UserId.getUserId(s.info.applicationInfo.uid)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003441 return s.info;
3442 }
3443 // Make shallow copies so we can store the metadata safely
3444 ServiceInfo si = new ServiceInfo(s.info);
3445 si.metaData = s.metaData;
Amith Yamasani742a6712011-05-04 14:49:28 -07003446 si.applicationInfo = generateApplicationInfo(s.owner, flags, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003447 return si;
3448 }
3449
3450 public final static class Provider extends Component {
3451 public final ProviderInfo info;
3452 public boolean syncable;
3453
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003454 public Provider(final ParseComponentArgs args, final ProviderInfo _info) {
3455 super(args, _info);
3456 info = _info;
3457 info.applicationInfo = args.owner.applicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003458 syncable = false;
3459 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 public Provider(Provider existingProvider) {
3462 super(existingProvider);
3463 this.info = existingProvider.info;
3464 this.syncable = existingProvider.syncable;
3465 }
3466
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003467 public void setPackageName(String packageName) {
3468 super.setPackageName(packageName);
3469 info.packageName = packageName;
3470 }
3471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003472 public String toString() {
3473 return "Provider{"
3474 + Integer.toHexString(System.identityHashCode(this))
3475 + " " + info.name + "}";
3476 }
3477 }
3478
Amith Yamasani742a6712011-05-04 14:49:28 -07003479 public static final ProviderInfo generateProviderInfo(Provider p, int flags, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003480 if (p == null) return null;
3481 if (!copyNeeded(flags, p.owner, p.metaData)
3482 && ((flags & PackageManager.GET_URI_PERMISSION_PATTERNS) != 0
Amith Yamasani742a6712011-05-04 14:49:28 -07003483 || p.info.uriPermissionPatterns == null)
3484 && userId == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003485 return p.info;
3486 }
3487 // Make shallow copies so we can store the metadata safely
3488 ProviderInfo pi = new ProviderInfo(p.info);
3489 pi.metaData = p.metaData;
3490 if ((flags & PackageManager.GET_URI_PERMISSION_PATTERNS) == 0) {
3491 pi.uriPermissionPatterns = null;
3492 }
Amith Yamasani742a6712011-05-04 14:49:28 -07003493 pi.applicationInfo = generateApplicationInfo(p.owner, flags, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 return pi;
3495 }
3496
3497 public final static class Instrumentation extends Component {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003498 public final InstrumentationInfo info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003500 public Instrumentation(final ParsePackageItemArgs args, final InstrumentationInfo _info) {
3501 super(args, _info);
3502 info = _info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003504
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003505 public void setPackageName(String packageName) {
3506 super.setPackageName(packageName);
3507 info.packageName = packageName;
3508 }
3509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003510 public String toString() {
3511 return "Instrumentation{"
3512 + Integer.toHexString(System.identityHashCode(this))
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003513 + " " + getComponentShortName() + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003514 }
3515 }
3516
3517 public static final InstrumentationInfo generateInstrumentationInfo(
3518 Instrumentation i, int flags) {
3519 if (i == null) return null;
3520 if ((flags&PackageManager.GET_META_DATA) == 0) {
3521 return i.info;
3522 }
3523 InstrumentationInfo ii = new InstrumentationInfo(i.info);
3524 ii.metaData = i.metaData;
3525 return ii;
3526 }
3527
3528 public static class IntentInfo extends IntentFilter {
3529 public boolean hasDefault;
3530 public int labelRes;
3531 public CharSequence nonLocalizedLabel;
3532 public int icon;
Adam Powell81cd2e92010-04-21 16:35:18 -07003533 public int logo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534 }
3535
3536 public final static class ActivityIntentInfo extends IntentInfo {
3537 public final Activity activity;
3538
3539 public ActivityIntentInfo(Activity _activity) {
3540 activity = _activity;
3541 }
3542
3543 public String toString() {
3544 return "ActivityIntentInfo{"
3545 + Integer.toHexString(System.identityHashCode(this))
3546 + " " + activity.info.name + "}";
3547 }
3548 }
3549
3550 public final static class ServiceIntentInfo extends IntentInfo {
3551 public final Service service;
3552
3553 public ServiceIntentInfo(Service _service) {
3554 service = _service;
3555 }
3556
3557 public String toString() {
3558 return "ServiceIntentInfo{"
3559 + Integer.toHexString(System.identityHashCode(this))
3560 + " " + service.info.name + "}";
3561 }
3562 }
Mitsuru Oshima69fff4a2009-07-21 09:51:05 -07003563
3564 /**
3565 * @hide
3566 */
3567 public static void setCompatibilityModeEnabled(boolean compatibilityModeEnabled) {
3568 sCompatibilityModeEnabled = compatibilityModeEnabled;
3569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570}