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