blob: 49cea3af4c3d8cd4a60c7b026e0e68bd5c567434 [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;
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -070027import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.os.Bundle;
29import android.os.PatternMatcher;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070030import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.util.AttributeSet;
Kenny Root05ca4c92011-09-15 10:36:25 -070032import android.util.Base64;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.util.DisplayMetrics;
Kenny Root05ca4c92011-09-15 10:36:25 -070034import android.util.Log;
Kenny Rootd2d29252011-08-08 11:27:57 -070035import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
Kenny Rootd63f7db2010-09-27 08:07:48 -070038import java.io.BufferedInputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import java.io.File;
40import java.io.IOException;
41import java.io.InputStream;
42import java.lang.ref.WeakReference;
Kenny Root05ca4c92011-09-15 10:36:25 -070043import java.security.KeyFactory;
44import java.security.NoSuchAlgorithmException;
45import java.security.PublicKey;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import java.security.cert.Certificate;
47import java.security.cert.CertificateEncodingException;
Kenny Root05ca4c92011-09-15 10:36:25 -070048import java.security.spec.EncodedKeySpec;
49import java.security.spec.InvalidKeySpecException;
50import java.security.spec.X509EncodedKeySpec;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import java.util.ArrayList;
52import java.util.Enumeration;
Dianne Hackborne639da72012-02-21 15:11:13 -080053import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import java.util.Iterator;
Kenny Root05ca4c92011-09-15 10:36:25 -070055import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import java.util.jar.JarEntry;
57import java.util.jar.JarFile;
Kenny Root6c918ce2013-04-02 14:04:24 -070058import java.util.zip.ZipEntry;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
Amith Yamasani742a6712011-05-04 14:49:28 -070060import com.android.internal.util.XmlUtils;
61
62import org.xmlpull.v1.XmlPullParser;
63import org.xmlpull.v1.XmlPullParserException;
64
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065/**
66 * Package archive parsing
67 *
68 * {@hide}
69 */
70public class PackageParser {
Kenny Rootd2d29252011-08-08 11:27:57 -070071 private static final boolean DEBUG_JAR = false;
72 private static final boolean DEBUG_PARSER = false;
73 private static final boolean DEBUG_BACKUP = false;
74
Kenny Rootbcc954d2011-08-08 16:19:08 -070075 /** File name in an APK for the Android manifest. */
76 private static final String ANDROID_MANIFEST_FILENAME = "AndroidManifest.xml";
77
Dianne Hackborna96cbb42009-05-13 15:06:13 -070078 /** @hide */
79 public static class NewPermissionInfo {
80 public final String name;
81 public final int sdkVersion;
82 public final int fileVersion;
83
84 public NewPermissionInfo(String name, int sdkVersion, int fileVersion) {
85 this.name = name;
86 this.sdkVersion = sdkVersion;
87 this.fileVersion = fileVersion;
88 }
89 }
Dianne Hackborn79245122012-03-12 10:51:26 -070090
91 /** @hide */
92 public static class SplitPermissionInfo {
93 public final String rootPerm;
94 public final String[] newPerms;
Dianne Hackborn31b0e0e2012-04-05 19:33:30 -070095 public final int targetSdk;
Dianne Hackborn79245122012-03-12 10:51:26 -070096
Dianne Hackborn31b0e0e2012-04-05 19:33:30 -070097 public SplitPermissionInfo(String rootPerm, String[] newPerms, int targetSdk) {
Dianne Hackborn79245122012-03-12 10:51:26 -070098 this.rootPerm = rootPerm;
99 this.newPerms = newPerms;
Dianne Hackborn31b0e0e2012-04-05 19:33:30 -0700100 this.targetSdk = targetSdk;
Dianne Hackborn79245122012-03-12 10:51:26 -0700101 }
102 }
103
Dianne Hackborna96cbb42009-05-13 15:06:13 -0700104 /**
105 * List of new permissions that have been added since 1.0.
106 * NOTE: These must be declared in SDK version order, with permissions
107 * added to older SDKs appearing before those added to newer SDKs.
Dianne Hackborn79245122012-03-12 10:51:26 -0700108 * If sdkVersion is 0, then this is not a permission that we want to
109 * automatically add to older apps, but we do want to allow it to be
110 * granted during a platform update.
Dianne Hackborna96cbb42009-05-13 15:06:13 -0700111 * @hide
112 */
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700113 public static final PackageParser.NewPermissionInfo NEW_PERMISSIONS[] =
114 new PackageParser.NewPermissionInfo[] {
San Mehat5a3a77d2009-06-01 09:25:28 -0700115 new PackageParser.NewPermissionInfo(android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700116 android.os.Build.VERSION_CODES.DONUT, 0),
117 new PackageParser.NewPermissionInfo(android.Manifest.permission.READ_PHONE_STATE,
118 android.os.Build.VERSION_CODES.DONUT, 0)
Dianne Hackborna96cbb42009-05-13 15:06:13 -0700119 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120
Dianne Hackborn79245122012-03-12 10:51:26 -0700121 /**
122 * List of permissions that have been split into more granular or dependent
123 * permissions.
124 * @hide
125 */
126 public static final PackageParser.SplitPermissionInfo SPLIT_PERMISSIONS[] =
127 new PackageParser.SplitPermissionInfo[] {
Dianne Hackborn2bd8d042012-06-11 12:27:05 -0700128 // READ_EXTERNAL_STORAGE is always required when an app requests
129 // WRITE_EXTERNAL_STORAGE, because we can't have an app that has
130 // write access without read access. The hack here with the target
131 // target SDK version ensures that this grant is always done.
Dianne Hackborn79245122012-03-12 10:51:26 -0700132 new PackageParser.SplitPermissionInfo(android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
Dianne Hackborn31b0e0e2012-04-05 19:33:30 -0700133 new String[] { android.Manifest.permission.READ_EXTERNAL_STORAGE },
Dianne Hackborn2bd8d042012-06-11 12:27:05 -0700134 android.os.Build.VERSION_CODES.CUR_DEVELOPMENT+1),
Dianne Hackborn31b0e0e2012-04-05 19:33:30 -0700135 new PackageParser.SplitPermissionInfo(android.Manifest.permission.READ_CONTACTS,
136 new String[] { android.Manifest.permission.READ_CALL_LOG },
137 android.os.Build.VERSION_CODES.JELLY_BEAN),
138 new PackageParser.SplitPermissionInfo(android.Manifest.permission.WRITE_CONTACTS,
139 new String[] { android.Manifest.permission.WRITE_CALL_LOG },
140 android.os.Build.VERSION_CODES.JELLY_BEAN)
Dianne Hackborn79245122012-03-12 10:51:26 -0700141 };
142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 private String mArchiveSourcePath;
144 private String[] mSeparateProcesses;
Dianne Hackbornd2509fd2011-09-12 12:29:43 -0700145 private boolean mOnlyCoreApps;
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -0700146 private static final int SDK_VERSION = Build.VERSION.SDK_INT;
147 private static final String SDK_CODENAME = "REL".equals(Build.VERSION.CODENAME)
148 ? null : Build.VERSION.CODENAME;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149
150 private int mParseError = PackageManager.INSTALL_SUCCEEDED;
151
152 private static final Object mSync = new Object();
153 private static WeakReference<byte[]> mReadBuffer;
154
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -0700155 private static boolean sCompatibilityModeEnabled = true;
156 private static final int PARSE_DEFAULT_INSTALL_LOCATION = PackageInfo.INSTALL_LOCATION_UNSPECIFIED;
Mitsuru Oshima69fff4a2009-07-21 09:51:05 -0700157
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700158 static class ParsePackageItemArgs {
159 final Package owner;
160 final String[] outError;
161 final int nameRes;
162 final int labelRes;
163 final int iconRes;
Adam Powell81cd2e92010-04-21 16:35:18 -0700164 final int logoRes;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700165
166 String tag;
167 TypedArray sa;
168
169 ParsePackageItemArgs(Package _owner, String[] _outError,
Adam Powell81cd2e92010-04-21 16:35:18 -0700170 int _nameRes, int _labelRes, int _iconRes, int _logoRes) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700171 owner = _owner;
172 outError = _outError;
173 nameRes = _nameRes;
174 labelRes = _labelRes;
175 iconRes = _iconRes;
Adam Powell81cd2e92010-04-21 16:35:18 -0700176 logoRes = _logoRes;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700177 }
178 }
179
180 static class ParseComponentArgs extends ParsePackageItemArgs {
181 final String[] sepProcesses;
182 final int processRes;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800183 final int descriptionRes;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700184 final int enabledRes;
185 int flags;
186
187 ParseComponentArgs(Package _owner, String[] _outError,
Adam Powell81cd2e92010-04-21 16:35:18 -0700188 int _nameRes, int _labelRes, int _iconRes, int _logoRes,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800189 String[] _sepProcesses, int _processRes,
190 int _descriptionRes, int _enabledRes) {
Adam Powell81cd2e92010-04-21 16:35:18 -0700191 super(_owner, _outError, _nameRes, _labelRes, _iconRes, _logoRes);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700192 sepProcesses = _sepProcesses;
193 processRes = _processRes;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800194 descriptionRes = _descriptionRes;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700195 enabledRes = _enabledRes;
196 }
197 }
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800198
199 /* Light weight package info.
200 * @hide
201 */
202 public static class PackageLite {
Kenny Root05ca4c92011-09-15 10:36:25 -0700203 public final String packageName;
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700204 public final int versionCode;
Kenny Root05ca4c92011-09-15 10:36:25 -0700205 public final int installLocation;
206 public final VerifierInfo[] verifiers;
207
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700208 public PackageLite(String packageName, int versionCode,
209 int installLocation, List<VerifierInfo> verifiers) {
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800210 this.packageName = packageName;
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700211 this.versionCode = versionCode;
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800212 this.installLocation = installLocation;
Kenny Root05ca4c92011-09-15 10:36:25 -0700213 this.verifiers = verifiers.toArray(new VerifierInfo[verifiers.size()]);
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800214 }
215 }
216
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700217 private ParsePackageItemArgs mParseInstrumentationArgs;
218 private ParseComponentArgs mParseActivityArgs;
219 private ParseComponentArgs mParseActivityAliasArgs;
220 private ParseComponentArgs mParseServiceArgs;
221 private ParseComponentArgs mParseProviderArgs;
222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 /** If set to true, we will only allow package files that exactly match
224 * the DTD. Otherwise, we try to get as much from the package as we
225 * can without failing. This should normally be set to false, to
226 * support extensions to the DTD in future versions. */
227 private static final boolean RIGID_PARSER = false;
228
229 private static final String TAG = "PackageParser";
230
231 public PackageParser(String archiveSourcePath) {
232 mArchiveSourcePath = archiveSourcePath;
233 }
234
235 public void setSeparateProcesses(String[] procs) {
236 mSeparateProcesses = procs;
237 }
238
Dianne Hackbornd2509fd2011-09-12 12:29:43 -0700239 public void setOnlyCoreApps(boolean onlyCoreApps) {
240 mOnlyCoreApps = onlyCoreApps;
241 }
242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 private static final boolean isPackageFilename(String name) {
244 return name.endsWith(".apk");
245 }
246
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700247 /*
Amith Yamasani13593602012-03-22 16:16:17 -0700248 public static PackageInfo generatePackageInfo(PackageParser.Package p,
249 int gids[], int flags, long firstInstallTime, long lastUpdateTime,
250 HashSet<String> grantedPermissions) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700251 PackageUserState state = new PackageUserState();
Amith Yamasani13593602012-03-22 16:16:17 -0700252 return generatePackageInfo(p, gids, flags, firstInstallTime, lastUpdateTime,
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700253 grantedPermissions, state, UserHandle.getCallingUserId());
Amith Yamasani13593602012-03-22 16:16:17 -0700254 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700255 */
Amith Yamasani13593602012-03-22 16:16:17 -0700256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 /**
258 * Generate and return the {@link PackageInfo} for a parsed package.
259 *
260 * @param p the parsed package.
261 * @param flags indicating which optional information is included.
262 */
263 public static PackageInfo generatePackageInfo(PackageParser.Package p,
Dianne Hackborne639da72012-02-21 15:11:13 -0800264 int gids[], int flags, long firstInstallTime, long lastUpdateTime,
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700265 HashSet<String> grantedPermissions, PackageUserState state) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266
Amith Yamasani483f3b02012-03-13 16:08:00 -0700267 return generatePackageInfo(p, gids, flags, firstInstallTime, lastUpdateTime,
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700268 grantedPermissions, state, UserHandle.getCallingUserId());
269 }
270
271 private static boolean checkUseInstalled(int flags, PackageUserState state) {
272 return state.installed || ((flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0);
Amith Yamasani483f3b02012-03-13 16:08:00 -0700273 }
274
Amith Yamasani13593602012-03-22 16:16:17 -0700275 public static PackageInfo generatePackageInfo(PackageParser.Package p,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700276 int gids[], int flags, long firstInstallTime, long lastUpdateTime,
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700277 HashSet<String> grantedPermissions, PackageUserState state, int userId) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700278
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700279 if (!checkUseInstalled(flags, state)) {
280 return null;
281 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 PackageInfo pi = new PackageInfo();
283 pi.packageName = p.packageName;
284 pi.versionCode = p.mVersionCode;
285 pi.versionName = p.mVersionName;
286 pi.sharedUserId = p.mSharedUserId;
287 pi.sharedUserLabel = p.mSharedUserLabel;
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700288 pi.applicationInfo = generateApplicationInfo(p, flags, state, userId);
Suchi Amalapurapu117818e2010-02-09 03:45:40 -0800289 pi.installLocation = p.installLocation;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800290 pi.requiredForAllUsers = p.mRequiredForAllUsers;
Amith Yamasani0ac1fc92013-03-27 18:56:08 -0700291 pi.restrictedAccountType = p.mRestrictedAccountType;
Dianne Hackborn78d68832010-10-07 01:12:46 -0700292 pi.firstInstallTime = firstInstallTime;
293 pi.lastUpdateTime = lastUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 if ((flags&PackageManager.GET_GIDS) != 0) {
295 pi.gids = gids;
296 }
297 if ((flags&PackageManager.GET_CONFIGURATIONS) != 0) {
298 int N = p.configPreferences.size();
299 if (N > 0) {
300 pi.configPreferences = new ConfigurationInfo[N];
Dianne Hackborn49237342009-08-27 20:08:01 -0700301 p.configPreferences.toArray(pi.configPreferences);
302 }
303 N = p.reqFeatures != null ? p.reqFeatures.size() : 0;
304 if (N > 0) {
305 pi.reqFeatures = new FeatureInfo[N];
306 p.reqFeatures.toArray(pi.reqFeatures);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 }
308 }
309 if ((flags&PackageManager.GET_ACTIVITIES) != 0) {
310 int N = p.activities.size();
311 if (N > 0) {
Dianne Hackborn7eca6872009-09-28 23:57:05 -0700312 if ((flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
313 pi.activities = new ActivityInfo[N];
314 } else {
315 int num = 0;
316 for (int i=0; i<N; i++) {
317 if (p.activities.get(i).info.enabled) num++;
318 }
319 pi.activities = new ActivityInfo[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 Activity activity = p.activities.get(i);
323 if (activity.info.enabled
324 || (flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700325 pi.activities[j++] = generateActivityInfo(p.activities.get(i), flags,
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700326 state, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 }
328 }
329 }
330 }
331 if ((flags&PackageManager.GET_RECEIVERS) != 0) {
332 int N = p.receivers.size();
333 if (N > 0) {
Dianne Hackborn7eca6872009-09-28 23:57:05 -0700334 if ((flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
335 pi.receivers = new ActivityInfo[N];
336 } else {
337 int num = 0;
338 for (int i=0; i<N; i++) {
339 if (p.receivers.get(i).info.enabled) num++;
340 }
341 pi.receivers = new ActivityInfo[num];
342 }
Suchi Amalapurapud83006c2009-10-28 23:39:46 -0700343 for (int i=0, j=0; i<N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 final Activity activity = p.receivers.get(i);
345 if (activity.info.enabled
346 || (flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
Amith Yamasani13593602012-03-22 16:16:17 -0700347 pi.receivers[j++] = generateActivityInfo(p.receivers.get(i), flags,
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700348 state, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 }
350 }
351 }
352 }
353 if ((flags&PackageManager.GET_SERVICES) != 0) {
354 int N = p.services.size();
355 if (N > 0) {
Dianne Hackborn7eca6872009-09-28 23:57:05 -0700356 if ((flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
357 pi.services = new ServiceInfo[N];
358 } else {
359 int num = 0;
360 for (int i=0; i<N; i++) {
361 if (p.services.get(i).info.enabled) num++;
362 }
363 pi.services = new ServiceInfo[num];
364 }
Suchi Amalapurapud83006c2009-10-28 23:39:46 -0700365 for (int i=0, j=0; i<N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 final Service service = p.services.get(i);
367 if (service.info.enabled
368 || (flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700369 pi.services[j++] = generateServiceInfo(p.services.get(i), flags,
370 state, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 }
372 }
373 }
374 }
375 if ((flags&PackageManager.GET_PROVIDERS) != 0) {
376 int N = p.providers.size();
377 if (N > 0) {
Dianne Hackborn7eca6872009-09-28 23:57:05 -0700378 if ((flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
379 pi.providers = new ProviderInfo[N];
380 } else {
381 int num = 0;
382 for (int i=0; i<N; i++) {
383 if (p.providers.get(i).info.enabled) num++;
384 }
385 pi.providers = new ProviderInfo[num];
386 }
Suchi Amalapurapud83006c2009-10-28 23:39:46 -0700387 for (int i=0, j=0; i<N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 final Provider provider = p.providers.get(i);
389 if (provider.info.enabled
390 || (flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700391 pi.providers[j++] = generateProviderInfo(p.providers.get(i), flags,
392 state, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 }
394 }
395 }
396 }
397 if ((flags&PackageManager.GET_INSTRUMENTATION) != 0) {
398 int N = p.instrumentation.size();
399 if (N > 0) {
400 pi.instrumentation = new InstrumentationInfo[N];
401 for (int i=0; i<N; i++) {
402 pi.instrumentation[i] = generateInstrumentationInfo(
403 p.instrumentation.get(i), flags);
404 }
405 }
406 }
407 if ((flags&PackageManager.GET_PERMISSIONS) != 0) {
408 int N = p.permissions.size();
409 if (N > 0) {
410 pi.permissions = new PermissionInfo[N];
411 for (int i=0; i<N; i++) {
412 pi.permissions[i] = generatePermissionInfo(p.permissions.get(i), flags);
413 }
414 }
415 N = p.requestedPermissions.size();
416 if (N > 0) {
417 pi.requestedPermissions = new String[N];
Dianne Hackborne639da72012-02-21 15:11:13 -0800418 pi.requestedPermissionsFlags = new int[N];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 for (int i=0; i<N; i++) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800420 final String perm = p.requestedPermissions.get(i);
421 pi.requestedPermissions[i] = perm;
422 if (p.requestedPermissionsRequired.get(i)) {
423 pi.requestedPermissionsFlags[i] |= PackageInfo.REQUESTED_PERMISSION_REQUIRED;
424 }
425 if (grantedPermissions != null && grantedPermissions.contains(perm)) {
426 pi.requestedPermissionsFlags[i] |= PackageInfo.REQUESTED_PERMISSION_GRANTED;
427 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 }
429 }
430 }
431 if ((flags&PackageManager.GET_SIGNATURES) != 0) {
Suchi Amalapurapud83006c2009-10-28 23:39:46 -0700432 int N = (p.mSignatures != null) ? p.mSignatures.length : 0;
433 if (N > 0) {
434 pi.signatures = new Signature[N];
435 System.arraycopy(p.mSignatures, 0, pi.signatures, 0, N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 }
437 }
438 return pi;
439 }
440
441 private Certificate[] loadCertificates(JarFile jarFile, JarEntry je,
442 byte[] readBuffer) {
443 try {
444 // We must read the stream for the JarEntry to retrieve
445 // its certificates.
Kenny Rootd63f7db2010-09-27 08:07:48 -0700446 InputStream is = new BufferedInputStream(jarFile.getInputStream(je));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 while (is.read(readBuffer, 0, readBuffer.length) != -1) {
448 // not using
449 }
450 is.close();
451 return je != null ? je.getCertificates() : null;
452 } catch (IOException e) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700453 Slog.w(TAG, "Exception reading " + je.getName() + " in "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 + jarFile.getName(), e);
Dianne Hackborn6e52b5d2010-04-05 14:33:01 -0700455 } catch (RuntimeException e) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700456 Slog.w(TAG, "Exception reading " + je.getName() + " in "
Dianne Hackborn6e52b5d2010-04-05 14:33:01 -0700457 + jarFile.getName(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 }
459 return null;
460 }
461
Suchi Amalapurapuaf8e9f42010-01-12 10:17:28 -0800462 public final static int PARSE_IS_SYSTEM = 1<<0;
463 public final static int PARSE_CHATTY = 1<<1;
464 public final static int PARSE_MUST_BE_APK = 1<<2;
465 public final static int PARSE_IGNORE_PROCESSES = 1<<3;
466 public final static int PARSE_FORWARD_LOCK = 1<<4;
467 public final static int PARSE_ON_SDCARD = 1<<5;
Dianne Hackborn806da1d2010-03-18 16:50:07 -0700468 public final static int PARSE_IS_SYSTEM_DIR = 1<<6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469
470 public int getParseError() {
471 return mParseError;
472 }
473
Suchi Amalapurapuaf8e9f42010-01-12 10:17:28 -0800474 public Package parsePackage(File sourceFile, String destCodePath,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 DisplayMetrics metrics, int flags) {
476 mParseError = PackageManager.INSTALL_SUCCEEDED;
477
478 mArchiveSourcePath = sourceFile.getPath();
479 if (!sourceFile.isFile()) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700480 Slog.w(TAG, "Skipping dir: " + mArchiveSourcePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 mParseError = PackageManager.INSTALL_PARSE_FAILED_NOT_APK;
482 return null;
483 }
484 if (!isPackageFilename(sourceFile.getName())
485 && (flags&PARSE_MUST_BE_APK) != 0) {
486 if ((flags&PARSE_IS_SYSTEM) == 0) {
487 // We expect to have non-.apk files in the system dir,
488 // so don't warn about them.
Kenny Rootd2d29252011-08-08 11:27:57 -0700489 Slog.w(TAG, "Skipping non-package file: " + mArchiveSourcePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 }
491 mParseError = PackageManager.INSTALL_PARSE_FAILED_NOT_APK;
492 return null;
493 }
494
Kenny Rootd2d29252011-08-08 11:27:57 -0700495 if (DEBUG_JAR)
496 Slog.d(TAG, "Scanning package: " + mArchiveSourcePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497
498 XmlResourceParser parser = null;
499 AssetManager assmgr = null;
Dianne Hackborn3b81bc12011-01-15 11:50:52 -0800500 Resources res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 boolean assetError = true;
502 try {
503 assmgr = new AssetManager();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700504 int cookie = assmgr.addAssetPath(mArchiveSourcePath);
Dianne Hackborn3b81bc12011-01-15 11:50:52 -0800505 if (cookie != 0) {
506 res = new Resources(assmgr, metrics, null);
Dianne Hackborn69cb8752011-05-19 18:13:32 -0700507 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 -0800508 Build.VERSION.RESOURCES_SDK_INT);
Kenny Rootbcc954d2011-08-08 16:19:08 -0700509 parser = assmgr.openXmlResourceParser(cookie, ANDROID_MANIFEST_FILENAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 assetError = false;
511 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -0700512 Slog.w(TAG, "Failed adding asset path:"+mArchiveSourcePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 }
514 } catch (Exception e) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700515 Slog.w(TAG, "Unable to read AndroidManifest.xml of "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 + mArchiveSourcePath, e);
517 }
Dianne Hackborn3b81bc12011-01-15 11:50:52 -0800518 if (assetError) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 if (assmgr != null) assmgr.close();
520 mParseError = PackageManager.INSTALL_PARSE_FAILED_BAD_MANIFEST;
521 return null;
522 }
523 String[] errorText = new String[1];
524 Package pkg = null;
525 Exception errorException = null;
526 try {
527 // XXXX todo: need to figure out correct configuration.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 pkg = parsePackage(res, parser, flags, errorText);
529 } catch (Exception e) {
530 errorException = e;
531 mParseError = PackageManager.INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION;
532 }
533
534
535 if (pkg == null) {
Dianne Hackbornd2509fd2011-09-12 12:29:43 -0700536 // If we are only parsing core apps, then a null with INSTALL_SUCCEEDED
537 // just means to skip this app so don't make a fuss about it.
538 if (!mOnlyCoreApps || mParseError != PackageManager.INSTALL_SUCCEEDED) {
539 if (errorException != null) {
540 Slog.w(TAG, mArchiveSourcePath, errorException);
541 } else {
542 Slog.w(TAG, mArchiveSourcePath + " (at "
543 + parser.getPositionDescription()
544 + "): " + errorText[0]);
545 }
546 if (mParseError == PackageManager.INSTALL_SUCCEEDED) {
547 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 }
550 parser.close();
551 assmgr.close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 return null;
553 }
554
555 parser.close();
556 assmgr.close();
557
Suchi Amalapurapuaf8e9f42010-01-12 10:17:28 -0800558 // Set code and resource paths
559 pkg.mPath = destCodePath;
560 pkg.mScanPath = mArchiveSourcePath;
561 //pkg.applicationInfo.sourceDir = destCodePath;
562 //pkg.applicationInfo.publicSourceDir = destRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 pkg.mSignatures = null;
564
565 return pkg;
566 }
567
Kenny Root6c918ce2013-04-02 14:04:24 -0700568 /**
569 * Gathers the {@link ManifestDigest} for {@code pkg} if it exists in the
570 * APK. If it successfully scanned the package and found the
571 * {@code AndroidManifest.xml}, {@code true} is returned.
572 */
573 public boolean collectManifestDigest(Package pkg) {
574 try {
575 final JarFile jarFile = new JarFile(mArchiveSourcePath);
576 try {
577 final ZipEntry je = jarFile.getEntry(ANDROID_MANIFEST_FILENAME);
578 if (je != null) {
579 pkg.manifestDigest = ManifestDigest.fromInputStream(jarFile.getInputStream(je));
580 }
581 } finally {
582 jarFile.close();
583 }
584 return true;
585 } catch (IOException e) {
586 return false;
587 }
588 }
589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 public boolean collectCertificates(Package pkg, int flags) {
591 pkg.mSignatures = null;
592
593 WeakReference<byte[]> readBufferRef;
594 byte[] readBuffer = null;
595 synchronized (mSync) {
596 readBufferRef = mReadBuffer;
597 if (readBufferRef != null) {
598 mReadBuffer = null;
599 readBuffer = readBufferRef.get();
600 }
601 if (readBuffer == null) {
602 readBuffer = new byte[8192];
603 readBufferRef = new WeakReference<byte[]>(readBuffer);
604 }
605 }
606
607 try {
608 JarFile jarFile = new JarFile(mArchiveSourcePath);
609
610 Certificate[] certs = null;
611
612 if ((flags&PARSE_IS_SYSTEM) != 0) {
613 // If this package comes from the system image, then we
614 // can trust it... we'll just use the AndroidManifest.xml
615 // to retrieve its signatures, not validating all of the
616 // files.
Kenny Rootbcc954d2011-08-08 16:19:08 -0700617 JarEntry jarEntry = jarFile.getJarEntry(ANDROID_MANIFEST_FILENAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 certs = loadCertificates(jarFile, jarEntry, readBuffer);
619 if (certs == null) {
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 at entry "
622 + jarEntry.getName() + "; ignoring!");
623 jarFile.close();
624 mParseError = PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
625 return false;
626 }
Kenny Rootd2d29252011-08-08 11:27:57 -0700627 if (DEBUG_JAR) {
628 Slog.i(TAG, "File " + mArchiveSourcePath + ": entry=" + jarEntry
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 + " certs=" + (certs != null ? certs.length : 0));
630 if (certs != null) {
631 final int N = certs.length;
632 for (int i=0; i<N; i++) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700633 Slog.i(TAG, " Public key: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 + certs[i].getPublicKey().getEncoded()
635 + " " + certs[i].getPublicKey());
636 }
637 }
638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -0700640 Enumeration<JarEntry> entries = jarFile.entries();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 while (entries.hasMoreElements()) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700642 final JarEntry je = entries.nextElement();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 if (je.isDirectory()) continue;
Kenny Rootd2d29252011-08-08 11:27:57 -0700644
Kenny Rootbcc954d2011-08-08 16:19:08 -0700645 final String name = je.getName();
646
647 if (name.startsWith("META-INF/"))
648 continue;
649
650 if (ANDROID_MANIFEST_FILENAME.equals(name)) {
Kenny Root6c918ce2013-04-02 14:04:24 -0700651 pkg.manifestDigest =
652 ManifestDigest.fromInputStream(jarFile.getInputStream(je));
Kenny Rootbcc954d2011-08-08 16:19:08 -0700653 }
654
655 final Certificate[] localCerts = loadCertificates(jarFile, je, readBuffer);
Kenny Rootd2d29252011-08-08 11:27:57 -0700656 if (DEBUG_JAR) {
657 Slog.i(TAG, "File " + mArchiveSourcePath + " entry " + je.getName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 + ": certs=" + certs + " ("
659 + (certs != null ? certs.length : 0) + ")");
660 }
Kenny Rootbcc954d2011-08-08 16:19:08 -0700661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 if (localCerts == null) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700663 Slog.e(TAG, "Package " + pkg.packageName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 + " has no certificates at entry "
665 + je.getName() + "; ignoring!");
666 jarFile.close();
667 mParseError = PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
668 return false;
669 } else if (certs == null) {
670 certs = localCerts;
671 } else {
672 // Ensure all certificates match.
673 for (int i=0; i<certs.length; i++) {
674 boolean found = false;
675 for (int j=0; j<localCerts.length; j++) {
676 if (certs[i] != null &&
677 certs[i].equals(localCerts[j])) {
678 found = true;
679 break;
680 }
681 }
682 if (!found || certs.length != localCerts.length) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700683 Slog.e(TAG, "Package " + pkg.packageName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 + " has mismatched certificates at entry "
685 + je.getName() + "; ignoring!");
686 jarFile.close();
687 mParseError = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;
688 return false;
689 }
690 }
691 }
692 }
693 }
694 jarFile.close();
695
696 synchronized (mSync) {
697 mReadBuffer = readBufferRef;
698 }
699
700 if (certs != null && certs.length > 0) {
701 final int N = certs.length;
702 pkg.mSignatures = new Signature[certs.length];
703 for (int i=0; i<N; i++) {
704 pkg.mSignatures[i] = new Signature(
705 certs[i].getEncoded());
706 }
707 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -0700708 Slog.e(TAG, "Package " + pkg.packageName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 + " has no certificates; ignoring!");
710 mParseError = PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
711 return false;
712 }
713 } catch (CertificateEncodingException e) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700714 Slog.w(TAG, "Exception reading " + mArchiveSourcePath, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 mParseError = PackageManager.INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING;
716 return false;
717 } catch (IOException e) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700718 Slog.w(TAG, "Exception reading " + mArchiveSourcePath, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 mParseError = PackageManager.INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING;
720 return false;
721 } catch (RuntimeException e) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700722 Slog.w(TAG, "Exception reading " + mArchiveSourcePath, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 mParseError = PackageManager.INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION;
724 return false;
725 }
726
727 return true;
728 }
729
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800730 /*
731 * Utility method that retrieves just the package name and install
732 * location from the apk location at the given file path.
733 * @param packageFilePath file location of the apk
734 * @param flags Special parse flags
Kenny Root930d3af2010-07-30 16:52:29 -0700735 * @return PackageLite object with package information or null on failure.
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800736 */
737 public static PackageLite parsePackageLite(String packageFilePath, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 AssetManager assmgr = null;
Kenny Root05ca4c92011-09-15 10:36:25 -0700739 final XmlResourceParser parser;
740 final Resources res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 try {
742 assmgr = new AssetManager();
Dianne Hackborn69cb8752011-05-19 18:13:32 -0700743 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 -0800744 Build.VERSION.RESOURCES_SDK_INT);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 int cookie = assmgr.addAssetPath(packageFilePath);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700747 if (cookie == 0) {
748 return null;
749 }
750
Kenny Root05ca4c92011-09-15 10:36:25 -0700751 final DisplayMetrics metrics = new DisplayMetrics();
752 metrics.setToDefaults();
753 res = new Resources(assmgr, metrics, null);
Kenny Rootbcc954d2011-08-08 16:19:08 -0700754 parser = assmgr.openXmlResourceParser(cookie, ANDROID_MANIFEST_FILENAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 } catch (Exception e) {
756 if (assmgr != null) assmgr.close();
Kenny Rootd2d29252011-08-08 11:27:57 -0700757 Slog.w(TAG, "Unable to read AndroidManifest.xml of "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 + packageFilePath, e);
759 return null;
760 }
Kenny Root05ca4c92011-09-15 10:36:25 -0700761
762 final AttributeSet attrs = parser;
763 final String errors[] = new String[1];
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800764 PackageLite packageLite = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 try {
Kenny Root05ca4c92011-09-15 10:36:25 -0700766 packageLite = parsePackageLite(res, parser, attrs, flags, errors);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 } catch (IOException e) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700768 Slog.w(TAG, packageFilePath, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 } catch (XmlPullParserException e) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700770 Slog.w(TAG, packageFilePath, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 } finally {
772 if (parser != null) parser.close();
773 if (assmgr != null) assmgr.close();
774 }
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800775 if (packageLite == null) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700776 Slog.e(TAG, "parsePackageLite error: " + errors[0]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 return null;
778 }
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800779 return packageLite;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 }
781
782 private static String validateName(String name, boolean requiresSeparator) {
783 final int N = name.length();
784 boolean hasSep = false;
785 boolean front = true;
786 for (int i=0; i<N; i++) {
787 final char c = name.charAt(i);
788 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
789 front = false;
790 continue;
791 }
792 if (!front) {
793 if ((c >= '0' && c <= '9') || c == '_') {
794 continue;
795 }
796 }
797 if (c == '.') {
798 hasSep = true;
799 front = true;
800 continue;
801 }
802 return "bad character '" + c + "'";
803 }
804 return hasSep || !requiresSeparator
805 ? null : "must have at least one '.' separator";
806 }
807
808 private static String parsePackageName(XmlPullParser parser,
809 AttributeSet attrs, int flags, String[] outError)
810 throws IOException, XmlPullParserException {
811
812 int type;
Kenny Rootd2d29252011-08-08 11:27:57 -0700813 while ((type = parser.next()) != XmlPullParser.START_TAG
814 && type != XmlPullParser.END_DOCUMENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 ;
816 }
817
Kenny Rootd2d29252011-08-08 11:27:57 -0700818 if (type != XmlPullParser.START_TAG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 outError[0] = "No start tag found";
820 return null;
821 }
Kenny Rootd2d29252011-08-08 11:27:57 -0700822 if (DEBUG_PARSER)
823 Slog.v(TAG, "Root element name: '" + parser.getName() + "'");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 if (!parser.getName().equals("manifest")) {
825 outError[0] = "No <manifest> tag";
826 return null;
827 }
828 String pkgName = attrs.getAttributeValue(null, "package");
829 if (pkgName == null || pkgName.length() == 0) {
830 outError[0] = "<manifest> does not specify package";
831 return null;
832 }
833 String nameError = validateName(pkgName, true);
834 if (nameError != null && !"android".equals(pkgName)) {
835 outError[0] = "<manifest> specifies bad package name \""
836 + pkgName + "\": " + nameError;
837 return null;
838 }
839
840 return pkgName.intern();
841 }
842
Kenny Root05ca4c92011-09-15 10:36:25 -0700843 private static PackageLite parsePackageLite(Resources res, XmlPullParser parser,
844 AttributeSet attrs, int flags, String[] outError) throws IOException,
845 XmlPullParserException {
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800846
847 int type;
Kenny Rootd2d29252011-08-08 11:27:57 -0700848 while ((type = parser.next()) != XmlPullParser.START_TAG
849 && type != XmlPullParser.END_DOCUMENT) {
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800850 ;
851 }
852
Kenny Rootd2d29252011-08-08 11:27:57 -0700853 if (type != XmlPullParser.START_TAG) {
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800854 outError[0] = "No start tag found";
855 return null;
856 }
Kenny Rootd2d29252011-08-08 11:27:57 -0700857 if (DEBUG_PARSER)
858 Slog.v(TAG, "Root element name: '" + parser.getName() + "'");
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800859 if (!parser.getName().equals("manifest")) {
860 outError[0] = "No <manifest> tag";
861 return null;
862 }
863 String pkgName = attrs.getAttributeValue(null, "package");
864 if (pkgName == null || pkgName.length() == 0) {
865 outError[0] = "<manifest> does not specify package";
866 return null;
867 }
868 String nameError = validateName(pkgName, true);
869 if (nameError != null && !"android".equals(pkgName)) {
870 outError[0] = "<manifest> specifies bad package name \""
871 + pkgName + "\": " + nameError;
872 return null;
873 }
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -0700874 int installLocation = PARSE_DEFAULT_INSTALL_LOCATION;
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700875 int versionCode = 0;
876 int numFound = 0;
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800877 for (int i = 0; i < attrs.getAttributeCount(); i++) {
878 String attr = attrs.getAttributeName(i);
879 if (attr.equals("installLocation")) {
880 installLocation = attrs.getAttributeIntValue(i,
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -0700881 PARSE_DEFAULT_INSTALL_LOCATION);
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700882 numFound++;
883 } else if (attr.equals("versionCode")) {
884 versionCode = attrs.getAttributeIntValue(i, 0);
885 numFound++;
886 }
887 if (numFound >= 2) {
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800888 break;
889 }
890 }
Kenny Root05ca4c92011-09-15 10:36:25 -0700891
892 // Only search the tree when the tag is directly below <manifest>
893 final int searchDepth = parser.getDepth() + 1;
894
895 final List<VerifierInfo> verifiers = new ArrayList<VerifierInfo>();
896 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
897 && (type != XmlPullParser.END_TAG || parser.getDepth() >= searchDepth)) {
898 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
899 continue;
900 }
901
902 if (parser.getDepth() == searchDepth && "package-verifier".equals(parser.getName())) {
903 final VerifierInfo verifier = parseVerifier(res, parser, attrs, flags, outError);
904 if (verifier != null) {
905 verifiers.add(verifier);
906 }
907 }
908 }
909
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700910 return new PackageLite(pkgName.intern(), versionCode, installLocation, verifiers);
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800911 }
912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 /**
914 * Temporary.
915 */
916 static public Signature stringToSignature(String str) {
917 final int N = str.length();
918 byte[] sig = new byte[N];
919 for (int i=0; i<N; i++) {
920 sig[i] = (byte)str.charAt(i);
921 }
922 return new Signature(sig);
923 }
924
925 private Package parsePackage(
926 Resources res, XmlResourceParser parser, int flags, String[] outError)
927 throws XmlPullParserException, IOException {
928 AttributeSet attrs = parser;
929
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700930 mParseInstrumentationArgs = null;
931 mParseActivityArgs = null;
932 mParseServiceArgs = null;
933 mParseProviderArgs = null;
934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 String pkgName = parsePackageName(parser, attrs, flags, outError);
936 if (pkgName == null) {
937 mParseError = PackageManager.INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME;
938 return null;
939 }
940 int type;
941
Dianne Hackbornd2509fd2011-09-12 12:29:43 -0700942 if (mOnlyCoreApps) {
943 boolean core = attrs.getAttributeBooleanValue(null, "coreApp", false);
944 if (!core) {
945 mParseError = PackageManager.INSTALL_SUCCEEDED;
946 return null;
947 }
948 }
949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 final Package pkg = new Package(pkgName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 boolean foundApp = false;
Dianne Hackborn851a5412009-05-08 12:06:44 -0700952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 TypedArray sa = res.obtainAttributes(attrs,
954 com.android.internal.R.styleable.AndroidManifest);
955 pkg.mVersionCode = sa.getInteger(
956 com.android.internal.R.styleable.AndroidManifest_versionCode, 0);
Dianne Hackborncf244ad2010-03-09 15:00:30 -0800957 pkg.mVersionName = sa.getNonConfigurationString(
958 com.android.internal.R.styleable.AndroidManifest_versionName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 if (pkg.mVersionName != null) {
960 pkg.mVersionName = pkg.mVersionName.intern();
961 }
Dianne Hackborncf244ad2010-03-09 15:00:30 -0800962 String str = sa.getNonConfigurationString(
963 com.android.internal.R.styleable.AndroidManifest_sharedUserId, 0);
964 if (str != null && str.length() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 String nameError = validateName(str, true);
966 if (nameError != null && !"android".equals(pkgName)) {
967 outError[0] = "<manifest> specifies bad sharedUserId name \""
968 + str + "\": " + nameError;
969 mParseError = PackageManager.INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID;
970 return null;
971 }
972 pkg.mSharedUserId = str.intern();
973 pkg.mSharedUserLabel = sa.getResourceId(
974 com.android.internal.R.styleable.AndroidManifest_sharedUserLabel, 0);
975 }
976 sa.recycle();
Suchi Amalapurapuaaec7792010-02-25 11:49:43 -0800977
Suchi Amalapurapu117818e2010-02-09 03:45:40 -0800978 pkg.installLocation = sa.getInteger(
979 com.android.internal.R.styleable.AndroidManifest_installLocation,
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -0700980 PARSE_DEFAULT_INSTALL_LOCATION);
Dianne Hackborn54e570f2010-10-04 18:32:32 -0700981 pkg.applicationInfo.installLocation = pkg.installLocation;
Kenny Root7cb9be22012-05-30 15:30:37 -0700982
983 /* Set the global "forward lock" flag */
984 if ((flags & PARSE_FORWARD_LOCK) != 0) {
985 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_FORWARD_LOCK;
986 }
987
988 /* Set the global "on SD card" flag */
989 if ((flags & PARSE_ON_SDCARD) != 0) {
990 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_EXTERNAL_STORAGE;
991 }
992
Dianne Hackborn723738c2009-06-25 19:48:04 -0700993 // Resource boolean are -1, so 1 means we don't know the value.
994 int supportsSmallScreens = 1;
995 int supportsNormalScreens = 1;
996 int supportsLargeScreens = 1;
Dianne Hackborn14cee9f2010-04-23 17:51:26 -0700997 int supportsXLargeScreens = 1;
Dianne Hackbornc4db95c2009-07-21 17:46:02 -0700998 int resizeable = 1;
Dianne Hackborn11b822d2009-07-21 20:03:02 -0700999 int anyDensity = 1;
Dianne Hackborn723738c2009-06-25 19:48:04 -07001000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 int outerDepth = parser.getDepth();
Kenny Rootd2d29252011-08-08 11:27:57 -07001002 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1003 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1004 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 continue;
1006 }
1007
1008 String tagName = parser.getName();
1009 if (tagName.equals("application")) {
1010 if (foundApp) {
1011 if (RIGID_PARSER) {
1012 outError[0] = "<manifest> has more than one <application>";
1013 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1014 return null;
1015 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -07001016 Slog.w(TAG, "<manifest> has more than one <application>");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 XmlUtils.skipCurrentTag(parser);
1018 continue;
1019 }
1020 }
1021
1022 foundApp = true;
1023 if (!parseApplication(pkg, res, parser, attrs, flags, outError)) {
1024 return null;
1025 }
1026 } else if (tagName.equals("permission-group")) {
Dianne Hackbornfd5015b2012-04-30 16:33:56 -07001027 if (parsePermissionGroup(pkg, flags, res, parser, attrs, outError) == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 return null;
1029 }
1030 } else if (tagName.equals("permission")) {
1031 if (parsePermission(pkg, res, parser, attrs, outError) == null) {
1032 return null;
1033 }
1034 } else if (tagName.equals("permission-tree")) {
1035 if (parsePermissionTree(pkg, res, parser, attrs, outError) == null) {
1036 return null;
1037 }
1038 } else if (tagName.equals("uses-permission")) {
1039 sa = res.obtainAttributes(attrs,
1040 com.android.internal.R.styleable.AndroidManifestUsesPermission);
1041
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001042 // Note: don't allow this value to be a reference to a resource
1043 // that may change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 String name = sa.getNonResourceString(
1045 com.android.internal.R.styleable.AndroidManifestUsesPermission_name);
Dianne Hackborne639da72012-02-21 15:11:13 -08001046 boolean required = sa.getBoolean(
1047 com.android.internal.R.styleable.AndroidManifestUsesPermission_required, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048
1049 sa.recycle();
1050
1051 if (name != null && !pkg.requestedPermissions.contains(name)) {
Dianne Hackborn854060a2009-07-09 18:14:31 -07001052 pkg.requestedPermissions.add(name.intern());
Nick Kralevich828c16f2013-03-26 16:59:37 -07001053 pkg.requestedPermissionsRequired.add(required ? Boolean.TRUE : Boolean.FALSE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 }
1055
1056 XmlUtils.skipCurrentTag(parser);
1057
1058 } else if (tagName.equals("uses-configuration")) {
1059 ConfigurationInfo cPref = new ConfigurationInfo();
1060 sa = res.obtainAttributes(attrs,
1061 com.android.internal.R.styleable.AndroidManifestUsesConfiguration);
1062 cPref.reqTouchScreen = sa.getInt(
1063 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqTouchScreen,
1064 Configuration.TOUCHSCREEN_UNDEFINED);
1065 cPref.reqKeyboardType = sa.getInt(
1066 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqKeyboardType,
1067 Configuration.KEYBOARD_UNDEFINED);
1068 if (sa.getBoolean(
1069 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqHardKeyboard,
1070 false)) {
1071 cPref.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD;
1072 }
1073 cPref.reqNavigation = sa.getInt(
1074 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqNavigation,
1075 Configuration.NAVIGATION_UNDEFINED);
1076 if (sa.getBoolean(
1077 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqFiveWayNav,
1078 false)) {
1079 cPref.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV;
1080 }
1081 sa.recycle();
1082 pkg.configPreferences.add(cPref);
1083
1084 XmlUtils.skipCurrentTag(parser);
1085
Suchi Amalapurapud299b812009-06-05 10:26:19 -07001086 } else if (tagName.equals("uses-feature")) {
Dianne Hackborn49237342009-08-27 20:08:01 -07001087 FeatureInfo fi = new FeatureInfo();
Suchi Amalapurapud299b812009-06-05 10:26:19 -07001088 sa = res.obtainAttributes(attrs,
1089 com.android.internal.R.styleable.AndroidManifestUsesFeature);
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001090 // Note: don't allow this value to be a reference to a resource
1091 // that may change.
Dianne Hackborn49237342009-08-27 20:08:01 -07001092 fi.name = sa.getNonResourceString(
1093 com.android.internal.R.styleable.AndroidManifestUsesFeature_name);
1094 if (fi.name == null) {
1095 fi.reqGlEsVersion = sa.getInt(
1096 com.android.internal.R.styleable.AndroidManifestUsesFeature_glEsVersion,
1097 FeatureInfo.GL_ES_VERSION_UNDEFINED);
1098 }
1099 if (sa.getBoolean(
1100 com.android.internal.R.styleable.AndroidManifestUsesFeature_required,
1101 true)) {
1102 fi.flags |= FeatureInfo.FLAG_REQUIRED;
1103 }
Suchi Amalapurapud299b812009-06-05 10:26:19 -07001104 sa.recycle();
Dianne Hackborn49237342009-08-27 20:08:01 -07001105 if (pkg.reqFeatures == null) {
1106 pkg.reqFeatures = new ArrayList<FeatureInfo>();
1107 }
1108 pkg.reqFeatures.add(fi);
1109
1110 if (fi.name == null) {
1111 ConfigurationInfo cPref = new ConfigurationInfo();
1112 cPref.reqGlEsVersion = fi.reqGlEsVersion;
1113 pkg.configPreferences.add(cPref);
1114 }
Suchi Amalapurapud299b812009-06-05 10:26:19 -07001115
1116 XmlUtils.skipCurrentTag(parser);
1117
Dianne Hackborn851a5412009-05-08 12:06:44 -07001118 } else if (tagName.equals("uses-sdk")) {
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001119 if (SDK_VERSION > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 sa = res.obtainAttributes(attrs,
1121 com.android.internal.R.styleable.AndroidManifestUsesSdk);
1122
Dianne Hackborn851a5412009-05-08 12:06:44 -07001123 int minVers = 0;
1124 String minCode = null;
1125 int targetVers = 0;
1126 String targetCode = null;
1127
1128 TypedValue val = sa.peekValue(
1129 com.android.internal.R.styleable.AndroidManifestUsesSdk_minSdkVersion);
1130 if (val != null) {
1131 if (val.type == TypedValue.TYPE_STRING && val.string != null) {
1132 targetCode = minCode = val.string.toString();
1133 } else {
1134 // If it's not a string, it's an integer.
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001135 targetVers = minVers = val.data;
Dianne Hackborn851a5412009-05-08 12:06:44 -07001136 }
1137 }
1138
1139 val = sa.peekValue(
1140 com.android.internal.R.styleable.AndroidManifestUsesSdk_targetSdkVersion);
1141 if (val != null) {
1142 if (val.type == TypedValue.TYPE_STRING && val.string != null) {
1143 targetCode = minCode = val.string.toString();
1144 } else {
1145 // If it's not a string, it's an integer.
1146 targetVers = val.data;
1147 }
1148 }
1149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 sa.recycle();
1151
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001152 if (minCode != null) {
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001153 if (!minCode.equals(SDK_CODENAME)) {
1154 if (SDK_CODENAME != null) {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001155 outError[0] = "Requires development platform " + minCode
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001156 + " (current platform is " + SDK_CODENAME + ")";
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001157 } else {
1158 outError[0] = "Requires development platform " + minCode
1159 + " but this is a release platform.";
1160 }
1161 mParseError = PackageManager.INSTALL_FAILED_OLDER_SDK;
1162 return null;
1163 }
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001164 } else if (minVers > SDK_VERSION) {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001165 outError[0] = "Requires newer sdk version #" + minVers
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001166 + " (current version is #" + SDK_VERSION + ")";
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001167 mParseError = PackageManager.INSTALL_FAILED_OLDER_SDK;
1168 return null;
1169 }
1170
Dianne Hackborn851a5412009-05-08 12:06:44 -07001171 if (targetCode != null) {
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001172 if (!targetCode.equals(SDK_CODENAME)) {
1173 if (SDK_CODENAME != null) {
Dianne Hackborn851a5412009-05-08 12:06:44 -07001174 outError[0] = "Requires development platform " + targetCode
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001175 + " (current platform is " + SDK_CODENAME + ")";
Dianne Hackborn851a5412009-05-08 12:06:44 -07001176 } else {
1177 outError[0] = "Requires development platform " + targetCode
1178 + " but this is a release platform.";
1179 }
1180 mParseError = PackageManager.INSTALL_FAILED_OLDER_SDK;
1181 return null;
1182 }
1183 // If the code matches, it definitely targets this SDK.
Dianne Hackborna96cbb42009-05-13 15:06:13 -07001184 pkg.applicationInfo.targetSdkVersion
1185 = android.os.Build.VERSION_CODES.CUR_DEVELOPMENT;
1186 } else {
1187 pkg.applicationInfo.targetSdkVersion = targetVers;
Dianne Hackborn851a5412009-05-08 12:06:44 -07001188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 }
1190
1191 XmlUtils.skipCurrentTag(parser);
1192
Dianne Hackborn723738c2009-06-25 19:48:04 -07001193 } else if (tagName.equals("supports-screens")) {
1194 sa = res.obtainAttributes(attrs,
1195 com.android.internal.R.styleable.AndroidManifestSupportsScreens);
1196
Dianne Hackborndf6e9802011-05-26 14:20:23 -07001197 pkg.applicationInfo.requiresSmallestWidthDp = sa.getInteger(
1198 com.android.internal.R.styleable.AndroidManifestSupportsScreens_requiresSmallestWidthDp,
1199 0);
1200 pkg.applicationInfo.compatibleWidthLimitDp = sa.getInteger(
1201 com.android.internal.R.styleable.AndroidManifestSupportsScreens_compatibleWidthLimitDp,
1202 0);
Dianne Hackborn2762ff32011-06-01 21:27:05 -07001203 pkg.applicationInfo.largestWidthLimitDp = sa.getInteger(
1204 com.android.internal.R.styleable.AndroidManifestSupportsScreens_largestWidthLimitDp,
1205 0);
Dianne Hackborndf6e9802011-05-26 14:20:23 -07001206
Dianne Hackborn723738c2009-06-25 19:48:04 -07001207 // This is a trick to get a boolean and still able to detect
1208 // if a value was actually set.
1209 supportsSmallScreens = sa.getInteger(
1210 com.android.internal.R.styleable.AndroidManifestSupportsScreens_smallScreens,
1211 supportsSmallScreens);
1212 supportsNormalScreens = sa.getInteger(
1213 com.android.internal.R.styleable.AndroidManifestSupportsScreens_normalScreens,
1214 supportsNormalScreens);
1215 supportsLargeScreens = sa.getInteger(
1216 com.android.internal.R.styleable.AndroidManifestSupportsScreens_largeScreens,
1217 supportsLargeScreens);
Dianne Hackborn14cee9f2010-04-23 17:51:26 -07001218 supportsXLargeScreens = sa.getInteger(
1219 com.android.internal.R.styleable.AndroidManifestSupportsScreens_xlargeScreens,
1220 supportsXLargeScreens);
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001221 resizeable = sa.getInteger(
1222 com.android.internal.R.styleable.AndroidManifestSupportsScreens_resizeable,
Dianne Hackborn14cee9f2010-04-23 17:51:26 -07001223 resizeable);
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001224 anyDensity = sa.getInteger(
1225 com.android.internal.R.styleable.AndroidManifestSupportsScreens_anyDensity,
1226 anyDensity);
Dianne Hackborn723738c2009-06-25 19:48:04 -07001227
1228 sa.recycle();
1229
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001230 XmlUtils.skipCurrentTag(parser);
Dianne Hackborn854060a2009-07-09 18:14:31 -07001231
1232 } else if (tagName.equals("protected-broadcast")) {
1233 sa = res.obtainAttributes(attrs,
1234 com.android.internal.R.styleable.AndroidManifestProtectedBroadcast);
1235
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001236 // Note: don't allow this value to be a reference to a resource
1237 // that may change.
Dianne Hackborn854060a2009-07-09 18:14:31 -07001238 String name = sa.getNonResourceString(
1239 com.android.internal.R.styleable.AndroidManifestProtectedBroadcast_name);
1240
1241 sa.recycle();
1242
1243 if (name != null && (flags&PARSE_IS_SYSTEM) != 0) {
1244 if (pkg.protectedBroadcasts == null) {
1245 pkg.protectedBroadcasts = new ArrayList<String>();
1246 }
1247 if (!pkg.protectedBroadcasts.contains(name)) {
1248 pkg.protectedBroadcasts.add(name.intern());
1249 }
1250 }
1251
1252 XmlUtils.skipCurrentTag(parser);
1253
1254 } else if (tagName.equals("instrumentation")) {
1255 if (parseInstrumentation(pkg, res, parser, attrs, outError) == null) {
1256 return null;
1257 }
1258
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001259 } else if (tagName.equals("original-package")) {
1260 sa = res.obtainAttributes(attrs,
1261 com.android.internal.R.styleable.AndroidManifestOriginalPackage);
1262
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001263 String orig =sa.getNonConfigurationString(
1264 com.android.internal.R.styleable.AndroidManifestOriginalPackage_name, 0);
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08001265 if (!pkg.packageName.equals(orig)) {
Dianne Hackbornc1552392010-03-03 16:19:01 -08001266 if (pkg.mOriginalPackages == null) {
1267 pkg.mOriginalPackages = new ArrayList<String>();
1268 pkg.mRealPackage = pkg.packageName;
1269 }
1270 pkg.mOriginalPackages.add(orig);
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08001271 }
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001272
1273 sa.recycle();
1274
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001275 XmlUtils.skipCurrentTag(parser);
1276
1277 } else if (tagName.equals("adopt-permissions")) {
1278 sa = res.obtainAttributes(attrs,
1279 com.android.internal.R.styleable.AndroidManifestOriginalPackage);
1280
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001281 String name = sa.getNonConfigurationString(
1282 com.android.internal.R.styleable.AndroidManifestOriginalPackage_name, 0);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001283
1284 sa.recycle();
1285
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08001286 if (name != null) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001287 if (pkg.mAdoptPermissions == null) {
1288 pkg.mAdoptPermissions = new ArrayList<String>();
1289 }
1290 pkg.mAdoptPermissions.add(name);
1291 }
1292
1293 XmlUtils.skipCurrentTag(parser);
1294
Dianne Hackborna0b46c92010-10-21 15:32:06 -07001295 } else if (tagName.equals("uses-gl-texture")) {
1296 // Just skip this tag
1297 XmlUtils.skipCurrentTag(parser);
1298 continue;
1299
1300 } else if (tagName.equals("compatible-screens")) {
1301 // Just skip this tag
1302 XmlUtils.skipCurrentTag(parser);
1303 continue;
1304
Dianne Hackborn854060a2009-07-09 18:14:31 -07001305 } else if (tagName.equals("eat-comment")) {
1306 // Just skip this tag
1307 XmlUtils.skipCurrentTag(parser);
1308 continue;
1309
1310 } else if (RIGID_PARSER) {
1311 outError[0] = "Bad element under <manifest>: "
1312 + parser.getName();
1313 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1314 return null;
1315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -07001317 Slog.w(TAG, "Unknown element under <manifest>: " + parser.getName()
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -07001318 + " at " + mArchiveSourcePath + " "
1319 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 XmlUtils.skipCurrentTag(parser);
1321 continue;
1322 }
1323 }
1324
1325 if (!foundApp && pkg.instrumentation.size() == 0) {
1326 outError[0] = "<manifest> does not contain an <application> or <instrumentation>";
1327 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_EMPTY;
1328 }
1329
Dianne Hackborna96cbb42009-05-13 15:06:13 -07001330 final int NP = PackageParser.NEW_PERMISSIONS.length;
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001331 StringBuilder implicitPerms = null;
Dianne Hackborna96cbb42009-05-13 15:06:13 -07001332 for (int ip=0; ip<NP; ip++) {
1333 final PackageParser.NewPermissionInfo npi
1334 = PackageParser.NEW_PERMISSIONS[ip];
1335 if (pkg.applicationInfo.targetSdkVersion >= npi.sdkVersion) {
1336 break;
1337 }
1338 if (!pkg.requestedPermissions.contains(npi.name)) {
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001339 if (implicitPerms == null) {
1340 implicitPerms = new StringBuilder(128);
1341 implicitPerms.append(pkg.packageName);
1342 implicitPerms.append(": compat added ");
1343 } else {
1344 implicitPerms.append(' ');
1345 }
1346 implicitPerms.append(npi.name);
Dianne Hackborna96cbb42009-05-13 15:06:13 -07001347 pkg.requestedPermissions.add(npi.name);
Dianne Hackborn65696252012-03-05 18:49:21 -08001348 pkg.requestedPermissionsRequired.add(Boolean.TRUE);
Dianne Hackborna96cbb42009-05-13 15:06:13 -07001349 }
Dianne Hackborn851a5412009-05-08 12:06:44 -07001350 }
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001351 if (implicitPerms != null) {
Kenny Rootd2d29252011-08-08 11:27:57 -07001352 Slog.i(TAG, implicitPerms.toString());
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001353 }
Dianne Hackborn79245122012-03-12 10:51:26 -07001354
1355 final int NS = PackageParser.SPLIT_PERMISSIONS.length;
1356 for (int is=0; is<NS; is++) {
1357 final PackageParser.SplitPermissionInfo spi
1358 = PackageParser.SPLIT_PERMISSIONS[is];
Dianne Hackborn31b0e0e2012-04-05 19:33:30 -07001359 if (pkg.applicationInfo.targetSdkVersion >= spi.targetSdk
1360 || !pkg.requestedPermissions.contains(spi.rootPerm)) {
Dianne Hackborn5e4705a2012-04-06 12:55:53 -07001361 continue;
Dianne Hackborn79245122012-03-12 10:51:26 -07001362 }
1363 for (int in=0; in<spi.newPerms.length; in++) {
1364 final String perm = spi.newPerms[in];
1365 if (!pkg.requestedPermissions.contains(perm)) {
1366 pkg.requestedPermissions.add(perm);
1367 pkg.requestedPermissionsRequired.add(Boolean.TRUE);
1368 }
1369 }
1370 }
1371
Dianne Hackborn723738c2009-06-25 19:48:04 -07001372 if (supportsSmallScreens < 0 || (supportsSmallScreens > 0
1373 && pkg.applicationInfo.targetSdkVersion
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001374 >= android.os.Build.VERSION_CODES.DONUT)) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07001375 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_SMALL_SCREENS;
1376 }
1377 if (supportsNormalScreens != 0) {
1378 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_NORMAL_SCREENS;
1379 }
1380 if (supportsLargeScreens < 0 || (supportsLargeScreens > 0
1381 && pkg.applicationInfo.targetSdkVersion
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001382 >= android.os.Build.VERSION_CODES.DONUT)) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07001383 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_LARGE_SCREENS;
1384 }
Dianne Hackborn14cee9f2010-04-23 17:51:26 -07001385 if (supportsXLargeScreens < 0 || (supportsXLargeScreens > 0
1386 && pkg.applicationInfo.targetSdkVersion
1387 >= android.os.Build.VERSION_CODES.GINGERBREAD)) {
1388 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_XLARGE_SCREENS;
1389 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001390 if (resizeable < 0 || (resizeable > 0
1391 && pkg.applicationInfo.targetSdkVersion
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001392 >= android.os.Build.VERSION_CODES.DONUT)) {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001393 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_RESIZEABLE_FOR_SCREENS;
1394 }
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001395 if (anyDensity < 0 || (anyDensity > 0
1396 && pkg.applicationInfo.targetSdkVersion
1397 >= android.os.Build.VERSION_CODES.DONUT)) {
1398 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES;
Mitsuru Oshima8d112672009-04-27 12:01:23 -07001399 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07001400
Nick Kralevich38f130e2013-04-04 13:19:10 -07001401 /*
1402 * b/8528162: Ignore the <uses-permission android:required> attribute if
1403 * targetSdkVersion < JELLY_BEAN_MR2. There are lots of apps in the wild
1404 * which are improperly using this attribute, even though it never worked.
1405 */
1406 if (pkg.applicationInfo.targetSdkVersion < Build.VERSION_CODES.JELLY_BEAN_MR2) {
1407 for (int i = 0; i < pkg.requestedPermissionsRequired.size(); i++) {
1408 pkg.requestedPermissionsRequired.set(i, Boolean.TRUE);
1409 }
1410 }
1411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 return pkg;
1413 }
1414
1415 private static String buildClassName(String pkg, CharSequence clsSeq,
1416 String[] outError) {
1417 if (clsSeq == null || clsSeq.length() <= 0) {
1418 outError[0] = "Empty class name in package " + pkg;
1419 return null;
1420 }
1421 String cls = clsSeq.toString();
1422 char c = cls.charAt(0);
1423 if (c == '.') {
1424 return (pkg + cls).intern();
1425 }
1426 if (cls.indexOf('.') < 0) {
1427 StringBuilder b = new StringBuilder(pkg);
1428 b.append('.');
1429 b.append(cls);
1430 return b.toString().intern();
1431 }
1432 if (c >= 'a' && c <= 'z') {
1433 return cls.intern();
1434 }
1435 outError[0] = "Bad class name " + cls + " in package " + pkg;
1436 return null;
1437 }
1438
1439 private static String buildCompoundName(String pkg,
1440 CharSequence procSeq, String type, String[] outError) {
1441 String proc = procSeq.toString();
1442 char c = proc.charAt(0);
1443 if (pkg != null && c == ':') {
1444 if (proc.length() < 2) {
1445 outError[0] = "Bad " + type + " name " + proc + " in package " + pkg
1446 + ": must be at least two characters";
1447 return null;
1448 }
1449 String subName = proc.substring(1);
1450 String nameError = validateName(subName, false);
1451 if (nameError != null) {
1452 outError[0] = "Invalid " + type + " name " + proc + " in package "
1453 + pkg + ": " + nameError;
1454 return null;
1455 }
1456 return (pkg + proc).intern();
1457 }
1458 String nameError = validateName(proc, true);
1459 if (nameError != null && !"system".equals(proc)) {
1460 outError[0] = "Invalid " + type + " name " + proc + " in package "
1461 + pkg + ": " + nameError;
1462 return null;
1463 }
1464 return proc.intern();
1465 }
1466
1467 private static String buildProcessName(String pkg, String defProc,
1468 CharSequence procSeq, int flags, String[] separateProcesses,
1469 String[] outError) {
1470 if ((flags&PARSE_IGNORE_PROCESSES) != 0 && !"system".equals(procSeq)) {
1471 return defProc != null ? defProc : pkg;
1472 }
1473 if (separateProcesses != null) {
1474 for (int i=separateProcesses.length-1; i>=0; i--) {
1475 String sp = separateProcesses[i];
1476 if (sp.equals(pkg) || sp.equals(defProc) || sp.equals(procSeq)) {
1477 return pkg;
1478 }
1479 }
1480 }
1481 if (procSeq == null || procSeq.length() <= 0) {
1482 return defProc;
1483 }
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07001484 return buildCompoundName(pkg, procSeq, "process", outError);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 }
1486
1487 private static String buildTaskAffinityName(String pkg, String defProc,
1488 CharSequence procSeq, String[] outError) {
1489 if (procSeq == null) {
1490 return defProc;
1491 }
1492 if (procSeq.length() <= 0) {
1493 return null;
1494 }
1495 return buildCompoundName(pkg, procSeq, "taskAffinity", outError);
1496 }
1497
Dianne Hackbornfd5015b2012-04-30 16:33:56 -07001498 private PermissionGroup parsePermissionGroup(Package owner, int flags, Resources res,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 XmlPullParser parser, AttributeSet attrs, String[] outError)
1500 throws XmlPullParserException, IOException {
1501 PermissionGroup perm = new PermissionGroup(owner);
1502
1503 TypedArray sa = res.obtainAttributes(attrs,
1504 com.android.internal.R.styleable.AndroidManifestPermissionGroup);
1505
1506 if (!parsePackageItemInfo(owner, perm.info, outError,
1507 "<permission-group>", sa,
1508 com.android.internal.R.styleable.AndroidManifestPermissionGroup_name,
1509 com.android.internal.R.styleable.AndroidManifestPermissionGroup_label,
Adam Powell81cd2e92010-04-21 16:35:18 -07001510 com.android.internal.R.styleable.AndroidManifestPermissionGroup_icon,
1511 com.android.internal.R.styleable.AndroidManifestPermissionGroup_logo)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 sa.recycle();
1513 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1514 return null;
1515 }
1516
1517 perm.info.descriptionRes = sa.getResourceId(
1518 com.android.internal.R.styleable.AndroidManifestPermissionGroup_description,
1519 0);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -07001520 perm.info.flags = sa.getInt(
1521 com.android.internal.R.styleable.AndroidManifestPermissionGroup_permissionGroupFlags, 0);
Dianne Hackbornfd5015b2012-04-30 16:33:56 -07001522 perm.info.priority = sa.getInt(
1523 com.android.internal.R.styleable.AndroidManifestPermissionGroup_priority, 0);
Dianne Hackborn99222d22012-05-06 16:30:15 -07001524 if (perm.info.priority > 0 && (flags&PARSE_IS_SYSTEM) == 0) {
Dianne Hackbornfd5015b2012-04-30 16:33:56 -07001525 perm.info.priority = 0;
1526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527
1528 sa.recycle();
1529
1530 if (!parseAllMetaData(res, parser, attrs, "<permission-group>", perm,
1531 outError)) {
1532 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1533 return null;
1534 }
1535
1536 owner.permissionGroups.add(perm);
1537
1538 return perm;
1539 }
1540
1541 private Permission parsePermission(Package owner, Resources res,
1542 XmlPullParser parser, AttributeSet attrs, String[] outError)
1543 throws XmlPullParserException, IOException {
1544 Permission perm = new Permission(owner);
1545
1546 TypedArray sa = res.obtainAttributes(attrs,
1547 com.android.internal.R.styleable.AndroidManifestPermission);
1548
1549 if (!parsePackageItemInfo(owner, perm.info, outError,
1550 "<permission>", sa,
1551 com.android.internal.R.styleable.AndroidManifestPermission_name,
1552 com.android.internal.R.styleable.AndroidManifestPermission_label,
Adam Powell81cd2e92010-04-21 16:35:18 -07001553 com.android.internal.R.styleable.AndroidManifestPermission_icon,
1554 com.android.internal.R.styleable.AndroidManifestPermission_logo)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 sa.recycle();
1556 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1557 return null;
1558 }
1559
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001560 // Note: don't allow this value to be a reference to a resource
1561 // that may change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 perm.info.group = sa.getNonResourceString(
1563 com.android.internal.R.styleable.AndroidManifestPermission_permissionGroup);
1564 if (perm.info.group != null) {
1565 perm.info.group = perm.info.group.intern();
1566 }
1567
1568 perm.info.descriptionRes = sa.getResourceId(
1569 com.android.internal.R.styleable.AndroidManifestPermission_description,
1570 0);
1571
1572 perm.info.protectionLevel = sa.getInt(
1573 com.android.internal.R.styleable.AndroidManifestPermission_protectionLevel,
1574 PermissionInfo.PROTECTION_NORMAL);
1575
Dianne Hackborn2ca2c872012-09-16 16:03:36 -07001576 perm.info.flags = sa.getInt(
1577 com.android.internal.R.styleable.AndroidManifestPermission_permissionFlags, 0);
1578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 sa.recycle();
Dianne Hackborne639da72012-02-21 15:11:13 -08001580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 if (perm.info.protectionLevel == -1) {
1582 outError[0] = "<permission> does not specify protectionLevel";
1583 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1584 return null;
1585 }
Dianne Hackborne639da72012-02-21 15:11:13 -08001586
1587 perm.info.protectionLevel = PermissionInfo.fixProtectionLevel(perm.info.protectionLevel);
1588
1589 if ((perm.info.protectionLevel&PermissionInfo.PROTECTION_MASK_FLAGS) != 0) {
1590 if ((perm.info.protectionLevel&PermissionInfo.PROTECTION_MASK_BASE) !=
1591 PermissionInfo.PROTECTION_SIGNATURE) {
1592 outError[0] = "<permission> protectionLevel specifies a flag but is "
1593 + "not based on signature type";
1594 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1595 return null;
1596 }
1597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598
1599 if (!parseAllMetaData(res, parser, attrs, "<permission>", perm,
1600 outError)) {
1601 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1602 return null;
1603 }
1604
1605 owner.permissions.add(perm);
1606
1607 return perm;
1608 }
1609
1610 private Permission parsePermissionTree(Package owner, Resources res,
1611 XmlPullParser parser, AttributeSet attrs, String[] outError)
1612 throws XmlPullParserException, IOException {
1613 Permission perm = new Permission(owner);
1614
1615 TypedArray sa = res.obtainAttributes(attrs,
1616 com.android.internal.R.styleable.AndroidManifestPermissionTree);
1617
1618 if (!parsePackageItemInfo(owner, perm.info, outError,
1619 "<permission-tree>", sa,
1620 com.android.internal.R.styleable.AndroidManifestPermissionTree_name,
1621 com.android.internal.R.styleable.AndroidManifestPermissionTree_label,
Adam Powell81cd2e92010-04-21 16:35:18 -07001622 com.android.internal.R.styleable.AndroidManifestPermissionTree_icon,
1623 com.android.internal.R.styleable.AndroidManifestPermissionTree_logo)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 sa.recycle();
1625 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1626 return null;
1627 }
1628
1629 sa.recycle();
1630
1631 int index = perm.info.name.indexOf('.');
1632 if (index > 0) {
1633 index = perm.info.name.indexOf('.', index+1);
1634 }
1635 if (index < 0) {
1636 outError[0] = "<permission-tree> name has less than three segments: "
1637 + perm.info.name;
1638 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1639 return null;
1640 }
1641
1642 perm.info.descriptionRes = 0;
1643 perm.info.protectionLevel = PermissionInfo.PROTECTION_NORMAL;
1644 perm.tree = true;
1645
1646 if (!parseAllMetaData(res, parser, attrs, "<permission-tree>", perm,
1647 outError)) {
1648 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1649 return null;
1650 }
1651
1652 owner.permissions.add(perm);
1653
1654 return perm;
1655 }
1656
1657 private Instrumentation parseInstrumentation(Package owner, Resources res,
1658 XmlPullParser parser, AttributeSet attrs, String[] outError)
1659 throws XmlPullParserException, IOException {
1660 TypedArray sa = res.obtainAttributes(attrs,
1661 com.android.internal.R.styleable.AndroidManifestInstrumentation);
1662
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001663 if (mParseInstrumentationArgs == null) {
1664 mParseInstrumentationArgs = new ParsePackageItemArgs(owner, outError,
1665 com.android.internal.R.styleable.AndroidManifestInstrumentation_name,
1666 com.android.internal.R.styleable.AndroidManifestInstrumentation_label,
Adam Powell81cd2e92010-04-21 16:35:18 -07001667 com.android.internal.R.styleable.AndroidManifestInstrumentation_icon,
1668 com.android.internal.R.styleable.AndroidManifestInstrumentation_logo);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001669 mParseInstrumentationArgs.tag = "<instrumentation>";
1670 }
1671
1672 mParseInstrumentationArgs.sa = sa;
1673
1674 Instrumentation a = new Instrumentation(mParseInstrumentationArgs,
1675 new InstrumentationInfo());
1676 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 sa.recycle();
1678 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1679 return null;
1680 }
1681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 String str;
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001683 // Note: don't allow this value to be a reference to a resource
1684 // that may change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 str = sa.getNonResourceString(
1686 com.android.internal.R.styleable.AndroidManifestInstrumentation_targetPackage);
1687 a.info.targetPackage = str != null ? str.intern() : null;
1688
1689 a.info.handleProfiling = sa.getBoolean(
1690 com.android.internal.R.styleable.AndroidManifestInstrumentation_handleProfiling,
1691 false);
1692
1693 a.info.functionalTest = sa.getBoolean(
1694 com.android.internal.R.styleable.AndroidManifestInstrumentation_functionalTest,
1695 false);
1696
1697 sa.recycle();
1698
1699 if (a.info.targetPackage == null) {
1700 outError[0] = "<instrumentation> does not specify targetPackage";
1701 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1702 return null;
1703 }
1704
1705 if (!parseAllMetaData(res, parser, attrs, "<instrumentation>", a,
1706 outError)) {
1707 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1708 return null;
1709 }
1710
1711 owner.instrumentation.add(a);
1712
1713 return a;
1714 }
1715
1716 private boolean parseApplication(Package owner, Resources res,
1717 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
1718 throws XmlPullParserException, IOException {
1719 final ApplicationInfo ai = owner.applicationInfo;
1720 final String pkgName = owner.applicationInfo.packageName;
1721
1722 TypedArray sa = res.obtainAttributes(attrs,
1723 com.android.internal.R.styleable.AndroidManifestApplication);
1724
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001725 String name = sa.getNonConfigurationString(
1726 com.android.internal.R.styleable.AndroidManifestApplication_name, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 if (name != null) {
1728 ai.className = buildClassName(pkgName, name, outError);
1729 if (ai.className == null) {
1730 sa.recycle();
1731 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1732 return false;
1733 }
1734 }
1735
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001736 String manageSpaceActivity = sa.getNonConfigurationString(
1737 com.android.internal.R.styleable.AndroidManifestApplication_manageSpaceActivity, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 if (manageSpaceActivity != null) {
1739 ai.manageSpaceActivityName = buildClassName(pkgName, manageSpaceActivity,
1740 outError);
1741 }
1742
Christopher Tate181fafa2009-05-14 11:12:14 -07001743 boolean allowBackup = sa.getBoolean(
1744 com.android.internal.R.styleable.AndroidManifestApplication_allowBackup, true);
1745 if (allowBackup) {
1746 ai.flags |= ApplicationInfo.FLAG_ALLOW_BACKUP;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001747
Christopher Tate3de55bc2010-03-12 17:28:08 -08001748 // backupAgent, killAfterRestore, and restoreAnyVersion are only relevant
1749 // if backup is possible for the given application.
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001750 String backupAgent = sa.getNonConfigurationString(
1751 com.android.internal.R.styleable.AndroidManifestApplication_backupAgent, 0);
Christopher Tate181fafa2009-05-14 11:12:14 -07001752 if (backupAgent != null) {
1753 ai.backupAgentName = buildClassName(pkgName, backupAgent, outError);
Kenny Rootd2d29252011-08-08 11:27:57 -07001754 if (DEBUG_BACKUP) {
1755 Slog.v(TAG, "android:backupAgent = " + ai.backupAgentName
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001756 + " from " + pkgName + "+" + backupAgent);
1757 }
Christopher Tate5e1ab332009-09-01 20:32:49 -07001758
1759 if (sa.getBoolean(
1760 com.android.internal.R.styleable.AndroidManifestApplication_killAfterRestore,
1761 true)) {
1762 ai.flags |= ApplicationInfo.FLAG_KILL_AFTER_RESTORE;
1763 }
1764 if (sa.getBoolean(
Christopher Tate3dda5182010-02-24 16:06:18 -08001765 com.android.internal.R.styleable.AndroidManifestApplication_restoreAnyVersion,
1766 false)) {
1767 ai.flags |= ApplicationInfo.FLAG_RESTORE_ANY_VERSION;
1768 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001769 }
1770 }
Christopher Tate4a627c72011-04-01 14:43:32 -07001771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 TypedValue v = sa.peekValue(
1773 com.android.internal.R.styleable.AndroidManifestApplication_label);
1774 if (v != null && (ai.labelRes=v.resourceId) == 0) {
1775 ai.nonLocalizedLabel = v.coerceToString();
1776 }
1777
1778 ai.icon = sa.getResourceId(
1779 com.android.internal.R.styleable.AndroidManifestApplication_icon, 0);
Adam Powell81cd2e92010-04-21 16:35:18 -07001780 ai.logo = sa.getResourceId(
1781 com.android.internal.R.styleable.AndroidManifestApplication_logo, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 ai.theme = sa.getResourceId(
Dianne Hackbornb35cd542011-01-04 21:30:53 -08001783 com.android.internal.R.styleable.AndroidManifestApplication_theme, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 ai.descriptionRes = sa.getResourceId(
1785 com.android.internal.R.styleable.AndroidManifestApplication_description, 0);
1786
1787 if ((flags&PARSE_IS_SYSTEM) != 0) {
1788 if (sa.getBoolean(
1789 com.android.internal.R.styleable.AndroidManifestApplication_persistent,
1790 false)) {
1791 ai.flags |= ApplicationInfo.FLAG_PERSISTENT;
1792 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001793 if (sa.getBoolean(
1794 com.android.internal.R.styleable.AndroidManifestApplication_requiredForAllUsers,
1795 false)) {
1796 owner.mRequiredForAllUsers = true;
1797 }
Amith Yamasani0ac1fc92013-03-27 18:56:08 -07001798 String accountType = sa.getString(com.android.internal.R.styleable
1799 .AndroidManifestApplication_restrictedAccountType);
1800 if (accountType != null && accountType.length() > 0) {
1801 owner.mRestrictedAccountType = accountType;
1802 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 }
1804
1805 if (sa.getBoolean(
1806 com.android.internal.R.styleable.AndroidManifestApplication_debuggable,
1807 false)) {
1808 ai.flags |= ApplicationInfo.FLAG_DEBUGGABLE;
1809 }
1810
1811 if (sa.getBoolean(
Ben Chengef3f5dd2010-03-29 15:47:26 -07001812 com.android.internal.R.styleable.AndroidManifestApplication_vmSafeMode,
Ben Cheng23085b72010-02-08 16:06:32 -08001813 false)) {
1814 ai.flags |= ApplicationInfo.FLAG_VM_SAFE_MODE;
1815 }
1816
Romain Guy529b60a2010-08-03 18:05:47 -07001817 boolean hardwareAccelerated = sa.getBoolean(
Romain Guy812ccbe2010-06-01 14:07:24 -07001818 com.android.internal.R.styleable.AndroidManifestApplication_hardwareAccelerated,
Dianne Hackborn2d6833b2011-06-24 16:04:19 -07001819 owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.ICE_CREAM_SANDWICH);
Romain Guy812ccbe2010-06-01 14:07:24 -07001820
1821 if (sa.getBoolean(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 com.android.internal.R.styleable.AndroidManifestApplication_hasCode,
1823 true)) {
1824 ai.flags |= ApplicationInfo.FLAG_HAS_CODE;
1825 }
1826
1827 if (sa.getBoolean(
1828 com.android.internal.R.styleable.AndroidManifestApplication_allowTaskReparenting,
1829 false)) {
1830 ai.flags |= ApplicationInfo.FLAG_ALLOW_TASK_REPARENTING;
1831 }
1832
1833 if (sa.getBoolean(
1834 com.android.internal.R.styleable.AndroidManifestApplication_allowClearUserData,
1835 true)) {
1836 ai.flags |= ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA;
1837 }
1838
Dianne Hackbornade3eca2009-05-11 18:54:45 -07001839 if (sa.getBoolean(
1840 com.android.internal.R.styleable.AndroidManifestApplication_testOnly,
Dianne Hackborne7fe35b2009-05-13 10:53:41 -07001841 false)) {
Dianne Hackbornade3eca2009-05-11 18:54:45 -07001842 ai.flags |= ApplicationInfo.FLAG_TEST_ONLY;
1843 }
1844
Jason parksa3cdaa52011-01-13 14:15:43 -06001845 if (sa.getBoolean(
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08001846 com.android.internal.R.styleable.AndroidManifestApplication_largeHeap,
Jason parksa3cdaa52011-01-13 14:15:43 -06001847 false)) {
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08001848 ai.flags |= ApplicationInfo.FLAG_LARGE_HEAP;
Jason parksa3cdaa52011-01-13 14:15:43 -06001849 }
1850
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -07001851 if (sa.getBoolean(
1852 com.android.internal.R.styleable.AndroidManifestApplication_supportsRtl,
1853 false /* default is no RTL support*/)) {
1854 ai.flags |= ApplicationInfo.FLAG_SUPPORTS_RTL;
1855 }
1856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 String str;
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001858 str = sa.getNonConfigurationString(
1859 com.android.internal.R.styleable.AndroidManifestApplication_permission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 ai.permission = (str != null && str.length() > 0) ? str.intern() : null;
1861
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07001862 if (owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.FROYO) {
1863 str = sa.getNonConfigurationString(
1864 com.android.internal.R.styleable.AndroidManifestApplication_taskAffinity, 0);
1865 } else {
1866 // Some older apps have been seen to use a resource reference
1867 // here that on older builds was ignored (with a warning). We
1868 // need to continue to do this for them so they don't break.
1869 str = sa.getNonResourceString(
1870 com.android.internal.R.styleable.AndroidManifestApplication_taskAffinity);
1871 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 ai.taskAffinity = buildTaskAffinityName(ai.packageName, ai.packageName,
1873 str, outError);
1874
1875 if (outError[0] == null) {
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07001876 CharSequence pname;
1877 if (owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.FROYO) {
1878 pname = sa.getNonConfigurationString(
1879 com.android.internal.R.styleable.AndroidManifestApplication_process, 0);
1880 } else {
1881 // Some older apps have been seen to use a resource reference
1882 // here that on older builds was ignored (with a warning). We
1883 // need to continue to do this for them so they don't break.
1884 pname = sa.getNonResourceString(
1885 com.android.internal.R.styleable.AndroidManifestApplication_process);
1886 }
1887 ai.processName = buildProcessName(ai.packageName, null, pname,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 flags, mSeparateProcesses, outError);
1889
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001890 ai.enabled = sa.getBoolean(
1891 com.android.internal.R.styleable.AndroidManifestApplication_enabled, true);
Dianne Hackborn860755f2010-06-03 18:47:52 -07001892
Dianne Hackborn02486b12010-08-26 14:18:37 -07001893 if (false) {
1894 if (sa.getBoolean(
1895 com.android.internal.R.styleable.AndroidManifestApplication_cantSaveState,
1896 false)) {
Dianne Hackborn54e570f2010-10-04 18:32:32 -07001897 ai.flags |= ApplicationInfo.FLAG_CANT_SAVE_STATE;
Dianne Hackborn02486b12010-08-26 14:18:37 -07001898
1899 // A heavy-weight application can not be in a custom process.
1900 // We can do direct compare because we intern all strings.
1901 if (ai.processName != null && ai.processName != ai.packageName) {
1902 outError[0] = "cantSaveState applications can not use custom processes";
1903 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07001904 }
1905 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 }
1907
Adam Powell269248d2011-08-02 10:26:54 -07001908 ai.uiOptions = sa.getInt(
1909 com.android.internal.R.styleable.AndroidManifestApplication_uiOptions, 0);
1910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 sa.recycle();
1912
1913 if (outError[0] != null) {
1914 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1915 return false;
1916 }
1917
1918 final int innerDepth = parser.getDepth();
1919
1920 int type;
Kenny Rootd2d29252011-08-08 11:27:57 -07001921 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1922 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1923 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 continue;
1925 }
1926
1927 String tagName = parser.getName();
1928 if (tagName.equals("activity")) {
Romain Guy529b60a2010-08-03 18:05:47 -07001929 Activity a = parseActivity(owner, res, parser, attrs, flags, outError, false,
1930 hardwareAccelerated);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 if (a == null) {
1932 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1933 return false;
1934 }
1935
1936 owner.activities.add(a);
1937
1938 } else if (tagName.equals("receiver")) {
Romain Guy529b60a2010-08-03 18:05:47 -07001939 Activity a = parseActivity(owner, res, parser, attrs, flags, outError, true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 if (a == null) {
1941 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1942 return false;
1943 }
1944
1945 owner.receivers.add(a);
1946
1947 } else if (tagName.equals("service")) {
1948 Service s = parseService(owner, res, parser, attrs, flags, outError);
1949 if (s == null) {
1950 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1951 return false;
1952 }
1953
1954 owner.services.add(s);
1955
1956 } else if (tagName.equals("provider")) {
1957 Provider p = parseProvider(owner, res, parser, attrs, flags, outError);
1958 if (p == null) {
1959 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1960 return false;
1961 }
1962
1963 owner.providers.add(p);
1964
1965 } else if (tagName.equals("activity-alias")) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001966 Activity a = parseActivityAlias(owner, res, parser, attrs, flags, outError);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 if (a == null) {
1968 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1969 return false;
1970 }
1971
1972 owner.activities.add(a);
1973
1974 } else if (parser.getName().equals("meta-data")) {
1975 // note: application meta-data is stored off to the side, so it can
1976 // remain null in the primary copy (we like to avoid extra copies because
1977 // it can be large)
1978 if ((owner.mAppMetaData = parseMetaData(res, parser, attrs, owner.mAppMetaData,
1979 outError)) == null) {
1980 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1981 return false;
1982 }
1983
Dianne Hackbornc895be72013-03-11 17:48:43 -07001984 } else if (tagName.equals("library")) {
1985 sa = res.obtainAttributes(attrs,
1986 com.android.internal.R.styleable.AndroidManifestLibrary);
1987
1988 // Note: don't allow this value to be a reference to a resource
1989 // that may change.
1990 String lname = sa.getNonResourceString(
1991 com.android.internal.R.styleable.AndroidManifestLibrary_name);
1992
1993 sa.recycle();
1994
1995 if (lname != null) {
1996 if (owner.libraryNames == null) {
1997 owner.libraryNames = new ArrayList<String>();
1998 }
1999 if (!owner.libraryNames.contains(lname)) {
2000 owner.libraryNames.add(lname.intern());
2001 }
2002 }
2003
2004 XmlUtils.skipCurrentTag(parser);
2005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 } else if (tagName.equals("uses-library")) {
2007 sa = res.obtainAttributes(attrs,
2008 com.android.internal.R.styleable.AndroidManifestUsesLibrary);
2009
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002010 // Note: don't allow this value to be a reference to a resource
2011 // that may change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 String lname = sa.getNonResourceString(
2013 com.android.internal.R.styleable.AndroidManifestUsesLibrary_name);
Dianne Hackborn49237342009-08-27 20:08:01 -07002014 boolean req = sa.getBoolean(
2015 com.android.internal.R.styleable.AndroidManifestUsesLibrary_required,
2016 true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017
2018 sa.recycle();
2019
Dianne Hackborn49237342009-08-27 20:08:01 -07002020 if (lname != null) {
2021 if (req) {
2022 if (owner.usesLibraries == null) {
2023 owner.usesLibraries = new ArrayList<String>();
2024 }
2025 if (!owner.usesLibraries.contains(lname)) {
2026 owner.usesLibraries.add(lname.intern());
2027 }
2028 } else {
2029 if (owner.usesOptionalLibraries == null) {
2030 owner.usesOptionalLibraries = new ArrayList<String>();
2031 }
2032 if (!owner.usesOptionalLibraries.contains(lname)) {
2033 owner.usesOptionalLibraries.add(lname.intern());
2034 }
2035 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 }
2037
2038 XmlUtils.skipCurrentTag(parser);
2039
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002040 } else if (tagName.equals("uses-package")) {
2041 // Dependencies for app installers; we don't currently try to
2042 // enforce this.
2043 XmlUtils.skipCurrentTag(parser);
2044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 } else {
2046 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002047 Slog.w(TAG, "Unknown element under <application>: " + tagName
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002048 + " at " + mArchiveSourcePath + " "
2049 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 XmlUtils.skipCurrentTag(parser);
2051 continue;
2052 } else {
2053 outError[0] = "Bad element under <application>: " + tagName;
2054 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
2055 return false;
2056 }
2057 }
2058 }
2059
2060 return true;
2061 }
2062
2063 private boolean parsePackageItemInfo(Package owner, PackageItemInfo outInfo,
2064 String[] outError, String tag, TypedArray sa,
Adam Powell81cd2e92010-04-21 16:35:18 -07002065 int nameRes, int labelRes, int iconRes, int logoRes) {
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002066 String name = sa.getNonConfigurationString(nameRes, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 if (name == null) {
2068 outError[0] = tag + " does not specify android:name";
2069 return false;
2070 }
2071
2072 outInfo.name
2073 = buildClassName(owner.applicationInfo.packageName, name, outError);
2074 if (outInfo.name == null) {
2075 return false;
2076 }
2077
2078 int iconVal = sa.getResourceId(iconRes, 0);
2079 if (iconVal != 0) {
2080 outInfo.icon = iconVal;
2081 outInfo.nonLocalizedLabel = null;
2082 }
Adam Powell81cd2e92010-04-21 16:35:18 -07002083
2084 int logoVal = sa.getResourceId(logoRes, 0);
2085 if (logoVal != 0) {
2086 outInfo.logo = logoVal;
2087 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088
2089 TypedValue v = sa.peekValue(labelRes);
2090 if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
2091 outInfo.nonLocalizedLabel = v.coerceToString();
2092 }
2093
2094 outInfo.packageName = owner.packageName;
2095
2096 return true;
2097 }
2098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 private Activity parseActivity(Package owner, Resources res,
2100 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError,
Romain Guy529b60a2010-08-03 18:05:47 -07002101 boolean receiver, boolean hardwareAccelerated)
2102 throws XmlPullParserException, IOException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 TypedArray sa = res.obtainAttributes(attrs,
2104 com.android.internal.R.styleable.AndroidManifestActivity);
2105
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002106 if (mParseActivityArgs == null) {
2107 mParseActivityArgs = new ParseComponentArgs(owner, outError,
2108 com.android.internal.R.styleable.AndroidManifestActivity_name,
2109 com.android.internal.R.styleable.AndroidManifestActivity_label,
2110 com.android.internal.R.styleable.AndroidManifestActivity_icon,
Adam Powell81cd2e92010-04-21 16:35:18 -07002111 com.android.internal.R.styleable.AndroidManifestActivity_logo,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002112 mSeparateProcesses,
2113 com.android.internal.R.styleable.AndroidManifestActivity_process,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08002114 com.android.internal.R.styleable.AndroidManifestActivity_description,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002115 com.android.internal.R.styleable.AndroidManifestActivity_enabled);
2116 }
2117
2118 mParseActivityArgs.tag = receiver ? "<receiver>" : "<activity>";
2119 mParseActivityArgs.sa = sa;
2120 mParseActivityArgs.flags = flags;
2121
2122 Activity a = new Activity(mParseActivityArgs, new ActivityInfo());
2123 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 sa.recycle();
2125 return null;
2126 }
2127
Dianne Hackborn7d19e022012-08-07 19:12:33 -07002128 boolean setExported = sa.hasValue(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 com.android.internal.R.styleable.AndroidManifestActivity_exported);
2130 if (setExported) {
2131 a.info.exported = sa.getBoolean(
2132 com.android.internal.R.styleable.AndroidManifestActivity_exported, false);
2133 }
2134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 a.info.theme = sa.getResourceId(
2136 com.android.internal.R.styleable.AndroidManifestActivity_theme, 0);
2137
Adam Powell269248d2011-08-02 10:26:54 -07002138 a.info.uiOptions = sa.getInt(
2139 com.android.internal.R.styleable.AndroidManifestActivity_uiOptions,
2140 a.info.applicationInfo.uiOptions);
2141
Adam Powelldd8fab22012-03-22 17:47:27 -07002142 String parentName = sa.getNonConfigurationString(
2143 com.android.internal.R.styleable.AndroidManifestActivity_parentActivityName, 0);
2144 if (parentName != null) {
2145 String parentClassName = buildClassName(a.info.packageName, parentName, outError);
2146 if (outError[0] == null) {
2147 a.info.parentActivityName = parentClassName;
2148 } else {
2149 Log.e(TAG, "Activity " + a.info.name + " specified invalid parentActivityName " +
2150 parentName);
2151 outError[0] = null;
2152 }
2153 }
2154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 String str;
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002156 str = sa.getNonConfigurationString(
2157 com.android.internal.R.styleable.AndroidManifestActivity_permission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158 if (str == null) {
2159 a.info.permission = owner.applicationInfo.permission;
2160 } else {
2161 a.info.permission = str.length() > 0 ? str.toString().intern() : null;
2162 }
2163
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002164 str = sa.getNonConfigurationString(
2165 com.android.internal.R.styleable.AndroidManifestActivity_taskAffinity, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 a.info.taskAffinity = buildTaskAffinityName(owner.applicationInfo.packageName,
2167 owner.applicationInfo.taskAffinity, str, outError);
2168
2169 a.info.flags = 0;
2170 if (sa.getBoolean(
2171 com.android.internal.R.styleable.AndroidManifestActivity_multiprocess,
2172 false)) {
2173 a.info.flags |= ActivityInfo.FLAG_MULTIPROCESS;
2174 }
2175
2176 if (sa.getBoolean(
2177 com.android.internal.R.styleable.AndroidManifestActivity_finishOnTaskLaunch,
2178 false)) {
2179 a.info.flags |= ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH;
2180 }
2181
2182 if (sa.getBoolean(
2183 com.android.internal.R.styleable.AndroidManifestActivity_clearTaskOnLaunch,
2184 false)) {
2185 a.info.flags |= ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH;
2186 }
2187
2188 if (sa.getBoolean(
2189 com.android.internal.R.styleable.AndroidManifestActivity_noHistory,
2190 false)) {
2191 a.info.flags |= ActivityInfo.FLAG_NO_HISTORY;
2192 }
2193
2194 if (sa.getBoolean(
2195 com.android.internal.R.styleable.AndroidManifestActivity_alwaysRetainTaskState,
2196 false)) {
2197 a.info.flags |= ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE;
2198 }
2199
2200 if (sa.getBoolean(
2201 com.android.internal.R.styleable.AndroidManifestActivity_stateNotNeeded,
2202 false)) {
2203 a.info.flags |= ActivityInfo.FLAG_STATE_NOT_NEEDED;
2204 }
2205
2206 if (sa.getBoolean(
2207 com.android.internal.R.styleable.AndroidManifestActivity_excludeFromRecents,
2208 false)) {
2209 a.info.flags |= ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS;
2210 }
2211
2212 if (sa.getBoolean(
2213 com.android.internal.R.styleable.AndroidManifestActivity_allowTaskReparenting,
2214 (owner.applicationInfo.flags&ApplicationInfo.FLAG_ALLOW_TASK_REPARENTING) != 0)) {
2215 a.info.flags |= ActivityInfo.FLAG_ALLOW_TASK_REPARENTING;
2216 }
2217
Dianne Hackbornffa42482009-09-23 22:20:11 -07002218 if (sa.getBoolean(
2219 com.android.internal.R.styleable.AndroidManifestActivity_finishOnCloseSystemDialogs,
2220 false)) {
2221 a.info.flags |= ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS;
2222 }
2223
Daniel Sandler613dde42010-06-21 13:46:39 -04002224 if (sa.getBoolean(
Craig Mautner5962b122012-10-05 14:45:52 -07002225 com.android.internal.R.styleable.AndroidManifestActivity_showOnLockScreen,
2226 false)) {
2227 a.info.flags |= ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN;
2228 }
2229
2230 if (sa.getBoolean(
Daniel Sandler613dde42010-06-21 13:46:39 -04002231 com.android.internal.R.styleable.AndroidManifestActivity_immersive,
2232 false)) {
2233 a.info.flags |= ActivityInfo.FLAG_IMMERSIVE;
2234 }
Craig Mautner5962b122012-10-05 14:45:52 -07002235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 if (!receiver) {
Romain Guy529b60a2010-08-03 18:05:47 -07002237 if (sa.getBoolean(
2238 com.android.internal.R.styleable.AndroidManifestActivity_hardwareAccelerated,
2239 hardwareAccelerated)) {
2240 a.info.flags |= ActivityInfo.FLAG_HARDWARE_ACCELERATED;
2241 }
2242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 a.info.launchMode = sa.getInt(
2244 com.android.internal.R.styleable.AndroidManifestActivity_launchMode,
2245 ActivityInfo.LAUNCH_MULTIPLE);
2246 a.info.screenOrientation = sa.getInt(
2247 com.android.internal.R.styleable.AndroidManifestActivity_screenOrientation,
2248 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
2249 a.info.configChanges = sa.getInt(
2250 com.android.internal.R.styleable.AndroidManifestActivity_configChanges,
2251 0);
2252 a.info.softInputMode = sa.getInt(
2253 com.android.internal.R.styleable.AndroidManifestActivity_windowSoftInputMode,
2254 0);
2255 } else {
2256 a.info.launchMode = ActivityInfo.LAUNCH_MULTIPLE;
2257 a.info.configChanges = 0;
2258 }
2259
Dianne Hackborn7d19e022012-08-07 19:12:33 -07002260 if (receiver) {
2261 if (sa.getBoolean(
2262 com.android.internal.R.styleable.AndroidManifestActivity_singleUser,
2263 false)) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07002264 a.info.flags |= ActivityInfo.FLAG_SINGLE_USER;
Dianne Hackborn7d19e022012-08-07 19:12:33 -07002265 if (a.info.exported) {
2266 Slog.w(TAG, "Activity exported request ignored due to singleUser: "
2267 + a.className + " at " + mArchiveSourcePath + " "
2268 + parser.getPositionDescription());
2269 a.info.exported = false;
2270 }
2271 setExported = true;
2272 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07002273 if (sa.getBoolean(
2274 com.android.internal.R.styleable.AndroidManifestActivity_primaryUserOnly,
2275 false)) {
2276 a.info.flags |= ActivityInfo.FLAG_PRIMARY_USER_ONLY;
2277 }
Dianne Hackborn7d19e022012-08-07 19:12:33 -07002278 }
2279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 sa.recycle();
2281
Dianne Hackborn54e570f2010-10-04 18:32:32 -07002282 if (receiver && (owner.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07002283 // A heavy-weight application can not have receives in its main process
2284 // We can do direct compare because we intern all strings.
2285 if (a.info.processName == owner.packageName) {
2286 outError[0] = "Heavy-weight applications can not have receivers in main process";
2287 }
2288 }
2289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002290 if (outError[0] != null) {
2291 return null;
2292 }
2293
2294 int outerDepth = parser.getDepth();
2295 int type;
2296 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2297 && (type != XmlPullParser.END_TAG
2298 || parser.getDepth() > outerDepth)) {
2299 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2300 continue;
2301 }
2302
2303 if (parser.getName().equals("intent-filter")) {
2304 ActivityIntentInfo intent = new ActivityIntentInfo(a);
2305 if (!parseIntent(res, parser, attrs, flags, intent, outError, !receiver)) {
2306 return null;
2307 }
2308 if (intent.countActions() == 0) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002309 Slog.w(TAG, "No actions in intent filter at "
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -07002310 + mArchiveSourcePath + " "
2311 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 } else {
2313 a.intents.add(intent);
2314 }
2315 } else if (parser.getName().equals("meta-data")) {
2316 if ((a.metaData=parseMetaData(res, parser, attrs, a.metaData,
2317 outError)) == null) {
2318 return null;
2319 }
2320 } else {
2321 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002322 Slog.w(TAG, "Problem in package " + mArchiveSourcePath + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 if (receiver) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002324 Slog.w(TAG, "Unknown element under <receiver>: " + parser.getName()
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002325 + " at " + mArchiveSourcePath + " "
2326 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -07002328 Slog.w(TAG, "Unknown element under <activity>: " + parser.getName()
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002329 + " at " + mArchiveSourcePath + " "
2330 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002331 }
2332 XmlUtils.skipCurrentTag(parser);
2333 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002334 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -07002335 if (receiver) {
2336 outError[0] = "Bad element under <receiver>: " + parser.getName();
2337 } else {
2338 outError[0] = "Bad element under <activity>: " + parser.getName();
2339 }
2340 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 }
2343 }
2344
2345 if (!setExported) {
2346 a.info.exported = a.intents.size() > 0;
2347 }
2348
2349 return a;
2350 }
2351
2352 private Activity parseActivityAlias(Package owner, Resources res,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002353 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
2354 throws XmlPullParserException, IOException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 TypedArray sa = res.obtainAttributes(attrs,
2356 com.android.internal.R.styleable.AndroidManifestActivityAlias);
2357
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002358 String targetActivity = sa.getNonConfigurationString(
2359 com.android.internal.R.styleable.AndroidManifestActivityAlias_targetActivity, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 if (targetActivity == null) {
2361 outError[0] = "<activity-alias> does not specify android:targetActivity";
2362 sa.recycle();
2363 return null;
2364 }
2365
2366 targetActivity = buildClassName(owner.applicationInfo.packageName,
2367 targetActivity, outError);
2368 if (targetActivity == null) {
2369 sa.recycle();
2370 return null;
2371 }
2372
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002373 if (mParseActivityAliasArgs == null) {
2374 mParseActivityAliasArgs = new ParseComponentArgs(owner, outError,
2375 com.android.internal.R.styleable.AndroidManifestActivityAlias_name,
2376 com.android.internal.R.styleable.AndroidManifestActivityAlias_label,
2377 com.android.internal.R.styleable.AndroidManifestActivityAlias_icon,
Adam Powell81cd2e92010-04-21 16:35:18 -07002378 com.android.internal.R.styleable.AndroidManifestActivityAlias_logo,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002379 mSeparateProcesses,
2380 0,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08002381 com.android.internal.R.styleable.AndroidManifestActivityAlias_description,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002382 com.android.internal.R.styleable.AndroidManifestActivityAlias_enabled);
2383 mParseActivityAliasArgs.tag = "<activity-alias>";
2384 }
2385
2386 mParseActivityAliasArgs.sa = sa;
2387 mParseActivityAliasArgs.flags = flags;
2388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 Activity target = null;
2390
2391 final int NA = owner.activities.size();
2392 for (int i=0; i<NA; i++) {
2393 Activity t = owner.activities.get(i);
2394 if (targetActivity.equals(t.info.name)) {
2395 target = t;
2396 break;
2397 }
2398 }
2399
2400 if (target == null) {
2401 outError[0] = "<activity-alias> target activity " + targetActivity
2402 + " not found in manifest";
2403 sa.recycle();
2404 return null;
2405 }
2406
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002407 ActivityInfo info = new ActivityInfo();
2408 info.targetActivity = targetActivity;
2409 info.configChanges = target.info.configChanges;
2410 info.flags = target.info.flags;
2411 info.icon = target.info.icon;
Adam Powell81cd2e92010-04-21 16:35:18 -07002412 info.logo = target.info.logo;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002413 info.labelRes = target.info.labelRes;
2414 info.nonLocalizedLabel = target.info.nonLocalizedLabel;
2415 info.launchMode = target.info.launchMode;
2416 info.processName = target.info.processName;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08002417 if (info.descriptionRes == 0) {
2418 info.descriptionRes = target.info.descriptionRes;
2419 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002420 info.screenOrientation = target.info.screenOrientation;
2421 info.taskAffinity = target.info.taskAffinity;
2422 info.theme = target.info.theme;
Dianne Hackborn0836c7c2011-10-20 18:40:23 -07002423 info.softInputMode = target.info.softInputMode;
Adam Powell269248d2011-08-02 10:26:54 -07002424 info.uiOptions = target.info.uiOptions;
Adam Powelldd8fab22012-03-22 17:47:27 -07002425 info.parentActivityName = target.info.parentActivityName;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002426
2427 Activity a = new Activity(mParseActivityAliasArgs, info);
2428 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 sa.recycle();
2430 return null;
2431 }
2432
2433 final boolean setExported = sa.hasValue(
2434 com.android.internal.R.styleable.AndroidManifestActivityAlias_exported);
2435 if (setExported) {
2436 a.info.exported = sa.getBoolean(
2437 com.android.internal.R.styleable.AndroidManifestActivityAlias_exported, false);
2438 }
2439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 String str;
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002441 str = sa.getNonConfigurationString(
2442 com.android.internal.R.styleable.AndroidManifestActivityAlias_permission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 if (str != null) {
2444 a.info.permission = str.length() > 0 ? str.toString().intern() : null;
2445 }
2446
Adam Powelldd8fab22012-03-22 17:47:27 -07002447 String parentName = sa.getNonConfigurationString(
2448 com.android.internal.R.styleable.AndroidManifestActivityAlias_parentActivityName,
2449 0);
2450 if (parentName != null) {
2451 String parentClassName = buildClassName(a.info.packageName, parentName, outError);
2452 if (outError[0] == null) {
2453 a.info.parentActivityName = parentClassName;
2454 } else {
2455 Log.e(TAG, "Activity alias " + a.info.name +
2456 " specified invalid parentActivityName " + parentName);
2457 outError[0] = null;
2458 }
2459 }
2460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 sa.recycle();
2462
2463 if (outError[0] != null) {
2464 return null;
2465 }
2466
2467 int outerDepth = parser.getDepth();
2468 int type;
2469 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2470 && (type != XmlPullParser.END_TAG
2471 || parser.getDepth() > outerDepth)) {
2472 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2473 continue;
2474 }
2475
2476 if (parser.getName().equals("intent-filter")) {
2477 ActivityIntentInfo intent = new ActivityIntentInfo(a);
2478 if (!parseIntent(res, parser, attrs, flags, intent, outError, true)) {
2479 return null;
2480 }
2481 if (intent.countActions() == 0) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002482 Slog.w(TAG, "No actions in intent filter at "
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -07002483 + mArchiveSourcePath + " "
2484 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 } else {
2486 a.intents.add(intent);
2487 }
2488 } else if (parser.getName().equals("meta-data")) {
2489 if ((a.metaData=parseMetaData(res, parser, attrs, a.metaData,
2490 outError)) == null) {
2491 return null;
2492 }
2493 } else {
2494 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002495 Slog.w(TAG, "Unknown element under <activity-alias>: " + parser.getName()
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002496 + " at " + mArchiveSourcePath + " "
2497 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 XmlUtils.skipCurrentTag(parser);
2499 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07002500 } else {
2501 outError[0] = "Bad element under <activity-alias>: " + parser.getName();
2502 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 }
2505 }
2506
2507 if (!setExported) {
2508 a.info.exported = a.intents.size() > 0;
2509 }
2510
2511 return a;
2512 }
2513
2514 private Provider parseProvider(Package owner, Resources res,
2515 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
2516 throws XmlPullParserException, IOException {
2517 TypedArray sa = res.obtainAttributes(attrs,
2518 com.android.internal.R.styleable.AndroidManifestProvider);
2519
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002520 if (mParseProviderArgs == null) {
2521 mParseProviderArgs = new ParseComponentArgs(owner, outError,
2522 com.android.internal.R.styleable.AndroidManifestProvider_name,
2523 com.android.internal.R.styleable.AndroidManifestProvider_label,
2524 com.android.internal.R.styleable.AndroidManifestProvider_icon,
Adam Powell81cd2e92010-04-21 16:35:18 -07002525 com.android.internal.R.styleable.AndroidManifestProvider_logo,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002526 mSeparateProcesses,
2527 com.android.internal.R.styleable.AndroidManifestProvider_process,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08002528 com.android.internal.R.styleable.AndroidManifestProvider_description,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002529 com.android.internal.R.styleable.AndroidManifestProvider_enabled);
2530 mParseProviderArgs.tag = "<provider>";
2531 }
2532
2533 mParseProviderArgs.sa = sa;
2534 mParseProviderArgs.flags = flags;
2535
2536 Provider p = new Provider(mParseProviderArgs, new ProviderInfo());
2537 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 sa.recycle();
2539 return null;
2540 }
2541
Nick Kralevichf097b162012-07-28 12:43:48 -07002542 boolean providerExportedDefault = false;
2543
2544 if (owner.applicationInfo.targetSdkVersion < Build.VERSION_CODES.JELLY_BEAN_MR1) {
2545 // For compatibility, applications targeting API level 16 or lower
2546 // should have their content providers exported by default, unless they
2547 // specify otherwise.
2548 providerExportedDefault = true;
2549 }
2550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 p.info.exported = sa.getBoolean(
Nick Kralevichf097b162012-07-28 12:43:48 -07002552 com.android.internal.R.styleable.AndroidManifestProvider_exported,
2553 providerExportedDefault);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002555 String cpname = sa.getNonConfigurationString(
2556 com.android.internal.R.styleable.AndroidManifestProvider_authorities, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557
2558 p.info.isSyncable = sa.getBoolean(
2559 com.android.internal.R.styleable.AndroidManifestProvider_syncable,
2560 false);
2561
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002562 String permission = sa.getNonConfigurationString(
2563 com.android.internal.R.styleable.AndroidManifestProvider_permission, 0);
2564 String str = sa.getNonConfigurationString(
2565 com.android.internal.R.styleable.AndroidManifestProvider_readPermission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 if (str == null) {
2567 str = permission;
2568 }
2569 if (str == null) {
2570 p.info.readPermission = owner.applicationInfo.permission;
2571 } else {
2572 p.info.readPermission =
2573 str.length() > 0 ? str.toString().intern() : null;
2574 }
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002575 str = sa.getNonConfigurationString(
2576 com.android.internal.R.styleable.AndroidManifestProvider_writePermission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 if (str == null) {
2578 str = permission;
2579 }
2580 if (str == null) {
2581 p.info.writePermission = owner.applicationInfo.permission;
2582 } else {
2583 p.info.writePermission =
2584 str.length() > 0 ? str.toString().intern() : null;
2585 }
2586
2587 p.info.grantUriPermissions = sa.getBoolean(
2588 com.android.internal.R.styleable.AndroidManifestProvider_grantUriPermissions,
2589 false);
2590
2591 p.info.multiprocess = sa.getBoolean(
2592 com.android.internal.R.styleable.AndroidManifestProvider_multiprocess,
2593 false);
2594
2595 p.info.initOrder = sa.getInt(
2596 com.android.internal.R.styleable.AndroidManifestProvider_initOrder,
2597 0);
2598
Dianne Hackborn7d19e022012-08-07 19:12:33 -07002599 p.info.flags = 0;
2600
2601 if (sa.getBoolean(
2602 com.android.internal.R.styleable.AndroidManifestProvider_singleUser,
2603 false)) {
2604 p.info.flags |= ProviderInfo.FLAG_SINGLE_USER;
2605 if (p.info.exported) {
2606 Slog.w(TAG, "Provider exported request ignored due to singleUser: "
2607 + p.className + " at " + mArchiveSourcePath + " "
2608 + parser.getPositionDescription());
2609 p.info.exported = false;
2610 }
2611 }
2612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 sa.recycle();
2614
Dianne Hackborn54e570f2010-10-04 18:32:32 -07002615 if ((owner.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07002616 // A heavy-weight application can not have providers in its main process
2617 // We can do direct compare because we intern all strings.
2618 if (p.info.processName == owner.packageName) {
2619 outError[0] = "Heavy-weight applications can not have providers in main process";
2620 return null;
2621 }
2622 }
2623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 if (cpname == null) {
Nick Kralevichf097b162012-07-28 12:43:48 -07002625 outError[0] = "<provider> does not include authorities attribute";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 return null;
2627 }
2628 p.info.authority = cpname.intern();
2629
2630 if (!parseProviderTags(res, parser, attrs, p, outError)) {
2631 return null;
2632 }
2633
2634 return p;
2635 }
2636
2637 private boolean parseProviderTags(Resources res,
2638 XmlPullParser parser, AttributeSet attrs,
2639 Provider outInfo, String[] outError)
2640 throws XmlPullParserException, IOException {
2641 int outerDepth = parser.getDepth();
2642 int type;
2643 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2644 && (type != XmlPullParser.END_TAG
2645 || parser.getDepth() > outerDepth)) {
2646 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2647 continue;
2648 }
2649
2650 if (parser.getName().equals("meta-data")) {
2651 if ((outInfo.metaData=parseMetaData(res, parser, attrs,
2652 outInfo.metaData, outError)) == null) {
2653 return false;
2654 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656 } else if (parser.getName().equals("grant-uri-permission")) {
2657 TypedArray sa = res.obtainAttributes(attrs,
2658 com.android.internal.R.styleable.AndroidManifestGrantUriPermission);
2659
2660 PatternMatcher pa = null;
2661
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002662 String str = sa.getNonConfigurationString(
2663 com.android.internal.R.styleable.AndroidManifestGrantUriPermission_path, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 if (str != null) {
2665 pa = new PatternMatcher(str, PatternMatcher.PATTERN_LITERAL);
2666 }
2667
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002668 str = sa.getNonConfigurationString(
2669 com.android.internal.R.styleable.AndroidManifestGrantUriPermission_pathPrefix, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670 if (str != null) {
2671 pa = new PatternMatcher(str, PatternMatcher.PATTERN_PREFIX);
2672 }
2673
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002674 str = sa.getNonConfigurationString(
2675 com.android.internal.R.styleable.AndroidManifestGrantUriPermission_pathPattern, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 if (str != null) {
2677 pa = new PatternMatcher(str, PatternMatcher.PATTERN_SIMPLE_GLOB);
2678 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 sa.recycle();
2681
2682 if (pa != null) {
2683 if (outInfo.info.uriPermissionPatterns == null) {
2684 outInfo.info.uriPermissionPatterns = new PatternMatcher[1];
2685 outInfo.info.uriPermissionPatterns[0] = pa;
2686 } else {
2687 final int N = outInfo.info.uriPermissionPatterns.length;
2688 PatternMatcher[] newp = new PatternMatcher[N+1];
2689 System.arraycopy(outInfo.info.uriPermissionPatterns, 0, newp, 0, N);
2690 newp[N] = pa;
2691 outInfo.info.uriPermissionPatterns = newp;
2692 }
2693 outInfo.info.grantUriPermissions = true;
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002694 } else {
2695 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002696 Slog.w(TAG, "Unknown element under <path-permission>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002697 + parser.getName() + " at " + mArchiveSourcePath + " "
2698 + parser.getPositionDescription());
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002699 XmlUtils.skipCurrentTag(parser);
2700 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07002701 } else {
2702 outError[0] = "No path, pathPrefix, or pathPattern for <path-permission>";
2703 return false;
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002704 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002705 }
2706 XmlUtils.skipCurrentTag(parser);
2707
2708 } else if (parser.getName().equals("path-permission")) {
2709 TypedArray sa = res.obtainAttributes(attrs,
2710 com.android.internal.R.styleable.AndroidManifestPathPermission);
2711
2712 PathPermission pa = null;
2713
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002714 String permission = sa.getNonConfigurationString(
2715 com.android.internal.R.styleable.AndroidManifestPathPermission_permission, 0);
2716 String readPermission = sa.getNonConfigurationString(
2717 com.android.internal.R.styleable.AndroidManifestPathPermission_readPermission, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002718 if (readPermission == null) {
2719 readPermission = permission;
2720 }
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002721 String writePermission = sa.getNonConfigurationString(
2722 com.android.internal.R.styleable.AndroidManifestPathPermission_writePermission, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002723 if (writePermission == null) {
2724 writePermission = permission;
2725 }
2726
2727 boolean havePerm = false;
2728 if (readPermission != null) {
2729 readPermission = readPermission.intern();
2730 havePerm = true;
2731 }
2732 if (writePermission != null) {
Bjorn Bringerte04b1ad2010-02-09 13:56:08 +00002733 writePermission = writePermission.intern();
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002734 havePerm = true;
2735 }
2736
2737 if (!havePerm) {
2738 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002739 Slog.w(TAG, "No readPermission or writePermssion for <path-permission>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002740 + parser.getName() + " at " + mArchiveSourcePath + " "
2741 + parser.getPositionDescription());
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002742 XmlUtils.skipCurrentTag(parser);
2743 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07002744 } else {
2745 outError[0] = "No readPermission or writePermssion for <path-permission>";
2746 return false;
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002747 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002748 }
2749
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002750 String path = sa.getNonConfigurationString(
2751 com.android.internal.R.styleable.AndroidManifestPathPermission_path, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002752 if (path != null) {
2753 pa = new PathPermission(path,
2754 PatternMatcher.PATTERN_LITERAL, readPermission, writePermission);
2755 }
2756
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002757 path = sa.getNonConfigurationString(
2758 com.android.internal.R.styleable.AndroidManifestPathPermission_pathPrefix, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002759 if (path != null) {
2760 pa = new PathPermission(path,
2761 PatternMatcher.PATTERN_PREFIX, readPermission, writePermission);
2762 }
2763
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002764 path = sa.getNonConfigurationString(
2765 com.android.internal.R.styleable.AndroidManifestPathPermission_pathPattern, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002766 if (path != null) {
2767 pa = new PathPermission(path,
2768 PatternMatcher.PATTERN_SIMPLE_GLOB, readPermission, writePermission);
2769 }
2770
2771 sa.recycle();
2772
2773 if (pa != null) {
2774 if (outInfo.info.pathPermissions == null) {
2775 outInfo.info.pathPermissions = new PathPermission[1];
2776 outInfo.info.pathPermissions[0] = pa;
2777 } else {
2778 final int N = outInfo.info.pathPermissions.length;
2779 PathPermission[] newp = new PathPermission[N+1];
2780 System.arraycopy(outInfo.info.pathPermissions, 0, newp, 0, N);
2781 newp[N] = pa;
2782 outInfo.info.pathPermissions = newp;
2783 }
2784 } else {
2785 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002786 Slog.w(TAG, "No path, pathPrefix, or pathPattern for <path-permission>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002787 + parser.getName() + " at " + mArchiveSourcePath + " "
2788 + parser.getPositionDescription());
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002789 XmlUtils.skipCurrentTag(parser);
2790 continue;
2791 }
2792 outError[0] = "No path, pathPrefix, or pathPattern for <path-permission>";
2793 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 }
2795 XmlUtils.skipCurrentTag(parser);
2796
2797 } else {
2798 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002799 Slog.w(TAG, "Unknown element under <provider>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002800 + parser.getName() + " at " + mArchiveSourcePath + " "
2801 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 XmlUtils.skipCurrentTag(parser);
2803 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07002804 } else {
2805 outError[0] = "Bad element under <provider>: " + parser.getName();
2806 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808 }
2809 }
2810 return true;
2811 }
2812
2813 private Service parseService(Package owner, Resources res,
2814 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
2815 throws XmlPullParserException, IOException {
2816 TypedArray sa = res.obtainAttributes(attrs,
2817 com.android.internal.R.styleable.AndroidManifestService);
2818
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002819 if (mParseServiceArgs == null) {
2820 mParseServiceArgs = new ParseComponentArgs(owner, outError,
2821 com.android.internal.R.styleable.AndroidManifestService_name,
2822 com.android.internal.R.styleable.AndroidManifestService_label,
2823 com.android.internal.R.styleable.AndroidManifestService_icon,
Adam Powell81cd2e92010-04-21 16:35:18 -07002824 com.android.internal.R.styleable.AndroidManifestService_logo,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002825 mSeparateProcesses,
2826 com.android.internal.R.styleable.AndroidManifestService_process,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08002827 com.android.internal.R.styleable.AndroidManifestService_description,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002828 com.android.internal.R.styleable.AndroidManifestService_enabled);
2829 mParseServiceArgs.tag = "<service>";
2830 }
2831
2832 mParseServiceArgs.sa = sa;
2833 mParseServiceArgs.flags = flags;
2834
2835 Service s = new Service(mParseServiceArgs, new ServiceInfo());
2836 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 sa.recycle();
2838 return null;
2839 }
2840
Dianne Hackbornb4163a62012-08-02 18:31:26 -07002841 boolean setExported = sa.hasValue(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 com.android.internal.R.styleable.AndroidManifestService_exported);
2843 if (setExported) {
2844 s.info.exported = sa.getBoolean(
2845 com.android.internal.R.styleable.AndroidManifestService_exported, false);
2846 }
2847
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002848 String str = sa.getNonConfigurationString(
2849 com.android.internal.R.styleable.AndroidManifestService_permission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 if (str == null) {
2851 s.info.permission = owner.applicationInfo.permission;
2852 } else {
2853 s.info.permission = str.length() > 0 ? str.toString().intern() : null;
2854 }
2855
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002856 s.info.flags = 0;
2857 if (sa.getBoolean(
2858 com.android.internal.R.styleable.AndroidManifestService_stopWithTask,
2859 false)) {
2860 s.info.flags |= ServiceInfo.FLAG_STOP_WITH_TASK;
2861 }
Dianne Hackborna0c283e2012-02-09 10:47:01 -08002862 if (sa.getBoolean(
2863 com.android.internal.R.styleable.AndroidManifestService_isolatedProcess,
2864 false)) {
2865 s.info.flags |= ServiceInfo.FLAG_ISOLATED_PROCESS;
2866 }
Dianne Hackbornb4163a62012-08-02 18:31:26 -07002867 if (sa.getBoolean(
2868 com.android.internal.R.styleable.AndroidManifestService_singleUser,
2869 false)) {
2870 s.info.flags |= ServiceInfo.FLAG_SINGLE_USER;
2871 if (s.info.exported) {
2872 Slog.w(TAG, "Service exported request ignored due to singleUser: "
2873 + s.className + " at " + mArchiveSourcePath + " "
2874 + parser.getPositionDescription());
2875 s.info.exported = false;
2876 }
2877 setExported = true;
2878 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 sa.recycle();
2881
Dianne Hackborn54e570f2010-10-04 18:32:32 -07002882 if ((owner.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07002883 // A heavy-weight application can not have services in its main process
2884 // We can do direct compare because we intern all strings.
2885 if (s.info.processName == owner.packageName) {
2886 outError[0] = "Heavy-weight applications can not have services in main process";
2887 return null;
2888 }
2889 }
2890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 int outerDepth = parser.getDepth();
2892 int type;
2893 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2894 && (type != XmlPullParser.END_TAG
2895 || parser.getDepth() > outerDepth)) {
2896 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2897 continue;
2898 }
2899
2900 if (parser.getName().equals("intent-filter")) {
2901 ServiceIntentInfo intent = new ServiceIntentInfo(s);
2902 if (!parseIntent(res, parser, attrs, flags, intent, outError, false)) {
2903 return null;
2904 }
2905
2906 s.intents.add(intent);
2907 } else if (parser.getName().equals("meta-data")) {
2908 if ((s.metaData=parseMetaData(res, parser, attrs, s.metaData,
2909 outError)) == null) {
2910 return null;
2911 }
2912 } else {
2913 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002914 Slog.w(TAG, "Unknown element under <service>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002915 + parser.getName() + " at " + mArchiveSourcePath + " "
2916 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 XmlUtils.skipCurrentTag(parser);
2918 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07002919 } else {
2920 outError[0] = "Bad element under <service>: " + parser.getName();
2921 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 }
2924 }
2925
2926 if (!setExported) {
2927 s.info.exported = s.intents.size() > 0;
2928 }
2929
2930 return s;
2931 }
2932
2933 private boolean parseAllMetaData(Resources res,
2934 XmlPullParser parser, AttributeSet attrs, String tag,
2935 Component outInfo, String[] outError)
2936 throws XmlPullParserException, IOException {
2937 int outerDepth = parser.getDepth();
2938 int type;
2939 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2940 && (type != XmlPullParser.END_TAG
2941 || parser.getDepth() > outerDepth)) {
2942 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2943 continue;
2944 }
2945
2946 if (parser.getName().equals("meta-data")) {
2947 if ((outInfo.metaData=parseMetaData(res, parser, attrs,
2948 outInfo.metaData, outError)) == null) {
2949 return false;
2950 }
2951 } else {
2952 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002953 Slog.w(TAG, "Unknown element under " + tag + ": "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002954 + parser.getName() + " at " + mArchiveSourcePath + " "
2955 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 XmlUtils.skipCurrentTag(parser);
2957 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07002958 } else {
2959 outError[0] = "Bad element under " + tag + ": " + parser.getName();
2960 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 }
2963 }
2964 return true;
2965 }
2966
2967 private Bundle parseMetaData(Resources res,
2968 XmlPullParser parser, AttributeSet attrs,
2969 Bundle data, String[] outError)
2970 throws XmlPullParserException, IOException {
2971
2972 TypedArray sa = res.obtainAttributes(attrs,
2973 com.android.internal.R.styleable.AndroidManifestMetaData);
2974
2975 if (data == null) {
2976 data = new Bundle();
2977 }
2978
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002979 String name = sa.getNonConfigurationString(
2980 com.android.internal.R.styleable.AndroidManifestMetaData_name, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 if (name == null) {
2982 outError[0] = "<meta-data> requires an android:name attribute";
2983 sa.recycle();
2984 return null;
2985 }
2986
Dianne Hackborn854060a2009-07-09 18:14:31 -07002987 name = name.intern();
2988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002989 TypedValue v = sa.peekValue(
2990 com.android.internal.R.styleable.AndroidManifestMetaData_resource);
2991 if (v != null && v.resourceId != 0) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002992 //Slog.i(TAG, "Meta data ref " + name + ": " + v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002993 data.putInt(name, v.resourceId);
2994 } else {
2995 v = sa.peekValue(
2996 com.android.internal.R.styleable.AndroidManifestMetaData_value);
Kenny Rootd2d29252011-08-08 11:27:57 -07002997 //Slog.i(TAG, "Meta data " + name + ": " + v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002998 if (v != null) {
2999 if (v.type == TypedValue.TYPE_STRING) {
3000 CharSequence cs = v.coerceToString();
Dianne Hackborn854060a2009-07-09 18:14:31 -07003001 data.putString(name, cs != null ? cs.toString().intern() : null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 } else if (v.type == TypedValue.TYPE_INT_BOOLEAN) {
3003 data.putBoolean(name, v.data != 0);
3004 } else if (v.type >= TypedValue.TYPE_FIRST_INT
3005 && v.type <= TypedValue.TYPE_LAST_INT) {
3006 data.putInt(name, v.data);
3007 } else if (v.type == TypedValue.TYPE_FLOAT) {
3008 data.putFloat(name, v.getFloat());
3009 } else {
3010 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07003011 Slog.w(TAG, "<meta-data> only supports string, integer, float, color, boolean, and resource reference types: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07003012 + parser.getName() + " at " + mArchiveSourcePath + " "
3013 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 } else {
3015 outError[0] = "<meta-data> only supports string, integer, float, color, boolean, and resource reference types";
3016 data = null;
3017 }
3018 }
3019 } else {
3020 outError[0] = "<meta-data> requires an android:value or android:resource attribute";
3021 data = null;
3022 }
3023 }
3024
3025 sa.recycle();
3026
3027 XmlUtils.skipCurrentTag(parser);
3028
3029 return data;
3030 }
3031
Kenny Root05ca4c92011-09-15 10:36:25 -07003032 private static VerifierInfo parseVerifier(Resources res, XmlPullParser parser,
3033 AttributeSet attrs, int flags, String[] outError) throws XmlPullParserException,
3034 IOException {
3035 final TypedArray sa = res.obtainAttributes(attrs,
3036 com.android.internal.R.styleable.AndroidManifestPackageVerifier);
3037
3038 final String packageName = sa.getNonResourceString(
3039 com.android.internal.R.styleable.AndroidManifestPackageVerifier_name);
3040
3041 final String encodedPublicKey = sa.getNonResourceString(
3042 com.android.internal.R.styleable.AndroidManifestPackageVerifier_publicKey);
3043
3044 sa.recycle();
3045
3046 if (packageName == null || packageName.length() == 0) {
3047 Slog.i(TAG, "verifier package name was null; skipping");
3048 return null;
3049 } else if (encodedPublicKey == null) {
3050 Slog.i(TAG, "verifier " + packageName + " public key was null; skipping");
3051 }
3052
3053 EncodedKeySpec keySpec;
3054 try {
3055 final byte[] encoded = Base64.decode(encodedPublicKey, Base64.DEFAULT);
3056 keySpec = new X509EncodedKeySpec(encoded);
3057 } catch (IllegalArgumentException e) {
3058 Slog.i(TAG, "Could not parse verifier " + packageName + " public key; invalid Base64");
3059 return null;
3060 }
3061
3062 /* First try the key as an RSA key. */
3063 try {
3064 final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
3065 final PublicKey publicKey = keyFactory.generatePublic(keySpec);
3066 return new VerifierInfo(packageName, publicKey);
3067 } catch (NoSuchAlgorithmException e) {
3068 Log.wtf(TAG, "Could not parse public key because RSA isn't included in build");
3069 return null;
3070 } catch (InvalidKeySpecException e) {
3071 // Not a RSA public key.
3072 }
3073
3074 /* Now try it as a DSA key. */
3075 try {
3076 final KeyFactory keyFactory = KeyFactory.getInstance("DSA");
3077 final PublicKey publicKey = keyFactory.generatePublic(keySpec);
3078 return new VerifierInfo(packageName, publicKey);
3079 } catch (NoSuchAlgorithmException e) {
3080 Log.wtf(TAG, "Could not parse public key because DSA isn't included in build");
3081 return null;
3082 } catch (InvalidKeySpecException e) {
3083 // Not a DSA public key.
3084 }
3085
3086 return null;
3087 }
3088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003089 private static final String ANDROID_RESOURCES
3090 = "http://schemas.android.com/apk/res/android";
3091
3092 private boolean parseIntent(Resources res,
3093 XmlPullParser parser, AttributeSet attrs, int flags,
3094 IntentInfo outInfo, String[] outError, boolean isActivity)
3095 throws XmlPullParserException, IOException {
3096
3097 TypedArray sa = res.obtainAttributes(attrs,
3098 com.android.internal.R.styleable.AndroidManifestIntentFilter);
3099
3100 int priority = sa.getInt(
3101 com.android.internal.R.styleable.AndroidManifestIntentFilter_priority, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 outInfo.setPriority(priority);
Kenny Root502e9a42011-01-10 13:48:15 -08003103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 TypedValue v = sa.peekValue(
3105 com.android.internal.R.styleable.AndroidManifestIntentFilter_label);
3106 if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
3107 outInfo.nonLocalizedLabel = v.coerceToString();
3108 }
3109
3110 outInfo.icon = sa.getResourceId(
3111 com.android.internal.R.styleable.AndroidManifestIntentFilter_icon, 0);
Adam Powell81cd2e92010-04-21 16:35:18 -07003112
3113 outInfo.logo = sa.getResourceId(
3114 com.android.internal.R.styleable.AndroidManifestIntentFilter_logo, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115
3116 sa.recycle();
3117
3118 int outerDepth = parser.getDepth();
3119 int type;
Kenny Rootd2d29252011-08-08 11:27:57 -07003120 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
3121 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
3122 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003123 continue;
3124 }
3125
3126 String nodeName = parser.getName();
3127 if (nodeName.equals("action")) {
3128 String value = attrs.getAttributeValue(
3129 ANDROID_RESOURCES, "name");
3130 if (value == null || value == "") {
3131 outError[0] = "No value supplied for <android:name>";
3132 return false;
3133 }
3134 XmlUtils.skipCurrentTag(parser);
3135
3136 outInfo.addAction(value);
3137 } else if (nodeName.equals("category")) {
3138 String value = attrs.getAttributeValue(
3139 ANDROID_RESOURCES, "name");
3140 if (value == null || value == "") {
3141 outError[0] = "No value supplied for <android:name>";
3142 return false;
3143 }
3144 XmlUtils.skipCurrentTag(parser);
3145
3146 outInfo.addCategory(value);
3147
3148 } else if (nodeName.equals("data")) {
3149 sa = res.obtainAttributes(attrs,
3150 com.android.internal.R.styleable.AndroidManifestData);
3151
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003152 String str = sa.getNonConfigurationString(
3153 com.android.internal.R.styleable.AndroidManifestData_mimeType, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154 if (str != null) {
3155 try {
3156 outInfo.addDataType(str);
3157 } catch (IntentFilter.MalformedMimeTypeException e) {
3158 outError[0] = e.toString();
3159 sa.recycle();
3160 return false;
3161 }
3162 }
3163
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003164 str = sa.getNonConfigurationString(
3165 com.android.internal.R.styleable.AndroidManifestData_scheme, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003166 if (str != null) {
3167 outInfo.addDataScheme(str);
3168 }
3169
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003170 String host = sa.getNonConfigurationString(
3171 com.android.internal.R.styleable.AndroidManifestData_host, 0);
3172 String port = sa.getNonConfigurationString(
3173 com.android.internal.R.styleable.AndroidManifestData_port, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 if (host != null) {
3175 outInfo.addDataAuthority(host, port);
3176 }
3177
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003178 str = sa.getNonConfigurationString(
3179 com.android.internal.R.styleable.AndroidManifestData_path, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 if (str != null) {
3181 outInfo.addDataPath(str, PatternMatcher.PATTERN_LITERAL);
3182 }
3183
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003184 str = sa.getNonConfigurationString(
3185 com.android.internal.R.styleable.AndroidManifestData_pathPrefix, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 if (str != null) {
3187 outInfo.addDataPath(str, PatternMatcher.PATTERN_PREFIX);
3188 }
3189
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003190 str = sa.getNonConfigurationString(
3191 com.android.internal.R.styleable.AndroidManifestData_pathPattern, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003192 if (str != null) {
3193 outInfo.addDataPath(str, PatternMatcher.PATTERN_SIMPLE_GLOB);
3194 }
3195
3196 sa.recycle();
3197 XmlUtils.skipCurrentTag(parser);
3198 } else if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07003199 Slog.w(TAG, "Unknown element under <intent-filter>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07003200 + parser.getName() + " at " + mArchiveSourcePath + " "
3201 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003202 XmlUtils.skipCurrentTag(parser);
3203 } else {
3204 outError[0] = "Bad element under <intent-filter>: " + parser.getName();
3205 return false;
3206 }
3207 }
3208
3209 outInfo.hasDefault = outInfo.hasCategory(Intent.CATEGORY_DEFAULT);
Kenny Rootd2d29252011-08-08 11:27:57 -07003210
3211 if (DEBUG_PARSER) {
3212 final StringBuilder cats = new StringBuilder("Intent d=");
3213 cats.append(outInfo.hasDefault);
3214 cats.append(", cat=");
3215
3216 final Iterator<String> it = outInfo.categoriesIterator();
3217 if (it != null) {
3218 while (it.hasNext()) {
3219 cats.append(' ');
3220 cats.append(it.next());
3221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222 }
Kenny Rootd2d29252011-08-08 11:27:57 -07003223 Slog.d(TAG, cats.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 }
3225
3226 return true;
3227 }
3228
3229 public final static class Package {
Amith Yamasani0ac1fc92013-03-27 18:56:08 -07003230
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003231 public String packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232
3233 // For now we only support one application per package.
3234 public final ApplicationInfo applicationInfo = new ApplicationInfo();
3235
3236 public final ArrayList<Permission> permissions = new ArrayList<Permission>(0);
3237 public final ArrayList<PermissionGroup> permissionGroups = new ArrayList<PermissionGroup>(0);
3238 public final ArrayList<Activity> activities = new ArrayList<Activity>(0);
3239 public final ArrayList<Activity> receivers = new ArrayList<Activity>(0);
3240 public final ArrayList<Provider> providers = new ArrayList<Provider>(0);
3241 public final ArrayList<Service> services = new ArrayList<Service>(0);
3242 public final ArrayList<Instrumentation> instrumentation = new ArrayList<Instrumentation>(0);
3243
3244 public final ArrayList<String> requestedPermissions = new ArrayList<String>();
Dianne Hackborne639da72012-02-21 15:11:13 -08003245 public final ArrayList<Boolean> requestedPermissionsRequired = new ArrayList<Boolean>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003246
Dianne Hackborn854060a2009-07-09 18:14:31 -07003247 public ArrayList<String> protectedBroadcasts;
Dianne Hackbornc895be72013-03-11 17:48:43 -07003248
3249 public ArrayList<String> libraryNames = null;
Dianne Hackborn49237342009-08-27 20:08:01 -07003250 public ArrayList<String> usesLibraries = null;
3251 public ArrayList<String> usesOptionalLibraries = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 public String[] usesLibraryFiles = null;
3253
Dianne Hackbornc1552392010-03-03 16:19:01 -08003254 public ArrayList<String> mOriginalPackages = null;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003255 public String mRealPackage = null;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08003256 public ArrayList<String> mAdoptPermissions = null;
3257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 // We store the application meta-data independently to avoid multiple unwanted references
3259 public Bundle mAppMetaData = null;
3260
3261 // If this is a 3rd party app, this is the path of the zip file.
3262 public String mPath;
3263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264 // The version code declared for this package.
3265 public int mVersionCode;
3266
3267 // The version name declared for this package.
3268 public String mVersionName;
3269
3270 // The shared user id that this package wants to use.
3271 public String mSharedUserId;
3272
3273 // The shared user label that this package wants to use.
3274 public int mSharedUserLabel;
3275
3276 // Signatures that were read from the package.
3277 public Signature mSignatures[];
3278
3279 // For use by package manager service for quick lookup of
3280 // preferred up order.
3281 public int mPreferredOrder = 0;
3282
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07003283 // For use by the package manager to keep track of the path to the
3284 // file an app came from.
3285 public String mScanPath;
3286
3287 // For use by package manager to keep track of where it has done dexopt.
3288 public boolean mDidDexOpt;
3289
Amith Yamasani13593602012-03-22 16:16:17 -07003290 // // User set enabled state.
3291 // public int mSetEnabled = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
3292 //
3293 // // Whether the package has been stopped.
3294 // public boolean mSetStopped = false;
Dianne Hackborne7f97212011-02-24 14:40:20 -08003295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003296 // Additional data supplied by callers.
3297 public Object mExtras;
Kenny Rootdeb11262010-08-02 11:36:21 -07003298
3299 // Whether an operation is currently pending on this package
3300 public boolean mOperationPending;
3301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 /*
3303 * Applications hardware preferences
3304 */
3305 public final ArrayList<ConfigurationInfo> configPreferences =
3306 new ArrayList<ConfigurationInfo>();
3307
Dianne Hackborn49237342009-08-27 20:08:01 -07003308 /*
3309 * Applications requested features
3310 */
3311 public ArrayList<FeatureInfo> reqFeatures = null;
3312
Suchi Amalapurapu117818e2010-02-09 03:45:40 -08003313 public int installLocation;
3314
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08003315 /* An app that's required for all users and cannot be uninstalled for a user */
3316 public boolean mRequiredForAllUsers;
3317
Amith Yamasani0ac1fc92013-03-27 18:56:08 -07003318 /* The restricted account authenticator type that is used by this application */
3319 public String mRestrictedAccountType;
3320
Kenny Rootbcc954d2011-08-08 16:19:08 -07003321 /**
3322 * Digest suitable for comparing whether this package's manifest is the
3323 * same as another.
3324 */
3325 public ManifestDigest manifestDigest;
3326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 public Package(String _name) {
3328 packageName = _name;
3329 applicationInfo.packageName = _name;
3330 applicationInfo.uid = -1;
3331 }
3332
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003333 public void setPackageName(String newName) {
3334 packageName = newName;
3335 applicationInfo.packageName = newName;
3336 for (int i=permissions.size()-1; i>=0; i--) {
3337 permissions.get(i).setPackageName(newName);
3338 }
3339 for (int i=permissionGroups.size()-1; i>=0; i--) {
3340 permissionGroups.get(i).setPackageName(newName);
3341 }
3342 for (int i=activities.size()-1; i>=0; i--) {
3343 activities.get(i).setPackageName(newName);
3344 }
3345 for (int i=receivers.size()-1; i>=0; i--) {
3346 receivers.get(i).setPackageName(newName);
3347 }
3348 for (int i=providers.size()-1; i>=0; i--) {
3349 providers.get(i).setPackageName(newName);
3350 }
3351 for (int i=services.size()-1; i>=0; i--) {
3352 services.get(i).setPackageName(newName);
3353 }
3354 for (int i=instrumentation.size()-1; i>=0; i--) {
3355 instrumentation.get(i).setPackageName(newName);
3356 }
3357 }
Dianne Hackborn65696252012-03-05 18:49:21 -08003358
3359 public boolean hasComponentClassName(String name) {
3360 for (int i=activities.size()-1; i>=0; i--) {
3361 if (name.equals(activities.get(i).className)) {
3362 return true;
3363 }
3364 }
3365 for (int i=receivers.size()-1; i>=0; i--) {
3366 if (name.equals(receivers.get(i).className)) {
3367 return true;
3368 }
3369 }
3370 for (int i=providers.size()-1; i>=0; i--) {
3371 if (name.equals(providers.get(i).className)) {
3372 return true;
3373 }
3374 }
3375 for (int i=services.size()-1; i>=0; i--) {
3376 if (name.equals(services.get(i).className)) {
3377 return true;
3378 }
3379 }
3380 for (int i=instrumentation.size()-1; i>=0; i--) {
3381 if (name.equals(instrumentation.get(i).className)) {
3382 return true;
3383 }
3384 }
3385 return false;
3386 }
3387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003388 public String toString() {
3389 return "Package{"
3390 + Integer.toHexString(System.identityHashCode(this))
3391 + " " + packageName + "}";
3392 }
3393 }
3394
3395 public static class Component<II extends IntentInfo> {
3396 public final Package owner;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003397 public final ArrayList<II> intents;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003398 public final String className;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003399 public Bundle metaData;
3400
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003401 ComponentName componentName;
3402 String componentShortName;
3403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 public Component(Package _owner) {
3405 owner = _owner;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003406 intents = null;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003407 className = null;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003408 }
3409
3410 public Component(final ParsePackageItemArgs args, final PackageItemInfo outInfo) {
3411 owner = args.owner;
3412 intents = new ArrayList<II>(0);
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003413 String name = args.sa.getNonConfigurationString(args.nameRes, 0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003414 if (name == null) {
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003415 className = null;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003416 args.outError[0] = args.tag + " does not specify android:name";
3417 return;
3418 }
3419
3420 outInfo.name
3421 = buildClassName(owner.applicationInfo.packageName, name, args.outError);
3422 if (outInfo.name == null) {
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003423 className = null;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003424 args.outError[0] = args.tag + " does not have valid android:name";
3425 return;
3426 }
3427
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003428 className = outInfo.name;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003429
3430 int iconVal = args.sa.getResourceId(args.iconRes, 0);
3431 if (iconVal != 0) {
3432 outInfo.icon = iconVal;
3433 outInfo.nonLocalizedLabel = null;
3434 }
Adam Powell81cd2e92010-04-21 16:35:18 -07003435
3436 int logoVal = args.sa.getResourceId(args.logoRes, 0);
3437 if (logoVal != 0) {
3438 outInfo.logo = logoVal;
3439 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003440
3441 TypedValue v = args.sa.peekValue(args.labelRes);
3442 if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
3443 outInfo.nonLocalizedLabel = v.coerceToString();
3444 }
3445
3446 outInfo.packageName = owner.packageName;
3447 }
3448
3449 public Component(final ParseComponentArgs args, final ComponentInfo outInfo) {
3450 this(args, (PackageItemInfo)outInfo);
3451 if (args.outError[0] != null) {
3452 return;
3453 }
3454
3455 if (args.processRes != 0) {
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07003456 CharSequence pname;
3457 if (owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.FROYO) {
3458 pname = args.sa.getNonConfigurationString(args.processRes, 0);
3459 } else {
3460 // Some older apps have been seen to use a resource reference
3461 // here that on older builds was ignored (with a warning). We
3462 // need to continue to do this for them so they don't break.
3463 pname = args.sa.getNonResourceString(args.processRes);
3464 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003465 outInfo.processName = buildProcessName(owner.applicationInfo.packageName,
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07003466 owner.applicationInfo.processName, pname,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003467 args.flags, args.sepProcesses, args.outError);
3468 }
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08003469
3470 if (args.descriptionRes != 0) {
3471 outInfo.descriptionRes = args.sa.getResourceId(args.descriptionRes, 0);
3472 }
3473
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003474 outInfo.enabled = args.sa.getBoolean(args.enabledRes, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003475 }
3476
3477 public Component(Component<II> clone) {
3478 owner = clone.owner;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003479 intents = clone.intents;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003480 className = clone.className;
3481 componentName = clone.componentName;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003482 componentShortName = clone.componentShortName;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003483 }
3484
3485 public ComponentName getComponentName() {
3486 if (componentName != null) {
3487 return componentName;
3488 }
3489 if (className != null) {
3490 componentName = new ComponentName(owner.applicationInfo.packageName,
3491 className);
3492 }
3493 return componentName;
3494 }
3495
3496 public String getComponentShortName() {
3497 if (componentShortName != null) {
3498 return componentShortName;
3499 }
3500 ComponentName component = getComponentName();
3501 if (component != null) {
3502 componentShortName = component.flattenToShortString();
3503 }
3504 return componentShortName;
3505 }
3506
3507 public void setPackageName(String packageName) {
3508 componentName = null;
3509 componentShortName = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003510 }
3511 }
3512
3513 public final static class Permission extends Component<IntentInfo> {
3514 public final PermissionInfo info;
3515 public boolean tree;
3516 public PermissionGroup group;
3517
3518 public Permission(Package _owner) {
3519 super(_owner);
3520 info = new PermissionInfo();
3521 }
3522
3523 public Permission(Package _owner, PermissionInfo _info) {
3524 super(_owner);
3525 info = _info;
3526 }
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003527
3528 public void setPackageName(String packageName) {
3529 super.setPackageName(packageName);
3530 info.packageName = packageName;
3531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003532
3533 public String toString() {
3534 return "Permission{"
3535 + Integer.toHexString(System.identityHashCode(this))
3536 + " " + info.name + "}";
3537 }
3538 }
3539
3540 public final static class PermissionGroup extends Component<IntentInfo> {
3541 public final PermissionGroupInfo info;
3542
3543 public PermissionGroup(Package _owner) {
3544 super(_owner);
3545 info = new PermissionGroupInfo();
3546 }
3547
3548 public PermissionGroup(Package _owner, PermissionGroupInfo _info) {
3549 super(_owner);
3550 info = _info;
3551 }
3552
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003553 public void setPackageName(String packageName) {
3554 super.setPackageName(packageName);
3555 info.packageName = packageName;
3556 }
3557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003558 public String toString() {
3559 return "PermissionGroup{"
3560 + Integer.toHexString(System.identityHashCode(this))
3561 + " " + info.name + "}";
3562 }
3563 }
3564
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003565 private static boolean copyNeeded(int flags, Package p,
3566 PackageUserState state, Bundle metaData, int userId) {
3567 if (userId != 0) {
3568 // We always need to copy for other users, since we need
3569 // to fix up the uid.
3570 return true;
3571 }
3572 if (state.enabled != PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
3573 boolean enabled = state.enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
Dianne Hackborn46730fc2010-07-24 16:32:42 -07003574 if (p.applicationInfo.enabled != enabled) {
3575 return true;
3576 }
3577 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003578 if (!state.installed) {
3579 return true;
3580 }
3581 if (state.stopped) {
3582 return true;
3583 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003584 if ((flags & PackageManager.GET_META_DATA) != 0
3585 && (metaData != null || p.mAppMetaData != null)) {
3586 return true;
3587 }
3588 if ((flags & PackageManager.GET_SHARED_LIBRARY_FILES) != 0
3589 && p.usesLibraryFiles != null) {
3590 return true;
3591 }
3592 return false;
3593 }
3594
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003595 public static ApplicationInfo generateApplicationInfo(Package p, int flags,
3596 PackageUserState state) {
3597 return generateApplicationInfo(p, flags, state, UserHandle.getCallingUserId());
Amith Yamasani742a6712011-05-04 14:49:28 -07003598 }
3599
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003600 private static void updateApplicationInfo(ApplicationInfo ai, int flags,
3601 PackageUserState state) {
3602 // CompatibilityMode is global state.
3603 if (!sCompatibilityModeEnabled) {
3604 ai.disableCompatibilityMode();
3605 }
3606 if (state.installed) {
3607 ai.flags |= ApplicationInfo.FLAG_INSTALLED;
3608 } else {
3609 ai.flags &= ~ApplicationInfo.FLAG_INSTALLED;
3610 }
3611 if (state.enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
3612 ai.enabled = true;
3613 } else if (state.enabled == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
3614 ai.enabled = (flags&PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS) != 0;
3615 } else if (state.enabled == PackageManager.COMPONENT_ENABLED_STATE_DISABLED
3616 || state.enabled == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER) {
3617 ai.enabled = false;
3618 }
3619 ai.enabledSetting = state.enabled;
3620 }
3621
Amith Yamasani13593602012-03-22 16:16:17 -07003622 public static ApplicationInfo generateApplicationInfo(Package p, int flags,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003623 PackageUserState state, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003624 if (p == null) return null;
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003625 if (!checkUseInstalled(flags, state)) {
3626 return null;
3627 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003628 if (!copyNeeded(flags, p, state, null, userId)
3629 && ((flags&PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS) == 0
3630 || state.enabled != PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED)) {
3631 // In this case it is safe to directly modify the internal ApplicationInfo state:
3632 // - CompatibilityMode is global state, so will be the same for every call.
3633 // - We only come in to here if the app should reported as installed; this is the
3634 // default state, and we will do a copy otherwise.
3635 // - The enable state will always be reported the same for the application across
3636 // calls; the only exception is for the UNTIL_USED mode, and in that case we will
3637 // be doing a copy.
3638 updateApplicationInfo(p.applicationInfo, flags, state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003639 return p.applicationInfo;
3640 }
3641
3642 // Make shallow copy so we can store the metadata/libraries safely
3643 ApplicationInfo ai = new ApplicationInfo(p.applicationInfo);
Amith Yamasani742a6712011-05-04 14:49:28 -07003644 if (userId != 0) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07003645 ai.uid = UserHandle.getUid(userId, ai.uid);
Amith Yamasani742a6712011-05-04 14:49:28 -07003646 ai.dataDir = PackageManager.getDataDirForUser(userId, ai.packageName);
3647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003648 if ((flags & PackageManager.GET_META_DATA) != 0) {
3649 ai.metaData = p.mAppMetaData;
3650 }
3651 if ((flags & PackageManager.GET_SHARED_LIBRARY_FILES) != 0) {
3652 ai.sharedLibraryFiles = p.usesLibraryFiles;
3653 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003654 if (state.stopped) {
Amith Yamasania4a54e22012-04-16 15:44:19 -07003655 ai.flags |= ApplicationInfo.FLAG_STOPPED;
Dianne Hackborne7f97212011-02-24 14:40:20 -08003656 } else {
Amith Yamasania4a54e22012-04-16 15:44:19 -07003657 ai.flags &= ~ApplicationInfo.FLAG_STOPPED;
Dianne Hackborne7f97212011-02-24 14:40:20 -08003658 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003659 updateApplicationInfo(ai, flags, state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003660 return ai;
3661 }
3662
3663 public static final PermissionInfo generatePermissionInfo(
3664 Permission p, int flags) {
3665 if (p == null) return null;
3666 if ((flags&PackageManager.GET_META_DATA) == 0) {
3667 return p.info;
3668 }
3669 PermissionInfo pi = new PermissionInfo(p.info);
3670 pi.metaData = p.metaData;
3671 return pi;
3672 }
3673
3674 public static final PermissionGroupInfo generatePermissionGroupInfo(
3675 PermissionGroup pg, int flags) {
3676 if (pg == null) return null;
3677 if ((flags&PackageManager.GET_META_DATA) == 0) {
3678 return pg.info;
3679 }
3680 PermissionGroupInfo pgi = new PermissionGroupInfo(pg.info);
3681 pgi.metaData = pg.metaData;
3682 return pgi;
3683 }
3684
3685 public final static class Activity extends Component<ActivityIntentInfo> {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003686 public final ActivityInfo info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003688 public Activity(final ParseComponentArgs args, final ActivityInfo _info) {
3689 super(args, _info);
3690 info = _info;
3691 info.applicationInfo = args.owner.applicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003692 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003693
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003694 public void setPackageName(String packageName) {
3695 super.setPackageName(packageName);
3696 info.packageName = packageName;
3697 }
3698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003699 public String toString() {
3700 return "Activity{"
3701 + Integer.toHexString(System.identityHashCode(this))
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003702 + " " + getComponentShortName() + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003703 }
3704 }
3705
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003706 public static final ActivityInfo generateActivityInfo(Activity a, int flags,
3707 PackageUserState state, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003708 if (a == null) return null;
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003709 if (!checkUseInstalled(flags, state)) {
3710 return null;
3711 }
3712 if (!copyNeeded(flags, a.owner, state, a.metaData, userId)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003713 return a.info;
3714 }
3715 // Make shallow copies so we can store the metadata safely
3716 ActivityInfo ai = new ActivityInfo(a.info);
3717 ai.metaData = a.metaData;
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003718 ai.applicationInfo = generateApplicationInfo(a.owner, flags, state, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003719 return ai;
3720 }
3721
3722 public final static class Service extends Component<ServiceIntentInfo> {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003723 public final ServiceInfo info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003724
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003725 public Service(final ParseComponentArgs args, final ServiceInfo _info) {
3726 super(args, _info);
3727 info = _info;
3728 info.applicationInfo = args.owner.applicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003730
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003731 public void setPackageName(String packageName) {
3732 super.setPackageName(packageName);
3733 info.packageName = packageName;
3734 }
3735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003736 public String toString() {
3737 return "Service{"
3738 + Integer.toHexString(System.identityHashCode(this))
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003739 + " " + getComponentShortName() + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003740 }
3741 }
3742
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003743 public static final ServiceInfo generateServiceInfo(Service s, int flags,
3744 PackageUserState state, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003745 if (s == null) return null;
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003746 if (!checkUseInstalled(flags, state)) {
3747 return null;
3748 }
3749 if (!copyNeeded(flags, s.owner, state, s.metaData, userId)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003750 return s.info;
3751 }
3752 // Make shallow copies so we can store the metadata safely
3753 ServiceInfo si = new ServiceInfo(s.info);
3754 si.metaData = s.metaData;
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003755 si.applicationInfo = generateApplicationInfo(s.owner, flags, state, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003756 return si;
3757 }
3758
3759 public final static class Provider extends Component {
3760 public final ProviderInfo info;
3761 public boolean syncable;
3762
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003763 public Provider(final ParseComponentArgs args, final ProviderInfo _info) {
3764 super(args, _info);
3765 info = _info;
3766 info.applicationInfo = args.owner.applicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003767 syncable = false;
3768 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003770 public Provider(Provider existingProvider) {
3771 super(existingProvider);
3772 this.info = existingProvider.info;
3773 this.syncable = existingProvider.syncable;
3774 }
3775
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003776 public void setPackageName(String packageName) {
3777 super.setPackageName(packageName);
3778 info.packageName = packageName;
3779 }
3780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003781 public String toString() {
3782 return "Provider{"
3783 + Integer.toHexString(System.identityHashCode(this))
3784 + " " + info.name + "}";
3785 }
3786 }
3787
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003788 public static final ProviderInfo generateProviderInfo(Provider p, int flags,
3789 PackageUserState state, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003790 if (p == null) return null;
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003791 if (!checkUseInstalled(flags, state)) {
3792 return null;
3793 }
3794 if (!copyNeeded(flags, p.owner, state, p.metaData, userId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003795 && ((flags & PackageManager.GET_URI_PERMISSION_PATTERNS) != 0
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003796 || p.info.uriPermissionPatterns == null)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003797 return p.info;
3798 }
3799 // Make shallow copies so we can store the metadata safely
3800 ProviderInfo pi = new ProviderInfo(p.info);
3801 pi.metaData = p.metaData;
3802 if ((flags & PackageManager.GET_URI_PERMISSION_PATTERNS) == 0) {
3803 pi.uriPermissionPatterns = null;
3804 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003805 pi.applicationInfo = generateApplicationInfo(p.owner, flags, state, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003806 return pi;
3807 }
3808
3809 public final static class Instrumentation extends Component {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003810 public final InstrumentationInfo info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003811
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003812 public Instrumentation(final ParsePackageItemArgs args, final InstrumentationInfo _info) {
3813 super(args, _info);
3814 info = _info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003815 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003816
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003817 public void setPackageName(String packageName) {
3818 super.setPackageName(packageName);
3819 info.packageName = packageName;
3820 }
3821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003822 public String toString() {
3823 return "Instrumentation{"
3824 + Integer.toHexString(System.identityHashCode(this))
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003825 + " " + getComponentShortName() + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003826 }
3827 }
3828
3829 public static final InstrumentationInfo generateInstrumentationInfo(
3830 Instrumentation i, int flags) {
3831 if (i == null) return null;
3832 if ((flags&PackageManager.GET_META_DATA) == 0) {
3833 return i.info;
3834 }
3835 InstrumentationInfo ii = new InstrumentationInfo(i.info);
3836 ii.metaData = i.metaData;
3837 return ii;
3838 }
3839
3840 public static class IntentInfo extends IntentFilter {
3841 public boolean hasDefault;
3842 public int labelRes;
3843 public CharSequence nonLocalizedLabel;
3844 public int icon;
Adam Powell81cd2e92010-04-21 16:35:18 -07003845 public int logo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003846 }
3847
3848 public final static class ActivityIntentInfo extends IntentInfo {
3849 public final Activity activity;
3850
3851 public ActivityIntentInfo(Activity _activity) {
3852 activity = _activity;
3853 }
3854
3855 public String toString() {
3856 return "ActivityIntentInfo{"
3857 + Integer.toHexString(System.identityHashCode(this))
3858 + " " + activity.info.name + "}";
3859 }
3860 }
3861
3862 public final static class ServiceIntentInfo extends IntentInfo {
3863 public final Service service;
3864
3865 public ServiceIntentInfo(Service _service) {
3866 service = _service;
3867 }
3868
3869 public String toString() {
3870 return "ServiceIntentInfo{"
3871 + Integer.toHexString(System.identityHashCode(this))
3872 + " " + service.info.name + "}";
3873 }
3874 }
Mitsuru Oshima69fff4a2009-07-21 09:51:05 -07003875
3876 /**
3877 * @hide
3878 */
3879 public static void setCompatibilityModeEnabled(boolean compatibilityModeEnabled) {
3880 sCompatibilityModeEnabled = compatibilityModeEnabled;
3881 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003882}