1 /*
2  * Copyright (C) 2020 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 com.android.server.pm.pkg;
18 
19 import android.annotation.AppIdInt;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.Size;
23 import android.annotation.SystemApi;
24 import android.annotation.UserIdInt;
25 import android.content.pm.ApplicationInfo;
26 import android.content.pm.PackageInfo;
27 import android.content.pm.PackageManager;
28 import android.content.pm.SigningInfo;
29 import android.os.UserHandle;
30 import android.processor.immutability.Immutable;
31 import android.util.SparseArray;
32 
33 import com.android.internal.R;
34 
35 import java.io.File;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Set;
39 
40 /**
41  * A wrapper containing device-specific state for an application. It wraps the mostly stateless
42  * {@link AndroidPackage}, available through {@link #getAndroidPackage()}.
43  *
44  * Any fields whose values depend on dynamic state, disk location, enforcement policy,
45  * cross-package dependencies, system/device owner/admin configuration, etc. are placed in this
46  * interface.
47  *
48  * The backing memory is shared with the internal system server and thus there is no cost to
49  * access these objects, unless the public API equivalent {@link PackageInfo} or
50  * {@link ApplicationInfo}.
51  *
52  * This also means the data is immutable and will throw {@link UnsupportedOperationException} if
53  * any collection type is mutated.
54  *
55  * @hide
56  */
57 @SystemApi(client = SystemApi.Client.SYSTEM_SERVER)
58 @Immutable
59 public interface PackageState {
60 
61     /*
62      * TODO: Documentation
63      * TODO: Currently missing, should be exposed as API?
64      *   - keySetData
65      *   - installSource
66      *   - incrementalStates
67      */
68 
69     // Non-doc comment invisible to API consumers:
70     // Guidelines:
71     //  - All return values should prefer non-null, immutable interfaces with only exposed getters
72     //    - Unless null itself communicates something important
73     //    - If the type is a Java collection type, it must be wrapped with unmodifiable
74     //  - All type names must be non-suffixed, with any internal types being refactored to suffix
75     //    with _Internal as necessary
76     //  - No exposure of raw values that are overridden during parsing, such as CPU ABI
77     //  - Mirroring another available system or public API is not enough justification to violate
78     //    these guidelines
79 
80     /**
81      * This can be null whenever a physical APK on device is missing. This can be the result of
82      * removing an external storage device where the APK resides.
83      * <p/>
84      * This will result in the system reading the state from disk, but without being able to parse
85      * the base APK's AndroidManifest.xml to read all of its metadata. The only available data that
86      * is written and read is the minimal set required to perform other checks in the system.
87      * <p/>
88      * This is important in order to enforce uniqueness within the system, as the package, even if
89      * on a removed storage device, is still considered installed. Another package of the same
90      * application ID or declaring the same permissions or similar cannot be installed.
91      * <p/>
92      * Re-attaching the storage device to make the APK available should allow the user to use the
93      * app once the device reboots or otherwise re-scans it.
94      * <p/>
95      * This can also occur in an device OTA situation where the package is no longer parsable on
96      * an updated SDK version, causing it to be rejected, but the state associated with it retained,
97      * similarly to if the package had been uninstalled with the --keep-data option.
98      */
99     @Nullable
getAndroidPackage()100     AndroidPackage getAndroidPackage();
101 
102     /**
103      * The non-user-specific UID, or the UID if the user ID is
104      * {@link android.os.UserHandle#SYSTEM}.
105      */
106     @AppIdInt
getAppId()107     int getAppId();
108 
109     /**
110      * Retrieves effective hidden API policy for this app. The state can be dependent on
111      * {@link #getAndroidPackage()} availability and whether the app is a system app.
112      *
113      * Note that during process start, this policy may be mutated by device specific process
114      * configuration, so this value isn't truly final.
115      *
116      * @return The (mostly) final {@link ApplicationInfo.HiddenApiEnforcementPolicy} that should be
117      * applied to this package.
118      */
119     @ApplicationInfo.HiddenApiEnforcementPolicy
getHiddenApiEnforcementPolicy()120     int getHiddenApiEnforcementPolicy();
121 
122     /**
123      * @see PackageInfo#packageName
124      * @see AndroidPackage#getPackageName()
125      */
126     @NonNull
getPackageName()127     String getPackageName();
128 
129     /**
130      * @see ApplicationInfo#primaryCpuAbi
131      */
132     @Nullable
getPrimaryCpuAbi()133     String getPrimaryCpuAbi();
134 
135     /**
136      * @see ApplicationInfo#secondaryCpuAbi
137      */
138     @Nullable
getSecondaryCpuAbi()139     String getSecondaryCpuAbi();
140 
141     /**
142      * @see ApplicationInfo#seInfo
143      * @return The SE info for this package, which may be overridden by a system configured value,
144      * or null if the package isn't available.
145      */
146     @Nullable
getSeInfo()147     String getSeInfo();
148 
149     /**
150      * @return State for a user or {@link PackageUserState#DEFAULT} if the state doesn't exist.
151      */
152     @NonNull
getStateForUser(@onNull UserHandle user)153     PackageUserState getStateForUser(@NonNull UserHandle user);
154 
155     /**
156      * List of shared libraries that this package declares a dependency on. This includes all
157      * types of libraries, system or app provided and Java or native.
158      * <p/>
159      * This includes libraries declared in the manifest under the following tags:
160      * <ul>
161      *     <li>uses-library</li>
162      *     <li>uses-native-library</li>
163      *     <li>uses-sdk-library</li>
164      *     <li>uses-static-library</li>
165      * </ul>
166      */
167     @NonNull
getSharedLibraryDependencies()168     List<SharedLibrary> getSharedLibraryDependencies();
169 
170     /** Whether this represents an APEX module. This is different from an APK inside an APEX. */
isApex()171     boolean isApex();
172 
173     /**
174      * @see ApplicationInfo#PRIVATE_FLAG_PRIVILEGED
175      */
isPrivileged()176     boolean isPrivileged();
177 
178     /**
179      * @see ApplicationInfo#FLAG_SYSTEM
180      */
isSystem()181     boolean isSystem();
182 
183     /**
184      * Whether this app is on the /data partition having been upgraded from a preinstalled app on a
185      * system partition.
186      */
isUpdatedSystemApp()187     boolean isUpdatedSystemApp();
188 
189     // Methods below this comment are not yet exposed as API
190 
191     /**
192      * Value set through {@link PackageManager#setApplicationCategoryHint(String, int)}. Only
193      * applied if the application itself does not declare a category.
194      *
195      * @see AndroidPackage#getCategory()
196      * @hide
197      */
getCategoryOverride()198     int getCategoryOverride();
199 
200     /**
201      * The install time CPU override, if any. This value is written at install time
202      * and doesn't change during the life of an install. If non-null,
203      * {@link #getPrimaryCpuAbiLegacy()} will also contain the same value.
204      *
205      * @hide
206      */
207     @Nullable
getCpuAbiOverride()208     String getCpuAbiOverride();
209 
210     /**
211      * In epoch milliseconds. The last modified time of the file directory which houses the app
212      * APKs. Only updated on package update; does not track realtime modifications.
213      *
214      * @hide
215      */
getLastModifiedTime()216     long getLastModifiedTime();
217 
218     /**
219      * An aggregation across the framework of the last time an app was used for a particular reason.
220      * Keys are indexes into the array represented by {@link PackageManager.NotifyReason}, values
221      * are in epoch milliseconds.
222      *
223      * @hide
224      */
225     @Immutable.Ignore
226     @Size(PackageManager.NOTIFY_PACKAGE_USE_REASONS_COUNT)
227     @NonNull
getLastPackageUsageTime()228     long[] getLastPackageUsageTime();
229 
230     /**
231      * In epoch milliseconds. The timestamp of the last time the package on device went through
232      * an update package installation.
233      *
234      * @hide
235      */
getLastUpdateTime()236     long getLastUpdateTime();
237 
238     /**
239      * Cached here in case the physical code directory on device is unmounted.
240      * @see AndroidPackage#getLongVersionCode()
241      * @hide
242      */
getVersionCode()243     long getVersionCode();
244 
245     /**
246      * Maps mime group name to the set of Mime types in a group. Mime groups declared by app are
247      * populated with empty sets at construction. Mime groups can not be created/removed at runtime,
248      * thus keys in this map should not change.
249      *
250      * @hide
251      */
252     @NonNull
getMimeGroups()253     Map<String, Set<String>> getMimeGroups();
254 
255     /**
256      * @see AndroidPackage#getPath()
257      * @hide
258      */
259     @NonNull
getPath()260     File getPath();
261 
262     /**
263      * Whether the package shares the same user ID as other packages
264      * @hide
265      */
hasSharedUser()266     boolean hasSharedUser();
267 
268 
269     /**
270      * Whether this app needs to be restore during next install/update.
271      * E.g. if an app was installed as archived and never had a chance to restore its data.
272      * @hide
273      */
isPendingRestore()274     boolean isPendingRestore();
275 
276     /**
277      * Retrieves the shared user app ID. Note that the actual shared user data is not available here
278      * and must be queried separately.
279      *
280      * @return the app ID of the shared user that this package is a part of, or -1 if it's not part
281      * of a shared user.
282      * @hide
283      */
getSharedUserAppId()284     int getSharedUserAppId();
285 
286     /** @hide */
287     @Immutable.Ignore
288     @NonNull
getSigningInfo()289     SigningInfo getSigningInfo();
290 
291     /** @hide */
292     @Immutable.Ignore
293     @NonNull
getUserStates()294     SparseArray<? extends PackageUserState> getUserStates();
295 
296     /**
297      * @return the result of {@link #getUserStates()}.get(userId) or
298      * {@link PackageUserState#DEFAULT} if the state doesn't exist.
299      * @hide
300      */
301     @NonNull
getUserStateOrDefault(@serIdInt int userId)302     default PackageUserState getUserStateOrDefault(@UserIdInt int userId) {
303         PackageUserState userState = getUserStates().get(userId);
304         return userState == null ? PackageUserState.DEFAULT : userState;
305     }
306 
307     /**
308      * The actual files resolved for each shared library.
309      *
310      * @see R.styleable#AndroidManifestUsesLibrary
311      * @hide
312      */
313     @NonNull
getUsesLibraryFiles()314     List<String> getUsesLibraryFiles();
315 
316     /**
317      * @see R.styleable#AndroidManifestUsesSdkLibrary
318      * @hide
319      */
320     @Immutable.Ignore
321     @NonNull
getUsesSdkLibraries()322     String[] getUsesSdkLibraries();
323 
324     /**
325      * @see R.styleable#AndroidManifestUsesSdkLibrary_versionMajor
326      * @hide
327      */
328     @Immutable.Ignore
329     @NonNull
getUsesSdkLibrariesVersionsMajor()330     long[] getUsesSdkLibrariesVersionsMajor();
331 
332     /**
333      * @see R.styleable#AndroidManifestUsesSdkLibrary_optional
334      * @hide
335      */
336     @Immutable.Ignore
337     @NonNull
getUsesSdkLibrariesOptional()338     boolean[] getUsesSdkLibrariesOptional();
339 
340     /**
341      * @see R.styleable#AndroidManifestUsesStaticLibrary
342      * @hide
343      */
344     @Immutable.Ignore
345     @NonNull
getUsesStaticLibraries()346     String[] getUsesStaticLibraries();
347 
348     /**
349      * @see R.styleable#AndroidManifestUsesStaticLibrary_version
350      * @hide
351      */
352     @Immutable.Ignore
353     @NonNull
getUsesStaticLibrariesVersions()354     long[] getUsesStaticLibrariesVersions();
355 
356     /**
357      * @see AndroidPackage#getVolumeUuid()
358      * @hide
359      */
360     @Nullable
getVolumeUuid()361     String getVolumeUuid();
362 
363     /**
364      * @see AndroidPackage#isDefaultToDeviceProtectedStorage()
365      * @hide
366      */
isDefaultToDeviceProtectedStorage()367     boolean isDefaultToDeviceProtectedStorage();
368 
369     /**
370      * @see AndroidPackage#isExternalStorage()
371      * @hide
372      */
isExternalStorage()373     boolean isExternalStorage();
374 
375     /**
376      * Whether a package was installed --force-queryable such that it is always queryable by any
377      * package, regardless of their manifest content.
378      *
379      * @hide
380      */
isForceQueryableOverride()381     boolean isForceQueryableOverride();
382 
383     /**
384      * Whether a package is treated as hidden until it is installed for a user.
385      *
386      * @see PackageManager#MATCH_HIDDEN_UNTIL_INSTALLED_COMPONENTS
387      * @see PackageManager#setSystemAppState
388      * @hide
389      */
isHiddenUntilInstalled()390     boolean isHiddenUntilInstalled();
391 
392     /**
393      * @see com.android.server.pm.permission.UserPermissionState
394      * @hide
395      */
isInstallPermissionsFixed()396     boolean isInstallPermissionsFixed();
397 
398     /**
399      * @see ApplicationInfo#PRIVATE_FLAG_ODM
400      * @hide
401      */
isOdm()402     boolean isOdm();
403 
404     /**
405      * @see ApplicationInfo#PRIVATE_FLAG_OEM
406      * @hide
407      */
isOem()408     boolean isOem();
409 
410     /**
411      * @see ApplicationInfo#PRIVATE_FLAG_PRODUCT
412      * @hide
413      */
isProduct()414     boolean isProduct();
415 
416     /**
417      * @see ApplicationInfo#PRIVATE_FLAG_REQUIRED_FOR_SYSTEM_USER
418      * @hide
419      */
isRequiredForSystemUser()420     boolean isRequiredForSystemUser();
421 
422     /**
423      * @see ApplicationInfo#PRIVATE_FLAG_SYSTEM_EXT
424      * @hide
425      */
isSystemExt()426     boolean isSystemExt();
427 
428     /**
429      * Whether or not an update is available. Ostensibly only for instant apps.
430      * @hide
431      */
isUpdateAvailable()432     boolean isUpdateAvailable();
433 
434     /**
435      * Whether this app is packaged in an updated apex.
436      *
437      * @hide
438      */
isApkInUpdatedApex()439     boolean isApkInUpdatedApex();
440 
441     /**
442      * @see ApplicationInfo#PRIVATE_FLAG_VENDOR
443      * @hide
444      */
isVendor()445     boolean isVendor();
446 
447     /**
448      * The name of the APEX module containing this package, if it is an APEX or APK-in-APEX.
449      * @hide
450      */
451     @Nullable
getApexModuleName()452     String getApexModuleName();
453 
454     /**
455      * @see ApplicationInfo#FLAG_PERSISTENT
456      * @see R.styleable#AndroidManifestApplication_persistent
457      * @hide
458      */
isPersistent()459     boolean isPersistent();
460 
461     /**
462      * @see ApplicationInfo#targetSdkVersion
463      * @see R.styleable#AndroidManifestUsesSdk_targetSdkVersion
464      * @hide
465      */
getTargetSdkVersion()466     int getTargetSdkVersion();
467 
468     /**
469      * @see R.styleable#AndroidManifestRestrictUpdate
470      * @hide
471      */
472     @Immutable.Ignore
473     @Nullable
getRestrictUpdateHash()474     byte[] getRestrictUpdateHash();
475 
476     /**
477      * whether the package has been scanned as a stopped system app. A package will be
478      * scanned in the stopped state if it is a system app that has a launcher entry and is
479      * <b>not</b> exempted by {@code <initial-package-state>} tag, and is not an APEX
480      * @hide
481      */
isScannedAsStoppedSystemApp()482     boolean isScannedAsStoppedSystemApp();
483 }
484