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