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.
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 flag {@link #REQUESTED_PERMISSION_GRANTED} set as appropriate.
217      */
218     public int[] requestedPermissionsFlags;
219 
220     /**
221      * Flag for {@link #requestedPermissionsFlags}: the requested permission
222      * is required for the application to run; the user can not optionally
223      * disable it.  Currently all permissions are required.
224      *
225      * @removed We do not support required permissions.
226      */
227     public static final int REQUESTED_PERMISSION_REQUIRED = 1<<0;
228 
229     /**
230      * Flag for {@link #requestedPermissionsFlags}: the requested permission
231      * is currently granted to the application.
232      */
233     public static final int REQUESTED_PERMISSION_GRANTED = 1<<1;
234 
235     /**
236      * Array of all signatures read from the package file. This is only filled
237      * in if the flag {@link PackageManager#GET_SIGNATURES} was set. A package
238      * must be signed with at least one certificate which is at position zero.
239      * The package can be signed with additional certificates which appear as
240      * subsequent entries.
241      *
242      * <strong>Note:</strong> Signature ordering is not guaranteed to be
243      * stable which means that a package signed with certificates A and B is
244      * equivalent to being signed with certificates B and A. This means that
245      * in case multiple signatures are reported you cannot assume the one at
246      * the first position to be the same across updates.
247      *
248      * <strong>Deprecated</strong> This has been replaced by the
249      * {@link PackageInfo#signingInfo} field, which takes into
250      * account signing certificate rotation.  For backwards compatibility in
251      * the event of signing certificate rotation, this will return the oldest
252      * reported signing certificate, so that an application will appear to
253      * callers as though no rotation occurred.
254      *
255      * @deprecated use {@code signingInfo} instead
256      */
257     @Deprecated
258     public Signature[] signatures;
259 
260     /**
261      * Signing information read from the package file, potentially
262      * including past signing certificates no longer used after signing
263      * certificate rotation.  This is only filled in if
264      * the flag {@link PackageManager#GET_SIGNING_CERTIFICATES} was set.
265      *
266      * Use this field instead of the deprecated {@code signatures} field.
267      * See {@link SigningInfo} for more information on its contents.
268      */
269     public SigningInfo signingInfo;
270 
271     /**
272      * Application specified preferred configuration
273      * {@link android.R.styleable#AndroidManifestUsesConfiguration
274      * &lt;uses-configuration&gt;} tags included under &lt;manifest&gt;,
275      * or null if there were none. This is only filled in if the flag
276      * {@link PackageManager#GET_CONFIGURATIONS} was set.
277      */
278     public ConfigurationInfo[] configPreferences;
279 
280     /**
281      * Features that this application has requested.
282      *
283      * @see FeatureInfo#FLAG_REQUIRED
284      */
285     public FeatureInfo[] reqFeatures;
286 
287     /**
288      * Groups of features that this application has requested.
289      * Each group contains a set of features that are required.
290      * A device must match the features listed in {@link #reqFeatures} and one
291      * or more FeatureGroups in order to have satisfied the feature requirement.
292      *
293      * @see FeatureInfo#FLAG_REQUIRED
294      */
295     public FeatureGroupInfo[] featureGroups;
296 
297     /**
298      * Constant corresponding to <code>auto</code> in
299      * the {@link android.R.attr#installLocation} attribute.
300      * @hide
301      */
302     @UnsupportedAppUsage
303     public static final int INSTALL_LOCATION_UNSPECIFIED = -1;
304 
305     /**
306      * Constant corresponding to <code>auto</code> in the
307      * {@link android.R.attr#installLocation} attribute.
308      */
309     public static final int INSTALL_LOCATION_AUTO = 0;
310 
311     /**
312      * Constant corresponding to <code>internalOnly</code> in the
313      * {@link android.R.attr#installLocation} attribute.
314      */
315     public static final int INSTALL_LOCATION_INTERNAL_ONLY = 1;
316 
317     /**
318      * Constant corresponding to <code>preferExternal</code> in the
319      * {@link android.R.attr#installLocation} attribute.
320      */
321     public static final int INSTALL_LOCATION_PREFER_EXTERNAL = 2;
322 
323     /**
324      * The install location requested by the package. From the
325      * {@link android.R.attr#installLocation} attribute, one of
326      * {@link #INSTALL_LOCATION_AUTO}, {@link #INSTALL_LOCATION_INTERNAL_ONLY},
327      * {@link #INSTALL_LOCATION_PREFER_EXTERNAL}
328      */
329     public int installLocation = INSTALL_LOCATION_INTERNAL_ONLY;
330 
331     /** @hide */
332     public boolean isStub;
333 
334     /** @hide */
335     @UnsupportedAppUsage
336     public boolean coreApp;
337 
338     /** @hide */
339     public boolean requiredForAllUsers;
340 
341     /** @hide */
342     public String restrictedAccountType;
343 
344     /** @hide */
345     public String requiredAccountType;
346 
347     /**
348      * What package, if any, this package will overlay.
349      *
350      * Package name of target package, or null.
351      * @hide
352      */
353     @UnsupportedAppUsage
354     public String overlayTarget;
355 
356     /**
357      * The name of the overlayable set of elements package, if any, this package will overlay.
358      *
359      * Overlayable name defined within the target package, or null.
360      * @hide
361      */
362     public String targetOverlayableName;
363 
364     /**
365      * The overlay category, if any, of this package
366      *
367      * @hide
368      */
369     public String overlayCategory;
370 
371     /** @hide */
372     public int overlayPriority;
373 
374     /**
375      * Whether the overlay is static, meaning it cannot be enabled/disabled at runtime.
376      * @hide
377      */
378     public boolean mOverlayIsStatic;
379 
380     /**
381      * The user-visible SDK version (ex. 26) of the framework against which the application claims
382      * to have been compiled, or {@code 0} if not specified.
383      * <p>
384      * This property is the compile-time equivalent of
385      * {@link android.os.Build.VERSION#SDK_INT Build.VERSION.SDK_INT}.
386      *
387      * @hide For platform use only; we don't expect developers to need to read this value.
388      */
389     public int compileSdkVersion;
390 
391     /**
392      * The development codename (ex. "O", "REL") of the framework against which the application
393      * claims to have been compiled, or {@code null} if not specified.
394      * <p>
395      * This property is the compile-time equivalent of
396      * {@link android.os.Build.VERSION#CODENAME Build.VERSION.CODENAME}.
397      *
398      * @hide For platform use only; we don't expect developers to need to read this value.
399      */
400     @Nullable
401     public String compileSdkVersionCodename;
402 
403     /**
404      * Whether the package is an APEX package.
405      */
406     public boolean isApex;
407 
PackageInfo()408     public PackageInfo() {
409     }
410 
411     /**
412      * Returns true if the package is a valid Runtime Overlay package.
413      * @hide
414      */
isOverlayPackage()415     public boolean isOverlayPackage() {
416         return overlayTarget != null;
417     }
418 
419     /**
420      * Returns true if the package is a valid static Runtime Overlay package. Static overlays
421      * are not updatable outside of a system update and are safe to load in the system process.
422      * @hide
423      */
isStaticOverlayPackage()424     public boolean isStaticOverlayPackage() {
425         return overlayTarget != null && mOverlayIsStatic;
426     }
427 
428     @Override
toString()429     public String toString() {
430         return "PackageInfo{"
431             + Integer.toHexString(System.identityHashCode(this))
432             + " " + packageName + "}";
433     }
434 
435     @Override
describeContents()436     public int describeContents() {
437         return 0;
438     }
439 
440     @Override
writeToParcel(Parcel dest, int parcelableFlags)441     public void writeToParcel(Parcel dest, int parcelableFlags) {
442         // Allow ApplicationInfo to be squashed.
443         final boolean prevAllowSquashing = dest.allowSquashing();
444         dest.writeString8(packageName);
445         dest.writeString8Array(splitNames);
446         dest.writeInt(versionCode);
447         dest.writeInt(versionCodeMajor);
448         dest.writeString8(versionName);
449         dest.writeInt(baseRevisionCode);
450         dest.writeIntArray(splitRevisionCodes);
451         dest.writeString8(sharedUserId);
452         dest.writeInt(sharedUserLabel);
453         if (applicationInfo != null) {
454             dest.writeInt(1);
455             applicationInfo.writeToParcel(dest, parcelableFlags);
456         } else {
457             dest.writeInt(0);
458         }
459         dest.writeLong(firstInstallTime);
460         dest.writeLong(lastUpdateTime);
461         dest.writeIntArray(gids);
462         dest.writeTypedArray(activities, parcelableFlags);
463         dest.writeTypedArray(receivers, parcelableFlags);
464         dest.writeTypedArray(services, parcelableFlags);
465         dest.writeTypedArray(providers, parcelableFlags);
466         dest.writeTypedArray(instrumentation, parcelableFlags);
467         dest.writeTypedArray(permissions, parcelableFlags);
468         dest.writeString8Array(requestedPermissions);
469         dest.writeIntArray(requestedPermissionsFlags);
470         dest.writeTypedArray(signatures, parcelableFlags);
471         dest.writeTypedArray(configPreferences, parcelableFlags);
472         dest.writeTypedArray(reqFeatures, parcelableFlags);
473         dest.writeTypedArray(featureGroups, parcelableFlags);
474         dest.writeInt(installLocation);
475         dest.writeInt(isStub ? 1 : 0);
476         dest.writeInt(coreApp ? 1 : 0);
477         dest.writeInt(requiredForAllUsers ? 1 : 0);
478         dest.writeString8(restrictedAccountType);
479         dest.writeString8(requiredAccountType);
480         dest.writeString8(overlayTarget);
481         dest.writeString8(overlayCategory);
482         dest.writeInt(overlayPriority);
483         dest.writeBoolean(mOverlayIsStatic);
484         dest.writeInt(compileSdkVersion);
485         dest.writeString8(compileSdkVersionCodename);
486         if (signingInfo != null) {
487             dest.writeInt(1);
488             signingInfo.writeToParcel(dest, parcelableFlags);
489         } else {
490             dest.writeInt(0);
491         }
492         dest.writeBoolean(isApex);
493         dest.restoreAllowSquashing(prevAllowSquashing);
494     }
495 
496     public static final @android.annotation.NonNull Parcelable.Creator<PackageInfo> CREATOR
497             = new Parcelable.Creator<PackageInfo>() {
498         @Override
499         public PackageInfo createFromParcel(Parcel source) {
500             return new PackageInfo(source);
501         }
502 
503         @Override
504         public PackageInfo[] newArray(int size) {
505             return new PackageInfo[size];
506         }
507     };
508 
509     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
PackageInfo(Parcel source)510     private PackageInfo(Parcel source) {
511         packageName = source.readString8();
512         splitNames = source.createString8Array();
513         versionCode = source.readInt();
514         versionCodeMajor = source.readInt();
515         versionName = source.readString8();
516         baseRevisionCode = source.readInt();
517         splitRevisionCodes = source.createIntArray();
518         sharedUserId = source.readString8();
519         sharedUserLabel = source.readInt();
520         int hasApp = source.readInt();
521         if (hasApp != 0) {
522             applicationInfo = ApplicationInfo.CREATOR.createFromParcel(source);
523         }
524         firstInstallTime = source.readLong();
525         lastUpdateTime = source.readLong();
526         gids = source.createIntArray();
527         activities = source.createTypedArray(ActivityInfo.CREATOR);
528         receivers = source.createTypedArray(ActivityInfo.CREATOR);
529         services = source.createTypedArray(ServiceInfo.CREATOR);
530         providers = source.createTypedArray(ProviderInfo.CREATOR);
531         instrumentation = source.createTypedArray(InstrumentationInfo.CREATOR);
532         permissions = source.createTypedArray(PermissionInfo.CREATOR);
533         requestedPermissions = source.createString8Array();
534         requestedPermissionsFlags = source.createIntArray();
535         signatures = source.createTypedArray(Signature.CREATOR);
536         configPreferences = source.createTypedArray(ConfigurationInfo.CREATOR);
537         reqFeatures = source.createTypedArray(FeatureInfo.CREATOR);
538         featureGroups = source.createTypedArray(FeatureGroupInfo.CREATOR);
539         installLocation = source.readInt();
540         isStub = source.readInt() != 0;
541         coreApp = source.readInt() != 0;
542         requiredForAllUsers = source.readInt() != 0;
543         restrictedAccountType = source.readString8();
544         requiredAccountType = source.readString8();
545         overlayTarget = source.readString8();
546         overlayCategory = source.readString8();
547         overlayPriority = source.readInt();
548         mOverlayIsStatic = source.readBoolean();
549         compileSdkVersion = source.readInt();
550         compileSdkVersionCodename = source.readString8();
551         int hasSigningInfo = source.readInt();
552         if (hasSigningInfo != 0) {
553             signingInfo = SigningInfo.CREATOR.createFromParcel(source);
554         }
555         isApex = source.readBoolean();
556     }
557 }
558