blob: 883516ea7e68945c7d2937d3a40092383719cecb [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.content.pm;
18
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import android.content.ComponentName;
20import android.content.Intent;
21import android.content.IntentFilter;
22import android.content.res.AssetManager;
23import android.content.res.Configuration;
24import android.content.res.Resources;
25import android.content.res.TypedArray;
26import android.content.res.XmlResourceParser;
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -070027import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.os.Bundle;
29import android.os.PatternMatcher;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070030import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.util.AttributeSet;
Kenny Root05ca4c92011-09-15 10:36:25 -070032import android.util.Base64;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.util.DisplayMetrics;
Kenny Root05ca4c92011-09-15 10:36:25 -070034import android.util.Log;
Kenny Rootd2d29252011-08-08 11:27:57 -070035import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
Kenny Rootd63f7db2010-09-27 08:07:48 -070038import java.io.BufferedInputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import java.io.File;
40import java.io.IOException;
41import java.io.InputStream;
42import java.lang.ref.WeakReference;
Kenny Root05ca4c92011-09-15 10:36:25 -070043import java.security.KeyFactory;
44import java.security.NoSuchAlgorithmException;
45import java.security.PublicKey;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import java.security.cert.Certificate;
47import java.security.cert.CertificateEncodingException;
Geremy Condraf1bcca82013-01-07 22:35:24 -080048import java.security.cert.CertificateFactory;
49import java.security.cert.CertPath;
50import java.security.cert.X509Certificate;
Kenny Root05ca4c92011-09-15 10:36:25 -070051import java.security.spec.EncodedKeySpec;
52import java.security.spec.InvalidKeySpecException;
53import java.security.spec.X509EncodedKeySpec;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import java.util.ArrayList;
55import java.util.Enumeration;
Geremy Condraf1bcca82013-01-07 22:35:24 -080056import java.util.HashMap;
Dianne Hackborne639da72012-02-21 15:11:13 -080057import java.util.HashSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import java.util.Iterator;
Kenny Root05ca4c92011-09-15 10:36:25 -070059import java.util.List;
Geremy Condraf1bcca82013-01-07 22:35:24 -080060import java.util.Map;
61import java.util.Set;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import java.util.jar.JarEntry;
63import java.util.jar.JarFile;
Kenny Root6c918ce2013-04-02 14:04:24 -070064import java.util.zip.ZipEntry;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065
Amith Yamasani742a6712011-05-04 14:49:28 -070066import com.android.internal.util.XmlUtils;
67
68import org.xmlpull.v1.XmlPullParser;
69import org.xmlpull.v1.XmlPullParserException;
70
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071/**
72 * Package archive parsing
73 *
74 * {@hide}
75 */
76public class PackageParser {
Kenny Rootd2d29252011-08-08 11:27:57 -070077 private static final boolean DEBUG_JAR = false;
78 private static final boolean DEBUG_PARSER = false;
79 private static final boolean DEBUG_BACKUP = false;
80
Kenny Rootbcc954d2011-08-08 16:19:08 -070081 /** File name in an APK for the Android manifest. */
82 private static final String ANDROID_MANIFEST_FILENAME = "AndroidManifest.xml";
83
Dianne Hackborna96cbb42009-05-13 15:06:13 -070084 /** @hide */
85 public static class NewPermissionInfo {
86 public final String name;
87 public final int sdkVersion;
88 public final int fileVersion;
89
90 public NewPermissionInfo(String name, int sdkVersion, int fileVersion) {
91 this.name = name;
92 this.sdkVersion = sdkVersion;
93 this.fileVersion = fileVersion;
94 }
95 }
Dianne Hackborn79245122012-03-12 10:51:26 -070096
97 /** @hide */
98 public static class SplitPermissionInfo {
99 public final String rootPerm;
100 public final String[] newPerms;
Dianne Hackborn31b0e0e2012-04-05 19:33:30 -0700101 public final int targetSdk;
Dianne Hackborn79245122012-03-12 10:51:26 -0700102
Dianne Hackborn31b0e0e2012-04-05 19:33:30 -0700103 public SplitPermissionInfo(String rootPerm, String[] newPerms, int targetSdk) {
Dianne Hackborn79245122012-03-12 10:51:26 -0700104 this.rootPerm = rootPerm;
105 this.newPerms = newPerms;
Dianne Hackborn31b0e0e2012-04-05 19:33:30 -0700106 this.targetSdk = targetSdk;
Dianne Hackborn79245122012-03-12 10:51:26 -0700107 }
108 }
109
Dianne Hackborna96cbb42009-05-13 15:06:13 -0700110 /**
111 * List of new permissions that have been added since 1.0.
112 * NOTE: These must be declared in SDK version order, with permissions
113 * added to older SDKs appearing before those added to newer SDKs.
Dianne Hackborn79245122012-03-12 10:51:26 -0700114 * If sdkVersion is 0, then this is not a permission that we want to
115 * automatically add to older apps, but we do want to allow it to be
116 * granted during a platform update.
Dianne Hackborna96cbb42009-05-13 15:06:13 -0700117 * @hide
118 */
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700119 public static final PackageParser.NewPermissionInfo NEW_PERMISSIONS[] =
120 new PackageParser.NewPermissionInfo[] {
San Mehat5a3a77d2009-06-01 09:25:28 -0700121 new PackageParser.NewPermissionInfo(android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
Jaikumar Ganesh45515652009-04-23 15:20:21 -0700122 android.os.Build.VERSION_CODES.DONUT, 0),
123 new PackageParser.NewPermissionInfo(android.Manifest.permission.READ_PHONE_STATE,
124 android.os.Build.VERSION_CODES.DONUT, 0)
Dianne Hackborna96cbb42009-05-13 15:06:13 -0700125 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126
Dianne Hackborn79245122012-03-12 10:51:26 -0700127 /**
128 * List of permissions that have been split into more granular or dependent
129 * permissions.
130 * @hide
131 */
132 public static final PackageParser.SplitPermissionInfo SPLIT_PERMISSIONS[] =
133 new PackageParser.SplitPermissionInfo[] {
Dianne Hackborn2bd8d042012-06-11 12:27:05 -0700134 // READ_EXTERNAL_STORAGE is always required when an app requests
135 // WRITE_EXTERNAL_STORAGE, because we can't have an app that has
136 // write access without read access. The hack here with the target
137 // target SDK version ensures that this grant is always done.
Dianne Hackborn79245122012-03-12 10:51:26 -0700138 new PackageParser.SplitPermissionInfo(android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
Dianne Hackborn31b0e0e2012-04-05 19:33:30 -0700139 new String[] { android.Manifest.permission.READ_EXTERNAL_STORAGE },
Dianne Hackborn2bd8d042012-06-11 12:27:05 -0700140 android.os.Build.VERSION_CODES.CUR_DEVELOPMENT+1),
Dianne Hackborn31b0e0e2012-04-05 19:33:30 -0700141 new PackageParser.SplitPermissionInfo(android.Manifest.permission.READ_CONTACTS,
142 new String[] { android.Manifest.permission.READ_CALL_LOG },
143 android.os.Build.VERSION_CODES.JELLY_BEAN),
144 new PackageParser.SplitPermissionInfo(android.Manifest.permission.WRITE_CONTACTS,
145 new String[] { android.Manifest.permission.WRITE_CALL_LOG },
146 android.os.Build.VERSION_CODES.JELLY_BEAN)
Dianne Hackborn79245122012-03-12 10:51:26 -0700147 };
148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 private String mArchiveSourcePath;
150 private String[] mSeparateProcesses;
Dianne Hackbornd2509fd2011-09-12 12:29:43 -0700151 private boolean mOnlyCoreApps;
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -0700152 private static final int SDK_VERSION = Build.VERSION.SDK_INT;
153 private static final String SDK_CODENAME = "REL".equals(Build.VERSION.CODENAME)
154 ? null : Build.VERSION.CODENAME;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155
156 private int mParseError = PackageManager.INSTALL_SUCCEEDED;
157
158 private static final Object mSync = new Object();
159 private static WeakReference<byte[]> mReadBuffer;
160
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -0700161 private static boolean sCompatibilityModeEnabled = true;
162 private static final int PARSE_DEFAULT_INSTALL_LOCATION = PackageInfo.INSTALL_LOCATION_UNSPECIFIED;
Mitsuru Oshima69fff4a2009-07-21 09:51:05 -0700163
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700164 static class ParsePackageItemArgs {
165 final Package owner;
166 final String[] outError;
167 final int nameRes;
168 final int labelRes;
169 final int iconRes;
Adam Powell81cd2e92010-04-21 16:35:18 -0700170 final int logoRes;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700171
172 String tag;
173 TypedArray sa;
174
175 ParsePackageItemArgs(Package _owner, String[] _outError,
Adam Powell81cd2e92010-04-21 16:35:18 -0700176 int _nameRes, int _labelRes, int _iconRes, int _logoRes) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700177 owner = _owner;
178 outError = _outError;
179 nameRes = _nameRes;
180 labelRes = _labelRes;
181 iconRes = _iconRes;
Adam Powell81cd2e92010-04-21 16:35:18 -0700182 logoRes = _logoRes;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700183 }
184 }
185
186 static class ParseComponentArgs extends ParsePackageItemArgs {
187 final String[] sepProcesses;
188 final int processRes;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800189 final int descriptionRes;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700190 final int enabledRes;
191 int flags;
192
193 ParseComponentArgs(Package _owner, String[] _outError,
Adam Powell81cd2e92010-04-21 16:35:18 -0700194 int _nameRes, int _labelRes, int _iconRes, int _logoRes,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800195 String[] _sepProcesses, int _processRes,
196 int _descriptionRes, int _enabledRes) {
Adam Powell81cd2e92010-04-21 16:35:18 -0700197 super(_owner, _outError, _nameRes, _labelRes, _iconRes, _logoRes);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700198 sepProcesses = _sepProcesses;
199 processRes = _processRes;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800200 descriptionRes = _descriptionRes;
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700201 enabledRes = _enabledRes;
202 }
203 }
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800204
205 /* Light weight package info.
206 * @hide
207 */
208 public static class PackageLite {
Kenny Root05ca4c92011-09-15 10:36:25 -0700209 public final String packageName;
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700210 public final int versionCode;
Kenny Root05ca4c92011-09-15 10:36:25 -0700211 public final int installLocation;
212 public final VerifierInfo[] verifiers;
213
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700214 public PackageLite(String packageName, int versionCode,
215 int installLocation, List<VerifierInfo> verifiers) {
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800216 this.packageName = packageName;
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700217 this.versionCode = versionCode;
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800218 this.installLocation = installLocation;
Kenny Root05ca4c92011-09-15 10:36:25 -0700219 this.verifiers = verifiers.toArray(new VerifierInfo[verifiers.size()]);
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800220 }
221 }
222
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700223 private ParsePackageItemArgs mParseInstrumentationArgs;
224 private ParseComponentArgs mParseActivityArgs;
225 private ParseComponentArgs mParseActivityAliasArgs;
226 private ParseComponentArgs mParseServiceArgs;
227 private ParseComponentArgs mParseProviderArgs;
228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 /** If set to true, we will only allow package files that exactly match
230 * the DTD. Otherwise, we try to get as much from the package as we
231 * can without failing. This should normally be set to false, to
232 * support extensions to the DTD in future versions. */
233 private static final boolean RIGID_PARSER = false;
234
235 private static final String TAG = "PackageParser";
236
237 public PackageParser(String archiveSourcePath) {
238 mArchiveSourcePath = archiveSourcePath;
239 }
240
241 public void setSeparateProcesses(String[] procs) {
242 mSeparateProcesses = procs;
243 }
244
Dianne Hackbornd2509fd2011-09-12 12:29:43 -0700245 public void setOnlyCoreApps(boolean onlyCoreApps) {
246 mOnlyCoreApps = onlyCoreApps;
247 }
248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 private static final boolean isPackageFilename(String name) {
250 return name.endsWith(".apk");
251 }
252
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700253 /*
Amith Yamasani13593602012-03-22 16:16:17 -0700254 public static PackageInfo generatePackageInfo(PackageParser.Package p,
255 int gids[], int flags, long firstInstallTime, long lastUpdateTime,
256 HashSet<String> grantedPermissions) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700257 PackageUserState state = new PackageUserState();
Amith Yamasani13593602012-03-22 16:16:17 -0700258 return generatePackageInfo(p, gids, flags, firstInstallTime, lastUpdateTime,
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700259 grantedPermissions, state, UserHandle.getCallingUserId());
Amith Yamasani13593602012-03-22 16:16:17 -0700260 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700261 */
Amith Yamasani13593602012-03-22 16:16:17 -0700262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 /**
264 * Generate and return the {@link PackageInfo} for a parsed package.
265 *
266 * @param p the parsed package.
267 * @param flags indicating which optional information is included.
268 */
269 public static PackageInfo generatePackageInfo(PackageParser.Package p,
Dianne Hackborne639da72012-02-21 15:11:13 -0800270 int gids[], int flags, long firstInstallTime, long lastUpdateTime,
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700271 HashSet<String> grantedPermissions, PackageUserState state) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272
Amith Yamasani483f3b02012-03-13 16:08:00 -0700273 return generatePackageInfo(p, gids, flags, firstInstallTime, lastUpdateTime,
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700274 grantedPermissions, state, UserHandle.getCallingUserId());
275 }
276
277 private static boolean checkUseInstalled(int flags, PackageUserState state) {
278 return state.installed || ((flags & PackageManager.GET_UNINSTALLED_PACKAGES) != 0);
Amith Yamasani483f3b02012-03-13 16:08:00 -0700279 }
280
Amith Yamasani13593602012-03-22 16:16:17 -0700281 public static PackageInfo generatePackageInfo(PackageParser.Package p,
Amith Yamasani483f3b02012-03-13 16:08:00 -0700282 int gids[], int flags, long firstInstallTime, long lastUpdateTime,
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700283 HashSet<String> grantedPermissions, PackageUserState state, int userId) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700284
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700285 if (!checkUseInstalled(flags, state)) {
286 return null;
287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 PackageInfo pi = new PackageInfo();
289 pi.packageName = p.packageName;
290 pi.versionCode = p.mVersionCode;
291 pi.versionName = p.mVersionName;
292 pi.sharedUserId = p.mSharedUserId;
293 pi.sharedUserLabel = p.mSharedUserLabel;
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700294 pi.applicationInfo = generateApplicationInfo(p, flags, state, userId);
Suchi Amalapurapu117818e2010-02-09 03:45:40 -0800295 pi.installLocation = p.installLocation;
Amith Yamasani0d8750d2013-05-01 15:25:28 -0700296 if ((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) != 0
297 || (pi.applicationInfo.flags&ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
298 pi.requiredForAllUsers = p.mRequiredForAllUsers;
299 }
Amith Yamasani0ac1fc92013-03-27 18:56:08 -0700300 pi.restrictedAccountType = p.mRestrictedAccountType;
Amith Yamasaniccbe3892013-04-12 17:52:42 -0700301 pi.requiredAccountType = p.mRequiredAccountType;
Dianne Hackborn78d68832010-10-07 01:12:46 -0700302 pi.firstInstallTime = firstInstallTime;
303 pi.lastUpdateTime = lastUpdateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 if ((flags&PackageManager.GET_GIDS) != 0) {
305 pi.gids = gids;
306 }
307 if ((flags&PackageManager.GET_CONFIGURATIONS) != 0) {
308 int N = p.configPreferences.size();
309 if (N > 0) {
310 pi.configPreferences = new ConfigurationInfo[N];
Dianne Hackborn49237342009-08-27 20:08:01 -0700311 p.configPreferences.toArray(pi.configPreferences);
312 }
313 N = p.reqFeatures != null ? p.reqFeatures.size() : 0;
314 if (N > 0) {
315 pi.reqFeatures = new FeatureInfo[N];
316 p.reqFeatures.toArray(pi.reqFeatures);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 }
318 }
319 if ((flags&PackageManager.GET_ACTIVITIES) != 0) {
320 int N = p.activities.size();
321 if (N > 0) {
Dianne Hackborn7eca6872009-09-28 23:57:05 -0700322 if ((flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
323 pi.activities = new ActivityInfo[N];
324 } else {
325 int num = 0;
326 for (int i=0; i<N; i++) {
327 if (p.activities.get(i).info.enabled) num++;
328 }
329 pi.activities = new ActivityInfo[num];
330 }
Suchi Amalapurapud83006c2009-10-28 23:39:46 -0700331 for (int i=0, j=0; i<N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 final Activity activity = p.activities.get(i);
333 if (activity.info.enabled
334 || (flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
Amith Yamasani742a6712011-05-04 14:49:28 -0700335 pi.activities[j++] = generateActivityInfo(p.activities.get(i), flags,
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700336 state, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 }
338 }
339 }
340 }
341 if ((flags&PackageManager.GET_RECEIVERS) != 0) {
342 int N = p.receivers.size();
343 if (N > 0) {
Dianne Hackborn7eca6872009-09-28 23:57:05 -0700344 if ((flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
345 pi.receivers = new ActivityInfo[N];
346 } else {
347 int num = 0;
348 for (int i=0; i<N; i++) {
349 if (p.receivers.get(i).info.enabled) num++;
350 }
351 pi.receivers = new ActivityInfo[num];
352 }
Suchi Amalapurapud83006c2009-10-28 23:39:46 -0700353 for (int i=0, j=0; i<N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 final Activity activity = p.receivers.get(i);
355 if (activity.info.enabled
356 || (flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
Amith Yamasani13593602012-03-22 16:16:17 -0700357 pi.receivers[j++] = generateActivityInfo(p.receivers.get(i), flags,
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700358 state, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 }
360 }
361 }
362 }
363 if ((flags&PackageManager.GET_SERVICES) != 0) {
364 int N = p.services.size();
365 if (N > 0) {
Dianne Hackborn7eca6872009-09-28 23:57:05 -0700366 if ((flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
367 pi.services = new ServiceInfo[N];
368 } else {
369 int num = 0;
370 for (int i=0; i<N; i++) {
371 if (p.services.get(i).info.enabled) num++;
372 }
373 pi.services = new ServiceInfo[num];
374 }
Suchi Amalapurapud83006c2009-10-28 23:39:46 -0700375 for (int i=0, j=0; i<N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 final Service service = p.services.get(i);
377 if (service.info.enabled
378 || (flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700379 pi.services[j++] = generateServiceInfo(p.services.get(i), flags,
380 state, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 }
382 }
383 }
384 }
385 if ((flags&PackageManager.GET_PROVIDERS) != 0) {
386 int N = p.providers.size();
387 if (N > 0) {
Dianne Hackborn7eca6872009-09-28 23:57:05 -0700388 if ((flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
389 pi.providers = new ProviderInfo[N];
390 } else {
391 int num = 0;
392 for (int i=0; i<N; i++) {
393 if (p.providers.get(i).info.enabled) num++;
394 }
395 pi.providers = new ProviderInfo[num];
396 }
Suchi Amalapurapud83006c2009-10-28 23:39:46 -0700397 for (int i=0, j=0; i<N; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 final Provider provider = p.providers.get(i);
399 if (provider.info.enabled
400 || (flags&PackageManager.GET_DISABLED_COMPONENTS) != 0) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700401 pi.providers[j++] = generateProviderInfo(p.providers.get(i), flags,
402 state, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 }
404 }
405 }
406 }
407 if ((flags&PackageManager.GET_INSTRUMENTATION) != 0) {
408 int N = p.instrumentation.size();
409 if (N > 0) {
410 pi.instrumentation = new InstrumentationInfo[N];
411 for (int i=0; i<N; i++) {
412 pi.instrumentation[i] = generateInstrumentationInfo(
413 p.instrumentation.get(i), flags);
414 }
415 }
416 }
417 if ((flags&PackageManager.GET_PERMISSIONS) != 0) {
418 int N = p.permissions.size();
419 if (N > 0) {
420 pi.permissions = new PermissionInfo[N];
421 for (int i=0; i<N; i++) {
422 pi.permissions[i] = generatePermissionInfo(p.permissions.get(i), flags);
423 }
424 }
425 N = p.requestedPermissions.size();
426 if (N > 0) {
427 pi.requestedPermissions = new String[N];
Dianne Hackborne639da72012-02-21 15:11:13 -0800428 pi.requestedPermissionsFlags = new int[N];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 for (int i=0; i<N; i++) {
Dianne Hackborne639da72012-02-21 15:11:13 -0800430 final String perm = p.requestedPermissions.get(i);
431 pi.requestedPermissions[i] = perm;
432 if (p.requestedPermissionsRequired.get(i)) {
433 pi.requestedPermissionsFlags[i] |= PackageInfo.REQUESTED_PERMISSION_REQUIRED;
434 }
435 if (grantedPermissions != null && grantedPermissions.contains(perm)) {
436 pi.requestedPermissionsFlags[i] |= PackageInfo.REQUESTED_PERMISSION_GRANTED;
437 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 }
439 }
440 }
441 if ((flags&PackageManager.GET_SIGNATURES) != 0) {
Suchi Amalapurapud83006c2009-10-28 23:39:46 -0700442 int N = (p.mSignatures != null) ? p.mSignatures.length : 0;
443 if (N > 0) {
444 pi.signatures = new Signature[N];
445 System.arraycopy(p.mSignatures, 0, pi.signatures, 0, N);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 }
447 }
448 return pi;
449 }
450
451 private Certificate[] loadCertificates(JarFile jarFile, JarEntry je,
452 byte[] readBuffer) {
453 try {
454 // We must read the stream for the JarEntry to retrieve
455 // its certificates.
Kenny Rootd63f7db2010-09-27 08:07:48 -0700456 InputStream is = new BufferedInputStream(jarFile.getInputStream(je));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 while (is.read(readBuffer, 0, readBuffer.length) != -1) {
458 // not using
459 }
460 is.close();
461 return je != null ? je.getCertificates() : null;
462 } catch (IOException e) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700463 Slog.w(TAG, "Exception reading " + je.getName() + " in "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 + jarFile.getName(), e);
Dianne Hackborn6e52b5d2010-04-05 14:33:01 -0700465 } catch (RuntimeException e) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700466 Slog.w(TAG, "Exception reading " + je.getName() + " in "
Dianne Hackborn6e52b5d2010-04-05 14:33:01 -0700467 + jarFile.getName(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 }
469 return null;
470 }
471
Suchi Amalapurapuaf8e9f42010-01-12 10:17:28 -0800472 public final static int PARSE_IS_SYSTEM = 1<<0;
473 public final static int PARSE_CHATTY = 1<<1;
474 public final static int PARSE_MUST_BE_APK = 1<<2;
475 public final static int PARSE_IGNORE_PROCESSES = 1<<3;
476 public final static int PARSE_FORWARD_LOCK = 1<<4;
477 public final static int PARSE_ON_SDCARD = 1<<5;
Dianne Hackborn806da1d2010-03-18 16:50:07 -0700478 public final static int PARSE_IS_SYSTEM_DIR = 1<<6;
Christopher Tateccbf84f2013-05-08 15:25:41 -0700479 public final static int PARSE_IS_PRIVILEGED = 1<<7;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480
481 public int getParseError() {
482 return mParseError;
483 }
484
Suchi Amalapurapuaf8e9f42010-01-12 10:17:28 -0800485 public Package parsePackage(File sourceFile, String destCodePath,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 DisplayMetrics metrics, int flags) {
487 mParseError = PackageManager.INSTALL_SUCCEEDED;
488
489 mArchiveSourcePath = sourceFile.getPath();
490 if (!sourceFile.isFile()) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700491 Slog.w(TAG, "Skipping dir: " + mArchiveSourcePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 mParseError = PackageManager.INSTALL_PARSE_FAILED_NOT_APK;
493 return null;
494 }
495 if (!isPackageFilename(sourceFile.getName())
496 && (flags&PARSE_MUST_BE_APK) != 0) {
497 if ((flags&PARSE_IS_SYSTEM) == 0) {
498 // We expect to have non-.apk files in the system dir,
499 // so don't warn about them.
Kenny Rootd2d29252011-08-08 11:27:57 -0700500 Slog.w(TAG, "Skipping non-package file: " + mArchiveSourcePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 }
502 mParseError = PackageManager.INSTALL_PARSE_FAILED_NOT_APK;
503 return null;
504 }
505
Kenny Rootd2d29252011-08-08 11:27:57 -0700506 if (DEBUG_JAR)
507 Slog.d(TAG, "Scanning package: " + mArchiveSourcePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508
509 XmlResourceParser parser = null;
510 AssetManager assmgr = null;
Dianne Hackborn3b81bc12011-01-15 11:50:52 -0800511 Resources res = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 boolean assetError = true;
513 try {
514 assmgr = new AssetManager();
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700515 int cookie = assmgr.addAssetPath(mArchiveSourcePath);
Dianne Hackborn3b81bc12011-01-15 11:50:52 -0800516 if (cookie != 0) {
517 res = new Resources(assmgr, metrics, null);
Dianne Hackborn69cb8752011-05-19 18:13:32 -0700518 assmgr.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Dianne Hackborn3b81bc12011-01-15 11:50:52 -0800519 Build.VERSION.RESOURCES_SDK_INT);
Kenny Rootbcc954d2011-08-08 16:19:08 -0700520 parser = assmgr.openXmlResourceParser(cookie, ANDROID_MANIFEST_FILENAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 assetError = false;
522 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -0700523 Slog.w(TAG, "Failed adding asset path:"+mArchiveSourcePath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 }
525 } catch (Exception e) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700526 Slog.w(TAG, "Unable to read AndroidManifest.xml of "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 + mArchiveSourcePath, e);
528 }
Dianne Hackborn3b81bc12011-01-15 11:50:52 -0800529 if (assetError) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 if (assmgr != null) assmgr.close();
531 mParseError = PackageManager.INSTALL_PARSE_FAILED_BAD_MANIFEST;
532 return null;
533 }
534 String[] errorText = new String[1];
535 Package pkg = null;
536 Exception errorException = null;
537 try {
538 // XXXX todo: need to figure out correct configuration.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 pkg = parsePackage(res, parser, flags, errorText);
540 } catch (Exception e) {
541 errorException = e;
542 mParseError = PackageManager.INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION;
543 }
544
545
546 if (pkg == null) {
Dianne Hackbornd2509fd2011-09-12 12:29:43 -0700547 // If we are only parsing core apps, then a null with INSTALL_SUCCEEDED
548 // just means to skip this app so don't make a fuss about it.
549 if (!mOnlyCoreApps || mParseError != PackageManager.INSTALL_SUCCEEDED) {
550 if (errorException != null) {
551 Slog.w(TAG, mArchiveSourcePath, errorException);
552 } else {
553 Slog.w(TAG, mArchiveSourcePath + " (at "
554 + parser.getPositionDescription()
555 + "): " + errorText[0]);
556 }
557 if (mParseError == PackageManager.INSTALL_SUCCEEDED) {
558 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
559 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 }
561 parser.close();
562 assmgr.close();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 return null;
564 }
565
566 parser.close();
567 assmgr.close();
568
Suchi Amalapurapuaf8e9f42010-01-12 10:17:28 -0800569 // Set code and resource paths
570 pkg.mPath = destCodePath;
571 pkg.mScanPath = mArchiveSourcePath;
572 //pkg.applicationInfo.sourceDir = destCodePath;
573 //pkg.applicationInfo.publicSourceDir = destRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 pkg.mSignatures = null;
575
576 return pkg;
577 }
578
Kenny Root6c918ce2013-04-02 14:04:24 -0700579 /**
580 * Gathers the {@link ManifestDigest} for {@code pkg} if it exists in the
581 * APK. If it successfully scanned the package and found the
582 * {@code AndroidManifest.xml}, {@code true} is returned.
583 */
584 public boolean collectManifestDigest(Package pkg) {
585 try {
586 final JarFile jarFile = new JarFile(mArchiveSourcePath);
587 try {
588 final ZipEntry je = jarFile.getEntry(ANDROID_MANIFEST_FILENAME);
589 if (je != null) {
590 pkg.manifestDigest = ManifestDigest.fromInputStream(jarFile.getInputStream(je));
591 }
592 } finally {
593 jarFile.close();
594 }
595 return true;
596 } catch (IOException e) {
597 return false;
598 }
599 }
600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 public boolean collectCertificates(Package pkg, int flags) {
602 pkg.mSignatures = null;
603
604 WeakReference<byte[]> readBufferRef;
605 byte[] readBuffer = null;
606 synchronized (mSync) {
607 readBufferRef = mReadBuffer;
608 if (readBufferRef != null) {
609 mReadBuffer = null;
610 readBuffer = readBufferRef.get();
611 }
612 if (readBuffer == null) {
613 readBuffer = new byte[8192];
614 readBufferRef = new WeakReference<byte[]>(readBuffer);
615 }
616 }
617
618 try {
619 JarFile jarFile = new JarFile(mArchiveSourcePath);
620
621 Certificate[] certs = null;
622
623 if ((flags&PARSE_IS_SYSTEM) != 0) {
624 // If this package comes from the system image, then we
625 // can trust it... we'll just use the AndroidManifest.xml
626 // to retrieve its signatures, not validating all of the
627 // files.
Kenny Rootbcc954d2011-08-08 16:19:08 -0700628 JarEntry jarEntry = jarFile.getJarEntry(ANDROID_MANIFEST_FILENAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 certs = loadCertificates(jarFile, jarEntry, readBuffer);
630 if (certs == null) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700631 Slog.e(TAG, "Package " + pkg.packageName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 + " has no certificates at entry "
633 + jarEntry.getName() + "; ignoring!");
634 jarFile.close();
635 mParseError = PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
636 return false;
637 }
Kenny Rootd2d29252011-08-08 11:27:57 -0700638 if (DEBUG_JAR) {
639 Slog.i(TAG, "File " + mArchiveSourcePath + ": entry=" + jarEntry
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 + " certs=" + (certs != null ? certs.length : 0));
641 if (certs != null) {
642 final int N = certs.length;
643 for (int i=0; i<N; i++) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700644 Slog.i(TAG, " Public key: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 + certs[i].getPublicKey().getEncoded()
646 + " " + certs[i].getPublicKey());
647 }
648 }
649 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -0700651 Enumeration<JarEntry> entries = jarFile.entries();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 while (entries.hasMoreElements()) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700653 final JarEntry je = entries.nextElement();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 if (je.isDirectory()) continue;
Kenny Rootd2d29252011-08-08 11:27:57 -0700655
Kenny Rootbcc954d2011-08-08 16:19:08 -0700656 final String name = je.getName();
657
658 if (name.startsWith("META-INF/"))
659 continue;
660
661 if (ANDROID_MANIFEST_FILENAME.equals(name)) {
Kenny Root6c918ce2013-04-02 14:04:24 -0700662 pkg.manifestDigest =
663 ManifestDigest.fromInputStream(jarFile.getInputStream(je));
Kenny Rootbcc954d2011-08-08 16:19:08 -0700664 }
665
666 final Certificate[] localCerts = loadCertificates(jarFile, je, readBuffer);
Kenny Rootd2d29252011-08-08 11:27:57 -0700667 if (DEBUG_JAR) {
668 Slog.i(TAG, "File " + mArchiveSourcePath + " entry " + je.getName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 + ": certs=" + certs + " ("
670 + (certs != null ? certs.length : 0) + ")");
671 }
Kenny Rootbcc954d2011-08-08 16:19:08 -0700672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 if (localCerts == null) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700674 Slog.e(TAG, "Package " + pkg.packageName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 + " has no certificates at entry "
676 + je.getName() + "; ignoring!");
677 jarFile.close();
678 mParseError = PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
679 return false;
680 } else if (certs == null) {
681 certs = localCerts;
682 } else {
683 // Ensure all certificates match.
684 for (int i=0; i<certs.length; i++) {
685 boolean found = false;
686 for (int j=0; j<localCerts.length; j++) {
687 if (certs[i] != null &&
688 certs[i].equals(localCerts[j])) {
689 found = true;
690 break;
691 }
692 }
693 if (!found || certs.length != localCerts.length) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700694 Slog.e(TAG, "Package " + pkg.packageName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 + " has mismatched certificates at entry "
696 + je.getName() + "; ignoring!");
697 jarFile.close();
698 mParseError = PackageManager.INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES;
699 return false;
700 }
701 }
702 }
703 }
704 }
705 jarFile.close();
706
707 synchronized (mSync) {
708 mReadBuffer = readBufferRef;
709 }
710
711 if (certs != null && certs.length > 0) {
712 final int N = certs.length;
713 pkg.mSignatures = new Signature[certs.length];
714 for (int i=0; i<N; i++) {
715 pkg.mSignatures[i] = new Signature(
716 certs[i].getEncoded());
717 }
718 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -0700719 Slog.e(TAG, "Package " + pkg.packageName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 + " has no certificates; ignoring!");
721 mParseError = PackageManager.INSTALL_PARSE_FAILED_NO_CERTIFICATES;
722 return false;
723 }
Geremy Condraf1bcca82013-01-07 22:35:24 -0800724
725 // Add the signing KeySet to the system
726 pkg.mSigningKeys = new HashSet<PublicKey>();
727 for (int i=0; i < certs.length; i++) {
728 pkg.mSigningKeys.add(certs[i].getPublicKey());
729 }
730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 } catch (CertificateEncodingException e) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700732 Slog.w(TAG, "Exception reading " + mArchiveSourcePath, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 mParseError = PackageManager.INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING;
734 return false;
735 } catch (IOException e) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700736 Slog.w(TAG, "Exception reading " + mArchiveSourcePath, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 mParseError = PackageManager.INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING;
738 return false;
739 } catch (RuntimeException e) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700740 Slog.w(TAG, "Exception reading " + mArchiveSourcePath, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 mParseError = PackageManager.INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION;
742 return false;
743 }
744
745 return true;
746 }
747
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800748 /*
749 * Utility method that retrieves just the package name and install
750 * location from the apk location at the given file path.
751 * @param packageFilePath file location of the apk
752 * @param flags Special parse flags
Kenny Root930d3af2010-07-30 16:52:29 -0700753 * @return PackageLite object with package information or null on failure.
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800754 */
755 public static PackageLite parsePackageLite(String packageFilePath, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 AssetManager assmgr = null;
Kenny Root05ca4c92011-09-15 10:36:25 -0700757 final XmlResourceParser parser;
758 final Resources res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 try {
760 assmgr = new AssetManager();
Dianne Hackborn69cb8752011-05-19 18:13:32 -0700761 assmgr.setConfiguration(0, 0, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Dianne Hackborn3b81bc12011-01-15 11:50:52 -0800762 Build.VERSION.RESOURCES_SDK_INT);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 int cookie = assmgr.addAssetPath(packageFilePath);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700765 if (cookie == 0) {
766 return null;
767 }
768
Kenny Root05ca4c92011-09-15 10:36:25 -0700769 final DisplayMetrics metrics = new DisplayMetrics();
770 metrics.setToDefaults();
771 res = new Resources(assmgr, metrics, null);
Kenny Rootbcc954d2011-08-08 16:19:08 -0700772 parser = assmgr.openXmlResourceParser(cookie, ANDROID_MANIFEST_FILENAME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 } catch (Exception e) {
774 if (assmgr != null) assmgr.close();
Kenny Rootd2d29252011-08-08 11:27:57 -0700775 Slog.w(TAG, "Unable to read AndroidManifest.xml of "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 + packageFilePath, e);
777 return null;
778 }
Kenny Root05ca4c92011-09-15 10:36:25 -0700779
780 final AttributeSet attrs = parser;
781 final String errors[] = new String[1];
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800782 PackageLite packageLite = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 try {
Kenny Root05ca4c92011-09-15 10:36:25 -0700784 packageLite = parsePackageLite(res, parser, attrs, flags, errors);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 } catch (IOException e) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700786 Slog.w(TAG, packageFilePath, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 } catch (XmlPullParserException e) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700788 Slog.w(TAG, packageFilePath, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 } finally {
790 if (parser != null) parser.close();
791 if (assmgr != null) assmgr.close();
792 }
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800793 if (packageLite == null) {
Kenny Rootd2d29252011-08-08 11:27:57 -0700794 Slog.e(TAG, "parsePackageLite error: " + errors[0]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 return null;
796 }
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800797 return packageLite;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 }
799
800 private static String validateName(String name, boolean requiresSeparator) {
801 final int N = name.length();
802 boolean hasSep = false;
803 boolean front = true;
804 for (int i=0; i<N; i++) {
805 final char c = name.charAt(i);
806 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
807 front = false;
808 continue;
809 }
810 if (!front) {
811 if ((c >= '0' && c <= '9') || c == '_') {
812 continue;
813 }
814 }
815 if (c == '.') {
816 hasSep = true;
817 front = true;
818 continue;
819 }
820 return "bad character '" + c + "'";
821 }
822 return hasSep || !requiresSeparator
823 ? null : "must have at least one '.' separator";
824 }
825
826 private static String parsePackageName(XmlPullParser parser,
827 AttributeSet attrs, int flags, String[] outError)
828 throws IOException, XmlPullParserException {
829
830 int type;
Kenny Rootd2d29252011-08-08 11:27:57 -0700831 while ((type = parser.next()) != XmlPullParser.START_TAG
832 && type != XmlPullParser.END_DOCUMENT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 ;
834 }
835
Kenny Rootd2d29252011-08-08 11:27:57 -0700836 if (type != XmlPullParser.START_TAG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 outError[0] = "No start tag found";
838 return null;
839 }
Kenny Rootd2d29252011-08-08 11:27:57 -0700840 if (DEBUG_PARSER)
841 Slog.v(TAG, "Root element name: '" + parser.getName() + "'");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 if (!parser.getName().equals("manifest")) {
843 outError[0] = "No <manifest> tag";
844 return null;
845 }
846 String pkgName = attrs.getAttributeValue(null, "package");
847 if (pkgName == null || pkgName.length() == 0) {
848 outError[0] = "<manifest> does not specify package";
849 return null;
850 }
851 String nameError = validateName(pkgName, true);
852 if (nameError != null && !"android".equals(pkgName)) {
853 outError[0] = "<manifest> specifies bad package name \""
854 + pkgName + "\": " + nameError;
855 return null;
856 }
857
858 return pkgName.intern();
859 }
860
Kenny Root05ca4c92011-09-15 10:36:25 -0700861 private static PackageLite parsePackageLite(Resources res, XmlPullParser parser,
862 AttributeSet attrs, int flags, String[] outError) throws IOException,
863 XmlPullParserException {
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800864
865 int type;
Kenny Rootd2d29252011-08-08 11:27:57 -0700866 while ((type = parser.next()) != XmlPullParser.START_TAG
867 && type != XmlPullParser.END_DOCUMENT) {
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800868 ;
869 }
870
Kenny Rootd2d29252011-08-08 11:27:57 -0700871 if (type != XmlPullParser.START_TAG) {
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800872 outError[0] = "No start tag found";
873 return null;
874 }
Kenny Rootd2d29252011-08-08 11:27:57 -0700875 if (DEBUG_PARSER)
876 Slog.v(TAG, "Root element name: '" + parser.getName() + "'");
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800877 if (!parser.getName().equals("manifest")) {
878 outError[0] = "No <manifest> tag";
879 return null;
880 }
881 String pkgName = attrs.getAttributeValue(null, "package");
882 if (pkgName == null || pkgName.length() == 0) {
883 outError[0] = "<manifest> does not specify package";
884 return null;
885 }
886 String nameError = validateName(pkgName, true);
887 if (nameError != null && !"android".equals(pkgName)) {
888 outError[0] = "<manifest> specifies bad package name \""
889 + pkgName + "\": " + nameError;
890 return null;
891 }
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -0700892 int installLocation = PARSE_DEFAULT_INSTALL_LOCATION;
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700893 int versionCode = 0;
894 int numFound = 0;
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800895 for (int i = 0; i < attrs.getAttributeCount(); i++) {
896 String attr = attrs.getAttributeName(i);
897 if (attr.equals("installLocation")) {
898 installLocation = attrs.getAttributeIntValue(i,
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -0700899 PARSE_DEFAULT_INSTALL_LOCATION);
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700900 numFound++;
901 } else if (attr.equals("versionCode")) {
902 versionCode = attrs.getAttributeIntValue(i, 0);
903 numFound++;
904 }
905 if (numFound >= 2) {
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800906 break;
907 }
908 }
Kenny Root05ca4c92011-09-15 10:36:25 -0700909
910 // Only search the tree when the tag is directly below <manifest>
911 final int searchDepth = parser.getDepth() + 1;
912
913 final List<VerifierInfo> verifiers = new ArrayList<VerifierInfo>();
914 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
915 && (type != XmlPullParser.END_TAG || parser.getDepth() >= searchDepth)) {
916 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
917 continue;
918 }
919
920 if (parser.getDepth() == searchDepth && "package-verifier".equals(parser.getName())) {
921 final VerifierInfo verifier = parseVerifier(res, parser, attrs, flags, outError);
922 if (verifier != null) {
923 verifiers.add(verifier);
924 }
925 }
926 }
927
Dianne Hackborn7767eac2012-08-23 18:25:40 -0700928 return new PackageLite(pkgName.intern(), versionCode, installLocation, verifiers);
Suchi Amalapurapua2b6c372010-03-05 17:40:11 -0800929 }
930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 /**
932 * Temporary.
933 */
934 static public Signature stringToSignature(String str) {
935 final int N = str.length();
936 byte[] sig = new byte[N];
937 for (int i=0; i<N; i++) {
938 sig[i] = (byte)str.charAt(i);
939 }
940 return new Signature(sig);
941 }
942
943 private Package parsePackage(
944 Resources res, XmlResourceParser parser, int flags, String[] outError)
945 throws XmlPullParserException, IOException {
946 AttributeSet attrs = parser;
947
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700948 mParseInstrumentationArgs = null;
949 mParseActivityArgs = null;
950 mParseServiceArgs = null;
951 mParseProviderArgs = null;
952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 String pkgName = parsePackageName(parser, attrs, flags, outError);
954 if (pkgName == null) {
955 mParseError = PackageManager.INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME;
956 return null;
957 }
958 int type;
959
Dianne Hackbornd2509fd2011-09-12 12:29:43 -0700960 if (mOnlyCoreApps) {
961 boolean core = attrs.getAttributeBooleanValue(null, "coreApp", false);
962 if (!core) {
963 mParseError = PackageManager.INSTALL_SUCCEEDED;
964 return null;
965 }
966 }
967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 final Package pkg = new Package(pkgName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 boolean foundApp = false;
Dianne Hackborn851a5412009-05-08 12:06:44 -0700970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 TypedArray sa = res.obtainAttributes(attrs,
972 com.android.internal.R.styleable.AndroidManifest);
973 pkg.mVersionCode = sa.getInteger(
974 com.android.internal.R.styleable.AndroidManifest_versionCode, 0);
Dianne Hackborncf244ad2010-03-09 15:00:30 -0800975 pkg.mVersionName = sa.getNonConfigurationString(
976 com.android.internal.R.styleable.AndroidManifest_versionName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 if (pkg.mVersionName != null) {
978 pkg.mVersionName = pkg.mVersionName.intern();
979 }
Dianne Hackborncf244ad2010-03-09 15:00:30 -0800980 String str = sa.getNonConfigurationString(
981 com.android.internal.R.styleable.AndroidManifest_sharedUserId, 0);
982 if (str != null && str.length() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 String nameError = validateName(str, true);
984 if (nameError != null && !"android".equals(pkgName)) {
985 outError[0] = "<manifest> specifies bad sharedUserId name \""
986 + str + "\": " + nameError;
987 mParseError = PackageManager.INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID;
988 return null;
989 }
990 pkg.mSharedUserId = str.intern();
991 pkg.mSharedUserLabel = sa.getResourceId(
992 com.android.internal.R.styleable.AndroidManifest_sharedUserLabel, 0);
993 }
994 sa.recycle();
Suchi Amalapurapuaaec7792010-02-25 11:49:43 -0800995
Suchi Amalapurapu117818e2010-02-09 03:45:40 -0800996 pkg.installLocation = sa.getInteger(
997 com.android.internal.R.styleable.AndroidManifest_installLocation,
Suchi Amalapurapu90d8ee62010-03-18 11:38:35 -0700998 PARSE_DEFAULT_INSTALL_LOCATION);
Dianne Hackborn54e570f2010-10-04 18:32:32 -0700999 pkg.applicationInfo.installLocation = pkg.installLocation;
Kenny Root7cb9be22012-05-30 15:30:37 -07001000
1001 /* Set the global "forward lock" flag */
1002 if ((flags & PARSE_FORWARD_LOCK) != 0) {
1003 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_FORWARD_LOCK;
1004 }
1005
1006 /* Set the global "on SD card" flag */
1007 if ((flags & PARSE_ON_SDCARD) != 0) {
1008 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_EXTERNAL_STORAGE;
1009 }
1010
Dianne Hackborn723738c2009-06-25 19:48:04 -07001011 // Resource boolean are -1, so 1 means we don't know the value.
1012 int supportsSmallScreens = 1;
1013 int supportsNormalScreens = 1;
1014 int supportsLargeScreens = 1;
Dianne Hackborn14cee9f2010-04-23 17:51:26 -07001015 int supportsXLargeScreens = 1;
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001016 int resizeable = 1;
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001017 int anyDensity = 1;
Dianne Hackborn723738c2009-06-25 19:48:04 -07001018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 int outerDepth = parser.getDepth();
Kenny Rootd2d29252011-08-08 11:27:57 -07001020 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1021 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1022 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 continue;
1024 }
1025
1026 String tagName = parser.getName();
1027 if (tagName.equals("application")) {
1028 if (foundApp) {
1029 if (RIGID_PARSER) {
1030 outError[0] = "<manifest> has more than one <application>";
1031 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1032 return null;
1033 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -07001034 Slog.w(TAG, "<manifest> has more than one <application>");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 XmlUtils.skipCurrentTag(parser);
1036 continue;
1037 }
1038 }
1039
1040 foundApp = true;
1041 if (!parseApplication(pkg, res, parser, attrs, flags, outError)) {
1042 return null;
1043 }
Geremy Condraf1bcca82013-01-07 22:35:24 -08001044 } else if (tagName.equals("keys")) {
1045 if (!parseKeys(pkg, res, parser, attrs, outError)) {
1046 return null;
1047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 } else if (tagName.equals("permission-group")) {
Dianne Hackbornfd5015b2012-04-30 16:33:56 -07001049 if (parsePermissionGroup(pkg, flags, res, parser, attrs, outError) == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 return null;
1051 }
1052 } else if (tagName.equals("permission")) {
1053 if (parsePermission(pkg, res, parser, attrs, outError) == null) {
1054 return null;
1055 }
1056 } else if (tagName.equals("permission-tree")) {
1057 if (parsePermissionTree(pkg, res, parser, attrs, outError) == null) {
1058 return null;
1059 }
1060 } else if (tagName.equals("uses-permission")) {
Nick Kralevich73f2d3c2013-04-04 14:38:13 -07001061 if (!parseUsesPermission(pkg, res, parser, attrs, outError)) {
1062 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 }
1064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 } else if (tagName.equals("uses-configuration")) {
1066 ConfigurationInfo cPref = new ConfigurationInfo();
1067 sa = res.obtainAttributes(attrs,
1068 com.android.internal.R.styleable.AndroidManifestUsesConfiguration);
1069 cPref.reqTouchScreen = sa.getInt(
1070 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqTouchScreen,
1071 Configuration.TOUCHSCREEN_UNDEFINED);
1072 cPref.reqKeyboardType = sa.getInt(
1073 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqKeyboardType,
1074 Configuration.KEYBOARD_UNDEFINED);
1075 if (sa.getBoolean(
1076 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqHardKeyboard,
1077 false)) {
1078 cPref.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD;
1079 }
1080 cPref.reqNavigation = sa.getInt(
1081 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqNavigation,
1082 Configuration.NAVIGATION_UNDEFINED);
1083 if (sa.getBoolean(
1084 com.android.internal.R.styleable.AndroidManifestUsesConfiguration_reqFiveWayNav,
1085 false)) {
1086 cPref.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV;
1087 }
1088 sa.recycle();
1089 pkg.configPreferences.add(cPref);
1090
1091 XmlUtils.skipCurrentTag(parser);
1092
Suchi Amalapurapud299b812009-06-05 10:26:19 -07001093 } else if (tagName.equals("uses-feature")) {
Dianne Hackborn49237342009-08-27 20:08:01 -07001094 FeatureInfo fi = new FeatureInfo();
Suchi Amalapurapud299b812009-06-05 10:26:19 -07001095 sa = res.obtainAttributes(attrs,
1096 com.android.internal.R.styleable.AndroidManifestUsesFeature);
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001097 // Note: don't allow this value to be a reference to a resource
1098 // that may change.
Dianne Hackborn49237342009-08-27 20:08:01 -07001099 fi.name = sa.getNonResourceString(
1100 com.android.internal.R.styleable.AndroidManifestUsesFeature_name);
1101 if (fi.name == null) {
1102 fi.reqGlEsVersion = sa.getInt(
1103 com.android.internal.R.styleable.AndroidManifestUsesFeature_glEsVersion,
1104 FeatureInfo.GL_ES_VERSION_UNDEFINED);
1105 }
1106 if (sa.getBoolean(
1107 com.android.internal.R.styleable.AndroidManifestUsesFeature_required,
1108 true)) {
1109 fi.flags |= FeatureInfo.FLAG_REQUIRED;
1110 }
Suchi Amalapurapud299b812009-06-05 10:26:19 -07001111 sa.recycle();
Dianne Hackborn49237342009-08-27 20:08:01 -07001112 if (pkg.reqFeatures == null) {
1113 pkg.reqFeatures = new ArrayList<FeatureInfo>();
1114 }
1115 pkg.reqFeatures.add(fi);
1116
1117 if (fi.name == null) {
1118 ConfigurationInfo cPref = new ConfigurationInfo();
1119 cPref.reqGlEsVersion = fi.reqGlEsVersion;
1120 pkg.configPreferences.add(cPref);
1121 }
Suchi Amalapurapud299b812009-06-05 10:26:19 -07001122
1123 XmlUtils.skipCurrentTag(parser);
1124
Dianne Hackborn851a5412009-05-08 12:06:44 -07001125 } else if (tagName.equals("uses-sdk")) {
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001126 if (SDK_VERSION > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 sa = res.obtainAttributes(attrs,
1128 com.android.internal.R.styleable.AndroidManifestUsesSdk);
1129
Dianne Hackborn851a5412009-05-08 12:06:44 -07001130 int minVers = 0;
1131 String minCode = null;
1132 int targetVers = 0;
1133 String targetCode = null;
1134
1135 TypedValue val = sa.peekValue(
1136 com.android.internal.R.styleable.AndroidManifestUsesSdk_minSdkVersion);
1137 if (val != null) {
1138 if (val.type == TypedValue.TYPE_STRING && val.string != null) {
1139 targetCode = minCode = val.string.toString();
1140 } else {
1141 // If it's not a string, it's an integer.
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001142 targetVers = minVers = val.data;
Dianne Hackborn851a5412009-05-08 12:06:44 -07001143 }
1144 }
1145
1146 val = sa.peekValue(
1147 com.android.internal.R.styleable.AndroidManifestUsesSdk_targetSdkVersion);
1148 if (val != null) {
1149 if (val.type == TypedValue.TYPE_STRING && val.string != null) {
1150 targetCode = minCode = val.string.toString();
1151 } else {
1152 // If it's not a string, it's an integer.
1153 targetVers = val.data;
1154 }
1155 }
1156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 sa.recycle();
1158
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001159 if (minCode != null) {
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001160 if (!minCode.equals(SDK_CODENAME)) {
1161 if (SDK_CODENAME != null) {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001162 outError[0] = "Requires development platform " + minCode
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001163 + " (current platform is " + SDK_CODENAME + ")";
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001164 } else {
1165 outError[0] = "Requires development platform " + minCode
1166 + " but this is a release platform.";
1167 }
1168 mParseError = PackageManager.INSTALL_FAILED_OLDER_SDK;
1169 return null;
1170 }
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001171 } else if (minVers > SDK_VERSION) {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001172 outError[0] = "Requires newer sdk version #" + minVers
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001173 + " (current version is #" + SDK_VERSION + ")";
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001174 mParseError = PackageManager.INSTALL_FAILED_OLDER_SDK;
1175 return null;
1176 }
1177
Dianne Hackborn851a5412009-05-08 12:06:44 -07001178 if (targetCode != null) {
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001179 if (!targetCode.equals(SDK_CODENAME)) {
1180 if (SDK_CODENAME != null) {
Dianne Hackborn851a5412009-05-08 12:06:44 -07001181 outError[0] = "Requires development platform " + targetCode
Suchi Amalapurapu8d5ae982009-10-06 09:26:09 -07001182 + " (current platform is " + SDK_CODENAME + ")";
Dianne Hackborn851a5412009-05-08 12:06:44 -07001183 } else {
1184 outError[0] = "Requires development platform " + targetCode
1185 + " but this is a release platform.";
1186 }
1187 mParseError = PackageManager.INSTALL_FAILED_OLDER_SDK;
1188 return null;
1189 }
1190 // If the code matches, it definitely targets this SDK.
Dianne Hackborna96cbb42009-05-13 15:06:13 -07001191 pkg.applicationInfo.targetSdkVersion
1192 = android.os.Build.VERSION_CODES.CUR_DEVELOPMENT;
1193 } else {
1194 pkg.applicationInfo.targetSdkVersion = targetVers;
Dianne Hackborn851a5412009-05-08 12:06:44 -07001195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 }
1197
1198 XmlUtils.skipCurrentTag(parser);
1199
Dianne Hackborn723738c2009-06-25 19:48:04 -07001200 } else if (tagName.equals("supports-screens")) {
1201 sa = res.obtainAttributes(attrs,
1202 com.android.internal.R.styleable.AndroidManifestSupportsScreens);
1203
Dianne Hackborndf6e9802011-05-26 14:20:23 -07001204 pkg.applicationInfo.requiresSmallestWidthDp = sa.getInteger(
1205 com.android.internal.R.styleable.AndroidManifestSupportsScreens_requiresSmallestWidthDp,
1206 0);
1207 pkg.applicationInfo.compatibleWidthLimitDp = sa.getInteger(
1208 com.android.internal.R.styleable.AndroidManifestSupportsScreens_compatibleWidthLimitDp,
1209 0);
Dianne Hackborn2762ff32011-06-01 21:27:05 -07001210 pkg.applicationInfo.largestWidthLimitDp = sa.getInteger(
1211 com.android.internal.R.styleable.AndroidManifestSupportsScreens_largestWidthLimitDp,
1212 0);
Dianne Hackborndf6e9802011-05-26 14:20:23 -07001213
Dianne Hackborn723738c2009-06-25 19:48:04 -07001214 // This is a trick to get a boolean and still able to detect
1215 // if a value was actually set.
1216 supportsSmallScreens = sa.getInteger(
1217 com.android.internal.R.styleable.AndroidManifestSupportsScreens_smallScreens,
1218 supportsSmallScreens);
1219 supportsNormalScreens = sa.getInteger(
1220 com.android.internal.R.styleable.AndroidManifestSupportsScreens_normalScreens,
1221 supportsNormalScreens);
1222 supportsLargeScreens = sa.getInteger(
1223 com.android.internal.R.styleable.AndroidManifestSupportsScreens_largeScreens,
1224 supportsLargeScreens);
Dianne Hackborn14cee9f2010-04-23 17:51:26 -07001225 supportsXLargeScreens = sa.getInteger(
1226 com.android.internal.R.styleable.AndroidManifestSupportsScreens_xlargeScreens,
1227 supportsXLargeScreens);
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001228 resizeable = sa.getInteger(
1229 com.android.internal.R.styleable.AndroidManifestSupportsScreens_resizeable,
Dianne Hackborn14cee9f2010-04-23 17:51:26 -07001230 resizeable);
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001231 anyDensity = sa.getInteger(
1232 com.android.internal.R.styleable.AndroidManifestSupportsScreens_anyDensity,
1233 anyDensity);
Dianne Hackborn723738c2009-06-25 19:48:04 -07001234
1235 sa.recycle();
1236
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001237 XmlUtils.skipCurrentTag(parser);
Dianne Hackborn854060a2009-07-09 18:14:31 -07001238
1239 } else if (tagName.equals("protected-broadcast")) {
1240 sa = res.obtainAttributes(attrs,
1241 com.android.internal.R.styleable.AndroidManifestProtectedBroadcast);
1242
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001243 // Note: don't allow this value to be a reference to a resource
1244 // that may change.
Dianne Hackborn854060a2009-07-09 18:14:31 -07001245 String name = sa.getNonResourceString(
1246 com.android.internal.R.styleable.AndroidManifestProtectedBroadcast_name);
1247
1248 sa.recycle();
1249
1250 if (name != null && (flags&PARSE_IS_SYSTEM) != 0) {
1251 if (pkg.protectedBroadcasts == null) {
1252 pkg.protectedBroadcasts = new ArrayList<String>();
1253 }
1254 if (!pkg.protectedBroadcasts.contains(name)) {
1255 pkg.protectedBroadcasts.add(name.intern());
1256 }
1257 }
1258
1259 XmlUtils.skipCurrentTag(parser);
1260
1261 } else if (tagName.equals("instrumentation")) {
1262 if (parseInstrumentation(pkg, res, parser, attrs, outError) == null) {
1263 return null;
1264 }
1265
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001266 } else if (tagName.equals("original-package")) {
1267 sa = res.obtainAttributes(attrs,
1268 com.android.internal.R.styleable.AndroidManifestOriginalPackage);
1269
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001270 String orig =sa.getNonConfigurationString(
1271 com.android.internal.R.styleable.AndroidManifestOriginalPackage_name, 0);
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08001272 if (!pkg.packageName.equals(orig)) {
Dianne Hackbornc1552392010-03-03 16:19:01 -08001273 if (pkg.mOriginalPackages == null) {
1274 pkg.mOriginalPackages = new ArrayList<String>();
1275 pkg.mRealPackage = pkg.packageName;
1276 }
1277 pkg.mOriginalPackages.add(orig);
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08001278 }
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001279
1280 sa.recycle();
1281
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001282 XmlUtils.skipCurrentTag(parser);
1283
1284 } else if (tagName.equals("adopt-permissions")) {
1285 sa = res.obtainAttributes(attrs,
1286 com.android.internal.R.styleable.AndroidManifestOriginalPackage);
1287
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001288 String name = sa.getNonConfigurationString(
1289 com.android.internal.R.styleable.AndroidManifestOriginalPackage_name, 0);
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001290
1291 sa.recycle();
1292
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08001293 if (name != null) {
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08001294 if (pkg.mAdoptPermissions == null) {
1295 pkg.mAdoptPermissions = new ArrayList<String>();
1296 }
1297 pkg.mAdoptPermissions.add(name);
1298 }
1299
1300 XmlUtils.skipCurrentTag(parser);
1301
Dianne Hackborna0b46c92010-10-21 15:32:06 -07001302 } else if (tagName.equals("uses-gl-texture")) {
1303 // Just skip this tag
1304 XmlUtils.skipCurrentTag(parser);
1305 continue;
1306
1307 } else if (tagName.equals("compatible-screens")) {
1308 // Just skip this tag
1309 XmlUtils.skipCurrentTag(parser);
1310 continue;
1311
Dianne Hackborn854060a2009-07-09 18:14:31 -07001312 } else if (tagName.equals("eat-comment")) {
1313 // Just skip this tag
1314 XmlUtils.skipCurrentTag(parser);
1315 continue;
1316
1317 } else if (RIGID_PARSER) {
1318 outError[0] = "Bad element under <manifest>: "
1319 + parser.getName();
1320 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1321 return null;
1322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -07001324 Slog.w(TAG, "Unknown element under <manifest>: " + parser.getName()
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -07001325 + " at " + mArchiveSourcePath + " "
1326 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 XmlUtils.skipCurrentTag(parser);
1328 continue;
1329 }
1330 }
1331
1332 if (!foundApp && pkg.instrumentation.size() == 0) {
1333 outError[0] = "<manifest> does not contain an <application> or <instrumentation>";
1334 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_EMPTY;
1335 }
1336
Dianne Hackborna96cbb42009-05-13 15:06:13 -07001337 final int NP = PackageParser.NEW_PERMISSIONS.length;
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001338 StringBuilder implicitPerms = null;
Dianne Hackborna96cbb42009-05-13 15:06:13 -07001339 for (int ip=0; ip<NP; ip++) {
1340 final PackageParser.NewPermissionInfo npi
1341 = PackageParser.NEW_PERMISSIONS[ip];
1342 if (pkg.applicationInfo.targetSdkVersion >= npi.sdkVersion) {
1343 break;
1344 }
1345 if (!pkg.requestedPermissions.contains(npi.name)) {
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001346 if (implicitPerms == null) {
1347 implicitPerms = new StringBuilder(128);
1348 implicitPerms.append(pkg.packageName);
1349 implicitPerms.append(": compat added ");
1350 } else {
1351 implicitPerms.append(' ');
1352 }
1353 implicitPerms.append(npi.name);
Dianne Hackborna96cbb42009-05-13 15:06:13 -07001354 pkg.requestedPermissions.add(npi.name);
Dianne Hackborn65696252012-03-05 18:49:21 -08001355 pkg.requestedPermissionsRequired.add(Boolean.TRUE);
Dianne Hackborna96cbb42009-05-13 15:06:13 -07001356 }
Dianne Hackborn851a5412009-05-08 12:06:44 -07001357 }
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001358 if (implicitPerms != null) {
Kenny Rootd2d29252011-08-08 11:27:57 -07001359 Slog.i(TAG, implicitPerms.toString());
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001360 }
Dianne Hackborn79245122012-03-12 10:51:26 -07001361
1362 final int NS = PackageParser.SPLIT_PERMISSIONS.length;
1363 for (int is=0; is<NS; is++) {
1364 final PackageParser.SplitPermissionInfo spi
1365 = PackageParser.SPLIT_PERMISSIONS[is];
Dianne Hackborn31b0e0e2012-04-05 19:33:30 -07001366 if (pkg.applicationInfo.targetSdkVersion >= spi.targetSdk
1367 || !pkg.requestedPermissions.contains(spi.rootPerm)) {
Dianne Hackborn5e4705a2012-04-06 12:55:53 -07001368 continue;
Dianne Hackborn79245122012-03-12 10:51:26 -07001369 }
1370 for (int in=0; in<spi.newPerms.length; in++) {
1371 final String perm = spi.newPerms[in];
1372 if (!pkg.requestedPermissions.contains(perm)) {
1373 pkg.requestedPermissions.add(perm);
1374 pkg.requestedPermissionsRequired.add(Boolean.TRUE);
1375 }
1376 }
1377 }
1378
Dianne Hackborn723738c2009-06-25 19:48:04 -07001379 if (supportsSmallScreens < 0 || (supportsSmallScreens > 0
1380 && pkg.applicationInfo.targetSdkVersion
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001381 >= android.os.Build.VERSION_CODES.DONUT)) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07001382 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_SMALL_SCREENS;
1383 }
1384 if (supportsNormalScreens != 0) {
1385 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_NORMAL_SCREENS;
1386 }
1387 if (supportsLargeScreens < 0 || (supportsLargeScreens > 0
1388 && pkg.applicationInfo.targetSdkVersion
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001389 >= android.os.Build.VERSION_CODES.DONUT)) {
Dianne Hackborn723738c2009-06-25 19:48:04 -07001390 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_LARGE_SCREENS;
1391 }
Dianne Hackborn14cee9f2010-04-23 17:51:26 -07001392 if (supportsXLargeScreens < 0 || (supportsXLargeScreens > 0
1393 && pkg.applicationInfo.targetSdkVersion
1394 >= android.os.Build.VERSION_CODES.GINGERBREAD)) {
1395 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_XLARGE_SCREENS;
1396 }
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001397 if (resizeable < 0 || (resizeable > 0
1398 && pkg.applicationInfo.targetSdkVersion
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001399 >= android.os.Build.VERSION_CODES.DONUT)) {
Dianne Hackbornc4db95c2009-07-21 17:46:02 -07001400 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_RESIZEABLE_FOR_SCREENS;
1401 }
Dianne Hackborn11b822d2009-07-21 20:03:02 -07001402 if (anyDensity < 0 || (anyDensity > 0
1403 && pkg.applicationInfo.targetSdkVersion
1404 >= android.os.Build.VERSION_CODES.DONUT)) {
1405 pkg.applicationInfo.flags |= ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES;
Mitsuru Oshima8d112672009-04-27 12:01:23 -07001406 }
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -07001407
Nick Kralevich38f130e2013-04-04 13:19:10 -07001408 /*
1409 * b/8528162: Ignore the <uses-permission android:required> attribute if
1410 * targetSdkVersion < JELLY_BEAN_MR2. There are lots of apps in the wild
1411 * which are improperly using this attribute, even though it never worked.
1412 */
1413 if (pkg.applicationInfo.targetSdkVersion < Build.VERSION_CODES.JELLY_BEAN_MR2) {
1414 for (int i = 0; i < pkg.requestedPermissionsRequired.size(); i++) {
1415 pkg.requestedPermissionsRequired.set(i, Boolean.TRUE);
1416 }
1417 }
1418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 return pkg;
1420 }
1421
Nick Kralevich73f2d3c2013-04-04 14:38:13 -07001422 private boolean parseUsesPermission(Package pkg, Resources res, XmlResourceParser parser,
1423 AttributeSet attrs, String[] outError)
1424 throws XmlPullParserException, IOException {
1425 TypedArray sa = res.obtainAttributes(attrs,
1426 com.android.internal.R.styleable.AndroidManifestUsesPermission);
1427
1428 // Note: don't allow this value to be a reference to a resource
1429 // that may change.
1430 String name = sa.getNonResourceString(
1431 com.android.internal.R.styleable.AndroidManifestUsesPermission_name);
Nick Kralevich32eb5b12013-04-11 10:20:09 -07001432/*
Nick Kralevich73f2d3c2013-04-04 14:38:13 -07001433 boolean required = sa.getBoolean(
1434 com.android.internal.R.styleable.AndroidManifestUsesPermission_required, true);
Nick Kralevich32eb5b12013-04-11 10:20:09 -07001435*/
1436 boolean required = true; // Optional <uses-permission> not supported
Nick Kralevich73f2d3c2013-04-04 14:38:13 -07001437
1438 sa.recycle();
1439
1440 if (name != null) {
1441 int index = pkg.requestedPermissions.indexOf(name);
1442 if (index == -1) {
1443 pkg.requestedPermissions.add(name.intern());
1444 pkg.requestedPermissionsRequired.add(required ? Boolean.TRUE : Boolean.FALSE);
1445 } else {
1446 if (pkg.requestedPermissionsRequired.get(index) != required) {
1447 outError[0] = "conflicting <uses-permission> entries";
1448 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1449 return false;
1450 }
1451 }
1452 }
1453
1454 XmlUtils.skipCurrentTag(parser);
1455 return true;
1456 }
1457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 private static String buildClassName(String pkg, CharSequence clsSeq,
1459 String[] outError) {
1460 if (clsSeq == null || clsSeq.length() <= 0) {
1461 outError[0] = "Empty class name in package " + pkg;
1462 return null;
1463 }
1464 String cls = clsSeq.toString();
1465 char c = cls.charAt(0);
1466 if (c == '.') {
1467 return (pkg + cls).intern();
1468 }
1469 if (cls.indexOf('.') < 0) {
1470 StringBuilder b = new StringBuilder(pkg);
1471 b.append('.');
1472 b.append(cls);
1473 return b.toString().intern();
1474 }
1475 if (c >= 'a' && c <= 'z') {
1476 return cls.intern();
1477 }
1478 outError[0] = "Bad class name " + cls + " in package " + pkg;
1479 return null;
1480 }
1481
1482 private static String buildCompoundName(String pkg,
1483 CharSequence procSeq, String type, String[] outError) {
1484 String proc = procSeq.toString();
1485 char c = proc.charAt(0);
1486 if (pkg != null && c == ':') {
1487 if (proc.length() < 2) {
1488 outError[0] = "Bad " + type + " name " + proc + " in package " + pkg
1489 + ": must be at least two characters";
1490 return null;
1491 }
1492 String subName = proc.substring(1);
1493 String nameError = validateName(subName, false);
1494 if (nameError != null) {
1495 outError[0] = "Invalid " + type + " name " + proc + " in package "
1496 + pkg + ": " + nameError;
1497 return null;
1498 }
1499 return (pkg + proc).intern();
1500 }
1501 String nameError = validateName(proc, true);
1502 if (nameError != null && !"system".equals(proc)) {
1503 outError[0] = "Invalid " + type + " name " + proc + " in package "
1504 + pkg + ": " + nameError;
1505 return null;
1506 }
1507 return proc.intern();
1508 }
1509
1510 private static String buildProcessName(String pkg, String defProc,
1511 CharSequence procSeq, int flags, String[] separateProcesses,
1512 String[] outError) {
1513 if ((flags&PARSE_IGNORE_PROCESSES) != 0 && !"system".equals(procSeq)) {
1514 return defProc != null ? defProc : pkg;
1515 }
1516 if (separateProcesses != null) {
1517 for (int i=separateProcesses.length-1; i>=0; i--) {
1518 String sp = separateProcesses[i];
1519 if (sp.equals(pkg) || sp.equals(defProc) || sp.equals(procSeq)) {
1520 return pkg;
1521 }
1522 }
1523 }
1524 if (procSeq == null || procSeq.length() <= 0) {
1525 return defProc;
1526 }
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07001527 return buildCompoundName(pkg, procSeq, "process", outError);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 }
1529
1530 private static String buildTaskAffinityName(String pkg, String defProc,
1531 CharSequence procSeq, String[] outError) {
1532 if (procSeq == null) {
1533 return defProc;
1534 }
1535 if (procSeq.length() <= 0) {
1536 return null;
1537 }
1538 return buildCompoundName(pkg, procSeq, "taskAffinity", outError);
1539 }
Geremy Condraf1bcca82013-01-07 22:35:24 -08001540
1541 private boolean parseKeys(Package owner, Resources res,
1542 XmlPullParser parser, AttributeSet attrs, String[] outError)
1543 throws XmlPullParserException, IOException {
1544 // we've encountered the 'keys' tag
1545 // all the keys and keysets that we want must be defined here
1546 // so we're going to iterate over the parser and pull out the things we want
1547 int outerDepth = parser.getDepth();
1548
1549 int type;
1550 PublicKey currentKey = null;
1551 Map<PublicKey, Set<String>> definedKeySets = new HashMap<PublicKey, Set<String>>();
1552 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1553 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1554 if (type == XmlPullParser.END_TAG) {
1555 continue;
1556 }
1557 String tagname = parser.getName();
1558 if (tagname.equals("publicKey")) {
1559 final TypedArray sa = res.obtainAttributes(attrs,
1560 com.android.internal.R.styleable.PublicKey);
1561 final String encodedKey = sa.getNonResourceString(
1562 com.android.internal.R.styleable.PublicKey_value);
1563 currentKey = parsePublicKey(encodedKey);
1564 definedKeySets.put(currentKey, new HashSet<String>());
1565 sa.recycle();
1566 } else if (tagname.equals("keyset")) {
1567 final TypedArray sa = res.obtainAttributes(attrs,
1568 com.android.internal.R.styleable.KeySet);
1569 final String name = sa.getNonResourceString(
1570 com.android.internal.R.styleable.KeySet_name);
1571 definedKeySets.get(currentKey).add(name);
1572 sa.recycle();
1573 } else if (RIGID_PARSER) {
1574 Slog.w(TAG, "Bad element under <keys>: " + parser.getName()
1575 + " at " + mArchiveSourcePath + " "
1576 + parser.getPositionDescription());
1577 return false;
1578 } else {
1579 Slog.w(TAG, "Unknown element under <keys>: " + parser.getName()
1580 + " at " + mArchiveSourcePath + " "
1581 + parser.getPositionDescription());
1582 XmlUtils.skipCurrentTag(parser);
1583 continue;
1584 }
1585 }
1586
1587 owner.mKeySetMapping = new HashMap<String, Set<PublicKey>>();
1588 for (Map.Entry<PublicKey, Set<String>> e : definedKeySets.entrySet()) {
1589 PublicKey key = e.getKey();
1590 Set<String> keySetNames = e.getValue();
1591 for (String alias : keySetNames) {
1592 if (owner.mKeySetMapping.containsKey(alias)) {
1593 owner.mKeySetMapping.get(alias).add(key);
1594 } else {
1595 Set<PublicKey> keys = new HashSet<PublicKey>();
1596 keys.add(key);
1597 owner.mKeySetMapping.put(alias, keys);
1598 }
1599 }
1600 }
1601
1602 return true;
1603 }
1604
Dianne Hackbornfd5015b2012-04-30 16:33:56 -07001605 private PermissionGroup parsePermissionGroup(Package owner, int flags, Resources res,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 XmlPullParser parser, AttributeSet attrs, String[] outError)
1607 throws XmlPullParserException, IOException {
1608 PermissionGroup perm = new PermissionGroup(owner);
1609
1610 TypedArray sa = res.obtainAttributes(attrs,
1611 com.android.internal.R.styleable.AndroidManifestPermissionGroup);
1612
1613 if (!parsePackageItemInfo(owner, perm.info, outError,
1614 "<permission-group>", sa,
1615 com.android.internal.R.styleable.AndroidManifestPermissionGroup_name,
1616 com.android.internal.R.styleable.AndroidManifestPermissionGroup_label,
Adam Powell81cd2e92010-04-21 16:35:18 -07001617 com.android.internal.R.styleable.AndroidManifestPermissionGroup_icon,
1618 com.android.internal.R.styleable.AndroidManifestPermissionGroup_logo)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 sa.recycle();
1620 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1621 return null;
1622 }
1623
1624 perm.info.descriptionRes = sa.getResourceId(
1625 com.android.internal.R.styleable.AndroidManifestPermissionGroup_description,
1626 0);
Dianne Hackborn7454d3b2012-09-12 17:22:00 -07001627 perm.info.flags = sa.getInt(
1628 com.android.internal.R.styleable.AndroidManifestPermissionGroup_permissionGroupFlags, 0);
Dianne Hackbornfd5015b2012-04-30 16:33:56 -07001629 perm.info.priority = sa.getInt(
1630 com.android.internal.R.styleable.AndroidManifestPermissionGroup_priority, 0);
Dianne Hackborn99222d22012-05-06 16:30:15 -07001631 if (perm.info.priority > 0 && (flags&PARSE_IS_SYSTEM) == 0) {
Dianne Hackbornfd5015b2012-04-30 16:33:56 -07001632 perm.info.priority = 0;
1633 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634
1635 sa.recycle();
1636
1637 if (!parseAllMetaData(res, parser, attrs, "<permission-group>", perm,
1638 outError)) {
1639 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1640 return null;
1641 }
1642
1643 owner.permissionGroups.add(perm);
1644
1645 return perm;
1646 }
1647
1648 private Permission parsePermission(Package owner, Resources res,
1649 XmlPullParser parser, AttributeSet attrs, String[] outError)
1650 throws XmlPullParserException, IOException {
1651 Permission perm = new Permission(owner);
1652
1653 TypedArray sa = res.obtainAttributes(attrs,
1654 com.android.internal.R.styleable.AndroidManifestPermission);
1655
1656 if (!parsePackageItemInfo(owner, perm.info, outError,
1657 "<permission>", sa,
1658 com.android.internal.R.styleable.AndroidManifestPermission_name,
1659 com.android.internal.R.styleable.AndroidManifestPermission_label,
Adam Powell81cd2e92010-04-21 16:35:18 -07001660 com.android.internal.R.styleable.AndroidManifestPermission_icon,
1661 com.android.internal.R.styleable.AndroidManifestPermission_logo)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 sa.recycle();
1663 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1664 return null;
1665 }
1666
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001667 // Note: don't allow this value to be a reference to a resource
1668 // that may change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 perm.info.group = sa.getNonResourceString(
1670 com.android.internal.R.styleable.AndroidManifestPermission_permissionGroup);
1671 if (perm.info.group != null) {
1672 perm.info.group = perm.info.group.intern();
1673 }
1674
1675 perm.info.descriptionRes = sa.getResourceId(
1676 com.android.internal.R.styleable.AndroidManifestPermission_description,
1677 0);
1678
1679 perm.info.protectionLevel = sa.getInt(
1680 com.android.internal.R.styleable.AndroidManifestPermission_protectionLevel,
1681 PermissionInfo.PROTECTION_NORMAL);
1682
Dianne Hackborn2ca2c872012-09-16 16:03:36 -07001683 perm.info.flags = sa.getInt(
1684 com.android.internal.R.styleable.AndroidManifestPermission_permissionFlags, 0);
1685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 sa.recycle();
Dianne Hackborne639da72012-02-21 15:11:13 -08001687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 if (perm.info.protectionLevel == -1) {
1689 outError[0] = "<permission> does not specify protectionLevel";
1690 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1691 return null;
1692 }
Dianne Hackborne639da72012-02-21 15:11:13 -08001693
1694 perm.info.protectionLevel = PermissionInfo.fixProtectionLevel(perm.info.protectionLevel);
1695
1696 if ((perm.info.protectionLevel&PermissionInfo.PROTECTION_MASK_FLAGS) != 0) {
1697 if ((perm.info.protectionLevel&PermissionInfo.PROTECTION_MASK_BASE) !=
1698 PermissionInfo.PROTECTION_SIGNATURE) {
1699 outError[0] = "<permission> protectionLevel specifies a flag but is "
1700 + "not based on signature type";
1701 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1702 return null;
1703 }
1704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705
1706 if (!parseAllMetaData(res, parser, attrs, "<permission>", perm,
1707 outError)) {
1708 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1709 return null;
1710 }
1711
1712 owner.permissions.add(perm);
1713
1714 return perm;
1715 }
1716
1717 private Permission parsePermissionTree(Package owner, Resources res,
1718 XmlPullParser parser, AttributeSet attrs, String[] outError)
1719 throws XmlPullParserException, IOException {
1720 Permission perm = new Permission(owner);
1721
1722 TypedArray sa = res.obtainAttributes(attrs,
1723 com.android.internal.R.styleable.AndroidManifestPermissionTree);
1724
1725 if (!parsePackageItemInfo(owner, perm.info, outError,
1726 "<permission-tree>", sa,
1727 com.android.internal.R.styleable.AndroidManifestPermissionTree_name,
1728 com.android.internal.R.styleable.AndroidManifestPermissionTree_label,
Adam Powell81cd2e92010-04-21 16:35:18 -07001729 com.android.internal.R.styleable.AndroidManifestPermissionTree_icon,
1730 com.android.internal.R.styleable.AndroidManifestPermissionTree_logo)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 sa.recycle();
1732 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1733 return null;
1734 }
1735
1736 sa.recycle();
1737
1738 int index = perm.info.name.indexOf('.');
1739 if (index > 0) {
1740 index = perm.info.name.indexOf('.', index+1);
1741 }
1742 if (index < 0) {
1743 outError[0] = "<permission-tree> name has less than three segments: "
1744 + perm.info.name;
1745 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1746 return null;
1747 }
1748
1749 perm.info.descriptionRes = 0;
1750 perm.info.protectionLevel = PermissionInfo.PROTECTION_NORMAL;
1751 perm.tree = true;
1752
1753 if (!parseAllMetaData(res, parser, attrs, "<permission-tree>", perm,
1754 outError)) {
1755 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1756 return null;
1757 }
1758
1759 owner.permissions.add(perm);
1760
1761 return perm;
1762 }
1763
1764 private Instrumentation parseInstrumentation(Package owner, Resources res,
1765 XmlPullParser parser, AttributeSet attrs, String[] outError)
1766 throws XmlPullParserException, IOException {
1767 TypedArray sa = res.obtainAttributes(attrs,
1768 com.android.internal.R.styleable.AndroidManifestInstrumentation);
1769
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001770 if (mParseInstrumentationArgs == null) {
1771 mParseInstrumentationArgs = new ParsePackageItemArgs(owner, outError,
1772 com.android.internal.R.styleable.AndroidManifestInstrumentation_name,
1773 com.android.internal.R.styleable.AndroidManifestInstrumentation_label,
Adam Powell81cd2e92010-04-21 16:35:18 -07001774 com.android.internal.R.styleable.AndroidManifestInstrumentation_icon,
1775 com.android.internal.R.styleable.AndroidManifestInstrumentation_logo);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07001776 mParseInstrumentationArgs.tag = "<instrumentation>";
1777 }
1778
1779 mParseInstrumentationArgs.sa = sa;
1780
1781 Instrumentation a = new Instrumentation(mParseInstrumentationArgs,
1782 new InstrumentationInfo());
1783 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 sa.recycle();
1785 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1786 return null;
1787 }
1788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 String str;
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001790 // Note: don't allow this value to be a reference to a resource
1791 // that may change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 str = sa.getNonResourceString(
1793 com.android.internal.R.styleable.AndroidManifestInstrumentation_targetPackage);
1794 a.info.targetPackage = str != null ? str.intern() : null;
1795
1796 a.info.handleProfiling = sa.getBoolean(
1797 com.android.internal.R.styleable.AndroidManifestInstrumentation_handleProfiling,
1798 false);
1799
1800 a.info.functionalTest = sa.getBoolean(
1801 com.android.internal.R.styleable.AndroidManifestInstrumentation_functionalTest,
1802 false);
1803
1804 sa.recycle();
1805
1806 if (a.info.targetPackage == null) {
1807 outError[0] = "<instrumentation> does not specify targetPackage";
1808 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1809 return null;
1810 }
1811
1812 if (!parseAllMetaData(res, parser, attrs, "<instrumentation>", a,
1813 outError)) {
1814 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1815 return null;
1816 }
1817
1818 owner.instrumentation.add(a);
1819
1820 return a;
1821 }
1822
1823 private boolean parseApplication(Package owner, Resources res,
1824 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
1825 throws XmlPullParserException, IOException {
1826 final ApplicationInfo ai = owner.applicationInfo;
1827 final String pkgName = owner.applicationInfo.packageName;
1828
1829 TypedArray sa = res.obtainAttributes(attrs,
1830 com.android.internal.R.styleable.AndroidManifestApplication);
1831
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001832 String name = sa.getNonConfigurationString(
1833 com.android.internal.R.styleable.AndroidManifestApplication_name, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 if (name != null) {
1835 ai.className = buildClassName(pkgName, name, outError);
1836 if (ai.className == null) {
1837 sa.recycle();
1838 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
1839 return false;
1840 }
1841 }
1842
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001843 String manageSpaceActivity = sa.getNonConfigurationString(
1844 com.android.internal.R.styleable.AndroidManifestApplication_manageSpaceActivity, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 if (manageSpaceActivity != null) {
1846 ai.manageSpaceActivityName = buildClassName(pkgName, manageSpaceActivity,
1847 outError);
1848 }
1849
Christopher Tate181fafa2009-05-14 11:12:14 -07001850 boolean allowBackup = sa.getBoolean(
1851 com.android.internal.R.styleable.AndroidManifestApplication_allowBackup, true);
1852 if (allowBackup) {
1853 ai.flags |= ApplicationInfo.FLAG_ALLOW_BACKUP;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001854
Christopher Tate3de55bc2010-03-12 17:28:08 -08001855 // backupAgent, killAfterRestore, and restoreAnyVersion are only relevant
1856 // if backup is possible for the given application.
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001857 String backupAgent = sa.getNonConfigurationString(
1858 com.android.internal.R.styleable.AndroidManifestApplication_backupAgent, 0);
Christopher Tate181fafa2009-05-14 11:12:14 -07001859 if (backupAgent != null) {
1860 ai.backupAgentName = buildClassName(pkgName, backupAgent, outError);
Kenny Rootd2d29252011-08-08 11:27:57 -07001861 if (DEBUG_BACKUP) {
1862 Slog.v(TAG, "android:backupAgent = " + ai.backupAgentName
Dianne Hackborna33e3f72009-09-29 17:28:24 -07001863 + " from " + pkgName + "+" + backupAgent);
1864 }
Christopher Tate5e1ab332009-09-01 20:32:49 -07001865
1866 if (sa.getBoolean(
1867 com.android.internal.R.styleable.AndroidManifestApplication_killAfterRestore,
1868 true)) {
1869 ai.flags |= ApplicationInfo.FLAG_KILL_AFTER_RESTORE;
1870 }
1871 if (sa.getBoolean(
Christopher Tate3dda5182010-02-24 16:06:18 -08001872 com.android.internal.R.styleable.AndroidManifestApplication_restoreAnyVersion,
1873 false)) {
1874 ai.flags |= ApplicationInfo.FLAG_RESTORE_ANY_VERSION;
1875 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001876 }
1877 }
Christopher Tate4a627c72011-04-01 14:43:32 -07001878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 TypedValue v = sa.peekValue(
1880 com.android.internal.R.styleable.AndroidManifestApplication_label);
1881 if (v != null && (ai.labelRes=v.resourceId) == 0) {
1882 ai.nonLocalizedLabel = v.coerceToString();
1883 }
1884
1885 ai.icon = sa.getResourceId(
1886 com.android.internal.R.styleable.AndroidManifestApplication_icon, 0);
Adam Powell81cd2e92010-04-21 16:35:18 -07001887 ai.logo = sa.getResourceId(
1888 com.android.internal.R.styleable.AndroidManifestApplication_logo, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 ai.theme = sa.getResourceId(
Dianne Hackbornb35cd542011-01-04 21:30:53 -08001890 com.android.internal.R.styleable.AndroidManifestApplication_theme, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 ai.descriptionRes = sa.getResourceId(
1892 com.android.internal.R.styleable.AndroidManifestApplication_description, 0);
1893
1894 if ((flags&PARSE_IS_SYSTEM) != 0) {
1895 if (sa.getBoolean(
1896 com.android.internal.R.styleable.AndroidManifestApplication_persistent,
1897 false)) {
1898 ai.flags |= ApplicationInfo.FLAG_PERSISTENT;
1899 }
Amith Yamasani0d8750d2013-05-01 15:25:28 -07001900 }
1901
1902 if (sa.getBoolean(
1903 com.android.internal.R.styleable.AndroidManifestApplication_requiredForAllUsers,
1904 false)) {
1905 owner.mRequiredForAllUsers = true;
Amith Yamasanie993ae12013-04-15 13:42:57 -07001906 }
1907
1908 String restrictedAccountType = sa.getString(com.android.internal.R.styleable
1909 .AndroidManifestApplication_restrictedAccountType);
1910 if (restrictedAccountType != null && restrictedAccountType.length() > 0) {
1911 owner.mRestrictedAccountType = restrictedAccountType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 }
1913
Amith Yamasaniccbe3892013-04-12 17:52:42 -07001914 String requiredAccountType = sa.getString(com.android.internal.R.styleable
1915 .AndroidManifestApplication_requiredAccountType);
1916 if (requiredAccountType != null && requiredAccountType.length() > 0) {
1917 owner.mRequiredAccountType = requiredAccountType;
1918 }
1919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 if (sa.getBoolean(
1921 com.android.internal.R.styleable.AndroidManifestApplication_debuggable,
1922 false)) {
1923 ai.flags |= ApplicationInfo.FLAG_DEBUGGABLE;
1924 }
1925
1926 if (sa.getBoolean(
Ben Chengef3f5dd2010-03-29 15:47:26 -07001927 com.android.internal.R.styleable.AndroidManifestApplication_vmSafeMode,
Ben Cheng23085b72010-02-08 16:06:32 -08001928 false)) {
1929 ai.flags |= ApplicationInfo.FLAG_VM_SAFE_MODE;
1930 }
1931
Romain Guy529b60a2010-08-03 18:05:47 -07001932 boolean hardwareAccelerated = sa.getBoolean(
Romain Guy812ccbe2010-06-01 14:07:24 -07001933 com.android.internal.R.styleable.AndroidManifestApplication_hardwareAccelerated,
Dianne Hackborn2d6833b2011-06-24 16:04:19 -07001934 owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.ICE_CREAM_SANDWICH);
Romain Guy812ccbe2010-06-01 14:07:24 -07001935
1936 if (sa.getBoolean(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 com.android.internal.R.styleable.AndroidManifestApplication_hasCode,
1938 true)) {
1939 ai.flags |= ApplicationInfo.FLAG_HAS_CODE;
1940 }
1941
1942 if (sa.getBoolean(
1943 com.android.internal.R.styleable.AndroidManifestApplication_allowTaskReparenting,
1944 false)) {
1945 ai.flags |= ApplicationInfo.FLAG_ALLOW_TASK_REPARENTING;
1946 }
1947
1948 if (sa.getBoolean(
1949 com.android.internal.R.styleable.AndroidManifestApplication_allowClearUserData,
1950 true)) {
1951 ai.flags |= ApplicationInfo.FLAG_ALLOW_CLEAR_USER_DATA;
1952 }
1953
Dianne Hackbornade3eca2009-05-11 18:54:45 -07001954 if (sa.getBoolean(
1955 com.android.internal.R.styleable.AndroidManifestApplication_testOnly,
Dianne Hackborne7fe35b2009-05-13 10:53:41 -07001956 false)) {
Dianne Hackbornade3eca2009-05-11 18:54:45 -07001957 ai.flags |= ApplicationInfo.FLAG_TEST_ONLY;
1958 }
1959
Jason parksa3cdaa52011-01-13 14:15:43 -06001960 if (sa.getBoolean(
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08001961 com.android.internal.R.styleable.AndroidManifestApplication_largeHeap,
Jason parksa3cdaa52011-01-13 14:15:43 -06001962 false)) {
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08001963 ai.flags |= ApplicationInfo.FLAG_LARGE_HEAP;
Jason parksa3cdaa52011-01-13 14:15:43 -06001964 }
1965
Fabrice Di Meglio59dfce82012-04-02 16:17:20 -07001966 if (sa.getBoolean(
1967 com.android.internal.R.styleable.AndroidManifestApplication_supportsRtl,
1968 false /* default is no RTL support*/)) {
1969 ai.flags |= ApplicationInfo.FLAG_SUPPORTS_RTL;
1970 }
1971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 String str;
Dianne Hackborncf244ad2010-03-09 15:00:30 -08001973 str = sa.getNonConfigurationString(
1974 com.android.internal.R.styleable.AndroidManifestApplication_permission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 ai.permission = (str != null && str.length() > 0) ? str.intern() : null;
1976
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07001977 if (owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.FROYO) {
1978 str = sa.getNonConfigurationString(
1979 com.android.internal.R.styleable.AndroidManifestApplication_taskAffinity, 0);
1980 } else {
1981 // Some older apps have been seen to use a resource reference
1982 // here that on older builds was ignored (with a warning). We
1983 // need to continue to do this for them so they don't break.
1984 str = sa.getNonResourceString(
1985 com.android.internal.R.styleable.AndroidManifestApplication_taskAffinity);
1986 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 ai.taskAffinity = buildTaskAffinityName(ai.packageName, ai.packageName,
1988 str, outError);
1989
1990 if (outError[0] == null) {
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07001991 CharSequence pname;
1992 if (owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.FROYO) {
1993 pname = sa.getNonConfigurationString(
1994 com.android.internal.R.styleable.AndroidManifestApplication_process, 0);
1995 } else {
1996 // Some older apps have been seen to use a resource reference
1997 // here that on older builds was ignored (with a warning). We
1998 // need to continue to do this for them so they don't break.
1999 pname = sa.getNonResourceString(
2000 com.android.internal.R.styleable.AndroidManifestApplication_process);
2001 }
2002 ai.processName = buildProcessName(ai.packageName, null, pname,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 flags, mSeparateProcesses, outError);
2004
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002005 ai.enabled = sa.getBoolean(
2006 com.android.internal.R.styleable.AndroidManifestApplication_enabled, true);
Dianne Hackborn860755f2010-06-03 18:47:52 -07002007
Dianne Hackborn02486b12010-08-26 14:18:37 -07002008 if (false) {
2009 if (sa.getBoolean(
2010 com.android.internal.R.styleable.AndroidManifestApplication_cantSaveState,
2011 false)) {
Dianne Hackborn54e570f2010-10-04 18:32:32 -07002012 ai.flags |= ApplicationInfo.FLAG_CANT_SAVE_STATE;
Dianne Hackborn02486b12010-08-26 14:18:37 -07002013
2014 // A heavy-weight application can not be in a custom process.
2015 // We can do direct compare because we intern all strings.
2016 if (ai.processName != null && ai.processName != ai.packageName) {
2017 outError[0] = "cantSaveState applications can not use custom processes";
2018 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07002019 }
2020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 }
2022
Adam Powell269248d2011-08-02 10:26:54 -07002023 ai.uiOptions = sa.getInt(
2024 com.android.internal.R.styleable.AndroidManifestApplication_uiOptions, 0);
2025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 sa.recycle();
2027
2028 if (outError[0] != null) {
2029 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
2030 return false;
2031 }
2032
2033 final int innerDepth = parser.getDepth();
2034
2035 int type;
Kenny Rootd2d29252011-08-08 11:27:57 -07002036 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
2037 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
2038 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 continue;
2040 }
2041
2042 String tagName = parser.getName();
2043 if (tagName.equals("activity")) {
Romain Guy529b60a2010-08-03 18:05:47 -07002044 Activity a = parseActivity(owner, res, parser, attrs, flags, outError, false,
2045 hardwareAccelerated);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 if (a == null) {
2047 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
2048 return false;
2049 }
2050
2051 owner.activities.add(a);
2052
2053 } else if (tagName.equals("receiver")) {
Romain Guy529b60a2010-08-03 18:05:47 -07002054 Activity a = parseActivity(owner, res, parser, attrs, flags, outError, true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 if (a == null) {
2056 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
2057 return false;
2058 }
2059
2060 owner.receivers.add(a);
2061
2062 } else if (tagName.equals("service")) {
2063 Service s = parseService(owner, res, parser, attrs, flags, outError);
2064 if (s == null) {
2065 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
2066 return false;
2067 }
2068
2069 owner.services.add(s);
2070
2071 } else if (tagName.equals("provider")) {
2072 Provider p = parseProvider(owner, res, parser, attrs, flags, outError);
2073 if (p == null) {
2074 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
2075 return false;
2076 }
2077
2078 owner.providers.add(p);
2079
2080 } else if (tagName.equals("activity-alias")) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002081 Activity a = parseActivityAlias(owner, res, parser, attrs, flags, outError);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 if (a == null) {
2083 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
2084 return false;
2085 }
2086
2087 owner.activities.add(a);
2088
2089 } else if (parser.getName().equals("meta-data")) {
2090 // note: application meta-data is stored off to the side, so it can
2091 // remain null in the primary copy (we like to avoid extra copies because
2092 // it can be large)
2093 if ((owner.mAppMetaData = parseMetaData(res, parser, attrs, owner.mAppMetaData,
2094 outError)) == null) {
2095 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
2096 return false;
2097 }
2098
Dianne Hackbornc895be72013-03-11 17:48:43 -07002099 } else if (tagName.equals("library")) {
2100 sa = res.obtainAttributes(attrs,
2101 com.android.internal.R.styleable.AndroidManifestLibrary);
2102
2103 // Note: don't allow this value to be a reference to a resource
2104 // that may change.
2105 String lname = sa.getNonResourceString(
2106 com.android.internal.R.styleable.AndroidManifestLibrary_name);
2107
2108 sa.recycle();
2109
2110 if (lname != null) {
2111 if (owner.libraryNames == null) {
2112 owner.libraryNames = new ArrayList<String>();
2113 }
2114 if (!owner.libraryNames.contains(lname)) {
2115 owner.libraryNames.add(lname.intern());
2116 }
2117 }
2118
2119 XmlUtils.skipCurrentTag(parser);
2120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 } else if (tagName.equals("uses-library")) {
2122 sa = res.obtainAttributes(attrs,
2123 com.android.internal.R.styleable.AndroidManifestUsesLibrary);
2124
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002125 // Note: don't allow this value to be a reference to a resource
2126 // that may change.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 String lname = sa.getNonResourceString(
2128 com.android.internal.R.styleable.AndroidManifestUsesLibrary_name);
Dianne Hackborn49237342009-08-27 20:08:01 -07002129 boolean req = sa.getBoolean(
2130 com.android.internal.R.styleable.AndroidManifestUsesLibrary_required,
2131 true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132
2133 sa.recycle();
2134
Dianne Hackborn49237342009-08-27 20:08:01 -07002135 if (lname != null) {
2136 if (req) {
2137 if (owner.usesLibraries == null) {
2138 owner.usesLibraries = new ArrayList<String>();
2139 }
2140 if (!owner.usesLibraries.contains(lname)) {
2141 owner.usesLibraries.add(lname.intern());
2142 }
2143 } else {
2144 if (owner.usesOptionalLibraries == null) {
2145 owner.usesOptionalLibraries = new ArrayList<String>();
2146 }
2147 if (!owner.usesOptionalLibraries.contains(lname)) {
2148 owner.usesOptionalLibraries.add(lname.intern());
2149 }
2150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 }
2152
2153 XmlUtils.skipCurrentTag(parser);
2154
Dianne Hackborncef65ee2010-09-30 18:27:22 -07002155 } else if (tagName.equals("uses-package")) {
2156 // Dependencies for app installers; we don't currently try to
2157 // enforce this.
2158 XmlUtils.skipCurrentTag(parser);
2159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 } else {
2161 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002162 Slog.w(TAG, "Unknown element under <application>: " + tagName
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002163 + " at " + mArchiveSourcePath + " "
2164 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 XmlUtils.skipCurrentTag(parser);
2166 continue;
2167 } else {
2168 outError[0] = "Bad element under <application>: " + tagName;
2169 mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
2170 return false;
2171 }
2172 }
2173 }
2174
2175 return true;
2176 }
2177
2178 private boolean parsePackageItemInfo(Package owner, PackageItemInfo outInfo,
2179 String[] outError, String tag, TypedArray sa,
Adam Powell81cd2e92010-04-21 16:35:18 -07002180 int nameRes, int labelRes, int iconRes, int logoRes) {
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002181 String name = sa.getNonConfigurationString(nameRes, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 if (name == null) {
2183 outError[0] = tag + " does not specify android:name";
2184 return false;
2185 }
2186
2187 outInfo.name
2188 = buildClassName(owner.applicationInfo.packageName, name, outError);
2189 if (outInfo.name == null) {
2190 return false;
2191 }
2192
2193 int iconVal = sa.getResourceId(iconRes, 0);
2194 if (iconVal != 0) {
2195 outInfo.icon = iconVal;
2196 outInfo.nonLocalizedLabel = null;
2197 }
Adam Powell81cd2e92010-04-21 16:35:18 -07002198
2199 int logoVal = sa.getResourceId(logoRes, 0);
2200 if (logoVal != 0) {
2201 outInfo.logo = logoVal;
2202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002203
2204 TypedValue v = sa.peekValue(labelRes);
2205 if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
2206 outInfo.nonLocalizedLabel = v.coerceToString();
2207 }
2208
2209 outInfo.packageName = owner.packageName;
2210
2211 return true;
2212 }
2213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 private Activity parseActivity(Package owner, Resources res,
2215 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError,
Romain Guy529b60a2010-08-03 18:05:47 -07002216 boolean receiver, boolean hardwareAccelerated)
2217 throws XmlPullParserException, IOException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 TypedArray sa = res.obtainAttributes(attrs,
2219 com.android.internal.R.styleable.AndroidManifestActivity);
2220
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002221 if (mParseActivityArgs == null) {
2222 mParseActivityArgs = new ParseComponentArgs(owner, outError,
2223 com.android.internal.R.styleable.AndroidManifestActivity_name,
2224 com.android.internal.R.styleable.AndroidManifestActivity_label,
2225 com.android.internal.R.styleable.AndroidManifestActivity_icon,
Adam Powell81cd2e92010-04-21 16:35:18 -07002226 com.android.internal.R.styleable.AndroidManifestActivity_logo,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002227 mSeparateProcesses,
2228 com.android.internal.R.styleable.AndroidManifestActivity_process,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08002229 com.android.internal.R.styleable.AndroidManifestActivity_description,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002230 com.android.internal.R.styleable.AndroidManifestActivity_enabled);
2231 }
2232
2233 mParseActivityArgs.tag = receiver ? "<receiver>" : "<activity>";
2234 mParseActivityArgs.sa = sa;
2235 mParseActivityArgs.flags = flags;
2236
2237 Activity a = new Activity(mParseActivityArgs, new ActivityInfo());
2238 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 sa.recycle();
2240 return null;
2241 }
2242
Dianne Hackborn7d19e022012-08-07 19:12:33 -07002243 boolean setExported = sa.hasValue(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 com.android.internal.R.styleable.AndroidManifestActivity_exported);
2245 if (setExported) {
2246 a.info.exported = sa.getBoolean(
2247 com.android.internal.R.styleable.AndroidManifestActivity_exported, false);
2248 }
2249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 a.info.theme = sa.getResourceId(
2251 com.android.internal.R.styleable.AndroidManifestActivity_theme, 0);
2252
Adam Powell269248d2011-08-02 10:26:54 -07002253 a.info.uiOptions = sa.getInt(
2254 com.android.internal.R.styleable.AndroidManifestActivity_uiOptions,
2255 a.info.applicationInfo.uiOptions);
2256
Adam Powelldd8fab22012-03-22 17:47:27 -07002257 String parentName = sa.getNonConfigurationString(
2258 com.android.internal.R.styleable.AndroidManifestActivity_parentActivityName, 0);
2259 if (parentName != null) {
2260 String parentClassName = buildClassName(a.info.packageName, parentName, outError);
2261 if (outError[0] == null) {
2262 a.info.parentActivityName = parentClassName;
2263 } else {
2264 Log.e(TAG, "Activity " + a.info.name + " specified invalid parentActivityName " +
2265 parentName);
2266 outError[0] = null;
2267 }
2268 }
2269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 String str;
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002271 str = sa.getNonConfigurationString(
2272 com.android.internal.R.styleable.AndroidManifestActivity_permission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 if (str == null) {
2274 a.info.permission = owner.applicationInfo.permission;
2275 } else {
2276 a.info.permission = str.length() > 0 ? str.toString().intern() : null;
2277 }
2278
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002279 str = sa.getNonConfigurationString(
2280 com.android.internal.R.styleable.AndroidManifestActivity_taskAffinity, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 a.info.taskAffinity = buildTaskAffinityName(owner.applicationInfo.packageName,
2282 owner.applicationInfo.taskAffinity, str, outError);
2283
2284 a.info.flags = 0;
2285 if (sa.getBoolean(
2286 com.android.internal.R.styleable.AndroidManifestActivity_multiprocess,
2287 false)) {
2288 a.info.flags |= ActivityInfo.FLAG_MULTIPROCESS;
2289 }
2290
2291 if (sa.getBoolean(
2292 com.android.internal.R.styleable.AndroidManifestActivity_finishOnTaskLaunch,
2293 false)) {
2294 a.info.flags |= ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH;
2295 }
2296
2297 if (sa.getBoolean(
2298 com.android.internal.R.styleable.AndroidManifestActivity_clearTaskOnLaunch,
2299 false)) {
2300 a.info.flags |= ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH;
2301 }
2302
2303 if (sa.getBoolean(
2304 com.android.internal.R.styleable.AndroidManifestActivity_noHistory,
2305 false)) {
2306 a.info.flags |= ActivityInfo.FLAG_NO_HISTORY;
2307 }
2308
2309 if (sa.getBoolean(
2310 com.android.internal.R.styleable.AndroidManifestActivity_alwaysRetainTaskState,
2311 false)) {
2312 a.info.flags |= ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE;
2313 }
2314
2315 if (sa.getBoolean(
2316 com.android.internal.R.styleable.AndroidManifestActivity_stateNotNeeded,
2317 false)) {
2318 a.info.flags |= ActivityInfo.FLAG_STATE_NOT_NEEDED;
2319 }
2320
2321 if (sa.getBoolean(
2322 com.android.internal.R.styleable.AndroidManifestActivity_excludeFromRecents,
2323 false)) {
2324 a.info.flags |= ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS;
2325 }
2326
2327 if (sa.getBoolean(
2328 com.android.internal.R.styleable.AndroidManifestActivity_allowTaskReparenting,
2329 (owner.applicationInfo.flags&ApplicationInfo.FLAG_ALLOW_TASK_REPARENTING) != 0)) {
2330 a.info.flags |= ActivityInfo.FLAG_ALLOW_TASK_REPARENTING;
2331 }
2332
Dianne Hackbornffa42482009-09-23 22:20:11 -07002333 if (sa.getBoolean(
2334 com.android.internal.R.styleable.AndroidManifestActivity_finishOnCloseSystemDialogs,
2335 false)) {
2336 a.info.flags |= ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS;
2337 }
2338
Daniel Sandler613dde42010-06-21 13:46:39 -04002339 if (sa.getBoolean(
Craig Mautner5962b122012-10-05 14:45:52 -07002340 com.android.internal.R.styleable.AndroidManifestActivity_showOnLockScreen,
2341 false)) {
2342 a.info.flags |= ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN;
2343 }
2344
2345 if (sa.getBoolean(
Daniel Sandler613dde42010-06-21 13:46:39 -04002346 com.android.internal.R.styleable.AndroidManifestActivity_immersive,
2347 false)) {
2348 a.info.flags |= ActivityInfo.FLAG_IMMERSIVE;
2349 }
Craig Mautner5962b122012-10-05 14:45:52 -07002350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 if (!receiver) {
Romain Guy529b60a2010-08-03 18:05:47 -07002352 if (sa.getBoolean(
2353 com.android.internal.R.styleable.AndroidManifestActivity_hardwareAccelerated,
2354 hardwareAccelerated)) {
2355 a.info.flags |= ActivityInfo.FLAG_HARDWARE_ACCELERATED;
2356 }
2357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 a.info.launchMode = sa.getInt(
2359 com.android.internal.R.styleable.AndroidManifestActivity_launchMode,
2360 ActivityInfo.LAUNCH_MULTIPLE);
2361 a.info.screenOrientation = sa.getInt(
2362 com.android.internal.R.styleable.AndroidManifestActivity_screenOrientation,
2363 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
2364 a.info.configChanges = sa.getInt(
2365 com.android.internal.R.styleable.AndroidManifestActivity_configChanges,
2366 0);
2367 a.info.softInputMode = sa.getInt(
2368 com.android.internal.R.styleable.AndroidManifestActivity_windowSoftInputMode,
2369 0);
2370 } else {
2371 a.info.launchMode = ActivityInfo.LAUNCH_MULTIPLE;
2372 a.info.configChanges = 0;
2373 }
2374
Dianne Hackborn7d19e022012-08-07 19:12:33 -07002375 if (receiver) {
2376 if (sa.getBoolean(
2377 com.android.internal.R.styleable.AndroidManifestActivity_singleUser,
2378 false)) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07002379 a.info.flags |= ActivityInfo.FLAG_SINGLE_USER;
Dianne Hackborn7d19e022012-08-07 19:12:33 -07002380 if (a.info.exported) {
2381 Slog.w(TAG, "Activity exported request ignored due to singleUser: "
2382 + a.className + " at " + mArchiveSourcePath + " "
2383 + parser.getPositionDescription());
2384 a.info.exported = false;
2385 }
2386 setExported = true;
2387 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07002388 if (sa.getBoolean(
2389 com.android.internal.R.styleable.AndroidManifestActivity_primaryUserOnly,
2390 false)) {
2391 a.info.flags |= ActivityInfo.FLAG_PRIMARY_USER_ONLY;
2392 }
Dianne Hackborn7d19e022012-08-07 19:12:33 -07002393 }
2394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 sa.recycle();
2396
Dianne Hackborn54e570f2010-10-04 18:32:32 -07002397 if (receiver && (owner.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07002398 // A heavy-weight application can not have receives in its main process
2399 // We can do direct compare because we intern all strings.
2400 if (a.info.processName == owner.packageName) {
2401 outError[0] = "Heavy-weight applications can not have receivers in main process";
2402 }
2403 }
2404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 if (outError[0] != null) {
2406 return null;
2407 }
2408
2409 int outerDepth = parser.getDepth();
2410 int type;
2411 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2412 && (type != XmlPullParser.END_TAG
2413 || parser.getDepth() > outerDepth)) {
2414 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2415 continue;
2416 }
2417
2418 if (parser.getName().equals("intent-filter")) {
2419 ActivityIntentInfo intent = new ActivityIntentInfo(a);
2420 if (!parseIntent(res, parser, attrs, flags, intent, outError, !receiver)) {
2421 return null;
2422 }
2423 if (intent.countActions() == 0) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002424 Slog.w(TAG, "No actions in intent filter at "
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -07002425 + mArchiveSourcePath + " "
2426 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 } else {
2428 a.intents.add(intent);
2429 }
2430 } else if (parser.getName().equals("meta-data")) {
2431 if ((a.metaData=parseMetaData(res, parser, attrs, a.metaData,
2432 outError)) == null) {
2433 return null;
2434 }
2435 } else {
2436 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002437 Slog.w(TAG, "Problem in package " + mArchiveSourcePath + ":");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 if (receiver) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002439 Slog.w(TAG, "Unknown element under <receiver>: " + parser.getName()
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002440 + " at " + mArchiveSourcePath + " "
2441 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -07002443 Slog.w(TAG, "Unknown element under <activity>: " + parser.getName()
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002444 + " at " + mArchiveSourcePath + " "
2445 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 }
2447 XmlUtils.skipCurrentTag(parser);
2448 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 } else {
Kenny Rootd2d29252011-08-08 11:27:57 -07002450 if (receiver) {
2451 outError[0] = "Bad element under <receiver>: " + parser.getName();
2452 } else {
2453 outError[0] = "Bad element under <activity>: " + parser.getName();
2454 }
2455 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 }
2458 }
2459
2460 if (!setExported) {
2461 a.info.exported = a.intents.size() > 0;
2462 }
2463
2464 return a;
2465 }
2466
2467 private Activity parseActivityAlias(Package owner, Resources res,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002468 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
2469 throws XmlPullParserException, IOException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 TypedArray sa = res.obtainAttributes(attrs,
2471 com.android.internal.R.styleable.AndroidManifestActivityAlias);
2472
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002473 String targetActivity = sa.getNonConfigurationString(
2474 com.android.internal.R.styleable.AndroidManifestActivityAlias_targetActivity, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475 if (targetActivity == null) {
2476 outError[0] = "<activity-alias> does not specify android:targetActivity";
2477 sa.recycle();
2478 return null;
2479 }
2480
2481 targetActivity = buildClassName(owner.applicationInfo.packageName,
2482 targetActivity, outError);
2483 if (targetActivity == null) {
2484 sa.recycle();
2485 return null;
2486 }
2487
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002488 if (mParseActivityAliasArgs == null) {
2489 mParseActivityAliasArgs = new ParseComponentArgs(owner, outError,
2490 com.android.internal.R.styleable.AndroidManifestActivityAlias_name,
2491 com.android.internal.R.styleable.AndroidManifestActivityAlias_label,
2492 com.android.internal.R.styleable.AndroidManifestActivityAlias_icon,
Adam Powell81cd2e92010-04-21 16:35:18 -07002493 com.android.internal.R.styleable.AndroidManifestActivityAlias_logo,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002494 mSeparateProcesses,
2495 0,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08002496 com.android.internal.R.styleable.AndroidManifestActivityAlias_description,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002497 com.android.internal.R.styleable.AndroidManifestActivityAlias_enabled);
2498 mParseActivityAliasArgs.tag = "<activity-alias>";
2499 }
2500
2501 mParseActivityAliasArgs.sa = sa;
2502 mParseActivityAliasArgs.flags = flags;
2503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 Activity target = null;
2505
2506 final int NA = owner.activities.size();
2507 for (int i=0; i<NA; i++) {
2508 Activity t = owner.activities.get(i);
2509 if (targetActivity.equals(t.info.name)) {
2510 target = t;
2511 break;
2512 }
2513 }
2514
2515 if (target == null) {
2516 outError[0] = "<activity-alias> target activity " + targetActivity
2517 + " not found in manifest";
2518 sa.recycle();
2519 return null;
2520 }
2521
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002522 ActivityInfo info = new ActivityInfo();
2523 info.targetActivity = targetActivity;
2524 info.configChanges = target.info.configChanges;
2525 info.flags = target.info.flags;
2526 info.icon = target.info.icon;
Adam Powell81cd2e92010-04-21 16:35:18 -07002527 info.logo = target.info.logo;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002528 info.labelRes = target.info.labelRes;
2529 info.nonLocalizedLabel = target.info.nonLocalizedLabel;
2530 info.launchMode = target.info.launchMode;
2531 info.processName = target.info.processName;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08002532 if (info.descriptionRes == 0) {
2533 info.descriptionRes = target.info.descriptionRes;
2534 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002535 info.screenOrientation = target.info.screenOrientation;
2536 info.taskAffinity = target.info.taskAffinity;
2537 info.theme = target.info.theme;
Dianne Hackborn0836c7c2011-10-20 18:40:23 -07002538 info.softInputMode = target.info.softInputMode;
Adam Powell269248d2011-08-02 10:26:54 -07002539 info.uiOptions = target.info.uiOptions;
Adam Powelldd8fab22012-03-22 17:47:27 -07002540 info.parentActivityName = target.info.parentActivityName;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002541
2542 Activity a = new Activity(mParseActivityAliasArgs, info);
2543 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 sa.recycle();
2545 return null;
2546 }
2547
2548 final boolean setExported = sa.hasValue(
2549 com.android.internal.R.styleable.AndroidManifestActivityAlias_exported);
2550 if (setExported) {
2551 a.info.exported = sa.getBoolean(
2552 com.android.internal.R.styleable.AndroidManifestActivityAlias_exported, false);
2553 }
2554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 String str;
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002556 str = sa.getNonConfigurationString(
2557 com.android.internal.R.styleable.AndroidManifestActivityAlias_permission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 if (str != null) {
2559 a.info.permission = str.length() > 0 ? str.toString().intern() : null;
2560 }
2561
Adam Powelldd8fab22012-03-22 17:47:27 -07002562 String parentName = sa.getNonConfigurationString(
2563 com.android.internal.R.styleable.AndroidManifestActivityAlias_parentActivityName,
2564 0);
2565 if (parentName != null) {
2566 String parentClassName = buildClassName(a.info.packageName, parentName, outError);
2567 if (outError[0] == null) {
2568 a.info.parentActivityName = parentClassName;
2569 } else {
2570 Log.e(TAG, "Activity alias " + a.info.name +
2571 " specified invalid parentActivityName " + parentName);
2572 outError[0] = null;
2573 }
2574 }
2575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 sa.recycle();
2577
2578 if (outError[0] != null) {
2579 return null;
2580 }
2581
2582 int outerDepth = parser.getDepth();
2583 int type;
2584 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2585 && (type != XmlPullParser.END_TAG
2586 || parser.getDepth() > outerDepth)) {
2587 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2588 continue;
2589 }
2590
2591 if (parser.getName().equals("intent-filter")) {
2592 ActivityIntentInfo intent = new ActivityIntentInfo(a);
2593 if (!parseIntent(res, parser, attrs, flags, intent, outError, true)) {
2594 return null;
2595 }
2596 if (intent.countActions() == 0) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002597 Slog.w(TAG, "No actions in intent filter at "
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -07002598 + mArchiveSourcePath + " "
2599 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 } else {
2601 a.intents.add(intent);
2602 }
2603 } else if (parser.getName().equals("meta-data")) {
2604 if ((a.metaData=parseMetaData(res, parser, attrs, a.metaData,
2605 outError)) == null) {
2606 return null;
2607 }
2608 } else {
2609 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002610 Slog.w(TAG, "Unknown element under <activity-alias>: " + parser.getName()
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002611 + " at " + mArchiveSourcePath + " "
2612 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 XmlUtils.skipCurrentTag(parser);
2614 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07002615 } else {
2616 outError[0] = "Bad element under <activity-alias>: " + parser.getName();
2617 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619 }
2620 }
2621
2622 if (!setExported) {
2623 a.info.exported = a.intents.size() > 0;
2624 }
2625
2626 return a;
2627 }
2628
2629 private Provider parseProvider(Package owner, Resources res,
2630 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
2631 throws XmlPullParserException, IOException {
2632 TypedArray sa = res.obtainAttributes(attrs,
2633 com.android.internal.R.styleable.AndroidManifestProvider);
2634
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002635 if (mParseProviderArgs == null) {
2636 mParseProviderArgs = new ParseComponentArgs(owner, outError,
2637 com.android.internal.R.styleable.AndroidManifestProvider_name,
2638 com.android.internal.R.styleable.AndroidManifestProvider_label,
2639 com.android.internal.R.styleable.AndroidManifestProvider_icon,
Adam Powell81cd2e92010-04-21 16:35:18 -07002640 com.android.internal.R.styleable.AndroidManifestProvider_logo,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002641 mSeparateProcesses,
2642 com.android.internal.R.styleable.AndroidManifestProvider_process,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08002643 com.android.internal.R.styleable.AndroidManifestProvider_description,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002644 com.android.internal.R.styleable.AndroidManifestProvider_enabled);
2645 mParseProviderArgs.tag = "<provider>";
2646 }
2647
2648 mParseProviderArgs.sa = sa;
2649 mParseProviderArgs.flags = flags;
2650
2651 Provider p = new Provider(mParseProviderArgs, new ProviderInfo());
2652 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 sa.recycle();
2654 return null;
2655 }
2656
Nick Kralevichf097b162012-07-28 12:43:48 -07002657 boolean providerExportedDefault = false;
2658
2659 if (owner.applicationInfo.targetSdkVersion < Build.VERSION_CODES.JELLY_BEAN_MR1) {
2660 // For compatibility, applications targeting API level 16 or lower
2661 // should have their content providers exported by default, unless they
2662 // specify otherwise.
2663 providerExportedDefault = true;
2664 }
2665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 p.info.exported = sa.getBoolean(
Nick Kralevichf097b162012-07-28 12:43:48 -07002667 com.android.internal.R.styleable.AndroidManifestProvider_exported,
2668 providerExportedDefault);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002670 String cpname = sa.getNonConfigurationString(
2671 com.android.internal.R.styleable.AndroidManifestProvider_authorities, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672
2673 p.info.isSyncable = sa.getBoolean(
2674 com.android.internal.R.styleable.AndroidManifestProvider_syncable,
2675 false);
2676
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002677 String permission = sa.getNonConfigurationString(
2678 com.android.internal.R.styleable.AndroidManifestProvider_permission, 0);
2679 String str = sa.getNonConfigurationString(
2680 com.android.internal.R.styleable.AndroidManifestProvider_readPermission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002681 if (str == null) {
2682 str = permission;
2683 }
2684 if (str == null) {
2685 p.info.readPermission = owner.applicationInfo.permission;
2686 } else {
2687 p.info.readPermission =
2688 str.length() > 0 ? str.toString().intern() : null;
2689 }
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002690 str = sa.getNonConfigurationString(
2691 com.android.internal.R.styleable.AndroidManifestProvider_writePermission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 if (str == null) {
2693 str = permission;
2694 }
2695 if (str == null) {
2696 p.info.writePermission = owner.applicationInfo.permission;
2697 } else {
2698 p.info.writePermission =
2699 str.length() > 0 ? str.toString().intern() : null;
2700 }
2701
2702 p.info.grantUriPermissions = sa.getBoolean(
2703 com.android.internal.R.styleable.AndroidManifestProvider_grantUriPermissions,
2704 false);
2705
2706 p.info.multiprocess = sa.getBoolean(
2707 com.android.internal.R.styleable.AndroidManifestProvider_multiprocess,
2708 false);
2709
2710 p.info.initOrder = sa.getInt(
2711 com.android.internal.R.styleable.AndroidManifestProvider_initOrder,
2712 0);
2713
Dianne Hackborn7d19e022012-08-07 19:12:33 -07002714 p.info.flags = 0;
2715
2716 if (sa.getBoolean(
2717 com.android.internal.R.styleable.AndroidManifestProvider_singleUser,
2718 false)) {
2719 p.info.flags |= ProviderInfo.FLAG_SINGLE_USER;
2720 if (p.info.exported) {
2721 Slog.w(TAG, "Provider exported request ignored due to singleUser: "
2722 + p.className + " at " + mArchiveSourcePath + " "
2723 + parser.getPositionDescription());
2724 p.info.exported = false;
2725 }
2726 }
2727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 sa.recycle();
2729
Dianne Hackborn54e570f2010-10-04 18:32:32 -07002730 if ((owner.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07002731 // A heavy-weight application can not have providers in its main process
2732 // We can do direct compare because we intern all strings.
2733 if (p.info.processName == owner.packageName) {
2734 outError[0] = "Heavy-weight applications can not have providers in main process";
2735 return null;
2736 }
2737 }
2738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 if (cpname == null) {
Nick Kralevichf097b162012-07-28 12:43:48 -07002740 outError[0] = "<provider> does not include authorities attribute";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 return null;
2742 }
2743 p.info.authority = cpname.intern();
2744
2745 if (!parseProviderTags(res, parser, attrs, p, outError)) {
2746 return null;
2747 }
2748
2749 return p;
2750 }
2751
2752 private boolean parseProviderTags(Resources res,
2753 XmlPullParser parser, AttributeSet attrs,
2754 Provider outInfo, String[] outError)
2755 throws XmlPullParserException, IOException {
2756 int outerDepth = parser.getDepth();
2757 int type;
2758 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
2759 && (type != XmlPullParser.END_TAG
2760 || parser.getDepth() > outerDepth)) {
2761 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
2762 continue;
2763 }
2764
2765 if (parser.getName().equals("meta-data")) {
2766 if ((outInfo.metaData=parseMetaData(res, parser, attrs,
2767 outInfo.metaData, outError)) == null) {
2768 return false;
2769 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 } else if (parser.getName().equals("grant-uri-permission")) {
2772 TypedArray sa = res.obtainAttributes(attrs,
2773 com.android.internal.R.styleable.AndroidManifestGrantUriPermission);
2774
2775 PatternMatcher pa = null;
2776
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002777 String str = sa.getNonConfigurationString(
2778 com.android.internal.R.styleable.AndroidManifestGrantUriPermission_path, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 if (str != null) {
2780 pa = new PatternMatcher(str, PatternMatcher.PATTERN_LITERAL);
2781 }
2782
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002783 str = sa.getNonConfigurationString(
2784 com.android.internal.R.styleable.AndroidManifestGrantUriPermission_pathPrefix, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 if (str != null) {
2786 pa = new PatternMatcher(str, PatternMatcher.PATTERN_PREFIX);
2787 }
2788
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002789 str = sa.getNonConfigurationString(
2790 com.android.internal.R.styleable.AndroidManifestGrantUriPermission_pathPattern, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 if (str != null) {
2792 pa = new PatternMatcher(str, PatternMatcher.PATTERN_SIMPLE_GLOB);
2793 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 sa.recycle();
2796
2797 if (pa != null) {
2798 if (outInfo.info.uriPermissionPatterns == null) {
2799 outInfo.info.uriPermissionPatterns = new PatternMatcher[1];
2800 outInfo.info.uriPermissionPatterns[0] = pa;
2801 } else {
2802 final int N = outInfo.info.uriPermissionPatterns.length;
2803 PatternMatcher[] newp = new PatternMatcher[N+1];
2804 System.arraycopy(outInfo.info.uriPermissionPatterns, 0, newp, 0, N);
2805 newp[N] = pa;
2806 outInfo.info.uriPermissionPatterns = newp;
2807 }
2808 outInfo.info.grantUriPermissions = true;
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002809 } else {
2810 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002811 Slog.w(TAG, "Unknown element under <path-permission>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002812 + parser.getName() + " at " + mArchiveSourcePath + " "
2813 + parser.getPositionDescription());
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002814 XmlUtils.skipCurrentTag(parser);
2815 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07002816 } else {
2817 outError[0] = "No path, pathPrefix, or pathPattern for <path-permission>";
2818 return false;
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002819 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002820 }
2821 XmlUtils.skipCurrentTag(parser);
2822
2823 } else if (parser.getName().equals("path-permission")) {
2824 TypedArray sa = res.obtainAttributes(attrs,
2825 com.android.internal.R.styleable.AndroidManifestPathPermission);
2826
2827 PathPermission pa = null;
2828
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002829 String permission = sa.getNonConfigurationString(
2830 com.android.internal.R.styleable.AndroidManifestPathPermission_permission, 0);
2831 String readPermission = sa.getNonConfigurationString(
2832 com.android.internal.R.styleable.AndroidManifestPathPermission_readPermission, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002833 if (readPermission == null) {
2834 readPermission = permission;
2835 }
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002836 String writePermission = sa.getNonConfigurationString(
2837 com.android.internal.R.styleable.AndroidManifestPathPermission_writePermission, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002838 if (writePermission == null) {
2839 writePermission = permission;
2840 }
2841
2842 boolean havePerm = false;
2843 if (readPermission != null) {
2844 readPermission = readPermission.intern();
2845 havePerm = true;
2846 }
2847 if (writePermission != null) {
Bjorn Bringerte04b1ad2010-02-09 13:56:08 +00002848 writePermission = writePermission.intern();
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002849 havePerm = true;
2850 }
2851
2852 if (!havePerm) {
2853 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002854 Slog.w(TAG, "No readPermission or writePermssion for <path-permission>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002855 + parser.getName() + " at " + mArchiveSourcePath + " "
2856 + parser.getPositionDescription());
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002857 XmlUtils.skipCurrentTag(parser);
2858 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07002859 } else {
2860 outError[0] = "No readPermission or writePermssion for <path-permission>";
2861 return false;
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002862 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002863 }
2864
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002865 String path = sa.getNonConfigurationString(
2866 com.android.internal.R.styleable.AndroidManifestPathPermission_path, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002867 if (path != null) {
2868 pa = new PathPermission(path,
2869 PatternMatcher.PATTERN_LITERAL, readPermission, writePermission);
2870 }
2871
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002872 path = sa.getNonConfigurationString(
2873 com.android.internal.R.styleable.AndroidManifestPathPermission_pathPrefix, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002874 if (path != null) {
2875 pa = new PathPermission(path,
2876 PatternMatcher.PATTERN_PREFIX, readPermission, writePermission);
2877 }
2878
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002879 path = sa.getNonConfigurationString(
2880 com.android.internal.R.styleable.AndroidManifestPathPermission_pathPattern, 0);
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002881 if (path != null) {
2882 pa = new PathPermission(path,
2883 PatternMatcher.PATTERN_SIMPLE_GLOB, readPermission, writePermission);
2884 }
2885
2886 sa.recycle();
2887
2888 if (pa != null) {
2889 if (outInfo.info.pathPermissions == null) {
2890 outInfo.info.pathPermissions = new PathPermission[1];
2891 outInfo.info.pathPermissions[0] = pa;
2892 } else {
2893 final int N = outInfo.info.pathPermissions.length;
2894 PathPermission[] newp = new PathPermission[N+1];
2895 System.arraycopy(outInfo.info.pathPermissions, 0, newp, 0, N);
2896 newp[N] = pa;
2897 outInfo.info.pathPermissions = newp;
2898 }
2899 } else {
2900 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002901 Slog.w(TAG, "No path, pathPrefix, or pathPattern for <path-permission>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002902 + parser.getName() + " at " + mArchiveSourcePath + " "
2903 + parser.getPositionDescription());
Dianne Hackborn2af632f2009-07-08 14:56:37 -07002904 XmlUtils.skipCurrentTag(parser);
2905 continue;
2906 }
2907 outError[0] = "No path, pathPrefix, or pathPattern for <path-permission>";
2908 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 }
2910 XmlUtils.skipCurrentTag(parser);
2911
2912 } else {
2913 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07002914 Slog.w(TAG, "Unknown element under <provider>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07002915 + parser.getName() + " at " + mArchiveSourcePath + " "
2916 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 XmlUtils.skipCurrentTag(parser);
2918 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07002919 } else {
2920 outError[0] = "Bad element under <provider>: " + parser.getName();
2921 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 }
2924 }
2925 return true;
2926 }
2927
2928 private Service parseService(Package owner, Resources res,
2929 XmlPullParser parser, AttributeSet attrs, int flags, String[] outError)
2930 throws XmlPullParserException, IOException {
2931 TypedArray sa = res.obtainAttributes(attrs,
2932 com.android.internal.R.styleable.AndroidManifestService);
2933
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002934 if (mParseServiceArgs == null) {
2935 mParseServiceArgs = new ParseComponentArgs(owner, outError,
2936 com.android.internal.R.styleable.AndroidManifestService_name,
2937 com.android.internal.R.styleable.AndroidManifestService_label,
2938 com.android.internal.R.styleable.AndroidManifestService_icon,
Adam Powell81cd2e92010-04-21 16:35:18 -07002939 com.android.internal.R.styleable.AndroidManifestService_logo,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002940 mSeparateProcesses,
2941 com.android.internal.R.styleable.AndroidManifestService_process,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08002942 com.android.internal.R.styleable.AndroidManifestService_description,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07002943 com.android.internal.R.styleable.AndroidManifestService_enabled);
2944 mParseServiceArgs.tag = "<service>";
2945 }
2946
2947 mParseServiceArgs.sa = sa;
2948 mParseServiceArgs.flags = flags;
2949
2950 Service s = new Service(mParseServiceArgs, new ServiceInfo());
2951 if (outError[0] != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 sa.recycle();
2953 return null;
2954 }
2955
Dianne Hackbornb4163a62012-08-02 18:31:26 -07002956 boolean setExported = sa.hasValue(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 com.android.internal.R.styleable.AndroidManifestService_exported);
2958 if (setExported) {
2959 s.info.exported = sa.getBoolean(
2960 com.android.internal.R.styleable.AndroidManifestService_exported, false);
2961 }
2962
Dianne Hackborncf244ad2010-03-09 15:00:30 -08002963 String str = sa.getNonConfigurationString(
2964 com.android.internal.R.styleable.AndroidManifestService_permission, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965 if (str == null) {
2966 s.info.permission = owner.applicationInfo.permission;
2967 } else {
2968 s.info.permission = str.length() > 0 ? str.toString().intern() : null;
2969 }
2970
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002971 s.info.flags = 0;
2972 if (sa.getBoolean(
2973 com.android.internal.R.styleable.AndroidManifestService_stopWithTask,
2974 false)) {
2975 s.info.flags |= ServiceInfo.FLAG_STOP_WITH_TASK;
2976 }
Dianne Hackborna0c283e2012-02-09 10:47:01 -08002977 if (sa.getBoolean(
2978 com.android.internal.R.styleable.AndroidManifestService_isolatedProcess,
2979 false)) {
2980 s.info.flags |= ServiceInfo.FLAG_ISOLATED_PROCESS;
2981 }
Dianne Hackbornb4163a62012-08-02 18:31:26 -07002982 if (sa.getBoolean(
2983 com.android.internal.R.styleable.AndroidManifestService_singleUser,
2984 false)) {
2985 s.info.flags |= ServiceInfo.FLAG_SINGLE_USER;
2986 if (s.info.exported) {
2987 Slog.w(TAG, "Service exported request ignored due to singleUser: "
2988 + s.className + " at " + mArchiveSourcePath + " "
2989 + parser.getPositionDescription());
2990 s.info.exported = false;
2991 }
2992 setExported = true;
2993 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002995 sa.recycle();
2996
Dianne Hackborn54e570f2010-10-04 18:32:32 -07002997 if ((owner.applicationInfo.flags&ApplicationInfo.FLAG_CANT_SAVE_STATE) != 0) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07002998 // A heavy-weight application can not have services in its main process
2999 // We can do direct compare because we intern all strings.
3000 if (s.info.processName == owner.packageName) {
3001 outError[0] = "Heavy-weight applications can not have services in main process";
3002 return null;
3003 }
3004 }
3005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003006 int outerDepth = parser.getDepth();
3007 int type;
3008 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
3009 && (type != XmlPullParser.END_TAG
3010 || parser.getDepth() > outerDepth)) {
3011 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
3012 continue;
3013 }
3014
3015 if (parser.getName().equals("intent-filter")) {
3016 ServiceIntentInfo intent = new ServiceIntentInfo(s);
3017 if (!parseIntent(res, parser, attrs, flags, intent, outError, false)) {
3018 return null;
3019 }
3020
3021 s.intents.add(intent);
3022 } else if (parser.getName().equals("meta-data")) {
3023 if ((s.metaData=parseMetaData(res, parser, attrs, s.metaData,
3024 outError)) == null) {
3025 return null;
3026 }
3027 } else {
3028 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07003029 Slog.w(TAG, "Unknown element under <service>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07003030 + parser.getName() + " at " + mArchiveSourcePath + " "
3031 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003032 XmlUtils.skipCurrentTag(parser);
3033 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07003034 } else {
3035 outError[0] = "Bad element under <service>: " + parser.getName();
3036 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003037 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003038 }
3039 }
3040
3041 if (!setExported) {
3042 s.info.exported = s.intents.size() > 0;
3043 }
3044
3045 return s;
3046 }
3047
3048 private boolean parseAllMetaData(Resources res,
3049 XmlPullParser parser, AttributeSet attrs, String tag,
3050 Component outInfo, String[] outError)
3051 throws XmlPullParserException, IOException {
3052 int outerDepth = parser.getDepth();
3053 int type;
3054 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
3055 && (type != XmlPullParser.END_TAG
3056 || parser.getDepth() > outerDepth)) {
3057 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
3058 continue;
3059 }
3060
3061 if (parser.getName().equals("meta-data")) {
3062 if ((outInfo.metaData=parseMetaData(res, parser, attrs,
3063 outInfo.metaData, outError)) == null) {
3064 return false;
3065 }
3066 } else {
3067 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07003068 Slog.w(TAG, "Unknown element under " + tag + ": "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07003069 + parser.getName() + " at " + mArchiveSourcePath + " "
3070 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003071 XmlUtils.skipCurrentTag(parser);
3072 continue;
Kenny Rootd2d29252011-08-08 11:27:57 -07003073 } else {
3074 outError[0] = "Bad element under " + tag + ": " + parser.getName();
3075 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003077 }
3078 }
3079 return true;
3080 }
3081
3082 private Bundle parseMetaData(Resources res,
3083 XmlPullParser parser, AttributeSet attrs,
3084 Bundle data, String[] outError)
3085 throws XmlPullParserException, IOException {
3086
3087 TypedArray sa = res.obtainAttributes(attrs,
3088 com.android.internal.R.styleable.AndroidManifestMetaData);
3089
3090 if (data == null) {
3091 data = new Bundle();
3092 }
3093
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003094 String name = sa.getNonConfigurationString(
3095 com.android.internal.R.styleable.AndroidManifestMetaData_name, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003096 if (name == null) {
3097 outError[0] = "<meta-data> requires an android:name attribute";
3098 sa.recycle();
3099 return null;
3100 }
3101
Dianne Hackborn854060a2009-07-09 18:14:31 -07003102 name = name.intern();
3103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 TypedValue v = sa.peekValue(
3105 com.android.internal.R.styleable.AndroidManifestMetaData_resource);
3106 if (v != null && v.resourceId != 0) {
Kenny Rootd2d29252011-08-08 11:27:57 -07003107 //Slog.i(TAG, "Meta data ref " + name + ": " + v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003108 data.putInt(name, v.resourceId);
3109 } else {
3110 v = sa.peekValue(
3111 com.android.internal.R.styleable.AndroidManifestMetaData_value);
Kenny Rootd2d29252011-08-08 11:27:57 -07003112 //Slog.i(TAG, "Meta data " + name + ": " + v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003113 if (v != null) {
3114 if (v.type == TypedValue.TYPE_STRING) {
3115 CharSequence cs = v.coerceToString();
Dianne Hackborn854060a2009-07-09 18:14:31 -07003116 data.putString(name, cs != null ? cs.toString().intern() : null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003117 } else if (v.type == TypedValue.TYPE_INT_BOOLEAN) {
3118 data.putBoolean(name, v.data != 0);
3119 } else if (v.type >= TypedValue.TYPE_FIRST_INT
3120 && v.type <= TypedValue.TYPE_LAST_INT) {
3121 data.putInt(name, v.data);
3122 } else if (v.type == TypedValue.TYPE_FLOAT) {
3123 data.putFloat(name, v.getFloat());
3124 } else {
3125 if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07003126 Slog.w(TAG, "<meta-data> only supports string, integer, float, color, boolean, and resource reference types: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07003127 + parser.getName() + " at " + mArchiveSourcePath + " "
3128 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003129 } else {
3130 outError[0] = "<meta-data> only supports string, integer, float, color, boolean, and resource reference types";
3131 data = null;
3132 }
3133 }
3134 } else {
3135 outError[0] = "<meta-data> requires an android:value or android:resource attribute";
3136 data = null;
3137 }
3138 }
3139
3140 sa.recycle();
3141
3142 XmlUtils.skipCurrentTag(parser);
3143
3144 return data;
3145 }
3146
Kenny Root05ca4c92011-09-15 10:36:25 -07003147 private static VerifierInfo parseVerifier(Resources res, XmlPullParser parser,
3148 AttributeSet attrs, int flags, String[] outError) throws XmlPullParserException,
3149 IOException {
3150 final TypedArray sa = res.obtainAttributes(attrs,
3151 com.android.internal.R.styleable.AndroidManifestPackageVerifier);
3152
3153 final String packageName = sa.getNonResourceString(
3154 com.android.internal.R.styleable.AndroidManifestPackageVerifier_name);
3155
3156 final String encodedPublicKey = sa.getNonResourceString(
3157 com.android.internal.R.styleable.AndroidManifestPackageVerifier_publicKey);
3158
3159 sa.recycle();
3160
3161 if (packageName == null || packageName.length() == 0) {
3162 Slog.i(TAG, "verifier package name was null; skipping");
3163 return null;
3164 } else if (encodedPublicKey == null) {
3165 Slog.i(TAG, "verifier " + packageName + " public key was null; skipping");
3166 }
3167
Geremy Condraf1bcca82013-01-07 22:35:24 -08003168 PublicKey publicKey = parsePublicKey(encodedPublicKey);
3169 if (publicKey != null) {
3170 return new VerifierInfo(packageName, publicKey);
3171 }
3172
3173 return null;
3174 }
3175
3176 public static final PublicKey parsePublicKey(String encodedPublicKey) {
Kenny Root05ca4c92011-09-15 10:36:25 -07003177 EncodedKeySpec keySpec;
3178 try {
3179 final byte[] encoded = Base64.decode(encodedPublicKey, Base64.DEFAULT);
3180 keySpec = new X509EncodedKeySpec(encoded);
3181 } catch (IllegalArgumentException e) {
Geremy Condraf1bcca82013-01-07 22:35:24 -08003182 Slog.i(TAG, "Could not parse verifier public key; invalid Base64");
Kenny Root05ca4c92011-09-15 10:36:25 -07003183 return null;
3184 }
3185
3186 /* First try the key as an RSA key. */
3187 try {
3188 final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
Geremy Condraf1bcca82013-01-07 22:35:24 -08003189 return keyFactory.generatePublic(keySpec);
Kenny Root05ca4c92011-09-15 10:36:25 -07003190 } catch (NoSuchAlgorithmException e) {
3191 Log.wtf(TAG, "Could not parse public key because RSA isn't included in build");
3192 return null;
3193 } catch (InvalidKeySpecException e) {
3194 // Not a RSA public key.
3195 }
3196
3197 /* Now try it as a DSA key. */
3198 try {
3199 final KeyFactory keyFactory = KeyFactory.getInstance("DSA");
Geremy Condraf1bcca82013-01-07 22:35:24 -08003200 return keyFactory.generatePublic(keySpec);
Kenny Root05ca4c92011-09-15 10:36:25 -07003201 } catch (NoSuchAlgorithmException e) {
3202 Log.wtf(TAG, "Could not parse public key because DSA isn't included in build");
3203 return null;
3204 } catch (InvalidKeySpecException e) {
3205 // Not a DSA public key.
3206 }
3207
3208 return null;
3209 }
3210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 private static final String ANDROID_RESOURCES
3212 = "http://schemas.android.com/apk/res/android";
3213
3214 private boolean parseIntent(Resources res,
3215 XmlPullParser parser, AttributeSet attrs, int flags,
3216 IntentInfo outInfo, String[] outError, boolean isActivity)
3217 throws XmlPullParserException, IOException {
3218
3219 TypedArray sa = res.obtainAttributes(attrs,
3220 com.android.internal.R.styleable.AndroidManifestIntentFilter);
3221
3222 int priority = sa.getInt(
3223 com.android.internal.R.styleable.AndroidManifestIntentFilter_priority, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 outInfo.setPriority(priority);
Kenny Root502e9a42011-01-10 13:48:15 -08003225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003226 TypedValue v = sa.peekValue(
3227 com.android.internal.R.styleable.AndroidManifestIntentFilter_label);
3228 if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
3229 outInfo.nonLocalizedLabel = v.coerceToString();
3230 }
3231
3232 outInfo.icon = sa.getResourceId(
3233 com.android.internal.R.styleable.AndroidManifestIntentFilter_icon, 0);
Adam Powell81cd2e92010-04-21 16:35:18 -07003234
3235 outInfo.logo = sa.getResourceId(
3236 com.android.internal.R.styleable.AndroidManifestIntentFilter_logo, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237
3238 sa.recycle();
3239
3240 int outerDepth = parser.getDepth();
3241 int type;
Kenny Rootd2d29252011-08-08 11:27:57 -07003242 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
3243 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
3244 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245 continue;
3246 }
3247
3248 String nodeName = parser.getName();
3249 if (nodeName.equals("action")) {
3250 String value = attrs.getAttributeValue(
3251 ANDROID_RESOURCES, "name");
3252 if (value == null || value == "") {
3253 outError[0] = "No value supplied for <android:name>";
3254 return false;
3255 }
3256 XmlUtils.skipCurrentTag(parser);
3257
3258 outInfo.addAction(value);
3259 } else if (nodeName.equals("category")) {
3260 String value = attrs.getAttributeValue(
3261 ANDROID_RESOURCES, "name");
3262 if (value == null || value == "") {
3263 outError[0] = "No value supplied for <android:name>";
3264 return false;
3265 }
3266 XmlUtils.skipCurrentTag(parser);
3267
3268 outInfo.addCategory(value);
3269
3270 } else if (nodeName.equals("data")) {
3271 sa = res.obtainAttributes(attrs,
3272 com.android.internal.R.styleable.AndroidManifestData);
3273
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003274 String str = sa.getNonConfigurationString(
3275 com.android.internal.R.styleable.AndroidManifestData_mimeType, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 if (str != null) {
3277 try {
3278 outInfo.addDataType(str);
3279 } catch (IntentFilter.MalformedMimeTypeException e) {
3280 outError[0] = e.toString();
3281 sa.recycle();
3282 return false;
3283 }
3284 }
3285
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003286 str = sa.getNonConfigurationString(
3287 com.android.internal.R.styleable.AndroidManifestData_scheme, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003288 if (str != null) {
3289 outInfo.addDataScheme(str);
3290 }
3291
Dianne Hackborndf1c0bf2013-06-12 16:21:38 -07003292 str = sa.getNonConfigurationString(
3293 com.android.internal.R.styleable.AndroidManifestData_ssp, 0);
3294 if (str != null) {
3295 outInfo.addDataSchemeSpecificPart(str, PatternMatcher.PATTERN_LITERAL);
3296 }
3297
3298 str = sa.getNonConfigurationString(
3299 com.android.internal.R.styleable.AndroidManifestData_sspPrefix, 0);
3300 if (str != null) {
3301 outInfo.addDataSchemeSpecificPart(str, PatternMatcher.PATTERN_PREFIX);
3302 }
3303
3304 str = sa.getNonConfigurationString(
3305 com.android.internal.R.styleable.AndroidManifestData_sspPattern, 0);
3306 if (str != null) {
3307 outInfo.addDataSchemeSpecificPart(str, PatternMatcher.PATTERN_SIMPLE_GLOB);
3308 }
3309
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003310 String host = sa.getNonConfigurationString(
3311 com.android.internal.R.styleable.AndroidManifestData_host, 0);
3312 String port = sa.getNonConfigurationString(
3313 com.android.internal.R.styleable.AndroidManifestData_port, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003314 if (host != null) {
3315 outInfo.addDataAuthority(host, port);
3316 }
3317
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003318 str = sa.getNonConfigurationString(
3319 com.android.internal.R.styleable.AndroidManifestData_path, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003320 if (str != null) {
3321 outInfo.addDataPath(str, PatternMatcher.PATTERN_LITERAL);
3322 }
3323
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003324 str = sa.getNonConfigurationString(
3325 com.android.internal.R.styleable.AndroidManifestData_pathPrefix, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003326 if (str != null) {
3327 outInfo.addDataPath(str, PatternMatcher.PATTERN_PREFIX);
3328 }
3329
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003330 str = sa.getNonConfigurationString(
3331 com.android.internal.R.styleable.AndroidManifestData_pathPattern, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332 if (str != null) {
3333 outInfo.addDataPath(str, PatternMatcher.PATTERN_SIMPLE_GLOB);
3334 }
3335
3336 sa.recycle();
3337 XmlUtils.skipCurrentTag(parser);
3338 } else if (!RIGID_PARSER) {
Kenny Rootd2d29252011-08-08 11:27:57 -07003339 Slog.w(TAG, "Unknown element under <intent-filter>: "
Dianne Hackborna33e3f72009-09-29 17:28:24 -07003340 + parser.getName() + " at " + mArchiveSourcePath + " "
3341 + parser.getPositionDescription());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003342 XmlUtils.skipCurrentTag(parser);
3343 } else {
3344 outError[0] = "Bad element under <intent-filter>: " + parser.getName();
3345 return false;
3346 }
3347 }
3348
3349 outInfo.hasDefault = outInfo.hasCategory(Intent.CATEGORY_DEFAULT);
Kenny Rootd2d29252011-08-08 11:27:57 -07003350
3351 if (DEBUG_PARSER) {
3352 final StringBuilder cats = new StringBuilder("Intent d=");
3353 cats.append(outInfo.hasDefault);
3354 cats.append(", cat=");
3355
3356 final Iterator<String> it = outInfo.categoriesIterator();
3357 if (it != null) {
3358 while (it.hasNext()) {
3359 cats.append(' ');
3360 cats.append(it.next());
3361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 }
Kenny Rootd2d29252011-08-08 11:27:57 -07003363 Slog.d(TAG, cats.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 }
3365
3366 return true;
3367 }
3368
3369 public final static class Package {
Amith Yamasani0ac1fc92013-03-27 18:56:08 -07003370
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003371 public String packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372
3373 // For now we only support one application per package.
3374 public final ApplicationInfo applicationInfo = new ApplicationInfo();
3375
3376 public final ArrayList<Permission> permissions = new ArrayList<Permission>(0);
3377 public final ArrayList<PermissionGroup> permissionGroups = new ArrayList<PermissionGroup>(0);
3378 public final ArrayList<Activity> activities = new ArrayList<Activity>(0);
3379 public final ArrayList<Activity> receivers = new ArrayList<Activity>(0);
3380 public final ArrayList<Provider> providers = new ArrayList<Provider>(0);
3381 public final ArrayList<Service> services = new ArrayList<Service>(0);
3382 public final ArrayList<Instrumentation> instrumentation = new ArrayList<Instrumentation>(0);
3383
3384 public final ArrayList<String> requestedPermissions = new ArrayList<String>();
Dianne Hackborne639da72012-02-21 15:11:13 -08003385 public final ArrayList<Boolean> requestedPermissionsRequired = new ArrayList<Boolean>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003386
Dianne Hackborn854060a2009-07-09 18:14:31 -07003387 public ArrayList<String> protectedBroadcasts;
Dianne Hackbornc895be72013-03-11 17:48:43 -07003388
3389 public ArrayList<String> libraryNames = null;
Dianne Hackborn49237342009-08-27 20:08:01 -07003390 public ArrayList<String> usesLibraries = null;
3391 public ArrayList<String> usesOptionalLibraries = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 public String[] usesLibraryFiles = null;
3393
Dianne Hackbornc1552392010-03-03 16:19:01 -08003394 public ArrayList<String> mOriginalPackages = null;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003395 public String mRealPackage = null;
Dianne Hackbornb858dfd2010-02-02 10:49:14 -08003396 public ArrayList<String> mAdoptPermissions = null;
3397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003398 // We store the application meta-data independently to avoid multiple unwanted references
3399 public Bundle mAppMetaData = null;
3400
3401 // If this is a 3rd party app, this is the path of the zip file.
3402 public String mPath;
3403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 // The version code declared for this package.
3405 public int mVersionCode;
3406
3407 // The version name declared for this package.
3408 public String mVersionName;
3409
3410 // The shared user id that this package wants to use.
3411 public String mSharedUserId;
3412
3413 // The shared user label that this package wants to use.
3414 public int mSharedUserLabel;
3415
3416 // Signatures that were read from the package.
3417 public Signature mSignatures[];
3418
3419 // For use by package manager service for quick lookup of
3420 // preferred up order.
3421 public int mPreferredOrder = 0;
3422
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07003423 // For use by the package manager to keep track of the path to the
3424 // file an app came from.
3425 public String mScanPath;
3426
3427 // For use by package manager to keep track of where it has done dexopt.
3428 public boolean mDidDexOpt;
3429
Amith Yamasani13593602012-03-22 16:16:17 -07003430 // // User set enabled state.
3431 // public int mSetEnabled = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
3432 //
3433 // // Whether the package has been stopped.
3434 // public boolean mSetStopped = false;
Dianne Hackborne7f97212011-02-24 14:40:20 -08003435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003436 // Additional data supplied by callers.
3437 public Object mExtras;
Kenny Rootdeb11262010-08-02 11:36:21 -07003438
3439 // Whether an operation is currently pending on this package
3440 public boolean mOperationPending;
3441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 /*
3443 * Applications hardware preferences
3444 */
3445 public final ArrayList<ConfigurationInfo> configPreferences =
3446 new ArrayList<ConfigurationInfo>();
3447
Dianne Hackborn49237342009-08-27 20:08:01 -07003448 /*
3449 * Applications requested features
3450 */
3451 public ArrayList<FeatureInfo> reqFeatures = null;
3452
Suchi Amalapurapu117818e2010-02-09 03:45:40 -08003453 public int installLocation;
3454
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08003455 /* An app that's required for all users and cannot be uninstalled for a user */
3456 public boolean mRequiredForAllUsers;
3457
Amith Yamasani0ac1fc92013-03-27 18:56:08 -07003458 /* The restricted account authenticator type that is used by this application */
3459 public String mRestrictedAccountType;
3460
Amith Yamasaniccbe3892013-04-12 17:52:42 -07003461 /* The required account type without which this application will not function */
3462 public String mRequiredAccountType;
3463
Kenny Rootbcc954d2011-08-08 16:19:08 -07003464 /**
3465 * Digest suitable for comparing whether this package's manifest is the
3466 * same as another.
3467 */
3468 public ManifestDigest manifestDigest;
3469
Geremy Condraf1bcca82013-01-07 22:35:24 -08003470 /**
3471 * Data used to feed the KeySetManager
3472 */
3473 public Set<PublicKey> mSigningKeys;
3474 public Map<String, Set<PublicKey>> mKeySetMapping;
3475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003476 public Package(String _name) {
3477 packageName = _name;
3478 applicationInfo.packageName = _name;
3479 applicationInfo.uid = -1;
3480 }
3481
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003482 public void setPackageName(String newName) {
3483 packageName = newName;
3484 applicationInfo.packageName = newName;
3485 for (int i=permissions.size()-1; i>=0; i--) {
3486 permissions.get(i).setPackageName(newName);
3487 }
3488 for (int i=permissionGroups.size()-1; i>=0; i--) {
3489 permissionGroups.get(i).setPackageName(newName);
3490 }
3491 for (int i=activities.size()-1; i>=0; i--) {
3492 activities.get(i).setPackageName(newName);
3493 }
3494 for (int i=receivers.size()-1; i>=0; i--) {
3495 receivers.get(i).setPackageName(newName);
3496 }
3497 for (int i=providers.size()-1; i>=0; i--) {
3498 providers.get(i).setPackageName(newName);
3499 }
3500 for (int i=services.size()-1; i>=0; i--) {
3501 services.get(i).setPackageName(newName);
3502 }
3503 for (int i=instrumentation.size()-1; i>=0; i--) {
3504 instrumentation.get(i).setPackageName(newName);
3505 }
3506 }
Dianne Hackborn65696252012-03-05 18:49:21 -08003507
3508 public boolean hasComponentClassName(String name) {
3509 for (int i=activities.size()-1; i>=0; i--) {
3510 if (name.equals(activities.get(i).className)) {
3511 return true;
3512 }
3513 }
3514 for (int i=receivers.size()-1; i>=0; i--) {
3515 if (name.equals(receivers.get(i).className)) {
3516 return true;
3517 }
3518 }
3519 for (int i=providers.size()-1; i>=0; i--) {
3520 if (name.equals(providers.get(i).className)) {
3521 return true;
3522 }
3523 }
3524 for (int i=services.size()-1; i>=0; i--) {
3525 if (name.equals(services.get(i).className)) {
3526 return true;
3527 }
3528 }
3529 for (int i=instrumentation.size()-1; i>=0; i--) {
3530 if (name.equals(instrumentation.get(i).className)) {
3531 return true;
3532 }
3533 }
3534 return false;
3535 }
3536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003537 public String toString() {
3538 return "Package{"
3539 + Integer.toHexString(System.identityHashCode(this))
3540 + " " + packageName + "}";
3541 }
3542 }
3543
3544 public static class Component<II extends IntentInfo> {
3545 public final Package owner;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003546 public final ArrayList<II> intents;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003547 public final String className;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003548 public Bundle metaData;
3549
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003550 ComponentName componentName;
3551 String componentShortName;
3552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 public Component(Package _owner) {
3554 owner = _owner;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003555 intents = null;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003556 className = null;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003557 }
3558
3559 public Component(final ParsePackageItemArgs args, final PackageItemInfo outInfo) {
3560 owner = args.owner;
3561 intents = new ArrayList<II>(0);
Dianne Hackborncf244ad2010-03-09 15:00:30 -08003562 String name = args.sa.getNonConfigurationString(args.nameRes, 0);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003563 if (name == null) {
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003564 className = null;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003565 args.outError[0] = args.tag + " does not specify android:name";
3566 return;
3567 }
3568
3569 outInfo.name
3570 = buildClassName(owner.applicationInfo.packageName, name, args.outError);
3571 if (outInfo.name == null) {
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003572 className = null;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003573 args.outError[0] = args.tag + " does not have valid android:name";
3574 return;
3575 }
3576
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003577 className = outInfo.name;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003578
3579 int iconVal = args.sa.getResourceId(args.iconRes, 0);
3580 if (iconVal != 0) {
3581 outInfo.icon = iconVal;
3582 outInfo.nonLocalizedLabel = null;
3583 }
Adam Powell81cd2e92010-04-21 16:35:18 -07003584
3585 int logoVal = args.sa.getResourceId(args.logoRes, 0);
3586 if (logoVal != 0) {
3587 outInfo.logo = logoVal;
3588 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003589
3590 TypedValue v = args.sa.peekValue(args.labelRes);
3591 if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
3592 outInfo.nonLocalizedLabel = v.coerceToString();
3593 }
3594
3595 outInfo.packageName = owner.packageName;
3596 }
3597
3598 public Component(final ParseComponentArgs args, final ComponentInfo outInfo) {
3599 this(args, (PackageItemInfo)outInfo);
3600 if (args.outError[0] != null) {
3601 return;
3602 }
3603
3604 if (args.processRes != 0) {
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07003605 CharSequence pname;
3606 if (owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.FROYO) {
3607 pname = args.sa.getNonConfigurationString(args.processRes, 0);
3608 } else {
3609 // Some older apps have been seen to use a resource reference
3610 // here that on older builds was ignored (with a warning). We
3611 // need to continue to do this for them so they don't break.
3612 pname = args.sa.getNonResourceString(args.processRes);
3613 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003614 outInfo.processName = buildProcessName(owner.applicationInfo.packageName,
Dianne Hackbornd1cff1b2010-04-02 16:51:26 -07003615 owner.applicationInfo.processName, pname,
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003616 args.flags, args.sepProcesses, args.outError);
3617 }
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08003618
3619 if (args.descriptionRes != 0) {
3620 outInfo.descriptionRes = args.sa.getResourceId(args.descriptionRes, 0);
3621 }
3622
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003623 outInfo.enabled = args.sa.getBoolean(args.enabledRes, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003624 }
3625
3626 public Component(Component<II> clone) {
3627 owner = clone.owner;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003628 intents = clone.intents;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003629 className = clone.className;
3630 componentName = clone.componentName;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003631 componentShortName = clone.componentShortName;
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003632 }
3633
3634 public ComponentName getComponentName() {
3635 if (componentName != null) {
3636 return componentName;
3637 }
3638 if (className != null) {
3639 componentName = new ComponentName(owner.applicationInfo.packageName,
3640 className);
3641 }
3642 return componentName;
3643 }
3644
3645 public String getComponentShortName() {
3646 if (componentShortName != null) {
3647 return componentShortName;
3648 }
3649 ComponentName component = getComponentName();
3650 if (component != null) {
3651 componentShortName = component.flattenToShortString();
3652 }
3653 return componentShortName;
3654 }
3655
3656 public void setPackageName(String packageName) {
3657 componentName = null;
3658 componentShortName = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003659 }
3660 }
3661
3662 public final static class Permission extends Component<IntentInfo> {
3663 public final PermissionInfo info;
3664 public boolean tree;
3665 public PermissionGroup group;
3666
3667 public Permission(Package _owner) {
3668 super(_owner);
3669 info = new PermissionInfo();
3670 }
3671
3672 public Permission(Package _owner, PermissionInfo _info) {
3673 super(_owner);
3674 info = _info;
3675 }
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003676
3677 public void setPackageName(String packageName) {
3678 super.setPackageName(packageName);
3679 info.packageName = packageName;
3680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003681
3682 public String toString() {
3683 return "Permission{"
3684 + Integer.toHexString(System.identityHashCode(this))
3685 + " " + info.name + "}";
3686 }
3687 }
3688
3689 public final static class PermissionGroup extends Component<IntentInfo> {
3690 public final PermissionGroupInfo info;
3691
3692 public PermissionGroup(Package _owner) {
3693 super(_owner);
3694 info = new PermissionGroupInfo();
3695 }
3696
3697 public PermissionGroup(Package _owner, PermissionGroupInfo _info) {
3698 super(_owner);
3699 info = _info;
3700 }
3701
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003702 public void setPackageName(String packageName) {
3703 super.setPackageName(packageName);
3704 info.packageName = packageName;
3705 }
3706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003707 public String toString() {
3708 return "PermissionGroup{"
3709 + Integer.toHexString(System.identityHashCode(this))
3710 + " " + info.name + "}";
3711 }
3712 }
3713
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003714 private static boolean copyNeeded(int flags, Package p,
3715 PackageUserState state, Bundle metaData, int userId) {
3716 if (userId != 0) {
3717 // We always need to copy for other users, since we need
3718 // to fix up the uid.
3719 return true;
3720 }
3721 if (state.enabled != PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
3722 boolean enabled = state.enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
Dianne Hackborn46730fc2010-07-24 16:32:42 -07003723 if (p.applicationInfo.enabled != enabled) {
3724 return true;
3725 }
3726 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003727 if (!state.installed) {
3728 return true;
3729 }
3730 if (state.stopped) {
3731 return true;
3732 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003733 if ((flags & PackageManager.GET_META_DATA) != 0
3734 && (metaData != null || p.mAppMetaData != null)) {
3735 return true;
3736 }
3737 if ((flags & PackageManager.GET_SHARED_LIBRARY_FILES) != 0
3738 && p.usesLibraryFiles != null) {
3739 return true;
3740 }
3741 return false;
3742 }
3743
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003744 public static ApplicationInfo generateApplicationInfo(Package p, int flags,
3745 PackageUserState state) {
3746 return generateApplicationInfo(p, flags, state, UserHandle.getCallingUserId());
Amith Yamasani742a6712011-05-04 14:49:28 -07003747 }
3748
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003749 private static void updateApplicationInfo(ApplicationInfo ai, int flags,
3750 PackageUserState state) {
3751 // CompatibilityMode is global state.
3752 if (!sCompatibilityModeEnabled) {
3753 ai.disableCompatibilityMode();
3754 }
3755 if (state.installed) {
3756 ai.flags |= ApplicationInfo.FLAG_INSTALLED;
3757 } else {
3758 ai.flags &= ~ApplicationInfo.FLAG_INSTALLED;
3759 }
3760 if (state.enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
3761 ai.enabled = true;
3762 } else if (state.enabled == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
3763 ai.enabled = (flags&PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS) != 0;
3764 } else if (state.enabled == PackageManager.COMPONENT_ENABLED_STATE_DISABLED
3765 || state.enabled == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER) {
3766 ai.enabled = false;
3767 }
3768 ai.enabledSetting = state.enabled;
3769 }
3770
Amith Yamasani13593602012-03-22 16:16:17 -07003771 public static ApplicationInfo generateApplicationInfo(Package p, int flags,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003772 PackageUserState state, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003773 if (p == null) return null;
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003774 if (!checkUseInstalled(flags, state)) {
3775 return null;
3776 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003777 if (!copyNeeded(flags, p, state, null, userId)
3778 && ((flags&PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS) == 0
3779 || state.enabled != PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED)) {
3780 // In this case it is safe to directly modify the internal ApplicationInfo state:
3781 // - CompatibilityMode is global state, so will be the same for every call.
3782 // - We only come in to here if the app should reported as installed; this is the
3783 // default state, and we will do a copy otherwise.
3784 // - The enable state will always be reported the same for the application across
3785 // calls; the only exception is for the UNTIL_USED mode, and in that case we will
3786 // be doing a copy.
3787 updateApplicationInfo(p.applicationInfo, flags, state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003788 return p.applicationInfo;
3789 }
3790
3791 // Make shallow copy so we can store the metadata/libraries safely
3792 ApplicationInfo ai = new ApplicationInfo(p.applicationInfo);
Amith Yamasani742a6712011-05-04 14:49:28 -07003793 if (userId != 0) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07003794 ai.uid = UserHandle.getUid(userId, ai.uid);
Amith Yamasani742a6712011-05-04 14:49:28 -07003795 ai.dataDir = PackageManager.getDataDirForUser(userId, ai.packageName);
3796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003797 if ((flags & PackageManager.GET_META_DATA) != 0) {
3798 ai.metaData = p.mAppMetaData;
3799 }
3800 if ((flags & PackageManager.GET_SHARED_LIBRARY_FILES) != 0) {
3801 ai.sharedLibraryFiles = p.usesLibraryFiles;
3802 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003803 if (state.stopped) {
Amith Yamasania4a54e22012-04-16 15:44:19 -07003804 ai.flags |= ApplicationInfo.FLAG_STOPPED;
Dianne Hackborne7f97212011-02-24 14:40:20 -08003805 } else {
Amith Yamasania4a54e22012-04-16 15:44:19 -07003806 ai.flags &= ~ApplicationInfo.FLAG_STOPPED;
Dianne Hackborne7f97212011-02-24 14:40:20 -08003807 }
Dianne Hackbornfd7aded2013-01-22 17:10:23 -08003808 updateApplicationInfo(ai, flags, state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003809 return ai;
3810 }
3811
3812 public static final PermissionInfo generatePermissionInfo(
3813 Permission p, int flags) {
3814 if (p == null) return null;
3815 if ((flags&PackageManager.GET_META_DATA) == 0) {
3816 return p.info;
3817 }
3818 PermissionInfo pi = new PermissionInfo(p.info);
3819 pi.metaData = p.metaData;
3820 return pi;
3821 }
3822
3823 public static final PermissionGroupInfo generatePermissionGroupInfo(
3824 PermissionGroup pg, int flags) {
3825 if (pg == null) return null;
3826 if ((flags&PackageManager.GET_META_DATA) == 0) {
3827 return pg.info;
3828 }
3829 PermissionGroupInfo pgi = new PermissionGroupInfo(pg.info);
3830 pgi.metaData = pg.metaData;
3831 return pgi;
3832 }
3833
3834 public final static class Activity extends Component<ActivityIntentInfo> {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003835 public final ActivityInfo info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003836
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003837 public Activity(final ParseComponentArgs args, final ActivityInfo _info) {
3838 super(args, _info);
3839 info = _info;
3840 info.applicationInfo = args.owner.applicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003841 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003842
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003843 public void setPackageName(String packageName) {
3844 super.setPackageName(packageName);
3845 info.packageName = packageName;
3846 }
3847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003848 public String toString() {
3849 return "Activity{"
3850 + Integer.toHexString(System.identityHashCode(this))
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003851 + " " + getComponentShortName() + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003852 }
3853 }
3854
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003855 public static final ActivityInfo generateActivityInfo(Activity a, int flags,
3856 PackageUserState state, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003857 if (a == null) return null;
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003858 if (!checkUseInstalled(flags, state)) {
3859 return null;
3860 }
3861 if (!copyNeeded(flags, a.owner, state, a.metaData, userId)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003862 return a.info;
3863 }
3864 // Make shallow copies so we can store the metadata safely
3865 ActivityInfo ai = new ActivityInfo(a.info);
3866 ai.metaData = a.metaData;
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003867 ai.applicationInfo = generateApplicationInfo(a.owner, flags, state, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003868 return ai;
3869 }
3870
3871 public final static class Service extends Component<ServiceIntentInfo> {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003872 public final ServiceInfo info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003873
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003874 public Service(final ParseComponentArgs args, final ServiceInfo _info) {
3875 super(args, _info);
3876 info = _info;
3877 info.applicationInfo = args.owner.applicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003879
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003880 public void setPackageName(String packageName) {
3881 super.setPackageName(packageName);
3882 info.packageName = packageName;
3883 }
3884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003885 public String toString() {
3886 return "Service{"
3887 + Integer.toHexString(System.identityHashCode(this))
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003888 + " " + getComponentShortName() + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003889 }
3890 }
3891
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003892 public static final ServiceInfo generateServiceInfo(Service s, int flags,
3893 PackageUserState state, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 if (s == null) return null;
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003895 if (!checkUseInstalled(flags, state)) {
3896 return null;
3897 }
3898 if (!copyNeeded(flags, s.owner, state, s.metaData, userId)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899 return s.info;
3900 }
3901 // Make shallow copies so we can store the metadata safely
3902 ServiceInfo si = new ServiceInfo(s.info);
3903 si.metaData = s.metaData;
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003904 si.applicationInfo = generateApplicationInfo(s.owner, flags, state, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003905 return si;
3906 }
3907
3908 public final static class Provider extends Component {
3909 public final ProviderInfo info;
3910 public boolean syncable;
3911
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003912 public Provider(final ParseComponentArgs args, final ProviderInfo _info) {
3913 super(args, _info);
3914 info = _info;
3915 info.applicationInfo = args.owner.applicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003916 syncable = false;
3917 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003919 public Provider(Provider existingProvider) {
3920 super(existingProvider);
3921 this.info = existingProvider.info;
3922 this.syncable = existingProvider.syncable;
3923 }
3924
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003925 public void setPackageName(String packageName) {
3926 super.setPackageName(packageName);
3927 info.packageName = packageName;
3928 }
3929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003930 public String toString() {
3931 return "Provider{"
3932 + Integer.toHexString(System.identityHashCode(this))
3933 + " " + info.name + "}";
3934 }
3935 }
3936
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003937 public static final ProviderInfo generateProviderInfo(Provider p, int flags,
3938 PackageUserState state, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 if (p == null) return null;
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003940 if (!checkUseInstalled(flags, state)) {
3941 return null;
3942 }
3943 if (!copyNeeded(flags, p.owner, state, p.metaData, userId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003944 && ((flags & PackageManager.GET_URI_PERMISSION_PATTERNS) != 0
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003945 || p.info.uriPermissionPatterns == null)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003946 return p.info;
3947 }
3948 // Make shallow copies so we can store the metadata safely
3949 ProviderInfo pi = new ProviderInfo(p.info);
3950 pi.metaData = p.metaData;
3951 if ((flags & PackageManager.GET_URI_PERMISSION_PATTERNS) == 0) {
3952 pi.uriPermissionPatterns = null;
3953 }
Dianne Hackborn7767eac2012-08-23 18:25:40 -07003954 pi.applicationInfo = generateApplicationInfo(p.owner, flags, state, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003955 return pi;
3956 }
3957
3958 public final static class Instrumentation extends Component {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003959 public final InstrumentationInfo info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003960
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003961 public Instrumentation(final ParsePackageItemArgs args, final InstrumentationInfo _info) {
3962 super(args, _info);
3963 info = _info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003964 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07003965
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003966 public void setPackageName(String packageName) {
3967 super.setPackageName(packageName);
3968 info.packageName = packageName;
3969 }
3970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003971 public String toString() {
3972 return "Instrumentation{"
3973 + Integer.toHexString(System.identityHashCode(this))
Dianne Hackborn6dee18c2010-02-09 23:59:16 -08003974 + " " + getComponentShortName() + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003975 }
3976 }
3977
3978 public static final InstrumentationInfo generateInstrumentationInfo(
3979 Instrumentation i, int flags) {
3980 if (i == null) return null;
3981 if ((flags&PackageManager.GET_META_DATA) == 0) {
3982 return i.info;
3983 }
3984 InstrumentationInfo ii = new InstrumentationInfo(i.info);
3985 ii.metaData = i.metaData;
3986 return ii;
3987 }
3988
3989 public static class IntentInfo extends IntentFilter {
3990 public boolean hasDefault;
3991 public int labelRes;
3992 public CharSequence nonLocalizedLabel;
3993 public int icon;
Adam Powell81cd2e92010-04-21 16:35:18 -07003994 public int logo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003995 }
3996
3997 public final static class ActivityIntentInfo extends IntentInfo {
3998 public final Activity activity;
3999
4000 public ActivityIntentInfo(Activity _activity) {
4001 activity = _activity;
4002 }
4003
4004 public String toString() {
4005 return "ActivityIntentInfo{"
4006 + Integer.toHexString(System.identityHashCode(this))
4007 + " " + activity.info.name + "}";
4008 }
4009 }
4010
4011 public final static class ServiceIntentInfo extends IntentInfo {
4012 public final Service service;
4013
4014 public ServiceIntentInfo(Service _service) {
4015 service = _service;
4016 }
4017
4018 public String toString() {
4019 return "ServiceIntentInfo{"
4020 + Integer.toHexString(System.identityHashCode(this))
4021 + " " + service.info.name + "}";
4022 }
4023 }
Mitsuru Oshima69fff4a2009-07-21 09:51:05 -07004024
4025 /**
4026 * @hide
4027 */
4028 public static void setCompatibilityModeEnabled(boolean compatibilityModeEnabled) {
4029 sCompatibilityModeEnabled = compatibilityModeEnabled;
4030 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004031}