blob: b7aecc9fbcbb790aec7b7559c1a5c36b172e6701 [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
Wale Ogunwaled26176f2016-01-25 20:04:04 -080019import com.android.internal.R;
20import com.android.internal.util.ArrayUtils;
21import com.android.internal.util.XmlUtils;
22
23import org.xmlpull.v1.XmlPullParser;
24import org.xmlpull.v1.XmlPullParserException;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -070025
Dianne Hackborn852975d2014-08-22 17:42:43 -070026import android.app.ActivityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.content.ComponentName;
28import android.content.Intent;
29import android.content.IntentFilter;
30import android.content.res.AssetManager;
31import android.content.res.Configuration;
32import android.content.res.Resources;
33import android.content.res.TypedArray;
34import android.content.res.XmlResourceParser;
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -070035import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.Bundle;
Jeff Sharkey4f5e8b32015-06-11 19:13:37 -070037import android.os.FileUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.os.PatternMatcher;
Todd Kennedy27c24fb2015-09-17 16:49:25 -070039import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070040import android.os.UserHandle;
Jeff Sharkeyc4858a22014-06-16 10:51:20 -070041import android.text.TextUtils;
Jeff Sharkey275e0852014-06-17 18:18:49 -070042import android.util.ArrayMap;
43import android.util.ArraySet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.util.AttributeSet;
Kenny Root05ca4c92011-09-15 10:36:25 -070045import android.util.Base64;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.util.DisplayMetrics;
Kenny Root05ca4c92011-09-15 10:36:25 -070047import android.util.Log;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -070048import android.util.Pair;
Kenny Rootd2d29252011-08-08 11:27:57 -070049import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.TypedValue;
Alex Klyubine4157182016-01-05 13:27:05 -080051import android.util.apk.ApkSignatureSchemeV2Verifier;
Jeff Sharkeyc5967e92016-01-07 18:50:29 -070052import android.util.jar.StrictJarFile;
Filip Gruszczynski848fe6b2015-08-19 13:32:41 -070053import android.view.Gravity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054
55import java.io.File;
56import java.io.IOException;
57import java.io.InputStream;
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -070058import java.io.PrintWriter;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -070059import java.security.GeneralSecurityException;
Kenny Root05ca4c92011-09-15 10:36:25 -070060import java.security.KeyFactory;
61import java.security.NoSuchAlgorithmException;
62import java.security.PublicKey;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import java.security.cert.Certificate;
64import java.security.cert.CertificateEncodingException;
Kenny Root05ca4c92011-09-15 10:36:25 -070065import java.security.spec.EncodedKeySpec;
66import java.security.spec.InvalidKeySpecException;
67import java.security.spec.X509EncodedKeySpec;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import java.util.ArrayList;
Dianne Hackbornffcda102014-04-24 13:06:27 -070069import java.util.Arrays;
Jeff Sharkey275e0852014-06-17 18:18:49 -070070import java.util.Collections;
71import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import java.util.Iterator;
Kenny Root05ca4c92011-09-15 10:36:25 -070073import java.util.List;
Geremy Condraf1bcca82013-01-07 22:35:24 -080074import java.util.Set;
Jeff Sharkey275e0852014-06-17 18:18:49 -070075import java.util.concurrent.atomic.AtomicReference;
Kenny Root6c918ce2013-04-02 14:04:24 -070076import java.util.zip.ZipEntry;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077
Wale Ogunwaled26176f2016-01-25 20:04:04 -080078import libcore.io.IoUtils;
79
80import static android.content.pm.ActivityInfo.FLAG_ALWAYS_FOCUSABLE;
81import static android.content.pm.ActivityInfo.FLAG_IMMERSIVE;
Wale Ogunwaled829d362016-02-10 19:24:49 -080082import static android.content.pm.ActivityInfo.RESIZE_MODE_FORCE_RESIZEABLE;
Wale Ogunwaled26176f2016-01-25 20:04:04 -080083import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE;
84import static android.content.pm.ActivityInfo.RESIZE_MODE_RESIZEABLE_AND_PIPABLE;
85import static android.content.pm.ActivityInfo.RESIZE_MODE_UNRESIZEABLE;
86import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Rubin Xubf67c9c2016-04-20 17:09:38 +010087import static android.content.pm.ApplicationInfo.FLAG_SUSPENDED;
Wale Ogunwale6afdf912016-01-30 13:01:33 -080088import static android.content.pm.ApplicationInfo.PRIVATE_FLAG_RESIZEABLE_ACTIVITIES;
Wale Ogunwaled26176f2016-01-25 20:04:04 -080089import static android.content.pm.PackageManager.INSTALL_PARSE_FAILED_BAD_MANIFEST;
90import static android.content.pm.PackageManager.INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME;
91import static android.content.pm.PackageManager.INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING;
92import static android.content.pm.PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;
93import static android.content.pm.PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
94import static android.content.pm.PackageManager.INSTALL_PARSE_FAILED_NOT_APK;
95import static android.content.pm.PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
96import static android.content.pm.PackageManager.INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION;
97import static android.os.Trace.TRACE_TAG_PACKAGE_MANAGER;
98
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099/**
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700100 * Parser for package files (APKs) on disk. This supports apps packaged either
101 * as a single "monolithic" APK, or apps packaged as a "cluster" of multiple
102 * APKs in a single directory.
103 * <p>
104 * Apps packaged as multiple APKs always consist of a single "base" APK (with a
105 * {@code null} split name) and zero or more "split" APKs (with unique split
106 * names). Any subset of those split APKs are a valid install, as long as the
107 * following constraints are met:
108 * <ul>
109 * <li>All APKs must have the exact same package name, version code, and signing
110 * certificates.
111 * <li>All APKs must have unique split names.
112 * <li>All installations must contain a single base APK.
113 * </ul>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 *
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700115 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 */
117public class PackageParser {
Kenny Rootd2d29252011-08-08 11:27:57 -0700118 private static final boolean DEBUG_JAR = false;
119 private static final boolean DEBUG_PARSER = false;
120 private static final boolean DEBUG_BACKUP = false;
121
Svet Ganova7532cf2016-05-02 08:13:17 -0700122 private static final boolean MULTI_PACKAGE_APK_ENABLED = false;
Svet Ganov354cd3c2015-12-17 11:35:04 -0800123 private static final int MAX_PACKAGES_PER_APK = 5;
124
Todd Kennedy66c55532016-02-26 16:22:11 -0800125 public static final int APK_SIGNING_UNKNOWN = 0;
126 public static final int APK_SIGNING_V1 = 1;
127 public static final int APK_SIGNING_V2 = 2;
128
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700129 // TODO: switch outError users to PackageParserException
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700130 // TODO: refactor "codePath" to "apkPath"
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700131
Kenny Rootbcc954d2011-08-08 16:19:08 -0700132 /** File name in an APK for the Android manifest. */
133 private static final String ANDROID_MANIFEST_FILENAME = "AndroidManifest.xml";
134
Jeff Sharkeyb9f36742015-04-08 21:02:14 -0700135 /** Path prefix for apps on expanded storage */
136 private static final String MNT_EXPAND = "/mnt/expand/";
137
Svet Ganov354cd3c2015-12-17 11:35:04 -0800138 private static final String TAG_MANIFEST = "manifest";
139 private static final String TAG_APPLICATION = "application";
140 private static final String TAG_OVERLAY = "overlay";
141 private static final String TAG_KEY_SETS = "key-sets";
142 private static final String TAG_PERMISSION_GROUP = "permission-group";
143 private static final String TAG_PERMISSION = "permission";
144 private static final String TAG_PERMISSION_TREE = "permission-tree";
145 private static final String TAG_USES_PERMISSION = "uses-permission";
146 private static final String TAG_USES_PERMISSION_SDK_M = "uses-permission-sdk-m";
147 private static final String TAG_USES_PERMISSION_SDK_23 = "uses-permission-sdk-23";
148 private static final String TAG_USES_CONFIGURATION = "uses-configuration";
149 private static final String TAG_USES_FEATURE = "uses-feature";
150 private static final String TAG_FEATURE_GROUP = "feature-group";
151 private static final String TAG_USES_SDK = "uses-sdk";
152 private static final String TAG_SUPPORT_SCREENS = "supports-screens";
153 private static final String TAG_PROTECTED_BROADCAST = "protected-broadcast";
154 private static final String TAG_INSTRUMENTATION = "instrumentation";
155 private static final String TAG_ORIGINAL_PACKAGE = "original-package";
156 private static final String TAG_ADOPT_PERMISSIONS = "adopt-permissions";
157 private static final String TAG_USES_GL_TEXTURE = "uses-gl-texture";
158 private static final String TAG_COMPATIBLE_SCREENS = "compatible-screens";
159 private static final String TAG_SUPPORTS_INPUT = "supports-input";
160 private static final String TAG_EAT_COMMENT = "eat-comment";
161 private static final String TAG_PACKAGE = "package";
Todd Kennedyfdd241a2016-04-28 12:26:53 -0700162 private static final String TAG_RESTRICT_UPDATE = "restrict-update";
Svet Ganov354cd3c2015-12-17 11:35:04 -0800163
164 // These are the tags supported by child packages
165 private static final Set<String> CHILD_PACKAGE_TAGS = new ArraySet<>();
166 static {
167 CHILD_PACKAGE_TAGS.add(TAG_APPLICATION);
168 CHILD_PACKAGE_TAGS.add(TAG_USES_PERMISSION);
169 CHILD_PACKAGE_TAGS.add(TAG_USES_PERMISSION_SDK_M);
170 CHILD_PACKAGE_TAGS.add(TAG_USES_PERMISSION_SDK_23);
171 CHILD_PACKAGE_TAGS.add(TAG_USES_CONFIGURATION);
172 CHILD_PACKAGE_TAGS.add(TAG_USES_FEATURE);
173 CHILD_PACKAGE_TAGS.add(TAG_FEATURE_GROUP);
174 CHILD_PACKAGE_TAGS.add(TAG_USES_SDK);
175 CHILD_PACKAGE_TAGS.add(TAG_SUPPORT_SCREENS);
176 CHILD_PACKAGE_TAGS.add(TAG_INSTRUMENTATION);
177 CHILD_PACKAGE_TAGS.add(TAG_USES_GL_TEXTURE);
178 CHILD_PACKAGE_TAGS.add(TAG_COMPATIBLE_SCREENS);
179 CHILD_PACKAGE_TAGS.add(TAG_SUPPORTS_INPUT);
180 CHILD_PACKAGE_TAGS.add(TAG_EAT_COMMENT);
181 }
182
Dianne Hackborna96cbb42009-05-13 15:06:13 -0700183 /** @hide */
184 public static class NewPermissionInfo {
185 public final String name;
186 public final int sdkVersion;
187 public final int fileVersion;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700188
Dianne Hackborna96cbb42009-05-13 15:06:13 -0700189 public NewPermissionInfo(String name, int sdkVersion, int fileVersion) {
190 this.name = name;
191 this.sdkVersion = sdkVersion;
192 this.fileVersion = fileVersion;
193 }
194 }
Dianne Hackborn79245122012-03-12 10:51:26 -0700195
196 /** @hide */
197 public static class SplitPermissionInfo {
198 public final String rootPerm;
199 public final String[] newPerms;
Dianne Hackborn31b0e0e2012-04-05 19:33:30 -0700200 public final int targetSdk;
Dianne Hackborn79245122012-03-12 10:51:26 -0700201
Dianne Hackborn31b0e0e2012-04-05 19:33:30 -0700202 public SplitPermissionInfo(String rootPerm, String[] newPerms, int targetSdk) {
Dianne Hackborn79245122012-03-12 10:51:26 -0700203 this.rootPerm = rootPerm;
204 this.newPerms = newPerms;
Dianne Hackborn31b0e0e2012-04-05 19:33:30 -0700205 this.targetSdk = targetSdk;
Dianne Hackborn79245122012-03-12 10:51:26 -0700206 }
207 }
208
Dianne Hackborna96cbb42009-05-13 15:06:13 -0700209 /**
210 * List of new permissions that have been added since 1.0.
211 * NOTE: These must be declared in SDK version order, with permissions
212 * added to older SDKs appearing before those added to newer SDKs.
Dianne Hackborn79245122012-03-12 10:51:26 -0700213 * If sdkVersion is 0, then this is not a permission that we want to
214 * automatically add to older apps, but we do want to allow it to be
215 * granted during a platform update.
Dianne Hackborna96cbb42009-05-13 15:06:13 -0700216 * @hide
217 */
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700218 public static final PackageParser.NewPermissionInfo NEW_PERMISSIONS[] =
219 new PackageParser.NewPermissionInfo[] {
San Mehat5a3a77d2009-06-01 09:25:28 -0700220 new PackageParser.NewPermissionInfo(android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700221 android.os.Build.VERSION_CODES.DONUT, 0),
222 new PackageParser.NewPermissionInfo(android.Manifest.permission.READ_PHONE_STATE,
223 android.os.Build.VERSION_CODES.DONUT, 0)
Dianne Hackborna96cbb42009-05-13 15:06:13 -0700224 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225
Dianne Hackborn79245122012-03-12 10:51:26 -0700226 /**
227 * List of permissions that have been split into more granular or dependent
228 * permissions.
229 * @hide
230 */
231 public static final PackageParser.SplitPermissionInfo SPLIT_PERMISSIONS[] =
232 new PackageParser.SplitPermissionInfo[] {
Dianne Hackborn2bd8d042012-06-11 12:27:05 -0700233 // READ_EXTERNAL_STORAGE is always required when an app requests
234 // WRITE_EXTERNAL_STORAGE, because we can't have an app that has
235 // write access without read access. The hack here with the target
236 // target SDK version ensures that this grant is always done.
Dianne Hackborn79245122012-03-12 10:51:26 -0700237 new PackageParser.SplitPermissionInfo(android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
Dianne Hackborn31b0e0e2012-04-05 19:33:30 -0700238 new String[] { android.Manifest.permission.READ_EXTERNAL_STORAGE },
Dianne Hackborn2bd8d042012-06-11 12:27:05 -0700239 android.os.Build.VERSION_CODES.CUR_DEVELOPMENT+1),
Dianne Hackborn31b0e0e2012-04-05 19:33:30 -0700240 new PackageParser.SplitPermissionInfo(android.Manifest.permission.READ_CONTACTS,
241 new String[] { android.Manifest.permission.READ_CALL_LOG },
242 android.os.Build.VERSION_CODES.JELLY_BEAN),
243 new PackageParser.SplitPermissionInfo(android.Manifest.permission.WRITE_CONTACTS,
244 new String[] { android.Manifest.permission.WRITE_CALL_LOG },
245 android.os.Build.VERSION_CODES.JELLY_BEAN)
Dianne Hackborn79245122012-03-12 10:51:26 -0700246 };
247
Jeff Sharkey275e0852014-06-17 18:18:49 -0700248 /**
249 * @deprecated callers should move to explicitly passing around source path.
250 */
251 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 private String mArchiveSourcePath;
Jeff Sharkey275e0852014-06-17 18:18:49 -0700253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 private String[] mSeparateProcesses;
Dianne Hackbornd2509fd2011-09-12 12:29:43 -0700255 private boolean mOnlyCoreApps;
Jeff Sharkey275e0852014-06-17 18:18:49 -0700256 private DisplayMetrics mMetrics;
257
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -0700258 private static final int SDK_VERSION = Build.VERSION.SDK_INT;
Dianne Hackbornffcda102014-04-24 13:06:27 -0700259 private static final String[] SDK_CODENAMES = Build.VERSION.ACTIVE_CODENAMES;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260
261 private int mParseError = PackageManager.INSTALL_SUCCEEDED;
262
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -0700263 private static boolean sCompatibilityModeEnabled = true;
Amith Yamasani655d0e22013-06-12 14:19:10 -0700264 private static final int PARSE_DEFAULT_INSTALL_LOCATION =
265 PackageInfo.INSTALL_LOCATION_UNSPECIFIED;
Mitsuru Oshima69fff4a2009-07-21 09:51:05 -0700266
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700267 static class ParsePackageItemArgs {
268 final Package owner;
269 final String[] outError;
270 final int nameRes;
271 final int labelRes;
272 final int iconRes;
Adam Powell81cd2e92010-04-21 16:35:18 -0700273 final int logoRes;
Jose Limaf78e3122014-03-06 12:13:15 -0800274 final int bannerRes;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700275
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700276 String tag;
277 TypedArray sa;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700278
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700279 ParsePackageItemArgs(Package _owner, String[] _outError,
Jose Limaf78e3122014-03-06 12:13:15 -0800280 int _nameRes, int _labelRes, int _iconRes, int _logoRes, int _bannerRes) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700281 owner = _owner;
282 outError = _outError;
283 nameRes = _nameRes;
284 labelRes = _labelRes;
285 iconRes = _iconRes;
Adam Powell81cd2e92010-04-21 16:35:18 -0700286 logoRes = _logoRes;
Jose Limaf78e3122014-03-06 12:13:15 -0800287 bannerRes = _bannerRes;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700288 }
289 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700290
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700291 static class ParseComponentArgs extends ParsePackageItemArgs {
292 final String[] sepProcesses;
293 final int processRes;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800294 final int descriptionRes;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700295 final int enabledRes;
296 int flags;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700297
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700298 ParseComponentArgs(Package _owner, String[] _outError,
Jose Limaf78e3122014-03-06 12:13:15 -0800299 int _nameRes, int _labelRes, int _iconRes, int _logoRes, int _bannerRes,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800300 String[] _sepProcesses, int _processRes,
301 int _descriptionRes, int _enabledRes) {
Jose Limaf78e3122014-03-06 12:13:15 -0800302 super(_owner, _outError, _nameRes, _labelRes, _iconRes, _logoRes, _bannerRes);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700303 sepProcesses = _sepProcesses;
304 processRes = _processRes;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800305 descriptionRes = _descriptionRes;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700306 enabledRes = _enabledRes;
307 }
308 }
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800309
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700310 /**
Jeff Sharkey73767b92014-07-04 20:18:13 -0700311 * Lightweight parsed details about a single package.
312 */
313 public static class PackageLite {
314 public final String packageName;
315 public final int versionCode;
Jeff Sharkeybe520fb2014-07-04 18:23:17 -0700316 public final int installLocation;
317 public final VerifierInfo[] verifiers;
Jeff Sharkey73767b92014-07-04 20:18:13 -0700318
319 /** Names of any split APKs, ordered by parsed splitName */
320 public final String[] splitNames;
321
322 /**
323 * Path where this package was found on disk. For monolithic packages
324 * this is path to single base APK file; for cluster packages this is
325 * path to the cluster directory.
326 */
327 public final String codePath;
328
329 /** Path of base APK */
330 public final String baseCodePath;
331 /** Paths of any split APKs, ordered by parsed splitName */
332 public final String[] splitCodePaths;
333
Jeff Sharkey88d2a3c2014-11-22 16:49:34 -0800334 /** Revision code of base APK */
335 public final int baseRevisionCode;
336 /** Revision codes of any split APKs, ordered by parsed splitName */
337 public final int[] splitRevisionCodes;
338
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700339 public final boolean coreApp;
Narayan Kamathff110bd2014-07-04 18:30:45 +0100340 public final boolean multiArch;
Selim Gurun2d291d82016-02-05 17:50:53 -0800341 public final boolean use32bitAbi;
Dmitriy Ivanovff193d62014-09-30 15:10:48 -0700342 public final boolean extractNativeLibs;
Narayan Kamathff110bd2014-07-04 18:30:45 +0100343
Jeff Sharkey941a8ba2014-08-20 16:26:32 -0700344 public PackageLite(String codePath, ApkLite baseApk, String[] splitNames,
Jeff Sharkey88d2a3c2014-11-22 16:49:34 -0800345 String[] splitCodePaths, int[] splitRevisionCodes) {
Jeff Sharkeybe520fb2014-07-04 18:23:17 -0700346 this.packageName = baseApk.packageName;
347 this.versionCode = baseApk.versionCode;
348 this.installLocation = baseApk.installLocation;
349 this.verifiers = baseApk.verifiers;
Jeff Sharkey73767b92014-07-04 20:18:13 -0700350 this.splitNames = splitNames;
351 this.codePath = codePath;
Jeff Sharkeybe520fb2014-07-04 18:23:17 -0700352 this.baseCodePath = baseApk.codePath;
Jeff Sharkey73767b92014-07-04 20:18:13 -0700353 this.splitCodePaths = splitCodePaths;
Jeff Sharkey88d2a3c2014-11-22 16:49:34 -0800354 this.baseRevisionCode = baseApk.revisionCode;
355 this.splitRevisionCodes = splitRevisionCodes;
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700356 this.coreApp = baseApk.coreApp;
Narayan Kamathff110bd2014-07-04 18:30:45 +0100357 this.multiArch = baseApk.multiArch;
Selim Gurun2d291d82016-02-05 17:50:53 -0800358 this.use32bitAbi = baseApk.use32bitAbi;
Dmitriy Ivanovff193d62014-09-30 15:10:48 -0700359 this.extractNativeLibs = baseApk.extractNativeLibs;
Jeff Sharkey73767b92014-07-04 20:18:13 -0700360 }
361
362 public List<String> getAllCodePaths() {
363 ArrayList<String> paths = new ArrayList<>();
364 paths.add(baseCodePath);
365 if (!ArrayUtils.isEmpty(splitCodePaths)) {
366 Collections.addAll(paths, splitCodePaths);
367 }
368 return paths;
369 }
370 }
371
372 /**
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700373 * Lightweight parsed details about a single APK file.
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800374 */
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700375 public static class ApkLite {
Jeff Sharkeybe520fb2014-07-04 18:23:17 -0700376 public final String codePath;
Kenny Root05ca4c92011-09-15 10:36:25 -0700377 public final String packageName;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -0700378 public final String splitName;
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700379 public final int versionCode;
Jeff Sharkey88d2a3c2014-11-22 16:49:34 -0800380 public final int revisionCode;
Kenny Root05ca4c92011-09-15 10:36:25 -0700381 public final int installLocation;
382 public final VerifierInfo[] verifiers;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -0700383 public final Signature[] signatures;
Todd Kennedyd9d438a2016-04-06 14:08:14 -0700384 public final Certificate[][] certificates;
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700385 public final boolean coreApp;
Narayan Kamathff110bd2014-07-04 18:30:45 +0100386 public final boolean multiArch;
Selim Gurun2d291d82016-02-05 17:50:53 -0800387 public final boolean use32bitAbi;
Dmitriy Ivanovff193d62014-09-30 15:10:48 -0700388 public final boolean extractNativeLibs;
Kenny Root05ca4c92011-09-15 10:36:25 -0700389
Jeff Sharkeybe520fb2014-07-04 18:23:17 -0700390 public ApkLite(String codePath, String packageName, String splitName, int versionCode,
Jeff Sharkey88d2a3c2014-11-22 16:49:34 -0800391 int revisionCode, int installLocation, List<VerifierInfo> verifiers,
Todd Kennedyd9d438a2016-04-06 14:08:14 -0700392 Signature[] signatures, Certificate[][] certificates, boolean coreApp,
393 boolean multiArch, boolean use32bitAbi, boolean extractNativeLibs) {
Jeff Sharkeybe520fb2014-07-04 18:23:17 -0700394 this.codePath = codePath;
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800395 this.packageName = packageName;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -0700396 this.splitName = splitName;
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700397 this.versionCode = versionCode;
Jeff Sharkey88d2a3c2014-11-22 16:49:34 -0800398 this.revisionCode = revisionCode;
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800399 this.installLocation = installLocation;
Kenny Root05ca4c92011-09-15 10:36:25 -0700400 this.verifiers = verifiers.toArray(new VerifierInfo[verifiers.size()]);
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -0700401 this.signatures = signatures;
Todd Kennedyd9d438a2016-04-06 14:08:14 -0700402 this.certificates = certificates;
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700403 this.coreApp = coreApp;
Narayan Kamathff110bd2014-07-04 18:30:45 +0100404 this.multiArch = multiArch;
Selim Gurun2d291d82016-02-05 17:50:53 -0800405 this.use32bitAbi = use32bitAbi;
Dmitriy Ivanovff193d62014-09-30 15:10:48 -0700406 this.extractNativeLibs = extractNativeLibs;
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800407 }
408 }
409
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700410 private ParsePackageItemArgs mParseInstrumentationArgs;
411 private ParseComponentArgs mParseActivityArgs;
412 private ParseComponentArgs mParseActivityAliasArgs;
413 private ParseComponentArgs mParseServiceArgs;
414 private ParseComponentArgs mParseProviderArgs;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -0700415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 /** If set to true, we will only allow package files that exactly match
417 * the DTD. Otherwise, we try to get as much from the package as we
418 * can without failing. This should normally be set to false, to
419 * support extensions to the DTD in future versions. */
420 private static final boolean RIGID_PARSER = false;
421
422 private static final String TAG = "PackageParser";
423
Jeff Sharkey275e0852014-06-17 18:18:49 -0700424 public PackageParser() {
425 mMetrics = new DisplayMetrics();
426 mMetrics.setToDefaults();
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700427 }
428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 public void setSeparateProcesses(String[] procs) {
430 mSeparateProcesses = procs;
431 }
432
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700433 /**
434 * Flag indicating this parser should only consider apps with
435 * {@code coreApp} manifest attribute to be valid apps. This is useful when
436 * creating a minimalist boot environment.
437 */
Dianne Hackbornd2509fd2011-09-12 12:29:43 -0700438 public void setOnlyCoreApps(boolean onlyCoreApps) {
439 mOnlyCoreApps = onlyCoreApps;
440 }
441
Jeff Sharkey275e0852014-06-17 18:18:49 -0700442 public void setDisplayMetrics(DisplayMetrics metrics) {
443 mMetrics = metrics;
444 }
445
Jeff Sharkey73767b92014-07-04 20:18:13 -0700446 public static final boolean isApkFile(File file) {
Adam Lesinski3bcbd902014-08-22 17:01:04 -0700447 return isApkPath(file.getName());
448 }
449
450 private static boolean isApkPath(String path) {
451 return path.endsWith(".apk");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 }
453
454 /**
455 * Generate and return the {@link PackageInfo} for a parsed package.
456 *
457 * @param p the parsed package.
458 * @param flags indicating which optional information is included.
459 */
460 public static PackageInfo generatePackageInfo(PackageParser.Package p,
Dianne Hackborne639da72012-02-21 15:11:13 -0800461 int gids[], int flags, long firstInstallTime, long lastUpdateTime,
Svet Ganov3695b8a2015-03-24 16:30:25 -0700462 Set<String> grantedPermissions, PackageUserState state) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463
Amith Yamasani483f3b02012-03-13 16:08:00 -0700464 return generatePackageInfo(p, gids, flags, firstInstallTime, lastUpdateTime,
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700465 grantedPermissions, state, UserHandle.getCallingUserId());
466 }
467
Amith Yamasani655d0e22013-06-12 14:19:10 -0700468 /**
Amith Yamasanie5bcff62014-07-19 15:44:09 -0700469 * Returns true if the package is installed and not hidden, or if the caller
470 * explicitly wanted all uninstalled and hidden packages as well.
Amith Yamasani655d0e22013-06-12 14:19:10 -0700471 */
Amith Yamasanie5bcff62014-07-19 15:44:09 -0700472 private static boolean checkUseInstalledOrHidden(int flags, PackageUserState state) {
473 return (state.installed && !state.hidden)
Amith Yamasani655d0e22013-06-12 14:19:10 -0700474 || (flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0;
Amith Yamasani483f3b02012-03-13 16:08:00 -0700475 }
476
Christopher Tateba629da2013-11-13 17:42:28 -0800477 public static boolean isAvailable(PackageUserState state) {
Amith Yamasanie5bcff62014-07-19 15:44:09 -0700478 return checkUseInstalledOrHidden(0, state);
Christopher Tateba629da2013-11-13 17:42:28 -0800479 }
480
Amith Yamasani13593602012-03-22 16:16:17 -0700481 public static PackageInfo generatePackageInfo(PackageParser.Package p,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700482 int gids[], int flags, long firstInstallTime, long lastUpdateTime,
Svetoslavc6d1c342015-02-26 14:44:43 -0800483 Set<String> grantedPermissions, PackageUserState state, int userId) {
Jeff Sharkeyc5967e92016-01-07 18:50:29 -0700484 if (!checkUseInstalledOrHidden(flags, state) || !p.isMatch(flags)) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700485 return null;
486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 PackageInfo pi = new PackageInfo();
488 pi.packageName = p.packageName;
Jeff Sharkey6c833e02014-07-14 22:44:30 -0700489 pi.splitNames = p.splitNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 pi.versionCode = p.mVersionCode;
Jeff Sharkey88d2a3c2014-11-22 16:49:34 -0800491 pi.baseRevisionCode = p.baseRevisionCode;
492 pi.splitRevisionCodes = p.splitRevisionCodes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 pi.versionName = p.mVersionName;
494 pi.sharedUserId = p.mSharedUserId;
495 pi.sharedUserLabel = p.mSharedUserLabel;
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700496 pi.applicationInfo = generateApplicationInfo(p, flags, state, userId);
Suchi Amalapurapu117818e2010-02-09 03:45:40 -0800497 pi.installLocation = p.installLocation;
Jeff Hao272bf3a2014-10-08 13:34:43 -0700498 pi.coreApp = p.coreApp;
Amith Yamasani0d8750d2013-05-01 15:25:28 -0700499 if ((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) != 0
500 || (pi.applicationInfo.flags&ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
501 pi.requiredForAllUsers = p.mRequiredForAllUsers;
502 }
Amith Yamasani0ac1fc92013-03-27 18:56:08 -0700503 pi.restrictedAccountType = p.mRestrictedAccountType;
Amith Yamasaniccbe3892013-04-12 17:52:42 -0700504 pi.requiredAccountType = p.mRequiredAccountType;
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +0100505 pi.overlayTarget = p.mOverlayTarget;
Dianne Hackborn78d6883692010-10-07 01:12:46 -0700506 pi.firstInstallTime = firstInstallTime;
507 pi.lastUpdateTime = lastUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 if ((flags&PackageManager.GET_GIDS) != 0) {
509 pi.gids = gids;
510 }
511 if ((flags&PackageManager.GET_CONFIGURATIONS) != 0) {
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700512 int N = p.configPreferences != null ? p.configPreferences.size() : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 if (N > 0) {
514 pi.configPreferences = new ConfigurationInfo[N];
Dianne Hackborn49237342009-08-27 20:08:01 -0700515 p.configPreferences.toArray(pi.configPreferences);
516 }
517 N = p.reqFeatures != null ? p.reqFeatures.size() : 0;
518 if (N > 0) {
519 pi.reqFeatures = new FeatureInfo[N];
520 p.reqFeatures.toArray(pi.reqFeatures);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 }
Adam Lesinskid3edfde2014-08-08 17:32:44 -0700522 N = p.featureGroups != null ? p.featureGroups.size() : 0;
523 if (N > 0) {
524 pi.featureGroups = new FeatureGroupInfo[N];
525 p.featureGroups.toArray(pi.featureGroups);
526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 }
Jeff Sharkey2bd31db2016-01-09 16:58:14 -0700528 if ((flags & PackageManager.GET_ACTIVITIES) != 0) {
529 final int N = p.activities.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 if (N > 0) {
Jeff Sharkey2bd31db2016-01-09 16:58:14 -0700531 int num = 0;
532 final ActivityInfo[] res = new ActivityInfo[N];
533 for (int i = 0; i < N; i++) {
534 final Activity a = p.activities.get(i);
535 if (state.isMatch(a.info, flags)) {
536 res[num++] = generateActivityInfo(a, flags, state, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 }
538 }
Jeff Sharkey2bd31db2016-01-09 16:58:14 -0700539 pi.activities = ArrayUtils.trimToSize(res, num);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 }
541 }
Jeff Sharkey2bd31db2016-01-09 16:58:14 -0700542 if ((flags & PackageManager.GET_RECEIVERS) != 0) {
543 final int N = p.receivers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 if (N > 0) {
Jeff Sharkey2bd31db2016-01-09 16:58:14 -0700545 int num = 0;
546 final ActivityInfo[] res = new ActivityInfo[N];
547 for (int i = 0; i < N; i++) {
548 final Activity a = p.receivers.get(i);
549 if (state.isMatch(a.info, flags)) {
550 res[num++] = generateActivityInfo(a, flags, state, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 }
552 }
Jeff Sharkey2bd31db2016-01-09 16:58:14 -0700553 pi.receivers = ArrayUtils.trimToSize(res, num);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 }
555 }
Jeff Sharkey2bd31db2016-01-09 16:58:14 -0700556 if ((flags & PackageManager.GET_SERVICES) != 0) {
557 final int N = p.services.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 if (N > 0) {
Jeff Sharkey2bd31db2016-01-09 16:58:14 -0700559 int num = 0;
560 final ServiceInfo[] res = new ServiceInfo[N];
561 for (int i = 0; i < N; i++) {
562 final Service s = p.services.get(i);
563 if (state.isMatch(s.info, flags)) {
564 res[num++] = generateServiceInfo(s, flags, state, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
566 }
Jeff Sharkey2bd31db2016-01-09 16:58:14 -0700567 pi.services = ArrayUtils.trimToSize(res, num);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 }
569 }
Jeff Sharkey2bd31db2016-01-09 16:58:14 -0700570 if ((flags & PackageManager.GET_PROVIDERS) != 0) {
571 final int N = p.providers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 if (N > 0) {
Jeff Sharkey2bd31db2016-01-09 16:58:14 -0700573 int num = 0;
574 final ProviderInfo[] res = new ProviderInfo[N];
575 for (int i = 0; i < N; i++) {
576 final Provider pr = p.providers.get(i);
577 if (state.isMatch(pr.info, flags)) {
578 res[num++] = generateProviderInfo(pr, flags, state, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 }
580 }
Jeff Sharkey2bd31db2016-01-09 16:58:14 -0700581 pi.providers = ArrayUtils.trimToSize(res, num);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 }
583 }
584 if ((flags&PackageManager.GET_INSTRUMENTATION) != 0) {
585 int N = p.instrumentation.size();
586 if (N > 0) {
587 pi.instrumentation = new InstrumentationInfo[N];
588 for (int i=0; i<N; i++) {
589 pi.instrumentation[i] = generateInstrumentationInfo(
590 p.instrumentation.get(i), flags);
591 }
592 }
593 }
594 if ((flags&PackageManager.GET_PERMISSIONS) != 0) {
595 int N = p.permissions.size();
596 if (N > 0) {
597 pi.permissions = new PermissionInfo[N];
598 for (int i=0; i<N; i++) {
599 pi.permissions[i] = generatePermissionInfo(p.permissions.get(i), flags);
600 }
601 }
602 N = p.requestedPermissions.size();
603 if (N > 0) {
604 pi.requestedPermissions = new String[N];
Dianne Hackborne639da72012-02-21 15:11:13 -0800605 pi.requestedPermissionsFlags = new int[N];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 for (int i=0; i<N; i++) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800607 final String perm = p.requestedPermissions.get(i);
608 pi.requestedPermissions[i] = perm;
Svet Ganov3695b8a2015-03-24 16:30:25 -0700609 // The notion of required permissions is deprecated but for compatibility.
Svetoslavc6d1c342015-02-26 14:44:43 -0800610 pi.requestedPermissionsFlags[i] |= PackageInfo.REQUESTED_PERMISSION_REQUIRED;
Dianne Hackborne639da72012-02-21 15:11:13 -0800611 if (grantedPermissions != null && grantedPermissions.contains(perm)) {
612 pi.requestedPermissionsFlags[i] |= PackageInfo.REQUESTED_PERMISSION_GRANTED;
613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 }
615 }
616 }
617 if ((flags&PackageManager.GET_SIGNATURES) != 0) {
Suchi Amalapurapud83006c2009-10-28 23:39:46 -0700618 int N = (p.mSignatures != null) ? p.mSignatures.length : 0;
619 if (N > 0) {
620 pi.signatures = new Signature[N];
621 System.arraycopy(p.mSignatures, 0, pi.signatures, 0, N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 }
623 }
624 return pi;
625 }
626
Jeff Sharkey275e0852014-06-17 18:18:49 -0700627 private static Certificate[][] loadCertificates(StrictJarFile jarFile, ZipEntry entry)
628 throws PackageParserException {
629 InputStream is = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 try {
631 // We must read the stream for the JarEntry to retrieve
632 // its certificates.
Jeff Sharkey275e0852014-06-17 18:18:49 -0700633 is = jarFile.getInputStream(entry);
634 readFullyIgnoringContents(is);
635 return jarFile.getCertificateChains(entry);
636 } catch (IOException | RuntimeException e) {
637 throw new PackageParserException(INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION,
638 "Failed reading " + entry.getName() + " in " + jarFile, e);
639 } finally {
640 IoUtils.closeQuietly(is);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 }
643
Suchi Amalapurapuaf8e9f42010-01-12 10:17:28 -0800644 public final static int PARSE_IS_SYSTEM = 1<<0;
645 public final static int PARSE_CHATTY = 1<<1;
646 public final static int PARSE_MUST_BE_APK = 1<<2;
647 public final static int PARSE_IGNORE_PROCESSES = 1<<3;
648 public final static int PARSE_FORWARD_LOCK = 1<<4;
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -0700649 public final static int PARSE_EXTERNAL_STORAGE = 1<<5;
Dianne Hackborn806da1d2010-03-18 16:50:07 -0700650 public final static int PARSE_IS_SYSTEM_DIR = 1<<6;
Christopher Tateccbf84f2013-05-08 15:25:41 -0700651 public final static int PARSE_IS_PRIVILEGED = 1<<7;
Jeff Sharkeybb7b7be2014-08-19 16:18:28 -0700652 public final static int PARSE_COLLECT_CERTIFICATES = 1<<8;
Jeff Sharkey275e0852014-06-17 18:18:49 -0700653 public final static int PARSE_TRUSTED_OVERLAY = 1<<9;
Christopher Tatef3d939c2015-09-17 12:25:51 -0700654 public final static int PARSE_ENFORCE_CODE = 1<<10;
Todd Kennedy373f0b42015-12-16 14:45:14 -0800655 public final static int PARSE_IS_EPHEMERAL = 1<<11;
Todd Kennedyb1072712016-04-26 15:41:20 -0700656 public final static int PARSE_FORCE_SDK = 1<<12;
Jeff Sharkey275e0852014-06-17 18:18:49 -0700657
658 private static final Comparator<String> sSplitNameComparator = new SplitNameComparator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700660 /**
Jeff Sharkey275e0852014-06-17 18:18:49 -0700661 * Used to sort a set of APKs based on their split names, always placing the
662 * base APK (with {@code null} split name) first.
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700663 */
Jeff Sharkey275e0852014-06-17 18:18:49 -0700664 private static class SplitNameComparator implements Comparator<String> {
665 @Override
666 public int compare(String lhs, String rhs) {
667 if (lhs == null) {
668 return -1;
669 } else if (rhs == null) {
670 return 1;
671 } else {
672 return lhs.compareTo(rhs);
673 }
674 }
675 }
676
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700677 /**
678 * Parse only lightweight details about the package at the given location.
679 * Automatically detects if the package is a monolithic style (single APK
680 * file) or cluster style (directory of APKs).
681 * <p>
682 * This performs sanity checking on cluster style packages, such as
683 * requiring identical package name and version codes, a single base APK,
684 * and unique split names.
685 *
686 * @see PackageParser#parsePackage(File, int)
687 */
Jeff Sharkey73767b92014-07-04 20:18:13 -0700688 public static PackageLite parsePackageLite(File packageFile, int flags)
689 throws PackageParserException {
690 if (packageFile.isDirectory()) {
691 return parseClusterPackageLite(packageFile, flags);
692 } else {
693 return parseMonolithicPackageLite(packageFile, flags);
694 }
695 }
696
697 private static PackageLite parseMonolithicPackageLite(File packageFile, int flags)
698 throws PackageParserException {
Jeff Sharkeybe520fb2014-07-04 18:23:17 -0700699 final ApkLite baseApk = parseApkLite(packageFile, flags);
Jeff Sharkey73767b92014-07-04 20:18:13 -0700700 final String packagePath = packageFile.getAbsolutePath();
Jeff Sharkey88d2a3c2014-11-22 16:49:34 -0800701 return new PackageLite(packagePath, baseApk, null, null, null);
Jeff Sharkey73767b92014-07-04 20:18:13 -0700702 }
703
704 private static PackageLite parseClusterPackageLite(File packageDir, int flags)
705 throws PackageParserException {
706 final File[] files = packageDir.listFiles();
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700707 if (ArrayUtils.isEmpty(files)) {
708 throw new PackageParserException(INSTALL_PARSE_FAILED_NOT_APK,
709 "No packages found in split");
710 }
711
Jeff Sharkey275e0852014-06-17 18:18:49 -0700712 String packageName = null;
713 int versionCode = 0;
714
Jeff Sharkeybe520fb2014-07-04 18:23:17 -0700715 final ArrayMap<String, ApkLite> apks = new ArrayMap<>();
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700716 for (File file : files) {
Jeff Sharkey73767b92014-07-04 20:18:13 -0700717 if (isApkFile(file)) {
718 final ApkLite lite = parseApkLite(file, flags);
Jeff Sharkey275e0852014-06-17 18:18:49 -0700719
720 // Assert that all package names and version codes are
721 // consistent with the first one we encounter.
722 if (packageName == null) {
723 packageName = lite.packageName;
724 versionCode = lite.versionCode;
725 } else {
726 if (!packageName.equals(lite.packageName)) {
727 throw new PackageParserException(INSTALL_PARSE_FAILED_BAD_MANIFEST,
728 "Inconsistent package " + lite.packageName + " in " + file
729 + "; expected " + packageName);
730 }
731 if (versionCode != lite.versionCode) {
732 throw new PackageParserException(INSTALL_PARSE_FAILED_BAD_MANIFEST,
733 "Inconsistent version " + lite.versionCode + " in " + file
734 + "; expected " + versionCode);
735 }
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700736 }
737
Jeff Sharkey275e0852014-06-17 18:18:49 -0700738 // Assert that each split is defined only once
Jeff Sharkeybe520fb2014-07-04 18:23:17 -0700739 if (apks.put(lite.splitName, lite) != null) {
Jeff Sharkey275e0852014-06-17 18:18:49 -0700740 throw new PackageParserException(INSTALL_PARSE_FAILED_BAD_MANIFEST,
741 "Split name " + lite.splitName
742 + " defined more than once; most recent was " + file);
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700743 }
744 }
745 }
746
Jeff Sharkeybe520fb2014-07-04 18:23:17 -0700747 final ApkLite baseApk = apks.remove(null);
Jeff Sharkey73767b92014-07-04 20:18:13 -0700748 if (baseApk == null) {
Jeff Sharkey275e0852014-06-17 18:18:49 -0700749 throw new PackageParserException(INSTALL_PARSE_FAILED_BAD_MANIFEST,
Jeff Sharkey73767b92014-07-04 20:18:13 -0700750 "Missing base APK in " + packageDir);
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700751 }
752
Jeff Sharkey57dcf5b2014-06-18 17:46:05 -0700753 // Always apply deterministic ordering based on splitName
754 final int size = apks.size();
Jeff Sharkey57dcf5b2014-06-18 17:46:05 -0700755
Jeff Sharkey73767b92014-07-04 20:18:13 -0700756 String[] splitNames = null;
757 String[] splitCodePaths = null;
Jeff Sharkey88d2a3c2014-11-22 16:49:34 -0800758 int[] splitRevisionCodes = null;
Jeff Sharkey73767b92014-07-04 20:18:13 -0700759 if (size > 0) {
760 splitNames = new String[size];
761 splitCodePaths = new String[size];
Jeff Sharkey88d2a3c2014-11-22 16:49:34 -0800762 splitRevisionCodes = new int[size];
Jeff Sharkey73767b92014-07-04 20:18:13 -0700763
764 splitNames = apks.keySet().toArray(splitNames);
765 Arrays.sort(splitNames, sSplitNameComparator);
766
767 for (int i = 0; i < size; i++) {
Jeff Sharkeybe520fb2014-07-04 18:23:17 -0700768 splitCodePaths[i] = apks.get(splitNames[i]).codePath;
Jeff Sharkey88d2a3c2014-11-22 16:49:34 -0800769 splitRevisionCodes[i] = apks.get(splitNames[i]).revisionCode;
Jeff Sharkey73767b92014-07-04 20:18:13 -0700770 }
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700771 }
772
Jeff Sharkey73767b92014-07-04 20:18:13 -0700773 final String codePath = packageDir.getAbsolutePath();
Jeff Sharkey88d2a3c2014-11-22 16:49:34 -0800774 return new PackageLite(codePath, baseApk, splitNames, splitCodePaths,
775 splitRevisionCodes);
Jeff Sharkey73767b92014-07-04 20:18:13 -0700776 }
777
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700778 /**
779 * Parse the package at the given location. Automatically detects if the
780 * package is a monolithic style (single APK file) or cluster style
781 * (directory of APKs).
782 * <p>
783 * This performs sanity checking on cluster style packages, such as
784 * requiring identical package name and version codes, a single base APK,
785 * and unique split names.
786 * <p>
787 * Note that this <em>does not</em> perform signature verification; that
788 * must be done separately in {@link #collectCertificates(Package, int)}.
789 *
790 * @see #parsePackageLite(File, int)
791 */
Jeff Sharkey73767b92014-07-04 20:18:13 -0700792 public Package parsePackage(File packageFile, int flags) throws PackageParserException {
793 if (packageFile.isDirectory()) {
794 return parseClusterPackage(packageFile, flags);
795 } else {
796 return parseMonolithicPackage(packageFile, flags);
797 }
798 }
799
800 /**
801 * Parse all APKs contained in the given directory, treating them as a
802 * single package. This also performs sanity checking, such as requiring
803 * identical package name and version codes, a single base APK, and unique
804 * split names.
805 * <p>
806 * Note that this <em>does not</em> perform signature verification; that
807 * must be done separately in {@link #collectCertificates(Package, int)}.
808 */
809 private Package parseClusterPackage(File packageDir, int flags) throws PackageParserException {
810 final PackageLite lite = parseClusterPackageLite(packageDir, 0);
811
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700812 if (mOnlyCoreApps && !lite.coreApp) {
813 throw new PackageParserException(INSTALL_PARSE_FAILED_MANIFEST_MALFORMED,
814 "Not a coreApp: " + packageDir);
815 }
816
Adam Lesinski3bcbd902014-08-22 17:01:04 -0700817 final AssetManager assets = new AssetManager();
818 try {
819 // Load the base and all splits into the AssetManager
820 // so that resources can be overriden when parsing the manifests.
821 loadApkIntoAssetManager(assets, lite.baseCodePath, flags);
Jeff Sharkey73767b92014-07-04 20:18:13 -0700822
Adam Lesinski3bcbd902014-08-22 17:01:04 -0700823 if (!ArrayUtils.isEmpty(lite.splitCodePaths)) {
824 for (String path : lite.splitCodePaths) {
825 loadApkIntoAssetManager(assets, path, flags);
826 }
Jeff Sharkey73767b92014-07-04 20:18:13 -0700827 }
Jeff Sharkey73767b92014-07-04 20:18:13 -0700828
Adam Lesinski3bcbd902014-08-22 17:01:04 -0700829 final File baseApk = new File(lite.baseCodePath);
830 final Package pkg = parseBaseApk(baseApk, assets, flags);
831 if (pkg == null) {
832 throw new PackageParserException(INSTALL_PARSE_FAILED_NOT_APK,
833 "Failed to parse base APK: " + baseApk);
834 }
835
836 if (!ArrayUtils.isEmpty(lite.splitNames)) {
837 final int num = lite.splitNames.length;
838 pkg.splitNames = lite.splitNames;
839 pkg.splitCodePaths = lite.splitCodePaths;
Jeff Sharkey88d2a3c2014-11-22 16:49:34 -0800840 pkg.splitRevisionCodes = lite.splitRevisionCodes;
Adam Lesinski3bcbd902014-08-22 17:01:04 -0700841 pkg.splitFlags = new int[num];
Alex Klyubinb9f8a522015-02-03 11:12:59 -0800842 pkg.splitPrivateFlags = new int[num];
Adam Lesinski3bcbd902014-08-22 17:01:04 -0700843
844 for (int i = 0; i < num; i++) {
845 parseSplitApk(pkg, i, assets, flags);
846 }
847 }
848
Svet Ganov354cd3c2015-12-17 11:35:04 -0800849 pkg.setCodePath(packageDir.getAbsolutePath());
Selim Gurun2d291d82016-02-05 17:50:53 -0800850 pkg.setUse32bitAbi(lite.use32bitAbi);
Adam Lesinski3bcbd902014-08-22 17:01:04 -0700851 return pkg;
852 } finally {
853 IoUtils.closeQuietly(assets);
854 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 }
856
Jeff Sharkey275e0852014-06-17 18:18:49 -0700857 /**
858 * Parse the given APK file, treating it as as a single monolithic package.
859 * <p>
860 * Note that this <em>does not</em> perform signature verification; that
861 * must be done separately in {@link #collectCertificates(Package, int)}.
Jeff Sharkeyd7460572014-07-06 20:44:55 -0700862 *
863 * @deprecated external callers should move to
864 * {@link #parsePackage(File, int)}. Eventually this method will
865 * be marked private.
Jeff Sharkey275e0852014-06-17 18:18:49 -0700866 */
Jeff Sharkeyd7460572014-07-06 20:44:55 -0700867 @Deprecated
Jeff Sharkey275e0852014-06-17 18:18:49 -0700868 public Package parseMonolithicPackage(File apkFile, int flags) throws PackageParserException {
Tao Baic9a02372016-01-12 15:02:24 -0800869 final PackageLite lite = parseMonolithicPackageLite(apkFile, flags);
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700870 if (mOnlyCoreApps) {
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700871 if (!lite.coreApp) {
872 throw new PackageParserException(INSTALL_PARSE_FAILED_MANIFEST_MALFORMED,
873 "Not a coreApp: " + apkFile);
874 }
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700875 }
Jeff Sharkey57dcf5b2014-06-18 17:46:05 -0700876
Adam Lesinski3bcbd902014-08-22 17:01:04 -0700877 final AssetManager assets = new AssetManager();
878 try {
879 final Package pkg = parseBaseApk(apkFile, assets, flags);
Svet Ganov354cd3c2015-12-17 11:35:04 -0800880 pkg.setCodePath(apkFile.getAbsolutePath());
Selim Gurun2d291d82016-02-05 17:50:53 -0800881 pkg.setUse32bitAbi(lite.use32bitAbi);
Adam Lesinski3bcbd902014-08-22 17:01:04 -0700882 return pkg;
883 } finally {
884 IoUtils.closeQuietly(assets);
885 }
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700886 }
887
Adam Lesinski3bcbd902014-08-22 17:01:04 -0700888 private static int loadApkIntoAssetManager(AssetManager assets, String apkPath, int flags)
889 throws PackageParserException {
890 if ((flags & PARSE_MUST_BE_APK) != 0 && !isApkPath(apkPath)) {
891 throw new PackageParserException(INSTALL_PARSE_FAILED_NOT_APK,
892 "Invalid package file: " + apkPath);
893 }
894
895 // The AssetManager guarantees uniqueness for asset paths, so if this asset path
896 // already exists in the AssetManager, addAssetPath will only return the cookie
897 // assigned to it.
898 int cookie = assets.addAssetPath(apkPath);
899 if (cookie == 0) {
900 throw new PackageParserException(INSTALL_PARSE_FAILED_BAD_MANIFEST,
901 "Failed adding asset path: " + apkPath);
902 }
903 return cookie;
904 }
905
906 private Package parseBaseApk(File apkFile, AssetManager assets, int flags)
907 throws PackageParserException {
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700908 final String apkPath = apkFile.getAbsolutePath();
Jeff Sharkey275e0852014-06-17 18:18:49 -0700909
Jeff Sharkeyb9f36742015-04-08 21:02:14 -0700910 String volumeUuid = null;
911 if (apkPath.startsWith(MNT_EXPAND)) {
912 final int end = apkPath.indexOf('/', MNT_EXPAND.length());
913 volumeUuid = apkPath.substring(MNT_EXPAND.length(), end);
914 }
915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 mParseError = PackageManager.INSTALL_SUCCEEDED;
Jeff Sharkeyc4858a22014-06-16 10:51:20 -0700917 mArchiveSourcePath = apkFile.getAbsolutePath();
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700918
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700919 if (DEBUG_JAR) Slog.d(TAG, "Scanning base APK: " + apkPath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920
Adam Lesinski3bcbd902014-08-22 17:01:04 -0700921 final int cookie = loadApkIntoAssetManager(assets, apkPath, flags);
922
Dianne Hackborn3b81bc12011-01-15 11:50:52 -0800923 Resources res = null;
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700924 XmlResourceParser parser = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 try {
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700926 res = new Resources(assets, mMetrics, null);
927 assets.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
928 Build.VERSION.RESOURCES_SDK_INT);
929 parser = assets.openXmlResourceParser(cookie, ANDROID_MANIFEST_FILENAME);
930
931 final String[] outError = new String[1];
932 final Package pkg = parseBaseApk(res, parser, flags, outError);
933 if (pkg == null) {
934 throw new PackageParserException(mParseError,
935 apkPath + " (at " + parser.getPositionDescription() + "): " + outError[0]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 }
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700937
Svet Ganov354cd3c2015-12-17 11:35:04 -0800938 pkg.setVolumeUuid(volumeUuid);
939 pkg.setApplicationVolumeUuid(volumeUuid);
940 pkg.setBaseCodePath(apkPath);
941 pkg.setSignatures(null);
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700942
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700943 return pkg;
944
945 } catch (PackageParserException e) {
946 throw e;
947 } catch (Exception e) {
948 throw new PackageParserException(INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION,
Jeff Sharkey78a13012014-07-15 20:18:34 -0700949 "Failed to read manifest from " + apkPath, e);
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700950 } finally {
951 IoUtils.closeQuietly(parser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 }
954
Adam Lesinski3bcbd902014-08-22 17:01:04 -0700955 private void parseSplitApk(Package pkg, int splitIndex, AssetManager assets, int flags)
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700956 throws PackageParserException {
957 final String apkPath = pkg.splitCodePaths[splitIndex];
Jeff Sharkey275e0852014-06-17 18:18:49 -0700958
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700959 mParseError = PackageManager.INSTALL_SUCCEEDED;
960 mArchiveSourcePath = apkPath;
961
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700962 if (DEBUG_JAR) Slog.d(TAG, "Scanning split APK: " + apkPath);
963
Adam Lesinski3bcbd902014-08-22 17:01:04 -0700964 final int cookie = loadApkIntoAssetManager(assets, apkPath, flags);
965
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700966 Resources res = null;
967 XmlResourceParser parser = null;
968 try {
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700969 res = new Resources(assets, mMetrics, null);
970 assets.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
971 Build.VERSION.RESOURCES_SDK_INT);
972 parser = assets.openXmlResourceParser(cookie, ANDROID_MANIFEST_FILENAME);
973
974 final String[] outError = new String[1];
975 pkg = parseSplitApk(pkg, res, parser, flags, splitIndex, outError);
976 if (pkg == null) {
977 throw new PackageParserException(mParseError,
978 apkPath + " (at " + parser.getPositionDescription() + "): " + outError[0]);
979 }
980
981 } catch (PackageParserException e) {
982 throw e;
983 } catch (Exception e) {
984 throw new PackageParserException(INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION,
Jeff Sharkey78a13012014-07-15 20:18:34 -0700985 "Failed to read manifest from " + apkPath, e);
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700986 } finally {
987 IoUtils.closeQuietly(parser);
Jeff Sharkeyda96e132014-07-15 14:54:09 -0700988 }
989 }
990
991 /**
992 * Parse the manifest of a <em>split APK</em>.
993 * <p>
994 * Note that split APKs have many more restrictions on what they're capable
995 * of doing, so many valid features of a base APK have been carefully
996 * omitted here.
997 */
998 private Package parseSplitApk(Package pkg, Resources res, XmlResourceParser parser, int flags,
Jeff Sharkey78a13012014-07-15 20:18:34 -0700999 int splitIndex, String[] outError) throws XmlPullParserException, IOException,
1000 PackageParserException {
Jeff Sharkeyda96e132014-07-15 14:54:09 -07001001 AttributeSet attrs = parser;
1002
Jeff Sharkey78a13012014-07-15 20:18:34 -07001003 // We parsed manifest tag earlier; just skip past it
Svet Ganov354cd3c2015-12-17 11:35:04 -08001004 parsePackageSplitNames(parser, attrs);
Jeff Sharkey78a13012014-07-15 20:18:34 -07001005
Jeff Sharkeyda96e132014-07-15 14:54:09 -07001006 mParseInstrumentationArgs = null;
1007 mParseActivityArgs = null;
1008 mParseServiceArgs = null;
1009 mParseProviderArgs = null;
1010
1011 int type;
1012
1013 boolean foundApp = false;
1014
1015 int outerDepth = parser.getDepth();
1016 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1017 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1018 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1019 continue;
1020 }
1021
1022 String tagName = parser.getName();
1023 if (tagName.equals("application")) {
1024 if (foundApp) {
1025 if (RIGID_PARSER) {
1026 outError[0] = "<manifest> has more than one <application>";
1027 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1028 return null;
1029 } else {
1030 Slog.w(TAG, "<manifest> has more than one <application>");
1031 XmlUtils.skipCurrentTag(parser);
1032 continue;
1033 }
1034 }
1035
1036 foundApp = true;
Svet Ganov354cd3c2015-12-17 11:35:04 -08001037 if (!parseSplitApplication(pkg, res, parser, flags, splitIndex, outError)) {
Jeff Sharkeyda96e132014-07-15 14:54:09 -07001038 return null;
1039 }
1040
1041 } else if (RIGID_PARSER) {
1042 outError[0] = "Bad element under <manifest>: "
1043 + parser.getName();
1044 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1045 return null;
1046
1047 } else {
1048 Slog.w(TAG, "Unknown element under <manifest>: " + parser.getName()
1049 + " at " + mArchiveSourcePath + " "
1050 + parser.getPositionDescription());
1051 XmlUtils.skipCurrentTag(parser);
1052 continue;
1053 }
1054 }
1055
1056 if (!foundApp) {
1057 outError[0] = "<manifest> does not contain an <application>";
1058 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_EMPTY;
1059 }
1060
1061 return pkg;
Jeff Sharkeyc4858a22014-06-16 10:51:20 -07001062 }
1063
Todd Kennedy66c55532016-02-26 16:22:11 -08001064 public static int getApkSigningVersion(Package pkg) {
1065 try {
1066 if (ApkSignatureSchemeV2Verifier.hasSignature(pkg.baseCodePath)) {
1067 return APK_SIGNING_V2;
1068 }
1069 return APK_SIGNING_V1;
1070 } catch (IOException e) {
1071 }
1072 return APK_SIGNING_UNKNOWN;
1073 }
1074
Kenny Root6c918ce2013-04-02 14:04:24 -07001075 /**
Todd Kennedyd9d438a2016-04-06 14:08:14 -07001076 * Populates the correct packages fields with the given certificates.
1077 * <p>
1078 * This is useful when we've already processed the certificates [such as during package
1079 * installation through an installer session]. We don't re-process the archive and
1080 * simply populate the correct fields.
1081 */
1082 public static void populateCertificates(Package pkg, Certificate[][] certificates)
1083 throws PackageParserException {
1084 pkg.mCertificates = null;
1085 pkg.mSignatures = null;
1086 pkg.mSigningKeys = null;
1087
1088 pkg.mCertificates = certificates;
1089 try {
1090 pkg.mSignatures = convertToSignatures(certificates);
1091 } catch (CertificateEncodingException e) {
1092 // certificates weren't encoded properly; something went wrong
1093 throw new PackageParserException(INSTALL_PARSE_FAILED_NO_CERTIFICATES,
1094 "Failed to collect certificates from " + pkg.baseCodePath, e);
1095 }
1096 pkg.mSigningKeys = new ArraySet<>(certificates.length);
1097 for (int i = 0; i < certificates.length; i++) {
1098 Certificate[] signerCerts = certificates[i];
1099 Certificate signerCert = signerCerts[0];
1100 pkg.mSigningKeys.add(signerCert.getPublicKey());
1101 }
1102 // add signatures to child packages
1103 final int childCount = (pkg.childPackages != null) ? pkg.childPackages.size() : 0;
1104 for (int i = 0; i < childCount; i++) {
1105 Package childPkg = pkg.childPackages.get(i);
1106 childPkg.mCertificates = pkg.mCertificates;
1107 childPkg.mSignatures = pkg.mSignatures;
1108 childPkg.mSigningKeys = pkg.mSigningKeys;
1109 }
1110 }
1111
1112 /**
Jeff Sharkey275e0852014-06-17 18:18:49 -07001113 * Collect certificates from all the APKs described in the given package,
Todd Kennedy373f0b42015-12-16 14:45:14 -08001114 * populating {@link Package#mSignatures}. Also asserts that all APK
Jeff Sharkey275e0852014-06-17 18:18:49 -07001115 * contents are signed correctly and consistently.
1116 */
Todd Kennedy66c55532016-02-26 16:22:11 -08001117 public static void collectCertificates(Package pkg, int parseFlags)
1118 throws PackageParserException {
Svet Ganov354cd3c2015-12-17 11:35:04 -08001119 collectCertificatesInternal(pkg, parseFlags);
1120 final int childCount = (pkg.childPackages != null) ? pkg.childPackages.size() : 0;
1121 for (int i = 0; i < childCount; i++) {
1122 Package childPkg = pkg.childPackages.get(i);
1123 childPkg.mCertificates = pkg.mCertificates;
1124 childPkg.mSignatures = pkg.mSignatures;
1125 childPkg.mSigningKeys = pkg.mSigningKeys;
1126 }
1127 }
1128
Todd Kennedy66c55532016-02-26 16:22:11 -08001129 private static void collectCertificatesInternal(Package pkg, int parseFlags)
1130 throws PackageParserException {
Jeff Sharkey275e0852014-06-17 18:18:49 -07001131 pkg.mCertificates = null;
1132 pkg.mSignatures = null;
1133 pkg.mSigningKeys = null;
1134
Todd Kennedy0f4bc2b2016-04-06 07:46:13 -07001135 Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "collectCertificates");
1136 try {
Todd Kennedyf720a9d2016-04-15 09:44:36 -07001137 collectCertificates(pkg, new File(pkg.baseCodePath), parseFlags);
Jeff Sharkey275e0852014-06-17 18:18:49 -07001138
Todd Kennedy0f4bc2b2016-04-06 07:46:13 -07001139 if (!ArrayUtils.isEmpty(pkg.splitCodePaths)) {
1140 for (int i = 0; i < pkg.splitCodePaths.length; i++) {
Todd Kennedyf720a9d2016-04-15 09:44:36 -07001141 collectCertificates(pkg, new File(pkg.splitCodePaths[i]), parseFlags);
Todd Kennedy0f4bc2b2016-04-06 07:46:13 -07001142 }
Jeff Sharkey275e0852014-06-17 18:18:49 -07001143 }
Todd Kennedy0f4bc2b2016-04-06 07:46:13 -07001144 } finally {
1145 Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
Jeff Sharkeyc4858a22014-06-16 10:51:20 -07001146 }
1147 }
1148
Todd Kennedyf720a9d2016-04-15 09:44:36 -07001149 private static void collectCertificates(Package pkg, File apkFile, int parseFlags)
Jeff Sharkey275e0852014-06-17 18:18:49 -07001150 throws PackageParserException {
1151 final String apkPath = apkFile.getAbsolutePath();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152
Alex Klyubine4157182016-01-05 13:27:05 -08001153 // Try to verify the APK using APK Signature Scheme v2.
1154 boolean verified = false;
1155 {
1156 Certificate[][] allSignersCerts = null;
1157 Signature[] signatures = null;
1158 try {
Todd Kennedy0f4bc2b2016-04-06 07:46:13 -07001159 Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "verifyV2");
Alex Klyubine4157182016-01-05 13:27:05 -08001160 allSignersCerts = ApkSignatureSchemeV2Verifier.verify(apkPath);
1161 signatures = convertToSignatures(allSignersCerts);
1162 // APK verified using APK Signature Scheme v2.
1163 verified = true;
1164 } catch (ApkSignatureSchemeV2Verifier.SignatureNotFoundException e) {
1165 // No APK Signature Scheme v2 signature found
1166 } catch (Exception e) {
1167 // APK Signature Scheme v2 signature was found but did not verify
1168 throw new PackageParserException(INSTALL_PARSE_FAILED_NO_CERTIFICATES,
1169 "Failed to collect certificates from " + apkPath
1170 + " using APK Signature Scheme v2",
1171 e);
Todd Kennedy0f4bc2b2016-04-06 07:46:13 -07001172 } finally {
1173 Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
Alex Klyubine4157182016-01-05 13:27:05 -08001174 }
1175
1176 if (verified) {
1177 if (pkg.mCertificates == null) {
1178 pkg.mCertificates = allSignersCerts;
1179 pkg.mSignatures = signatures;
1180 pkg.mSigningKeys = new ArraySet<>(allSignersCerts.length);
1181 for (int i = 0; i < allSignersCerts.length; i++) {
1182 Certificate[] signerCerts = allSignersCerts[i];
1183 Certificate signerCert = signerCerts[0];
1184 pkg.mSigningKeys.add(signerCert.getPublicKey());
1185 }
1186 } else {
1187 if (!Signature.areExactMatch(pkg.mSignatures, signatures)) {
1188 throw new PackageParserException(
1189 INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES,
1190 apkPath + " has mismatched certificates");
1191 }
1192 }
1193 // Not yet done, because we need to confirm that AndroidManifest.xml exists and,
1194 // if requested, that classes.dex exists.
1195 }
1196 }
1197
Jeff Sharkey275e0852014-06-17 18:18:49 -07001198 StrictJarFile jarFile = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 try {
Todd Kennedy27c24fb2015-09-17 16:49:25 -07001200 Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "strictJarFileCtor");
Alex Klyubin9b59bc42016-03-24 12:02:20 -07001201 // Ignore signature stripping protections when verifying APKs from system partition.
1202 // For those APKs we only care about extracting signer certificates, and don't care
1203 // about verifying integrity.
1204 boolean signatureSchemeRollbackProtectionsEnforced =
Todd Kennedyd022ac22016-04-13 10:49:29 -07001205 (parseFlags & PARSE_IS_SYSTEM_DIR) == 0;
Alex Klyubine4157182016-01-05 13:27:05 -08001206 jarFile = new StrictJarFile(
1207 apkPath,
Alex Klyubin9b59bc42016-03-24 12:02:20 -07001208 !verified, // whether to verify JAR signature
1209 signatureSchemeRollbackProtectionsEnforced);
Todd Kennedy27c24fb2015-09-17 16:49:25 -07001210 Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211
Jeff Sharkey275e0852014-06-17 18:18:49 -07001212 // Always verify manifest, regardless of source
1213 final ZipEntry manifestEntry = jarFile.findEntry(ANDROID_MANIFEST_FILENAME);
1214 if (manifestEntry == null) {
1215 throw new PackageParserException(INSTALL_PARSE_FAILED_BAD_MANIFEST,
1216 "Package " + apkPath + " has no manifest");
1217 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218
Alex Klyubine4157182016-01-05 13:27:05 -08001219 // Optimization: early termination when APK already verified
1220 if (verified) {
Alex Klyubine4157182016-01-05 13:27:05 -08001221 return;
1222 }
1223
1224 // APK's integrity needs to be verified using JAR signature scheme.
Todd Kennedy0f4bc2b2016-04-06 07:46:13 -07001225 Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "verifyV1");
Jeff Sharkey275e0852014-06-17 18:18:49 -07001226 final List<ZipEntry> toVerify = new ArrayList<>();
1227 toVerify.add(manifestEntry);
1228
1229 // If we're parsing an untrusted package, verify all contents
Todd Kennedyd022ac22016-04-13 10:49:29 -07001230 if ((parseFlags & PARSE_IS_SYSTEM_DIR) == 0) {
Jeff Sharkey275e0852014-06-17 18:18:49 -07001231 final Iterator<ZipEntry> i = jarFile.iterator();
1232 while (i.hasNext()) {
1233 final ZipEntry entry = i.next();
1234
1235 if (entry.isDirectory()) continue;
Christopher Tatef3d939c2015-09-17 12:25:51 -07001236
1237 final String entryName = entry.getName();
1238 if (entryName.startsWith("META-INF/")) continue;
1239 if (entryName.equals(ANDROID_MANIFEST_FILENAME)) continue;
Jeff Sharkey275e0852014-06-17 18:18:49 -07001240
1241 toVerify.add(entry);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 }
Jeff Sharkey275e0852014-06-17 18:18:49 -07001243 }
1244
1245 // Verify that entries are signed consistently with the first entry
1246 // we encountered. Note that for splits, certificates may have
1247 // already been populated during an earlier parse of a base APK.
1248 for (ZipEntry entry : toVerify) {
1249 final Certificate[][] entryCerts = loadCertificates(jarFile, entry);
1250 if (ArrayUtils.isEmpty(entryCerts)) {
1251 throw new PackageParserException(INSTALL_PARSE_FAILED_NO_CERTIFICATES,
1252 "Package " + apkPath + " has no certificates at entry "
1253 + entry.getName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 }
Jeff Sharkeyec55ef02014-07-08 11:28:00 -07001255 final Signature[] entrySignatures = convertToSignatures(entryCerts);
Kenny Rootd2d29252011-08-08 11:27:57 -07001256
Jeff Sharkey275e0852014-06-17 18:18:49 -07001257 if (pkg.mCertificates == null) {
1258 pkg.mCertificates = entryCerts;
Jeff Sharkeyec55ef02014-07-08 11:28:00 -07001259 pkg.mSignatures = entrySignatures;
dcashman55b10782014-04-09 14:20:38 -07001260 pkg.mSigningKeys = new ArraySet<PublicKey>();
1261 for (int i=0; i < entryCerts.length; i++) {
Jeff Sharkey275e0852014-06-17 18:18:49 -07001262 pkg.mSigningKeys.add(entryCerts[i][0].getPublicKey());
Kenny Rootbcc954d2011-08-08 16:19:08 -07001263 }
Jeff Sharkey275e0852014-06-17 18:18:49 -07001264 } else {
Jeff Sharkeyec55ef02014-07-08 11:28:00 -07001265 if (!Signature.areExactMatch(pkg.mSignatures, entrySignatures)) {
Jeff Sharkey275e0852014-06-17 18:18:49 -07001266 throw new PackageParserException(
1267 INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES, "Package " + apkPath
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 + " has mismatched certificates at entry "
Jeff Sharkey275e0852014-06-17 18:18:49 -07001269 + entry.getName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 }
1271 }
1272 }
Todd Kennedy27c24fb2015-09-17 16:49:25 -07001273 Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
Jeff Sharkeybc097552014-09-09 14:57:26 -07001274 } catch (GeneralSecurityException e) {
Jeff Sharkey275e0852014-06-17 18:18:49 -07001275 throw new PackageParserException(INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING,
1276 "Failed to collect certificates from " + apkPath, e);
Jeff Sharkeybc097552014-09-09 14:57:26 -07001277 } catch (IOException | RuntimeException e) {
1278 throw new PackageParserException(INSTALL_PARSE_FAILED_NO_CERTIFICATES,
1279 "Failed to collect certificates from " + apkPath, e);
Jeff Sharkey275e0852014-06-17 18:18:49 -07001280 } finally {
1281 closeQuietly(jarFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 }
1284
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07001285 private static Signature[] convertToSignatures(Certificate[][] certs)
1286 throws CertificateEncodingException {
1287 final Signature[] res = new Signature[certs.length];
1288 for (int i = 0; i < certs.length; i++) {
1289 res[i] = new Signature(certs[i]);
1290 }
1291 return res;
1292 }
1293
Jeff Sharkey275e0852014-06-17 18:18:49 -07001294 /**
1295 * Utility method that retrieves lightweight details about a single APK
1296 * file, including package name, split name, and install location.
1297 *
1298 * @param apkFile path to a single APK
Jeff Sharkeybb7b7be2014-08-19 16:18:28 -07001299 * @param flags optional parse flags, such as
1300 * {@link #PARSE_COLLECT_CERTIFICATES}
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001301 */
Jeff Sharkey275e0852014-06-17 18:18:49 -07001302 public static ApkLite parseApkLite(File apkFile, int flags)
1303 throws PackageParserException {
1304 final String apkPath = apkFile.getAbsolutePath();
1305
Jeff Sharkeyda96e132014-07-15 14:54:09 -07001306 AssetManager assets = null;
Jeff Sharkey275e0852014-06-17 18:18:49 -07001307 XmlResourceParser parser = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 try {
Jeff Sharkeyda96e132014-07-15 14:54:09 -07001309 assets = new AssetManager();
1310 assets.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08001311 Build.VERSION.RESOURCES_SDK_INT);
Kenny Root1ebd74a2011-08-03 15:09:44 -07001312
Jeff Sharkeyda96e132014-07-15 14:54:09 -07001313 int cookie = assets.addAssetPath(apkPath);
Kenny Root1ebd74a2011-08-03 15:09:44 -07001314 if (cookie == 0) {
Jeff Sharkey275e0852014-06-17 18:18:49 -07001315 throw new PackageParserException(INSTALL_PARSE_FAILED_NOT_APK,
1316 "Failed to parse " + apkPath);
Kenny Root1ebd74a2011-08-03 15:09:44 -07001317 }
1318
Kenny Root05ca4c92011-09-15 10:36:25 -07001319 final DisplayMetrics metrics = new DisplayMetrics();
1320 metrics.setToDefaults();
Jeff Sharkey275e0852014-06-17 18:18:49 -07001321
Jeff Sharkeyda96e132014-07-15 14:54:09 -07001322 final Resources res = new Resources(assets, metrics, null);
1323 parser = assets.openXmlResourceParser(cookie, ANDROID_MANIFEST_FILENAME);
Kenny Root05ca4c92011-09-15 10:36:25 -07001324
Jeff Sharkey275e0852014-06-17 18:18:49 -07001325 final Signature[] signatures;
Todd Kennedyd9d438a2016-04-06 14:08:14 -07001326 final Certificate[][] certificates;
Jeff Sharkeybb7b7be2014-08-19 16:18:28 -07001327 if ((flags & PARSE_COLLECT_CERTIFICATES) != 0) {
1328 // TODO: factor signature related items out of Package object
1329 final Package tempPkg = new Package(null);
Todd Kennedy0f4bc2b2016-04-06 07:46:13 -07001330 Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "collectCertificates");
1331 try {
Todd Kennedyf720a9d2016-04-15 09:44:36 -07001332 collectCertificates(tempPkg, apkFile, 0 /*parseFlags*/);
Todd Kennedy0f4bc2b2016-04-06 07:46:13 -07001333 } finally {
1334 Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
1335 }
Jeff Sharkeybb7b7be2014-08-19 16:18:28 -07001336 signatures = tempPkg.mSignatures;
Todd Kennedyd9d438a2016-04-06 14:08:14 -07001337 certificates = tempPkg.mCertificates;
Jeff Sharkey275e0852014-06-17 18:18:49 -07001338 } else {
1339 signatures = null;
Todd Kennedyd9d438a2016-04-06 14:08:14 -07001340 certificates = null;
Jeff Sharkey275e0852014-06-17 18:18:49 -07001341 }
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07001342
Jeff Sharkey275e0852014-06-17 18:18:49 -07001343 final AttributeSet attrs = parser;
Todd Kennedyd9d438a2016-04-06 14:08:14 -07001344 return parseApkLite(apkPath, res, parser, attrs, flags, signatures, certificates);
Jeff Sharkey275e0852014-06-17 18:18:49 -07001345
1346 } catch (XmlPullParserException | IOException | RuntimeException e) {
1347 throw new PackageParserException(INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION,
1348 "Failed to parse " + apkPath, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 } finally {
Jeff Sharkeyda96e132014-07-15 14:54:09 -07001350 IoUtils.closeQuietly(parser);
1351 IoUtils.closeQuietly(assets);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 }
1354
Jeff Sharkey4f5e8b32015-06-11 19:13:37 -07001355 private static String validateName(String name, boolean requireSeparator,
1356 boolean requireFilename) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 final int N = name.length();
1358 boolean hasSep = false;
1359 boolean front = true;
1360 for (int i=0; i<N; i++) {
1361 final char c = name.charAt(i);
1362 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
1363 front = false;
1364 continue;
1365 }
1366 if (!front) {
1367 if ((c >= '0' && c <= '9') || c == '_') {
1368 continue;
1369 }
1370 }
1371 if (c == '.') {
1372 hasSep = true;
1373 front = true;
1374 continue;
1375 }
1376 return "bad character '" + c + "'";
1377 }
Jeff Sharkey4f5e8b32015-06-11 19:13:37 -07001378 if (requireFilename && !FileUtils.isValidExtFilename(name)) {
1379 return "Invalid filename";
1380 }
1381 return hasSep || !requireSeparator
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 ? null : "must have at least one '.' separator";
1383 }
1384
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07001385 private static Pair<String, String> parsePackageSplitNames(XmlPullParser parser,
Svet Ganov354cd3c2015-12-17 11:35:04 -08001386 AttributeSet attrs) throws IOException, XmlPullParserException,
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07001387 PackageParserException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388
1389 int type;
Kenny Rootd2d29252011-08-08 11:27:57 -07001390 while ((type = parser.next()) != XmlPullParser.START_TAG
1391 && type != XmlPullParser.END_DOCUMENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 }
1393
Kenny Rootd2d29252011-08-08 11:27:57 -07001394 if (type != XmlPullParser.START_TAG) {
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07001395 throw new PackageParserException(INSTALL_PARSE_FAILED_MANIFEST_MALFORMED,
1396 "No start tag found");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 }
Svet Ganov354cd3c2015-12-17 11:35:04 -08001398 if (!parser.getName().equals(TAG_MANIFEST)) {
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07001399 throw new PackageParserException(INSTALL_PARSE_FAILED_MANIFEST_MALFORMED,
1400 "No <manifest> tag");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 }
1402
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07001403 final String packageName = attrs.getAttributeValue(null, "package");
1404 if (!"android".equals(packageName)) {
Jeff Sharkey4f5e8b32015-06-11 19:13:37 -07001405 final String error = validateName(packageName, true, true);
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07001406 if (error != null) {
1407 throw new PackageParserException(INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME,
1408 "Invalid manifest package: " + error);
1409 }
1410 }
1411
Jeff Sharkey275e0852014-06-17 18:18:49 -07001412 String splitName = attrs.getAttributeValue(null, "split");
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07001413 if (splitName != null) {
Jeff Sharkey275e0852014-06-17 18:18:49 -07001414 if (splitName.length() == 0) {
1415 splitName = null;
1416 } else {
Jeff Sharkey4f5e8b32015-06-11 19:13:37 -07001417 final String error = validateName(splitName, false, false);
Jeff Sharkey275e0852014-06-17 18:18:49 -07001418 if (error != null) {
1419 throw new PackageParserException(INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME,
1420 "Invalid manifest split: " + error);
1421 }
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07001422 }
1423 }
1424
1425 return Pair.create(packageName.intern(),
1426 (splitName != null) ? splitName.intern() : splitName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 }
1428
Jeff Sharkeybe520fb2014-07-04 18:23:17 -07001429 private static ApkLite parseApkLite(String codePath, Resources res, XmlPullParser parser,
Todd Kennedyd9d438a2016-04-06 14:08:14 -07001430 AttributeSet attrs, int flags, Signature[] signatures, Certificate[][] certificates)
1431 throws IOException, XmlPullParserException, PackageParserException {
Svet Ganov354cd3c2015-12-17 11:35:04 -08001432 final Pair<String, String> packageSplit = parsePackageSplitNames(parser, attrs);
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001433
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -07001434 int installLocation = PARSE_DEFAULT_INSTALL_LOCATION;
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001435 int versionCode = 0;
Jeff Sharkey88d2a3c2014-11-22 16:49:34 -08001436 int revisionCode = 0;
Jeff Sharkeyda96e132014-07-15 14:54:09 -07001437 boolean coreApp = false;
Narayan Kamathff110bd2014-07-04 18:30:45 +01001438 boolean multiArch = false;
Selim Gurun2d291d82016-02-05 17:50:53 -08001439 boolean use32bitAbi = false;
Dmitriy Ivanovff193d62014-09-30 15:10:48 -07001440 boolean extractNativeLibs = true;
Jeff Sharkeyda96e132014-07-15 14:54:09 -07001441
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001442 for (int i = 0; i < attrs.getAttributeCount(); i++) {
Jeff Sharkey88d2a3c2014-11-22 16:49:34 -08001443 final String attr = attrs.getAttributeName(i);
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001444 if (attr.equals("installLocation")) {
1445 installLocation = attrs.getAttributeIntValue(i,
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -07001446 PARSE_DEFAULT_INSTALL_LOCATION);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001447 } else if (attr.equals("versionCode")) {
1448 versionCode = attrs.getAttributeIntValue(i, 0);
Jeff Sharkey88d2a3c2014-11-22 16:49:34 -08001449 } else if (attr.equals("revisionCode")) {
1450 revisionCode = attrs.getAttributeIntValue(i, 0);
Jeff Sharkeyda96e132014-07-15 14:54:09 -07001451 } else if (attr.equals("coreApp")) {
1452 coreApp = attrs.getAttributeBooleanValue(i, false);
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001453 }
1454 }
Kenny Root05ca4c92011-09-15 10:36:25 -07001455
1456 // Only search the tree when the tag is directly below <manifest>
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07001457 int type;
Kenny Root05ca4c92011-09-15 10:36:25 -07001458 final int searchDepth = parser.getDepth() + 1;
1459
1460 final List<VerifierInfo> verifiers = new ArrayList<VerifierInfo>();
1461 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1462 && (type != XmlPullParser.END_TAG || parser.getDepth() >= searchDepth)) {
1463 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1464 continue;
1465 }
1466
1467 if (parser.getDepth() == searchDepth && "package-verifier".equals(parser.getName())) {
Jeff Sharkey275e0852014-06-17 18:18:49 -07001468 final VerifierInfo verifier = parseVerifier(res, parser, attrs, flags);
Kenny Root05ca4c92011-09-15 10:36:25 -07001469 if (verifier != null) {
1470 verifiers.add(verifier);
1471 }
1472 }
Narayan Kamatha8755a82014-07-15 12:26:35 +01001473
1474 if (parser.getDepth() == searchDepth && "application".equals(parser.getName())) {
1475 for (int i = 0; i < attrs.getAttributeCount(); ++i) {
1476 final String attr = attrs.getAttributeName(i);
1477 if ("multiArch".equals(attr)) {
1478 multiArch = attrs.getAttributeBooleanValue(i, false);
Dmitriy Ivanovff193d62014-09-30 15:10:48 -07001479 }
Selim Gurun2d291d82016-02-05 17:50:53 -08001480 if ("use32bitAbi".equals(attr)) {
1481 use32bitAbi = attrs.getAttributeBooleanValue(i, false);
Tao Baic9a02372016-01-12 15:02:24 -08001482 }
Dmitriy Ivanovff193d62014-09-30 15:10:48 -07001483 if ("extractNativeLibs".equals(attr)) {
1484 extractNativeLibs = attrs.getAttributeBooleanValue(i, true);
Narayan Kamatha8755a82014-07-15 12:26:35 +01001485 }
1486 }
1487 }
Kenny Root05ca4c92011-09-15 10:36:25 -07001488 }
1489
Jeff Sharkeybe520fb2014-07-04 18:23:17 -07001490 return new ApkLite(codePath, packageSplit.first, packageSplit.second, versionCode,
Todd Kennedyd9d438a2016-04-06 14:08:14 -07001491 revisionCode, installLocation, verifiers, signatures, certificates, coreApp,
1492 multiArch, use32bitAbi, extractNativeLibs);
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -08001493 }
1494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 /**
1496 * Temporary.
1497 */
1498 static public Signature stringToSignature(String str) {
1499 final int N = str.length();
1500 byte[] sig = new byte[N];
1501 for (int i=0; i<N; i++) {
1502 sig[i] = (byte)str.charAt(i);
1503 }
1504 return new Signature(sig);
1505 }
1506
Jeff Sharkeyda96e132014-07-15 14:54:09 -07001507 /**
Svet Ganov354cd3c2015-12-17 11:35:04 -08001508 * Parses a child package and adds it to the parent if successful. If you add
1509 * new tags that need to be supported by child packages make sure to add them
1510 * to {@link #CHILD_PACKAGE_TAGS}.
1511 *
1512 * @param parentPkg The parent that contains the child
1513 * @param res Resources against which to resolve values
1514 * @param parser Parser of the manifest
1515 * @param flags Flags about how to parse
1516 * @param outError Human readable error if parsing fails
1517 * @return True of parsing succeeded.
1518 *
1519 * @throws XmlPullParserException
1520 * @throws IOException
1521 */
1522 private boolean parseBaseApkChild(Package parentPkg, Resources res, XmlResourceParser parser,
1523 int flags, String[] outError) throws XmlPullParserException, IOException {
1524 // Let ppl not abuse this mechanism by limiting the packages per APK
1525 if (parentPkg.childPackages != null && parentPkg.childPackages.size() + 2
1526 > MAX_PACKAGES_PER_APK) {
1527 outError[0] = "Maximum number of packages per APK is: " + MAX_PACKAGES_PER_APK;
1528 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1529 return false;
1530 }
1531
1532 // Make sure we have a valid child package name
1533 String childPackageName = parser.getAttributeValue(null, "package");
1534 if (validateName(childPackageName, true, false) != null) {
1535 mParseError = PackageManager.INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME;
1536 return false;
1537 }
1538
1539 // Child packages must be unique
1540 if (childPackageName.equals(parentPkg.packageName)) {
1541 String message = "Child package name cannot be equal to parent package name: "
1542 + parentPkg.packageName;
1543 Slog.w(TAG, message);
1544 outError[0] = message;
1545 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1546 return false;
1547 }
1548
1549 // Child packages must be unique
1550 if (parentPkg.hasChildPackage(childPackageName)) {
1551 String message = "Duplicate child package:" + childPackageName;
1552 Slog.w(TAG, message);
1553 outError[0] = message;
1554 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1555 return false;
1556 }
1557
1558 // Go ahead and parse the child
1559 Package childPkg = new Package(childPackageName);
1560
1561 // Child package inherits parent version code/name/target SDK
1562 childPkg.mVersionCode = parentPkg.mVersionCode;
1563 childPkg.baseRevisionCode = parentPkg.baseRevisionCode;
1564 childPkg.mVersionName = parentPkg.mVersionName;
1565 childPkg.applicationInfo.targetSdkVersion = parentPkg.applicationInfo.targetSdkVersion;
Todd Kennedy89d60182016-03-11 11:18:32 -08001566 childPkg.applicationInfo.minSdkVersion = parentPkg.applicationInfo.minSdkVersion;
Svet Ganov354cd3c2015-12-17 11:35:04 -08001567
1568 childPkg = parseBaseApkCommon(childPkg, CHILD_PACKAGE_TAGS, res, parser, flags, outError);
1569 if (childPkg == null) {
1570 // If we got null then error was set during child parsing
1571 return false;
1572 }
1573
1574 // Set the parent-child relation
1575 if (parentPkg.childPackages == null) {
1576 parentPkg.childPackages = new ArrayList<>();
1577 }
1578 parentPkg.childPackages.add(childPkg);
1579 childPkg.parentPackage = parentPkg;
1580
1581 return true;
1582 }
1583
1584 /**
1585 * Parse the manifest of a <em>base APK</em>. When adding new features you
1586 * need to consider whether they should be supported by split APKs and child
1587 * packages.
1588 *
1589 * @param res The resources from which to resolve values
1590 * @param parser The manifest parser
1591 * @param flags Flags how to parse
1592 * @param outError Human readable error message
1593 * @return Parsed package or null on error.
1594 *
1595 * @throws XmlPullParserException
1596 * @throws IOException
Jeff Sharkeyda96e132014-07-15 14:54:09 -07001597 */
Jeff Sharkeyc4858a22014-06-16 10:51:20 -07001598 private Package parseBaseApk(Resources res, XmlResourceParser parser, int flags,
Jeff Sharkeyda96e132014-07-15 14:54:09 -07001599 String[] outError) throws XmlPullParserException, IOException {
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07001600 final String splitName;
Svet Ganov354cd3c2015-12-17 11:35:04 -08001601 final String pkgName;
1602
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07001603 try {
Svet Ganov354cd3c2015-12-17 11:35:04 -08001604 Pair<String, String> packageSplit = parsePackageSplitNames(parser, parser);
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07001605 pkgName = packageSplit.first;
1606 splitName = packageSplit.second;
Svet Ganov354cd3c2015-12-17 11:35:04 -08001607
1608 if (!TextUtils.isEmpty(splitName)) {
1609 outError[0] = "Expected base APK, but found split " + splitName;
1610 mParseError = PackageManager.INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME;
1611 return null;
1612 }
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07001613 } catch (PackageParserException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 mParseError = PackageManager.INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME;
1615 return null;
1616 }
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07001617
Jeff Sharkeyc4858a22014-06-16 10:51:20 -07001618 final Package pkg = new Package(pkgName);
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07001619
Svet Ganov354cd3c2015-12-17 11:35:04 -08001620 TypedArray sa = res.obtainAttributes(parser,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 com.android.internal.R.styleable.AndroidManifest);
Svet Ganov354cd3c2015-12-17 11:35:04 -08001622
Dianne Hackborn8472e612014-01-23 17:57:20 -08001623 pkg.mVersionCode = pkg.applicationInfo.versionCode = sa.getInteger(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 com.android.internal.R.styleable.AndroidManifest_versionCode, 0);
Jeff Sharkey88d2a3c2014-11-22 16:49:34 -08001625 pkg.baseRevisionCode = sa.getInteger(
1626 com.android.internal.R.styleable.AndroidManifest_revisionCode, 0);
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001627 pkg.mVersionName = sa.getNonConfigurationString(
1628 com.android.internal.R.styleable.AndroidManifest_versionName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 if (pkg.mVersionName != null) {
1630 pkg.mVersionName = pkg.mVersionName.intern();
1631 }
Svet Ganov354cd3c2015-12-17 11:35:04 -08001632
1633 pkg.coreApp = parser.getAttributeBooleanValue(null, "coreApp", false);
1634
1635 sa.recycle();
1636
1637 return parseBaseApkCommon(pkg, null, res, parser, flags, outError);
1638 }
1639
1640 /**
1641 * This is the common parsing routing for handling parent and child
1642 * packages in a base APK. The difference between parent and child
Todd Kennedyfdd241a2016-04-28 12:26:53 -07001643 * parsing is that some tags are not supported by child packages as
Svet Ganov354cd3c2015-12-17 11:35:04 -08001644 * well as some manifest attributes are ignored. The implementation
Todd Kennedyfdd241a2016-04-28 12:26:53 -07001645 * assumes the calling code has already handled the manifest tag if needed
Svet Ganov354cd3c2015-12-17 11:35:04 -08001646 * (this applies to the parent only).
1647 *
1648 * @param pkg The package which to populate
1649 * @param acceptedTags Which tags to handle, null to handle all
1650 * @param res Resources against which to resolve values
1651 * @param parser Parser of the manifest
1652 * @param flags Flags about how to parse
1653 * @param outError Human readable error if parsing fails
1654 * @return The package if parsing succeeded or null.
1655 *
1656 * @throws XmlPullParserException
1657 * @throws IOException
1658 */
1659 private Package parseBaseApkCommon(Package pkg, Set<String> acceptedTags, Resources res,
1660 XmlResourceParser parser, int flags, String[] outError) throws XmlPullParserException,
1661 IOException {
Svet Ganov354cd3c2015-12-17 11:35:04 -08001662 mParseInstrumentationArgs = null;
1663 mParseActivityArgs = null;
1664 mParseServiceArgs = null;
1665 mParseProviderArgs = null;
1666
1667 int type;
1668 boolean foundApp = false;
1669
1670 TypedArray sa = res.obtainAttributes(parser,
1671 com.android.internal.R.styleable.AndroidManifest);
1672
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001673 String str = sa.getNonConfigurationString(
1674 com.android.internal.R.styleable.AndroidManifest_sharedUserId, 0);
1675 if (str != null && str.length() > 0) {
Jeff Sharkey4f5e8b32015-06-11 19:13:37 -07001676 String nameError = validateName(str, true, false);
Svet Ganov354cd3c2015-12-17 11:35:04 -08001677 if (nameError != null && !"android".equals(pkg.packageName)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 outError[0] = "<manifest> specifies bad sharedUserId name \""
1679 + str + "\": " + nameError;
1680 mParseError = PackageManager.INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID;
1681 return null;
1682 }
1683 pkg.mSharedUserId = str.intern();
1684 pkg.mSharedUserLabel = sa.getResourceId(
1685 com.android.internal.R.styleable.AndroidManifest_sharedUserLabel, 0);
1686 }
Suchi Amalapurapuaaec7792010-02-25 11:49:43 -08001687
Suchi Amalapurapu117818e2010-02-09 03:45:40 -08001688 pkg.installLocation = sa.getInteger(
1689 com.android.internal.R.styleable.AndroidManifest_installLocation,
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -07001690 PARSE_DEFAULT_INSTALL_LOCATION);
Dianne Hackborn54e570f2010-10-04 18:32:32 -07001691 pkg.applicationInfo.installLocation = pkg.installLocation;
Kenny Root7cb9be22012-05-30 15:30:37 -07001692
Alan Viverettefd274a02014-03-27 15:39:15 -07001693
Kenny Root7cb9be22012-05-30 15:30:37 -07001694 /* Set the global "forward lock" flag */
1695 if ((flags & PARSE_FORWARD_LOCK) != 0) {
Alex Klyubinb9f8a522015-02-03 11:12:59 -08001696 pkg.applicationInfo.privateFlags |= ApplicationInfo.PRIVATE_FLAG_FORWARD_LOCK;
Kenny Root7cb9be22012-05-30 15:30:37 -07001697 }
1698
1699 /* Set the global "on SD card" flag */
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -07001700 if ((flags & PARSE_EXTERNAL_STORAGE) != 0) {
Kenny Root7cb9be22012-05-30 15:30:37 -07001701 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_EXTERNAL_STORAGE;
1702 }
1703
Svet Ganov2acf0632015-11-24 19:10:59 -08001704 if ((flags & PARSE_IS_EPHEMERAL) != 0) {
1705 pkg.applicationInfo.privateFlags |= ApplicationInfo.PRIVATE_FLAG_EPHEMERAL;
1706 }
1707
Dianne Hackborn723738c2009-06-25 19:48:04 -07001708 // Resource boolean are -1, so 1 means we don't know the value.
1709 int supportsSmallScreens = 1;
1710 int supportsNormalScreens = 1;
1711 int supportsLargeScreens = 1;
Dianne Hackborn14cee9f2010-04-23 17:51:26 -07001712 int supportsXLargeScreens = 1;
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001713 int resizeable = 1;
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001714 int anyDensity = 1;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07001715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 int outerDepth = parser.getDepth();
Kenny Rootd2d29252011-08-08 11:27:57 -07001717 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1718 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1719 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 continue;
1721 }
1722
1723 String tagName = parser.getName();
Svet Ganov354cd3c2015-12-17 11:35:04 -08001724
1725 if (acceptedTags != null && !acceptedTags.contains(tagName)) {
1726 Slog.w(TAG, "Skipping unsupported element under <manifest>: "
1727 + tagName + " at " + mArchiveSourcePath + " "
1728 + parser.getPositionDescription());
1729 XmlUtils.skipCurrentTag(parser);
1730 continue;
1731 }
1732
1733 if (tagName.equals(TAG_APPLICATION)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 if (foundApp) {
1735 if (RIGID_PARSER) {
1736 outError[0] = "<manifest> has more than one <application>";
1737 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1738 return null;
1739 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -07001740 Slog.w(TAG, "<manifest> has more than one <application>");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 XmlUtils.skipCurrentTag(parser);
1742 continue;
1743 }
1744 }
1745
1746 foundApp = true;
Svet Ganov354cd3c2015-12-17 11:35:04 -08001747 if (!parseBaseApplication(pkg, res, parser, flags, outError)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 return null;
1749 }
Svet Ganov354cd3c2015-12-17 11:35:04 -08001750 } else if (tagName.equals(TAG_OVERLAY)) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08001751 sa = res.obtainAttributes(parser,
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01001752 com.android.internal.R.styleable.AndroidManifestResourceOverlay);
1753 pkg.mOverlayTarget = sa.getString(
1754 com.android.internal.R.styleable.AndroidManifestResourceOverlay_targetPackage);
1755 pkg.mOverlayPriority = sa.getInt(
1756 com.android.internal.R.styleable.AndroidManifestResourceOverlay_priority,
1757 -1);
1758 sa.recycle();
1759
1760 if (pkg.mOverlayTarget == null) {
1761 outError[0] = "<overlay> does not specify a target package";
1762 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1763 return null;
1764 }
1765 if (pkg.mOverlayPriority < 0 || pkg.mOverlayPriority > 9999) {
1766 outError[0] = "<overlay> priority must be between 0 and 9999";
1767 mParseError =
1768 PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1769 return null;
1770 }
1771 XmlUtils.skipCurrentTag(parser);
1772
Svet Ganov354cd3c2015-12-17 11:35:04 -08001773 } else if (tagName.equals(TAG_KEY_SETS)) {
1774 if (!parseKeySets(pkg, res, parser, outError)) {
Geremy Condraf1bcca82013-01-07 22:35:24 -08001775 return null;
1776 }
Svet Ganov354cd3c2015-12-17 11:35:04 -08001777 } else if (tagName.equals(TAG_PERMISSION_GROUP)) {
1778 if (parsePermissionGroup(pkg, flags, res, parser, outError) == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 return null;
1780 }
Svet Ganov354cd3c2015-12-17 11:35:04 -08001781 } else if (tagName.equals(TAG_PERMISSION)) {
1782 if (parsePermission(pkg, res, parser, outError) == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 return null;
1784 }
Svet Ganov354cd3c2015-12-17 11:35:04 -08001785 } else if (tagName.equals(TAG_PERMISSION_TREE)) {
1786 if (parsePermissionTree(pkg, res, parser, outError) == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 return null;
1788 }
Svet Ganov354cd3c2015-12-17 11:35:04 -08001789 } else if (tagName.equals(TAG_USES_PERMISSION)) {
1790 if (!parseUsesPermission(pkg, res, parser)) {
Svetoslav96a77162015-04-03 13:58:33 -07001791 return null;
1792 }
Svet Ganov354cd3c2015-12-17 11:35:04 -08001793 } else if (tagName.equals(TAG_USES_PERMISSION_SDK_M)
1794 || tagName.equals(TAG_USES_PERMISSION_SDK_23)) {
1795 if (!parseUsesPermission(pkg, res, parser)) {
Nick Kralevich73f2d3c2013-04-04 14:38:13 -07001796 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 }
Svet Ganov354cd3c2015-12-17 11:35:04 -08001798 } else if (tagName.equals(TAG_USES_CONFIGURATION)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 ConfigurationInfo cPref = new ConfigurationInfo();
Svet Ganov354cd3c2015-12-17 11:35:04 -08001800 sa = res.obtainAttributes(parser,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 com.android.internal.R.styleable.AndroidManifestUsesConfiguration);
1802 cPref.reqTouchScreen = sa.getInt(
1803 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqTouchScreen,
1804 Configuration.TOUCHSCREEN_UNDEFINED);
1805 cPref.reqKeyboardType = sa.getInt(
1806 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqKeyboardType,
1807 Configuration.KEYBOARD_UNDEFINED);
1808 if (sa.getBoolean(
1809 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqHardKeyboard,
1810 false)) {
1811 cPref.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD;
1812 }
1813 cPref.reqNavigation = sa.getInt(
1814 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqNavigation,
1815 Configuration.NAVIGATION_UNDEFINED);
1816 if (sa.getBoolean(
1817 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqFiveWayNav,
1818 false)) {
1819 cPref.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV;
1820 }
1821 sa.recycle();
Jeff Sharkeyda96e132014-07-15 14:54:09 -07001822 pkg.configPreferences = ArrayUtils.add(pkg.configPreferences, cPref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823
1824 XmlUtils.skipCurrentTag(parser);
1825
Svet Ganov354cd3c2015-12-17 11:35:04 -08001826 } else if (tagName.equals(TAG_USES_FEATURE)) {
1827 FeatureInfo fi = parseUsesFeature(res, parser);
Jeff Sharkeyda96e132014-07-15 14:54:09 -07001828 pkg.reqFeatures = ArrayUtils.add(pkg.reqFeatures, fi);
1829
Dianne Hackborn49237342009-08-27 20:08:01 -07001830 if (fi.name == null) {
1831 ConfigurationInfo cPref = new ConfigurationInfo();
1832 cPref.reqGlEsVersion = fi.reqGlEsVersion;
Jeff Sharkeyda96e132014-07-15 14:54:09 -07001833 pkg.configPreferences = ArrayUtils.add(pkg.configPreferences, cPref);
Dianne Hackborn49237342009-08-27 20:08:01 -07001834 }
Suchi Amalapurapud299b812009-06-05 10:26:19 -07001835
1836 XmlUtils.skipCurrentTag(parser);
1837
Svet Ganov354cd3c2015-12-17 11:35:04 -08001838 } else if (tagName.equals(TAG_FEATURE_GROUP)) {
Adam Lesinskid3edfde2014-08-08 17:32:44 -07001839 FeatureGroupInfo group = new FeatureGroupInfo();
1840 ArrayList<FeatureInfo> features = null;
1841 final int innerDepth = parser.getDepth();
1842 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1843 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
1844 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1845 continue;
1846 }
Adam Lesinski2c72b682014-06-24 09:56:01 -07001847
Adam Lesinskid3edfde2014-08-08 17:32:44 -07001848 final String innerTagName = parser.getName();
1849 if (innerTagName.equals("uses-feature")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08001850 FeatureInfo featureInfo = parseUsesFeature(res, parser);
Adam Lesinskid3edfde2014-08-08 17:32:44 -07001851 // FeatureGroups are stricter and mandate that
1852 // any <uses-feature> declared are mandatory.
1853 featureInfo.flags |= FeatureInfo.FLAG_REQUIRED;
1854 features = ArrayUtils.add(features, featureInfo);
1855 } else {
1856 Slog.w(TAG, "Unknown element under <feature-group>: " + innerTagName +
1857 " at " + mArchiveSourcePath + " " +
1858 parser.getPositionDescription());
1859 }
1860 XmlUtils.skipCurrentTag(parser);
1861 }
1862
1863 if (features != null) {
1864 group.features = new FeatureInfo[features.size()];
1865 group.features = features.toArray(group.features);
1866 }
1867 pkg.featureGroups = ArrayUtils.add(pkg.featureGroups, group);
Adam Lesinski2c72b682014-06-24 09:56:01 -07001868
Svet Ganov354cd3c2015-12-17 11:35:04 -08001869 } else if (tagName.equals(TAG_USES_SDK)) {
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001870 if (SDK_VERSION > 0) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08001871 sa = res.obtainAttributes(parser,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 com.android.internal.R.styleable.AndroidManifestUsesSdk);
1873
Dianne Hackborn851a5412009-05-08 12:06:44 -07001874 int minVers = 0;
1875 String minCode = null;
1876 int targetVers = 0;
1877 String targetCode = null;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07001878
Dianne Hackborn851a5412009-05-08 12:06:44 -07001879 TypedValue val = sa.peekValue(
1880 com.android.internal.R.styleable.AndroidManifestUsesSdk_minSdkVersion);
1881 if (val != null) {
1882 if (val.type == TypedValue.TYPE_STRING && val.string != null) {
1883 targetCode = minCode = val.string.toString();
1884 } else {
1885 // If it's not a string, it's an integer.
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001886 targetVers = minVers = val.data;
Dianne Hackborn851a5412009-05-08 12:06:44 -07001887 }
1888 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07001889
Dianne Hackborn851a5412009-05-08 12:06:44 -07001890 val = sa.peekValue(
1891 com.android.internal.R.styleable.AndroidManifestUsesSdk_targetSdkVersion);
1892 if (val != null) {
1893 if (val.type == TypedValue.TYPE_STRING && val.string != null) {
Todd Kennedy89d60182016-03-11 11:18:32 -08001894 targetCode = val.string.toString();
1895 if (minCode == null) {
1896 minCode = targetCode;
1897 }
Dianne Hackborn851a5412009-05-08 12:06:44 -07001898 } else {
1899 // If it's not a string, it's an integer.
1900 targetVers = val.data;
Todd Kennedy89d60182016-03-11 11:18:32 -08001901 if (minVers == 0) {
1902 minVers = targetVers;
1903 }
Dianne Hackborn851a5412009-05-08 12:06:44 -07001904 }
1905 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07001906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 sa.recycle();
1908
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001909 if (minCode != null) {
Dianne Hackbornffcda102014-04-24 13:06:27 -07001910 boolean allowedCodename = false;
1911 for (String codename : SDK_CODENAMES) {
1912 if (minCode.equals(codename)) {
1913 allowedCodename = true;
1914 break;
1915 }
1916 }
1917 if (!allowedCodename) {
1918 if (SDK_CODENAMES.length > 0) {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001919 outError[0] = "Requires development platform " + minCode
Dianne Hackbornffcda102014-04-24 13:06:27 -07001920 + " (current platform is any of "
1921 + Arrays.toString(SDK_CODENAMES) + ")";
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001922 } else {
1923 outError[0] = "Requires development platform " + minCode
1924 + " but this is a release platform.";
1925 }
1926 mParseError = PackageManager.INSTALL_FAILED_OLDER_SDK;
1927 return null;
1928 }
Todd Kennedy89d60182016-03-11 11:18:32 -08001929 pkg.applicationInfo.minSdkVersion = minCode;
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001930 } else if (minVers > SDK_VERSION) {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001931 outError[0] = "Requires newer sdk version #" + minVers
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001932 + " (current version is #" + SDK_VERSION + ")";
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001933 mParseError = PackageManager.INSTALL_FAILED_OLDER_SDK;
1934 return null;
Todd Kennedy89d60182016-03-11 11:18:32 -08001935 } else {
1936 pkg.applicationInfo.minSdkVersion = Integer.toString(minVers);
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001937 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07001938
Dianne Hackborn851a5412009-05-08 12:06:44 -07001939 if (targetCode != null) {
Dianne Hackbornffcda102014-04-24 13:06:27 -07001940 boolean allowedCodename = false;
1941 for (String codename : SDK_CODENAMES) {
1942 if (targetCode.equals(codename)) {
1943 allowedCodename = true;
1944 break;
1945 }
1946 }
1947 if (!allowedCodename) {
1948 if (SDK_CODENAMES.length > 0) {
Dianne Hackborn851a5412009-05-08 12:06:44 -07001949 outError[0] = "Requires development platform " + targetCode
Dianne Hackbornffcda102014-04-24 13:06:27 -07001950 + " (current platform is any of "
1951 + Arrays.toString(SDK_CODENAMES) + ")";
Dianne Hackborn851a5412009-05-08 12:06:44 -07001952 } else {
1953 outError[0] = "Requires development platform " + targetCode
1954 + " but this is a release platform.";
1955 }
1956 mParseError = PackageManager.INSTALL_FAILED_OLDER_SDK;
1957 return null;
1958 }
1959 // If the code matches, it definitely targets this SDK.
Dianne Hackborna96cbb42009-05-13 15:06:13 -07001960 pkg.applicationInfo.targetSdkVersion
1961 = android.os.Build.VERSION_CODES.CUR_DEVELOPMENT;
1962 } else {
1963 pkg.applicationInfo.targetSdkVersion = targetVers;
Dianne Hackborn851a5412009-05-08 12:06:44 -07001964 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 }
1966
1967 XmlUtils.skipCurrentTag(parser);
1968
Svet Ganov354cd3c2015-12-17 11:35:04 -08001969 } else if (tagName.equals(TAG_SUPPORT_SCREENS)) {
1970 sa = res.obtainAttributes(parser,
Dianne Hackborn723738c2009-06-25 19:48:04 -07001971 com.android.internal.R.styleable.AndroidManifestSupportsScreens);
1972
Dianne Hackborndf6e9802011-05-26 14:20:23 -07001973 pkg.applicationInfo.requiresSmallestWidthDp = sa.getInteger(
1974 com.android.internal.R.styleable.AndroidManifestSupportsScreens_requiresSmallestWidthDp,
1975 0);
1976 pkg.applicationInfo.compatibleWidthLimitDp = sa.getInteger(
1977 com.android.internal.R.styleable.AndroidManifestSupportsScreens_compatibleWidthLimitDp,
1978 0);
Dianne Hackborn2762ff32011-06-01 21:27:05 -07001979 pkg.applicationInfo.largestWidthLimitDp = sa.getInteger(
1980 com.android.internal.R.styleable.AndroidManifestSupportsScreens_largestWidthLimitDp,
1981 0);
Dianne Hackborndf6e9802011-05-26 14:20:23 -07001982
Dianne Hackborn723738c2009-06-25 19:48:04 -07001983 // This is a trick to get a boolean and still able to detect
1984 // if a value was actually set.
1985 supportsSmallScreens = sa.getInteger(
1986 com.android.internal.R.styleable.AndroidManifestSupportsScreens_smallScreens,
1987 supportsSmallScreens);
1988 supportsNormalScreens = sa.getInteger(
1989 com.android.internal.R.styleable.AndroidManifestSupportsScreens_normalScreens,
1990 supportsNormalScreens);
1991 supportsLargeScreens = sa.getInteger(
1992 com.android.internal.R.styleable.AndroidManifestSupportsScreens_largeScreens,
1993 supportsLargeScreens);
Dianne Hackborn14cee9f2010-04-23 17:51:26 -07001994 supportsXLargeScreens = sa.getInteger(
1995 com.android.internal.R.styleable.AndroidManifestSupportsScreens_xlargeScreens,
1996 supportsXLargeScreens);
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001997 resizeable = sa.getInteger(
1998 com.android.internal.R.styleable.AndroidManifestSupportsScreens_resizeable,
Dianne Hackborn14cee9f2010-04-23 17:51:26 -07001999 resizeable);
Dianne Hackborn11b822d2009-07-21 20:03:02 -07002000 anyDensity = sa.getInteger(
2001 com.android.internal.R.styleable.AndroidManifestSupportsScreens_anyDensity,
2002 anyDensity);
Dianne Hackborn723738c2009-06-25 19:48:04 -07002003
2004 sa.recycle();
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002005
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002006 XmlUtils.skipCurrentTag(parser);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002007
Svet Ganov354cd3c2015-12-17 11:35:04 -08002008 } else if (tagName.equals(TAG_PROTECTED_BROADCAST)) {
2009 sa = res.obtainAttributes(parser,
Dianne Hackborn854060af2009-07-09 18:14:31 -07002010 com.android.internal.R.styleable.AndroidManifestProtectedBroadcast);
2011
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002012 // Note: don't allow this value to be a reference to a resource
2013 // that may change.
Dianne Hackborn854060af2009-07-09 18:14:31 -07002014 String name = sa.getNonResourceString(
2015 com.android.internal.R.styleable.AndroidManifestProtectedBroadcast_name);
2016
2017 sa.recycle();
2018
2019 if (name != null && (flags&PARSE_IS_SYSTEM) != 0) {
2020 if (pkg.protectedBroadcasts == null) {
2021 pkg.protectedBroadcasts = new ArrayList<String>();
2022 }
2023 if (!pkg.protectedBroadcasts.contains(name)) {
2024 pkg.protectedBroadcasts.add(name.intern());
2025 }
2026 }
2027
2028 XmlUtils.skipCurrentTag(parser);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002029
Svet Ganov354cd3c2015-12-17 11:35:04 -08002030 } else if (tagName.equals(TAG_INSTRUMENTATION)) {
2031 if (parseInstrumentation(pkg, res, parser, outError) == null) {
Dianne Hackborn854060af2009-07-09 18:14:31 -07002032 return null;
2033 }
Svet Ganov354cd3c2015-12-17 11:35:04 -08002034 } else if (tagName.equals(TAG_ORIGINAL_PACKAGE)) {
2035 sa = res.obtainAttributes(parser,
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08002036 com.android.internal.R.styleable.AndroidManifestOriginalPackage);
2037
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002038 String orig =sa.getNonConfigurationString(
2039 com.android.internal.R.styleable.AndroidManifestOriginalPackage_name, 0);
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08002040 if (!pkg.packageName.equals(orig)) {
Dianne Hackbornc1552392010-03-03 16:19:01 -08002041 if (pkg.mOriginalPackages == null) {
2042 pkg.mOriginalPackages = new ArrayList<String>();
2043 pkg.mRealPackage = pkg.packageName;
2044 }
2045 pkg.mOriginalPackages.add(orig);
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08002046 }
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08002047
2048 sa.recycle();
2049
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08002050 XmlUtils.skipCurrentTag(parser);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002051
Svet Ganov354cd3c2015-12-17 11:35:04 -08002052 } else if (tagName.equals(TAG_ADOPT_PERMISSIONS)) {
2053 sa = res.obtainAttributes(parser,
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08002054 com.android.internal.R.styleable.AndroidManifestOriginalPackage);
2055
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002056 String name = sa.getNonConfigurationString(
2057 com.android.internal.R.styleable.AndroidManifestOriginalPackage_name, 0);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08002058
2059 sa.recycle();
2060
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08002061 if (name != null) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08002062 if (pkg.mAdoptPermissions == null) {
2063 pkg.mAdoptPermissions = new ArrayList<String>();
2064 }
2065 pkg.mAdoptPermissions.add(name);
2066 }
2067
2068 XmlUtils.skipCurrentTag(parser);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002069
Svet Ganov354cd3c2015-12-17 11:35:04 -08002070 } else if (tagName.equals(TAG_USES_GL_TEXTURE)) {
Dianne Hackborna0b46c92010-10-21 15:32:06 -07002071 // Just skip this tag
2072 XmlUtils.skipCurrentTag(parser);
2073 continue;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002074
Svet Ganov354cd3c2015-12-17 11:35:04 -08002075 } else if (tagName.equals(TAG_COMPATIBLE_SCREENS)) {
Dianne Hackborna0b46c92010-10-21 15:32:06 -07002076 // Just skip this tag
2077 XmlUtils.skipCurrentTag(parser);
2078 continue;
Svet Ganov354cd3c2015-12-17 11:35:04 -08002079 } else if (tagName.equals(TAG_SUPPORTS_INPUT)) {//
Michael Wrighteaeb1902013-09-05 18:15:57 -07002080 XmlUtils.skipCurrentTag(parser);
2081 continue;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002082
Svet Ganov354cd3c2015-12-17 11:35:04 -08002083 } else if (tagName.equals(TAG_EAT_COMMENT)) {
Dianne Hackborn854060af2009-07-09 18:14:31 -07002084 // Just skip this tag
2085 XmlUtils.skipCurrentTag(parser);
2086 continue;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002087
Svet Ganov354cd3c2015-12-17 11:35:04 -08002088 } else if (tagName.equals(TAG_PACKAGE)) {
Svet Ganova7532cf2016-05-02 08:13:17 -07002089 if (!MULTI_PACKAGE_APK_ENABLED) {
2090 XmlUtils.skipCurrentTag(parser);
2091 continue;
2092 }
Svet Ganov354cd3c2015-12-17 11:35:04 -08002093 if (!parseBaseApkChild(pkg, res, parser, flags, outError)) {
2094 // If parsing a child failed the error is already set
2095 return null;
2096 }
Todd Kennedyfdd241a2016-04-28 12:26:53 -07002097
2098 } else if (tagName.equals(TAG_RESTRICT_UPDATE)) {
2099 if ((flags & PARSE_IS_SYSTEM_DIR) != 0) {
2100 sa = res.obtainAttributes(parser,
2101 com.android.internal.R.styleable.AndroidManifestRestrictUpdate);
2102 final String hash = sa.getNonConfigurationString(
2103 com.android.internal.R.styleable.AndroidManifestRestrictUpdate_hash, 0);
2104 sa.recycle();
2105
2106 pkg.restrictUpdateHash = null;
2107 if (hash != null) {
2108 final int hashLength = hash.length();
2109 final byte[] hashBytes = new byte[hashLength / 2];
2110 for (int i = 0; i < hashLength; i += 2){
2111 hashBytes[i/2] = (byte) ((Character.digit(hash.charAt(i), 16) << 4)
2112 + Character.digit(hash.charAt(i + 1), 16));
2113 }
2114 pkg.restrictUpdateHash = hashBytes;
2115 }
2116 }
2117
2118 XmlUtils.skipCurrentTag(parser);
2119
Dianne Hackborn854060af2009-07-09 18:14:31 -07002120 } else if (RIGID_PARSER) {
2121 outError[0] = "Bad element under <manifest>: "
2122 + parser.getName();
2123 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
2124 return null;
2125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -07002127 Slog.w(TAG, "Unknown element under <manifest>: " + parser.getName()
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -07002128 + " at " + mArchiveSourcePath + " "
2129 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 XmlUtils.skipCurrentTag(parser);
2131 continue;
2132 }
2133 }
2134
2135 if (!foundApp && pkg.instrumentation.size() == 0) {
2136 outError[0] = "<manifest> does not contain an <application> or <instrumentation>";
2137 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_EMPTY;
2138 }
2139
Dianne Hackborna96cbb42009-05-13 15:06:13 -07002140 final int NP = PackageParser.NEW_PERMISSIONS.length;
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002141 StringBuilder implicitPerms = null;
Dianne Hackborna96cbb42009-05-13 15:06:13 -07002142 for (int ip=0; ip<NP; ip++) {
2143 final PackageParser.NewPermissionInfo npi
2144 = PackageParser.NEW_PERMISSIONS[ip];
2145 if (pkg.applicationInfo.targetSdkVersion >= npi.sdkVersion) {
2146 break;
2147 }
2148 if (!pkg.requestedPermissions.contains(npi.name)) {
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002149 if (implicitPerms == null) {
2150 implicitPerms = new StringBuilder(128);
2151 implicitPerms.append(pkg.packageName);
2152 implicitPerms.append(": compat added ");
2153 } else {
2154 implicitPerms.append(' ');
2155 }
2156 implicitPerms.append(npi.name);
Dianne Hackborna96cbb42009-05-13 15:06:13 -07002157 pkg.requestedPermissions.add(npi.name);
2158 }
Dianne Hackborn851a5412009-05-08 12:06:44 -07002159 }
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002160 if (implicitPerms != null) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002161 Slog.i(TAG, implicitPerms.toString());
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002162 }
Dianne Hackborn79245122012-03-12 10:51:26 -07002163
2164 final int NS = PackageParser.SPLIT_PERMISSIONS.length;
2165 for (int is=0; is<NS; is++) {
2166 final PackageParser.SplitPermissionInfo spi
2167 = PackageParser.SPLIT_PERMISSIONS[is];
Dianne Hackborn31b0e0e2012-04-05 19:33:30 -07002168 if (pkg.applicationInfo.targetSdkVersion >= spi.targetSdk
2169 || !pkg.requestedPermissions.contains(spi.rootPerm)) {
Dianne Hackborn5e4705a2012-04-06 12:55:53 -07002170 continue;
Dianne Hackborn79245122012-03-12 10:51:26 -07002171 }
2172 for (int in=0; in<spi.newPerms.length; in++) {
2173 final String perm = spi.newPerms[in];
2174 if (!pkg.requestedPermissions.contains(perm)) {
2175 pkg.requestedPermissions.add(perm);
Dianne Hackborn79245122012-03-12 10:51:26 -07002176 }
2177 }
2178 }
2179
Dianne Hackborn723738c2009-06-25 19:48:04 -07002180 if (supportsSmallScreens < 0 || (supportsSmallScreens > 0
2181 && pkg.applicationInfo.targetSdkVersion
Dianne Hackborn11b822d2009-07-21 20:03:02 -07002182 >= android.os.Build.VERSION_CODES.DONUT)) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07002183 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_SMALL_SCREENS;
2184 }
2185 if (supportsNormalScreens != 0) {
2186 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_NORMAL_SCREENS;
2187 }
2188 if (supportsLargeScreens < 0 || (supportsLargeScreens > 0
2189 && pkg.applicationInfo.targetSdkVersion
Dianne Hackborn11b822d2009-07-21 20:03:02 -07002190 >= android.os.Build.VERSION_CODES.DONUT)) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07002191 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_LARGE_SCREENS;
2192 }
Dianne Hackborn14cee9f2010-04-23 17:51:26 -07002193 if (supportsXLargeScreens < 0 || (supportsXLargeScreens > 0
2194 && pkg.applicationInfo.targetSdkVersion
2195 >= android.os.Build.VERSION_CODES.GINGERBREAD)) {
2196 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_XLARGE_SCREENS;
2197 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07002198 if (resizeable < 0 || (resizeable > 0
2199 && pkg.applicationInfo.targetSdkVersion
Dianne Hackborn11b822d2009-07-21 20:03:02 -07002200 >= android.os.Build.VERSION_CODES.DONUT)) {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07002201 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_RESIZEABLE_FOR_SCREENS;
2202 }
Dianne Hackborn11b822d2009-07-21 20:03:02 -07002203 if (anyDensity < 0 || (anyDensity > 0
2204 && pkg.applicationInfo.targetSdkVersion
2205 >= android.os.Build.VERSION_CODES.DONUT)) {
2206 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES;
Mitsuru Oshima8d112672009-04-27 12:01:23 -07002207 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07002208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 return pkg;
2210 }
2211
Todd Kennedy27c24fb2015-09-17 16:49:25 -07002212 private FeatureInfo parseUsesFeature(Resources res, AttributeSet attrs) {
Adam Lesinskid3edfde2014-08-08 17:32:44 -07002213 FeatureInfo fi = new FeatureInfo();
2214 TypedArray sa = res.obtainAttributes(attrs,
2215 com.android.internal.R.styleable.AndroidManifestUsesFeature);
2216 // Note: don't allow this value to be a reference to a resource
2217 // that may change.
2218 fi.name = sa.getNonResourceString(
2219 com.android.internal.R.styleable.AndroidManifestUsesFeature_name);
Jeff Sharkey115d2c12016-02-15 17:25:57 -07002220 fi.version = sa.getInt(
2221 com.android.internal.R.styleable.AndroidManifestUsesFeature_version, 0);
Adam Lesinskid3edfde2014-08-08 17:32:44 -07002222 if (fi.name == null) {
2223 fi.reqGlEsVersion = sa.getInt(
2224 com.android.internal.R.styleable.AndroidManifestUsesFeature_glEsVersion,
2225 FeatureInfo.GL_ES_VERSION_UNDEFINED);
2226 }
2227 if (sa.getBoolean(
2228 com.android.internal.R.styleable.AndroidManifestUsesFeature_required, true)) {
2229 fi.flags |= FeatureInfo.FLAG_REQUIRED;
2230 }
2231 sa.recycle();
2232 return fi;
2233 }
2234
Svet Ganov354cd3c2015-12-17 11:35:04 -08002235 private boolean parseUsesPermission(Package pkg, Resources res, XmlResourceParser parser)
2236 throws XmlPullParserException, IOException {
2237 TypedArray sa = res.obtainAttributes(parser,
Nick Kralevich73f2d3c2013-04-04 14:38:13 -07002238 com.android.internal.R.styleable.AndroidManifestUsesPermission);
2239
2240 // Note: don't allow this value to be a reference to a resource
2241 // that may change.
2242 String name = sa.getNonResourceString(
2243 com.android.internal.R.styleable.AndroidManifestUsesPermission_name);
Nick Kralevich73f2d3c2013-04-04 14:38:13 -07002244
Christopher Tatefb0676a2013-09-16 16:34:52 -07002245 int maxSdkVersion = 0;
2246 TypedValue val = sa.peekValue(
2247 com.android.internal.R.styleable.AndroidManifestUsesPermission_maxSdkVersion);
2248 if (val != null) {
2249 if (val.type >= TypedValue.TYPE_FIRST_INT && val.type <= TypedValue.TYPE_LAST_INT) {
2250 maxSdkVersion = val.data;
2251 }
2252 }
2253
Nick Kralevich73f2d3c2013-04-04 14:38:13 -07002254 sa.recycle();
2255
Christopher Tatefb0676a2013-09-16 16:34:52 -07002256 if ((maxSdkVersion == 0) || (maxSdkVersion >= Build.VERSION.RESOURCES_SDK_INT)) {
2257 if (name != null) {
2258 int index = pkg.requestedPermissions.indexOf(name);
2259 if (index == -1) {
2260 pkg.requestedPermissions.add(name.intern());
Christopher Tatefb0676a2013-09-16 16:34:52 -07002261 } else {
Svetoslav96a77162015-04-03 13:58:33 -07002262 Slog.w(TAG, "Ignoring duplicate uses-permissions/uses-permissions-sdk-m: "
2263 + name + " in package: " + pkg.packageName + " at: "
2264 + parser.getPositionDescription());
Nick Kralevich73f2d3c2013-04-04 14:38:13 -07002265 }
2266 }
2267 }
2268
2269 XmlUtils.skipCurrentTag(parser);
2270 return true;
2271 }
2272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 private static String buildClassName(String pkg, CharSequence clsSeq,
2274 String[] outError) {
2275 if (clsSeq == null || clsSeq.length() <= 0) {
2276 outError[0] = "Empty class name in package " + pkg;
2277 return null;
2278 }
2279 String cls = clsSeq.toString();
2280 char c = cls.charAt(0);
2281 if (c == '.') {
2282 return (pkg + cls).intern();
2283 }
2284 if (cls.indexOf('.') < 0) {
2285 StringBuilder b = new StringBuilder(pkg);
2286 b.append('.');
2287 b.append(cls);
2288 return b.toString().intern();
2289 }
2290 if (c >= 'a' && c <= 'z') {
2291 return cls.intern();
2292 }
2293 outError[0] = "Bad class name " + cls + " in package " + pkg;
2294 return null;
2295 }
2296
2297 private static String buildCompoundName(String pkg,
2298 CharSequence procSeq, String type, String[] outError) {
2299 String proc = procSeq.toString();
2300 char c = proc.charAt(0);
2301 if (pkg != null && c == ':') {
2302 if (proc.length() < 2) {
2303 outError[0] = "Bad " + type + " name " + proc + " in package " + pkg
2304 + ": must be at least two characters";
2305 return null;
2306 }
2307 String subName = proc.substring(1);
Jeff Sharkey4f5e8b32015-06-11 19:13:37 -07002308 String nameError = validateName(subName, false, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 if (nameError != null) {
2310 outError[0] = "Invalid " + type + " name " + proc + " in package "
2311 + pkg + ": " + nameError;
2312 return null;
2313 }
2314 return (pkg + proc).intern();
2315 }
Jeff Sharkey4f5e8b32015-06-11 19:13:37 -07002316 String nameError = validateName(proc, true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002317 if (nameError != null && !"system".equals(proc)) {
2318 outError[0] = "Invalid " + type + " name " + proc + " in package "
2319 + pkg + ": " + nameError;
2320 return null;
2321 }
2322 return proc.intern();
2323 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 private static String buildProcessName(String pkg, String defProc,
2326 CharSequence procSeq, int flags, String[] separateProcesses,
2327 String[] outError) {
2328 if ((flags&PARSE_IGNORE_PROCESSES) != 0 && !"system".equals(procSeq)) {
2329 return defProc != null ? defProc : pkg;
2330 }
2331 if (separateProcesses != null) {
2332 for (int i=separateProcesses.length-1; i>=0; i--) {
2333 String sp = separateProcesses[i];
2334 if (sp.equals(pkg) || sp.equals(defProc) || sp.equals(procSeq)) {
2335 return pkg;
2336 }
2337 }
2338 }
2339 if (procSeq == null || procSeq.length() <= 0) {
2340 return defProc;
2341 }
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07002342 return buildCompoundName(pkg, procSeq, "process", outError);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 }
2344
2345 private static String buildTaskAffinityName(String pkg, String defProc,
2346 CharSequence procSeq, String[] outError) {
2347 if (procSeq == null) {
2348 return defProc;
2349 }
2350 if (procSeq.length() <= 0) {
2351 return null;
2352 }
2353 return buildCompoundName(pkg, procSeq, "taskAffinity", outError);
2354 }
Geremy Condraf1bcca82013-01-07 22:35:24 -08002355
dcashman989eb3712014-06-17 12:56:12 -07002356 private boolean parseKeySets(Package owner, Resources res,
Svet Ganov354cd3c2015-12-17 11:35:04 -08002357 XmlResourceParser parser, String[] outError)
Geremy Condraf1bcca82013-01-07 22:35:24 -08002358 throws XmlPullParserException, IOException {
dcashman989eb3712014-06-17 12:56:12 -07002359 // we've encountered the 'key-sets' tag
Geremy Condraf1bcca82013-01-07 22:35:24 -08002360 // all the keys and keysets that we want must be defined here
2361 // so we're going to iterate over the parser and pull out the things we want
2362 int outerDepth = parser.getDepth();
dcashman989eb3712014-06-17 12:56:12 -07002363 int currentKeySetDepth = -1;
Geremy Condraf1bcca82013-01-07 22:35:24 -08002364 int type;
dcashman989eb3712014-06-17 12:56:12 -07002365 String currentKeySet = null;
2366 ArrayMap<String, PublicKey> publicKeys = new ArrayMap<String, PublicKey>();
2367 ArraySet<String> upgradeKeySets = new ArraySet<String>();
2368 ArrayMap<String, ArraySet<String>> definedKeySets = new ArrayMap<String, ArraySet<String>>();
2369 ArraySet<String> improperKeySets = new ArraySet<String>();
Geremy Condraf1bcca82013-01-07 22:35:24 -08002370 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
2371 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
2372 if (type == XmlPullParser.END_TAG) {
dcashman989eb3712014-06-17 12:56:12 -07002373 if (parser.getDepth() == currentKeySetDepth) {
2374 currentKeySet = null;
2375 currentKeySetDepth = -1;
Kenny Root37dca152013-07-10 14:01:49 -07002376 }
Geremy Condraf1bcca82013-01-07 22:35:24 -08002377 continue;
2378 }
dcashman989eb3712014-06-17 12:56:12 -07002379 String tagName = parser.getName();
2380 if (tagName.equals("key-set")) {
2381 if (currentKeySet != null) {
dcashman5db0cd42015-05-22 15:24:40 -07002382 outError[0] = "Improperly nested 'key-set' tag at "
2383 + parser.getPositionDescription();
2384 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
dcashman989eb3712014-06-17 12:56:12 -07002385 return false;
2386 }
Svet Ganov354cd3c2015-12-17 11:35:04 -08002387 final TypedArray sa = res.obtainAttributes(parser,
dcashman989eb3712014-06-17 12:56:12 -07002388 com.android.internal.R.styleable.AndroidManifestKeySet);
2389 final String keysetName = sa.getNonResourceString(
2390 com.android.internal.R.styleable.AndroidManifestKeySet_name);
2391 definedKeySets.put(keysetName, new ArraySet<String>());
2392 currentKeySet = keysetName;
2393 currentKeySetDepth = parser.getDepth();
2394 sa.recycle();
2395 } else if (tagName.equals("public-key")) {
2396 if (currentKeySet == null) {
dcashman5db0cd42015-05-22 15:24:40 -07002397 outError[0] = "Improperly nested 'key-set' tag at "
2398 + parser.getPositionDescription();
2399 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
dcashman989eb3712014-06-17 12:56:12 -07002400 return false;
2401 }
Svet Ganov354cd3c2015-12-17 11:35:04 -08002402 final TypedArray sa = res.obtainAttributes(parser,
dcashman989eb3712014-06-17 12:56:12 -07002403 com.android.internal.R.styleable.AndroidManifestPublicKey);
2404 final String publicKeyName = sa.getNonResourceString(
2405 com.android.internal.R.styleable.AndroidManifestPublicKey_name);
Geremy Condraf1bcca82013-01-07 22:35:24 -08002406 final String encodedKey = sa.getNonResourceString(
dcashman989eb3712014-06-17 12:56:12 -07002407 com.android.internal.R.styleable.AndroidManifestPublicKey_value);
2408 if (encodedKey == null && publicKeys.get(publicKeyName) == null) {
dcashman5db0cd42015-05-22 15:24:40 -07002409 outError[0] = "'public-key' " + publicKeyName + " must define a public-key value"
2410 + " on first use at " + parser.getPositionDescription();
2411 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
Kenny Root37dca152013-07-10 14:01:49 -07002412 sa.recycle();
dcashman989eb3712014-06-17 12:56:12 -07002413 return false;
2414 } else if (encodedKey != null) {
2415 PublicKey currentKey = parsePublicKey(encodedKey);
2416 if (currentKey == null) {
2417 Slog.w(TAG, "No recognized valid key in 'public-key' tag at "
2418 + parser.getPositionDescription() + " key-set " + currentKeySet
2419 + " will not be added to the package's defined key-sets.");
2420 sa.recycle();
2421 improperKeySets.add(currentKeySet);
2422 XmlUtils.skipCurrentTag(parser);
2423 continue;
2424 }
2425 if (publicKeys.get(publicKeyName) == null
2426 || publicKeys.get(publicKeyName).equals(currentKey)) {
2427
2428 /* public-key first definition, or matches old definition */
2429 publicKeys.put(publicKeyName, currentKey);
2430 } else {
dcashman5db0cd42015-05-22 15:24:40 -07002431 outError[0] = "Value of 'public-key' " + publicKeyName
dcashman989eb3712014-06-17 12:56:12 -07002432 + " conflicts with previously defined value at "
dcashman5db0cd42015-05-22 15:24:40 -07002433 + parser.getPositionDescription();
2434 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
dcashman989eb3712014-06-17 12:56:12 -07002435 sa.recycle();
2436 return false;
2437 }
Kenny Root37dca152013-07-10 14:01:49 -07002438 }
dcashman989eb3712014-06-17 12:56:12 -07002439 definedKeySets.get(currentKeySet).add(publicKeyName);
Geremy Condraf1bcca82013-01-07 22:35:24 -08002440 sa.recycle();
dcashman989eb3712014-06-17 12:56:12 -07002441 XmlUtils.skipCurrentTag(parser);
2442 } else if (tagName.equals("upgrade-key-set")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08002443 final TypedArray sa = res.obtainAttributes(parser,
dcashman989eb3712014-06-17 12:56:12 -07002444 com.android.internal.R.styleable.AndroidManifestUpgradeKeySet);
2445 String name = sa.getNonResourceString(
2446 com.android.internal.R.styleable.AndroidManifestUpgradeKeySet_name);
2447 upgradeKeySets.add(name);
Geremy Condraf1bcca82013-01-07 22:35:24 -08002448 sa.recycle();
dcashman989eb3712014-06-17 12:56:12 -07002449 XmlUtils.skipCurrentTag(parser);
Geremy Condraf1bcca82013-01-07 22:35:24 -08002450 } else if (RIGID_PARSER) {
dcashman5db0cd42015-05-22 15:24:40 -07002451 outError[0] = "Bad element under <key-sets>: " + parser.getName()
Geremy Condraf1bcca82013-01-07 22:35:24 -08002452 + " at " + mArchiveSourcePath + " "
dcashman5db0cd42015-05-22 15:24:40 -07002453 + parser.getPositionDescription();
2454 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
Geremy Condraf1bcca82013-01-07 22:35:24 -08002455 return false;
2456 } else {
dcashman989eb3712014-06-17 12:56:12 -07002457 Slog.w(TAG, "Unknown element under <key-sets>: " + parser.getName()
Geremy Condraf1bcca82013-01-07 22:35:24 -08002458 + " at " + mArchiveSourcePath + " "
2459 + parser.getPositionDescription());
2460 XmlUtils.skipCurrentTag(parser);
2461 continue;
2462 }
2463 }
dcashman989eb3712014-06-17 12:56:12 -07002464 Set<String> publicKeyNames = publicKeys.keySet();
2465 if (publicKeyNames.removeAll(definedKeySets.keySet())) {
dcashman5db0cd42015-05-22 15:24:40 -07002466 outError[0] = "Package" + owner.packageName + " AndroidManifext.xml "
2467 + "'key-set' and 'public-key' names must be distinct.";
2468 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
dcashman989eb3712014-06-17 12:56:12 -07002469 return false;
2470 }
2471 owner.mKeySetMapping = new ArrayMap<String, ArraySet<PublicKey>>();
2472 for (ArrayMap.Entry<String, ArraySet<String>> e: definedKeySets.entrySet()) {
2473 final String keySetName = e.getKey();
2474 if (e.getValue().size() == 0) {
2475 Slog.w(TAG, "Package" + owner.packageName + " AndroidManifext.xml "
2476 + "'key-set' " + keySetName + " has no valid associated 'public-key'."
2477 + " Not including in package's defined key-sets.");
2478 continue;
2479 } else if (improperKeySets.contains(keySetName)) {
2480 Slog.w(TAG, "Package" + owner.packageName + " AndroidManifext.xml "
2481 + "'key-set' " + keySetName + " contained improper 'public-key'"
2482 + " tags. Not including in package's defined key-sets.");
2483 continue;
2484 }
2485 owner.mKeySetMapping.put(keySetName, new ArraySet<PublicKey>());
2486 for (String s : e.getValue()) {
2487 owner.mKeySetMapping.get(keySetName).add(publicKeys.get(s));
Geremy Condraf1bcca82013-01-07 22:35:24 -08002488 }
2489 }
dcashman989eb3712014-06-17 12:56:12 -07002490 if (owner.mKeySetMapping.keySet().containsAll(upgradeKeySets)) {
2491 owner.mUpgradeKeySets = upgradeKeySets;
2492 } else {
dcashman5db0cd42015-05-22 15:24:40 -07002493 outError[0] ="Package" + owner.packageName + " AndroidManifext.xml "
2494 + "does not define all 'upgrade-key-set's .";
2495 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
dcashman989eb3712014-06-17 12:56:12 -07002496 return false;
2497 }
Geremy Condraf1bcca82013-01-07 22:35:24 -08002498 return true;
2499 }
2500
Dianne Hackbornfd5015b2012-04-30 16:33:56 -07002501 private PermissionGroup parsePermissionGroup(Package owner, int flags, Resources res,
Svet Ganov354cd3c2015-12-17 11:35:04 -08002502 XmlResourceParser parser, String[] outError)
2503 throws XmlPullParserException, IOException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 PermissionGroup perm = new PermissionGroup(owner);
2505
Svet Ganov354cd3c2015-12-17 11:35:04 -08002506 TypedArray sa = res.obtainAttributes(parser,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 com.android.internal.R.styleable.AndroidManifestPermissionGroup);
2508
2509 if (!parsePackageItemInfo(owner, perm.info, outError,
2510 "<permission-group>", sa,
2511 com.android.internal.R.styleable.AndroidManifestPermissionGroup_name,
2512 com.android.internal.R.styleable.AndroidManifestPermissionGroup_label,
Adam Powell81cd2e92010-04-21 16:35:18 -07002513 com.android.internal.R.styleable.AndroidManifestPermissionGroup_icon,
Jose Limaf78e3122014-03-06 12:13:15 -08002514 com.android.internal.R.styleable.AndroidManifestPermissionGroup_logo,
2515 com.android.internal.R.styleable.AndroidManifestPermissionGroup_banner)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 sa.recycle();
2517 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
2518 return null;
2519 }
2520
2521 perm.info.descriptionRes = sa.getResourceId(
2522 com.android.internal.R.styleable.AndroidManifestPermissionGroup_description,
2523 0);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -07002524 perm.info.flags = sa.getInt(
2525 com.android.internal.R.styleable.AndroidManifestPermissionGroup_permissionGroupFlags, 0);
Dianne Hackbornfd5015b2012-04-30 16:33:56 -07002526 perm.info.priority = sa.getInt(
2527 com.android.internal.R.styleable.AndroidManifestPermissionGroup_priority, 0);
Dianne Hackborn99222d22012-05-06 16:30:15 -07002528 if (perm.info.priority > 0 && (flags&PARSE_IS_SYSTEM) == 0) {
Dianne Hackbornfd5015b2012-04-30 16:33:56 -07002529 perm.info.priority = 0;
2530 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002531
2532 sa.recycle();
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002533
Svet Ganov354cd3c2015-12-17 11:35:04 -08002534 if (!parseAllMetaData(res, parser, "<permission-group>", perm,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 outError)) {
2536 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
2537 return null;
2538 }
2539
2540 owner.permissionGroups.add(perm);
2541
2542 return perm;
2543 }
2544
2545 private Permission parsePermission(Package owner, Resources res,
Svet Ganov354cd3c2015-12-17 11:35:04 -08002546 XmlResourceParser parser, String[] outError)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 throws XmlPullParserException, IOException {
2548 Permission perm = new Permission(owner);
2549
Svet Ganov354cd3c2015-12-17 11:35:04 -08002550 TypedArray sa = res.obtainAttributes(parser,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 com.android.internal.R.styleable.AndroidManifestPermission);
2552
2553 if (!parsePackageItemInfo(owner, perm.info, outError,
2554 "<permission>", sa,
2555 com.android.internal.R.styleable.AndroidManifestPermission_name,
2556 com.android.internal.R.styleable.AndroidManifestPermission_label,
Adam Powell81cd2e92010-04-21 16:35:18 -07002557 com.android.internal.R.styleable.AndroidManifestPermission_icon,
Jose Limaf78e3122014-03-06 12:13:15 -08002558 com.android.internal.R.styleable.AndroidManifestPermission_logo,
2559 com.android.internal.R.styleable.AndroidManifestPermission_banner)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 sa.recycle();
2561 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
2562 return null;
2563 }
2564
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002565 // Note: don't allow this value to be a reference to a resource
2566 // that may change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 perm.info.group = sa.getNonResourceString(
2568 com.android.internal.R.styleable.AndroidManifestPermission_permissionGroup);
2569 if (perm.info.group != null) {
2570 perm.info.group = perm.info.group.intern();
2571 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 perm.info.descriptionRes = sa.getResourceId(
2574 com.android.internal.R.styleable.AndroidManifestPermission_description,
2575 0);
2576
2577 perm.info.protectionLevel = sa.getInt(
2578 com.android.internal.R.styleable.AndroidManifestPermission_protectionLevel,
2579 PermissionInfo.PROTECTION_NORMAL);
2580
Dianne Hackborn2ca2c872012-09-16 16:03:36 -07002581 perm.info.flags = sa.getInt(
2582 com.android.internal.R.styleable.AndroidManifestPermission_permissionFlags, 0);
2583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 sa.recycle();
Dianne Hackborne639da72012-02-21 15:11:13 -08002585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 if (perm.info.protectionLevel == -1) {
2587 outError[0] = "<permission> does not specify protectionLevel";
2588 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
2589 return null;
2590 }
Dianne Hackborne639da72012-02-21 15:11:13 -08002591
2592 perm.info.protectionLevel = PermissionInfo.fixProtectionLevel(perm.info.protectionLevel);
2593
2594 if ((perm.info.protectionLevel&PermissionInfo.PROTECTION_MASK_FLAGS) != 0) {
2595 if ((perm.info.protectionLevel&PermissionInfo.PROTECTION_MASK_BASE) !=
2596 PermissionInfo.PROTECTION_SIGNATURE) {
2597 outError[0] = "<permission> protectionLevel specifies a flag but is "
2598 + "not based on signature type";
2599 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
2600 return null;
2601 }
2602 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002603
Svet Ganov354cd3c2015-12-17 11:35:04 -08002604 if (!parseAllMetaData(res, parser, "<permission>", perm, outError)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
2606 return null;
2607 }
2608
2609 owner.permissions.add(perm);
2610
2611 return perm;
2612 }
2613
2614 private Permission parsePermissionTree(Package owner, Resources res,
Svet Ganov354cd3c2015-12-17 11:35:04 -08002615 XmlResourceParser parser, String[] outError)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 throws XmlPullParserException, IOException {
2617 Permission perm = new Permission(owner);
2618
Svet Ganov354cd3c2015-12-17 11:35:04 -08002619 TypedArray sa = res.obtainAttributes(parser,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 com.android.internal.R.styleable.AndroidManifestPermissionTree);
2621
2622 if (!parsePackageItemInfo(owner, perm.info, outError,
2623 "<permission-tree>", sa,
2624 com.android.internal.R.styleable.AndroidManifestPermissionTree_name,
2625 com.android.internal.R.styleable.AndroidManifestPermissionTree_label,
Adam Powell81cd2e92010-04-21 16:35:18 -07002626 com.android.internal.R.styleable.AndroidManifestPermissionTree_icon,
Jose Limaf78e3122014-03-06 12:13:15 -08002627 com.android.internal.R.styleable.AndroidManifestPermissionTree_logo,
2628 com.android.internal.R.styleable.AndroidManifestPermissionTree_banner)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 sa.recycle();
2630 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
2631 return null;
2632 }
2633
2634 sa.recycle();
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 int index = perm.info.name.indexOf('.');
2637 if (index > 0) {
2638 index = perm.info.name.indexOf('.', index+1);
2639 }
2640 if (index < 0) {
2641 outError[0] = "<permission-tree> name has less than three segments: "
2642 + perm.info.name;
2643 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
2644 return null;
2645 }
2646
2647 perm.info.descriptionRes = 0;
2648 perm.info.protectionLevel = PermissionInfo.PROTECTION_NORMAL;
2649 perm.tree = true;
2650
Svet Ganov354cd3c2015-12-17 11:35:04 -08002651 if (!parseAllMetaData(res, parser, "<permission-tree>", perm,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 outError)) {
2653 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
2654 return null;
2655 }
2656
2657 owner.permissions.add(perm);
2658
2659 return perm;
2660 }
2661
2662 private Instrumentation parseInstrumentation(Package owner, Resources res,
Svet Ganov354cd3c2015-12-17 11:35:04 -08002663 XmlResourceParser parser, String[] outError)
2664 throws XmlPullParserException, IOException {
2665 TypedArray sa = res.obtainAttributes(parser,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 com.android.internal.R.styleable.AndroidManifestInstrumentation);
2667
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002668 if (mParseInstrumentationArgs == null) {
2669 mParseInstrumentationArgs = new ParsePackageItemArgs(owner, outError,
2670 com.android.internal.R.styleable.AndroidManifestInstrumentation_name,
2671 com.android.internal.R.styleable.AndroidManifestInstrumentation_label,
Adam Powell81cd2e92010-04-21 16:35:18 -07002672 com.android.internal.R.styleable.AndroidManifestInstrumentation_icon,
Jose Limaf78e3122014-03-06 12:13:15 -08002673 com.android.internal.R.styleable.AndroidManifestInstrumentation_logo,
2674 com.android.internal.R.styleable.AndroidManifestInstrumentation_banner);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002675 mParseInstrumentationArgs.tag = "<instrumentation>";
2676 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002677
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002678 mParseInstrumentationArgs.sa = sa;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002679
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002680 Instrumentation a = new Instrumentation(mParseInstrumentationArgs,
2681 new InstrumentationInfo());
2682 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 sa.recycle();
2684 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
2685 return null;
2686 }
2687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 String str;
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002689 // Note: don't allow this value to be a reference to a resource
2690 // that may change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 str = sa.getNonResourceString(
2692 com.android.internal.R.styleable.AndroidManifestInstrumentation_targetPackage);
2693 a.info.targetPackage = str != null ? str.intern() : null;
2694
2695 a.info.handleProfiling = sa.getBoolean(
2696 com.android.internal.R.styleable.AndroidManifestInstrumentation_handleProfiling,
2697 false);
2698
2699 a.info.functionalTest = sa.getBoolean(
2700 com.android.internal.R.styleable.AndroidManifestInstrumentation_functionalTest,
2701 false);
2702
2703 sa.recycle();
2704
2705 if (a.info.targetPackage == null) {
2706 outError[0] = "<instrumentation> does not specify targetPackage";
2707 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
2708 return null;
2709 }
2710
Svet Ganov354cd3c2015-12-17 11:35:04 -08002711 if (!parseAllMetaData(res, parser, "<instrumentation>", a,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 outError)) {
2713 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
2714 return null;
2715 }
2716
2717 owner.instrumentation.add(a);
2718
2719 return a;
2720 }
2721
Jeff Sharkeyda96e132014-07-15 14:54:09 -07002722 /**
2723 * Parse the {@code application} XML tree at the current parse location in a
2724 * <em>base APK</em> manifest.
2725 * <p>
2726 * When adding new features, carefully consider if they should also be
2727 * supported by split APKs.
2728 */
2729 private boolean parseBaseApplication(Package owner, Resources res,
Svet Ganov354cd3c2015-12-17 11:35:04 -08002730 XmlResourceParser parser, int flags, String[] outError)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 throws XmlPullParserException, IOException {
2732 final ApplicationInfo ai = owner.applicationInfo;
2733 final String pkgName = owner.applicationInfo.packageName;
2734
Svet Ganov354cd3c2015-12-17 11:35:04 -08002735 TypedArray sa = res.obtainAttributes(parser,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 com.android.internal.R.styleable.AndroidManifestApplication);
2737
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002738 String name = sa.getNonConfigurationString(
2739 com.android.internal.R.styleable.AndroidManifestApplication_name, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 if (name != null) {
2741 ai.className = buildClassName(pkgName, name, outError);
2742 if (ai.className == null) {
2743 sa.recycle();
2744 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
2745 return false;
2746 }
2747 }
2748
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002749 String manageSpaceActivity = sa.getNonConfigurationString(
Dianne Hackborn1d0b1772013-09-06 14:02:54 -07002750 com.android.internal.R.styleable.AndroidManifestApplication_manageSpaceActivity,
2751 Configuration.NATIVE_CONFIG_VERSION);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 if (manageSpaceActivity != null) {
2753 ai.manageSpaceActivityName = buildClassName(pkgName, manageSpaceActivity,
2754 outError);
2755 }
2756
Christopher Tate181fafa2009-05-14 11:12:14 -07002757 boolean allowBackup = sa.getBoolean(
2758 com.android.internal.R.styleable.AndroidManifestApplication_allowBackup, true);
2759 if (allowBackup) {
2760 ai.flags |= ApplicationInfo.FLAG_ALLOW_BACKUP;
Christopher Tate5e1ab332009-09-01 20:32:49 -07002761
Christopher Tate43fbc5f2016-02-17 18:00:48 -08002762 // backupAgent, killAfterRestore, fullBackupContent, backupInForeground,
2763 // and restoreAnyVersion are only relevant if backup is possible for the
2764 // given application.
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002765 String backupAgent = sa.getNonConfigurationString(
Dianne Hackborn1d0b1772013-09-06 14:02:54 -07002766 com.android.internal.R.styleable.AndroidManifestApplication_backupAgent,
2767 Configuration.NATIVE_CONFIG_VERSION);
Christopher Tate181fafa2009-05-14 11:12:14 -07002768 if (backupAgent != null) {
2769 ai.backupAgentName = buildClassName(pkgName, backupAgent, outError);
Kenny Rootd2d29252011-08-08 11:27:57 -07002770 if (DEBUG_BACKUP) {
2771 Slog.v(TAG, "android:backupAgent = " + ai.backupAgentName
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002772 + " from " + pkgName + "+" + backupAgent);
2773 }
Christopher Tate5e1ab332009-09-01 20:32:49 -07002774
2775 if (sa.getBoolean(
2776 com.android.internal.R.styleable.AndroidManifestApplication_killAfterRestore,
2777 true)) {
2778 ai.flags |= ApplicationInfo.FLAG_KILL_AFTER_RESTORE;
2779 }
2780 if (sa.getBoolean(
Christopher Tate3dda5182010-02-24 16:06:18 -08002781 com.android.internal.R.styleable.AndroidManifestApplication_restoreAnyVersion,
2782 false)) {
2783 ai.flags |= ApplicationInfo.FLAG_RESTORE_ANY_VERSION;
2784 }
Christopher Tated1de2562014-06-17 17:12:35 -07002785 if (sa.getBoolean(
2786 com.android.internal.R.styleable.AndroidManifestApplication_fullBackupOnly,
2787 false)) {
2788 ai.flags |= ApplicationInfo.FLAG_FULL_BACKUP_ONLY;
2789 }
Christopher Tate43fbc5f2016-02-17 18:00:48 -08002790 if (sa.getBoolean(
2791 com.android.internal.R.styleable.AndroidManifestApplication_backupInForeground,
2792 false)) {
2793 ai.privateFlags |= ApplicationInfo.PRIVATE_FLAG_BACKUP_IN_FOREGROUND;
2794 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002795 }
Matthew Williams303650c2015-04-17 18:22:51 -07002796
2797 TypedValue v = sa.peekValue(
2798 com.android.internal.R.styleable.AndroidManifestApplication_fullBackupContent);
2799 if (v != null && (ai.fullBackupContent = v.resourceId) == 0) {
2800 if (DEBUG_BACKUP) {
2801 Slog.v(TAG, "fullBackupContent specified as boolean=" +
2802 (v.data == 0 ? "false" : "true"));
2803 }
2804 // "false" => -1, "true" => 0
2805 ai.fullBackupContent = (v.data == 0 ? -1 : 0);
2806 }
2807 if (DEBUG_BACKUP) {
2808 Slog.v(TAG, "fullBackupContent=" + ai.fullBackupContent + " for " + pkgName);
2809 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002810 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 TypedValue v = sa.peekValue(
2813 com.android.internal.R.styleable.AndroidManifestApplication_label);
2814 if (v != null && (ai.labelRes=v.resourceId) == 0) {
2815 ai.nonLocalizedLabel = v.coerceToString();
2816 }
2817
2818 ai.icon = sa.getResourceId(
2819 com.android.internal.R.styleable.AndroidManifestApplication_icon, 0);
Adam Powell81cd2e92010-04-21 16:35:18 -07002820 ai.logo = sa.getResourceId(
2821 com.android.internal.R.styleable.AndroidManifestApplication_logo, 0);
Jose Limaf78e3122014-03-06 12:13:15 -08002822 ai.banner = sa.getResourceId(
2823 com.android.internal.R.styleable.AndroidManifestApplication_banner, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 ai.theme = sa.getResourceId(
Dianne Hackbornb35cd542011-01-04 21:30:53 -08002825 com.android.internal.R.styleable.AndroidManifestApplication_theme, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 ai.descriptionRes = sa.getResourceId(
2827 com.android.internal.R.styleable.AndroidManifestApplication_description, 0);
2828
2829 if ((flags&PARSE_IS_SYSTEM) != 0) {
2830 if (sa.getBoolean(
2831 com.android.internal.R.styleable.AndroidManifestApplication_persistent,
2832 false)) {
2833 ai.flags |= ApplicationInfo.FLAG_PERSISTENT;
2834 }
Amith Yamasani0d8750d2013-05-01 15:25:28 -07002835 }
2836
2837 if (sa.getBoolean(
2838 com.android.internal.R.styleable.AndroidManifestApplication_requiredForAllUsers,
2839 false)) {
2840 owner.mRequiredForAllUsers = true;
Amith Yamasanie993ae12013-04-15 13:42:57 -07002841 }
2842
2843 String restrictedAccountType = sa.getString(com.android.internal.R.styleable
2844 .AndroidManifestApplication_restrictedAccountType);
2845 if (restrictedAccountType != null && restrictedAccountType.length() > 0) {
2846 owner.mRestrictedAccountType = restrictedAccountType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 }
2848
Amith Yamasaniccbe3892013-04-12 17:52:42 -07002849 String requiredAccountType = sa.getString(com.android.internal.R.styleable
2850 .AndroidManifestApplication_requiredAccountType);
2851 if (requiredAccountType != null && requiredAccountType.length() > 0) {
2852 owner.mRequiredAccountType = requiredAccountType;
2853 }
2854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 if (sa.getBoolean(
2856 com.android.internal.R.styleable.AndroidManifestApplication_debuggable,
2857 false)) {
2858 ai.flags |= ApplicationInfo.FLAG_DEBUGGABLE;
2859 }
2860
2861 if (sa.getBoolean(
Ben Chengef3f5dd2010-03-29 15:47:26 -07002862 com.android.internal.R.styleable.AndroidManifestApplication_vmSafeMode,
Ben Cheng23085b72010-02-08 16:06:32 -08002863 false)) {
2864 ai.flags |= ApplicationInfo.FLAG_VM_SAFE_MODE;
2865 }
2866
Jeff Sharkeyda96e132014-07-15 14:54:09 -07002867 owner.baseHardwareAccelerated = sa.getBoolean(
Romain Guy812ccbe2010-06-01 14:07:24 -07002868 com.android.internal.R.styleable.AndroidManifestApplication_hardwareAccelerated,
Dianne Hackborn2d6833b2011-06-24 16:04:19 -07002869 owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.ICE_CREAM_SANDWICH);
Alan Viveretted70b9e72015-05-27 14:29:20 -07002870 if (owner.baseHardwareAccelerated) {
2871 ai.flags |= ApplicationInfo.FLAG_HARDWARE_ACCELERATED;
2872 }
Romain Guy812ccbe2010-06-01 14:07:24 -07002873
2874 if (sa.getBoolean(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 com.android.internal.R.styleable.AndroidManifestApplication_hasCode,
2876 true)) {
2877 ai.flags |= ApplicationInfo.FLAG_HAS_CODE;
2878 }
2879
2880 if (sa.getBoolean(
2881 com.android.internal.R.styleable.AndroidManifestApplication_allowTaskReparenting,
2882 false)) {
2883 ai.flags |= ApplicationInfo.FLAG_ALLOW_TASK_REPARENTING;
2884 }
2885
2886 if (sa.getBoolean(
2887 com.android.internal.R.styleable.AndroidManifestApplication_allowClearUserData,
2888 true)) {
2889 ai.flags |= ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA;
2890 }
2891
Svet Ganov354cd3c2015-12-17 11:35:04 -08002892 // The parent package controls installation, hence specify test only installs.
2893 if (owner.parentPackage == null) {
2894 if (sa.getBoolean(
2895 com.android.internal.R.styleable.AndroidManifestApplication_testOnly,
2896 false)) {
2897 ai.flags |= ApplicationInfo.FLAG_TEST_ONLY;
2898 }
Dianne Hackbornade3eca2009-05-11 18:54:45 -07002899 }
2900
Jason parksa3cdaa52011-01-13 14:15:43 -06002901 if (sa.getBoolean(
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08002902 com.android.internal.R.styleable.AndroidManifestApplication_largeHeap,
Jason parksa3cdaa52011-01-13 14:15:43 -06002903 false)) {
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08002904 ai.flags |= ApplicationInfo.FLAG_LARGE_HEAP;
Jason parksa3cdaa52011-01-13 14:15:43 -06002905 }
2906
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -07002907 if (sa.getBoolean(
Alex Klyubin01a959d2015-03-18 10:05:45 -07002908 com.android.internal.R.styleable.AndroidManifestApplication_usesCleartextTraffic,
2909 true)) {
2910 ai.flags |= ApplicationInfo.FLAG_USES_CLEARTEXT_TRAFFIC;
2911 }
2912
2913 if (sa.getBoolean(
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -07002914 com.android.internal.R.styleable.AndroidManifestApplication_supportsRtl,
2915 false /* default is no RTL support*/)) {
2916 ai.flags |= ApplicationInfo.FLAG_SUPPORTS_RTL;
2917 }
2918
Narayan Kamath589a1bc2014-07-03 14:43:26 +01002919 if (sa.getBoolean(
2920 com.android.internal.R.styleable.AndroidManifestApplication_multiArch,
2921 false)) {
2922 ai.flags |= ApplicationInfo.FLAG_MULTIARCH;
2923 }
2924
Dmitriy Ivanovff193d62014-09-30 15:10:48 -07002925 if (sa.getBoolean(
2926 com.android.internal.R.styleable.AndroidManifestApplication_extractNativeLibs,
2927 true)) {
2928 ai.flags |= ApplicationInfo.FLAG_EXTRACT_NATIVE_LIBS;
2929 }
2930
Todd Kennedyd022ac22016-04-13 10:49:29 -07002931 if (sa.getBoolean(
2932 R.styleable.AndroidManifestApplication_defaultToDeviceProtectedStorage,
2933 false)) {
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002934 ai.privateFlags |= ApplicationInfo.PRIVATE_FLAG_DEFAULT_TO_DEVICE_PROTECTED_STORAGE;
Jeff Sharkeye17ac152015-11-06 22:40:29 -08002935 }
Todd Kennedyd022ac22016-04-13 10:49:29 -07002936 if (sa.getBoolean(
2937 R.styleable.AndroidManifestApplication_directBootAware,
2938 false)) {
Jeff Sharkey8a372a02016-03-16 16:25:45 -06002939 ai.privateFlags |= ApplicationInfo.PRIVATE_FLAG_DIRECT_BOOT_AWARE;
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08002940 }
Jeff Sharkeye17ac152015-11-06 22:40:29 -08002941
Wale Ogunwale6afdf912016-01-30 13:01:33 -08002942 if (sa.getBoolean(R.styleable.AndroidManifestApplication_resizeableActivity,
2943 owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.N)) {
2944 ai.privateFlags |= PRIVATE_FLAG_RESIZEABLE_ACTIVITIES;
2945 }
2946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 String str;
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002948 str = sa.getNonConfigurationString(
2949 com.android.internal.R.styleable.AndroidManifestApplication_permission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 ai.permission = (str != null && str.length() > 0) ? str.intern() : null;
2951
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07002952 if (owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.FROYO) {
2953 str = sa.getNonConfigurationString(
Dianne Hackborn1d0b1772013-09-06 14:02:54 -07002954 com.android.internal.R.styleable.AndroidManifestApplication_taskAffinity,
2955 Configuration.NATIVE_CONFIG_VERSION);
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07002956 } else {
2957 // Some older apps have been seen to use a resource reference
2958 // here that on older builds was ignored (with a warning). We
2959 // need to continue to do this for them so they don't break.
2960 str = sa.getNonResourceString(
2961 com.android.internal.R.styleable.AndroidManifestApplication_taskAffinity);
2962 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 ai.taskAffinity = buildTaskAffinityName(ai.packageName, ai.packageName,
2964 str, outError);
2965
2966 if (outError[0] == null) {
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07002967 CharSequence pname;
2968 if (owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.FROYO) {
2969 pname = sa.getNonConfigurationString(
Dianne Hackborn1d0b1772013-09-06 14:02:54 -07002970 com.android.internal.R.styleable.AndroidManifestApplication_process,
2971 Configuration.NATIVE_CONFIG_VERSION);
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07002972 } else {
2973 // Some older apps have been seen to use a resource reference
2974 // here that on older builds was ignored (with a warning). We
2975 // need to continue to do this for them so they don't break.
2976 pname = sa.getNonResourceString(
2977 com.android.internal.R.styleable.AndroidManifestApplication_process);
2978 }
2979 ai.processName = buildProcessName(ai.packageName, null, pname,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 flags, mSeparateProcesses, outError);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002981
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002982 ai.enabled = sa.getBoolean(
2983 com.android.internal.R.styleable.AndroidManifestApplication_enabled, true);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07002984
Jose Lima12d0b4c2014-03-14 16:55:12 -07002985 if (sa.getBoolean(
2986 com.android.internal.R.styleable.AndroidManifestApplication_isGame, false)) {
2987 ai.flags |= ApplicationInfo.FLAG_IS_GAME;
2988 }
2989
Dianne Hackborn02486b12010-08-26 14:18:37 -07002990 if (false) {
2991 if (sa.getBoolean(
2992 com.android.internal.R.styleable.AndroidManifestApplication_cantSaveState,
2993 false)) {
Alex Klyubinb9f8a522015-02-03 11:12:59 -08002994 ai.privateFlags |= ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE;
Dianne Hackborn02486b12010-08-26 14:18:37 -07002995
2996 // A heavy-weight application can not be in a custom process.
2997 // We can do direct compare because we intern all strings.
2998 if (ai.processName != null && ai.processName != ai.packageName) {
2999 outError[0] = "cantSaveState applications can not use custom processes";
3000 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07003001 }
3002 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003 }
3004
Adam Powell269248d2011-08-02 10:26:54 -07003005 ai.uiOptions = sa.getInt(
3006 com.android.internal.R.styleable.AndroidManifestApplication_uiOptions, 0);
3007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003008 sa.recycle();
3009
3010 if (outError[0] != null) {
3011 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
3012 return false;
3013 }
3014
3015 final int innerDepth = parser.getDepth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 int type;
Kenny Rootd2d29252011-08-08 11:27:57 -07003017 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
3018 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
3019 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003020 continue;
3021 }
3022
3023 String tagName = parser.getName();
3024 if (tagName.equals("activity")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08003025 Activity a = parseActivity(owner, res, parser, flags, outError, false,
Jeff Sharkeyda96e132014-07-15 14:54:09 -07003026 owner.baseHardwareAccelerated);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 if (a == null) {
3028 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
3029 return false;
3030 }
3031
3032 owner.activities.add(a);
3033
3034 } else if (tagName.equals("receiver")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08003035 Activity a = parseActivity(owner, res, parser, flags, outError, true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003036 if (a == null) {
3037 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
3038 return false;
3039 }
3040
3041 owner.receivers.add(a);
3042
3043 } else if (tagName.equals("service")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08003044 Service s = parseService(owner, res, parser, flags, outError);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003045 if (s == null) {
3046 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
3047 return false;
3048 }
3049
3050 owner.services.add(s);
3051
3052 } else if (tagName.equals("provider")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08003053 Provider p = parseProvider(owner, res, parser, flags, outError);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003054 if (p == null) {
3055 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
3056 return false;
3057 }
3058
3059 owner.providers.add(p);
3060
3061 } else if (tagName.equals("activity-alias")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08003062 Activity a = parseActivityAlias(owner, res, parser, flags, outError);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 if (a == null) {
3064 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
3065 return false;
3066 }
3067
3068 owner.activities.add(a);
3069
3070 } else if (parser.getName().equals("meta-data")) {
3071 // note: application meta-data is stored off to the side, so it can
3072 // remain null in the primary copy (we like to avoid extra copies because
3073 // it can be large)
Svet Ganov354cd3c2015-12-17 11:35:04 -08003074 if ((owner.mAppMetaData = parseMetaData(res, parser, owner.mAppMetaData,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003075 outError)) == null) {
3076 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
3077 return false;
3078 }
3079
Dianne Hackbornc895be72013-03-11 17:48:43 -07003080 } else if (tagName.equals("library")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08003081 sa = res.obtainAttributes(parser,
Dianne Hackbornc895be72013-03-11 17:48:43 -07003082 com.android.internal.R.styleable.AndroidManifestLibrary);
3083
3084 // Note: don't allow this value to be a reference to a resource
3085 // that may change.
3086 String lname = sa.getNonResourceString(
3087 com.android.internal.R.styleable.AndroidManifestLibrary_name);
3088
3089 sa.recycle();
3090
3091 if (lname != null) {
Jeff Sharkeyda96e132014-07-15 14:54:09 -07003092 lname = lname.intern();
3093 if (!ArrayUtils.contains(owner.libraryNames, lname)) {
3094 owner.libraryNames = ArrayUtils.add(owner.libraryNames, lname);
Dianne Hackbornc895be72013-03-11 17:48:43 -07003095 }
3096 }
3097
3098 XmlUtils.skipCurrentTag(parser);
3099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 } else if (tagName.equals("uses-library")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08003101 sa = res.obtainAttributes(parser,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 com.android.internal.R.styleable.AndroidManifestUsesLibrary);
3103
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003104 // Note: don't allow this value to be a reference to a resource
3105 // that may change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106 String lname = sa.getNonResourceString(
3107 com.android.internal.R.styleable.AndroidManifestUsesLibrary_name);
Dianne Hackborn49237342009-08-27 20:08:01 -07003108 boolean req = sa.getBoolean(
3109 com.android.internal.R.styleable.AndroidManifestUsesLibrary_required,
3110 true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111
3112 sa.recycle();
3113
Dianne Hackborn49237342009-08-27 20:08:01 -07003114 if (lname != null) {
Jeff Sharkeyda96e132014-07-15 14:54:09 -07003115 lname = lname.intern();
Dianne Hackborn49237342009-08-27 20:08:01 -07003116 if (req) {
Jeff Sharkeyda96e132014-07-15 14:54:09 -07003117 owner.usesLibraries = ArrayUtils.add(owner.usesLibraries, lname);
Dianne Hackborn49237342009-08-27 20:08:01 -07003118 } else {
Jeff Sharkeyda96e132014-07-15 14:54:09 -07003119 owner.usesOptionalLibraries = ArrayUtils.add(
3120 owner.usesOptionalLibraries, lname);
3121 }
3122 }
3123
3124 XmlUtils.skipCurrentTag(parser);
3125
3126 } else if (tagName.equals("uses-package")) {
3127 // Dependencies for app installers; we don't currently try to
3128 // enforce this.
3129 XmlUtils.skipCurrentTag(parser);
3130
3131 } else {
3132 if (!RIGID_PARSER) {
3133 Slog.w(TAG, "Unknown element under <application>: " + tagName
3134 + " at " + mArchiveSourcePath + " "
3135 + parser.getPositionDescription());
3136 XmlUtils.skipCurrentTag(parser);
3137 continue;
3138 } else {
3139 outError[0] = "Bad element under <application>: " + tagName;
3140 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
3141 return false;
3142 }
3143 }
3144 }
3145
Narayan Kamathb12dcfb2015-03-30 11:31:04 +01003146 modifySharedLibrariesForBackwardCompatibility(owner);
Narayan Kamathe2d2ed42015-01-13 15:51:57 +00003147
Fabrice Di Megliod3d8a322015-04-01 15:58:47 -07003148 if (hasDomainURLs(owner)) {
3149 owner.applicationInfo.privateFlags |= ApplicationInfo.PRIVATE_FLAG_HAS_DOMAIN_URLS;
3150 } else {
3151 owner.applicationInfo.privateFlags &= ~ApplicationInfo.PRIVATE_FLAG_HAS_DOMAIN_URLS;
3152 }
3153
Jeff Sharkeyda96e132014-07-15 14:54:09 -07003154 return true;
3155 }
3156
Narayan Kamathb12dcfb2015-03-30 11:31:04 +01003157 private static void modifySharedLibrariesForBackwardCompatibility(Package owner) {
3158 // "org.apache.http.legacy" is now a part of the boot classpath so it doesn't need
3159 // to be an explicit dependency.
3160 //
3161 // A future change will remove this library from the boot classpath, at which point
3162 // all apps that target SDK 21 and earlier will have it automatically added to their
3163 // dependency lists.
3164 owner.usesLibraries = ArrayUtils.remove(owner.usesLibraries, "org.apache.http.legacy");
3165 owner.usesOptionalLibraries = ArrayUtils.remove(owner.usesOptionalLibraries,
3166 "org.apache.http.legacy");
Narayan Kamathe2d2ed42015-01-13 15:51:57 +00003167 }
3168
Jeff Sharkeyda96e132014-07-15 14:54:09 -07003169 /**
Fabrice Di Meglioe48c1372015-04-09 15:55:42 -07003170 * Check if one of the IntentFilter as both actions DEFAULT / VIEW and a HTTP/HTTPS data URI
Fabrice Di Megliod3d8a322015-04-01 15:58:47 -07003171 */
3172 private static boolean hasDomainURLs(Package pkg) {
3173 if (pkg == null || pkg.activities == null) return false;
3174 final ArrayList<Activity> activities = pkg.activities;
3175 final int countActivities = activities.size();
3176 for (int n=0; n<countActivities; n++) {
3177 Activity activity = activities.get(n);
3178 ArrayList<ActivityIntentInfo> filters = activity.intents;
3179 if (filters == null) continue;
3180 final int countFilters = filters.size();
3181 for (int m=0; m<countFilters; m++) {
3182 ActivityIntentInfo aii = filters.get(m);
3183 if (!aii.hasAction(Intent.ACTION_VIEW)) continue;
Fabrice Di Meglioe48c1372015-04-09 15:55:42 -07003184 if (!aii.hasAction(Intent.ACTION_DEFAULT)) continue;
Fabrice Di Megliod3d8a322015-04-01 15:58:47 -07003185 if (aii.hasDataScheme(IntentFilter.SCHEME_HTTP) ||
3186 aii.hasDataScheme(IntentFilter.SCHEME_HTTPS)) {
3187 return true;
3188 }
3189 }
3190 }
3191 return false;
3192 }
3193
3194 /**
Jeff Sharkeyda96e132014-07-15 14:54:09 -07003195 * Parse the {@code application} XML tree at the current parse location in a
3196 * <em>split APK</em> manifest.
3197 * <p>
3198 * Note that split APKs have many more restrictions on what they're capable
3199 * of doing, so many valid features of a base APK have been carefully
3200 * omitted here.
3201 */
Svet Ganov354cd3c2015-12-17 11:35:04 -08003202 private boolean parseSplitApplication(Package owner, Resources res, XmlResourceParser parser,
3203 int flags, int splitIndex, String[] outError)
Jeff Sharkeyda96e132014-07-15 14:54:09 -07003204 throws XmlPullParserException, IOException {
Svet Ganov354cd3c2015-12-17 11:35:04 -08003205 TypedArray sa = res.obtainAttributes(parser,
Jeff Sharkeyda96e132014-07-15 14:54:09 -07003206 com.android.internal.R.styleable.AndroidManifestApplication);
3207
3208 if (sa.getBoolean(
3209 com.android.internal.R.styleable.AndroidManifestApplication_hasCode, true)) {
3210 owner.splitFlags[splitIndex] |= ApplicationInfo.FLAG_HAS_CODE;
3211 }
3212
3213 final int innerDepth = parser.getDepth();
3214 int type;
3215 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
3216 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
3217 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
3218 continue;
3219 }
3220
3221 String tagName = parser.getName();
3222 if (tagName.equals("activity")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08003223 Activity a = parseActivity(owner, res, parser, flags, outError, false,
Jeff Sharkeyda96e132014-07-15 14:54:09 -07003224 owner.baseHardwareAccelerated);
3225 if (a == null) {
3226 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
3227 return false;
3228 }
3229
3230 owner.activities.add(a);
3231
3232 } else if (tagName.equals("receiver")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08003233 Activity a = parseActivity(owner, res, parser, flags, outError, true, false);
Jeff Sharkeyda96e132014-07-15 14:54:09 -07003234 if (a == null) {
3235 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
3236 return false;
3237 }
3238
3239 owner.receivers.add(a);
3240
3241 } else if (tagName.equals("service")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08003242 Service s = parseService(owner, res, parser, flags, outError);
Jeff Sharkeyda96e132014-07-15 14:54:09 -07003243 if (s == null) {
3244 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
3245 return false;
3246 }
3247
3248 owner.services.add(s);
3249
3250 } else if (tagName.equals("provider")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08003251 Provider p = parseProvider(owner, res, parser, flags, outError);
Jeff Sharkeyda96e132014-07-15 14:54:09 -07003252 if (p == null) {
3253 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
3254 return false;
3255 }
3256
3257 owner.providers.add(p);
3258
3259 } else if (tagName.equals("activity-alias")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08003260 Activity a = parseActivityAlias(owner, res, parser, flags, outError);
Jeff Sharkeyda96e132014-07-15 14:54:09 -07003261 if (a == null) {
3262 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
3263 return false;
3264 }
3265
3266 owner.activities.add(a);
3267
3268 } else if (parser.getName().equals("meta-data")) {
3269 // note: application meta-data is stored off to the side, so it can
3270 // remain null in the primary copy (we like to avoid extra copies because
3271 // it can be large)
Svet Ganov354cd3c2015-12-17 11:35:04 -08003272 if ((owner.mAppMetaData = parseMetaData(res, parser, owner.mAppMetaData,
Jeff Sharkeyda96e132014-07-15 14:54:09 -07003273 outError)) == null) {
3274 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
3275 return false;
3276 }
3277
3278 } else if (tagName.equals("uses-library")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08003279 sa = res.obtainAttributes(parser,
Jeff Sharkeyda96e132014-07-15 14:54:09 -07003280 com.android.internal.R.styleable.AndroidManifestUsesLibrary);
3281
3282 // Note: don't allow this value to be a reference to a resource
3283 // that may change.
3284 String lname = sa.getNonResourceString(
3285 com.android.internal.R.styleable.AndroidManifestUsesLibrary_name);
3286 boolean req = sa.getBoolean(
3287 com.android.internal.R.styleable.AndroidManifestUsesLibrary_required,
3288 true);
3289
3290 sa.recycle();
3291
3292 if (lname != null) {
3293 lname = lname.intern();
3294 if (req) {
3295 // Upgrade to treat as stronger constraint
3296 owner.usesLibraries = ArrayUtils.add(owner.usesLibraries, lname);
3297 owner.usesOptionalLibraries = ArrayUtils.remove(
3298 owner.usesOptionalLibraries, lname);
3299 } else {
3300 // Ignore if someone already defined as required
3301 if (!ArrayUtils.contains(owner.usesLibraries, lname)) {
3302 owner.usesOptionalLibraries = ArrayUtils.add(
3303 owner.usesOptionalLibraries, lname);
Dianne Hackborn49237342009-08-27 20:08:01 -07003304 }
3305 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 }
3307
3308 XmlUtils.skipCurrentTag(parser);
3309
Dianne Hackborncef65ee2010-09-30 18:27:22 -07003310 } else if (tagName.equals("uses-package")) {
3311 // Dependencies for app installers; we don't currently try to
3312 // enforce this.
3313 XmlUtils.skipCurrentTag(parser);
3314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003315 } else {
3316 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07003317 Slog.w(TAG, "Unknown element under <application>: " + tagName
Dianne Hackborna33e3f72009-09-29 17:28:24 -07003318 + " at " + mArchiveSourcePath + " "
3319 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003320 XmlUtils.skipCurrentTag(parser);
3321 continue;
3322 } else {
3323 outError[0] = "Bad element under <application>: " + tagName;
3324 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
3325 return false;
3326 }
3327 }
3328 }
3329
3330 return true;
3331 }
3332
3333 private boolean parsePackageItemInfo(Package owner, PackageItemInfo outInfo,
3334 String[] outError, String tag, TypedArray sa,
Jose Limaf78e3122014-03-06 12:13:15 -08003335 int nameRes, int labelRes, int iconRes, int logoRes, int bannerRes) {
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003336 String name = sa.getNonConfigurationString(nameRes, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003337 if (name == null) {
3338 outError[0] = tag + " does not specify android:name";
3339 return false;
3340 }
3341
3342 outInfo.name
3343 = buildClassName(owner.applicationInfo.packageName, name, outError);
3344 if (outInfo.name == null) {
3345 return false;
3346 }
3347
3348 int iconVal = sa.getResourceId(iconRes, 0);
3349 if (iconVal != 0) {
3350 outInfo.icon = iconVal;
3351 outInfo.nonLocalizedLabel = null;
3352 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003353
Adam Powell81cd2e92010-04-21 16:35:18 -07003354 int logoVal = sa.getResourceId(logoRes, 0);
3355 if (logoVal != 0) {
3356 outInfo.logo = logoVal;
3357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003358
Jose Limaf78e3122014-03-06 12:13:15 -08003359 int bannerVal = sa.getResourceId(bannerRes, 0);
3360 if (bannerVal != 0) {
3361 outInfo.banner = bannerVal;
3362 }
3363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 TypedValue v = sa.peekValue(labelRes);
3365 if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
3366 outInfo.nonLocalizedLabel = v.coerceToString();
3367 }
3368
3369 outInfo.packageName = owner.packageName;
3370
3371 return true;
3372 }
3373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003374 private Activity parseActivity(Package owner, Resources res,
Svet Ganov354cd3c2015-12-17 11:35:04 -08003375 XmlResourceParser parser, int flags, String[] outError,
Romain Guy529b60a2010-08-03 18:05:47 -07003376 boolean receiver, boolean hardwareAccelerated)
3377 throws XmlPullParserException, IOException {
Svet Ganov354cd3c2015-12-17 11:35:04 -08003378 TypedArray sa = res.obtainAttributes(parser, R.styleable.AndroidManifestActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003380 if (mParseActivityArgs == null) {
3381 mParseActivityArgs = new ParseComponentArgs(owner, outError,
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003382 R.styleable.AndroidManifestActivity_name,
3383 R.styleable.AndroidManifestActivity_label,
3384 R.styleable.AndroidManifestActivity_icon,
3385 R.styleable.AndroidManifestActivity_logo,
3386 R.styleable.AndroidManifestActivity_banner,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003387 mSeparateProcesses,
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003388 R.styleable.AndroidManifestActivity_process,
3389 R.styleable.AndroidManifestActivity_description,
3390 R.styleable.AndroidManifestActivity_enabled);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003391 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003392
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003393 mParseActivityArgs.tag = receiver ? "<receiver>" : "<activity>";
3394 mParseActivityArgs.sa = sa;
3395 mParseActivityArgs.flags = flags;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003396
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003397 Activity a = new Activity(mParseActivityArgs, new ActivityInfo());
3398 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003399 sa.recycle();
3400 return null;
3401 }
3402
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003403 boolean setExported = sa.hasValue(R.styleable.AndroidManifestActivity_exported);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 if (setExported) {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003405 a.info.exported = sa.getBoolean(R.styleable.AndroidManifestActivity_exported, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003406 }
3407
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003408 a.info.theme = sa.getResourceId(R.styleable.AndroidManifestActivity_theme, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003409
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003410 a.info.uiOptions = sa.getInt(R.styleable.AndroidManifestActivity_uiOptions,
Adam Powell269248d2011-08-02 10:26:54 -07003411 a.info.applicationInfo.uiOptions);
3412
Adam Powelldd8fab22012-03-22 17:47:27 -07003413 String parentName = sa.getNonConfigurationString(
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003414 R.styleable.AndroidManifestActivity_parentActivityName,
Dianne Hackborn1d0b1772013-09-06 14:02:54 -07003415 Configuration.NATIVE_CONFIG_VERSION);
Adam Powelldd8fab22012-03-22 17:47:27 -07003416 if (parentName != null) {
3417 String parentClassName = buildClassName(a.info.packageName, parentName, outError);
3418 if (outError[0] == null) {
3419 a.info.parentActivityName = parentClassName;
3420 } else {
3421 Log.e(TAG, "Activity " + a.info.name + " specified invalid parentActivityName " +
3422 parentName);
3423 outError[0] = null;
3424 }
3425 }
3426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 String str;
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003428 str = sa.getNonConfigurationString(R.styleable.AndroidManifestActivity_permission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003429 if (str == null) {
3430 a.info.permission = owner.applicationInfo.permission;
3431 } else {
3432 a.info.permission = str.length() > 0 ? str.toString().intern() : null;
3433 }
3434
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003435 str = sa.getNonConfigurationString(
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003436 R.styleable.AndroidManifestActivity_taskAffinity,
Dianne Hackborn1d0b1772013-09-06 14:02:54 -07003437 Configuration.NATIVE_CONFIG_VERSION);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 a.info.taskAffinity = buildTaskAffinityName(owner.applicationInfo.packageName,
3439 owner.applicationInfo.taskAffinity, str, outError);
3440
3441 a.info.flags = 0;
3442 if (sa.getBoolean(
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003443 R.styleable.AndroidManifestActivity_multiprocess, false)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003444 a.info.flags |= ActivityInfo.FLAG_MULTIPROCESS;
3445 }
3446
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003447 if (sa.getBoolean(R.styleable.AndroidManifestActivity_finishOnTaskLaunch, false)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003448 a.info.flags |= ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH;
3449 }
3450
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003451 if (sa.getBoolean(R.styleable.AndroidManifestActivity_clearTaskOnLaunch, false)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452 a.info.flags |= ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH;
3453 }
3454
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003455 if (sa.getBoolean(R.styleable.AndroidManifestActivity_noHistory, false)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456 a.info.flags |= ActivityInfo.FLAG_NO_HISTORY;
3457 }
3458
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003459 if (sa.getBoolean(R.styleable.AndroidManifestActivity_alwaysRetainTaskState, false)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003460 a.info.flags |= ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE;
3461 }
3462
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003463 if (sa.getBoolean(R.styleable.AndroidManifestActivity_stateNotNeeded, false)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003464 a.info.flags |= ActivityInfo.FLAG_STATE_NOT_NEEDED;
3465 }
3466
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003467 if (sa.getBoolean(R.styleable.AndroidManifestActivity_excludeFromRecents, false)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 a.info.flags |= ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS;
3469 }
3470
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003471 if (sa.getBoolean(R.styleable.AndroidManifestActivity_allowTaskReparenting,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003472 (owner.applicationInfo.flags&ApplicationInfo.FLAG_ALLOW_TASK_REPARENTING) != 0)) {
3473 a.info.flags |= ActivityInfo.FLAG_ALLOW_TASK_REPARENTING;
3474 }
3475
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003476 if (sa.getBoolean(R.styleable.AndroidManifestActivity_finishOnCloseSystemDialogs, false)) {
Dianne Hackbornffa42482009-09-23 22:20:11 -07003477 a.info.flags |= ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS;
3478 }
3479
Wale Ogunwale6dfdfd62015-04-15 12:01:38 -07003480 if (sa.getBoolean(R.styleable.AndroidManifestActivity_showOnLockScreen, false)
3481 || sa.getBoolean(R.styleable.AndroidManifestActivity_showForAllUsers, false)) {
3482 a.info.flags |= ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
Craig Mautner5962b122012-10-05 14:45:52 -07003483 }
3484
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003485 if (sa.getBoolean(R.styleable.AndroidManifestActivity_immersive, false)) {
Daniel Sandler613dde42010-06-21 13:46:39 -04003486 a.info.flags |= ActivityInfo.FLAG_IMMERSIVE;
3487 }
Craig Mautner5962b122012-10-05 14:45:52 -07003488
Fyodor Kupolov4f5de342015-08-25 10:08:45 -07003489 if (sa.getBoolean(R.styleable.AndroidManifestActivity_systemUserOnly, false)) {
3490 a.info.flags |= ActivityInfo.FLAG_SYSTEM_USER_ONLY;
Fyodor Kupolov703d1c42015-01-27 11:59:00 -08003491 }
3492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 if (!receiver) {
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003494 if (sa.getBoolean(R.styleable.AndroidManifestActivity_hardwareAccelerated,
Romain Guy529b60a2010-08-03 18:05:47 -07003495 hardwareAccelerated)) {
3496 a.info.flags |= ActivityInfo.FLAG_HARDWARE_ACCELERATED;
3497 }
3498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 a.info.launchMode = sa.getInt(
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003500 R.styleable.AndroidManifestActivity_launchMode, ActivityInfo.LAUNCH_MULTIPLE);
Craig Mautner2dac0562014-05-06 09:06:44 -07003501 a.info.documentLaunchMode = sa.getInt(
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003502 R.styleable.AndroidManifestActivity_documentLaunchMode,
Craig Mautner2dac0562014-05-06 09:06:44 -07003503 ActivityInfo.DOCUMENT_LAUNCH_NONE);
Craig Mautnerffcfcaa2014-06-05 09:54:38 -07003504 a.info.maxRecents = sa.getInt(
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003505 R.styleable.AndroidManifestActivity_maxRecents,
Dianne Hackborn852975d2014-08-22 17:42:43 -07003506 ActivityManager.getDefaultAppRecentsLimitStatic());
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003507 a.info.configChanges = sa.getInt(R.styleable.AndroidManifestActivity_configChanges, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003508 a.info.softInputMode = sa.getInt(
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003509 R.styleable.AndroidManifestActivity_windowSoftInputMode, 0);
Craig Mautner2dac0562014-05-06 09:06:44 -07003510
Craig Mautner43e52ed2014-06-16 17:18:52 -07003511 a.info.persistableMode = sa.getInteger(
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003512 R.styleable.AndroidManifestActivity_persistableMode,
Craig Mautner43e52ed2014-06-16 17:18:52 -07003513 ActivityInfo.PERSIST_ROOT_ONLY);
Craig Mautner2dac0562014-05-06 09:06:44 -07003514
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003515 if (sa.getBoolean(R.styleable.AndroidManifestActivity_allowEmbedded, false)) {
Craig Mautner2dac0562014-05-06 09:06:44 -07003516 a.info.flags |= ActivityInfo.FLAG_ALLOW_EMBEDDED;
3517 }
3518
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003519 if (sa.getBoolean(R.styleable.AndroidManifestActivity_autoRemoveFromRecents, false)) {
Craig Mautner2dac0562014-05-06 09:06:44 -07003520 a.info.flags |= ActivityInfo.FLAG_AUTO_REMOVE_FROM_RECENTS;
3521 }
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07003522
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003523 if (sa.getBoolean(R.styleable.AndroidManifestActivity_relinquishTaskIdentity, false)) {
Craig Mautner9d4e9bc2014-06-18 18:34:56 -07003524 a.info.flags |= ActivityInfo.FLAG_RELINQUISH_TASK_IDENTITY;
3525 }
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003526
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003527 if (sa.getBoolean(R.styleable.AndroidManifestActivity_resumeWhilePausing, false)) {
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003528 a.info.flags |= ActivityInfo.FLAG_RESUME_WHILE_PAUSING;
3529 }
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003530
Wale Ogunwaled26176f2016-01-25 20:04:04 -08003531 a.info.screenOrientation = sa.getInt(
3532 R.styleable.AndroidManifestActivity_screenOrientation,
3533 SCREEN_ORIENTATION_UNSPECIFIED);
Wale Ogunwale6a591362015-10-05 20:18:27 -07003534
Wale Ogunwaled26176f2016-01-25 20:04:04 -08003535 a.info.resizeMode = RESIZE_MODE_UNRESIZEABLE;
Wale Ogunwale163828d2016-02-01 11:48:19 -08003536 final boolean appDefault = (owner.applicationInfo.privateFlags
3537 & PRIVATE_FLAG_RESIZEABLE_ACTIVITIES) != 0;
3538 final boolean resizeable = sa.getBoolean(
3539 R.styleable.AndroidManifestActivity_resizeableActivity, appDefault);
3540
3541 if (resizeable) {
3542 if (sa.getBoolean(R.styleable.AndroidManifestActivity_supportsPictureInPicture,
3543 false)) {
3544 a.info.resizeMode = RESIZE_MODE_RESIZEABLE_AND_PIPABLE;
3545 } else {
3546 a.info.resizeMode = RESIZE_MODE_RESIZEABLE;
Wale Ogunwale6cae7652015-12-26 07:36:26 -08003547 }
Wale Ogunwale163828d2016-02-01 11:48:19 -08003548 } else if (owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.N) {
3549 a.info.resizeMode = RESIZE_MODE_UNRESIZEABLE;
Wale Ogunwale508ff552016-02-15 19:31:50 -08003550 } else if (!a.info.isFixedOrientation() && (a.info.flags & FLAG_IMMERSIVE) == 0) {
Wale Ogunwaled829d362016-02-10 19:24:49 -08003551 a.info.resizeMode = RESIZE_MODE_FORCE_RESIZEABLE;
Wale Ogunwale6cae7652015-12-26 07:36:26 -08003552 }
3553
3554 if (sa.getBoolean(R.styleable.AndroidManifestActivity_alwaysFocusable, false)) {
Wale Ogunwaled26176f2016-01-25 20:04:04 -08003555 a.info.flags |= FLAG_ALWAYS_FOCUSABLE;
Wale Ogunwale6cae7652015-12-26 07:36:26 -08003556 }
Wale Ogunwaleb60692e2015-10-24 12:35:56 -07003557
Craig Mautner15df08a2015-04-01 12:17:18 -07003558 a.info.lockTaskLaunchMode =
3559 sa.getInt(R.styleable.AndroidManifestActivity_lockTaskMode, 0);
Jeff Sharkeye17ac152015-11-06 22:40:29 -08003560
Jeff Sharkey8a372a02016-03-16 16:25:45 -06003561 a.info.encryptionAware = a.info.directBootAware = sa.getBoolean(
3562 R.styleable.AndroidManifestActivity_directBootAware,
Todd Kennedyd022ac22016-04-13 10:49:29 -07003563 false);
Ruben Brunkf56c9f42016-04-22 18:20:55 -07003564
3565 a.info.requestedVrComponent =
3566 sa.getString(R.styleable.AndroidManifestActivity_enableVrMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003567 } else {
3568 a.info.launchMode = ActivityInfo.LAUNCH_MULTIPLE;
3569 a.info.configChanges = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570
Wale Ogunwale9d3de4c2015-02-01 16:49:44 -08003571 if (sa.getBoolean(R.styleable.AndroidManifestActivity_singleUser, false)) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07003572 a.info.flags |= ActivityInfo.FLAG_SINGLE_USER;
Amith Yamasani4b9d79c2014-05-21 19:14:21 -07003573 if (a.info.exported && (flags & PARSE_IS_PRIVILEGED) == 0) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07003574 Slog.w(TAG, "Activity exported request ignored due to singleUser: "
3575 + a.className + " at " + mArchiveSourcePath + " "
3576 + parser.getPositionDescription());
3577 a.info.exported = false;
Amith Yamasani4b9d79c2014-05-21 19:14:21 -07003578 setExported = true;
Dianne Hackborn7d19e022012-08-07 19:12:33 -07003579 }
Dianne Hackborn7d19e022012-08-07 19:12:33 -07003580 }
Jeff Sharkeye17ac152015-11-06 22:40:29 -08003581
Jeff Sharkey8a372a02016-03-16 16:25:45 -06003582 a.info.encryptionAware = a.info.directBootAware = sa.getBoolean(
3583 R.styleable.AndroidManifestActivity_directBootAware,
Todd Kennedyd022ac22016-04-13 10:49:29 -07003584 false);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07003585 }
3586
Jeff Sharkey8a372a02016-03-16 16:25:45 -06003587 if (a.info.directBootAware) {
Jeff Sharkey8924e872015-11-30 12:52:10 -07003588 owner.applicationInfo.privateFlags |=
Jeff Sharkey8a372a02016-03-16 16:25:45 -06003589 ApplicationInfo.PRIVATE_FLAG_PARTIALLY_DIRECT_BOOT_AWARE;
Jeff Sharkey8924e872015-11-30 12:52:10 -07003590 }
3591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003592 sa.recycle();
3593
Alex Klyubinb9f8a522015-02-03 11:12:59 -08003594 if (receiver && (owner.applicationInfo.privateFlags
3595 &ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07003596 // A heavy-weight application can not have receives in its main process
3597 // We can do direct compare because we intern all strings.
3598 if (a.info.processName == owner.packageName) {
3599 outError[0] = "Heavy-weight applications can not have receivers in main process";
3600 }
3601 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003603 if (outError[0] != null) {
3604 return null;
3605 }
3606
3607 int outerDepth = parser.getDepth();
3608 int type;
3609 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
3610 && (type != XmlPullParser.END_TAG
3611 || parser.getDepth() > outerDepth)) {
3612 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
3613 continue;
3614 }
3615
3616 if (parser.getName().equals("intent-filter")) {
3617 ActivityIntentInfo intent = new ActivityIntentInfo(a);
Svet Ganov354cd3c2015-12-17 11:35:04 -08003618 if (!parseIntent(res, parser, true, true, intent, outError)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003619 return null;
3620 }
3621 if (intent.countActions() == 0) {
Kenny Rootd2d29252011-08-08 11:27:57 -07003622 Slog.w(TAG, "No actions in intent filter at "
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -07003623 + mArchiveSourcePath + " "
3624 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003625 } else {
3626 a.intents.add(intent);
3627 }
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003628 } else if (!receiver && parser.getName().equals("preferred")) {
3629 ActivityIntentInfo intent = new ActivityIntentInfo(a);
Svet Ganov354cd3c2015-12-17 11:35:04 -08003630 if (!parseIntent(res, parser, false, false, intent, outError)) {
Dianne Hackbornb09491f2013-07-22 15:30:11 -07003631 return null;
3632 }
3633 if (intent.countActions() == 0) {
3634 Slog.w(TAG, "No actions in preferred at "
3635 + mArchiveSourcePath + " "
3636 + parser.getPositionDescription());
3637 } else {
3638 if (owner.preferredActivityFilters == null) {
3639 owner.preferredActivityFilters = new ArrayList<ActivityIntentInfo>();
3640 }
3641 owner.preferredActivityFilters.add(intent);
3642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003643 } else if (parser.getName().equals("meta-data")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08003644 if ((a.metaData = parseMetaData(res, parser, a.metaData,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003645 outError)) == null) {
3646 return null;
3647 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003648 } else if (!receiver && parser.getName().equals("layout")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08003649 parseLayout(res, parser, a);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003650 } else {
3651 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07003652 Slog.w(TAG, "Problem in package " + mArchiveSourcePath + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003653 if (receiver) {
Kenny Rootd2d29252011-08-08 11:27:57 -07003654 Slog.w(TAG, "Unknown element under <receiver>: " + parser.getName()
Dianne Hackborna33e3f72009-09-29 17:28:24 -07003655 + " at " + mArchiveSourcePath + " "
3656 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003657 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -07003658 Slog.w(TAG, "Unknown element under <activity>: " + parser.getName()
Dianne Hackborna33e3f72009-09-29 17:28:24 -07003659 + " at " + mArchiveSourcePath + " "
3660 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003661 }
3662 XmlUtils.skipCurrentTag(parser);
3663 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003664 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -07003665 if (receiver) {
3666 outError[0] = "Bad element under <receiver>: " + parser.getName();
3667 } else {
3668 outError[0] = "Bad element under <activity>: " + parser.getName();
3669 }
3670 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003672 }
3673 }
3674
3675 if (!setExported) {
3676 a.info.exported = a.intents.size() > 0;
3677 }
3678
3679 return a;
3680 }
3681
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003682 private void parseLayout(Resources res, AttributeSet attrs, Activity a) {
Filip Gruszczynski848fe6b2015-08-19 13:32:41 -07003683 TypedArray sw = res.obtainAttributes(attrs,
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003684 com.android.internal.R.styleable.AndroidManifestLayout);
Filip Gruszczynski848fe6b2015-08-19 13:32:41 -07003685 int width = -1;
3686 float widthFraction = -1f;
3687 int height = -1;
3688 float heightFraction = -1f;
3689 final int widthType = sw.getType(
Wale Ogunwaleb12e1092016-01-30 12:23:09 -08003690 com.android.internal.R.styleable.AndroidManifestLayout_defaultWidth);
Filip Gruszczynski848fe6b2015-08-19 13:32:41 -07003691 if (widthType == TypedValue.TYPE_FRACTION) {
3692 widthFraction = sw.getFraction(
Wale Ogunwaleb12e1092016-01-30 12:23:09 -08003693 com.android.internal.R.styleable.AndroidManifestLayout_defaultWidth,
Filip Gruszczynski848fe6b2015-08-19 13:32:41 -07003694 1, 1, -1);
3695 } else if (widthType == TypedValue.TYPE_DIMENSION) {
3696 width = sw.getDimensionPixelSize(
Wale Ogunwaleb12e1092016-01-30 12:23:09 -08003697 com.android.internal.R.styleable.AndroidManifestLayout_defaultWidth,
Filip Gruszczynski848fe6b2015-08-19 13:32:41 -07003698 -1);
3699 }
3700 final int heightType = sw.getType(
Wale Ogunwaleb12e1092016-01-30 12:23:09 -08003701 com.android.internal.R.styleable.AndroidManifestLayout_defaultHeight);
Filip Gruszczynski848fe6b2015-08-19 13:32:41 -07003702 if (heightType == TypedValue.TYPE_FRACTION) {
3703 heightFraction = sw.getFraction(
Wale Ogunwaleb12e1092016-01-30 12:23:09 -08003704 com.android.internal.R.styleable.AndroidManifestLayout_defaultHeight,
Filip Gruszczynski848fe6b2015-08-19 13:32:41 -07003705 1, 1, -1);
3706 } else if (heightType == TypedValue.TYPE_DIMENSION) {
3707 height = sw.getDimensionPixelSize(
Wale Ogunwaleb12e1092016-01-30 12:23:09 -08003708 com.android.internal.R.styleable.AndroidManifestLayout_defaultHeight,
Filip Gruszczynski848fe6b2015-08-19 13:32:41 -07003709 -1);
3710 }
3711 int gravity = sw.getInt(
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003712 com.android.internal.R.styleable.AndroidManifestLayout_gravity,
Filip Gruszczynski848fe6b2015-08-19 13:32:41 -07003713 Gravity.CENTER);
Andrii Kulian2e751b82016-03-16 16:59:32 -07003714 int minimalWidth = sw.getDimensionPixelSize(
3715 com.android.internal.R.styleable.AndroidManifestLayout_minimalWidth,
3716 -1);
3717 int minimalHeight = sw.getDimensionPixelSize(
3718 com.android.internal.R.styleable.AndroidManifestLayout_minimalHeight,
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003719 -1);
Filip Gruszczynski848fe6b2015-08-19 13:32:41 -07003720 sw.recycle();
Andrii Kulian2e751b82016-03-16 16:59:32 -07003721 a.info.windowLayout = new ActivityInfo.WindowLayout(width, widthFraction,
3722 height, heightFraction, gravity, minimalWidth, minimalHeight);
Filip Gruszczynski848fe6b2015-08-19 13:32:41 -07003723 }
3724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003725 private Activity parseActivityAlias(Package owner, Resources res,
Svet Ganov354cd3c2015-12-17 11:35:04 -08003726 XmlResourceParser parser, int flags, String[] outError)
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003727 throws XmlPullParserException, IOException {
Svet Ganov354cd3c2015-12-17 11:35:04 -08003728 TypedArray sa = res.obtainAttributes(parser,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 com.android.internal.R.styleable.AndroidManifestActivityAlias);
3730
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003731 String targetActivity = sa.getNonConfigurationString(
Dianne Hackborn1d0b1772013-09-06 14:02:54 -07003732 com.android.internal.R.styleable.AndroidManifestActivityAlias_targetActivity,
3733 Configuration.NATIVE_CONFIG_VERSION);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003734 if (targetActivity == null) {
3735 outError[0] = "<activity-alias> does not specify android:targetActivity";
3736 sa.recycle();
3737 return null;
3738 }
3739
3740 targetActivity = buildClassName(owner.applicationInfo.packageName,
3741 targetActivity, outError);
3742 if (targetActivity == null) {
3743 sa.recycle();
3744 return null;
3745 }
3746
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003747 if (mParseActivityAliasArgs == null) {
3748 mParseActivityAliasArgs = new ParseComponentArgs(owner, outError,
3749 com.android.internal.R.styleable.AndroidManifestActivityAlias_name,
3750 com.android.internal.R.styleable.AndroidManifestActivityAlias_label,
3751 com.android.internal.R.styleable.AndroidManifestActivityAlias_icon,
Adam Powell81cd2e92010-04-21 16:35:18 -07003752 com.android.internal.R.styleable.AndroidManifestActivityAlias_logo,
Jose Limaf78e3122014-03-06 12:13:15 -08003753 com.android.internal.R.styleable.AndroidManifestActivityAlias_banner,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003754 mSeparateProcesses,
3755 0,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08003756 com.android.internal.R.styleable.AndroidManifestActivityAlias_description,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003757 com.android.internal.R.styleable.AndroidManifestActivityAlias_enabled);
3758 mParseActivityAliasArgs.tag = "<activity-alias>";
3759 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003760
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003761 mParseActivityAliasArgs.sa = sa;
3762 mParseActivityAliasArgs.flags = flags;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003764 Activity target = null;
3765
3766 final int NA = owner.activities.size();
3767 for (int i=0; i<NA; i++) {
3768 Activity t = owner.activities.get(i);
3769 if (targetActivity.equals(t.info.name)) {
3770 target = t;
3771 break;
3772 }
3773 }
3774
3775 if (target == null) {
3776 outError[0] = "<activity-alias> target activity " + targetActivity
3777 + " not found in manifest";
3778 sa.recycle();
3779 return null;
3780 }
3781
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003782 ActivityInfo info = new ActivityInfo();
3783 info.targetActivity = targetActivity;
3784 info.configChanges = target.info.configChanges;
3785 info.flags = target.info.flags;
3786 info.icon = target.info.icon;
Adam Powell81cd2e92010-04-21 16:35:18 -07003787 info.logo = target.info.logo;
Jose Limaf78e3122014-03-06 12:13:15 -08003788 info.banner = target.info.banner;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003789 info.labelRes = target.info.labelRes;
3790 info.nonLocalizedLabel = target.info.nonLocalizedLabel;
3791 info.launchMode = target.info.launchMode;
Yao Chen597ad3f2015-06-09 16:00:14 -07003792 info.lockTaskLaunchMode = target.info.lockTaskLaunchMode;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003793 info.processName = target.info.processName;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08003794 if (info.descriptionRes == 0) {
3795 info.descriptionRes = target.info.descriptionRes;
3796 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003797 info.screenOrientation = target.info.screenOrientation;
3798 info.taskAffinity = target.info.taskAffinity;
3799 info.theme = target.info.theme;
Dianne Hackborn0836c7c2011-10-20 18:40:23 -07003800 info.softInputMode = target.info.softInputMode;
Adam Powell269248d2011-08-02 10:26:54 -07003801 info.uiOptions = target.info.uiOptions;
Adam Powelldd8fab22012-03-22 17:47:27 -07003802 info.parentActivityName = target.info.parentActivityName;
Craig Mautner8307ea72014-09-11 15:03:53 -07003803 info.maxRecents = target.info.maxRecents;
Andrii Kulian2e751b82016-03-16 16:59:32 -07003804 info.windowLayout = target.info.windowLayout;
Wale Ogunwaled26176f2016-01-25 20:04:04 -08003805 info.resizeMode = target.info.resizeMode;
Jeff Sharkey8a372a02016-03-16 16:25:45 -06003806 info.encryptionAware = info.directBootAware = target.info.directBootAware;
Craig Mautner8307ea72014-09-11 15:03:53 -07003807
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003808 Activity a = new Activity(mParseActivityAliasArgs, info);
3809 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003810 sa.recycle();
3811 return null;
3812 }
3813
3814 final boolean setExported = sa.hasValue(
3815 com.android.internal.R.styleable.AndroidManifestActivityAlias_exported);
3816 if (setExported) {
3817 a.info.exported = sa.getBoolean(
3818 com.android.internal.R.styleable.AndroidManifestActivityAlias_exported, false);
3819 }
3820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003821 String str;
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003822 str = sa.getNonConfigurationString(
3823 com.android.internal.R.styleable.AndroidManifestActivityAlias_permission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003824 if (str != null) {
3825 a.info.permission = str.length() > 0 ? str.toString().intern() : null;
3826 }
3827
Adam Powelldd8fab22012-03-22 17:47:27 -07003828 String parentName = sa.getNonConfigurationString(
3829 com.android.internal.R.styleable.AndroidManifestActivityAlias_parentActivityName,
Dianne Hackborn1d0b1772013-09-06 14:02:54 -07003830 Configuration.NATIVE_CONFIG_VERSION);
Adam Powelldd8fab22012-03-22 17:47:27 -07003831 if (parentName != null) {
3832 String parentClassName = buildClassName(a.info.packageName, parentName, outError);
3833 if (outError[0] == null) {
3834 a.info.parentActivityName = parentClassName;
3835 } else {
3836 Log.e(TAG, "Activity alias " + a.info.name +
3837 " specified invalid parentActivityName " + parentName);
3838 outError[0] = null;
3839 }
3840 }
3841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003842 sa.recycle();
3843
3844 if (outError[0] != null) {
3845 return null;
3846 }
3847
3848 int outerDepth = parser.getDepth();
3849 int type;
3850 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
3851 && (type != XmlPullParser.END_TAG
3852 || parser.getDepth() > outerDepth)) {
3853 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
3854 continue;
3855 }
3856
3857 if (parser.getName().equals("intent-filter")) {
3858 ActivityIntentInfo intent = new ActivityIntentInfo(a);
Svet Ganov354cd3c2015-12-17 11:35:04 -08003859 if (!parseIntent(res, parser, true, true, intent, outError)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003860 return null;
3861 }
3862 if (intent.countActions() == 0) {
Kenny Rootd2d29252011-08-08 11:27:57 -07003863 Slog.w(TAG, "No actions in intent filter at "
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -07003864 + mArchiveSourcePath + " "
3865 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003866 } else {
3867 a.intents.add(intent);
3868 }
3869 } else if (parser.getName().equals("meta-data")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08003870 if ((a.metaData=parseMetaData(res, parser, a.metaData,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003871 outError)) == null) {
3872 return null;
3873 }
3874 } else {
3875 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07003876 Slog.w(TAG, "Unknown element under <activity-alias>: " + parser.getName()
Dianne Hackborna33e3f72009-09-29 17:28:24 -07003877 + " at " + mArchiveSourcePath + " "
3878 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003879 XmlUtils.skipCurrentTag(parser);
3880 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07003881 } else {
3882 outError[0] = "Bad element under <activity-alias>: " + parser.getName();
3883 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003885 }
3886 }
3887
3888 if (!setExported) {
3889 a.info.exported = a.intents.size() > 0;
3890 }
3891
3892 return a;
3893 }
3894
3895 private Provider parseProvider(Package owner, Resources res,
Svet Ganov354cd3c2015-12-17 11:35:04 -08003896 XmlResourceParser parser, int flags, String[] outError)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003897 throws XmlPullParserException, IOException {
Svet Ganov354cd3c2015-12-17 11:35:04 -08003898 TypedArray sa = res.obtainAttributes(parser,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899 com.android.internal.R.styleable.AndroidManifestProvider);
3900
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003901 if (mParseProviderArgs == null) {
3902 mParseProviderArgs = new ParseComponentArgs(owner, outError,
3903 com.android.internal.R.styleable.AndroidManifestProvider_name,
3904 com.android.internal.R.styleable.AndroidManifestProvider_label,
3905 com.android.internal.R.styleable.AndroidManifestProvider_icon,
Adam Powell81cd2e92010-04-21 16:35:18 -07003906 com.android.internal.R.styleable.AndroidManifestProvider_logo,
Jose Limaf78e3122014-03-06 12:13:15 -08003907 com.android.internal.R.styleable.AndroidManifestProvider_banner,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003908 mSeparateProcesses,
3909 com.android.internal.R.styleable.AndroidManifestProvider_process,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08003910 com.android.internal.R.styleable.AndroidManifestProvider_description,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003911 com.android.internal.R.styleable.AndroidManifestProvider_enabled);
3912 mParseProviderArgs.tag = "<provider>";
3913 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003914
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003915 mParseProviderArgs.sa = sa;
3916 mParseProviderArgs.flags = flags;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07003917
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003918 Provider p = new Provider(mParseProviderArgs, new ProviderInfo());
3919 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003920 sa.recycle();
3921 return null;
3922 }
3923
Nick Kralevichf097b162012-07-28 12:43:48 -07003924 boolean providerExportedDefault = false;
3925
3926 if (owner.applicationInfo.targetSdkVersion < Build.VERSION_CODES.JELLY_BEAN_MR1) {
3927 // For compatibility, applications targeting API level 16 or lower
3928 // should have their content providers exported by default, unless they
3929 // specify otherwise.
3930 providerExportedDefault = true;
3931 }
3932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003933 p.info.exported = sa.getBoolean(
Nick Kralevichf097b162012-07-28 12:43:48 -07003934 com.android.internal.R.styleable.AndroidManifestProvider_exported,
3935 providerExportedDefault);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003936
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003937 String cpname = sa.getNonConfigurationString(
3938 com.android.internal.R.styleable.AndroidManifestProvider_authorities, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939
3940 p.info.isSyncable = sa.getBoolean(
3941 com.android.internal.R.styleable.AndroidManifestProvider_syncable,
3942 false);
3943
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003944 String permission = sa.getNonConfigurationString(
3945 com.android.internal.R.styleable.AndroidManifestProvider_permission, 0);
3946 String str = sa.getNonConfigurationString(
3947 com.android.internal.R.styleable.AndroidManifestProvider_readPermission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003948 if (str == null) {
3949 str = permission;
3950 }
3951 if (str == null) {
3952 p.info.readPermission = owner.applicationInfo.permission;
3953 } else {
3954 p.info.readPermission =
3955 str.length() > 0 ? str.toString().intern() : null;
3956 }
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003957 str = sa.getNonConfigurationString(
3958 com.android.internal.R.styleable.AndroidManifestProvider_writePermission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003959 if (str == null) {
3960 str = permission;
3961 }
3962 if (str == null) {
3963 p.info.writePermission = owner.applicationInfo.permission;
3964 } else {
3965 p.info.writePermission =
3966 str.length() > 0 ? str.toString().intern() : null;
3967 }
3968
3969 p.info.grantUriPermissions = sa.getBoolean(
3970 com.android.internal.R.styleable.AndroidManifestProvider_grantUriPermissions,
3971 false);
3972
3973 p.info.multiprocess = sa.getBoolean(
3974 com.android.internal.R.styleable.AndroidManifestProvider_multiprocess,
3975 false);
3976
3977 p.info.initOrder = sa.getInt(
3978 com.android.internal.R.styleable.AndroidManifestProvider_initOrder,
3979 0);
3980
Dianne Hackborn7d19e022012-08-07 19:12:33 -07003981 p.info.flags = 0;
3982
3983 if (sa.getBoolean(
3984 com.android.internal.R.styleable.AndroidManifestProvider_singleUser,
3985 false)) {
3986 p.info.flags |= ProviderInfo.FLAG_SINGLE_USER;
Amith Yamasani4b9d79c2014-05-21 19:14:21 -07003987 if (p.info.exported && (flags & PARSE_IS_PRIVILEGED) == 0) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07003988 Slog.w(TAG, "Provider exported request ignored due to singleUser: "
3989 + p.className + " at " + mArchiveSourcePath + " "
3990 + parser.getPositionDescription());
3991 p.info.exported = false;
3992 }
3993 }
3994
Jeff Sharkey8a372a02016-03-16 16:25:45 -06003995 p.info.encryptionAware = p.info.directBootAware = sa.getBoolean(
3996 R.styleable.AndroidManifestProvider_directBootAware,
Todd Kennedyd022ac22016-04-13 10:49:29 -07003997 false);
Jeff Sharkey8a372a02016-03-16 16:25:45 -06003998 if (p.info.directBootAware) {
Jeff Sharkey8924e872015-11-30 12:52:10 -07003999 owner.applicationInfo.privateFlags |=
Jeff Sharkey8a372a02016-03-16 16:25:45 -06004000 ApplicationInfo.PRIVATE_FLAG_PARTIALLY_DIRECT_BOOT_AWARE;
Jeff Sharkey8924e872015-11-30 12:52:10 -07004001 }
Jeff Sharkeye17ac152015-11-06 22:40:29 -08004002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004003 sa.recycle();
4004
Alex Klyubinb9f8a522015-02-03 11:12:59 -08004005 if ((owner.applicationInfo.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE)
4006 != 0) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07004007 // A heavy-weight application can not have providers in its main process
4008 // We can do direct compare because we intern all strings.
4009 if (p.info.processName == owner.packageName) {
4010 outError[0] = "Heavy-weight applications can not have providers in main process";
4011 return null;
4012 }
4013 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004015 if (cpname == null) {
Nick Kralevichf097b162012-07-28 12:43:48 -07004016 outError[0] = "<provider> does not include authorities attribute";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004017 return null;
4018 }
Dianne Hackborn1bc177c2014-12-03 10:20:30 -08004019 if (cpname.length() <= 0) {
4020 outError[0] = "<provider> has empty authorities attribute";
4021 return null;
4022 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004023 p.info.authority = cpname.intern();
4024
Svet Ganov354cd3c2015-12-17 11:35:04 -08004025 if (!parseProviderTags(res, parser, p, outError)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004026 return null;
4027 }
4028
4029 return p;
4030 }
4031
4032 private boolean parseProviderTags(Resources res,
Svet Ganov354cd3c2015-12-17 11:35:04 -08004033 XmlResourceParser parser, Provider outInfo, String[] outError)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004034 throws XmlPullParserException, IOException {
4035 int outerDepth = parser.getDepth();
4036 int type;
4037 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
4038 && (type != XmlPullParser.END_TAG
4039 || parser.getDepth() > outerDepth)) {
4040 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
4041 continue;
4042 }
4043
Jeff Sharkey85f5f812013-10-07 10:16:12 -07004044 if (parser.getName().equals("intent-filter")) {
4045 ProviderIntentInfo intent = new ProviderIntentInfo(outInfo);
Svet Ganov354cd3c2015-12-17 11:35:04 -08004046 if (!parseIntent(res, parser, true, false, intent, outError)) {
Jeff Sharkey85f5f812013-10-07 10:16:12 -07004047 return false;
4048 }
4049 outInfo.intents.add(intent);
4050
4051 } else if (parser.getName().equals("meta-data")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08004052 if ((outInfo.metaData=parseMetaData(res, parser,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053 outInfo.metaData, outError)) == null) {
4054 return false;
4055 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004057 } else if (parser.getName().equals("grant-uri-permission")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08004058 TypedArray sa = res.obtainAttributes(parser,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004059 com.android.internal.R.styleable.AndroidManifestGrantUriPermission);
4060
4061 PatternMatcher pa = null;
4062
Dianne Hackborncf244ad2010-03-09 15:00:30 -08004063 String str = sa.getNonConfigurationString(
4064 com.android.internal.R.styleable.AndroidManifestGrantUriPermission_path, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004065 if (str != null) {
4066 pa = new PatternMatcher(str, PatternMatcher.PATTERN_LITERAL);
4067 }
4068
Dianne Hackborncf244ad2010-03-09 15:00:30 -08004069 str = sa.getNonConfigurationString(
4070 com.android.internal.R.styleable.AndroidManifestGrantUriPermission_pathPrefix, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004071 if (str != null) {
4072 pa = new PatternMatcher(str, PatternMatcher.PATTERN_PREFIX);
4073 }
4074
Dianne Hackborncf244ad2010-03-09 15:00:30 -08004075 str = sa.getNonConfigurationString(
4076 com.android.internal.R.styleable.AndroidManifestGrantUriPermission_pathPattern, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004077 if (str != null) {
4078 pa = new PatternMatcher(str, PatternMatcher.PATTERN_SIMPLE_GLOB);
4079 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004081 sa.recycle();
4082
4083 if (pa != null) {
4084 if (outInfo.info.uriPermissionPatterns == null) {
4085 outInfo.info.uriPermissionPatterns = new PatternMatcher[1];
4086 outInfo.info.uriPermissionPatterns[0] = pa;
4087 } else {
4088 final int N = outInfo.info.uriPermissionPatterns.length;
4089 PatternMatcher[] newp = new PatternMatcher[N+1];
4090 System.arraycopy(outInfo.info.uriPermissionPatterns, 0, newp, 0, N);
4091 newp[N] = pa;
4092 outInfo.info.uriPermissionPatterns = newp;
4093 }
4094 outInfo.info.grantUriPermissions = true;
Dianne Hackborn2af632f2009-07-08 14:56:37 -07004095 } else {
4096 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07004097 Slog.w(TAG, "Unknown element under <path-permission>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07004098 + parser.getName() + " at " + mArchiveSourcePath + " "
4099 + parser.getPositionDescription());
Dianne Hackborn2af632f2009-07-08 14:56:37 -07004100 XmlUtils.skipCurrentTag(parser);
4101 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07004102 } else {
4103 outError[0] = "No path, pathPrefix, or pathPattern for <path-permission>";
4104 return false;
Dianne Hackborn2af632f2009-07-08 14:56:37 -07004105 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07004106 }
4107 XmlUtils.skipCurrentTag(parser);
4108
4109 } else if (parser.getName().equals("path-permission")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08004110 TypedArray sa = res.obtainAttributes(parser,
Dianne Hackborn2af632f2009-07-08 14:56:37 -07004111 com.android.internal.R.styleable.AndroidManifestPathPermission);
4112
4113 PathPermission pa = null;
4114
Dianne Hackborncf244ad2010-03-09 15:00:30 -08004115 String permission = sa.getNonConfigurationString(
4116 com.android.internal.R.styleable.AndroidManifestPathPermission_permission, 0);
4117 String readPermission = sa.getNonConfigurationString(
4118 com.android.internal.R.styleable.AndroidManifestPathPermission_readPermission, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07004119 if (readPermission == null) {
4120 readPermission = permission;
4121 }
Dianne Hackborncf244ad2010-03-09 15:00:30 -08004122 String writePermission = sa.getNonConfigurationString(
4123 com.android.internal.R.styleable.AndroidManifestPathPermission_writePermission, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07004124 if (writePermission == null) {
4125 writePermission = permission;
4126 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004127
Dianne Hackborn2af632f2009-07-08 14:56:37 -07004128 boolean havePerm = false;
4129 if (readPermission != null) {
4130 readPermission = readPermission.intern();
4131 havePerm = true;
4132 }
4133 if (writePermission != null) {
Bjorn Bringerte04b1ad2010-02-09 13:56:08 +00004134 writePermission = writePermission.intern();
Dianne Hackborn2af632f2009-07-08 14:56:37 -07004135 havePerm = true;
4136 }
4137
4138 if (!havePerm) {
4139 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07004140 Slog.w(TAG, "No readPermission or writePermssion for <path-permission>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07004141 + parser.getName() + " at " + mArchiveSourcePath + " "
4142 + parser.getPositionDescription());
Dianne Hackborn2af632f2009-07-08 14:56:37 -07004143 XmlUtils.skipCurrentTag(parser);
4144 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07004145 } else {
4146 outError[0] = "No readPermission or writePermssion for <path-permission>";
4147 return false;
Dianne Hackborn2af632f2009-07-08 14:56:37 -07004148 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07004149 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004150
Dianne Hackborncf244ad2010-03-09 15:00:30 -08004151 String path = sa.getNonConfigurationString(
4152 com.android.internal.R.styleable.AndroidManifestPathPermission_path, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07004153 if (path != null) {
4154 pa = new PathPermission(path,
4155 PatternMatcher.PATTERN_LITERAL, readPermission, writePermission);
4156 }
4157
Dianne Hackborncf244ad2010-03-09 15:00:30 -08004158 path = sa.getNonConfigurationString(
4159 com.android.internal.R.styleable.AndroidManifestPathPermission_pathPrefix, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07004160 if (path != null) {
4161 pa = new PathPermission(path,
4162 PatternMatcher.PATTERN_PREFIX, readPermission, writePermission);
4163 }
4164
Dianne Hackborncf244ad2010-03-09 15:00:30 -08004165 path = sa.getNonConfigurationString(
4166 com.android.internal.R.styleable.AndroidManifestPathPermission_pathPattern, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07004167 if (path != null) {
4168 pa = new PathPermission(path,
4169 PatternMatcher.PATTERN_SIMPLE_GLOB, readPermission, writePermission);
4170 }
4171
4172 sa.recycle();
4173
4174 if (pa != null) {
4175 if (outInfo.info.pathPermissions == null) {
4176 outInfo.info.pathPermissions = new PathPermission[1];
4177 outInfo.info.pathPermissions[0] = pa;
4178 } else {
4179 final int N = outInfo.info.pathPermissions.length;
4180 PathPermission[] newp = new PathPermission[N+1];
4181 System.arraycopy(outInfo.info.pathPermissions, 0, newp, 0, N);
4182 newp[N] = pa;
4183 outInfo.info.pathPermissions = newp;
4184 }
4185 } else {
4186 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07004187 Slog.w(TAG, "No path, pathPrefix, or pathPattern for <path-permission>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07004188 + parser.getName() + " at " + mArchiveSourcePath + " "
4189 + parser.getPositionDescription());
Dianne Hackborn2af632f2009-07-08 14:56:37 -07004190 XmlUtils.skipCurrentTag(parser);
4191 continue;
4192 }
4193 outError[0] = "No path, pathPrefix, or pathPattern for <path-permission>";
4194 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004195 }
4196 XmlUtils.skipCurrentTag(parser);
4197
4198 } else {
4199 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07004200 Slog.w(TAG, "Unknown element under <provider>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07004201 + parser.getName() + " at " + mArchiveSourcePath + " "
4202 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004203 XmlUtils.skipCurrentTag(parser);
4204 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07004205 } else {
4206 outError[0] = "Bad element under <provider>: " + parser.getName();
4207 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004209 }
4210 }
4211 return true;
4212 }
4213
4214 private Service parseService(Package owner, Resources res,
Svet Ganov354cd3c2015-12-17 11:35:04 -08004215 XmlResourceParser parser, int flags, String[] outError)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004216 throws XmlPullParserException, IOException {
Svet Ganov354cd3c2015-12-17 11:35:04 -08004217 TypedArray sa = res.obtainAttributes(parser,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004218 com.android.internal.R.styleable.AndroidManifestService);
4219
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004220 if (mParseServiceArgs == null) {
4221 mParseServiceArgs = new ParseComponentArgs(owner, outError,
4222 com.android.internal.R.styleable.AndroidManifestService_name,
4223 com.android.internal.R.styleable.AndroidManifestService_label,
4224 com.android.internal.R.styleable.AndroidManifestService_icon,
Adam Powell81cd2e92010-04-21 16:35:18 -07004225 com.android.internal.R.styleable.AndroidManifestService_logo,
Jose Limaf78e3122014-03-06 12:13:15 -08004226 com.android.internal.R.styleable.AndroidManifestService_banner,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004227 mSeparateProcesses,
4228 com.android.internal.R.styleable.AndroidManifestService_process,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08004229 com.android.internal.R.styleable.AndroidManifestService_description,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004230 com.android.internal.R.styleable.AndroidManifestService_enabled);
4231 mParseServiceArgs.tag = "<service>";
4232 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004233
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004234 mParseServiceArgs.sa = sa;
4235 mParseServiceArgs.flags = flags;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004236
Dianne Hackborn1d442e02009-04-20 18:14:05 -07004237 Service s = new Service(mParseServiceArgs, new ServiceInfo());
4238 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004239 sa.recycle();
4240 return null;
4241 }
4242
Dianne Hackbornb4163a62012-08-02 18:31:26 -07004243 boolean setExported = sa.hasValue(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004244 com.android.internal.R.styleable.AndroidManifestService_exported);
4245 if (setExported) {
4246 s.info.exported = sa.getBoolean(
4247 com.android.internal.R.styleable.AndroidManifestService_exported, false);
4248 }
4249
Dianne Hackborncf244ad2010-03-09 15:00:30 -08004250 String str = sa.getNonConfigurationString(
4251 com.android.internal.R.styleable.AndroidManifestService_permission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004252 if (str == null) {
4253 s.info.permission = owner.applicationInfo.permission;
4254 } else {
4255 s.info.permission = str.length() > 0 ? str.toString().intern() : null;
4256 }
4257
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004258 s.info.flags = 0;
4259 if (sa.getBoolean(
4260 com.android.internal.R.styleable.AndroidManifestService_stopWithTask,
4261 false)) {
4262 s.info.flags |= ServiceInfo.FLAG_STOP_WITH_TASK;
4263 }
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004264 if (sa.getBoolean(
4265 com.android.internal.R.styleable.AndroidManifestService_isolatedProcess,
4266 false)) {
4267 s.info.flags |= ServiceInfo.FLAG_ISOLATED_PROCESS;
4268 }
Dianne Hackbornb4163a62012-08-02 18:31:26 -07004269 if (sa.getBoolean(
Robert Sesekb9a86662015-12-09 16:22:45 -05004270 com.android.internal.R.styleable.AndroidManifestService_externalService,
4271 false)) {
4272 s.info.flags |= ServiceInfo.FLAG_EXTERNAL_SERVICE;
4273 }
4274 if (sa.getBoolean(
Dianne Hackbornb4163a62012-08-02 18:31:26 -07004275 com.android.internal.R.styleable.AndroidManifestService_singleUser,
4276 false)) {
4277 s.info.flags |= ServiceInfo.FLAG_SINGLE_USER;
Amith Yamasani4b9d79c2014-05-21 19:14:21 -07004278 if (s.info.exported && (flags & PARSE_IS_PRIVILEGED) == 0) {
Dianne Hackbornb4163a62012-08-02 18:31:26 -07004279 Slog.w(TAG, "Service exported request ignored due to singleUser: "
4280 + s.className + " at " + mArchiveSourcePath + " "
4281 + parser.getPositionDescription());
4282 s.info.exported = false;
Amith Yamasani4b9d79c2014-05-21 19:14:21 -07004283 setExported = true;
Dianne Hackbornb4163a62012-08-02 18:31:26 -07004284 }
Dianne Hackbornb4163a62012-08-02 18:31:26 -07004285 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07004286
Jeff Sharkey8a372a02016-03-16 16:25:45 -06004287 s.info.encryptionAware = s.info.directBootAware = sa.getBoolean(
4288 R.styleable.AndroidManifestService_directBootAware,
Todd Kennedyd022ac22016-04-13 10:49:29 -07004289 false);
Jeff Sharkey8a372a02016-03-16 16:25:45 -06004290 if (s.info.directBootAware) {
Jeff Sharkey8924e872015-11-30 12:52:10 -07004291 owner.applicationInfo.privateFlags |=
Jeff Sharkey8a372a02016-03-16 16:25:45 -06004292 ApplicationInfo.PRIVATE_FLAG_PARTIALLY_DIRECT_BOOT_AWARE;
Jeff Sharkey8924e872015-11-30 12:52:10 -07004293 }
Jeff Sharkeye17ac152015-11-06 22:40:29 -08004294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004295 sa.recycle();
4296
Alex Klyubinb9f8a522015-02-03 11:12:59 -08004297 if ((owner.applicationInfo.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE)
4298 != 0) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07004299 // A heavy-weight application can not have services in its main process
4300 // We can do direct compare because we intern all strings.
4301 if (s.info.processName == owner.packageName) {
4302 outError[0] = "Heavy-weight applications can not have services in main process";
4303 return null;
4304 }
4305 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004307 int outerDepth = parser.getDepth();
4308 int type;
4309 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
4310 && (type != XmlPullParser.END_TAG
4311 || parser.getDepth() > outerDepth)) {
4312 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
4313 continue;
4314 }
4315
4316 if (parser.getName().equals("intent-filter")) {
4317 ServiceIntentInfo intent = new ServiceIntentInfo(s);
Svet Ganov354cd3c2015-12-17 11:35:04 -08004318 if (!parseIntent(res, parser, true, false, intent, outError)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004319 return null;
4320 }
4321
4322 s.intents.add(intent);
4323 } else if (parser.getName().equals("meta-data")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08004324 if ((s.metaData=parseMetaData(res, parser, s.metaData,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004325 outError)) == null) {
4326 return null;
4327 }
4328 } else {
4329 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07004330 Slog.w(TAG, "Unknown element under <service>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07004331 + parser.getName() + " at " + mArchiveSourcePath + " "
4332 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004333 XmlUtils.skipCurrentTag(parser);
4334 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07004335 } else {
4336 outError[0] = "Bad element under <service>: " + parser.getName();
4337 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004339 }
4340 }
4341
4342 if (!setExported) {
4343 s.info.exported = s.intents.size() > 0;
4344 }
4345
4346 return s;
4347 }
4348
Svet Ganov354cd3c2015-12-17 11:35:04 -08004349 private boolean parseAllMetaData(Resources res, XmlResourceParser parser, String tag,
4350 Component<?> outInfo, String[] outError) throws XmlPullParserException, IOException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004351 int outerDepth = parser.getDepth();
4352 int type;
4353 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
4354 && (type != XmlPullParser.END_TAG
4355 || parser.getDepth() > outerDepth)) {
4356 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
4357 continue;
4358 }
4359
4360 if (parser.getName().equals("meta-data")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08004361 if ((outInfo.metaData=parseMetaData(res, parser,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004362 outInfo.metaData, outError)) == null) {
4363 return false;
4364 }
4365 } else {
4366 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07004367 Slog.w(TAG, "Unknown element under " + tag + ": "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07004368 + parser.getName() + " at " + mArchiveSourcePath + " "
4369 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004370 XmlUtils.skipCurrentTag(parser);
4371 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07004372 } else {
4373 outError[0] = "Bad element under " + tag + ": " + parser.getName();
4374 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004376 }
4377 }
4378 return true;
4379 }
4380
4381 private Bundle parseMetaData(Resources res,
Svet Ganov354cd3c2015-12-17 11:35:04 -08004382 XmlResourceParser parser, Bundle data, String[] outError)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004383 throws XmlPullParserException, IOException {
4384
Svet Ganov354cd3c2015-12-17 11:35:04 -08004385 TypedArray sa = res.obtainAttributes(parser,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004386 com.android.internal.R.styleable.AndroidManifestMetaData);
4387
4388 if (data == null) {
4389 data = new Bundle();
4390 }
4391
Dianne Hackborncf244ad2010-03-09 15:00:30 -08004392 String name = sa.getNonConfigurationString(
4393 com.android.internal.R.styleable.AndroidManifestMetaData_name, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004394 if (name == null) {
4395 outError[0] = "<meta-data> requires an android:name attribute";
4396 sa.recycle();
4397 return null;
4398 }
4399
Dianne Hackborn854060af2009-07-09 18:14:31 -07004400 name = name.intern();
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004402 TypedValue v = sa.peekValue(
4403 com.android.internal.R.styleable.AndroidManifestMetaData_resource);
4404 if (v != null && v.resourceId != 0) {
Kenny Rootd2d29252011-08-08 11:27:57 -07004405 //Slog.i(TAG, "Meta data ref " + name + ": " + v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004406 data.putInt(name, v.resourceId);
4407 } else {
4408 v = sa.peekValue(
4409 com.android.internal.R.styleable.AndroidManifestMetaData_value);
Kenny Rootd2d29252011-08-08 11:27:57 -07004410 //Slog.i(TAG, "Meta data " + name + ": " + v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004411 if (v != null) {
4412 if (v.type == TypedValue.TYPE_STRING) {
4413 CharSequence cs = v.coerceToString();
Dianne Hackborn854060af2009-07-09 18:14:31 -07004414 data.putString(name, cs != null ? cs.toString().intern() : null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004415 } else if (v.type == TypedValue.TYPE_INT_BOOLEAN) {
4416 data.putBoolean(name, v.data != 0);
4417 } else if (v.type >= TypedValue.TYPE_FIRST_INT
4418 && v.type <= TypedValue.TYPE_LAST_INT) {
4419 data.putInt(name, v.data);
4420 } else if (v.type == TypedValue.TYPE_FLOAT) {
4421 data.putFloat(name, v.getFloat());
4422 } else {
4423 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07004424 Slog.w(TAG, "<meta-data> only supports string, integer, float, color, boolean, and resource reference types: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07004425 + parser.getName() + " at " + mArchiveSourcePath + " "
4426 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004427 } else {
4428 outError[0] = "<meta-data> only supports string, integer, float, color, boolean, and resource reference types";
4429 data = null;
4430 }
4431 }
4432 } else {
4433 outError[0] = "<meta-data> requires an android:value or android:resource attribute";
4434 data = null;
4435 }
4436 }
4437
4438 sa.recycle();
4439
4440 XmlUtils.skipCurrentTag(parser);
4441
4442 return data;
4443 }
4444
Kenny Root05ca4c92011-09-15 10:36:25 -07004445 private static VerifierInfo parseVerifier(Resources res, XmlPullParser parser,
Jeff Sharkey275e0852014-06-17 18:18:49 -07004446 AttributeSet attrs, int flags) {
Kenny Root05ca4c92011-09-15 10:36:25 -07004447 final TypedArray sa = res.obtainAttributes(attrs,
4448 com.android.internal.R.styleable.AndroidManifestPackageVerifier);
4449
4450 final String packageName = sa.getNonResourceString(
4451 com.android.internal.R.styleable.AndroidManifestPackageVerifier_name);
4452
4453 final String encodedPublicKey = sa.getNonResourceString(
4454 com.android.internal.R.styleable.AndroidManifestPackageVerifier_publicKey);
4455
4456 sa.recycle();
4457
4458 if (packageName == null || packageName.length() == 0) {
4459 Slog.i(TAG, "verifier package name was null; skipping");
4460 return null;
Kenny Root05ca4c92011-09-15 10:36:25 -07004461 }
4462
Christopher Tate30147332014-04-15 12:57:47 -07004463 final PublicKey publicKey = parsePublicKey(encodedPublicKey);
4464 if (publicKey == null) {
4465 Slog.i(TAG, "Unable to parse verifier public key for " + packageName);
4466 return null;
Geremy Condraf1bcca82013-01-07 22:35:24 -08004467 }
4468
Christopher Tate30147332014-04-15 12:57:47 -07004469 return new VerifierInfo(packageName, publicKey);
Geremy Condraf1bcca82013-01-07 22:35:24 -08004470 }
4471
Christopher Tate30147332014-04-15 12:57:47 -07004472 public static final PublicKey parsePublicKey(final String encodedPublicKey) {
4473 if (encodedPublicKey == null) {
dcashman1616f302015-05-29 14:47:23 -07004474 Slog.w(TAG, "Could not parse null public key");
Christopher Tate30147332014-04-15 12:57:47 -07004475 return null;
4476 }
4477
Kenny Root05ca4c92011-09-15 10:36:25 -07004478 EncodedKeySpec keySpec;
4479 try {
4480 final byte[] encoded = Base64.decode(encodedPublicKey, Base64.DEFAULT);
4481 keySpec = new X509EncodedKeySpec(encoded);
4482 } catch (IllegalArgumentException e) {
dcashman1616f302015-05-29 14:47:23 -07004483 Slog.w(TAG, "Could not parse verifier public key; invalid Base64");
Kenny Root05ca4c92011-09-15 10:36:25 -07004484 return null;
4485 }
4486
4487 /* First try the key as an RSA key. */
4488 try {
4489 final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
Geremy Condraf1bcca82013-01-07 22:35:24 -08004490 return keyFactory.generatePublic(keySpec);
Kenny Root05ca4c92011-09-15 10:36:25 -07004491 } catch (NoSuchAlgorithmException e) {
dcashman1616f302015-05-29 14:47:23 -07004492 Slog.wtf(TAG, "Could not parse public key: RSA KeyFactory not included in build");
Kenny Root05ca4c92011-09-15 10:36:25 -07004493 } catch (InvalidKeySpecException e) {
4494 // Not a RSA public key.
4495 }
4496
dcashman1616f302015-05-29 14:47:23 -07004497 /* Now try it as a ECDSA key. */
4498 try {
4499 final KeyFactory keyFactory = KeyFactory.getInstance("EC");
4500 return keyFactory.generatePublic(keySpec);
4501 } catch (NoSuchAlgorithmException e) {
4502 Slog.wtf(TAG, "Could not parse public key: EC KeyFactory not included in build");
4503 } catch (InvalidKeySpecException e) {
4504 // Not a ECDSA public key.
4505 }
4506
Kenny Root05ca4c92011-09-15 10:36:25 -07004507 /* Now try it as a DSA key. */
4508 try {
4509 final KeyFactory keyFactory = KeyFactory.getInstance("DSA");
Geremy Condraf1bcca82013-01-07 22:35:24 -08004510 return keyFactory.generatePublic(keySpec);
Kenny Root05ca4c92011-09-15 10:36:25 -07004511 } catch (NoSuchAlgorithmException e) {
dcashman1616f302015-05-29 14:47:23 -07004512 Slog.wtf(TAG, "Could not parse public key: DSA KeyFactory not included in build");
Kenny Root05ca4c92011-09-15 10:36:25 -07004513 } catch (InvalidKeySpecException e) {
4514 // Not a DSA public key.
4515 }
4516
dcashman1616f302015-05-29 14:47:23 -07004517 /* Not a supported key type */
Kenny Root05ca4c92011-09-15 10:36:25 -07004518 return null;
4519 }
4520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004521 private static final String ANDROID_RESOURCES
4522 = "http://schemas.android.com/apk/res/android";
4523
Svet Ganov354cd3c2015-12-17 11:35:04 -08004524 private boolean parseIntent(Resources res, XmlResourceParser parser,
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08004525 boolean allowGlobs, boolean allowAutoVerify, IntentInfo outInfo, String[] outError)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004526 throws XmlPullParserException, IOException {
4527
Svet Ganov354cd3c2015-12-17 11:35:04 -08004528 TypedArray sa = res.obtainAttributes(parser,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004529 com.android.internal.R.styleable.AndroidManifestIntentFilter);
4530
4531 int priority = sa.getInt(
4532 com.android.internal.R.styleable.AndroidManifestIntentFilter_priority, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004533 outInfo.setPriority(priority);
Kenny Root502e9a42011-01-10 13:48:15 -08004534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004535 TypedValue v = sa.peekValue(
4536 com.android.internal.R.styleable.AndroidManifestIntentFilter_label);
4537 if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
4538 outInfo.nonLocalizedLabel = v.coerceToString();
4539 }
4540
4541 outInfo.icon = sa.getResourceId(
4542 com.android.internal.R.styleable.AndroidManifestIntentFilter_icon, 0);
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004543
Adam Powell81cd2e92010-04-21 16:35:18 -07004544 outInfo.logo = sa.getResourceId(
4545 com.android.internal.R.styleable.AndroidManifestIntentFilter_logo, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004546
Jose Limaf78e3122014-03-06 12:13:15 -08004547 outInfo.banner = sa.getResourceId(
4548 com.android.internal.R.styleable.AndroidManifestIntentFilter_banner, 0);
4549
Fabrice Di Meglio1c1b4712014-11-19 17:12:32 -08004550 if (allowAutoVerify) {
4551 outInfo.setAutoVerify(sa.getBoolean(
4552 com.android.internal.R.styleable.AndroidManifestIntentFilter_autoVerify,
4553 false));
4554 }
4555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004556 sa.recycle();
4557
4558 int outerDepth = parser.getDepth();
4559 int type;
Kenny Rootd2d29252011-08-08 11:27:57 -07004560 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
4561 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
4562 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004563 continue;
4564 }
4565
4566 String nodeName = parser.getName();
4567 if (nodeName.equals("action")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08004568 String value = parser.getAttributeValue(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004569 ANDROID_RESOURCES, "name");
4570 if (value == null || value == "") {
4571 outError[0] = "No value supplied for <android:name>";
4572 return false;
4573 }
4574 XmlUtils.skipCurrentTag(parser);
4575
4576 outInfo.addAction(value);
4577 } else if (nodeName.equals("category")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08004578 String value = parser.getAttributeValue(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004579 ANDROID_RESOURCES, "name");
4580 if (value == null || value == "") {
4581 outError[0] = "No value supplied for <android:name>";
4582 return false;
4583 }
4584 XmlUtils.skipCurrentTag(parser);
4585
4586 outInfo.addCategory(value);
4587
4588 } else if (nodeName.equals("data")) {
Svet Ganov354cd3c2015-12-17 11:35:04 -08004589 sa = res.obtainAttributes(parser,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004590 com.android.internal.R.styleable.AndroidManifestData);
4591
Dianne Hackborncf244ad2010-03-09 15:00:30 -08004592 String str = sa.getNonConfigurationString(
4593 com.android.internal.R.styleable.AndroidManifestData_mimeType, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004594 if (str != null) {
4595 try {
4596 outInfo.addDataType(str);
4597 } catch (IntentFilter.MalformedMimeTypeException e) {
4598 outError[0] = e.toString();
4599 sa.recycle();
4600 return false;
4601 }
4602 }
4603
Dianne Hackborncf244ad2010-03-09 15:00:30 -08004604 str = sa.getNonConfigurationString(
4605 com.android.internal.R.styleable.AndroidManifestData_scheme, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004606 if (str != null) {
4607 outInfo.addDataScheme(str);
4608 }
4609
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -07004610 str = sa.getNonConfigurationString(
4611 com.android.internal.R.styleable.AndroidManifestData_ssp, 0);
4612 if (str != null) {
4613 outInfo.addDataSchemeSpecificPart(str, PatternMatcher.PATTERN_LITERAL);
4614 }
4615
4616 str = sa.getNonConfigurationString(
4617 com.android.internal.R.styleable.AndroidManifestData_sspPrefix, 0);
4618 if (str != null) {
4619 outInfo.addDataSchemeSpecificPart(str, PatternMatcher.PATTERN_PREFIX);
4620 }
4621
4622 str = sa.getNonConfigurationString(
4623 com.android.internal.R.styleable.AndroidManifestData_sspPattern, 0);
4624 if (str != null) {
Dianne Hackbornb09491f2013-07-22 15:30:11 -07004625 if (!allowGlobs) {
4626 outError[0] = "sspPattern not allowed here; ssp must be literal";
4627 return false;
4628 }
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -07004629 outInfo.addDataSchemeSpecificPart(str, PatternMatcher.PATTERN_SIMPLE_GLOB);
4630 }
4631
Dianne Hackborncf244ad2010-03-09 15:00:30 -08004632 String host = sa.getNonConfigurationString(
4633 com.android.internal.R.styleable.AndroidManifestData_host, 0);
4634 String port = sa.getNonConfigurationString(
4635 com.android.internal.R.styleable.AndroidManifestData_port, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004636 if (host != null) {
4637 outInfo.addDataAuthority(host, port);
4638 }
4639
Dianne Hackborncf244ad2010-03-09 15:00:30 -08004640 str = sa.getNonConfigurationString(
4641 com.android.internal.R.styleable.AndroidManifestData_path, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004642 if (str != null) {
4643 outInfo.addDataPath(str, PatternMatcher.PATTERN_LITERAL);
4644 }
4645
Dianne Hackborncf244ad2010-03-09 15:00:30 -08004646 str = sa.getNonConfigurationString(
4647 com.android.internal.R.styleable.AndroidManifestData_pathPrefix, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004648 if (str != null) {
4649 outInfo.addDataPath(str, PatternMatcher.PATTERN_PREFIX);
4650 }
4651
Dianne Hackborncf244ad2010-03-09 15:00:30 -08004652 str = sa.getNonConfigurationString(
4653 com.android.internal.R.styleable.AndroidManifestData_pathPattern, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004654 if (str != null) {
Dianne Hackbornb09491f2013-07-22 15:30:11 -07004655 if (!allowGlobs) {
4656 outError[0] = "pathPattern not allowed here; path must be literal";
4657 return false;
4658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004659 outInfo.addDataPath(str, PatternMatcher.PATTERN_SIMPLE_GLOB);
4660 }
4661
4662 sa.recycle();
4663 XmlUtils.skipCurrentTag(parser);
4664 } else if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07004665 Slog.w(TAG, "Unknown element under <intent-filter>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07004666 + parser.getName() + " at " + mArchiveSourcePath + " "
4667 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004668 XmlUtils.skipCurrentTag(parser);
4669 } else {
4670 outError[0] = "Bad element under <intent-filter>: " + parser.getName();
4671 return false;
4672 }
4673 }
4674
4675 outInfo.hasDefault = outInfo.hasCategory(Intent.CATEGORY_DEFAULT);
Kenny Rootd2d29252011-08-08 11:27:57 -07004676
4677 if (DEBUG_PARSER) {
4678 final StringBuilder cats = new StringBuilder("Intent d=");
4679 cats.append(outInfo.hasDefault);
4680 cats.append(", cat=");
4681
4682 final Iterator<String> it = outInfo.categoriesIterator();
4683 if (it != null) {
4684 while (it.hasNext()) {
4685 cats.append(' ');
4686 cats.append(it.next());
4687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004688 }
Kenny Rootd2d29252011-08-08 11:27:57 -07004689 Slog.d(TAG, cats.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004690 }
4691
4692 return true;
4693 }
4694
Jeff Sharkeyc4858a22014-06-16 10:51:20 -07004695 /**
4696 * Representation of a full package parsed from APK files on disk. A package
4697 * consists of a single base APK, and zero or more split APKs.
4698 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004699 public final static class Package {
Amith Yamasani0ac1fc92013-03-27 18:56:08 -07004700
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08004701 public String packageName;
Jeff Sharkey88d2a3c2014-11-22 16:49:34 -08004702
Jeff Sharkey57dcf5b2014-06-18 17:46:05 -07004703 /** Names of any split APKs, ordered by parsed splitName */
4704 public String[] splitNames;
Jeff Sharkeyc4858a22014-06-16 10:51:20 -07004705
4706 // TODO: work towards making these paths invariant
Jeff Sharkey275e0852014-06-17 18:18:49 -07004707
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07004708 public String volumeUuid;
4709
Jeff Sharkey57dcf5b2014-06-18 17:46:05 -07004710 /**
4711 * Path where this package was found on disk. For monolithic packages
4712 * this is path to single base APK file; for cluster packages this is
4713 * path to the cluster directory.
4714 */
Jeff Sharkeyc4858a22014-06-16 10:51:20 -07004715 public String codePath;
Jeff Sharkey57dcf5b2014-06-18 17:46:05 -07004716
4717 /** Path of base APK */
4718 public String baseCodePath;
4719 /** Paths of any split APKs, ordered by parsed splitName */
Jeff Sharkeyc4858a22014-06-16 10:51:20 -07004720 public String[] splitCodePaths;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004721
Jeff Sharkey88d2a3c2014-11-22 16:49:34 -08004722 /** Revision code of base APK */
4723 public int baseRevisionCode;
4724 /** Revision codes of any split APKs, ordered by parsed splitName */
4725 public int[] splitRevisionCodes;
4726
Jeff Sharkeyda96e132014-07-15 14:54:09 -07004727 /** Flags of any split APKs; ordered by parsed splitName */
4728 public int[] splitFlags;
4729
Alex Klyubinb9f8a522015-02-03 11:12:59 -08004730 /**
4731 * Private flags of any split APKs; ordered by parsed splitName.
4732 *
4733 * {@hide}
4734 */
4735 public int[] splitPrivateFlags;
4736
Jeff Sharkeyda96e132014-07-15 14:54:09 -07004737 public boolean baseHardwareAccelerated;
4738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004739 // For now we only support one application per package.
4740 public final ApplicationInfo applicationInfo = new ApplicationInfo();
4741
4742 public final ArrayList<Permission> permissions = new ArrayList<Permission>(0);
4743 public final ArrayList<PermissionGroup> permissionGroups = new ArrayList<PermissionGroup>(0);
4744 public final ArrayList<Activity> activities = new ArrayList<Activity>(0);
4745 public final ArrayList<Activity> receivers = new ArrayList<Activity>(0);
4746 public final ArrayList<Provider> providers = new ArrayList<Provider>(0);
4747 public final ArrayList<Service> services = new ArrayList<Service>(0);
4748 public final ArrayList<Instrumentation> instrumentation = new ArrayList<Instrumentation>(0);
4749
4750 public final ArrayList<String> requestedPermissions = new ArrayList<String>();
4751
Dianne Hackborn854060af2009-07-09 18:14:31 -07004752 public ArrayList<String> protectedBroadcasts;
Dianne Hackbornc895be72013-03-11 17:48:43 -07004753
Svet Ganov354cd3c2015-12-17 11:35:04 -08004754 public Package parentPackage;
4755 public ArrayList<Package> childPackages;
4756
Dianne Hackbornc895be72013-03-11 17:48:43 -07004757 public ArrayList<String> libraryNames = null;
Dianne Hackborn49237342009-08-27 20:08:01 -07004758 public ArrayList<String> usesLibraries = null;
4759 public ArrayList<String> usesOptionalLibraries = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004760 public String[] usesLibraryFiles = null;
4761
Dianne Hackbornb09491f2013-07-22 15:30:11 -07004762 public ArrayList<ActivityIntentInfo> preferredActivityFilters = null;
4763
Dianne Hackbornc1552392010-03-03 16:19:01 -08004764 public ArrayList<String> mOriginalPackages = null;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08004765 public String mRealPackage = null;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08004766 public ArrayList<String> mAdoptPermissions = null;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004768 // We store the application meta-data independently to avoid multiple unwanted references
4769 public Bundle mAppMetaData = null;
4770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004771 // The version code declared for this package.
4772 public int mVersionCode;
Jeff Sharkey88d2a3c2014-11-22 16:49:34 -08004773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004774 // The version name declared for this package.
4775 public String mVersionName;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07004776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004777 // The shared user id that this package wants to use.
4778 public String mSharedUserId;
4779
4780 // The shared user label that this package wants to use.
4781 public int mSharedUserLabel;
4782
4783 // Signatures that were read from the package.
Jeff Sharkey275e0852014-06-17 18:18:49 -07004784 public Signature[] mSignatures;
4785 public Certificate[][] mCertificates;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004786
4787 // For use by package manager service for quick lookup of
4788 // preferred up order.
4789 public int mPreferredOrder = 0;
4790
Brian Carlstromff1ec4d2014-03-17 15:21:35 -07004791 // For use by package manager to keep track of when a package was last used.
Brian Carlstromca82e612016-04-19 23:16:08 -07004792 public long[] mLastPackageUsageTimeInMills =
4793 new long[PackageManager.NOTIFY_PACKAGE_USE_REASONS_COUNT];
Brian Carlstromff1ec4d2014-03-17 15:21:35 -07004794
Amith Yamasani13593602012-03-22 16:16:17 -07004795 // // User set enabled state.
4796 // public int mSetEnabled = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
4797 //
4798 // // Whether the package has been stopped.
4799 // public boolean mSetStopped = false;
Dianne Hackborne7f97212011-02-24 14:40:20 -08004800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004801 // Additional data supplied by callers.
4802 public Object mExtras;
Kenny Rootdeb11262010-08-02 11:36:21 -07004803
Jeff Sharkeyda96e132014-07-15 14:54:09 -07004804 // Applications hardware preferences
4805 public ArrayList<ConfigurationInfo> configPreferences = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004806
Jeff Sharkeyda96e132014-07-15 14:54:09 -07004807 // Applications requested features
Dianne Hackborn49237342009-08-27 20:08:01 -07004808 public ArrayList<FeatureInfo> reqFeatures = null;
4809
Adam Lesinskid3edfde2014-08-08 17:32:44 -07004810 // Applications requested feature groups
4811 public ArrayList<FeatureGroupInfo> featureGroups = null;
4812
Suchi Amalapurapu117818e2010-02-09 03:45:40 -08004813 public int installLocation;
4814
Jeff Hao272bf3a2014-10-08 13:34:43 -07004815 public boolean coreApp;
4816
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08004817 /* An app that's required for all users and cannot be uninstalled for a user */
4818 public boolean mRequiredForAllUsers;
4819
Amith Yamasani0ac1fc92013-03-27 18:56:08 -07004820 /* The restricted account authenticator type that is used by this application */
4821 public String mRestrictedAccountType;
4822
Amith Yamasaniccbe3892013-04-12 17:52:42 -07004823 /* The required account type without which this application will not function */
4824 public String mRequiredAccountType;
4825
MÃ¥rten Kongstad48d22322014-01-31 14:43:27 +01004826 public String mOverlayTarget;
4827 public int mOverlayPriority;
4828 public boolean mTrustedOverlay;
4829
Geremy Condraf1bcca82013-01-07 22:35:24 -08004830 /**
dcashman989eb3712014-06-17 12:56:12 -07004831 * Data used to feed the KeySetManagerService
Geremy Condraf1bcca82013-01-07 22:35:24 -08004832 */
dcashman989eb3712014-06-17 12:56:12 -07004833 public ArraySet<PublicKey> mSigningKeys;
4834 public ArraySet<String> mUpgradeKeySets;
4835 public ArrayMap<String, ArraySet<PublicKey>> mKeySetMapping;
Geremy Condraf1bcca82013-01-07 22:35:24 -08004836
Narayan Kamath4903f642014-08-11 13:33:45 +01004837 /**
4838 * The install time abi override for this package, if any.
4839 *
4840 * TODO: This seems like a horrible place to put the abiOverride because
4841 * this isn't something the packageParser parsers. However, this fits in with
4842 * the rest of the PackageManager where package scanning randomly pushes
4843 * and prods fields out of {@code this.applicationInfo}.
4844 */
4845 public String cpuAbiOverride;
Selim Gurun2d291d82016-02-05 17:50:53 -08004846 /**
4847 * The install time abi override to choose 32bit abi's when multiple abi's
4848 * are present. This is only meaningfull for multiarch applications.
4849 * The use32bitAbi attribute is ignored if cpuAbiOverride is also set.
4850 */
4851 public boolean use32bitAbi;
Narayan Kamath4903f642014-08-11 13:33:45 +01004852
Todd Kennedyfdd241a2016-04-28 12:26:53 -07004853 public byte[] restrictUpdateHash;
4854
Jeff Sharkeyc4858a22014-06-16 10:51:20 -07004855 public Package(String packageName) {
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07004856 this.packageName = packageName;
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07004857 applicationInfo.packageName = packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004858 applicationInfo.uid = -1;
4859 }
4860
Svet Ganov354cd3c2015-12-17 11:35:04 -08004861 public void setApplicationVolumeUuid(String volumeUuid) {
4862 this.applicationInfo.volumeUuid = volumeUuid;
4863 if (childPackages != null) {
4864 final int packageCount = childPackages.size();
4865 for (int i = 0; i < packageCount; i++) {
4866 childPackages.get(i).applicationInfo.volumeUuid = volumeUuid;
4867 }
4868 }
4869 }
4870
4871 public void setApplicationInfoCodePath(String codePath) {
4872 this.applicationInfo.setCodePath(codePath);
4873 if (childPackages != null) {
4874 final int packageCount = childPackages.size();
4875 for (int i = 0; i < packageCount; i++) {
4876 childPackages.get(i).applicationInfo.setCodePath(codePath);
4877 }
4878 }
4879 }
4880
4881 public void setApplicationInfoResourcePath(String resourcePath) {
4882 this.applicationInfo.setResourcePath(resourcePath);
4883 if (childPackages != null) {
4884 final int packageCount = childPackages.size();
4885 for (int i = 0; i < packageCount; i++) {
4886 childPackages.get(i).applicationInfo.setResourcePath(resourcePath);
4887 }
4888 }
4889 }
4890
4891 public void setApplicationInfoBaseResourcePath(String resourcePath) {
4892 this.applicationInfo.setBaseResourcePath(resourcePath);
4893 if (childPackages != null) {
4894 final int packageCount = childPackages.size();
4895 for (int i = 0; i < packageCount; i++) {
4896 childPackages.get(i).applicationInfo.setBaseResourcePath(resourcePath);
4897 }
4898 }
4899 }
4900
4901 public void setApplicationInfoBaseCodePath(String baseCodePath) {
4902 this.applicationInfo.setBaseCodePath(baseCodePath);
4903 if (childPackages != null) {
4904 final int packageCount = childPackages.size();
4905 for (int i = 0; i < packageCount; i++) {
4906 childPackages.get(i).applicationInfo.setBaseCodePath(baseCodePath);
4907 }
4908 }
4909 }
4910
4911 public boolean hasChildPackage(String packageName) {
4912 final int childCount = (childPackages != null) ? childPackages.size() : 0;
4913 for (int i = 0; i < childCount; i++) {
4914 if (childPackages.get(i).packageName.equals(packageName)) {
4915 return true;
4916 }
4917 }
4918 return false;
4919 }
4920
4921 public void setApplicationInfoSplitCodePaths(String[] splitCodePaths) {
4922 this.applicationInfo.setSplitCodePaths(splitCodePaths);
4923 // Children have no splits
4924 }
4925
4926 public void setApplicationInfoSplitResourcePaths(String[] resroucePaths) {
4927 this.applicationInfo.setSplitResourcePaths(resroucePaths);
4928 // Children have no splits
4929 }
4930
4931 public void setSplitCodePaths(String[] codePaths) {
4932 this.splitCodePaths = codePaths;
4933 }
4934
4935 public void setCodePath(String codePath) {
4936 this.codePath = codePath;
4937 if (childPackages != null) {
4938 final int packageCount = childPackages.size();
4939 for (int i = 0; i < packageCount; i++) {
4940 childPackages.get(i).codePath = codePath;
4941 }
4942 }
4943 }
4944
4945 public void setBaseCodePath(String baseCodePath) {
4946 this.baseCodePath = baseCodePath;
4947 if (childPackages != null) {
4948 final int packageCount = childPackages.size();
4949 for (int i = 0; i < packageCount; i++) {
4950 childPackages.get(i).baseCodePath = baseCodePath;
4951 }
4952 }
4953 }
4954
4955 public void setSignatures(Signature[] signatures) {
4956 this.mSignatures = signatures;
4957 if (childPackages != null) {
4958 final int packageCount = childPackages.size();
4959 for (int i = 0; i < packageCount; i++) {
4960 childPackages.get(i).mSignatures = signatures;
4961 }
4962 }
4963 }
4964
4965 public void setVolumeUuid(String volumeUuid) {
4966 this.volumeUuid = volumeUuid;
4967 if (childPackages != null) {
4968 final int packageCount = childPackages.size();
4969 for (int i = 0; i < packageCount; i++) {
4970 childPackages.get(i).volumeUuid = volumeUuid;
4971 }
4972 }
4973 }
4974
4975 public void setApplicationInfoFlags(int mask, int flags) {
4976 applicationInfo.flags = (applicationInfo.flags & ~mask) | (mask & flags);
4977 if (childPackages != null) {
4978 final int packageCount = childPackages.size();
4979 for (int i = 0; i < packageCount; i++) {
4980 childPackages.get(i).applicationInfo.flags =
4981 (applicationInfo.flags & ~mask) | (mask & flags);
4982 }
4983 }
4984 }
4985
Selim Gurun75827b22016-02-09 10:55:33 -08004986 public void setUse32bitAbi(boolean use32bitAbi) {
Selim Gurun2d291d82016-02-05 17:50:53 -08004987 this.use32bitAbi = use32bitAbi;
Svet Ganov354cd3c2015-12-17 11:35:04 -08004988 if (childPackages != null) {
4989 final int packageCount = childPackages.size();
4990 for (int i = 0; i < packageCount; i++) {
Selim Gurun2d291d82016-02-05 17:50:53 -08004991 childPackages.get(i).use32bitAbi = use32bitAbi;
Svet Ganov354cd3c2015-12-17 11:35:04 -08004992 }
4993 }
4994 }
4995
Jeff Sharkey57dcf5b2014-06-18 17:46:05 -07004996 public List<String> getAllCodePaths() {
Jeff Sharkey275e0852014-06-17 18:18:49 -07004997 ArrayList<String> paths = new ArrayList<>();
Jeff Sharkey57dcf5b2014-06-18 17:46:05 -07004998 paths.add(baseCodePath);
Jeff Sharkey275e0852014-06-17 18:18:49 -07004999 if (!ArrayUtils.isEmpty(splitCodePaths)) {
5000 Collections.addAll(paths, splitCodePaths);
5001 }
5002 return paths;
5003 }
5004
Jeff Sharkeyda96e132014-07-15 14:54:09 -07005005 /**
5006 * Filtered set of {@link #getAllCodePaths()} that excludes
5007 * resource-only APKs.
5008 */
5009 public List<String> getAllCodePathsExcludingResourceOnly() {
5010 ArrayList<String> paths = new ArrayList<>();
5011 if ((applicationInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0) {
5012 paths.add(baseCodePath);
5013 }
5014 if (!ArrayUtils.isEmpty(splitCodePaths)) {
5015 for (int i = 0; i < splitCodePaths.length; i++) {
5016 if ((splitFlags[i] & ApplicationInfo.FLAG_HAS_CODE) != 0) {
5017 paths.add(splitCodePaths[i]);
5018 }
5019 }
5020 }
5021 return paths;
5022 }
5023
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08005024 public void setPackageName(String newName) {
5025 packageName = newName;
5026 applicationInfo.packageName = newName;
5027 for (int i=permissions.size()-1; i>=0; i--) {
5028 permissions.get(i).setPackageName(newName);
5029 }
5030 for (int i=permissionGroups.size()-1; i>=0; i--) {
5031 permissionGroups.get(i).setPackageName(newName);
5032 }
5033 for (int i=activities.size()-1; i>=0; i--) {
5034 activities.get(i).setPackageName(newName);
5035 }
5036 for (int i=receivers.size()-1; i>=0; i--) {
5037 receivers.get(i).setPackageName(newName);
5038 }
5039 for (int i=providers.size()-1; i>=0; i--) {
5040 providers.get(i).setPackageName(newName);
5041 }
5042 for (int i=services.size()-1; i>=0; i--) {
5043 services.get(i).setPackageName(newName);
5044 }
5045 for (int i=instrumentation.size()-1; i>=0; i--) {
5046 instrumentation.get(i).setPackageName(newName);
5047 }
5048 }
Dianne Hackborn65696252012-03-05 18:49:21 -08005049
5050 public boolean hasComponentClassName(String name) {
5051 for (int i=activities.size()-1; i>=0; i--) {
5052 if (name.equals(activities.get(i).className)) {
5053 return true;
5054 }
5055 }
5056 for (int i=receivers.size()-1; i>=0; i--) {
5057 if (name.equals(receivers.get(i).className)) {
5058 return true;
5059 }
5060 }
5061 for (int i=providers.size()-1; i>=0; i--) {
5062 if (name.equals(providers.get(i).className)) {
5063 return true;
5064 }
5065 }
5066 for (int i=services.size()-1; i>=0; i--) {
5067 if (name.equals(services.get(i).className)) {
5068 return true;
5069 }
5070 }
5071 for (int i=instrumentation.size()-1; i>=0; i--) {
5072 if (name.equals(instrumentation.get(i).className)) {
5073 return true;
5074 }
5075 }
5076 return false;
5077 }
5078
Fyodor Kupoloveeea67b2015-02-23 17:14:45 -08005079 /**
5080 * @hide
5081 */
5082 public boolean isForwardLocked() {
5083 return applicationInfo.isForwardLocked();
5084 }
5085
Fyodor Kupolovb94c1652015-03-03 12:25:30 -08005086 /**
5087 * @hide
5088 */
5089 public boolean isSystemApp() {
5090 return applicationInfo.isSystemApp();
5091 }
5092
5093 /**
5094 * @hide
5095 */
Svet Ganovadc1cf42015-06-15 16:36:24 -07005096 public boolean isPrivilegedApp() {
5097 return applicationInfo.isPrivilegedApp();
5098 }
5099
5100 /**
5101 * @hide
5102 */
Fyodor Kupolovb94c1652015-03-03 12:25:30 -08005103 public boolean isUpdatedSystemApp() {
5104 return applicationInfo.isUpdatedSystemApp();
5105 }
5106
Fyodor Kupolovebcac162015-09-09 15:56:45 -07005107 /**
5108 * @hide
5109 */
5110 public boolean canHaveOatDir() {
5111 // The following app types CANNOT have oat directory
5112 // - non-updated system apps
5113 // - forward-locked apps or apps installed in ASEC containers
5114 return (!isSystemApp() || isUpdatedSystemApp())
5115 && !isForwardLocked() && !applicationInfo.isExternalAsec();
5116 }
5117
Jeff Sharkeyc5967e92016-01-07 18:50:29 -07005118 public boolean isMatch(int flags) {
5119 if ((flags & PackageManager.MATCH_SYSTEM_ONLY) != 0) {
5120 return isSystemApp();
5121 }
5122 return true;
5123 }
5124
Brian Carlstromca82e612016-04-19 23:16:08 -07005125 public long getLatestPackageUseTimeInMills() {
5126 long latestUse = 0L;
5127 for (long use : mLastPackageUsageTimeInMills) {
5128 latestUse = Math.max(latestUse, use);
5129 }
5130 return latestUse;
5131 }
5132
David Brazdil90e26992016-04-18 14:08:52 +01005133 public long getLatestForegroundPackageUseTimeInMills() {
5134 int[] foregroundReasons = {
5135 PackageManager.NOTIFY_PACKAGE_USE_ACTIVITY,
5136 PackageManager.NOTIFY_PACKAGE_USE_FOREGROUND_SERVICE
5137 };
5138
5139 long latestUse = 0L;
5140 for (int reason : foregroundReasons) {
5141 latestUse = Math.max(latestUse, mLastPackageUsageTimeInMills[reason]);
5142 }
5143 return latestUse;
5144 }
5145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005146 public String toString() {
5147 return "Package{"
5148 + Integer.toHexString(System.identityHashCode(this))
5149 + " " + packageName + "}";
5150 }
5151 }
5152
5153 public static class Component<II extends IntentInfo> {
5154 public final Package owner;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005155 public final ArrayList<II> intents;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08005156 public final String className;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005157 public Bundle metaData;
5158
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08005159 ComponentName componentName;
5160 String componentShortName;
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07005161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005162 public Component(Package _owner) {
5163 owner = _owner;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005164 intents = null;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08005165 className = null;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005166 }
5167
5168 public Component(final ParsePackageItemArgs args, final PackageItemInfo outInfo) {
5169 owner = args.owner;
5170 intents = new ArrayList<II>(0);
Dianne Hackborncf244ad2010-03-09 15:00:30 -08005171 String name = args.sa.getNonConfigurationString(args.nameRes, 0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005172 if (name == null) {
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08005173 className = null;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005174 args.outError[0] = args.tag + " does not specify android:name";
5175 return;
5176 }
5177
5178 outInfo.name
5179 = buildClassName(owner.applicationInfo.packageName, name, args.outError);
5180 if (outInfo.name == null) {
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08005181 className = null;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005182 args.outError[0] = args.tag + " does not have valid android:name";
5183 return;
5184 }
5185
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08005186 className = outInfo.name;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005187
5188 int iconVal = args.sa.getResourceId(args.iconRes, 0);
5189 if (iconVal != 0) {
5190 outInfo.icon = iconVal;
5191 outInfo.nonLocalizedLabel = null;
5192 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07005193
Adam Powell81cd2e92010-04-21 16:35:18 -07005194 int logoVal = args.sa.getResourceId(args.logoRes, 0);
5195 if (logoVal != 0) {
5196 outInfo.logo = logoVal;
5197 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005198
Jose Limaf78e3122014-03-06 12:13:15 -08005199 int bannerVal = args.sa.getResourceId(args.bannerRes, 0);
5200 if (bannerVal != 0) {
5201 outInfo.banner = bannerVal;
5202 }
5203
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005204 TypedValue v = args.sa.peekValue(args.labelRes);
5205 if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
5206 outInfo.nonLocalizedLabel = v.coerceToString();
5207 }
5208
5209 outInfo.packageName = owner.packageName;
5210 }
5211
5212 public Component(final ParseComponentArgs args, final ComponentInfo outInfo) {
5213 this(args, (PackageItemInfo)outInfo);
5214 if (args.outError[0] != null) {
5215 return;
5216 }
5217
5218 if (args.processRes != 0) {
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07005219 CharSequence pname;
5220 if (owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.FROYO) {
Dianne Hackborn1d0b1772013-09-06 14:02:54 -07005221 pname = args.sa.getNonConfigurationString(args.processRes,
5222 Configuration.NATIVE_CONFIG_VERSION);
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07005223 } else {
5224 // Some older apps have been seen to use a resource reference
5225 // here that on older builds was ignored (with a warning). We
5226 // need to continue to do this for them so they don't break.
5227 pname = args.sa.getNonResourceString(args.processRes);
5228 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005229 outInfo.processName = buildProcessName(owner.applicationInfo.packageName,
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07005230 owner.applicationInfo.processName, pname,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005231 args.flags, args.sepProcesses, args.outError);
5232 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07005233
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08005234 if (args.descriptionRes != 0) {
5235 outInfo.descriptionRes = args.sa.getResourceId(args.descriptionRes, 0);
5236 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07005237
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005238 outInfo.enabled = args.sa.getBoolean(args.enabledRes, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005239 }
5240
5241 public Component(Component<II> clone) {
5242 owner = clone.owner;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005243 intents = clone.intents;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08005244 className = clone.className;
5245 componentName = clone.componentName;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005246 componentShortName = clone.componentShortName;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08005247 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07005248
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08005249 public ComponentName getComponentName() {
5250 if (componentName != null) {
5251 return componentName;
5252 }
5253 if (className != null) {
5254 componentName = new ComponentName(owner.applicationInfo.packageName,
5255 className);
5256 }
5257 return componentName;
5258 }
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -07005259
5260 public void appendComponentShortName(StringBuilder sb) {
5261 ComponentName.appendShortString(sb, owner.applicationInfo.packageName, className);
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08005262 }
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -07005263
5264 public void printComponentShortName(PrintWriter pw) {
5265 ComponentName.printShortString(pw, owner.applicationInfo.packageName, className);
5266 }
5267
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08005268 public void setPackageName(String packageName) {
5269 componentName = null;
5270 componentShortName = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005271 }
5272 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07005273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005274 public final static class Permission extends Component<IntentInfo> {
5275 public final PermissionInfo info;
5276 public boolean tree;
5277 public PermissionGroup group;
5278
5279 public Permission(Package _owner) {
5280 super(_owner);
5281 info = new PermissionInfo();
5282 }
5283
5284 public Permission(Package _owner, PermissionInfo _info) {
5285 super(_owner);
5286 info = _info;
5287 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07005288
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08005289 public void setPackageName(String packageName) {
5290 super.setPackageName(packageName);
5291 info.packageName = packageName;
5292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005293
5294 public String toString() {
5295 return "Permission{"
5296 + Integer.toHexString(System.identityHashCode(this))
5297 + " " + info.name + "}";
5298 }
5299 }
5300
5301 public final static class PermissionGroup extends Component<IntentInfo> {
5302 public final PermissionGroupInfo info;
5303
5304 public PermissionGroup(Package _owner) {
5305 super(_owner);
5306 info = new PermissionGroupInfo();
5307 }
5308
5309 public PermissionGroup(Package _owner, PermissionGroupInfo _info) {
5310 super(_owner);
5311 info = _info;
5312 }
5313
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08005314 public void setPackageName(String packageName) {
5315 super.setPackageName(packageName);
5316 info.packageName = packageName;
5317 }
5318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005319 public String toString() {
5320 return "PermissionGroup{"
5321 + Integer.toHexString(System.identityHashCode(this))
5322 + " " + info.name + "}";
5323 }
5324 }
5325
Dianne Hackborn7767eac2012-08-23 18:25:40 -07005326 private static boolean copyNeeded(int flags, Package p,
5327 PackageUserState state, Bundle metaData, int userId) {
Xiaohui Chen98404fd2015-08-17 16:09:02 -07005328 if (userId != UserHandle.USER_SYSTEM) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07005329 // We always need to copy for other users, since we need
5330 // to fix up the uid.
5331 return true;
5332 }
5333 if (state.enabled != PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
5334 boolean enabled = state.enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
Dianne Hackborn46730fc2010-07-24 16:32:42 -07005335 if (p.applicationInfo.enabled != enabled) {
5336 return true;
5337 }
5338 }
Rubin Xubf67c9c2016-04-20 17:09:38 +01005339 boolean suspended = (p.applicationInfo.flags & FLAG_SUSPENDED) != 0;
5340 if (state.suspended != suspended) {
5341 return true;
5342 }
Amith Yamasanie5bcff62014-07-19 15:44:09 -07005343 if (!state.installed || state.hidden) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07005344 return true;
5345 }
5346 if (state.stopped) {
5347 return true;
5348 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005349 if ((flags & PackageManager.GET_META_DATA) != 0
5350 && (metaData != null || p.mAppMetaData != null)) {
5351 return true;
5352 }
5353 if ((flags & PackageManager.GET_SHARED_LIBRARY_FILES) != 0
5354 && p.usesLibraryFiles != null) {
5355 return true;
5356 }
5357 return false;
5358 }
5359
Dianne Hackborn7767eac2012-08-23 18:25:40 -07005360 public static ApplicationInfo generateApplicationInfo(Package p, int flags,
5361 PackageUserState state) {
5362 return generateApplicationInfo(p, flags, state, UserHandle.getCallingUserId());
Amith Yamasani742a6712011-05-04 14:49:28 -07005363 }
5364
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08005365 private static void updateApplicationInfo(ApplicationInfo ai, int flags,
5366 PackageUserState state) {
5367 // CompatibilityMode is global state.
5368 if (!sCompatibilityModeEnabled) {
5369 ai.disableCompatibilityMode();
5370 }
5371 if (state.installed) {
5372 ai.flags |= ApplicationInfo.FLAG_INSTALLED;
5373 } else {
5374 ai.flags &= ~ApplicationInfo.FLAG_INSTALLED;
5375 }
Andrei Stingaceanu1e283912015-11-26 15:26:28 +00005376 if (state.suspended) {
5377 ai.flags |= ApplicationInfo.FLAG_SUSPENDED;
5378 } else {
5379 ai.flags &= ~ApplicationInfo.FLAG_SUSPENDED;
5380 }
Amith Yamasanie5bcff62014-07-19 15:44:09 -07005381 if (state.hidden) {
Alex Klyubinb9f8a522015-02-03 11:12:59 -08005382 ai.privateFlags |= ApplicationInfo.PRIVATE_FLAG_HIDDEN;
Amith Yamasani655d0e22013-06-12 14:19:10 -07005383 } else {
Alex Klyubinb9f8a522015-02-03 11:12:59 -08005384 ai.privateFlags &= ~ApplicationInfo.PRIVATE_FLAG_HIDDEN;
Amith Yamasani655d0e22013-06-12 14:19:10 -07005385 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08005386 if (state.enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
5387 ai.enabled = true;
5388 } else if (state.enabled == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
5389 ai.enabled = (flags&PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS) != 0;
5390 } else if (state.enabled == PackageManager.COMPONENT_ENABLED_STATE_DISABLED
5391 || state.enabled == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER) {
5392 ai.enabled = false;
5393 }
5394 ai.enabledSetting = state.enabled;
5395 }
5396
Amith Yamasani13593602012-03-22 16:16:17 -07005397 public static ApplicationInfo generateApplicationInfo(Package p, int flags,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07005398 PackageUserState state, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005399 if (p == null) return null;
Jeff Sharkeyc5967e92016-01-07 18:50:29 -07005400 if (!checkUseInstalledOrHidden(flags, state) || !p.isMatch(flags)) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07005401 return null;
5402 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08005403 if (!copyNeeded(flags, p, state, null, userId)
5404 && ((flags&PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS) == 0
5405 || state.enabled != PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED)) {
5406 // In this case it is safe to directly modify the internal ApplicationInfo state:
5407 // - CompatibilityMode is global state, so will be the same for every call.
5408 // - We only come in to here if the app should reported as installed; this is the
5409 // default state, and we will do a copy otherwise.
5410 // - The enable state will always be reported the same for the application across
5411 // calls; the only exception is for the UNTIL_USED mode, and in that case we will
5412 // be doing a copy.
5413 updateApplicationInfo(p.applicationInfo, flags, state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005414 return p.applicationInfo;
5415 }
5416
5417 // Make shallow copy so we can store the metadata/libraries safely
5418 ApplicationInfo ai = new ApplicationInfo(p.applicationInfo);
Jeff Sharkey15447792015-11-05 16:18:51 -08005419 ai.initForUser(userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005420 if ((flags & PackageManager.GET_META_DATA) != 0) {
5421 ai.metaData = p.mAppMetaData;
5422 }
5423 if ((flags & PackageManager.GET_SHARED_LIBRARY_FILES) != 0) {
5424 ai.sharedLibraryFiles = p.usesLibraryFiles;
5425 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07005426 if (state.stopped) {
Amith Yamasania4a54e22012-04-16 15:44:19 -07005427 ai.flags |= ApplicationInfo.FLAG_STOPPED;
Dianne Hackborne7f97212011-02-24 14:40:20 -08005428 } else {
Amith Yamasania4a54e22012-04-16 15:44:19 -07005429 ai.flags &= ~ApplicationInfo.FLAG_STOPPED;
Dianne Hackborne7f97212011-02-24 14:40:20 -08005430 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08005431 updateApplicationInfo(ai, flags, state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005432 return ai;
5433 }
5434
Dianne Hackbornace27912014-09-18 18:38:30 -07005435 public static ApplicationInfo generateApplicationInfo(ApplicationInfo ai, int flags,
5436 PackageUserState state, int userId) {
5437 if (ai == null) return null;
5438 if (!checkUseInstalledOrHidden(flags, state)) {
5439 return null;
5440 }
5441 // This is only used to return the ResolverActivity; we will just always
5442 // make a copy.
5443 ai = new ApplicationInfo(ai);
Jeff Sharkey15447792015-11-05 16:18:51 -08005444 ai.initForUser(userId);
Dianne Hackbornace27912014-09-18 18:38:30 -07005445 if (state.stopped) {
5446 ai.flags |= ApplicationInfo.FLAG_STOPPED;
5447 } else {
5448 ai.flags &= ~ApplicationInfo.FLAG_STOPPED;
5449 }
5450 updateApplicationInfo(ai, flags, state);
5451 return ai;
5452 }
5453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005454 public static final PermissionInfo generatePermissionInfo(
5455 Permission p, int flags) {
5456 if (p == null) return null;
5457 if ((flags&PackageManager.GET_META_DATA) == 0) {
5458 return p.info;
5459 }
5460 PermissionInfo pi = new PermissionInfo(p.info);
5461 pi.metaData = p.metaData;
5462 return pi;
5463 }
5464
5465 public static final PermissionGroupInfo generatePermissionGroupInfo(
5466 PermissionGroup pg, int flags) {
5467 if (pg == null) return null;
5468 if ((flags&PackageManager.GET_META_DATA) == 0) {
5469 return pg.info;
5470 }
5471 PermissionGroupInfo pgi = new PermissionGroupInfo(pg.info);
5472 pgi.metaData = pg.metaData;
5473 return pgi;
5474 }
5475
5476 public final static class Activity extends Component<ActivityIntentInfo> {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005477 public final ActivityInfo info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005478
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005479 public Activity(final ParseComponentArgs args, final ActivityInfo _info) {
5480 super(args, _info);
5481 info = _info;
5482 info.applicationInfo = args.owner.applicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005483 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07005484
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08005485 public void setPackageName(String packageName) {
5486 super.setPackageName(packageName);
5487 info.packageName = packageName;
5488 }
5489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005490 public String toString() {
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -07005491 StringBuilder sb = new StringBuilder(128);
5492 sb.append("Activity{");
5493 sb.append(Integer.toHexString(System.identityHashCode(this)));
5494 sb.append(' ');
5495 appendComponentShortName(sb);
5496 sb.append('}');
5497 return sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005498 }
5499 }
5500
Dianne Hackborn7767eac2012-08-23 18:25:40 -07005501 public static final ActivityInfo generateActivityInfo(Activity a, int flags,
5502 PackageUserState state, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005503 if (a == null) return null;
Amith Yamasanie5bcff62014-07-19 15:44:09 -07005504 if (!checkUseInstalledOrHidden(flags, state)) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07005505 return null;
5506 }
5507 if (!copyNeeded(flags, a.owner, state, a.metaData, userId)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005508 return a.info;
5509 }
5510 // Make shallow copies so we can store the metadata safely
5511 ActivityInfo ai = new ActivityInfo(a.info);
5512 ai.metaData = a.metaData;
Dianne Hackborn7767eac2012-08-23 18:25:40 -07005513 ai.applicationInfo = generateApplicationInfo(a.owner, flags, state, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005514 return ai;
5515 }
5516
Dianne Hackbornace27912014-09-18 18:38:30 -07005517 public static final ActivityInfo generateActivityInfo(ActivityInfo ai, int flags,
5518 PackageUserState state, int userId) {
5519 if (ai == null) return null;
5520 if (!checkUseInstalledOrHidden(flags, state)) {
5521 return null;
5522 }
5523 // This is only used to return the ResolverActivity; we will just always
5524 // make a copy.
5525 ai = new ActivityInfo(ai);
5526 ai.applicationInfo = generateApplicationInfo(ai.applicationInfo, flags, state, userId);
5527 return ai;
5528 }
5529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005530 public final static class Service extends Component<ServiceIntentInfo> {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005531 public final ServiceInfo info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005532
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005533 public Service(final ParseComponentArgs args, final ServiceInfo _info) {
5534 super(args, _info);
5535 info = _info;
5536 info.applicationInfo = args.owner.applicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005537 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07005538
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08005539 public void setPackageName(String packageName) {
5540 super.setPackageName(packageName);
5541 info.packageName = packageName;
5542 }
5543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005544 public String toString() {
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -07005545 StringBuilder sb = new StringBuilder(128);
5546 sb.append("Service{");
5547 sb.append(Integer.toHexString(System.identityHashCode(this)));
5548 sb.append(' ');
5549 appendComponentShortName(sb);
5550 sb.append('}');
5551 return sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005552 }
5553 }
5554
Dianne Hackborn7767eac2012-08-23 18:25:40 -07005555 public static final ServiceInfo generateServiceInfo(Service s, int flags,
5556 PackageUserState state, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005557 if (s == null) return null;
Amith Yamasanie5bcff62014-07-19 15:44:09 -07005558 if (!checkUseInstalledOrHidden(flags, state)) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07005559 return null;
5560 }
5561 if (!copyNeeded(flags, s.owner, state, s.metaData, userId)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005562 return s.info;
5563 }
5564 // Make shallow copies so we can store the metadata safely
5565 ServiceInfo si = new ServiceInfo(s.info);
5566 si.metaData = s.metaData;
Dianne Hackborn7767eac2012-08-23 18:25:40 -07005567 si.applicationInfo = generateApplicationInfo(s.owner, flags, state, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005568 return si;
5569 }
5570
Jeff Sharkey85f5f812013-10-07 10:16:12 -07005571 public final static class Provider extends Component<ProviderIntentInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005572 public final ProviderInfo info;
5573 public boolean syncable;
5574
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005575 public Provider(final ParseComponentArgs args, final ProviderInfo _info) {
5576 super(args, _info);
5577 info = _info;
5578 info.applicationInfo = args.owner.applicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005579 syncable = false;
5580 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07005581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005582 public Provider(Provider existingProvider) {
5583 super(existingProvider);
5584 this.info = existingProvider.info;
5585 this.syncable = existingProvider.syncable;
5586 }
5587
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08005588 public void setPackageName(String packageName) {
5589 super.setPackageName(packageName);
5590 info.packageName = packageName;
5591 }
5592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005593 public String toString() {
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -07005594 StringBuilder sb = new StringBuilder(128);
5595 sb.append("Provider{");
5596 sb.append(Integer.toHexString(System.identityHashCode(this)));
5597 sb.append(' ');
5598 appendComponentShortName(sb);
5599 sb.append('}');
5600 return sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005601 }
5602 }
5603
Dianne Hackborn7767eac2012-08-23 18:25:40 -07005604 public static final ProviderInfo generateProviderInfo(Provider p, int flags,
5605 PackageUserState state, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005606 if (p == null) return null;
Amith Yamasanie5bcff62014-07-19 15:44:09 -07005607 if (!checkUseInstalledOrHidden(flags, state)) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07005608 return null;
5609 }
5610 if (!copyNeeded(flags, p.owner, state, p.metaData, userId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005611 && ((flags & PackageManager.GET_URI_PERMISSION_PATTERNS) != 0
Dianne Hackborn7767eac2012-08-23 18:25:40 -07005612 || p.info.uriPermissionPatterns == null)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005613 return p.info;
5614 }
5615 // Make shallow copies so we can store the metadata safely
5616 ProviderInfo pi = new ProviderInfo(p.info);
5617 pi.metaData = p.metaData;
5618 if ((flags & PackageManager.GET_URI_PERMISSION_PATTERNS) == 0) {
5619 pi.uriPermissionPatterns = null;
5620 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07005621 pi.applicationInfo = generateApplicationInfo(p.owner, flags, state, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005622 return pi;
5623 }
5624
Todd Kennedy0f4bc2b2016-04-06 07:46:13 -07005625 public final static class Instrumentation extends Component<IntentInfo> {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005626 public final InstrumentationInfo info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005627
Dianne Hackborn1d442e02009-04-20 18:14:05 -07005628 public Instrumentation(final ParsePackageItemArgs args, final InstrumentationInfo _info) {
5629 super(args, _info);
5630 info = _info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005631 }
Filip Gruszczynskid2f1d942015-10-14 15:10:12 -07005632
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08005633 public void setPackageName(String packageName) {
5634 super.setPackageName(packageName);
5635 info.packageName = packageName;
5636 }
5637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005638 public String toString() {
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -07005639 StringBuilder sb = new StringBuilder(128);
5640 sb.append("Instrumentation{");
5641 sb.append(Integer.toHexString(System.identityHashCode(this)));
5642 sb.append(' ');
5643 appendComponentShortName(sb);
5644 sb.append('}');
5645 return sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005646 }
5647 }
5648
5649 public static final InstrumentationInfo generateInstrumentationInfo(
5650 Instrumentation i, int flags) {
5651 if (i == null) return null;
5652 if ((flags&PackageManager.GET_META_DATA) == 0) {
5653 return i.info;
5654 }
5655 InstrumentationInfo ii = new InstrumentationInfo(i.info);
5656 ii.metaData = i.metaData;
5657 return ii;
5658 }
5659
5660 public static class IntentInfo extends IntentFilter {
5661 public boolean hasDefault;
5662 public int labelRes;
5663 public CharSequence nonLocalizedLabel;
5664 public int icon;
Adam Powell81cd2e92010-04-21 16:35:18 -07005665 public int logo;
Jose Limaf78e3122014-03-06 12:13:15 -08005666 public int banner;
Dianne Hackbornb09491f2013-07-22 15:30:11 -07005667 public int preferred;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005668 }
5669
5670 public final static class ActivityIntentInfo extends IntentInfo {
5671 public final Activity activity;
5672
5673 public ActivityIntentInfo(Activity _activity) {
5674 activity = _activity;
5675 }
5676
5677 public String toString() {
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -07005678 StringBuilder sb = new StringBuilder(128);
5679 sb.append("ActivityIntentInfo{");
5680 sb.append(Integer.toHexString(System.identityHashCode(this)));
5681 sb.append(' ');
5682 activity.appendComponentShortName(sb);
5683 sb.append('}');
5684 return sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005685 }
5686 }
5687
5688 public final static class ServiceIntentInfo extends IntentInfo {
5689 public final Service service;
5690
5691 public ServiceIntentInfo(Service _service) {
5692 service = _service;
5693 }
5694
5695 public String toString() {
Dianne Hackborn6d8dfbd2013-09-23 17:38:51 -07005696 StringBuilder sb = new StringBuilder(128);
5697 sb.append("ServiceIntentInfo{");
5698 sb.append(Integer.toHexString(System.identityHashCode(this)));
5699 sb.append(' ');
5700 service.appendComponentShortName(sb);
5701 sb.append('}');
5702 return sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005703 }
5704 }
Mitsuru Oshima69fff4a2009-07-21 09:51:05 -07005705
Jeff Sharkey85f5f812013-10-07 10:16:12 -07005706 public static final class ProviderIntentInfo extends IntentInfo {
5707 public final Provider provider;
5708
5709 public ProviderIntentInfo(Provider provider) {
5710 this.provider = provider;
5711 }
5712
5713 public String toString() {
5714 StringBuilder sb = new StringBuilder(128);
5715 sb.append("ProviderIntentInfo{");
5716 sb.append(Integer.toHexString(System.identityHashCode(this)));
5717 sb.append(' ');
5718 provider.appendComponentShortName(sb);
5719 sb.append('}');
5720 return sb.toString();
5721 }
5722 }
5723
Mitsuru Oshima69fff4a2009-07-21 09:51:05 -07005724 /**
5725 * @hide
5726 */
5727 public static void setCompatibilityModeEnabled(boolean compatibilityModeEnabled) {
5728 sCompatibilityModeEnabled = compatibilityModeEnabled;
5729 }
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07005730
Jeff Sharkey275e0852014-06-17 18:18:49 -07005731 private static AtomicReference<byte[]> sBuffer = new AtomicReference<byte[]>();
5732
5733 public static long readFullyIgnoringContents(InputStream in) throws IOException {
5734 byte[] buffer = sBuffer.getAndSet(null);
5735 if (buffer == null) {
5736 buffer = new byte[4096];
5737 }
5738
5739 int n = 0;
5740 int count = 0;
5741 while ((n = in.read(buffer, 0, buffer.length)) != -1) {
5742 count += n;
5743 }
5744
5745 sBuffer.set(buffer);
5746 return count;
5747 }
5748
5749 public static void closeQuietly(StrictJarFile jarFile) {
5750 if (jarFile != null) {
5751 try {
5752 jarFile.close();
5753 } catch (Exception ignored) {
5754 }
5755 }
5756 }
5757
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07005758 public static class PackageParserException extends Exception {
5759 public final int error;
5760
5761 public PackageParserException(int error, String detailMessage) {
5762 super(detailMessage);
5763 this.error = error;
5764 }
Jeff Sharkey275e0852014-06-17 18:18:49 -07005765
5766 public PackageParserException(int error, String detailMessage, Throwable throwable) {
5767 super(detailMessage, throwable);
5768 this.error = error;
5769 }
Jeff Sharkey3a44f3f2014-04-28 17:36:31 -07005770 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005771}