1 /*
2  * Copyright (C) 2021 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.parsing;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.content.pm.PackageInfo;
22 import android.content.pm.VerifierInfo;
23 
24 import com.android.internal.util.ArrayUtils;
25 import com.android.internal.util.CollectionUtils;
26 import com.android.internal.util.DataClass;
27 
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.Set;
32 
33 /**
34  * Lightweight parsed details about a single package.
35  *
36  * @hide
37  */
38 @DataClass(genConstructor = false, genConstDefs = false)
39 public class PackageLite {
40     /** Name of the package as used to identify it in the system */
41     private final @NonNull String mPackageName;
42     /**
43      * Path where this package was found on disk. For monolithic packages
44      * this is path to single base APK file; for cluster packages this is
45      * path to the cluster directory.
46      */
47     private final @NonNull String mPath;
48     /** Path of base APK */
49     private final @NonNull String mBaseApkPath;
50     /** Paths of any split APKs, ordered by parsed splitName */
51     private final @Nullable String[] mSplitApkPaths;
52     /** Names of any split APKs, ordered by parsed splitName */
53     private final @Nullable String[] mSplitNames;
54     /** Dependencies of any split APKs, ordered by parsed splitName */
55     private final @Nullable String[] mUsesSplitNames;
56     private final @Nullable String[] mConfigForSplit;
57     /** Indicate the types of the required split are necessary for base APK to run */
58     private final @Nullable Set<String> mBaseRequiredSplitTypes;
59     /** Indicate the types of the required split are necessary for split APKs to run */
60     private final @Nullable Set<String>[] mRequiredSplitTypes;
61     /** Split type of any split APKs, ordered by parsed splitName */
62     private final @Nullable Set<String>[] mSplitTypes;
63     /** Major and minor version number of this package */
64     private final int mVersionCodeMajor;
65     private final int mVersionCode;
66     private final int mTargetSdk;
67     /** Revision code of base APK */
68     private final int mBaseRevisionCode;
69     /** Revision codes of any split APKs, ordered by parsed splitName */
70     private final @Nullable int[] mSplitRevisionCodes;
71     /**
72      * Indicate the install location of this package
73      *
74      * @see {@link PackageInfo#INSTALL_LOCATION_AUTO}
75      * @see {@link PackageInfo#INSTALL_LOCATION_INTERNAL_ONLY}
76      * @see {@link PackageInfo#INSTALL_LOCATION_PREFER_EXTERNAL}
77      */
78     private final int mInstallLocation;
79     /** Information about a package verifiers as used during package verification */
80     private final @NonNull VerifierInfo[] mVerifiers;
81 
82     /** Indicate whether any split APKs that are features. Ordered by splitName */
83     private final @Nullable boolean[] mIsFeatureSplits;
84     /** Indicate whether each split should be load into their own Context objects */
85     private final boolean mIsolatedSplits;
86     /**
87      * Indicate whether this package requires at least one split (either feature or resource)
88      * to be present in order to function
89      */
90     private final boolean mSplitRequired;
91     /** Indicate whether this app is coreApp */
92     private final boolean mCoreApp;
93     /** Indicate whether this app can be debugged */
94     private final boolean mDebuggable;
95     /** Indicate whether this app needs to be loaded into other applications' processes */
96     private final boolean mMultiArch;
97     /** Indicate whether the 32 bit version of the ABI should be used */
98     private final boolean mUse32bitAbi;
99     /** Indicate whether installer extracts native libraries */
100     private final boolean mExtractNativeLibs;
101     /** Indicate whether this app is profileable by Shell */
102     private final boolean mProfileableByShell;
103     /**
104      * Indicate whether this package wants to run the dex within its APK but not extracted
105      * or locally compiled variants.
106      */
107     private final boolean mUseEmbeddedDex;
108     /**
109      * Indicates if this package is a sdk.
110      */
111     private final boolean mIsSdkLibrary;
112 
PackageLite(String path, String baseApkPath, ApkLite baseApk, String[] splitNames, boolean[] isFeatureSplits, String[] usesSplitNames, String[] configForSplit, String[] splitApkPaths, int[] splitRevisionCodes, int targetSdk, Set<String>[] requiredSplitTypes, Set<String>[] splitTypes)113     public PackageLite(String path, String baseApkPath, ApkLite baseApk,
114             String[] splitNames, boolean[] isFeatureSplits, String[] usesSplitNames,
115             String[] configForSplit, String[] splitApkPaths, int[] splitRevisionCodes,
116             int targetSdk, Set<String>[] requiredSplitTypes, Set<String>[] splitTypes) {
117         // The following paths may be different from the path in ApkLite because we
118         // move or rename the APK files. Use parameters to indicate the correct paths.
119         mPath = path;
120         mBaseApkPath = baseApkPath;
121         mPackageName = baseApk.getPackageName();
122         mVersionCode = baseApk.getVersionCode();
123         mVersionCodeMajor = baseApk.getVersionCodeMajor();
124         mInstallLocation = baseApk.getInstallLocation();
125         mVerifiers = baseApk.getVerifiers();
126         mBaseRevisionCode = baseApk.getRevisionCode();
127         mCoreApp = baseApk.isCoreApp();
128         mDebuggable = baseApk.isDebuggable();
129         mMultiArch = baseApk.isMultiArch();
130         mUse32bitAbi = baseApk.isUse32bitAbi();
131         mExtractNativeLibs = baseApk.isExtractNativeLibs();
132         mIsolatedSplits = baseApk.isIsolatedSplits();
133         mUseEmbeddedDex = baseApk.isUseEmbeddedDex();
134         mBaseRequiredSplitTypes = baseApk.getRequiredSplitTypes();
135         mRequiredSplitTypes = requiredSplitTypes;
136         mSplitRequired = (baseApk.isSplitRequired() || hasAnyRequiredSplitTypes());
137         mProfileableByShell = baseApk.isProfileableByShell();
138         mIsSdkLibrary = baseApk.isIsSdkLibrary();
139         mSplitNames = splitNames;
140         mSplitTypes = splitTypes;
141         mIsFeatureSplits = isFeatureSplits;
142         mUsesSplitNames = usesSplitNames;
143         mConfigForSplit = configForSplit;
144         mSplitApkPaths = splitApkPaths;
145         mSplitRevisionCodes = splitRevisionCodes;
146         mTargetSdk = targetSdk;
147     }
148 
149     /**
150      * Return code path to the base APK file, and split APK files if any.
151      */
getAllApkPaths()152     public List<String> getAllApkPaths() {
153         final ArrayList<String> paths = new ArrayList<>();
154         paths.add(mBaseApkPath);
155         if (!ArrayUtils.isEmpty(mSplitApkPaths)) {
156             Collections.addAll(paths, mSplitApkPaths);
157         }
158         return paths;
159     }
160 
161     /**
162      * Return {@link #mVersionCode} and {@link #mVersionCodeMajor} combined together as a
163      * single long value. The {@link #mVersionCodeMajor} is placed in the upper 32 bits.
164      */
getLongVersionCode()165     public long getLongVersionCode() {
166         return PackageInfo.composeLongVersionCode(mVersionCodeMajor, mVersionCode);
167     }
168 
169     /**
170      * Return if requiredSplitTypes presents in the package.
171      */
hasAnyRequiredSplitTypes()172     private boolean hasAnyRequiredSplitTypes() {
173         if (!CollectionUtils.isEmpty(mBaseRequiredSplitTypes)) {
174             return true;
175         }
176         return ArrayUtils.find(mRequiredSplitTypes, r -> !CollectionUtils.isEmpty(r)) != null;
177     }
178 
179 
180 
181     // Code below generated by codegen v1.0.23.
182     //
183     // DO NOT MODIFY!
184     // CHECKSTYLE:OFF Generated code
185     //
186     // To regenerate run:
187     // $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/content/pm/parsing/PackageLite.java
188     //
189     // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
190     //   Settings > Editor > Code Style > Formatter Control
191     //@formatter:off
192 
193 
194     /**
195      * Name of the package as used to identify it in the system
196      */
197     @DataClass.Generated.Member
getPackageName()198     public @NonNull String getPackageName() {
199         return mPackageName;
200     }
201 
202     /**
203      * Path where this package was found on disk. For monolithic packages
204      * this is path to single base APK file; for cluster packages this is
205      * path to the cluster directory.
206      */
207     @DataClass.Generated.Member
getPath()208     public @NonNull String getPath() {
209         return mPath;
210     }
211 
212     /**
213      * Path of base APK
214      */
215     @DataClass.Generated.Member
getBaseApkPath()216     public @NonNull String getBaseApkPath() {
217         return mBaseApkPath;
218     }
219 
220     /**
221      * Paths of any split APKs, ordered by parsed splitName
222      */
223     @DataClass.Generated.Member
getSplitApkPaths()224     public @Nullable String[] getSplitApkPaths() {
225         return mSplitApkPaths;
226     }
227 
228     /**
229      * Names of any split APKs, ordered by parsed splitName
230      */
231     @DataClass.Generated.Member
getSplitNames()232     public @Nullable String[] getSplitNames() {
233         return mSplitNames;
234     }
235 
236     /**
237      * Dependencies of any split APKs, ordered by parsed splitName
238      */
239     @DataClass.Generated.Member
getUsesSplitNames()240     public @Nullable String[] getUsesSplitNames() {
241         return mUsesSplitNames;
242     }
243 
244     @DataClass.Generated.Member
getConfigForSplit()245     public @Nullable String[] getConfigForSplit() {
246         return mConfigForSplit;
247     }
248 
249     /**
250      * Indicate the types of the required split are necessary for base APK to run
251      */
252     @DataClass.Generated.Member
getBaseRequiredSplitTypes()253     public @Nullable Set<String> getBaseRequiredSplitTypes() {
254         return mBaseRequiredSplitTypes;
255     }
256 
257     /**
258      * Indicate the types of the required split are necessary for split APKs to run
259      */
260     @DataClass.Generated.Member
getRequiredSplitTypes()261     public @Nullable Set<String>[] getRequiredSplitTypes() {
262         return mRequiredSplitTypes;
263     }
264 
265     /**
266      * Split type of any split APKs, ordered by parsed splitName
267      */
268     @DataClass.Generated.Member
getSplitTypes()269     public @Nullable Set<String>[] getSplitTypes() {
270         return mSplitTypes;
271     }
272 
273     /**
274      * Major and minor version number of this package
275      */
276     @DataClass.Generated.Member
getVersionCodeMajor()277     public int getVersionCodeMajor() {
278         return mVersionCodeMajor;
279     }
280 
281     @DataClass.Generated.Member
getVersionCode()282     public int getVersionCode() {
283         return mVersionCode;
284     }
285 
286     @DataClass.Generated.Member
getTargetSdk()287     public int getTargetSdk() {
288         return mTargetSdk;
289     }
290 
291     /**
292      * Revision code of base APK
293      */
294     @DataClass.Generated.Member
getBaseRevisionCode()295     public int getBaseRevisionCode() {
296         return mBaseRevisionCode;
297     }
298 
299     /**
300      * Revision codes of any split APKs, ordered by parsed splitName
301      */
302     @DataClass.Generated.Member
getSplitRevisionCodes()303     public @Nullable int[] getSplitRevisionCodes() {
304         return mSplitRevisionCodes;
305     }
306 
307     /**
308      * Indicate the install location of this package
309      *
310      * @see {@link PackageInfo#INSTALL_LOCATION_AUTO}
311      * @see {@link PackageInfo#INSTALL_LOCATION_INTERNAL_ONLY}
312      * @see {@link PackageInfo#INSTALL_LOCATION_PREFER_EXTERNAL}
313      */
314     @DataClass.Generated.Member
getInstallLocation()315     public int getInstallLocation() {
316         return mInstallLocation;
317     }
318 
319     /**
320      * Information about a package verifiers as used during package verification
321      */
322     @DataClass.Generated.Member
getVerifiers()323     public @NonNull VerifierInfo[] getVerifiers() {
324         return mVerifiers;
325     }
326 
327     /**
328      * Indicate whether any split APKs that are features. Ordered by splitName
329      */
330     @DataClass.Generated.Member
getIsFeatureSplits()331     public @Nullable boolean[] getIsFeatureSplits() {
332         return mIsFeatureSplits;
333     }
334 
335     /**
336      * Indicate whether each split should be load into their own Context objects
337      */
338     @DataClass.Generated.Member
isIsolatedSplits()339     public boolean isIsolatedSplits() {
340         return mIsolatedSplits;
341     }
342 
343     /**
344      * Indicate whether this package requires at least one split (either feature or resource)
345      * to be present in order to function
346      */
347     @DataClass.Generated.Member
isSplitRequired()348     public boolean isSplitRequired() {
349         return mSplitRequired;
350     }
351 
352     /**
353      * Indicate whether this app is coreApp
354      */
355     @DataClass.Generated.Member
isCoreApp()356     public boolean isCoreApp() {
357         return mCoreApp;
358     }
359 
360     /**
361      * Indicate whether this app can be debugged
362      */
363     @DataClass.Generated.Member
isDebuggable()364     public boolean isDebuggable() {
365         return mDebuggable;
366     }
367 
368     /**
369      * Indicate whether this app needs to be loaded into other applications' processes
370      */
371     @DataClass.Generated.Member
isMultiArch()372     public boolean isMultiArch() {
373         return mMultiArch;
374     }
375 
376     /**
377      * Indicate whether the 32 bit version of the ABI should be used
378      */
379     @DataClass.Generated.Member
isUse32bitAbi()380     public boolean isUse32bitAbi() {
381         return mUse32bitAbi;
382     }
383 
384     /**
385      * Indicate whether installer extracts native libraries
386      */
387     @DataClass.Generated.Member
isExtractNativeLibs()388     public boolean isExtractNativeLibs() {
389         return mExtractNativeLibs;
390     }
391 
392     /**
393      * Indicate whether this app is profileable by Shell
394      */
395     @DataClass.Generated.Member
isProfileableByShell()396     public boolean isProfileableByShell() {
397         return mProfileableByShell;
398     }
399 
400     /**
401      * Indicate whether this package wants to run the dex within its APK but not extracted
402      * or locally compiled variants.
403      */
404     @DataClass.Generated.Member
isUseEmbeddedDex()405     public boolean isUseEmbeddedDex() {
406         return mUseEmbeddedDex;
407     }
408 
409     /**
410      * Indicates if this package is a sdk.
411      */
412     @DataClass.Generated.Member
isIsSdkLibrary()413     public boolean isIsSdkLibrary() {
414         return mIsSdkLibrary;
415     }
416 
417     @DataClass.Generated(
418             time = 1643132127068L,
419             codegenVersion = "1.0.23",
420             sourceFile = "frameworks/base/core/java/android/content/pm/parsing/PackageLite.java",
421             inputSignatures =
422                     "private final @android.annotation.NonNull java.lang.String mPackageName\nprivate final @android.annotation.NonNull java.lang.String mPath\nprivate final @android.annotation.NonNull java.lang.String mBaseApkPath\nprivate final @android.annotation.Nullable java.lang.String[] mSplitApkPaths\nprivate final @android.annotation.Nullable java.lang.String[] mSplitNames\nprivate final @android.annotation.Nullable java.lang.String[] mUsesSplitNames\nprivate final @android.annotation.Nullable java.lang.String[] mConfigForSplit\nprivate final @android.annotation.Nullable java.util.Set<java.lang.String> mBaseRequiredSplitTypes\nprivate final @android.annotation.Nullable java.util.Set<java.lang.String>[] mRequiredSplitTypes\nprivate final @android.annotation.Nullable java.util.Set<java.lang.String>[] mSplitTypes\nprivate final  int mVersionCodeMajor\nprivate final  int mVersionCode\nprivate final  int mTargetSdk\nprivate final  int mBaseRevisionCode\nprivate final @android.annotation.Nullable int[] mSplitRevisionCodes\nprivate final  int mInstallLocation\nprivate final @android.annotation.NonNull android.content.pm.VerifierInfo[] mVerifiers\nprivate final @android.annotation.Nullable boolean[] mIsFeatureSplits\nprivate final  boolean mIsolatedSplits\nprivate final  boolean mSplitRequired\nprivate final  boolean mCoreApp\nprivate final  boolean mDebuggable\nprivate final  boolean mMultiArch\nprivate final  boolean mUse32bitAbi\nprivate final  boolean mExtractNativeLibs\nprivate final  boolean mProfileableByShell\nprivate final  boolean mUseEmbeddedDex\nprivate final  boolean mIsSdkLibrary\npublic  java.util.List<java.lang.String> getAllApkPaths()\npublic  long getLongVersionCode()\nprivate  boolean hasAnyRequiredSplitTypes()\nclass PackageLite extends java.lang.Object implements []\n@com.android.internal.util.DataClass(genConstructor=false, genConstDefs=false)")
423     @Deprecated
__metadata()424     private void __metadata() {}
425 
426 
427     //@formatter:on
428     // End of generated code
429 
430 }
431