1 /*
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 
17 package android.content.pm;
18 
19 import android.annotation.Nullable;
20 import android.compat.annotation.UnsupportedAppUsage;
21 import android.os.Build;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 /**
26  * Overall information about the contents of a package.  This corresponds
27  * to all of the information collected from AndroidManifest.xml.
28  */
29 public class PackageInfo implements Parcelable {
30     /**
31      * The name of this package.  From the <manifest> tag's "name"
32      * attribute.
33      */
34     public String packageName;
35 
36     /**
37      * The names of any installed split APKs for this package.
38      */
39     public String[] splitNames;
40 
41     /**
42      * @deprecated Use {@link #getLongVersionCode()} instead, which includes both
43      * this and the additional
44      * {@link android.R.styleable#AndroidManifest_versionCodeMajor versionCodeMajor} attribute.
45      * The version number of this package, as specified by the <manifest>
46      * tag's {@link android.R.styleable#AndroidManifest_versionCode versionCode}
47      * attribute.
48      * @see #getLongVersionCode()
49      */
50     @Deprecated
51     public int versionCode;
52 
53     /**
54      * @hide
55      * The major version number of this package, as specified by the <manifest>
56      * tag's {@link android.R.styleable#AndroidManifest_versionCode versionCodeMajor}
57      * attribute.
58      * @see #getLongVersionCode()
59      */
60     public int versionCodeMajor;
61 
62     /**
63      * Return {@link android.R.styleable#AndroidManifest_versionCode versionCode} and
64      * {@link android.R.styleable#AndroidManifest_versionCodeMajor versionCodeMajor} combined
65      * together as a single long value.  The
66      * {@link android.R.styleable#AndroidManifest_versionCodeMajor versionCodeMajor} is placed in
67      * the upper 32 bits.
68      */
getLongVersionCode()69     public long getLongVersionCode() {
70         return composeLongVersionCode(versionCodeMajor, versionCode);
71     }
72 
73     /**
74      * Set the full version code in this PackageInfo, updating {@link #versionCode}
75      * with the lower bits.
76      * @see #getLongVersionCode()
77      */
setLongVersionCode(long longVersionCode)78     public void setLongVersionCode(long longVersionCode) {
79         versionCodeMajor = (int) (longVersionCode>>32);
80         versionCode = (int) longVersionCode;
81     }
82 
83     /**
84      * @hide Internal implementation for composing a minor and major version code in to
85      * a single long version code.
86      */
composeLongVersionCode(int major, int minor)87     public static long composeLongVersionCode(int major, int minor) {
88         return (((long) major) << 32) | (((long) minor) & 0xffffffffL);
89     }
90 
91     /**
92      * The version name of this package, as specified by the &lt;manifest&gt;
93      * tag's {@link android.R.styleable#AndroidManifest_versionName versionName}
94      * attribute, or null if there was none.
95      */
96     public String versionName;
97 
98     /**
99      * The revision number of the base APK for this package, as specified by the
100      * &lt;manifest&gt; tag's
101      * {@link android.R.styleable#AndroidManifest_revisionCode revisionCode}
102      * attribute.
103      */
104     public int baseRevisionCode;
105 
106     /**
107      * The revision number of any split APKs for this package, as specified by
108      * the &lt;manifest&gt; tag's
109      * {@link android.R.styleable#AndroidManifest_revisionCode revisionCode}
110      * attribute. Indexes are a 1:1 mapping against {@link #splitNames}.
111      */
112     public int[] splitRevisionCodes;
113 
114     /**
115      * The shared user ID name of this package, as specified by the &lt;manifest&gt;
116      * tag's {@link android.R.styleable#AndroidManifest_sharedUserId sharedUserId}
117      * attribute.
118      */
119     public String sharedUserId;
120 
121     /**
122      * The shared user ID label of this package, as specified by the &lt;manifest&gt;
123      * tag's {@link android.R.styleable#AndroidManifest_sharedUserLabel sharedUserLabel}
124      * attribute.
125      */
126     public int sharedUserLabel;
127 
128     /**
129      * Information collected from the &lt;application&gt; tag, or null if
130      * there was none.
131      */
132     public ApplicationInfo applicationInfo;
133 
134     /**
135      * The time at which the app was first installed.  Units are as
136      * per {@link System#currentTimeMillis()}.
137      */
138     public long firstInstallTime;
139 
140     /**
141      * The time at which the app was last updated.  Units are as
142      * per {@link System#currentTimeMillis()}.
143      */
144     public long lastUpdateTime;
145 
146     /**
147      * All kernel group-IDs that have been assigned to this package.
148      * This is only filled in if the flag {@link PackageManager#GET_GIDS} was set.
149      */
150     public int[] gids;
151 
152     /**
153      * Array of all {@link android.R.styleable#AndroidManifestActivity
154      * &lt;activity&gt;} tags included under &lt;application&gt;,
155      * or null if there were none.  This is only filled in if the flag
156      * {@link PackageManager#GET_ACTIVITIES} was set.
157      */
158     public ActivityInfo[] activities;
159 
160     /**
161      * Array of all {@link android.R.styleable#AndroidManifestReceiver
162      * &lt;receiver&gt;} tags included under &lt;application&gt;,
163      * or null if there were none.  This is only filled in if the flag
164      * {@link PackageManager#GET_RECEIVERS} was set.
165      */
166     public ActivityInfo[] receivers;
167 
168     /**
169      * Array of all {@link android.R.styleable#AndroidManifestService
170      * &lt;service&gt;} tags included under &lt;application&gt;,
171      * or null if there were none.  This is only filled in if the flag
172      * {@link PackageManager#GET_SERVICES} was set.
173      */
174     public ServiceInfo[] services;
175 
176     /**
177      * Array of all {@link android.R.styleable#AndroidManifestProvider
178      * &lt;provider&gt;} tags included under &lt;application&gt;,
179      * or null if there were none.  This is only filled in if the flag
180      * {@link PackageManager#GET_PROVIDERS} was set.
181      */
182     public ProviderInfo[] providers;
183 
184     /**
185      * Array of all {@link android.R.styleable#AndroidManifestInstrumentation
186      * &lt;instrumentation&gt;} tags included under &lt;manifest&gt;,
187      * or null if there were none.  This is only filled in if the flag
188      * {@link PackageManager#GET_INSTRUMENTATION} was set.
189      */
190     public InstrumentationInfo[] instrumentation;
191 
192     /**
193      * Array of all {@link android.R.styleable#AndroidManifestPermission
194      * &lt;permission&gt;} tags included under &lt;manifest&gt;,
195      * or null if there were none.  This is only filled in if the flag
196      * {@link PackageManager#GET_PERMISSIONS} was set.
197      */
198     public PermissionInfo[] permissions;
199 
200     /**
201      * Array of all {@link android.R.styleable#AndroidManifestUsesPermission
202      * &lt;uses-permission&gt;} tags included under &lt;manifest&gt;,
203      * or null if there were none.  This is only filled in if the flag
204      * {@link PackageManager#GET_PERMISSIONS} was set.  This list includes
205      * all permissions requested, even those that were not granted or known
206      * by the system at install time.
207      */
208     public String[] requestedPermissions;
209 
210     /**
211      * Array of flags of all {@link android.R.styleable#AndroidManifestUsesPermission
212      * &lt;uses-permission&gt;} tags included under &lt;manifest&gt;,
213      * or null if there were none.  This is only filled in if the flag
214      * {@link PackageManager#GET_PERMISSIONS} was set.  Each value matches
215      * the corresponding entry in {@link #requestedPermissions}, and will have
216      * the flags {@link #REQUESTED_PERMISSION_GRANTED} and
217      * {@link #REQUESTED_PERMISSION_NEVER_FOR_LOCATION} set as appropriate.
218      */
219     public int[] requestedPermissionsFlags;
220 
221     /**
222      * Array of all {@link android.R.styleable#AndroidManifestAttribution
223      * &lt;attribution&gt;} tags included under &lt;manifest&gt;, or null if there were none. This
224      * is only filled if the flag {@link PackageManager#GET_ATTRIBUTIONS_LONG} was set.
225      */
226     @SuppressWarnings({"ArrayReturn", "NullableCollection"})
227     public @Nullable Attribution[] attributions;
228 
229     /**
230      * Flag for {@link #requestedPermissionsFlags}: the requested permission
231      * is required for the application to run; the user can not optionally
232      * disable it.  Currently all permissions are required.
233      *
234      * @removed We do not support required permissions.
235      */
236     public static final int REQUESTED_PERMISSION_REQUIRED = 0x00000001;
237 
238     /**
239      * Flag for {@link #requestedPermissionsFlags}: the requested permission
240      * is currently granted to the application.
241      */
242     public static final int REQUESTED_PERMISSION_GRANTED = 0x00000002;
243 
244     /**
245      * Flag for {@link #requestedPermissionsFlags}: the requested permission has
246      * declared {@code neverForLocation} in their manifest as a strong assertion
247      * by a developer that they will never use this permission to derive the
248      * physical location of the device, regardless of
249      * {@link android.Manifest.permission#ACCESS_FINE_LOCATION} and/or
250      * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION} being granted.
251      */
252     public static final int REQUESTED_PERMISSION_NEVER_FOR_LOCATION = 0x00010000;
253 
254     /**
255      * Flag for {@link #requestedPermissionsFlags}: the requested permission was
256      * not explicitly requested via uses-permission, but was instead implicitly
257      * requested (e.g., for version compatibility reasons).
258      */
259     public static final int REQUESTED_PERMISSION_IMPLICIT = 0x00000004;
260 
261     /**
262      * Array of all signatures read from the package file. This is only filled
263      * in if the flag {@link PackageManager#GET_SIGNATURES} was set. A package
264      * must be signed with at least one certificate which is at position zero.
265      * The package can be signed with additional certificates which appear as
266      * subsequent entries.
267      *
268      * <strong>Note:</strong> Signature ordering is not guaranteed to be
269      * stable which means that a package signed with certificates A and B is
270      * equivalent to being signed with certificates B and A. This means that
271      * in case multiple signatures are reported you cannot assume the one at
272      * the first position to be the same across updates.
273      *
274      * <strong>Deprecated</strong> This has been replaced by the
275      * {@link PackageInfo#signingInfo} field, which takes into
276      * account signing certificate rotation.  For backwards compatibility in
277      * the event of signing certificate rotation, this will return the oldest
278      * reported signing certificate, so that an application will appear to
279      * callers as though no rotation occurred.
280      *
281      * @deprecated use {@code signingInfo} instead
282      */
283     @Deprecated
284     public Signature[] signatures;
285 
286     /**
287      * Signing information read from the package file, potentially
288      * including past signing certificates no longer used after signing
289      * certificate rotation.  This is only filled in if
290      * the flag {@link PackageManager#GET_SIGNING_CERTIFICATES} was set.
291      *
292      * Use this field instead of the deprecated {@code signatures} field.
293      * See {@link SigningInfo} for more information on its contents.
294      */
295     public SigningInfo signingInfo;
296 
297     /**
298      * Application specified preferred configuration
299      * {@link android.R.styleable#AndroidManifestUsesConfiguration
300      * &lt;uses-configuration&gt;} tags included under &lt;manifest&gt;,
301      * or null if there were none. This is only filled in if the flag
302      * {@link PackageManager#GET_CONFIGURATIONS} was set.
303      */
304     public ConfigurationInfo[] configPreferences;
305 
306     /**
307      * Features that this application has requested.
308      *
309      * @see FeatureInfo#FLAG_REQUIRED
310      */
311     public FeatureInfo[] reqFeatures;
312 
313     /**
314      * Groups of features that this application has requested.
315      * Each group contains a set of features that are required.
316      * A device must match the features listed in {@link #reqFeatures} and one
317      * or more FeatureGroups in order to have satisfied the feature requirement.
318      *
319      * @see FeatureInfo#FLAG_REQUIRED
320      */
321     public FeatureGroupInfo[] featureGroups;
322 
323     /**
324      * Constant corresponding to <code>auto</code> in
325      * the {@link android.R.attr#installLocation} attribute.
326      * @hide
327      */
328     @UnsupportedAppUsage
329     public static final int INSTALL_LOCATION_UNSPECIFIED = -1;
330 
331     /**
332      * Constant corresponding to <code>auto</code> in the
333      * {@link android.R.attr#installLocation} attribute.
334      */
335     public static final int INSTALL_LOCATION_AUTO = 0;
336 
337     /**
338      * Constant corresponding to <code>internalOnly</code> in the
339      * {@link android.R.attr#installLocation} attribute.
340      */
341     public static final int INSTALL_LOCATION_INTERNAL_ONLY = 1;
342 
343     /**
344      * Constant corresponding to <code>preferExternal</code> in the
345      * {@link android.R.attr#installLocation} attribute.
346      */
347     public static final int INSTALL_LOCATION_PREFER_EXTERNAL = 2;
348 
349     /**
350      * The install location requested by the package. From the
351      * {@link android.R.attr#installLocation} attribute, one of
352      * {@link #INSTALL_LOCATION_AUTO}, {@link #INSTALL_LOCATION_INTERNAL_ONLY},
353      * {@link #INSTALL_LOCATION_PREFER_EXTERNAL}
354      */
355     public int installLocation = INSTALL_LOCATION_INTERNAL_ONLY;
356 
357     /**
358      * Whether or not the package is a stub and should be replaced by a full version of the app.
359      *
360      * @hide
361      */
362     public boolean isStub;
363 
364     /**
365      * Whether the app is included when the device is booted into a minimal state. Set through the
366      * non-namespaced "coreApp" attribute of the manifest tag.
367      *
368      * @hide
369      */
370     @UnsupportedAppUsage
371     public boolean coreApp;
372 
373     /**
374      * Signals that this app is required for all users on the device.
375      *
376      * When a restricted user profile is created, the user is prompted with a list of apps to
377      * install on that user. Settings uses this field to determine obligatory apps which cannot be
378      * deselected.
379      *
380      * This restriction is not handled by the framework itself.
381      * @hide
382      */
383     public boolean requiredForAllUsers;
384 
385     /**
386      * The restricted account authenticator type that is used by this application.
387      * @hide
388      */
389     public String restrictedAccountType;
390 
391     /**
392      * The required account type without which this application will not function.
393      * @hide
394      */
395     public String requiredAccountType;
396 
397     /**
398      * What package, if any, this package will overlay.
399      *
400      * Package name of target package, or null.
401      * @hide
402      */
403     @UnsupportedAppUsage
404     public String overlayTarget;
405 
406     /**
407      * The name of the overlayable set of elements package, if any, this package will overlay.
408      *
409      * Overlayable name defined within the target package, or null.
410      * @hide
411      */
412     public String targetOverlayableName;
413 
414     /**
415      * The overlay category, if any, of this package
416      *
417      * @hide
418      */
419     public String overlayCategory;
420 
421     /** @hide */
422     public int overlayPriority;
423 
424     /**
425      * Whether the overlay is static, meaning it cannot be enabled/disabled at runtime.
426      * @hide
427      */
428     public boolean mOverlayIsStatic;
429 
430     /**
431      * The user-visible SDK version (ex. 26) of the framework against which the application claims
432      * to have been compiled, or {@code 0} if not specified.
433      * <p>
434      * This property is the compile-time equivalent of
435      * {@link android.os.Build.VERSION#SDK_INT Build.VERSION.SDK_INT}.
436      *
437      * @hide For platform use only; we don't expect developers to need to read this value.
438      */
439     public int compileSdkVersion;
440 
441     /**
442      * The development codename (ex. "O", "REL") of the framework against which the application
443      * claims to have been compiled, or {@code null} if not specified.
444      * <p>
445      * This property is the compile-time equivalent of
446      * {@link android.os.Build.VERSION#CODENAME Build.VERSION.CODENAME}.
447      *
448      * @hide For platform use only; we don't expect developers to need to read this value.
449      */
450     @Nullable
451     public String compileSdkVersionCodename;
452 
453     /**
454      * Whether the package is an APEX package.
455      */
456     public boolean isApex;
457 
458     /**
459      * Whether this is an active APEX package.
460      * @hide
461      */
462     public boolean isActiveApex;
463 
PackageInfo()464     public PackageInfo() {
465     }
466 
467     /**
468      * Returns true if the package is a valid Runtime Overlay package.
469      * @hide
470      */
isOverlayPackage()471     public boolean isOverlayPackage() {
472         return overlayTarget != null;
473     }
474 
475     /**
476      * Returns true if the package is a valid static Runtime Overlay package. Static overlays
477      * are not updatable outside of a system update and are safe to load in the system process.
478      * @hide
479      */
isStaticOverlayPackage()480     public boolean isStaticOverlayPackage() {
481         return overlayTarget != null && mOverlayIsStatic;
482     }
483 
484     @Override
toString()485     public String toString() {
486         return "PackageInfo{"
487             + Integer.toHexString(System.identityHashCode(this))
488             + " " + packageName + "}";
489     }
490 
491     @Override
describeContents()492     public int describeContents() {
493         return 0;
494     }
495 
496     @Override
writeToParcel(Parcel dest, int parcelableFlags)497     public void writeToParcel(Parcel dest, int parcelableFlags) {
498         // Allow ApplicationInfo to be squashed.
499         final boolean prevAllowSquashing = dest.allowSquashing();
500         dest.writeString8(packageName);
501         dest.writeString8Array(splitNames);
502         dest.writeInt(versionCode);
503         dest.writeInt(versionCodeMajor);
504         dest.writeString8(versionName);
505         dest.writeInt(baseRevisionCode);
506         dest.writeIntArray(splitRevisionCodes);
507         dest.writeString8(sharedUserId);
508         dest.writeInt(sharedUserLabel);
509         if (applicationInfo != null) {
510             dest.writeInt(1);
511             applicationInfo.writeToParcel(dest, parcelableFlags);
512         } else {
513             dest.writeInt(0);
514         }
515         dest.writeLong(firstInstallTime);
516         dest.writeLong(lastUpdateTime);
517         dest.writeIntArray(gids);
518         dest.writeTypedArray(activities, parcelableFlags);
519         dest.writeTypedArray(receivers, parcelableFlags);
520         dest.writeTypedArray(services, parcelableFlags);
521         dest.writeTypedArray(providers, parcelableFlags);
522         dest.writeTypedArray(instrumentation, parcelableFlags);
523         dest.writeTypedArray(permissions, parcelableFlags);
524         dest.writeString8Array(requestedPermissions);
525         dest.writeIntArray(requestedPermissionsFlags);
526         dest.writeTypedArray(signatures, parcelableFlags);
527         dest.writeTypedArray(configPreferences, parcelableFlags);
528         dest.writeTypedArray(reqFeatures, parcelableFlags);
529         dest.writeTypedArray(featureGroups, parcelableFlags);
530         dest.writeTypedArray(attributions, parcelableFlags);
531         dest.writeInt(installLocation);
532         dest.writeInt(isStub ? 1 : 0);
533         dest.writeInt(coreApp ? 1 : 0);
534         dest.writeInt(requiredForAllUsers ? 1 : 0);
535         dest.writeString8(restrictedAccountType);
536         dest.writeString8(requiredAccountType);
537         dest.writeString8(overlayTarget);
538         dest.writeString8(overlayCategory);
539         dest.writeInt(overlayPriority);
540         dest.writeBoolean(mOverlayIsStatic);
541         dest.writeInt(compileSdkVersion);
542         dest.writeString8(compileSdkVersionCodename);
543         if (signingInfo != null) {
544             dest.writeInt(1);
545             signingInfo.writeToParcel(dest, parcelableFlags);
546         } else {
547             dest.writeInt(0);
548         }
549         dest.writeBoolean(isApex);
550         dest.writeBoolean(isActiveApex);
551         dest.restoreAllowSquashing(prevAllowSquashing);
552     }
553 
554     public static final @android.annotation.NonNull Parcelable.Creator<PackageInfo> CREATOR
555             = new Parcelable.Creator<PackageInfo>() {
556         @Override
557         public PackageInfo createFromParcel(Parcel source) {
558             return new PackageInfo(source);
559         }
560 
561         @Override
562         public PackageInfo[] newArray(int size) {
563             return new PackageInfo[size];
564         }
565     };
566 
567     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
PackageInfo(Parcel source)568     private PackageInfo(Parcel source) {
569         packageName = source.readString8();
570         splitNames = source.createString8Array();
571         versionCode = source.readInt();
572         versionCodeMajor = source.readInt();
573         versionName = source.readString8();
574         baseRevisionCode = source.readInt();
575         splitRevisionCodes = source.createIntArray();
576         sharedUserId = source.readString8();
577         sharedUserLabel = source.readInt();
578         int hasApp = source.readInt();
579         if (hasApp != 0) {
580             applicationInfo = ApplicationInfo.CREATOR.createFromParcel(source);
581         }
582         firstInstallTime = source.readLong();
583         lastUpdateTime = source.readLong();
584         gids = source.createIntArray();
585         activities = source.createTypedArray(ActivityInfo.CREATOR);
586         receivers = source.createTypedArray(ActivityInfo.CREATOR);
587         services = source.createTypedArray(ServiceInfo.CREATOR);
588         providers = source.createTypedArray(ProviderInfo.CREATOR);
589         instrumentation = source.createTypedArray(InstrumentationInfo.CREATOR);
590         permissions = source.createTypedArray(PermissionInfo.CREATOR);
591         requestedPermissions = source.createString8Array();
592         requestedPermissionsFlags = source.createIntArray();
593         signatures = source.createTypedArray(Signature.CREATOR);
594         configPreferences = source.createTypedArray(ConfigurationInfo.CREATOR);
595         reqFeatures = source.createTypedArray(FeatureInfo.CREATOR);
596         featureGroups = source.createTypedArray(FeatureGroupInfo.CREATOR);
597         attributions = source.createTypedArray(Attribution.CREATOR);
598         installLocation = source.readInt();
599         isStub = source.readInt() != 0;
600         coreApp = source.readInt() != 0;
601         requiredForAllUsers = source.readInt() != 0;
602         restrictedAccountType = source.readString8();
603         requiredAccountType = source.readString8();
604         overlayTarget = source.readString8();
605         overlayCategory = source.readString8();
606         overlayPriority = source.readInt();
607         mOverlayIsStatic = source.readBoolean();
608         compileSdkVersion = source.readInt();
609         compileSdkVersionCodename = source.readString8();
610         int hasSigningInfo = source.readInt();
611         if (hasSigningInfo != 0) {
612             signingInfo = SigningInfo.CREATOR.createFromParcel(source);
613         }
614         isApex = source.readBoolean();
615         isActiveApex = source.readBoolean();
616     }
617 }
618