blob: 384aed89ff3e0cb8e73e7a133fa96e29d68a67a0 [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")) {
Nick Kralevich73f2d3c2013-04-04 14:38:13 -07001039 if (!parseUsesPermission(pkg, res, parser, attrs, outError)) {
1040 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 }
1042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 } else if (tagName.equals("uses-configuration")) {
1044 ConfigurationInfo cPref = new ConfigurationInfo();
1045 sa = res.obtainAttributes(attrs,
1046 com.android.internal.R.styleable.AndroidManifestUsesConfiguration);
1047 cPref.reqTouchScreen = sa.getInt(
1048 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqTouchScreen,
1049 Configuration.TOUCHSCREEN_UNDEFINED);
1050 cPref.reqKeyboardType = sa.getInt(
1051 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqKeyboardType,
1052 Configuration.KEYBOARD_UNDEFINED);
1053 if (sa.getBoolean(
1054 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqHardKeyboard,
1055 false)) {
1056 cPref.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD;
1057 }
1058 cPref.reqNavigation = sa.getInt(
1059 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqNavigation,
1060 Configuration.NAVIGATION_UNDEFINED);
1061 if (sa.getBoolean(
1062 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqFiveWayNav,
1063 false)) {
1064 cPref.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV;
1065 }
1066 sa.recycle();
1067 pkg.configPreferences.add(cPref);
1068
1069 XmlUtils.skipCurrentTag(parser);
1070
Suchi Amalapurapud299b812009-06-05 10:26:19 -07001071 } else if (tagName.equals("uses-feature")) {
Dianne Hackborn49237342009-08-27 20:08:01 -07001072 FeatureInfo fi = new FeatureInfo();
Suchi Amalapurapud299b812009-06-05 10:26:19 -07001073 sa = res.obtainAttributes(attrs,
1074 com.android.internal.R.styleable.AndroidManifestUsesFeature);
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001075 // Note: don't allow this value to be a reference to a resource
1076 // that may change.
Dianne Hackborn49237342009-08-27 20:08:01 -07001077 fi.name = sa.getNonResourceString(
1078 com.android.internal.R.styleable.AndroidManifestUsesFeature_name);
1079 if (fi.name == null) {
1080 fi.reqGlEsVersion = sa.getInt(
1081 com.android.internal.R.styleable.AndroidManifestUsesFeature_glEsVersion,
1082 FeatureInfo.GL_ES_VERSION_UNDEFINED);
1083 }
1084 if (sa.getBoolean(
1085 com.android.internal.R.styleable.AndroidManifestUsesFeature_required,
1086 true)) {
1087 fi.flags |= FeatureInfo.FLAG_REQUIRED;
1088 }
Suchi Amalapurapud299b812009-06-05 10:26:19 -07001089 sa.recycle();
Dianne Hackborn49237342009-08-27 20:08:01 -07001090 if (pkg.reqFeatures == null) {
1091 pkg.reqFeatures = new ArrayList<FeatureInfo>();
1092 }
1093 pkg.reqFeatures.add(fi);
1094
1095 if (fi.name == null) {
1096 ConfigurationInfo cPref = new ConfigurationInfo();
1097 cPref.reqGlEsVersion = fi.reqGlEsVersion;
1098 pkg.configPreferences.add(cPref);
1099 }
Suchi Amalapurapud299b812009-06-05 10:26:19 -07001100
1101 XmlUtils.skipCurrentTag(parser);
1102
Dianne Hackborn851a5412009-05-08 12:06:44 -07001103 } else if (tagName.equals("uses-sdk")) {
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001104 if (SDK_VERSION > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 sa = res.obtainAttributes(attrs,
1106 com.android.internal.R.styleable.AndroidManifestUsesSdk);
1107
Dianne Hackborn851a5412009-05-08 12:06:44 -07001108 int minVers = 0;
1109 String minCode = null;
1110 int targetVers = 0;
1111 String targetCode = null;
1112
1113 TypedValue val = sa.peekValue(
1114 com.android.internal.R.styleable.AndroidManifestUsesSdk_minSdkVersion);
1115 if (val != null) {
1116 if (val.type == TypedValue.TYPE_STRING && val.string != null) {
1117 targetCode = minCode = val.string.toString();
1118 } else {
1119 // If it's not a string, it's an integer.
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001120 targetVers = minVers = val.data;
Dianne Hackborn851a5412009-05-08 12:06:44 -07001121 }
1122 }
1123
1124 val = sa.peekValue(
1125 com.android.internal.R.styleable.AndroidManifestUsesSdk_targetSdkVersion);
1126 if (val != null) {
1127 if (val.type == TypedValue.TYPE_STRING && val.string != null) {
1128 targetCode = minCode = val.string.toString();
1129 } else {
1130 // If it's not a string, it's an integer.
1131 targetVers = val.data;
1132 }
1133 }
1134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 sa.recycle();
1136
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001137 if (minCode != null) {
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001138 if (!minCode.equals(SDK_CODENAME)) {
1139 if (SDK_CODENAME != null) {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001140 outError[0] = "Requires development platform " + minCode
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001141 + " (current platform is " + SDK_CODENAME + ")";
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001142 } else {
1143 outError[0] = "Requires development platform " + minCode
1144 + " but this is a release platform.";
1145 }
1146 mParseError = PackageManager.INSTALL_FAILED_OLDER_SDK;
1147 return null;
1148 }
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001149 } else if (minVers > SDK_VERSION) {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001150 outError[0] = "Requires newer sdk version #" + minVers
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001151 + " (current version is #" + SDK_VERSION + ")";
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001152 mParseError = PackageManager.INSTALL_FAILED_OLDER_SDK;
1153 return null;
1154 }
1155
Dianne Hackborn851a5412009-05-08 12:06:44 -07001156 if (targetCode != null) {
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001157 if (!targetCode.equals(SDK_CODENAME)) {
1158 if (SDK_CODENAME != null) {
Dianne Hackborn851a5412009-05-08 12:06:44 -07001159 outError[0] = "Requires development platform " + targetCode
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001160 + " (current platform is " + SDK_CODENAME + ")";
Dianne Hackborn851a5412009-05-08 12:06:44 -07001161 } else {
1162 outError[0] = "Requires development platform " + targetCode
1163 + " but this is a release platform.";
1164 }
1165 mParseError = PackageManager.INSTALL_FAILED_OLDER_SDK;
1166 return null;
1167 }
1168 // If the code matches, it definitely targets this SDK.
Dianne Hackborna96cbb42009-05-13 15:06:13 -07001169 pkg.applicationInfo.targetSdkVersion
1170 = android.os.Build.VERSION_CODES.CUR_DEVELOPMENT;
1171 } else {
1172 pkg.applicationInfo.targetSdkVersion = targetVers;
Dianne Hackborn851a5412009-05-08 12:06:44 -07001173 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 }
1175
1176 XmlUtils.skipCurrentTag(parser);
1177
Dianne Hackborn723738c2009-06-25 19:48:04 -07001178 } else if (tagName.equals("supports-screens")) {
1179 sa = res.obtainAttributes(attrs,
1180 com.android.internal.R.styleable.AndroidManifestSupportsScreens);
1181
Dianne Hackborndf6e9802011-05-26 14:20:23 -07001182 pkg.applicationInfo.requiresSmallestWidthDp = sa.getInteger(
1183 com.android.internal.R.styleable.AndroidManifestSupportsScreens_requiresSmallestWidthDp,
1184 0);
1185 pkg.applicationInfo.compatibleWidthLimitDp = sa.getInteger(
1186 com.android.internal.R.styleable.AndroidManifestSupportsScreens_compatibleWidthLimitDp,
1187 0);
Dianne Hackborn2762ff32011-06-01 21:27:05 -07001188 pkg.applicationInfo.largestWidthLimitDp = sa.getInteger(
1189 com.android.internal.R.styleable.AndroidManifestSupportsScreens_largestWidthLimitDp,
1190 0);
Dianne Hackborndf6e9802011-05-26 14:20:23 -07001191
Dianne Hackborn723738c2009-06-25 19:48:04 -07001192 // This is a trick to get a boolean and still able to detect
1193 // if a value was actually set.
1194 supportsSmallScreens = sa.getInteger(
1195 com.android.internal.R.styleable.AndroidManifestSupportsScreens_smallScreens,
1196 supportsSmallScreens);
1197 supportsNormalScreens = sa.getInteger(
1198 com.android.internal.R.styleable.AndroidManifestSupportsScreens_normalScreens,
1199 supportsNormalScreens);
1200 supportsLargeScreens = sa.getInteger(
1201 com.android.internal.R.styleable.AndroidManifestSupportsScreens_largeScreens,
1202 supportsLargeScreens);
Dianne Hackborn14cee9f2010-04-23 17:51:26 -07001203 supportsXLargeScreens = sa.getInteger(
1204 com.android.internal.R.styleable.AndroidManifestSupportsScreens_xlargeScreens,
1205 supportsXLargeScreens);
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001206 resizeable = sa.getInteger(
1207 com.android.internal.R.styleable.AndroidManifestSupportsScreens_resizeable,
Dianne Hackborn14cee9f2010-04-23 17:51:26 -07001208 resizeable);
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001209 anyDensity = sa.getInteger(
1210 com.android.internal.R.styleable.AndroidManifestSupportsScreens_anyDensity,
1211 anyDensity);
Dianne Hackborn723738c2009-06-25 19:48:04 -07001212
1213 sa.recycle();
1214
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001215 XmlUtils.skipCurrentTag(parser);
Dianne Hackborn854060a2009-07-09 18:14:31 -07001216
1217 } else if (tagName.equals("protected-broadcast")) {
1218 sa = res.obtainAttributes(attrs,
1219 com.android.internal.R.styleable.AndroidManifestProtectedBroadcast);
1220
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001221 // Note: don't allow this value to be a reference to a resource
1222 // that may change.
Dianne Hackborn854060a2009-07-09 18:14:31 -07001223 String name = sa.getNonResourceString(
1224 com.android.internal.R.styleable.AndroidManifestProtectedBroadcast_name);
1225
1226 sa.recycle();
1227
1228 if (name != null && (flags&PARSE_IS_SYSTEM) != 0) {
1229 if (pkg.protectedBroadcasts == null) {
1230 pkg.protectedBroadcasts = new ArrayList<String>();
1231 }
1232 if (!pkg.protectedBroadcasts.contains(name)) {
1233 pkg.protectedBroadcasts.add(name.intern());
1234 }
1235 }
1236
1237 XmlUtils.skipCurrentTag(parser);
1238
1239 } else if (tagName.equals("instrumentation")) {
1240 if (parseInstrumentation(pkg, res, parser, attrs, outError) == null) {
1241 return null;
1242 }
1243
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001244 } else if (tagName.equals("original-package")) {
1245 sa = res.obtainAttributes(attrs,
1246 com.android.internal.R.styleable.AndroidManifestOriginalPackage);
1247
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001248 String orig =sa.getNonConfigurationString(
1249 com.android.internal.R.styleable.AndroidManifestOriginalPackage_name, 0);
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08001250 if (!pkg.packageName.equals(orig)) {
Dianne Hackbornc1552392010-03-03 16:19:01 -08001251 if (pkg.mOriginalPackages == null) {
1252 pkg.mOriginalPackages = new ArrayList<String>();
1253 pkg.mRealPackage = pkg.packageName;
1254 }
1255 pkg.mOriginalPackages.add(orig);
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08001256 }
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001257
1258 sa.recycle();
1259
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001260 XmlUtils.skipCurrentTag(parser);
1261
1262 } else if (tagName.equals("adopt-permissions")) {
1263 sa = res.obtainAttributes(attrs,
1264 com.android.internal.R.styleable.AndroidManifestOriginalPackage);
1265
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001266 String name = sa.getNonConfigurationString(
1267 com.android.internal.R.styleable.AndroidManifestOriginalPackage_name, 0);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001268
1269 sa.recycle();
1270
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08001271 if (name != null) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001272 if (pkg.mAdoptPermissions == null) {
1273 pkg.mAdoptPermissions = new ArrayList<String>();
1274 }
1275 pkg.mAdoptPermissions.add(name);
1276 }
1277
1278 XmlUtils.skipCurrentTag(parser);
1279
Dianne Hackborna0b46c92010-10-21 15:32:06 -07001280 } else if (tagName.equals("uses-gl-texture")) {
1281 // Just skip this tag
1282 XmlUtils.skipCurrentTag(parser);
1283 continue;
1284
1285 } else if (tagName.equals("compatible-screens")) {
1286 // Just skip this tag
1287 XmlUtils.skipCurrentTag(parser);
1288 continue;
1289
Dianne Hackborn854060a2009-07-09 18:14:31 -07001290 } else if (tagName.equals("eat-comment")) {
1291 // Just skip this tag
1292 XmlUtils.skipCurrentTag(parser);
1293 continue;
1294
1295 } else if (RIGID_PARSER) {
1296 outError[0] = "Bad element under <manifest>: "
1297 + parser.getName();
1298 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1299 return null;
1300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -07001302 Slog.w(TAG, "Unknown element under <manifest>: " + parser.getName()
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -07001303 + " at " + mArchiveSourcePath + " "
1304 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 XmlUtils.skipCurrentTag(parser);
1306 continue;
1307 }
1308 }
1309
1310 if (!foundApp && pkg.instrumentation.size() == 0) {
1311 outError[0] = "<manifest> does not contain an <application> or <instrumentation>";
1312 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_EMPTY;
1313 }
1314
Dianne Hackborna96cbb42009-05-13 15:06:13 -07001315 final int NP = PackageParser.NEW_PERMISSIONS.length;
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001316 StringBuilder implicitPerms = null;
Dianne Hackborna96cbb42009-05-13 15:06:13 -07001317 for (int ip=0; ip<NP; ip++) {
1318 final PackageParser.NewPermissionInfo npi
1319 = PackageParser.NEW_PERMISSIONS[ip];
1320 if (pkg.applicationInfo.targetSdkVersion >= npi.sdkVersion) {
1321 break;
1322 }
1323 if (!pkg.requestedPermissions.contains(npi.name)) {
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001324 if (implicitPerms == null) {
1325 implicitPerms = new StringBuilder(128);
1326 implicitPerms.append(pkg.packageName);
1327 implicitPerms.append(": compat added ");
1328 } else {
1329 implicitPerms.append(' ');
1330 }
1331 implicitPerms.append(npi.name);
Dianne Hackborna96cbb42009-05-13 15:06:13 -07001332 pkg.requestedPermissions.add(npi.name);
Dianne Hackborn65696252012-03-05 18:49:21 -08001333 pkg.requestedPermissionsRequired.add(Boolean.TRUE);
Dianne Hackborna96cbb42009-05-13 15:06:13 -07001334 }
Dianne Hackborn851a5412009-05-08 12:06:44 -07001335 }
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001336 if (implicitPerms != null) {
Kenny Rootd2d29252011-08-08 11:27:57 -07001337 Slog.i(TAG, implicitPerms.toString());
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001338 }
Dianne Hackborn79245122012-03-12 10:51:26 -07001339
1340 final int NS = PackageParser.SPLIT_PERMISSIONS.length;
1341 for (int is=0; is<NS; is++) {
1342 final PackageParser.SplitPermissionInfo spi
1343 = PackageParser.SPLIT_PERMISSIONS[is];
Dianne Hackborn31b0e0e2012-04-05 19:33:30 -07001344 if (pkg.applicationInfo.targetSdkVersion >= spi.targetSdk
1345 || !pkg.requestedPermissions.contains(spi.rootPerm)) {
Dianne Hackborn5e4705a2012-04-06 12:55:53 -07001346 continue;
Dianne Hackborn79245122012-03-12 10:51:26 -07001347 }
1348 for (int in=0; in<spi.newPerms.length; in++) {
1349 final String perm = spi.newPerms[in];
1350 if (!pkg.requestedPermissions.contains(perm)) {
1351 pkg.requestedPermissions.add(perm);
1352 pkg.requestedPermissionsRequired.add(Boolean.TRUE);
1353 }
1354 }
1355 }
1356
Dianne Hackborn723738c2009-06-25 19:48:04 -07001357 if (supportsSmallScreens < 0 || (supportsSmallScreens > 0
1358 && pkg.applicationInfo.targetSdkVersion
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001359 >= android.os.Build.VERSION_CODES.DONUT)) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07001360 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_SMALL_SCREENS;
1361 }
1362 if (supportsNormalScreens != 0) {
1363 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_NORMAL_SCREENS;
1364 }
1365 if (supportsLargeScreens < 0 || (supportsLargeScreens > 0
1366 && pkg.applicationInfo.targetSdkVersion
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001367 >= android.os.Build.VERSION_CODES.DONUT)) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07001368 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_LARGE_SCREENS;
1369 }
Dianne Hackborn14cee9f2010-04-23 17:51:26 -07001370 if (supportsXLargeScreens < 0 || (supportsXLargeScreens > 0
1371 && pkg.applicationInfo.targetSdkVersion
1372 >= android.os.Build.VERSION_CODES.GINGERBREAD)) {
1373 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_XLARGE_SCREENS;
1374 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001375 if (resizeable < 0 || (resizeable > 0
1376 && pkg.applicationInfo.targetSdkVersion
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001377 >= android.os.Build.VERSION_CODES.DONUT)) {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001378 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_RESIZEABLE_FOR_SCREENS;
1379 }
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001380 if (anyDensity < 0 || (anyDensity > 0
1381 && pkg.applicationInfo.targetSdkVersion
1382 >= android.os.Build.VERSION_CODES.DONUT)) {
1383 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES;
Mitsuru Oshima8d112672009-04-27 12:01:23 -07001384 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07001385
Nick Kralevich38f130e2013-04-04 13:19:10 -07001386 /*
1387 * b/8528162: Ignore the <uses-permission android:required> attribute if
1388 * targetSdkVersion < JELLY_BEAN_MR2. There are lots of apps in the wild
1389 * which are improperly using this attribute, even though it never worked.
1390 */
1391 if (pkg.applicationInfo.targetSdkVersion < Build.VERSION_CODES.JELLY_BEAN_MR2) {
1392 for (int i = 0; i < pkg.requestedPermissionsRequired.size(); i++) {
1393 pkg.requestedPermissionsRequired.set(i, Boolean.TRUE);
1394 }
1395 }
1396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 return pkg;
1398 }
1399
Nick Kralevich73f2d3c2013-04-04 14:38:13 -07001400 private boolean parseUsesPermission(Package pkg, Resources res, XmlResourceParser parser,
1401 AttributeSet attrs, String[] outError)
1402 throws XmlPullParserException, IOException {
1403 TypedArray sa = res.obtainAttributes(attrs,
1404 com.android.internal.R.styleable.AndroidManifestUsesPermission);
1405
1406 // Note: don't allow this value to be a reference to a resource
1407 // that may change.
1408 String name = sa.getNonResourceString(
1409 com.android.internal.R.styleable.AndroidManifestUsesPermission_name);
1410 boolean required = sa.getBoolean(
1411 com.android.internal.R.styleable.AndroidManifestUsesPermission_required, true);
1412
1413 sa.recycle();
1414
1415 if (name != null) {
1416 int index = pkg.requestedPermissions.indexOf(name);
1417 if (index == -1) {
1418 pkg.requestedPermissions.add(name.intern());
1419 pkg.requestedPermissionsRequired.add(required ? Boolean.TRUE : Boolean.FALSE);
1420 } else {
1421 if (pkg.requestedPermissionsRequired.get(index) != required) {
1422 outError[0] = "conflicting <uses-permission> entries";
1423 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1424 return false;
1425 }
1426 }
1427 }
1428
1429 XmlUtils.skipCurrentTag(parser);
1430 return true;
1431 }
1432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 private static String buildClassName(String pkg, CharSequence clsSeq,
1434 String[] outError) {
1435 if (clsSeq == null || clsSeq.length() <= 0) {
1436 outError[0] = "Empty class name in package " + pkg;
1437 return null;
1438 }
1439 String cls = clsSeq.toString();
1440 char c = cls.charAt(0);
1441 if (c == '.') {
1442 return (pkg + cls).intern();
1443 }
1444 if (cls.indexOf('.') < 0) {
1445 StringBuilder b = new StringBuilder(pkg);
1446 b.append('.');
1447 b.append(cls);
1448 return b.toString().intern();
1449 }
1450 if (c >= 'a' && c <= 'z') {
1451 return cls.intern();
1452 }
1453 outError[0] = "Bad class name " + cls + " in package " + pkg;
1454 return null;
1455 }
1456
1457 private static String buildCompoundName(String pkg,
1458 CharSequence procSeq, String type, String[] outError) {
1459 String proc = procSeq.toString();
1460 char c = proc.charAt(0);
1461 if (pkg != null && c == ':') {
1462 if (proc.length() < 2) {
1463 outError[0] = "Bad " + type + " name " + proc + " in package " + pkg
1464 + ": must be at least two characters";
1465 return null;
1466 }
1467 String subName = proc.substring(1);
1468 String nameError = validateName(subName, false);
1469 if (nameError != null) {
1470 outError[0] = "Invalid " + type + " name " + proc + " in package "
1471 + pkg + ": " + nameError;
1472 return null;
1473 }
1474 return (pkg + proc).intern();
1475 }
1476 String nameError = validateName(proc, true);
1477 if (nameError != null && !"system".equals(proc)) {
1478 outError[0] = "Invalid " + type + " name " + proc + " in package "
1479 + pkg + ": " + nameError;
1480 return null;
1481 }
1482 return proc.intern();
1483 }
1484
1485 private static String buildProcessName(String pkg, String defProc,
1486 CharSequence procSeq, int flags, String[] separateProcesses,
1487 String[] outError) {
1488 if ((flags&PARSE_IGNORE_PROCESSES) != 0 && !"system".equals(procSeq)) {
1489 return defProc != null ? defProc : pkg;
1490 }
1491 if (separateProcesses != null) {
1492 for (int i=separateProcesses.length-1; i>=0; i--) {
1493 String sp = separateProcesses[i];
1494 if (sp.equals(pkg) || sp.equals(defProc) || sp.equals(procSeq)) {
1495 return pkg;
1496 }
1497 }
1498 }
1499 if (procSeq == null || procSeq.length() <= 0) {
1500 return defProc;
1501 }
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07001502 return buildCompoundName(pkg, procSeq, "process", outError);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 }
1504
1505 private static String buildTaskAffinityName(String pkg, String defProc,
1506 CharSequence procSeq, String[] outError) {
1507 if (procSeq == null) {
1508 return defProc;
1509 }
1510 if (procSeq.length() <= 0) {
1511 return null;
1512 }
1513 return buildCompoundName(pkg, procSeq, "taskAffinity", outError);
1514 }
1515
Dianne Hackbornfd5015b2012-04-30 16:33:56 -07001516 private PermissionGroup parsePermissionGroup(Package owner, int flags, Resources res,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 XmlPullParser parser, AttributeSet attrs, String[] outError)
1518 throws XmlPullParserException, IOException {
1519 PermissionGroup perm = new PermissionGroup(owner);
1520
1521 TypedArray sa = res.obtainAttributes(attrs,
1522 com.android.internal.R.styleable.AndroidManifestPermissionGroup);
1523
1524 if (!parsePackageItemInfo(owner, perm.info, outError,
1525 "<permission-group>", sa,
1526 com.android.internal.R.styleable.AndroidManifestPermissionGroup_name,
1527 com.android.internal.R.styleable.AndroidManifestPermissionGroup_label,
Adam Powell81cd2e92010-04-21 16:35:18 -07001528 com.android.internal.R.styleable.AndroidManifestPermissionGroup_icon,
1529 com.android.internal.R.styleable.AndroidManifestPermissionGroup_logo)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 sa.recycle();
1531 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1532 return null;
1533 }
1534
1535 perm.info.descriptionRes = sa.getResourceId(
1536 com.android.internal.R.styleable.AndroidManifestPermissionGroup_description,
1537 0);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -07001538 perm.info.flags = sa.getInt(
1539 com.android.internal.R.styleable.AndroidManifestPermissionGroup_permissionGroupFlags, 0);
Dianne Hackbornfd5015b2012-04-30 16:33:56 -07001540 perm.info.priority = sa.getInt(
1541 com.android.internal.R.styleable.AndroidManifestPermissionGroup_priority, 0);
Dianne Hackborn99222d22012-05-06 16:30:15 -07001542 if (perm.info.priority > 0 && (flags&PARSE_IS_SYSTEM) == 0) {
Dianne Hackbornfd5015b2012-04-30 16:33:56 -07001543 perm.info.priority = 0;
1544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545
1546 sa.recycle();
1547
1548 if (!parseAllMetaData(res, parser, attrs, "<permission-group>", perm,
1549 outError)) {
1550 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1551 return null;
1552 }
1553
1554 owner.permissionGroups.add(perm);
1555
1556 return perm;
1557 }
1558
1559 private Permission parsePermission(Package owner, Resources res,
1560 XmlPullParser parser, AttributeSet attrs, String[] outError)
1561 throws XmlPullParserException, IOException {
1562 Permission perm = new Permission(owner);
1563
1564 TypedArray sa = res.obtainAttributes(attrs,
1565 com.android.internal.R.styleable.AndroidManifestPermission);
1566
1567 if (!parsePackageItemInfo(owner, perm.info, outError,
1568 "<permission>", sa,
1569 com.android.internal.R.styleable.AndroidManifestPermission_name,
1570 com.android.internal.R.styleable.AndroidManifestPermission_label,
Adam Powell81cd2e92010-04-21 16:35:18 -07001571 com.android.internal.R.styleable.AndroidManifestPermission_icon,
1572 com.android.internal.R.styleable.AndroidManifestPermission_logo)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 sa.recycle();
1574 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1575 return null;
1576 }
1577
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001578 // Note: don't allow this value to be a reference to a resource
1579 // that may change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 perm.info.group = sa.getNonResourceString(
1581 com.android.internal.R.styleable.AndroidManifestPermission_permissionGroup);
1582 if (perm.info.group != null) {
1583 perm.info.group = perm.info.group.intern();
1584 }
1585
1586 perm.info.descriptionRes = sa.getResourceId(
1587 com.android.internal.R.styleable.AndroidManifestPermission_description,
1588 0);
1589
1590 perm.info.protectionLevel = sa.getInt(
1591 com.android.internal.R.styleable.AndroidManifestPermission_protectionLevel,
1592 PermissionInfo.PROTECTION_NORMAL);
1593
Dianne Hackborn2ca2c872012-09-16 16:03:36 -07001594 perm.info.flags = sa.getInt(
1595 com.android.internal.R.styleable.AndroidManifestPermission_permissionFlags, 0);
1596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 sa.recycle();
Dianne Hackborne639da72012-02-21 15:11:13 -08001598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 if (perm.info.protectionLevel == -1) {
1600 outError[0] = "<permission> does not specify protectionLevel";
1601 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1602 return null;
1603 }
Dianne Hackborne639da72012-02-21 15:11:13 -08001604
1605 perm.info.protectionLevel = PermissionInfo.fixProtectionLevel(perm.info.protectionLevel);
1606
1607 if ((perm.info.protectionLevel&PermissionInfo.PROTECTION_MASK_FLAGS) != 0) {
1608 if ((perm.info.protectionLevel&PermissionInfo.PROTECTION_MASK_BASE) !=
1609 PermissionInfo.PROTECTION_SIGNATURE) {
1610 outError[0] = "<permission> protectionLevel specifies a flag but is "
1611 + "not based on signature type";
1612 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1613 return null;
1614 }
1615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616
1617 if (!parseAllMetaData(res, parser, attrs, "<permission>", perm,
1618 outError)) {
1619 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1620 return null;
1621 }
1622
1623 owner.permissions.add(perm);
1624
1625 return perm;
1626 }
1627
1628 private Permission parsePermissionTree(Package owner, Resources res,
1629 XmlPullParser parser, AttributeSet attrs, String[] outError)
1630 throws XmlPullParserException, IOException {
1631 Permission perm = new Permission(owner);
1632
1633 TypedArray sa = res.obtainAttributes(attrs,
1634 com.android.internal.R.styleable.AndroidManifestPermissionTree);
1635
1636 if (!parsePackageItemInfo(owner, perm.info, outError,
1637 "<permission-tree>", sa,
1638 com.android.internal.R.styleable.AndroidManifestPermissionTree_name,
1639 com.android.internal.R.styleable.AndroidManifestPermissionTree_label,
Adam Powell81cd2e92010-04-21 16:35:18 -07001640 com.android.internal.R.styleable.AndroidManifestPermissionTree_icon,
1641 com.android.internal.R.styleable.AndroidManifestPermissionTree_logo)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 sa.recycle();
1643 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1644 return null;
1645 }
1646
1647 sa.recycle();
1648
1649 int index = perm.info.name.indexOf('.');
1650 if (index > 0) {
1651 index = perm.info.name.indexOf('.', index+1);
1652 }
1653 if (index < 0) {
1654 outError[0] = "<permission-tree> name has less than three segments: "
1655 + perm.info.name;
1656 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1657 return null;
1658 }
1659
1660 perm.info.descriptionRes = 0;
1661 perm.info.protectionLevel = PermissionInfo.PROTECTION_NORMAL;
1662 perm.tree = true;
1663
1664 if (!parseAllMetaData(res, parser, attrs, "<permission-tree>", perm,
1665 outError)) {
1666 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1667 return null;
1668 }
1669
1670 owner.permissions.add(perm);
1671
1672 return perm;
1673 }
1674
1675 private Instrumentation parseInstrumentation(Package owner, Resources res,
1676 XmlPullParser parser, AttributeSet attrs, String[] outError)
1677 throws XmlPullParserException, IOException {
1678 TypedArray sa = res.obtainAttributes(attrs,
1679 com.android.internal.R.styleable.AndroidManifestInstrumentation);
1680
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001681 if (mParseInstrumentationArgs == null) {
1682 mParseInstrumentationArgs = new ParsePackageItemArgs(owner, outError,
1683 com.android.internal.R.styleable.AndroidManifestInstrumentation_name,
1684 com.android.internal.R.styleable.AndroidManifestInstrumentation_label,
Adam Powell81cd2e92010-04-21 16:35:18 -07001685 com.android.internal.R.styleable.AndroidManifestInstrumentation_icon,
1686 com.android.internal.R.styleable.AndroidManifestInstrumentation_logo);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001687 mParseInstrumentationArgs.tag = "<instrumentation>";
1688 }
1689
1690 mParseInstrumentationArgs.sa = sa;
1691
1692 Instrumentation a = new Instrumentation(mParseInstrumentationArgs,
1693 new InstrumentationInfo());
1694 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 sa.recycle();
1696 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1697 return null;
1698 }
1699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 String str;
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001701 // Note: don't allow this value to be a reference to a resource
1702 // that may change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 str = sa.getNonResourceString(
1704 com.android.internal.R.styleable.AndroidManifestInstrumentation_targetPackage);
1705 a.info.targetPackage = str != null ? str.intern() : null;
1706
1707 a.info.handleProfiling = sa.getBoolean(
1708 com.android.internal.R.styleable.AndroidManifestInstrumentation_handleProfiling,
1709 false);
1710
1711 a.info.functionalTest = sa.getBoolean(
1712 com.android.internal.R.styleable.AndroidManifestInstrumentation_functionalTest,
1713 false);
1714
1715 sa.recycle();
1716
1717 if (a.info.targetPackage == null) {
1718 outError[0] = "<instrumentation> does not specify targetPackage";
1719 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1720 return null;
1721 }
1722
1723 if (!parseAllMetaData(res, parser, attrs, "<instrumentation>", a,
1724 outError)) {
1725 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1726 return null;
1727 }
1728
1729 owner.instrumentation.add(a);
1730
1731 return a;
1732 }
1733
1734 private boolean parseApplication(Package owner, Resources res,
1735 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
1736 throws XmlPullParserException, IOException {
1737 final ApplicationInfo ai = owner.applicationInfo;
1738 final String pkgName = owner.applicationInfo.packageName;
1739
1740 TypedArray sa = res.obtainAttributes(attrs,
1741 com.android.internal.R.styleable.AndroidManifestApplication);
1742
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001743 String name = sa.getNonConfigurationString(
1744 com.android.internal.R.styleable.AndroidManifestApplication_name, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 if (name != null) {
1746 ai.className = buildClassName(pkgName, name, outError);
1747 if (ai.className == null) {
1748 sa.recycle();
1749 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1750 return false;
1751 }
1752 }
1753
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001754 String manageSpaceActivity = sa.getNonConfigurationString(
1755 com.android.internal.R.styleable.AndroidManifestApplication_manageSpaceActivity, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 if (manageSpaceActivity != null) {
1757 ai.manageSpaceActivityName = buildClassName(pkgName, manageSpaceActivity,
1758 outError);
1759 }
1760
Christopher Tate181fafa2009-05-14 11:12:14 -07001761 boolean allowBackup = sa.getBoolean(
1762 com.android.internal.R.styleable.AndroidManifestApplication_allowBackup, true);
1763 if (allowBackup) {
1764 ai.flags |= ApplicationInfo.FLAG_ALLOW_BACKUP;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001765
Christopher Tate3de55bc2010-03-12 17:28:08 -08001766 // backupAgent, killAfterRestore, and restoreAnyVersion are only relevant
1767 // if backup is possible for the given application.
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001768 String backupAgent = sa.getNonConfigurationString(
1769 com.android.internal.R.styleable.AndroidManifestApplication_backupAgent, 0);
Christopher Tate181fafa2009-05-14 11:12:14 -07001770 if (backupAgent != null) {
1771 ai.backupAgentName = buildClassName(pkgName, backupAgent, outError);
Kenny Rootd2d29252011-08-08 11:27:57 -07001772 if (DEBUG_BACKUP) {
1773 Slog.v(TAG, "android:backupAgent = " + ai.backupAgentName
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001774 + " from " + pkgName + "+" + backupAgent);
1775 }
Christopher Tate5e1ab332009-09-01 20:32:49 -07001776
1777 if (sa.getBoolean(
1778 com.android.internal.R.styleable.AndroidManifestApplication_killAfterRestore,
1779 true)) {
1780 ai.flags |= ApplicationInfo.FLAG_KILL_AFTER_RESTORE;
1781 }
1782 if (sa.getBoolean(
Christopher Tate3dda5182010-02-24 16:06:18 -08001783 com.android.internal.R.styleable.AndroidManifestApplication_restoreAnyVersion,
1784 false)) {
1785 ai.flags |= ApplicationInfo.FLAG_RESTORE_ANY_VERSION;
1786 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001787 }
1788 }
Christopher Tate4a627c72011-04-01 14:43:32 -07001789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 TypedValue v = sa.peekValue(
1791 com.android.internal.R.styleable.AndroidManifestApplication_label);
1792 if (v != null && (ai.labelRes=v.resourceId) == 0) {
1793 ai.nonLocalizedLabel = v.coerceToString();
1794 }
1795
1796 ai.icon = sa.getResourceId(
1797 com.android.internal.R.styleable.AndroidManifestApplication_icon, 0);
Adam Powell81cd2e92010-04-21 16:35:18 -07001798 ai.logo = sa.getResourceId(
1799 com.android.internal.R.styleable.AndroidManifestApplication_logo, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 ai.theme = sa.getResourceId(
Dianne Hackbornb35cd542011-01-04 21:30:53 -08001801 com.android.internal.R.styleable.AndroidManifestApplication_theme, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 ai.descriptionRes = sa.getResourceId(
1803 com.android.internal.R.styleable.AndroidManifestApplication_description, 0);
1804
1805 if ((flags&PARSE_IS_SYSTEM) != 0) {
1806 if (sa.getBoolean(
1807 com.android.internal.R.styleable.AndroidManifestApplication_persistent,
1808 false)) {
1809 ai.flags |= ApplicationInfo.FLAG_PERSISTENT;
1810 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001811 if (sa.getBoolean(
1812 com.android.internal.R.styleable.AndroidManifestApplication_requiredForAllUsers,
1813 false)) {
1814 owner.mRequiredForAllUsers = true;
1815 }
Amith Yamasani0ac1fc92013-03-27 18:56:08 -07001816 String accountType = sa.getString(com.android.internal.R.styleable
1817 .AndroidManifestApplication_restrictedAccountType);
1818 if (accountType != null && accountType.length() > 0) {
1819 owner.mRestrictedAccountType = accountType;
1820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 }
1822
1823 if (sa.getBoolean(
1824 com.android.internal.R.styleable.AndroidManifestApplication_debuggable,
1825 false)) {
1826 ai.flags |= ApplicationInfo.FLAG_DEBUGGABLE;
1827 }
1828
1829 if (sa.getBoolean(
Ben Chengef3f5dd2010-03-29 15:47:26 -07001830 com.android.internal.R.styleable.AndroidManifestApplication_vmSafeMode,
Ben Cheng23085b72010-02-08 16:06:32 -08001831 false)) {
1832 ai.flags |= ApplicationInfo.FLAG_VM_SAFE_MODE;
1833 }
1834
Romain Guy529b60a2010-08-03 18:05:47 -07001835 boolean hardwareAccelerated = sa.getBoolean(
Romain Guy812ccbe2010-06-01 14:07:24 -07001836 com.android.internal.R.styleable.AndroidManifestApplication_hardwareAccelerated,
Dianne Hackborn2d6833b2011-06-24 16:04:19 -07001837 owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.ICE_CREAM_SANDWICH);
Romain Guy812ccbe2010-06-01 14:07:24 -07001838
1839 if (sa.getBoolean(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 com.android.internal.R.styleable.AndroidManifestApplication_hasCode,
1841 true)) {
1842 ai.flags |= ApplicationInfo.FLAG_HAS_CODE;
1843 }
1844
1845 if (sa.getBoolean(
1846 com.android.internal.R.styleable.AndroidManifestApplication_allowTaskReparenting,
1847 false)) {
1848 ai.flags |= ApplicationInfo.FLAG_ALLOW_TASK_REPARENTING;
1849 }
1850
1851 if (sa.getBoolean(
1852 com.android.internal.R.styleable.AndroidManifestApplication_allowClearUserData,
1853 true)) {
1854 ai.flags |= ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA;
1855 }
1856
Dianne Hackbornade3eca2009-05-11 18:54:45 -07001857 if (sa.getBoolean(
1858 com.android.internal.R.styleable.AndroidManifestApplication_testOnly,
Dianne Hackborne7fe35b2009-05-13 10:53:41 -07001859 false)) {
Dianne Hackbornade3eca2009-05-11 18:54:45 -07001860 ai.flags |= ApplicationInfo.FLAG_TEST_ONLY;
1861 }
1862
Jason parksa3cdaa52011-01-13 14:15:43 -06001863 if (sa.getBoolean(
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08001864 com.android.internal.R.styleable.AndroidManifestApplication_largeHeap,
Jason parksa3cdaa52011-01-13 14:15:43 -06001865 false)) {
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08001866 ai.flags |= ApplicationInfo.FLAG_LARGE_HEAP;
Jason parksa3cdaa52011-01-13 14:15:43 -06001867 }
1868
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -07001869 if (sa.getBoolean(
1870 com.android.internal.R.styleable.AndroidManifestApplication_supportsRtl,
1871 false /* default is no RTL support*/)) {
1872 ai.flags |= ApplicationInfo.FLAG_SUPPORTS_RTL;
1873 }
1874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 String str;
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001876 str = sa.getNonConfigurationString(
1877 com.android.internal.R.styleable.AndroidManifestApplication_permission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 ai.permission = (str != null && str.length() > 0) ? str.intern() : null;
1879
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07001880 if (owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.FROYO) {
1881 str = sa.getNonConfigurationString(
1882 com.android.internal.R.styleable.AndroidManifestApplication_taskAffinity, 0);
1883 } else {
1884 // Some older apps have been seen to use a resource reference
1885 // here that on older builds was ignored (with a warning). We
1886 // need to continue to do this for them so they don't break.
1887 str = sa.getNonResourceString(
1888 com.android.internal.R.styleable.AndroidManifestApplication_taskAffinity);
1889 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 ai.taskAffinity = buildTaskAffinityName(ai.packageName, ai.packageName,
1891 str, outError);
1892
1893 if (outError[0] == null) {
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07001894 CharSequence pname;
1895 if (owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.FROYO) {
1896 pname = sa.getNonConfigurationString(
1897 com.android.internal.R.styleable.AndroidManifestApplication_process, 0);
1898 } else {
1899 // Some older apps have been seen to use a resource reference
1900 // here that on older builds was ignored (with a warning). We
1901 // need to continue to do this for them so they don't break.
1902 pname = sa.getNonResourceString(
1903 com.android.internal.R.styleable.AndroidManifestApplication_process);
1904 }
1905 ai.processName = buildProcessName(ai.packageName, null, pname,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 flags, mSeparateProcesses, outError);
1907
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001908 ai.enabled = sa.getBoolean(
1909 com.android.internal.R.styleable.AndroidManifestApplication_enabled, true);
Dianne Hackborn860755f2010-06-03 18:47:52 -07001910
Dianne Hackborn02486b12010-08-26 14:18:37 -07001911 if (false) {
1912 if (sa.getBoolean(
1913 com.android.internal.R.styleable.AndroidManifestApplication_cantSaveState,
1914 false)) {
Dianne Hackborn54e570f2010-10-04 18:32:32 -07001915 ai.flags |= ApplicationInfo.FLAG_CANT_SAVE_STATE;
Dianne Hackborn02486b12010-08-26 14:18:37 -07001916
1917 // A heavy-weight application can not be in a custom process.
1918 // We can do direct compare because we intern all strings.
1919 if (ai.processName != null && ai.processName != ai.packageName) {
1920 outError[0] = "cantSaveState applications can not use custom processes";
1921 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07001922 }
1923 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 }
1925
Adam Powell269248d2011-08-02 10:26:54 -07001926 ai.uiOptions = sa.getInt(
1927 com.android.internal.R.styleable.AndroidManifestApplication_uiOptions, 0);
1928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 sa.recycle();
1930
1931 if (outError[0] != null) {
1932 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1933 return false;
1934 }
1935
1936 final int innerDepth = parser.getDepth();
1937
1938 int type;
Kenny Rootd2d29252011-08-08 11:27:57 -07001939 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1940 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1941 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 continue;
1943 }
1944
1945 String tagName = parser.getName();
1946 if (tagName.equals("activity")) {
Romain Guy529b60a2010-08-03 18:05:47 -07001947 Activity a = parseActivity(owner, res, parser, attrs, flags, outError, false,
1948 hardwareAccelerated);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 if (a == null) {
1950 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1951 return false;
1952 }
1953
1954 owner.activities.add(a);
1955
1956 } else if (tagName.equals("receiver")) {
Romain Guy529b60a2010-08-03 18:05:47 -07001957 Activity a = parseActivity(owner, res, parser, attrs, flags, outError, true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 if (a == null) {
1959 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1960 return false;
1961 }
1962
1963 owner.receivers.add(a);
1964
1965 } else if (tagName.equals("service")) {
1966 Service s = parseService(owner, res, parser, attrs, flags, outError);
1967 if (s == null) {
1968 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1969 return false;
1970 }
1971
1972 owner.services.add(s);
1973
1974 } else if (tagName.equals("provider")) {
1975 Provider p = parseProvider(owner, res, parser, attrs, flags, outError);
1976 if (p == null) {
1977 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1978 return false;
1979 }
1980
1981 owner.providers.add(p);
1982
1983 } else if (tagName.equals("activity-alias")) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001984 Activity a = parseActivityAlias(owner, res, parser, attrs, flags, outError);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 if (a == null) {
1986 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1987 return false;
1988 }
1989
1990 owner.activities.add(a);
1991
1992 } else if (parser.getName().equals("meta-data")) {
1993 // note: application meta-data is stored off to the side, so it can
1994 // remain null in the primary copy (we like to avoid extra copies because
1995 // it can be large)
1996 if ((owner.mAppMetaData = parseMetaData(res, parser, attrs, owner.mAppMetaData,
1997 outError)) == null) {
1998 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1999 return false;
2000 }
2001
Dianne Hackbornc895be72013-03-11 17:48:43 -07002002 } else if (tagName.equals("library")) {
2003 sa = res.obtainAttributes(attrs,
2004 com.android.internal.R.styleable.AndroidManifestLibrary);
2005
2006 // Note: don't allow this value to be a reference to a resource
2007 // that may change.
2008 String lname = sa.getNonResourceString(
2009 com.android.internal.R.styleable.AndroidManifestLibrary_name);
2010
2011 sa.recycle();
2012
2013 if (lname != null) {
2014 if (owner.libraryNames == null) {
2015 owner.libraryNames = new ArrayList<String>();
2016 }
2017 if (!owner.libraryNames.contains(lname)) {
2018 owner.libraryNames.add(lname.intern());
2019 }
2020 }
2021
2022 XmlUtils.skipCurrentTag(parser);
2023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 } else if (tagName.equals("uses-library")) {
2025 sa = res.obtainAttributes(attrs,
2026 com.android.internal.R.styleable.AndroidManifestUsesLibrary);
2027
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002028 // Note: don't allow this value to be a reference to a resource
2029 // that may change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 String lname = sa.getNonResourceString(
2031 com.android.internal.R.styleable.AndroidManifestUsesLibrary_name);
Dianne Hackborn49237342009-08-27 20:08:01 -07002032 boolean req = sa.getBoolean(
2033 com.android.internal.R.styleable.AndroidManifestUsesLibrary_required,
2034 true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035
2036 sa.recycle();
2037
Dianne Hackborn49237342009-08-27 20:08:01 -07002038 if (lname != null) {
2039 if (req) {
2040 if (owner.usesLibraries == null) {
2041 owner.usesLibraries = new ArrayList<String>();
2042 }
2043 if (!owner.usesLibraries.contains(lname)) {
2044 owner.usesLibraries.add(lname.intern());
2045 }
2046 } else {
2047 if (owner.usesOptionalLibraries == null) {
2048 owner.usesOptionalLibraries = new ArrayList<String>();
2049 }
2050 if (!owner.usesOptionalLibraries.contains(lname)) {
2051 owner.usesOptionalLibraries.add(lname.intern());
2052 }
2053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 }
2055
2056 XmlUtils.skipCurrentTag(parser);
2057
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002058 } else if (tagName.equals("uses-package")) {
2059 // Dependencies for app installers; we don't currently try to
2060 // enforce this.
2061 XmlUtils.skipCurrentTag(parser);
2062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 } else {
2064 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002065 Slog.w(TAG, "Unknown element under <application>: " + tagName
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002066 + " at " + mArchiveSourcePath + " "
2067 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 XmlUtils.skipCurrentTag(parser);
2069 continue;
2070 } else {
2071 outError[0] = "Bad element under <application>: " + tagName;
2072 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
2073 return false;
2074 }
2075 }
2076 }
2077
2078 return true;
2079 }
2080
2081 private boolean parsePackageItemInfo(Package owner, PackageItemInfo outInfo,
2082 String[] outError, String tag, TypedArray sa,
Adam Powell81cd2e92010-04-21 16:35:18 -07002083 int nameRes, int labelRes, int iconRes, int logoRes) {
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002084 String name = sa.getNonConfigurationString(nameRes, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 if (name == null) {
2086 outError[0] = tag + " does not specify android:name";
2087 return false;
2088 }
2089
2090 outInfo.name
2091 = buildClassName(owner.applicationInfo.packageName, name, outError);
2092 if (outInfo.name == null) {
2093 return false;
2094 }
2095
2096 int iconVal = sa.getResourceId(iconRes, 0);
2097 if (iconVal != 0) {
2098 outInfo.icon = iconVal;
2099 outInfo.nonLocalizedLabel = null;
2100 }
Adam Powell81cd2e92010-04-21 16:35:18 -07002101
2102 int logoVal = sa.getResourceId(logoRes, 0);
2103 if (logoVal != 0) {
2104 outInfo.logo = logoVal;
2105 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106
2107 TypedValue v = sa.peekValue(labelRes);
2108 if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
2109 outInfo.nonLocalizedLabel = v.coerceToString();
2110 }
2111
2112 outInfo.packageName = owner.packageName;
2113
2114 return true;
2115 }
2116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 private Activity parseActivity(Package owner, Resources res,
2118 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError,
Romain Guy529b60a2010-08-03 18:05:47 -07002119 boolean receiver, boolean hardwareAccelerated)
2120 throws XmlPullParserException, IOException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 TypedArray sa = res.obtainAttributes(attrs,
2122 com.android.internal.R.styleable.AndroidManifestActivity);
2123
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002124 if (mParseActivityArgs == null) {
2125 mParseActivityArgs = new ParseComponentArgs(owner, outError,
2126 com.android.internal.R.styleable.AndroidManifestActivity_name,
2127 com.android.internal.R.styleable.AndroidManifestActivity_label,
2128 com.android.internal.R.styleable.AndroidManifestActivity_icon,
Adam Powell81cd2e92010-04-21 16:35:18 -07002129 com.android.internal.R.styleable.AndroidManifestActivity_logo,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002130 mSeparateProcesses,
2131 com.android.internal.R.styleable.AndroidManifestActivity_process,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08002132 com.android.internal.R.styleable.AndroidManifestActivity_description,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002133 com.android.internal.R.styleable.AndroidManifestActivity_enabled);
2134 }
2135
2136 mParseActivityArgs.tag = receiver ? "<receiver>" : "<activity>";
2137 mParseActivityArgs.sa = sa;
2138 mParseActivityArgs.flags = flags;
2139
2140 Activity a = new Activity(mParseActivityArgs, new ActivityInfo());
2141 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 sa.recycle();
2143 return null;
2144 }
2145
Dianne Hackborn7d19e022012-08-07 19:12:33 -07002146 boolean setExported = sa.hasValue(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 com.android.internal.R.styleable.AndroidManifestActivity_exported);
2148 if (setExported) {
2149 a.info.exported = sa.getBoolean(
2150 com.android.internal.R.styleable.AndroidManifestActivity_exported, false);
2151 }
2152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 a.info.theme = sa.getResourceId(
2154 com.android.internal.R.styleable.AndroidManifestActivity_theme, 0);
2155
Adam Powell269248d2011-08-02 10:26:54 -07002156 a.info.uiOptions = sa.getInt(
2157 com.android.internal.R.styleable.AndroidManifestActivity_uiOptions,
2158 a.info.applicationInfo.uiOptions);
2159
Adam Powelldd8fab22012-03-22 17:47:27 -07002160 String parentName = sa.getNonConfigurationString(
2161 com.android.internal.R.styleable.AndroidManifestActivity_parentActivityName, 0);
2162 if (parentName != null) {
2163 String parentClassName = buildClassName(a.info.packageName, parentName, outError);
2164 if (outError[0] == null) {
2165 a.info.parentActivityName = parentClassName;
2166 } else {
2167 Log.e(TAG, "Activity " + a.info.name + " specified invalid parentActivityName " +
2168 parentName);
2169 outError[0] = null;
2170 }
2171 }
2172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 String str;
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002174 str = sa.getNonConfigurationString(
2175 com.android.internal.R.styleable.AndroidManifestActivity_permission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 if (str == null) {
2177 a.info.permission = owner.applicationInfo.permission;
2178 } else {
2179 a.info.permission = str.length() > 0 ? str.toString().intern() : null;
2180 }
2181
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002182 str = sa.getNonConfigurationString(
2183 com.android.internal.R.styleable.AndroidManifestActivity_taskAffinity, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 a.info.taskAffinity = buildTaskAffinityName(owner.applicationInfo.packageName,
2185 owner.applicationInfo.taskAffinity, str, outError);
2186
2187 a.info.flags = 0;
2188 if (sa.getBoolean(
2189 com.android.internal.R.styleable.AndroidManifestActivity_multiprocess,
2190 false)) {
2191 a.info.flags |= ActivityInfo.FLAG_MULTIPROCESS;
2192 }
2193
2194 if (sa.getBoolean(
2195 com.android.internal.R.styleable.AndroidManifestActivity_finishOnTaskLaunch,
2196 false)) {
2197 a.info.flags |= ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH;
2198 }
2199
2200 if (sa.getBoolean(
2201 com.android.internal.R.styleable.AndroidManifestActivity_clearTaskOnLaunch,
2202 false)) {
2203 a.info.flags |= ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH;
2204 }
2205
2206 if (sa.getBoolean(
2207 com.android.internal.R.styleable.AndroidManifestActivity_noHistory,
2208 false)) {
2209 a.info.flags |= ActivityInfo.FLAG_NO_HISTORY;
2210 }
2211
2212 if (sa.getBoolean(
2213 com.android.internal.R.styleable.AndroidManifestActivity_alwaysRetainTaskState,
2214 false)) {
2215 a.info.flags |= ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE;
2216 }
2217
2218 if (sa.getBoolean(
2219 com.android.internal.R.styleable.AndroidManifestActivity_stateNotNeeded,
2220 false)) {
2221 a.info.flags |= ActivityInfo.FLAG_STATE_NOT_NEEDED;
2222 }
2223
2224 if (sa.getBoolean(
2225 com.android.internal.R.styleable.AndroidManifestActivity_excludeFromRecents,
2226 false)) {
2227 a.info.flags |= ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS;
2228 }
2229
2230 if (sa.getBoolean(
2231 com.android.internal.R.styleable.AndroidManifestActivity_allowTaskReparenting,
2232 (owner.applicationInfo.flags&ApplicationInfo.FLAG_ALLOW_TASK_REPARENTING) != 0)) {
2233 a.info.flags |= ActivityInfo.FLAG_ALLOW_TASK_REPARENTING;
2234 }
2235
Dianne Hackbornffa42482009-09-23 22:20:11 -07002236 if (sa.getBoolean(
2237 com.android.internal.R.styleable.AndroidManifestActivity_finishOnCloseSystemDialogs,
2238 false)) {
2239 a.info.flags |= ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS;
2240 }
2241
Daniel Sandler613dde42010-06-21 13:46:39 -04002242 if (sa.getBoolean(
Craig Mautner5962b122012-10-05 14:45:52 -07002243 com.android.internal.R.styleable.AndroidManifestActivity_showOnLockScreen,
2244 false)) {
2245 a.info.flags |= ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN;
2246 }
2247
2248 if (sa.getBoolean(
Daniel Sandler613dde42010-06-21 13:46:39 -04002249 com.android.internal.R.styleable.AndroidManifestActivity_immersive,
2250 false)) {
2251 a.info.flags |= ActivityInfo.FLAG_IMMERSIVE;
2252 }
Craig Mautner5962b122012-10-05 14:45:52 -07002253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 if (!receiver) {
Romain Guy529b60a2010-08-03 18:05:47 -07002255 if (sa.getBoolean(
2256 com.android.internal.R.styleable.AndroidManifestActivity_hardwareAccelerated,
2257 hardwareAccelerated)) {
2258 a.info.flags |= ActivityInfo.FLAG_HARDWARE_ACCELERATED;
2259 }
2260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 a.info.launchMode = sa.getInt(
2262 com.android.internal.R.styleable.AndroidManifestActivity_launchMode,
2263 ActivityInfo.LAUNCH_MULTIPLE);
2264 a.info.screenOrientation = sa.getInt(
2265 com.android.internal.R.styleable.AndroidManifestActivity_screenOrientation,
2266 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
2267 a.info.configChanges = sa.getInt(
2268 com.android.internal.R.styleable.AndroidManifestActivity_configChanges,
2269 0);
2270 a.info.softInputMode = sa.getInt(
2271 com.android.internal.R.styleable.AndroidManifestActivity_windowSoftInputMode,
2272 0);
2273 } else {
2274 a.info.launchMode = ActivityInfo.LAUNCH_MULTIPLE;
2275 a.info.configChanges = 0;
2276 }
2277
Dianne Hackborn7d19e022012-08-07 19:12:33 -07002278 if (receiver) {
2279 if (sa.getBoolean(
2280 com.android.internal.R.styleable.AndroidManifestActivity_singleUser,
2281 false)) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07002282 a.info.flags |= ActivityInfo.FLAG_SINGLE_USER;
Dianne Hackborn7d19e022012-08-07 19:12:33 -07002283 if (a.info.exported) {
2284 Slog.w(TAG, "Activity exported request ignored due to singleUser: "
2285 + a.className + " at " + mArchiveSourcePath + " "
2286 + parser.getPositionDescription());
2287 a.info.exported = false;
2288 }
2289 setExported = true;
2290 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07002291 if (sa.getBoolean(
2292 com.android.internal.R.styleable.AndroidManifestActivity_primaryUserOnly,
2293 false)) {
2294 a.info.flags |= ActivityInfo.FLAG_PRIMARY_USER_ONLY;
2295 }
Dianne Hackborn7d19e022012-08-07 19:12:33 -07002296 }
2297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 sa.recycle();
2299
Dianne Hackborn54e570f2010-10-04 18:32:32 -07002300 if (receiver && (owner.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07002301 // A heavy-weight application can not have receives in its main process
2302 // We can do direct compare because we intern all strings.
2303 if (a.info.processName == owner.packageName) {
2304 outError[0] = "Heavy-weight applications can not have receivers in main process";
2305 }
2306 }
2307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 if (outError[0] != null) {
2309 return null;
2310 }
2311
2312 int outerDepth = parser.getDepth();
2313 int type;
2314 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2315 && (type != XmlPullParser.END_TAG
2316 || parser.getDepth() > outerDepth)) {
2317 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2318 continue;
2319 }
2320
2321 if (parser.getName().equals("intent-filter")) {
2322 ActivityIntentInfo intent = new ActivityIntentInfo(a);
2323 if (!parseIntent(res, parser, attrs, flags, intent, outError, !receiver)) {
2324 return null;
2325 }
2326 if (intent.countActions() == 0) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002327 Slog.w(TAG, "No actions in intent filter at "
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -07002328 + mArchiveSourcePath + " "
2329 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002330 } else {
2331 a.intents.add(intent);
2332 }
2333 } else if (parser.getName().equals("meta-data")) {
2334 if ((a.metaData=parseMetaData(res, parser, attrs, a.metaData,
2335 outError)) == null) {
2336 return null;
2337 }
2338 } else {
2339 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002340 Slog.w(TAG, "Problem in package " + mArchiveSourcePath + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 if (receiver) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002342 Slog.w(TAG, "Unknown element under <receiver>: " + parser.getName()
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002343 + " at " + mArchiveSourcePath + " "
2344 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -07002346 Slog.w(TAG, "Unknown element under <activity>: " + parser.getName()
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002347 + " at " + mArchiveSourcePath + " "
2348 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 }
2350 XmlUtils.skipCurrentTag(parser);
2351 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -07002353 if (receiver) {
2354 outError[0] = "Bad element under <receiver>: " + parser.getName();
2355 } else {
2356 outError[0] = "Bad element under <activity>: " + parser.getName();
2357 }
2358 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 }
2361 }
2362
2363 if (!setExported) {
2364 a.info.exported = a.intents.size() > 0;
2365 }
2366
2367 return a;
2368 }
2369
2370 private Activity parseActivityAlias(Package owner, Resources res,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002371 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
2372 throws XmlPullParserException, IOException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 TypedArray sa = res.obtainAttributes(attrs,
2374 com.android.internal.R.styleable.AndroidManifestActivityAlias);
2375
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002376 String targetActivity = sa.getNonConfigurationString(
2377 com.android.internal.R.styleable.AndroidManifestActivityAlias_targetActivity, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 if (targetActivity == null) {
2379 outError[0] = "<activity-alias> does not specify android:targetActivity";
2380 sa.recycle();
2381 return null;
2382 }
2383
2384 targetActivity = buildClassName(owner.applicationInfo.packageName,
2385 targetActivity, outError);
2386 if (targetActivity == null) {
2387 sa.recycle();
2388 return null;
2389 }
2390
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002391 if (mParseActivityAliasArgs == null) {
2392 mParseActivityAliasArgs = new ParseComponentArgs(owner, outError,
2393 com.android.internal.R.styleable.AndroidManifestActivityAlias_name,
2394 com.android.internal.R.styleable.AndroidManifestActivityAlias_label,
2395 com.android.internal.R.styleable.AndroidManifestActivityAlias_icon,
Adam Powell81cd2e92010-04-21 16:35:18 -07002396 com.android.internal.R.styleable.AndroidManifestActivityAlias_logo,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002397 mSeparateProcesses,
2398 0,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08002399 com.android.internal.R.styleable.AndroidManifestActivityAlias_description,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002400 com.android.internal.R.styleable.AndroidManifestActivityAlias_enabled);
2401 mParseActivityAliasArgs.tag = "<activity-alias>";
2402 }
2403
2404 mParseActivityAliasArgs.sa = sa;
2405 mParseActivityAliasArgs.flags = flags;
2406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 Activity target = null;
2408
2409 final int NA = owner.activities.size();
2410 for (int i=0; i<NA; i++) {
2411 Activity t = owner.activities.get(i);
2412 if (targetActivity.equals(t.info.name)) {
2413 target = t;
2414 break;
2415 }
2416 }
2417
2418 if (target == null) {
2419 outError[0] = "<activity-alias> target activity " + targetActivity
2420 + " not found in manifest";
2421 sa.recycle();
2422 return null;
2423 }
2424
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002425 ActivityInfo info = new ActivityInfo();
2426 info.targetActivity = targetActivity;
2427 info.configChanges = target.info.configChanges;
2428 info.flags = target.info.flags;
2429 info.icon = target.info.icon;
Adam Powell81cd2e92010-04-21 16:35:18 -07002430 info.logo = target.info.logo;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002431 info.labelRes = target.info.labelRes;
2432 info.nonLocalizedLabel = target.info.nonLocalizedLabel;
2433 info.launchMode = target.info.launchMode;
2434 info.processName = target.info.processName;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08002435 if (info.descriptionRes == 0) {
2436 info.descriptionRes = target.info.descriptionRes;
2437 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002438 info.screenOrientation = target.info.screenOrientation;
2439 info.taskAffinity = target.info.taskAffinity;
2440 info.theme = target.info.theme;
Dianne Hackborn0836c7c2011-10-20 18:40:23 -07002441 info.softInputMode = target.info.softInputMode;
Adam Powell269248d2011-08-02 10:26:54 -07002442 info.uiOptions = target.info.uiOptions;
Adam Powelldd8fab22012-03-22 17:47:27 -07002443 info.parentActivityName = target.info.parentActivityName;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002444
2445 Activity a = new Activity(mParseActivityAliasArgs, info);
2446 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 sa.recycle();
2448 return null;
2449 }
2450
2451 final boolean setExported = sa.hasValue(
2452 com.android.internal.R.styleable.AndroidManifestActivityAlias_exported);
2453 if (setExported) {
2454 a.info.exported = sa.getBoolean(
2455 com.android.internal.R.styleable.AndroidManifestActivityAlias_exported, false);
2456 }
2457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458 String str;
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002459 str = sa.getNonConfigurationString(
2460 com.android.internal.R.styleable.AndroidManifestActivityAlias_permission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 if (str != null) {
2462 a.info.permission = str.length() > 0 ? str.toString().intern() : null;
2463 }
2464
Adam Powelldd8fab22012-03-22 17:47:27 -07002465 String parentName = sa.getNonConfigurationString(
2466 com.android.internal.R.styleable.AndroidManifestActivityAlias_parentActivityName,
2467 0);
2468 if (parentName != null) {
2469 String parentClassName = buildClassName(a.info.packageName, parentName, outError);
2470 if (outError[0] == null) {
2471 a.info.parentActivityName = parentClassName;
2472 } else {
2473 Log.e(TAG, "Activity alias " + a.info.name +
2474 " specified invalid parentActivityName " + parentName);
2475 outError[0] = null;
2476 }
2477 }
2478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 sa.recycle();
2480
2481 if (outError[0] != null) {
2482 return null;
2483 }
2484
2485 int outerDepth = parser.getDepth();
2486 int type;
2487 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2488 && (type != XmlPullParser.END_TAG
2489 || parser.getDepth() > outerDepth)) {
2490 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2491 continue;
2492 }
2493
2494 if (parser.getName().equals("intent-filter")) {
2495 ActivityIntentInfo intent = new ActivityIntentInfo(a);
2496 if (!parseIntent(res, parser, attrs, flags, intent, outError, true)) {
2497 return null;
2498 }
2499 if (intent.countActions() == 0) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002500 Slog.w(TAG, "No actions in intent filter at "
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -07002501 + mArchiveSourcePath + " "
2502 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 } else {
2504 a.intents.add(intent);
2505 }
2506 } else if (parser.getName().equals("meta-data")) {
2507 if ((a.metaData=parseMetaData(res, parser, attrs, a.metaData,
2508 outError)) == null) {
2509 return null;
2510 }
2511 } else {
2512 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002513 Slog.w(TAG, "Unknown element under <activity-alias>: " + parser.getName()
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002514 + " at " + mArchiveSourcePath + " "
2515 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 XmlUtils.skipCurrentTag(parser);
2517 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07002518 } else {
2519 outError[0] = "Bad element under <activity-alias>: " + parser.getName();
2520 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 }
2523 }
2524
2525 if (!setExported) {
2526 a.info.exported = a.intents.size() > 0;
2527 }
2528
2529 return a;
2530 }
2531
2532 private Provider parseProvider(Package owner, Resources res,
2533 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
2534 throws XmlPullParserException, IOException {
2535 TypedArray sa = res.obtainAttributes(attrs,
2536 com.android.internal.R.styleable.AndroidManifestProvider);
2537
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002538 if (mParseProviderArgs == null) {
2539 mParseProviderArgs = new ParseComponentArgs(owner, outError,
2540 com.android.internal.R.styleable.AndroidManifestProvider_name,
2541 com.android.internal.R.styleable.AndroidManifestProvider_label,
2542 com.android.internal.R.styleable.AndroidManifestProvider_icon,
Adam Powell81cd2e92010-04-21 16:35:18 -07002543 com.android.internal.R.styleable.AndroidManifestProvider_logo,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002544 mSeparateProcesses,
2545 com.android.internal.R.styleable.AndroidManifestProvider_process,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08002546 com.android.internal.R.styleable.AndroidManifestProvider_description,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002547 com.android.internal.R.styleable.AndroidManifestProvider_enabled);
2548 mParseProviderArgs.tag = "<provider>";
2549 }
2550
2551 mParseProviderArgs.sa = sa;
2552 mParseProviderArgs.flags = flags;
2553
2554 Provider p = new Provider(mParseProviderArgs, new ProviderInfo());
2555 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 sa.recycle();
2557 return null;
2558 }
2559
Nick Kralevichf097b162012-07-28 12:43:48 -07002560 boolean providerExportedDefault = false;
2561
2562 if (owner.applicationInfo.targetSdkVersion < Build.VERSION_CODES.JELLY_BEAN_MR1) {
2563 // For compatibility, applications targeting API level 16 or lower
2564 // should have their content providers exported by default, unless they
2565 // specify otherwise.
2566 providerExportedDefault = true;
2567 }
2568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 p.info.exported = sa.getBoolean(
Nick Kralevichf097b162012-07-28 12:43:48 -07002570 com.android.internal.R.styleable.AndroidManifestProvider_exported,
2571 providerExportedDefault);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002573 String cpname = sa.getNonConfigurationString(
2574 com.android.internal.R.styleable.AndroidManifestProvider_authorities, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002575
2576 p.info.isSyncable = sa.getBoolean(
2577 com.android.internal.R.styleable.AndroidManifestProvider_syncable,
2578 false);
2579
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002580 String permission = sa.getNonConfigurationString(
2581 com.android.internal.R.styleable.AndroidManifestProvider_permission, 0);
2582 String str = sa.getNonConfigurationString(
2583 com.android.internal.R.styleable.AndroidManifestProvider_readPermission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 if (str == null) {
2585 str = permission;
2586 }
2587 if (str == null) {
2588 p.info.readPermission = owner.applicationInfo.permission;
2589 } else {
2590 p.info.readPermission =
2591 str.length() > 0 ? str.toString().intern() : null;
2592 }
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002593 str = sa.getNonConfigurationString(
2594 com.android.internal.R.styleable.AndroidManifestProvider_writePermission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 if (str == null) {
2596 str = permission;
2597 }
2598 if (str == null) {
2599 p.info.writePermission = owner.applicationInfo.permission;
2600 } else {
2601 p.info.writePermission =
2602 str.length() > 0 ? str.toString().intern() : null;
2603 }
2604
2605 p.info.grantUriPermissions = sa.getBoolean(
2606 com.android.internal.R.styleable.AndroidManifestProvider_grantUriPermissions,
2607 false);
2608
2609 p.info.multiprocess = sa.getBoolean(
2610 com.android.internal.R.styleable.AndroidManifestProvider_multiprocess,
2611 false);
2612
2613 p.info.initOrder = sa.getInt(
2614 com.android.internal.R.styleable.AndroidManifestProvider_initOrder,
2615 0);
2616
Dianne Hackborn7d19e022012-08-07 19:12:33 -07002617 p.info.flags = 0;
2618
2619 if (sa.getBoolean(
2620 com.android.internal.R.styleable.AndroidManifestProvider_singleUser,
2621 false)) {
2622 p.info.flags |= ProviderInfo.FLAG_SINGLE_USER;
2623 if (p.info.exported) {
2624 Slog.w(TAG, "Provider exported request ignored due to singleUser: "
2625 + p.className + " at " + mArchiveSourcePath + " "
2626 + parser.getPositionDescription());
2627 p.info.exported = false;
2628 }
2629 }
2630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 sa.recycle();
2632
Dianne Hackborn54e570f2010-10-04 18:32:32 -07002633 if ((owner.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07002634 // A heavy-weight application can not have providers in its main process
2635 // We can do direct compare because we intern all strings.
2636 if (p.info.processName == owner.packageName) {
2637 outError[0] = "Heavy-weight applications can not have providers in main process";
2638 return null;
2639 }
2640 }
2641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 if (cpname == null) {
Nick Kralevichf097b162012-07-28 12:43:48 -07002643 outError[0] = "<provider> does not include authorities attribute";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002644 return null;
2645 }
2646 p.info.authority = cpname.intern();
2647
2648 if (!parseProviderTags(res, parser, attrs, p, outError)) {
2649 return null;
2650 }
2651
2652 return p;
2653 }
2654
2655 private boolean parseProviderTags(Resources res,
2656 XmlPullParser parser, AttributeSet attrs,
2657 Provider outInfo, String[] outError)
2658 throws XmlPullParserException, IOException {
2659 int outerDepth = parser.getDepth();
2660 int type;
2661 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2662 && (type != XmlPullParser.END_TAG
2663 || parser.getDepth() > outerDepth)) {
2664 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2665 continue;
2666 }
2667
2668 if (parser.getName().equals("meta-data")) {
2669 if ((outInfo.metaData=parseMetaData(res, parser, attrs,
2670 outInfo.metaData, outError)) == null) {
2671 return false;
2672 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 } else if (parser.getName().equals("grant-uri-permission")) {
2675 TypedArray sa = res.obtainAttributes(attrs,
2676 com.android.internal.R.styleable.AndroidManifestGrantUriPermission);
2677
2678 PatternMatcher pa = null;
2679
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002680 String str = sa.getNonConfigurationString(
2681 com.android.internal.R.styleable.AndroidManifestGrantUriPermission_path, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 if (str != null) {
2683 pa = new PatternMatcher(str, PatternMatcher.PATTERN_LITERAL);
2684 }
2685
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002686 str = sa.getNonConfigurationString(
2687 com.android.internal.R.styleable.AndroidManifestGrantUriPermission_pathPrefix, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 if (str != null) {
2689 pa = new PatternMatcher(str, PatternMatcher.PATTERN_PREFIX);
2690 }
2691
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002692 str = sa.getNonConfigurationString(
2693 com.android.internal.R.styleable.AndroidManifestGrantUriPermission_pathPattern, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 if (str != null) {
2695 pa = new PatternMatcher(str, PatternMatcher.PATTERN_SIMPLE_GLOB);
2696 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 sa.recycle();
2699
2700 if (pa != null) {
2701 if (outInfo.info.uriPermissionPatterns == null) {
2702 outInfo.info.uriPermissionPatterns = new PatternMatcher[1];
2703 outInfo.info.uriPermissionPatterns[0] = pa;
2704 } else {
2705 final int N = outInfo.info.uriPermissionPatterns.length;
2706 PatternMatcher[] newp = new PatternMatcher[N+1];
2707 System.arraycopy(outInfo.info.uriPermissionPatterns, 0, newp, 0, N);
2708 newp[N] = pa;
2709 outInfo.info.uriPermissionPatterns = newp;
2710 }
2711 outInfo.info.grantUriPermissions = true;
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002712 } else {
2713 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002714 Slog.w(TAG, "Unknown element under <path-permission>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002715 + parser.getName() + " at " + mArchiveSourcePath + " "
2716 + parser.getPositionDescription());
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002717 XmlUtils.skipCurrentTag(parser);
2718 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07002719 } else {
2720 outError[0] = "No path, pathPrefix, or pathPattern for <path-permission>";
2721 return false;
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002722 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002723 }
2724 XmlUtils.skipCurrentTag(parser);
2725
2726 } else if (parser.getName().equals("path-permission")) {
2727 TypedArray sa = res.obtainAttributes(attrs,
2728 com.android.internal.R.styleable.AndroidManifestPathPermission);
2729
2730 PathPermission pa = null;
2731
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002732 String permission = sa.getNonConfigurationString(
2733 com.android.internal.R.styleable.AndroidManifestPathPermission_permission, 0);
2734 String readPermission = sa.getNonConfigurationString(
2735 com.android.internal.R.styleable.AndroidManifestPathPermission_readPermission, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002736 if (readPermission == null) {
2737 readPermission = permission;
2738 }
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002739 String writePermission = sa.getNonConfigurationString(
2740 com.android.internal.R.styleable.AndroidManifestPathPermission_writePermission, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002741 if (writePermission == null) {
2742 writePermission = permission;
2743 }
2744
2745 boolean havePerm = false;
2746 if (readPermission != null) {
2747 readPermission = readPermission.intern();
2748 havePerm = true;
2749 }
2750 if (writePermission != null) {
Bjorn Bringerte04b1ad2010-02-09 13:56:08 +00002751 writePermission = writePermission.intern();
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002752 havePerm = true;
2753 }
2754
2755 if (!havePerm) {
2756 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002757 Slog.w(TAG, "No readPermission or writePermssion for <path-permission>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002758 + parser.getName() + " at " + mArchiveSourcePath + " "
2759 + parser.getPositionDescription());
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002760 XmlUtils.skipCurrentTag(parser);
2761 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07002762 } else {
2763 outError[0] = "No readPermission or writePermssion for <path-permission>";
2764 return false;
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002765 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002766 }
2767
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002768 String path = sa.getNonConfigurationString(
2769 com.android.internal.R.styleable.AndroidManifestPathPermission_path, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002770 if (path != null) {
2771 pa = new PathPermission(path,
2772 PatternMatcher.PATTERN_LITERAL, readPermission, writePermission);
2773 }
2774
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002775 path = sa.getNonConfigurationString(
2776 com.android.internal.R.styleable.AndroidManifestPathPermission_pathPrefix, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002777 if (path != null) {
2778 pa = new PathPermission(path,
2779 PatternMatcher.PATTERN_PREFIX, readPermission, writePermission);
2780 }
2781
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002782 path = sa.getNonConfigurationString(
2783 com.android.internal.R.styleable.AndroidManifestPathPermission_pathPattern, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002784 if (path != null) {
2785 pa = new PathPermission(path,
2786 PatternMatcher.PATTERN_SIMPLE_GLOB, readPermission, writePermission);
2787 }
2788
2789 sa.recycle();
2790
2791 if (pa != null) {
2792 if (outInfo.info.pathPermissions == null) {
2793 outInfo.info.pathPermissions = new PathPermission[1];
2794 outInfo.info.pathPermissions[0] = pa;
2795 } else {
2796 final int N = outInfo.info.pathPermissions.length;
2797 PathPermission[] newp = new PathPermission[N+1];
2798 System.arraycopy(outInfo.info.pathPermissions, 0, newp, 0, N);
2799 newp[N] = pa;
2800 outInfo.info.pathPermissions = newp;
2801 }
2802 } else {
2803 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002804 Slog.w(TAG, "No path, pathPrefix, or pathPattern for <path-permission>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002805 + parser.getName() + " at " + mArchiveSourcePath + " "
2806 + parser.getPositionDescription());
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002807 XmlUtils.skipCurrentTag(parser);
2808 continue;
2809 }
2810 outError[0] = "No path, pathPrefix, or pathPattern for <path-permission>";
2811 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 }
2813 XmlUtils.skipCurrentTag(parser);
2814
2815 } else {
2816 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002817 Slog.w(TAG, "Unknown element under <provider>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002818 + parser.getName() + " at " + mArchiveSourcePath + " "
2819 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 XmlUtils.skipCurrentTag(parser);
2821 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07002822 } else {
2823 outError[0] = "Bad element under <provider>: " + parser.getName();
2824 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002825 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 }
2827 }
2828 return true;
2829 }
2830
2831 private Service parseService(Package owner, Resources res,
2832 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
2833 throws XmlPullParserException, IOException {
2834 TypedArray sa = res.obtainAttributes(attrs,
2835 com.android.internal.R.styleable.AndroidManifestService);
2836
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002837 if (mParseServiceArgs == null) {
2838 mParseServiceArgs = new ParseComponentArgs(owner, outError,
2839 com.android.internal.R.styleable.AndroidManifestService_name,
2840 com.android.internal.R.styleable.AndroidManifestService_label,
2841 com.android.internal.R.styleable.AndroidManifestService_icon,
Adam Powell81cd2e92010-04-21 16:35:18 -07002842 com.android.internal.R.styleable.AndroidManifestService_logo,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002843 mSeparateProcesses,
2844 com.android.internal.R.styleable.AndroidManifestService_process,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08002845 com.android.internal.R.styleable.AndroidManifestService_description,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002846 com.android.internal.R.styleable.AndroidManifestService_enabled);
2847 mParseServiceArgs.tag = "<service>";
2848 }
2849
2850 mParseServiceArgs.sa = sa;
2851 mParseServiceArgs.flags = flags;
2852
2853 Service s = new Service(mParseServiceArgs, new ServiceInfo());
2854 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 sa.recycle();
2856 return null;
2857 }
2858
Dianne Hackbornb4163a62012-08-02 18:31:26 -07002859 boolean setExported = sa.hasValue(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 com.android.internal.R.styleable.AndroidManifestService_exported);
2861 if (setExported) {
2862 s.info.exported = sa.getBoolean(
2863 com.android.internal.R.styleable.AndroidManifestService_exported, false);
2864 }
2865
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002866 String str = sa.getNonConfigurationString(
2867 com.android.internal.R.styleable.AndroidManifestService_permission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 if (str == null) {
2869 s.info.permission = owner.applicationInfo.permission;
2870 } else {
2871 s.info.permission = str.length() > 0 ? str.toString().intern() : null;
2872 }
2873
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002874 s.info.flags = 0;
2875 if (sa.getBoolean(
2876 com.android.internal.R.styleable.AndroidManifestService_stopWithTask,
2877 false)) {
2878 s.info.flags |= ServiceInfo.FLAG_STOP_WITH_TASK;
2879 }
Dianne Hackborna0c283e2012-02-09 10:47:01 -08002880 if (sa.getBoolean(
2881 com.android.internal.R.styleable.AndroidManifestService_isolatedProcess,
2882 false)) {
2883 s.info.flags |= ServiceInfo.FLAG_ISOLATED_PROCESS;
2884 }
Dianne Hackbornb4163a62012-08-02 18:31:26 -07002885 if (sa.getBoolean(
2886 com.android.internal.R.styleable.AndroidManifestService_singleUser,
2887 false)) {
2888 s.info.flags |= ServiceInfo.FLAG_SINGLE_USER;
2889 if (s.info.exported) {
2890 Slog.w(TAG, "Service exported request ignored due to singleUser: "
2891 + s.className + " at " + mArchiveSourcePath + " "
2892 + parser.getPositionDescription());
2893 s.info.exported = false;
2894 }
2895 setExported = true;
2896 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 sa.recycle();
2899
Dianne Hackborn54e570f2010-10-04 18:32:32 -07002900 if ((owner.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07002901 // A heavy-weight application can not have services in its main process
2902 // We can do direct compare because we intern all strings.
2903 if (s.info.processName == owner.packageName) {
2904 outError[0] = "Heavy-weight applications can not have services in main process";
2905 return null;
2906 }
2907 }
2908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 int outerDepth = parser.getDepth();
2910 int type;
2911 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2912 && (type != XmlPullParser.END_TAG
2913 || parser.getDepth() > outerDepth)) {
2914 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2915 continue;
2916 }
2917
2918 if (parser.getName().equals("intent-filter")) {
2919 ServiceIntentInfo intent = new ServiceIntentInfo(s);
2920 if (!parseIntent(res, parser, attrs, flags, intent, outError, false)) {
2921 return null;
2922 }
2923
2924 s.intents.add(intent);
2925 } else if (parser.getName().equals("meta-data")) {
2926 if ((s.metaData=parseMetaData(res, parser, attrs, s.metaData,
2927 outError)) == null) {
2928 return null;
2929 }
2930 } else {
2931 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002932 Slog.w(TAG, "Unknown element under <service>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002933 + parser.getName() + " at " + mArchiveSourcePath + " "
2934 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 XmlUtils.skipCurrentTag(parser);
2936 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07002937 } else {
2938 outError[0] = "Bad element under <service>: " + parser.getName();
2939 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 }
2942 }
2943
2944 if (!setExported) {
2945 s.info.exported = s.intents.size() > 0;
2946 }
2947
2948 return s;
2949 }
2950
2951 private boolean parseAllMetaData(Resources res,
2952 XmlPullParser parser, AttributeSet attrs, String tag,
2953 Component outInfo, String[] outError)
2954 throws XmlPullParserException, IOException {
2955 int outerDepth = parser.getDepth();
2956 int type;
2957 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2958 && (type != XmlPullParser.END_TAG
2959 || parser.getDepth() > outerDepth)) {
2960 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2961 continue;
2962 }
2963
2964 if (parser.getName().equals("meta-data")) {
2965 if ((outInfo.metaData=parseMetaData(res, parser, attrs,
2966 outInfo.metaData, outError)) == null) {
2967 return false;
2968 }
2969 } else {
2970 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002971 Slog.w(TAG, "Unknown element under " + tag + ": "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002972 + parser.getName() + " at " + mArchiveSourcePath + " "
2973 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002974 XmlUtils.skipCurrentTag(parser);
2975 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07002976 } else {
2977 outError[0] = "Bad element under " + tag + ": " + parser.getName();
2978 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 }
2981 }
2982 return true;
2983 }
2984
2985 private Bundle parseMetaData(Resources res,
2986 XmlPullParser parser, AttributeSet attrs,
2987 Bundle data, String[] outError)
2988 throws XmlPullParserException, IOException {
2989
2990 TypedArray sa = res.obtainAttributes(attrs,
2991 com.android.internal.R.styleable.AndroidManifestMetaData);
2992
2993 if (data == null) {
2994 data = new Bundle();
2995 }
2996
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002997 String name = sa.getNonConfigurationString(
2998 com.android.internal.R.styleable.AndroidManifestMetaData_name, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002999 if (name == null) {
3000 outError[0] = "<meta-data> requires an android:name attribute";
3001 sa.recycle();
3002 return null;
3003 }
3004
Dianne Hackborn854060a2009-07-09 18:14:31 -07003005 name = name.intern();
3006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 TypedValue v = sa.peekValue(
3008 com.android.internal.R.styleable.AndroidManifestMetaData_resource);
3009 if (v != null && v.resourceId != 0) {
Kenny Rootd2d29252011-08-08 11:27:57 -07003010 //Slog.i(TAG, "Meta data ref " + name + ": " + v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 data.putInt(name, v.resourceId);
3012 } else {
3013 v = sa.peekValue(
3014 com.android.internal.R.styleable.AndroidManifestMetaData_value);
Kenny Rootd2d29252011-08-08 11:27:57 -07003015 //Slog.i(TAG, "Meta data " + name + ": " + v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 if (v != null) {
3017 if (v.type == TypedValue.TYPE_STRING) {
3018 CharSequence cs = v.coerceToString();
Dianne Hackborn854060a2009-07-09 18:14:31 -07003019 data.putString(name, cs != null ? cs.toString().intern() : null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003020 } else if (v.type == TypedValue.TYPE_INT_BOOLEAN) {
3021 data.putBoolean(name, v.data != 0);
3022 } else if (v.type >= TypedValue.TYPE_FIRST_INT
3023 && v.type <= TypedValue.TYPE_LAST_INT) {
3024 data.putInt(name, v.data);
3025 } else if (v.type == TypedValue.TYPE_FLOAT) {
3026 data.putFloat(name, v.getFloat());
3027 } else {
3028 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07003029 Slog.w(TAG, "<meta-data> only supports string, integer, float, color, boolean, and resource reference types: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07003030 + parser.getName() + " at " + mArchiveSourcePath + " "
3031 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003032 } else {
3033 outError[0] = "<meta-data> only supports string, integer, float, color, boolean, and resource reference types";
3034 data = null;
3035 }
3036 }
3037 } else {
3038 outError[0] = "<meta-data> requires an android:value or android:resource attribute";
3039 data = null;
3040 }
3041 }
3042
3043 sa.recycle();
3044
3045 XmlUtils.skipCurrentTag(parser);
3046
3047 return data;
3048 }
3049
Kenny Root05ca4c92011-09-15 10:36:25 -07003050 private static VerifierInfo parseVerifier(Resources res, XmlPullParser parser,
3051 AttributeSet attrs, int flags, String[] outError) throws XmlPullParserException,
3052 IOException {
3053 final TypedArray sa = res.obtainAttributes(attrs,
3054 com.android.internal.R.styleable.AndroidManifestPackageVerifier);
3055
3056 final String packageName = sa.getNonResourceString(
3057 com.android.internal.R.styleable.AndroidManifestPackageVerifier_name);
3058
3059 final String encodedPublicKey = sa.getNonResourceString(
3060 com.android.internal.R.styleable.AndroidManifestPackageVerifier_publicKey);
3061
3062 sa.recycle();
3063
3064 if (packageName == null || packageName.length() == 0) {
3065 Slog.i(TAG, "verifier package name was null; skipping");
3066 return null;
3067 } else if (encodedPublicKey == null) {
3068 Slog.i(TAG, "verifier " + packageName + " public key was null; skipping");
3069 }
3070
3071 EncodedKeySpec keySpec;
3072 try {
3073 final byte[] encoded = Base64.decode(encodedPublicKey, Base64.DEFAULT);
3074 keySpec = new X509EncodedKeySpec(encoded);
3075 } catch (IllegalArgumentException e) {
3076 Slog.i(TAG, "Could not parse verifier " + packageName + " public key; invalid Base64");
3077 return null;
3078 }
3079
3080 /* First try the key as an RSA key. */
3081 try {
3082 final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
3083 final PublicKey publicKey = keyFactory.generatePublic(keySpec);
3084 return new VerifierInfo(packageName, publicKey);
3085 } catch (NoSuchAlgorithmException e) {
3086 Log.wtf(TAG, "Could not parse public key because RSA isn't included in build");
3087 return null;
3088 } catch (InvalidKeySpecException e) {
3089 // Not a RSA public key.
3090 }
3091
3092 /* Now try it as a DSA key. */
3093 try {
3094 final KeyFactory keyFactory = KeyFactory.getInstance("DSA");
3095 final PublicKey publicKey = keyFactory.generatePublic(keySpec);
3096 return new VerifierInfo(packageName, publicKey);
3097 } catch (NoSuchAlgorithmException e) {
3098 Log.wtf(TAG, "Could not parse public key because DSA isn't included in build");
3099 return null;
3100 } catch (InvalidKeySpecException e) {
3101 // Not a DSA public key.
3102 }
3103
3104 return null;
3105 }
3106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 private static final String ANDROID_RESOURCES
3108 = "http://schemas.android.com/apk/res/android";
3109
3110 private boolean parseIntent(Resources res,
3111 XmlPullParser parser, AttributeSet attrs, int flags,
3112 IntentInfo outInfo, String[] outError, boolean isActivity)
3113 throws XmlPullParserException, IOException {
3114
3115 TypedArray sa = res.obtainAttributes(attrs,
3116 com.android.internal.R.styleable.AndroidManifestIntentFilter);
3117
3118 int priority = sa.getInt(
3119 com.android.internal.R.styleable.AndroidManifestIntentFilter_priority, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 outInfo.setPriority(priority);
Kenny Root502e9a42011-01-10 13:48:15 -08003121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003122 TypedValue v = sa.peekValue(
3123 com.android.internal.R.styleable.AndroidManifestIntentFilter_label);
3124 if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
3125 outInfo.nonLocalizedLabel = v.coerceToString();
3126 }
3127
3128 outInfo.icon = sa.getResourceId(
3129 com.android.internal.R.styleable.AndroidManifestIntentFilter_icon, 0);
Adam Powell81cd2e92010-04-21 16:35:18 -07003130
3131 outInfo.logo = sa.getResourceId(
3132 com.android.internal.R.styleable.AndroidManifestIntentFilter_logo, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133
3134 sa.recycle();
3135
3136 int outerDepth = parser.getDepth();
3137 int type;
Kenny Rootd2d29252011-08-08 11:27:57 -07003138 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
3139 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
3140 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003141 continue;
3142 }
3143
3144 String nodeName = parser.getName();
3145 if (nodeName.equals("action")) {
3146 String value = attrs.getAttributeValue(
3147 ANDROID_RESOURCES, "name");
3148 if (value == null || value == "") {
3149 outError[0] = "No value supplied for <android:name>";
3150 return false;
3151 }
3152 XmlUtils.skipCurrentTag(parser);
3153
3154 outInfo.addAction(value);
3155 } else if (nodeName.equals("category")) {
3156 String value = attrs.getAttributeValue(
3157 ANDROID_RESOURCES, "name");
3158 if (value == null || value == "") {
3159 outError[0] = "No value supplied for <android:name>";
3160 return false;
3161 }
3162 XmlUtils.skipCurrentTag(parser);
3163
3164 outInfo.addCategory(value);
3165
3166 } else if (nodeName.equals("data")) {
3167 sa = res.obtainAttributes(attrs,
3168 com.android.internal.R.styleable.AndroidManifestData);
3169
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003170 String str = sa.getNonConfigurationString(
3171 com.android.internal.R.styleable.AndroidManifestData_mimeType, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 if (str != null) {
3173 try {
3174 outInfo.addDataType(str);
3175 } catch (IntentFilter.MalformedMimeTypeException e) {
3176 outError[0] = e.toString();
3177 sa.recycle();
3178 return false;
3179 }
3180 }
3181
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003182 str = sa.getNonConfigurationString(
3183 com.android.internal.R.styleable.AndroidManifestData_scheme, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 if (str != null) {
3185 outInfo.addDataScheme(str);
3186 }
3187
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003188 String host = sa.getNonConfigurationString(
3189 com.android.internal.R.styleable.AndroidManifestData_host, 0);
3190 String port = sa.getNonConfigurationString(
3191 com.android.internal.R.styleable.AndroidManifestData_port, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003192 if (host != null) {
3193 outInfo.addDataAuthority(host, port);
3194 }
3195
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003196 str = sa.getNonConfigurationString(
3197 com.android.internal.R.styleable.AndroidManifestData_path, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003198 if (str != null) {
3199 outInfo.addDataPath(str, PatternMatcher.PATTERN_LITERAL);
3200 }
3201
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003202 str = sa.getNonConfigurationString(
3203 com.android.internal.R.styleable.AndroidManifestData_pathPrefix, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003204 if (str != null) {
3205 outInfo.addDataPath(str, PatternMatcher.PATTERN_PREFIX);
3206 }
3207
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003208 str = sa.getNonConfigurationString(
3209 com.android.internal.R.styleable.AndroidManifestData_pathPattern, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003210 if (str != null) {
3211 outInfo.addDataPath(str, PatternMatcher.PATTERN_SIMPLE_GLOB);
3212 }
3213
3214 sa.recycle();
3215 XmlUtils.skipCurrentTag(parser);
3216 } else if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07003217 Slog.w(TAG, "Unknown element under <intent-filter>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07003218 + parser.getName() + " at " + mArchiveSourcePath + " "
3219 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 XmlUtils.skipCurrentTag(parser);
3221 } else {
3222 outError[0] = "Bad element under <intent-filter>: " + parser.getName();
3223 return false;
3224 }
3225 }
3226
3227 outInfo.hasDefault = outInfo.hasCategory(Intent.CATEGORY_DEFAULT);
Kenny Rootd2d29252011-08-08 11:27:57 -07003228
3229 if (DEBUG_PARSER) {
3230 final StringBuilder cats = new StringBuilder("Intent d=");
3231 cats.append(outInfo.hasDefault);
3232 cats.append(", cat=");
3233
3234 final Iterator<String> it = outInfo.categoriesIterator();
3235 if (it != null) {
3236 while (it.hasNext()) {
3237 cats.append(' ');
3238 cats.append(it.next());
3239 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 }
Kenny Rootd2d29252011-08-08 11:27:57 -07003241 Slog.d(TAG, cats.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 }
3243
3244 return true;
3245 }
3246
3247 public final static class Package {
Amith Yamasani0ac1fc92013-03-27 18:56:08 -07003248
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003249 public String packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003250
3251 // For now we only support one application per package.
3252 public final ApplicationInfo applicationInfo = new ApplicationInfo();
3253
3254 public final ArrayList<Permission> permissions = new ArrayList<Permission>(0);
3255 public final ArrayList<PermissionGroup> permissionGroups = new ArrayList<PermissionGroup>(0);
3256 public final ArrayList<Activity> activities = new ArrayList<Activity>(0);
3257 public final ArrayList<Activity> receivers = new ArrayList<Activity>(0);
3258 public final ArrayList<Provider> providers = new ArrayList<Provider>(0);
3259 public final ArrayList<Service> services = new ArrayList<Service>(0);
3260 public final ArrayList<Instrumentation> instrumentation = new ArrayList<Instrumentation>(0);
3261
3262 public final ArrayList<String> requestedPermissions = new ArrayList<String>();
Dianne Hackborne639da72012-02-21 15:11:13 -08003263 public final ArrayList<Boolean> requestedPermissionsRequired = new ArrayList<Boolean>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264
Dianne Hackborn854060a2009-07-09 18:14:31 -07003265 public ArrayList<String> protectedBroadcasts;
Dianne Hackbornc895be72013-03-11 17:48:43 -07003266
3267 public ArrayList<String> libraryNames = null;
Dianne Hackborn49237342009-08-27 20:08:01 -07003268 public ArrayList<String> usesLibraries = null;
3269 public ArrayList<String> usesOptionalLibraries = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003270 public String[] usesLibraryFiles = null;
3271
Dianne Hackbornc1552392010-03-03 16:19:01 -08003272 public ArrayList<String> mOriginalPackages = null;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003273 public String mRealPackage = null;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08003274 public ArrayList<String> mAdoptPermissions = null;
3275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 // We store the application meta-data independently to avoid multiple unwanted references
3277 public Bundle mAppMetaData = null;
3278
3279 // If this is a 3rd party app, this is the path of the zip file.
3280 public String mPath;
3281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282 // The version code declared for this package.
3283 public int mVersionCode;
3284
3285 // The version name declared for this package.
3286 public String mVersionName;
3287
3288 // The shared user id that this package wants to use.
3289 public String mSharedUserId;
3290
3291 // The shared user label that this package wants to use.
3292 public int mSharedUserLabel;
3293
3294 // Signatures that were read from the package.
3295 public Signature mSignatures[];
3296
3297 // For use by package manager service for quick lookup of
3298 // preferred up order.
3299 public int mPreferredOrder = 0;
3300
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07003301 // For use by the package manager to keep track of the path to the
3302 // file an app came from.
3303 public String mScanPath;
3304
3305 // For use by package manager to keep track of where it has done dexopt.
3306 public boolean mDidDexOpt;
3307
Amith Yamasani13593602012-03-22 16:16:17 -07003308 // // User set enabled state.
3309 // public int mSetEnabled = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
3310 //
3311 // // Whether the package has been stopped.
3312 // public boolean mSetStopped = false;
Dianne Hackborne7f97212011-02-24 14:40:20 -08003313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003314 // Additional data supplied by callers.
3315 public Object mExtras;
Kenny Rootdeb11262010-08-02 11:36:21 -07003316
3317 // Whether an operation is currently pending on this package
3318 public boolean mOperationPending;
3319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003320 /*
3321 * Applications hardware preferences
3322 */
3323 public final ArrayList<ConfigurationInfo> configPreferences =
3324 new ArrayList<ConfigurationInfo>();
3325
Dianne Hackborn49237342009-08-27 20:08:01 -07003326 /*
3327 * Applications requested features
3328 */
3329 public ArrayList<FeatureInfo> reqFeatures = null;
3330
Suchi Amalapurapu117818e2010-02-09 03:45:40 -08003331 public int installLocation;
3332
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08003333 /* An app that's required for all users and cannot be uninstalled for a user */
3334 public boolean mRequiredForAllUsers;
3335
Amith Yamasani0ac1fc92013-03-27 18:56:08 -07003336 /* The restricted account authenticator type that is used by this application */
3337 public String mRestrictedAccountType;
3338
Kenny Rootbcc954d2011-08-08 16:19:08 -07003339 /**
3340 * Digest suitable for comparing whether this package's manifest is the
3341 * same as another.
3342 */
3343 public ManifestDigest manifestDigest;
3344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 public Package(String _name) {
3346 packageName = _name;
3347 applicationInfo.packageName = _name;
3348 applicationInfo.uid = -1;
3349 }
3350
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003351 public void setPackageName(String newName) {
3352 packageName = newName;
3353 applicationInfo.packageName = newName;
3354 for (int i=permissions.size()-1; i>=0; i--) {
3355 permissions.get(i).setPackageName(newName);
3356 }
3357 for (int i=permissionGroups.size()-1; i>=0; i--) {
3358 permissionGroups.get(i).setPackageName(newName);
3359 }
3360 for (int i=activities.size()-1; i>=0; i--) {
3361 activities.get(i).setPackageName(newName);
3362 }
3363 for (int i=receivers.size()-1; i>=0; i--) {
3364 receivers.get(i).setPackageName(newName);
3365 }
3366 for (int i=providers.size()-1; i>=0; i--) {
3367 providers.get(i).setPackageName(newName);
3368 }
3369 for (int i=services.size()-1; i>=0; i--) {
3370 services.get(i).setPackageName(newName);
3371 }
3372 for (int i=instrumentation.size()-1; i>=0; i--) {
3373 instrumentation.get(i).setPackageName(newName);
3374 }
3375 }
Dianne Hackborn65696252012-03-05 18:49:21 -08003376
3377 public boolean hasComponentClassName(String name) {
3378 for (int i=activities.size()-1; i>=0; i--) {
3379 if (name.equals(activities.get(i).className)) {
3380 return true;
3381 }
3382 }
3383 for (int i=receivers.size()-1; i>=0; i--) {
3384 if (name.equals(receivers.get(i).className)) {
3385 return true;
3386 }
3387 }
3388 for (int i=providers.size()-1; i>=0; i--) {
3389 if (name.equals(providers.get(i).className)) {
3390 return true;
3391 }
3392 }
3393 for (int i=services.size()-1; i>=0; i--) {
3394 if (name.equals(services.get(i).className)) {
3395 return true;
3396 }
3397 }
3398 for (int i=instrumentation.size()-1; i>=0; i--) {
3399 if (name.equals(instrumentation.get(i).className)) {
3400 return true;
3401 }
3402 }
3403 return false;
3404 }
3405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003406 public String toString() {
3407 return "Package{"
3408 + Integer.toHexString(System.identityHashCode(this))
3409 + " " + packageName + "}";
3410 }
3411 }
3412
3413 public static class Component<II extends IntentInfo> {
3414 public final Package owner;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003415 public final ArrayList<II> intents;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003416 public final String className;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003417 public Bundle metaData;
3418
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003419 ComponentName componentName;
3420 String componentShortName;
3421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003422 public Component(Package _owner) {
3423 owner = _owner;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003424 intents = null;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003425 className = null;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003426 }
3427
3428 public Component(final ParsePackageItemArgs args, final PackageItemInfo outInfo) {
3429 owner = args.owner;
3430 intents = new ArrayList<II>(0);
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003431 String name = args.sa.getNonConfigurationString(args.nameRes, 0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003432 if (name == null) {
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003433 className = null;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003434 args.outError[0] = args.tag + " does not specify android:name";
3435 return;
3436 }
3437
3438 outInfo.name
3439 = buildClassName(owner.applicationInfo.packageName, name, args.outError);
3440 if (outInfo.name == null) {
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003441 className = null;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003442 args.outError[0] = args.tag + " does not have valid android:name";
3443 return;
3444 }
3445
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003446 className = outInfo.name;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003447
3448 int iconVal = args.sa.getResourceId(args.iconRes, 0);
3449 if (iconVal != 0) {
3450 outInfo.icon = iconVal;
3451 outInfo.nonLocalizedLabel = null;
3452 }
Adam Powell81cd2e92010-04-21 16:35:18 -07003453
3454 int logoVal = args.sa.getResourceId(args.logoRes, 0);
3455 if (logoVal != 0) {
3456 outInfo.logo = logoVal;
3457 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003458
3459 TypedValue v = args.sa.peekValue(args.labelRes);
3460 if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
3461 outInfo.nonLocalizedLabel = v.coerceToString();
3462 }
3463
3464 outInfo.packageName = owner.packageName;
3465 }
3466
3467 public Component(final ParseComponentArgs args, final ComponentInfo outInfo) {
3468 this(args, (PackageItemInfo)outInfo);
3469 if (args.outError[0] != null) {
3470 return;
3471 }
3472
3473 if (args.processRes != 0) {
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07003474 CharSequence pname;
3475 if (owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.FROYO) {
3476 pname = args.sa.getNonConfigurationString(args.processRes, 0);
3477 } else {
3478 // Some older apps have been seen to use a resource reference
3479 // here that on older builds was ignored (with a warning). We
3480 // need to continue to do this for them so they don't break.
3481 pname = args.sa.getNonResourceString(args.processRes);
3482 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003483 outInfo.processName = buildProcessName(owner.applicationInfo.packageName,
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07003484 owner.applicationInfo.processName, pname,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003485 args.flags, args.sepProcesses, args.outError);
3486 }
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08003487
3488 if (args.descriptionRes != 0) {
3489 outInfo.descriptionRes = args.sa.getResourceId(args.descriptionRes, 0);
3490 }
3491
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003492 outInfo.enabled = args.sa.getBoolean(args.enabledRes, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 }
3494
3495 public Component(Component<II> clone) {
3496 owner = clone.owner;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003497 intents = clone.intents;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003498 className = clone.className;
3499 componentName = clone.componentName;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003500 componentShortName = clone.componentShortName;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003501 }
3502
3503 public ComponentName getComponentName() {
3504 if (componentName != null) {
3505 return componentName;
3506 }
3507 if (className != null) {
3508 componentName = new ComponentName(owner.applicationInfo.packageName,
3509 className);
3510 }
3511 return componentName;
3512 }
3513
3514 public String getComponentShortName() {
3515 if (componentShortName != null) {
3516 return componentShortName;
3517 }
3518 ComponentName component = getComponentName();
3519 if (component != null) {
3520 componentShortName = component.flattenToShortString();
3521 }
3522 return componentShortName;
3523 }
3524
3525 public void setPackageName(String packageName) {
3526 componentName = null;
3527 componentShortName = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003528 }
3529 }
3530
3531 public final static class Permission extends Component<IntentInfo> {
3532 public final PermissionInfo info;
3533 public boolean tree;
3534 public PermissionGroup group;
3535
3536 public Permission(Package _owner) {
3537 super(_owner);
3538 info = new PermissionInfo();
3539 }
3540
3541 public Permission(Package _owner, PermissionInfo _info) {
3542 super(_owner);
3543 info = _info;
3544 }
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003545
3546 public void setPackageName(String packageName) {
3547 super.setPackageName(packageName);
3548 info.packageName = packageName;
3549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550
3551 public String toString() {
3552 return "Permission{"
3553 + Integer.toHexString(System.identityHashCode(this))
3554 + " " + info.name + "}";
3555 }
3556 }
3557
3558 public final static class PermissionGroup extends Component<IntentInfo> {
3559 public final PermissionGroupInfo info;
3560
3561 public PermissionGroup(Package _owner) {
3562 super(_owner);
3563 info = new PermissionGroupInfo();
3564 }
3565
3566 public PermissionGroup(Package _owner, PermissionGroupInfo _info) {
3567 super(_owner);
3568 info = _info;
3569 }
3570
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003571 public void setPackageName(String packageName) {
3572 super.setPackageName(packageName);
3573 info.packageName = packageName;
3574 }
3575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003576 public String toString() {
3577 return "PermissionGroup{"
3578 + Integer.toHexString(System.identityHashCode(this))
3579 + " " + info.name + "}";
3580 }
3581 }
3582
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003583 private static boolean copyNeeded(int flags, Package p,
3584 PackageUserState state, Bundle metaData, int userId) {
3585 if (userId != 0) {
3586 // We always need to copy for other users, since we need
3587 // to fix up the uid.
3588 return true;
3589 }
3590 if (state.enabled != PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
3591 boolean enabled = state.enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
Dianne Hackborn46730fc2010-07-24 16:32:42 -07003592 if (p.applicationInfo.enabled != enabled) {
3593 return true;
3594 }
3595 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003596 if (!state.installed) {
3597 return true;
3598 }
3599 if (state.stopped) {
3600 return true;
3601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003602 if ((flags & PackageManager.GET_META_DATA) != 0
3603 && (metaData != null || p.mAppMetaData != null)) {
3604 return true;
3605 }
3606 if ((flags & PackageManager.GET_SHARED_LIBRARY_FILES) != 0
3607 && p.usesLibraryFiles != null) {
3608 return true;
3609 }
3610 return false;
3611 }
3612
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003613 public static ApplicationInfo generateApplicationInfo(Package p, int flags,
3614 PackageUserState state) {
3615 return generateApplicationInfo(p, flags, state, UserHandle.getCallingUserId());
Amith Yamasani742a6712011-05-04 14:49:28 -07003616 }
3617
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003618 private static void updateApplicationInfo(ApplicationInfo ai, int flags,
3619 PackageUserState state) {
3620 // CompatibilityMode is global state.
3621 if (!sCompatibilityModeEnabled) {
3622 ai.disableCompatibilityMode();
3623 }
3624 if (state.installed) {
3625 ai.flags |= ApplicationInfo.FLAG_INSTALLED;
3626 } else {
3627 ai.flags &= ~ApplicationInfo.FLAG_INSTALLED;
3628 }
3629 if (state.enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
3630 ai.enabled = true;
3631 } else if (state.enabled == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
3632 ai.enabled = (flags&PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS) != 0;
3633 } else if (state.enabled == PackageManager.COMPONENT_ENABLED_STATE_DISABLED
3634 || state.enabled == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER) {
3635 ai.enabled = false;
3636 }
3637 ai.enabledSetting = state.enabled;
3638 }
3639
Amith Yamasani13593602012-03-22 16:16:17 -07003640 public static ApplicationInfo generateApplicationInfo(Package p, int flags,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003641 PackageUserState state, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003642 if (p == null) return null;
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003643 if (!checkUseInstalled(flags, state)) {
3644 return null;
3645 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003646 if (!copyNeeded(flags, p, state, null, userId)
3647 && ((flags&PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS) == 0
3648 || state.enabled != PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED)) {
3649 // In this case it is safe to directly modify the internal ApplicationInfo state:
3650 // - CompatibilityMode is global state, so will be the same for every call.
3651 // - We only come in to here if the app should reported as installed; this is the
3652 // default state, and we will do a copy otherwise.
3653 // - The enable state will always be reported the same for the application across
3654 // calls; the only exception is for the UNTIL_USED mode, and in that case we will
3655 // be doing a copy.
3656 updateApplicationInfo(p.applicationInfo, flags, state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003657 return p.applicationInfo;
3658 }
3659
3660 // Make shallow copy so we can store the metadata/libraries safely
3661 ApplicationInfo ai = new ApplicationInfo(p.applicationInfo);
Amith Yamasani742a6712011-05-04 14:49:28 -07003662 if (userId != 0) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07003663 ai.uid = UserHandle.getUid(userId, ai.uid);
Amith Yamasani742a6712011-05-04 14:49:28 -07003664 ai.dataDir = PackageManager.getDataDirForUser(userId, ai.packageName);
3665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003666 if ((flags & PackageManager.GET_META_DATA) != 0) {
3667 ai.metaData = p.mAppMetaData;
3668 }
3669 if ((flags & PackageManager.GET_SHARED_LIBRARY_FILES) != 0) {
3670 ai.sharedLibraryFiles = p.usesLibraryFiles;
3671 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003672 if (state.stopped) {
Amith Yamasania4a54e22012-04-16 15:44:19 -07003673 ai.flags |= ApplicationInfo.FLAG_STOPPED;
Dianne Hackborne7f97212011-02-24 14:40:20 -08003674 } else {
Amith Yamasania4a54e22012-04-16 15:44:19 -07003675 ai.flags &= ~ApplicationInfo.FLAG_STOPPED;
Dianne Hackborne7f97212011-02-24 14:40:20 -08003676 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003677 updateApplicationInfo(ai, flags, state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003678 return ai;
3679 }
3680
3681 public static final PermissionInfo generatePermissionInfo(
3682 Permission p, int flags) {
3683 if (p == null) return null;
3684 if ((flags&PackageManager.GET_META_DATA) == 0) {
3685 return p.info;
3686 }
3687 PermissionInfo pi = new PermissionInfo(p.info);
3688 pi.metaData = p.metaData;
3689 return pi;
3690 }
3691
3692 public static final PermissionGroupInfo generatePermissionGroupInfo(
3693 PermissionGroup pg, int flags) {
3694 if (pg == null) return null;
3695 if ((flags&PackageManager.GET_META_DATA) == 0) {
3696 return pg.info;
3697 }
3698 PermissionGroupInfo pgi = new PermissionGroupInfo(pg.info);
3699 pgi.metaData = pg.metaData;
3700 return pgi;
3701 }
3702
3703 public final static class Activity extends Component<ActivityIntentInfo> {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003704 public final ActivityInfo info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003705
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003706 public Activity(final ParseComponentArgs args, final ActivityInfo _info) {
3707 super(args, _info);
3708 info = _info;
3709 info.applicationInfo = args.owner.applicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003710 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003711
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003712 public void setPackageName(String packageName) {
3713 super.setPackageName(packageName);
3714 info.packageName = packageName;
3715 }
3716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003717 public String toString() {
3718 return "Activity{"
3719 + Integer.toHexString(System.identityHashCode(this))
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003720 + " " + getComponentShortName() + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003721 }
3722 }
3723
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003724 public static final ActivityInfo generateActivityInfo(Activity a, int flags,
3725 PackageUserState state, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003726 if (a == null) return null;
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003727 if (!checkUseInstalled(flags, state)) {
3728 return null;
3729 }
3730 if (!copyNeeded(flags, a.owner, state, a.metaData, userId)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003731 return a.info;
3732 }
3733 // Make shallow copies so we can store the metadata safely
3734 ActivityInfo ai = new ActivityInfo(a.info);
3735 ai.metaData = a.metaData;
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003736 ai.applicationInfo = generateApplicationInfo(a.owner, flags, state, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003737 return ai;
3738 }
3739
3740 public final static class Service extends Component<ServiceIntentInfo> {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003741 public final ServiceInfo info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003742
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003743 public Service(final ParseComponentArgs args, final ServiceInfo _info) {
3744 super(args, _info);
3745 info = _info;
3746 info.applicationInfo = args.owner.applicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003747 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003748
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003749 public void setPackageName(String packageName) {
3750 super.setPackageName(packageName);
3751 info.packageName = packageName;
3752 }
3753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003754 public String toString() {
3755 return "Service{"
3756 + Integer.toHexString(System.identityHashCode(this))
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003757 + " " + getComponentShortName() + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003758 }
3759 }
3760
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003761 public static final ServiceInfo generateServiceInfo(Service s, int flags,
3762 PackageUserState state, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003763 if (s == null) return null;
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003764 if (!checkUseInstalled(flags, state)) {
3765 return null;
3766 }
3767 if (!copyNeeded(flags, s.owner, state, s.metaData, userId)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003768 return s.info;
3769 }
3770 // Make shallow copies so we can store the metadata safely
3771 ServiceInfo si = new ServiceInfo(s.info);
3772 si.metaData = s.metaData;
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003773 si.applicationInfo = generateApplicationInfo(s.owner, flags, state, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003774 return si;
3775 }
3776
3777 public final static class Provider extends Component {
3778 public final ProviderInfo info;
3779 public boolean syncable;
3780
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003781 public Provider(final ParseComponentArgs args, final ProviderInfo _info) {
3782 super(args, _info);
3783 info = _info;
3784 info.applicationInfo = args.owner.applicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003785 syncable = false;
3786 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003788 public Provider(Provider existingProvider) {
3789 super(existingProvider);
3790 this.info = existingProvider.info;
3791 this.syncable = existingProvider.syncable;
3792 }
3793
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003794 public void setPackageName(String packageName) {
3795 super.setPackageName(packageName);
3796 info.packageName = packageName;
3797 }
3798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003799 public String toString() {
3800 return "Provider{"
3801 + Integer.toHexString(System.identityHashCode(this))
3802 + " " + info.name + "}";
3803 }
3804 }
3805
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003806 public static final ProviderInfo generateProviderInfo(Provider p, int flags,
3807 PackageUserState state, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003808 if (p == null) return null;
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003809 if (!checkUseInstalled(flags, state)) {
3810 return null;
3811 }
3812 if (!copyNeeded(flags, p.owner, state, p.metaData, userId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003813 && ((flags & PackageManager.GET_URI_PERMISSION_PATTERNS) != 0
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003814 || p.info.uriPermissionPatterns == null)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003815 return p.info;
3816 }
3817 // Make shallow copies so we can store the metadata safely
3818 ProviderInfo pi = new ProviderInfo(p.info);
3819 pi.metaData = p.metaData;
3820 if ((flags & PackageManager.GET_URI_PERMISSION_PATTERNS) == 0) {
3821 pi.uriPermissionPatterns = null;
3822 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003823 pi.applicationInfo = generateApplicationInfo(p.owner, flags, state, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003824 return pi;
3825 }
3826
3827 public final static class Instrumentation extends Component {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003828 public final InstrumentationInfo info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003829
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003830 public Instrumentation(final ParsePackageItemArgs args, final InstrumentationInfo _info) {
3831 super(args, _info);
3832 info = _info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003833 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003834
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003835 public void setPackageName(String packageName) {
3836 super.setPackageName(packageName);
3837 info.packageName = packageName;
3838 }
3839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003840 public String toString() {
3841 return "Instrumentation{"
3842 + Integer.toHexString(System.identityHashCode(this))
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003843 + " " + getComponentShortName() + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003844 }
3845 }
3846
3847 public static final InstrumentationInfo generateInstrumentationInfo(
3848 Instrumentation i, int flags) {
3849 if (i == null) return null;
3850 if ((flags&PackageManager.GET_META_DATA) == 0) {
3851 return i.info;
3852 }
3853 InstrumentationInfo ii = new InstrumentationInfo(i.info);
3854 ii.metaData = i.metaData;
3855 return ii;
3856 }
3857
3858 public static class IntentInfo extends IntentFilter {
3859 public boolean hasDefault;
3860 public int labelRes;
3861 public CharSequence nonLocalizedLabel;
3862 public int icon;
Adam Powell81cd2e92010-04-21 16:35:18 -07003863 public int logo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003864 }
3865
3866 public final static class ActivityIntentInfo extends IntentInfo {
3867 public final Activity activity;
3868
3869 public ActivityIntentInfo(Activity _activity) {
3870 activity = _activity;
3871 }
3872
3873 public String toString() {
3874 return "ActivityIntentInfo{"
3875 + Integer.toHexString(System.identityHashCode(this))
3876 + " " + activity.info.name + "}";
3877 }
3878 }
3879
3880 public final static class ServiceIntentInfo extends IntentInfo {
3881 public final Service service;
3882
3883 public ServiceIntentInfo(Service _service) {
3884 service = _service;
3885 }
3886
3887 public String toString() {
3888 return "ServiceIntentInfo{"
3889 + Integer.toHexString(System.identityHashCode(this))
3890 + " " + service.info.name + "}";
3891 }
3892 }
Mitsuru Oshima69fff4a2009-07-21 09:51:05 -07003893
3894 /**
3895 * @hide
3896 */
3897 public static void setCompatibilityModeEnabled(boolean compatibilityModeEnabled) {
3898 sCompatibilityModeEnabled = compatibilityModeEnabled;
3899 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003900}