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 com.android.server.pm;
18 
19 import static android.content.pm.Flags.improveInstallFreeze;
20 import static android.content.pm.PackageInstaller.SessionParams.USER_ACTION_UNSPECIFIED;
21 import static android.content.pm.PackageManager.INSTALL_REASON_UNKNOWN;
22 import static android.content.pm.PackageManager.INSTALL_SCENARIO_DEFAULT;
23 import static android.content.pm.PackageManager.INSTALL_SUCCEEDED;
24 import static android.os.Process.INVALID_UID;
25 
26 import static com.android.server.art.model.DexoptResult.DexContainerFileDexoptResult;
27 import static com.android.server.art.model.DexoptResult.PackageDexoptResult;
28 import static com.android.server.pm.PackageManagerService.EMPTY_INT_ARRAY;
29 import static com.android.server.pm.PackageManagerService.SCAN_AS_INSTANT_APP;
30 import static com.android.server.pm.PackageManagerService.TAG;
31 
32 import android.annotation.NonNull;
33 import android.annotation.Nullable;
34 import android.apex.ApexInfo;
35 import android.app.AppOpsManager;
36 import android.content.pm.ArchivedPackageParcel;
37 import android.content.pm.DataLoaderType;
38 import android.content.pm.IPackageInstallObserver2;
39 import android.content.pm.PackageInstaller;
40 import android.content.pm.PackageManager;
41 import android.content.pm.SharedLibraryInfo;
42 import android.content.pm.SigningDetails;
43 import android.content.pm.parsing.PackageLite;
44 import android.content.pm.verify.domain.DomainSet;
45 import android.net.Uri;
46 import android.os.Build;
47 import android.os.Process;
48 import android.os.UserHandle;
49 import android.util.ArrayMap;
50 import android.util.ExceptionUtils;
51 import android.util.Slog;
52 import android.util.SparseArray;
53 
54 import com.android.internal.pm.parsing.pkg.ParsedPackage;
55 import com.android.internal.pm.pkg.parsing.ParsingPackageUtils;
56 import com.android.internal.util.ArrayUtils;
57 import com.android.server.art.model.DexoptResult;
58 import com.android.server.pm.pkg.AndroidPackage;
59 import com.android.server.pm.pkg.PackageState;
60 import com.android.server.pm.pkg.PackageStateInternal;
61 
62 import java.io.File;
63 import java.util.ArrayList;
64 import java.util.LinkedHashSet;
65 import java.util.List;
66 
67 final class InstallRequest {
68     private final int mUserId;
69     @Nullable
70     private final InstallArgs mInstallArgs;
71     @Nullable
72     private Runnable mPostInstallRunnable;
73     @Nullable
74     private PackageRemovedInfo mRemovedInfo;
75 
76     @PackageManagerService.ScanFlags
77     private int mScanFlags;
78     @ParsingPackageUtils.ParseFlags
79     private int mParseFlags;
80     private boolean mReplace;
81 
82     @Nullable /* The original package's name if it is being replaced, otherwise {@code null} */
83     private String mExistingPackageName;
84     /** parsed package to be scanned */
85     @Nullable
86     private ParsedPackage mParsedPackage;
87     @Nullable
88     private ArchivedPackageParcel mArchivedPackage;
89     private boolean mClearCodeCache;
90     private boolean mSystem;
91     @Nullable
92     private PackageSetting mOriginalPs;
93     @Nullable
94     private PackageSetting mDisabledPs;
95 
96     /** Package Installed Info */
97     @Nullable
98     private String mName;
99     private int mAppId = INVALID_UID;
100     // The set of users that originally had this package installed.
101     @Nullable
102     private int[] mOrigUsers;
103     // The set of users that now have this package installed.
104     @Nullable
105     private int[] mNewUsers;
106     @Nullable
107     private AndroidPackage mPkg;
108     @Nullable
109     private PackageLite mPackageLite;
110     private int mReturnCode;
111     private int mInternalErrorCode;
112     @Nullable
113     private String mReturnMsg;
114     // The set of packages consuming this shared library or null if no consumers exist.
115     @Nullable
116     private ArrayList<AndroidPackage> mLibraryConsumers;
117     @Nullable
118     private PackageFreezer mFreezer;
119     /** The package this package replaces */
120     @Nullable
121     private String mOrigPackage;
122     @Nullable
123     private String mOrigPermission;
124     // The ApexInfo returned by ApexManager#installPackage, used by rebootless APEX install
125     @Nullable
126     private ApexInfo mApexInfo;
127 
128     /**
129      * For tracking {@link PackageState#getApexModuleName()}.
130      */
131     @Nullable
132     private String mApexModuleName;
133 
134     /**
135      * The title of the responsible installer for the archive behavior used
136      */
137     @Nullable
138     private SparseArray<String> mResponsibleInstallerTitles;
139 
140     @Nullable
141     private ScanResult mScanResult;
142 
143     private boolean mIsInstallInherit;
144     private boolean mIsInstallForUsers;
145 
146     @Nullable
147     private final PackageMetrics mPackageMetrics;
148     private final int mSessionId;
149     private final int mRequireUserAction;
150 
151     private int mDexoptStatus;
152 
153     @NonNull
154     private int[] mFirstTimeBroadcastUserIds = EMPTY_INT_ARRAY;
155     @NonNull
156     private int[] mFirstTimeBroadcastInstantUserIds = EMPTY_INT_ARRAY;
157     @NonNull
158     private int[] mUpdateBroadcastUserIds = EMPTY_INT_ARRAY;
159     @NonNull
160     private int[] mUpdateBroadcastInstantUserIds = EMPTY_INT_ARRAY;
161 
162     @NonNull
163     private final ArrayList<String> mWarnings = new ArrayList<>();
164 
165     @Nullable
166     private DomainSet mPreVerifiedDomains;
167 
168     private int mInstallerUidForInstallExisting = INVALID_UID;
169 
170     private final boolean mHasAppMetadataFileFromInstaller;
171 
172     // New install
InstallRequest(InstallingSession params)173     InstallRequest(InstallingSession params) {
174         mUserId = params.getUser().getIdentifier();
175         mInstallArgs = new InstallArgs(params.mOriginInfo, params.mMoveInfo, params.mObserver,
176                 params.mInstallFlags, params.mDevelopmentInstallFlags, params.mInstallSource,
177                 params.mVolumeUuid, params.getUser(), null /*instructionSets*/,
178                 params.mPackageAbiOverride, params.mPermissionStates,
179                 params.mAllowlistedRestrictedPermissions, params.mAutoRevokePermissionsMode,
180                 params.mTraceMethod, params.mTraceCookie, params.mSigningDetails,
181                 params.mInstallReason, params.mInstallScenario, params.mForceQueryableOverride,
182                 params.mDataLoaderType, params.mPackageSource,
183                 params.mApplicationEnabledSettingPersistent, params.mDexoptCompilerFilter);
184         mPackageLite = params.mPackageLite;
185         mPackageMetrics = new PackageMetrics(this);
186         mIsInstallInherit = params.mIsInherit;
187         mSessionId = params.mSessionId;
188         mRequireUserAction = params.mRequireUserAction;
189         mPreVerifiedDomains = params.mPreVerifiedDomains;
190         mHasAppMetadataFileFromInstaller = params.mHasAppMetadataFile;
191     }
192 
193     // Install existing package as user
InstallRequest(int userId, int returnCode, AndroidPackage pkg, int[] newUsers, Runnable runnable, int appId, int installerUid, boolean isSystem)194     InstallRequest(int userId, int returnCode, AndroidPackage pkg, int[] newUsers,
195             Runnable runnable, int appId, int installerUid, boolean isSystem) {
196         mUserId = userId;
197         mInstallArgs = null;
198         mReturnCode = returnCode;
199         mPkg = pkg;
200         mNewUsers = newUsers;
201         mPostInstallRunnable = runnable;
202         mPackageMetrics = new PackageMetrics(this);
203         mIsInstallForUsers = true;
204         mSessionId = -1;
205         mRequireUserAction = USER_ACTION_UNSPECIFIED;
206         mAppId = appId;
207         mInstallerUidForInstallExisting = installerUid;
208         mSystem = isSystem;
209         mHasAppMetadataFileFromInstaller = false;
210     }
211 
212     // addForInit
InstallRequest(ParsedPackage parsedPackage, int parseFlags, int scanFlags, @Nullable UserHandle user, ScanResult scanResult, PackageSetting disabledPs)213     InstallRequest(ParsedPackage parsedPackage, int parseFlags, int scanFlags,
214             @Nullable UserHandle user, ScanResult scanResult, PackageSetting disabledPs) {
215         if (user != null) {
216             mUserId = user.getIdentifier();
217         } else {
218             // APEX
219             mUserId = UserHandle.USER_SYSTEM;
220         }
221         mInstallArgs = null;
222         mParsedPackage = parsedPackage;
223         mArchivedPackage = null;
224         mParseFlags = parseFlags;
225         mScanFlags = scanFlags;
226         mScanResult = scanResult;
227         mPackageMetrics = null; // No logging from this code path
228         mSessionId = -1;
229         mRequireUserAction = USER_ACTION_UNSPECIFIED;
230         mDisabledPs = disabledPs;
231         mHasAppMetadataFileFromInstaller = false;
232     }
233 
234     @Nullable
getName()235     public String getName() {
236         return mName;
237     }
238 
239     @Nullable
getReturnMsg()240     public String getReturnMsg() {
241         return mReturnMsg;
242     }
243 
244     @Nullable
getOriginInfo()245     public OriginInfo getOriginInfo() {
246         return mInstallArgs == null ? null : mInstallArgs.mOriginInfo;
247     }
248 
249     @Nullable
getRemovedInfo()250     public PackageRemovedInfo getRemovedInfo() {
251         return mRemovedInfo;
252     }
253 
254     @Nullable
getOrigPackage()255     public String getOrigPackage() {
256         return mOrigPackage;
257     }
258 
259     @Nullable
getOrigPermission()260     public String getOrigPermission() {
261         return mOrigPermission;
262     }
263 
264     @Nullable
getCodeFile()265     public File getCodeFile() {
266         return mInstallArgs == null ? null : mInstallArgs.mCodeFile;
267     }
268 
269     @Nullable
getCodePath()270     public String getCodePath() {
271         return (mInstallArgs != null && mInstallArgs.mCodeFile != null)
272                 ? mInstallArgs.mCodeFile.getAbsolutePath() : null;
273     }
274 
275     @Nullable
getAbiOverride()276     public String getAbiOverride() {
277         return mInstallArgs == null ? null : mInstallArgs.mAbiOverride;
278     }
279 
getReturnCode()280     public int getReturnCode() {
281         return mReturnCode;
282     }
283 
getInternalErrorCode()284     public int getInternalErrorCode() {
285         return mInternalErrorCode;
286     }
287 
288     @Nullable
getObserver()289     public IPackageInstallObserver2 getObserver() {
290         return mInstallArgs == null ? null : mInstallArgs.mObserver;
291     }
292 
isInstallMove()293     public boolean isInstallMove() {
294         return mInstallArgs != null && mInstallArgs.mMoveInfo != null;
295     }
296 
297     @Nullable
getMoveToUuid()298     public String getMoveToUuid() {
299         return (mInstallArgs != null && mInstallArgs.mMoveInfo != null)
300                 ? mInstallArgs.mMoveInfo.mToUuid : null;
301     }
302 
303     @Nullable
getMovePackageName()304     public String getMovePackageName() {
305         return (mInstallArgs != null && mInstallArgs.mMoveInfo != null)
306                 ? mInstallArgs.mMoveInfo.mPackageName : null;
307     }
308 
309     @Nullable
getMoveFromCodePath()310     public String getMoveFromCodePath() {
311         return (mInstallArgs != null && mInstallArgs.mMoveInfo != null)
312                 ? mInstallArgs.mMoveInfo.mFromCodePath : null;
313     }
314 
315     @Nullable
getOldCodeFile()316     public File getOldCodeFile() {
317         return (mRemovedInfo != null && mRemovedInfo.mArgs != null)
318                 ? mRemovedInfo.mArgs.getCodeFile() : null;
319     }
320 
321     @Nullable
getOldInstructionSet()322     public String[] getOldInstructionSet() {
323         return (mRemovedInfo != null && mRemovedInfo.mArgs != null)
324                 ? mRemovedInfo.mArgs.getInstructionSets() : null;
325     }
326 
getUser()327     public UserHandle getUser() {
328         return new UserHandle(mUserId);
329     }
330 
getUserId()331     public int getUserId() {
332         return mUserId;
333     }
334 
getInstallFlags()335     public int getInstallFlags() {
336         return mInstallArgs == null ? 0 : mInstallArgs.mInstallFlags;
337     }
338 
getDevelopmentInstallFlags()339     public int getDevelopmentInstallFlags() {
340         return mInstallArgs == null ? 0 : mInstallArgs.mDevelopmentInstallFlags;
341     }
342 
getInstallReason()343     public int getInstallReason() {
344         return mInstallArgs == null ? INSTALL_REASON_UNKNOWN : mInstallArgs.mInstallReason;
345     }
346 
347     @Nullable
getVolumeUuid()348     public String getVolumeUuid() {
349         return mInstallArgs == null ? null : mInstallArgs.mVolumeUuid;
350     }
351 
352     @Nullable
getPkg()353     public AndroidPackage getPkg() {
354         return mPkg;
355     }
356 
357     @Nullable
getPackageLite()358     public PackageLite getPackageLite() {
359         return mPackageLite;
360     }
361 
362     @Nullable
getTraceMethod()363     public String getTraceMethod() {
364         return mInstallArgs == null ? null : mInstallArgs.mTraceMethod;
365     }
366 
getTraceCookie()367     public int getTraceCookie() {
368         return mInstallArgs == null ? 0 : mInstallArgs.mTraceCookie;
369     }
370 
isUpdate()371     public boolean isUpdate() {
372         return mRemovedInfo != null && mRemovedInfo.mRemovedPackage != null;
373     }
374 
isArchived()375     public boolean isArchived() {
376         return PackageInstallerSession.isArchivedInstallation(getInstallFlags());
377     }
378 
hasAppMetadataFile()379     public boolean hasAppMetadataFile() {
380         return mHasAppMetadataFileFromInstaller;
381     }
382 
383     @Nullable
getRemovedPackage()384     public String getRemovedPackage() {
385         return mRemovedInfo != null ? mRemovedInfo.mRemovedPackage : null;
386     }
387 
isInstallExistingForUser()388     public boolean isInstallExistingForUser() {
389         return mInstallArgs == null;
390     }
391 
392     @Nullable
getInstallSource()393     public InstallSource getInstallSource() {
394         return mInstallArgs == null ? null : mInstallArgs.mInstallSource;
395     }
396 
397     @Nullable
getInstallerPackageName()398     public String getInstallerPackageName() {
399         return (mInstallArgs != null && mInstallArgs.mInstallSource != null)
400                 ? mInstallArgs.mInstallSource.mInstallerPackageName : null;
401     }
402 
getInstallerPackageUid()403     public int getInstallerPackageUid() {
404         return (mInstallArgs != null && mInstallArgs.mInstallSource != null)
405                 ? mInstallArgs.mInstallSource.mInstallerPackageUid
406                 : mInstallerUidForInstallExisting;
407     }
408 
getDataLoaderType()409     public int getDataLoaderType() {
410         return mInstallArgs == null ? DataLoaderType.NONE : mInstallArgs.mDataLoaderType;
411     }
412 
getSignatureSchemeVersion()413     public int getSignatureSchemeVersion() {
414         return mInstallArgs == null ? SigningDetails.SignatureSchemeVersion.UNKNOWN
415                 : mInstallArgs.mSigningDetails.getSignatureSchemeVersion();
416     }
417 
418     @NonNull
getSigningDetails()419     public SigningDetails getSigningDetails() {
420         return mInstallArgs == null ? SigningDetails.UNKNOWN : mInstallArgs.mSigningDetails;
421     }
422 
423     @Nullable
getOriginUri()424     public Uri getOriginUri() {
425         return mInstallArgs == null ? null : Uri.fromFile(mInstallArgs.mOriginInfo.mResolvedFile);
426     }
427 
428     @Nullable
getApexInfo()429     public ApexInfo getApexInfo() {
430         return mApexInfo;
431     }
432 
433     @Nullable
getApexModuleName()434     public String getApexModuleName() {
435         return mApexModuleName;
436     }
437 
438     @Nullable
getResponsibleInstallerTitles()439     public SparseArray<String> getResponsibleInstallerTitles() {
440         return mResponsibleInstallerTitles;
441     }
442 
isRollback()443     public boolean isRollback() {
444         return mInstallArgs != null
445                 && mInstallArgs.mInstallReason == PackageManager.INSTALL_REASON_ROLLBACK;
446     }
447 
448     @Nullable
getNewUsers()449     public int[] getNewUsers() {
450         return mNewUsers;
451     }
452 
453     @Nullable
getOriginUsers()454     public int[] getOriginUsers() {
455         return mOrigUsers;
456     }
457 
getAppId()458     public int getAppId() {
459         return mAppId;
460     }
461 
462     @Nullable
getPermissionStates()463     public ArrayMap<String, Integer> getPermissionStates() {
464         return mInstallArgs == null ? null : mInstallArgs.mPermissionStates;
465     }
466 
467     @Nullable
getLibraryConsumers()468     public ArrayList<AndroidPackage> getLibraryConsumers() {
469         return mLibraryConsumers;
470     }
471 
472     @Nullable
getAllowlistedRestrictedPermissions()473     public List<String> getAllowlistedRestrictedPermissions() {
474         return mInstallArgs == null ? null : mInstallArgs.mAllowlistedRestrictedPermissions;
475     }
476 
getAutoRevokePermissionsMode()477     public int getAutoRevokePermissionsMode() {
478         return mInstallArgs == null
479                 ? AppOpsManager.MODE_DEFAULT : mInstallArgs.mAutoRevokePermissionsMode;
480     }
481 
getPackageSource()482     public int getPackageSource() {
483         return mInstallArgs == null
484                 ? PackageInstaller.PACKAGE_SOURCE_UNSPECIFIED : mInstallArgs.mPackageSource;
485     }
486 
getInstallScenario()487     public int getInstallScenario() {
488         return mInstallArgs == null ? INSTALL_SCENARIO_DEFAULT : mInstallArgs.mInstallScenario;
489     }
490 
491     @Nullable
getParsedPackage()492     public ParsedPackage getParsedPackage() {
493         return mParsedPackage;
494     }
495 
496     @Nullable
getArchivedPackage()497     public ArchivedPackageParcel getArchivedPackage() { return mArchivedPackage; }
498 
499     @ParsingPackageUtils.ParseFlags
getParseFlags()500     public int getParseFlags() {
501         return mParseFlags;
502     }
503 
504     @PackageManagerService.ScanFlags
getScanFlags()505     public int getScanFlags() {
506         return mScanFlags;
507     }
508 
509     @Nullable
getExistingPackageName()510     public String getExistingPackageName() {
511         return mExistingPackageName;
512     }
513 
514     @Nullable
getScanRequestOldPackage()515     public AndroidPackage getScanRequestOldPackage() {
516         assertScanResultExists();
517         return mScanResult.mRequest.mOldPkg;
518     }
519 
isClearCodeCache()520     public boolean isClearCodeCache() {
521         return mClearCodeCache;
522     }
523 
isInstallReplace()524     public boolean isInstallReplace() {
525         return mReplace;
526     }
527 
isInstallSystem()528     public boolean isInstallSystem() {
529         return mSystem;
530     }
531 
isInstallInherit()532     public boolean isInstallInherit() {
533         return mIsInstallInherit;
534     }
535 
isInstallForUsers()536     public boolean isInstallForUsers() {
537         return mIsInstallForUsers;
538     }
539 
isInstallFromAdb()540     public boolean isInstallFromAdb() {
541         return mInstallArgs != null
542                 && (mInstallArgs.mInstallFlags & PackageManager.INSTALL_FROM_ADB) != 0;
543     }
544 
545     @Nullable
getOriginalPackageSetting()546     public PackageSetting getOriginalPackageSetting() {
547         return mOriginalPs;
548     }
549 
550     @Nullable
getDisabledPackageSetting()551     public PackageSetting getDisabledPackageSetting() {
552         return mDisabledPs;
553     }
554 
555     @Nullable
getScanRequestOldPackageSetting()556     public PackageSetting getScanRequestOldPackageSetting() {
557         assertScanResultExists();
558         return mScanResult.mRequest.mOldPkgSetting;
559     }
560 
561     @Nullable
getScanRequestOriginalPackageSetting()562     public PackageSetting getScanRequestOriginalPackageSetting() {
563         assertScanResultExists();
564         return mScanResult.mRequest.mOriginalPkgSetting;
565     }
566 
567     @Nullable
getScanRequestPackageSetting()568     public PackageSetting getScanRequestPackageSetting() {
569         assertScanResultExists();
570         return mScanResult.mRequest.mPkgSetting;
571     }
572 
573     @Nullable
getScanRequestDisabledPackageSetting()574     public PackageSetting getScanRequestDisabledPackageSetting() {
575         assertScanResultExists();
576         return mScanResult.mRequest.mDisabledPkgSetting;
577     }
578 
579     @Nullable
getRealPackageName()580     public String getRealPackageName() {
581         assertScanResultExists();
582         return mScanResult.mRequest.mRealPkgName;
583     }
584 
585     @Nullable
getChangedAbiCodePath()586     public List<String> getChangedAbiCodePath() {
587         assertScanResultExists();
588         return mScanResult.mChangedAbiCodePath;
589     }
590 
isApplicationEnabledSettingPersistent()591     public boolean isApplicationEnabledSettingPersistent() {
592         return mInstallArgs == null ? false : mInstallArgs.mApplicationEnabledSettingPersistent;
593     }
594 
isForceQueryableOverride()595     public boolean isForceQueryableOverride() {
596         return mInstallArgs != null && mInstallArgs.mForceQueryableOverride;
597     }
598 
599     @Nullable
getSdkSharedLibraryInfo()600     public SharedLibraryInfo getSdkSharedLibraryInfo() {
601         assertScanResultExists();
602         return mScanResult.mSdkSharedLibraryInfo;
603     }
604 
605     @Nullable
getStaticSharedLibraryInfo()606     public SharedLibraryInfo getStaticSharedLibraryInfo() {
607         assertScanResultExists();
608         return mScanResult.mStaticSharedLibraryInfo;
609     }
610 
611     @Nullable
getDynamicSharedLibraryInfos()612     public List<SharedLibraryInfo> getDynamicSharedLibraryInfos() {
613         assertScanResultExists();
614         return mScanResult.mDynamicSharedLibraryInfos;
615     }
616 
617     @Nullable
getScannedPackageSetting()618     public PackageSetting getScannedPackageSetting() {
619         assertScanResultExists();
620         return mScanResult.mPkgSetting;
621     }
622 
623     @Nullable
getRealPackageSetting()624     public PackageSetting getRealPackageSetting() {
625         // TODO: Fix this to have 1 mutable PackageSetting for scan/install. If the previous
626         //  setting needs to be passed to have a comparison, hide it behind an immutable
627         //  interface. There's no good reason to have 3 different ways to access the real
628         //  PackageSetting object, only one of which is actually correct.
629         PackageSetting realPkgSetting = isExistingSettingCopied()
630                 ? getScanRequestPackageSetting() : getScannedPackageSetting();
631         if (realPkgSetting == null) {
632             realPkgSetting = getScannedPackageSetting();
633         }
634         return realPkgSetting;
635     }
636 
isExistingSettingCopied()637     public boolean isExistingSettingCopied() {
638         assertScanResultExists();
639         return mScanResult.mExistingSettingCopied;
640     }
641 
642     /**
643      * Whether the original PackageSetting needs to be updated with
644      * a new app ID. Useful when leaving a sharedUserId.
645      */
needsNewAppId()646     public boolean needsNewAppId() {
647         assertScanResultExists();
648         return mScanResult.mPreviousAppId != Process.INVALID_UID;
649     }
650 
getPreviousAppId()651     public int getPreviousAppId() {
652         assertScanResultExists();
653         return mScanResult.mPreviousAppId;
654     }
655 
isPlatformPackage()656     public boolean isPlatformPackage() {
657         assertScanResultExists();
658         return mScanResult.mRequest.mIsPlatformPackage;
659     }
660 
isInstantInstall()661     public boolean isInstantInstall() {
662         return (mScanFlags & SCAN_AS_INSTANT_APP) != 0;
663     }
664 
assertScanResultExists()665     public void assertScanResultExists() {
666         if (mScanResult == null) {
667             // Should not happen. This indicates a bug in the installation code flow
668             if (Build.IS_USERDEBUG || Build.IS_ENG) {
669                 throw new IllegalStateException("ScanResult cannot be null.");
670             } else {
671                 Slog.e(TAG, "ScanResult is null and it should not happen");
672             }
673         }
674     }
675 
getSessionId()676     public int getSessionId() {
677         return mSessionId;
678     }
679 
getRequireUserAction()680     public int getRequireUserAction() {
681         return mRequireUserAction;
682     }
683 
getDexoptStatus()684     public int getDexoptStatus() {
685         return mDexoptStatus;
686     }
687 
isAllNewUsers()688     public boolean isAllNewUsers() {
689         return mOrigUsers == null || mOrigUsers.length == 0;
690     }
getFirstTimeBroadcastUserIds()691     public int[] getFirstTimeBroadcastUserIds() {
692         return mFirstTimeBroadcastUserIds;
693     }
694 
getFirstTimeBroadcastInstantUserIds()695     public int[] getFirstTimeBroadcastInstantUserIds() {
696         return mFirstTimeBroadcastInstantUserIds;
697     }
698 
getUpdateBroadcastUserIds()699     public int[] getUpdateBroadcastUserIds() {
700         return mUpdateBroadcastUserIds;
701     }
702 
getUpdateBroadcastInstantUserIds()703     public int[] getUpdateBroadcastInstantUserIds() {
704         return mUpdateBroadcastInstantUserIds;
705     }
706 
707     @NonNull
getWarnings()708     public ArrayList<String> getWarnings() {
709         return mWarnings;
710     }
711 
712     @Nullable
getDexoptCompilerFilter()713     public String getDexoptCompilerFilter() {
714         return mInstallArgs != null ? mInstallArgs.mDexoptCompilerFilter : null;
715     }
716 
setScanFlags(int scanFlags)717     public void setScanFlags(int scanFlags) {
718         mScanFlags = scanFlags;
719     }
720 
closeFreezer()721     public void closeFreezer() {
722         if (mFreezer != null) {
723             mFreezer.close();
724         }
725     }
726 
setPostInstallRunnable(Runnable runnable)727     public void setPostInstallRunnable(Runnable runnable) {
728         mPostInstallRunnable = runnable;
729     }
730 
hasPostInstallRunnable()731     public boolean hasPostInstallRunnable() {
732         return mPostInstallRunnable != null;
733     }
734 
runPostInstallRunnable()735     public void runPostInstallRunnable() {
736         if (mPostInstallRunnable != null) {
737             mPostInstallRunnable.run();
738         }
739     }
740 
setCodeFile(File codeFile)741     public void setCodeFile(File codeFile) {
742         if (mInstallArgs != null) {
743             mInstallArgs.mCodeFile = codeFile;
744         }
745     }
746 
setError(int code, String msg)747     public void setError(int code, String msg) {
748         setReturnCode(code);
749         setReturnMessage(msg);
750         Slog.w(TAG, msg);
751         if (mPackageMetrics != null) {
752             mPackageMetrics.onInstallFailed();
753         }
754     }
755 
setError(PackageManagerException e)756     public void setError(PackageManagerException e) {
757         setError(null, e);
758     }
759 
setError(String msg, PackageManagerException e)760     public void setError(String msg, PackageManagerException e) {
761         mInternalErrorCode = e.internalErrorCode;
762         mReturnCode = e.error;
763         setReturnMessage(ExceptionUtils.getCompleteMessage(msg, e));
764         Slog.w(TAG, msg, e);
765         if (mPackageMetrics != null) {
766             mPackageMetrics.onInstallFailed();
767         }
768     }
769 
setReturnCode(int returnCode)770     public void setReturnCode(int returnCode) {
771         mReturnCode = returnCode;
772     }
773 
setReturnMessage(String returnMsg)774     public void setReturnMessage(String returnMsg) {
775         mReturnMsg = returnMsg;
776     }
777 
setApexInfo(ApexInfo apexInfo)778     public void setApexInfo(ApexInfo apexInfo) {
779         mApexInfo = apexInfo;
780     }
781 
setApexModuleName(@ullable String apexModuleName)782     public void setApexModuleName(@Nullable String apexModuleName) {
783         mApexModuleName = apexModuleName;
784     }
785 
setResponsibleInstallerTitles( @onNull SparseArray<String> responsibleInstallerTitles)786     public void setResponsibleInstallerTitles(
787             @NonNull SparseArray<String> responsibleInstallerTitles) {
788         mResponsibleInstallerTitles = responsibleInstallerTitles;
789     }
790 
setPkg(AndroidPackage pkg)791     public void setPkg(AndroidPackage pkg) {
792         mPkg = pkg;
793     }
794 
setAppId(int appId)795     public void setAppId(int appId) {
796         mAppId = appId;
797     }
798 
setNewUsers(int[] newUsers)799     public void setNewUsers(int[] newUsers) {
800         mNewUsers = newUsers;
801         populateBroadcastUsers();
802     }
803 
setOriginPackage(String originPackage)804     public void setOriginPackage(String originPackage) {
805         mOrigPackage = originPackage;
806     }
807 
setOriginPermission(String originPermission)808     public void setOriginPermission(String originPermission) {
809         mOrigPermission = originPermission;
810     }
811 
setName(String packageName)812     public void setName(String packageName) {
813         mName = packageName;
814     }
815 
setOriginUsers(int[] userIds)816     public void setOriginUsers(int[] userIds) {
817         mOrigUsers = userIds;
818     }
819 
setFreezer(PackageFreezer freezer)820     public void setFreezer(PackageFreezer freezer) {
821         mFreezer = freezer;
822     }
823 
setRemovedInfo(PackageRemovedInfo removedInfo)824     public void setRemovedInfo(PackageRemovedInfo removedInfo) {
825         mRemovedInfo = removedInfo;
826     }
827 
setLibraryConsumers(ArrayList<AndroidPackage> libraryConsumers)828     public void setLibraryConsumers(ArrayList<AndroidPackage> libraryConsumers) {
829         mLibraryConsumers = libraryConsumers;
830     }
831 
setPrepareResult(boolean replace, int scanFlags, int parseFlags, PackageState existingPackageState, ParsedPackage packageToScan, ArchivedPackageParcel archivedPackage, boolean clearCodeCache, boolean system, PackageSetting originalPs, PackageSetting disabledPs)832     public void setPrepareResult(boolean replace, int scanFlags,
833             int parseFlags, PackageState existingPackageState,
834             ParsedPackage packageToScan, ArchivedPackageParcel archivedPackage,
835             boolean clearCodeCache, boolean system,
836             PackageSetting originalPs, PackageSetting disabledPs) {
837         mReplace = replace;
838         mScanFlags = scanFlags;
839         mParseFlags = parseFlags;
840         mExistingPackageName =
841                 existingPackageState != null ? existingPackageState.getPackageName() : null;
842         mParsedPackage = packageToScan;
843         mArchivedPackage = archivedPackage;
844         mClearCodeCache = clearCodeCache;
845         mSystem = system;
846         mOriginalPs = originalPs;
847         mDisabledPs = disabledPs;
848     }
849 
setScanResult(@onNull ScanResult scanResult)850     public void setScanResult(@NonNull ScanResult scanResult) {
851         mScanResult = scanResult;
852     }
853 
setScannedPackageSettingAppId(int appId)854     public void setScannedPackageSettingAppId(int appId) {
855         assertScanResultExists();
856         mScanResult.mPkgSetting.setAppId(appId);
857     }
858 
setScannedPackageSettingFirstInstallTimeFromReplaced( @ullable PackageStateInternal replacedPkgSetting, int[] userId)859     public void setScannedPackageSettingFirstInstallTimeFromReplaced(
860             @Nullable PackageStateInternal replacedPkgSetting, int[] userId) {
861         assertScanResultExists();
862         mScanResult.mPkgSetting.setFirstInstallTimeFromReplaced(replacedPkgSetting, userId);
863     }
864 
setScannedPackageSettingLastUpdateTime(long lastUpdateTim)865     public void setScannedPackageSettingLastUpdateTime(long lastUpdateTim) {
866         assertScanResultExists();
867         mScanResult.mPkgSetting.setLastUpdateTime(lastUpdateTim);
868     }
869 
setRemovedAppId(int appId)870     public void setRemovedAppId(int appId) {
871         if (mRemovedInfo != null) {
872             mRemovedInfo.mUid = appId;
873             mRemovedInfo.mIsAppIdRemoved = true;
874         }
875     }
876 
877     /**
878      *  Determine the set of users who are adding this package for the first time (aka "new" users)
879      *  vs. those who are seeing an update (aka "update" users). The lists can be calculated as soon
880      *  as the "new" users are set.
881      */
populateBroadcastUsers()882     private void populateBroadcastUsers() {
883         assertScanResultExists();
884         mFirstTimeBroadcastUserIds = EMPTY_INT_ARRAY;
885         mFirstTimeBroadcastInstantUserIds = EMPTY_INT_ARRAY;
886         mUpdateBroadcastUserIds = EMPTY_INT_ARRAY;
887         mUpdateBroadcastInstantUserIds = EMPTY_INT_ARRAY;
888 
889         final boolean allNewUsers = isAllNewUsers();
890         if (allNewUsers) {
891             // App was not currently installed on any user
892             for (int newUser : mNewUsers) {
893                 final boolean isInstantApp =
894                         mScanResult.mPkgSetting.getUserStateOrDefault(newUser).isInstantApp();
895                 if (isInstantApp) {
896                     mFirstTimeBroadcastInstantUserIds =
897                             ArrayUtils.appendInt(mFirstTimeBroadcastInstantUserIds, newUser);
898                 } else {
899                     mFirstTimeBroadcastUserIds =
900                             ArrayUtils.appendInt(mFirstTimeBroadcastUserIds, newUser);
901                 }
902             }
903             return;
904         }
905         // App was already installed on some users, but is new to some other users
906         for (int newUser : mNewUsers) {
907             boolean isFirstTimeUser = !ArrayUtils.contains(mOrigUsers, newUser);
908             final boolean isInstantApp =
909                     mScanResult.mPkgSetting.getUserStateOrDefault(newUser).isInstantApp();
910             if (isFirstTimeUser) {
911                 if (isInstantApp) {
912                     mFirstTimeBroadcastInstantUserIds =
913                             ArrayUtils.appendInt(mFirstTimeBroadcastInstantUserIds, newUser);
914                 } else {
915                     mFirstTimeBroadcastUserIds =
916                             ArrayUtils.appendInt(mFirstTimeBroadcastUserIds, newUser);
917                 }
918             } else {
919                 if (isInstantApp) {
920                     mUpdateBroadcastInstantUserIds =
921                             ArrayUtils.appendInt(mUpdateBroadcastInstantUserIds, newUser);
922                 } else {
923                     mUpdateBroadcastUserIds =
924                             ArrayUtils.appendInt(mUpdateBroadcastUserIds, newUser);
925                 }
926             }
927         }
928     }
929 
930     @Nullable
getPreVerifiedDomains()931     public DomainSet getPreVerifiedDomains() {
932         return mPreVerifiedDomains;
933     }
934 
addWarning(@onNull String warning)935     public void addWarning(@NonNull String warning) {
936         mWarnings.add(warning);
937     }
938 
onPrepareStarted()939     public void onPrepareStarted() {
940         if (mPackageMetrics != null) {
941             mPackageMetrics.onStepStarted(PackageMetrics.STEP_PREPARE);
942         }
943     }
944 
onPrepareFinished()945     public void onPrepareFinished() {
946         if (mPackageMetrics != null) {
947             mPackageMetrics.onStepFinished(PackageMetrics.STEP_PREPARE);
948         }
949     }
950 
onScanStarted()951     public void onScanStarted() {
952         if (mPackageMetrics != null) {
953             mPackageMetrics.onStepStarted(PackageMetrics.STEP_SCAN);
954         }
955     }
956 
onScanFinished()957     public void onScanFinished() {
958         if (mPackageMetrics != null) {
959             mPackageMetrics.onStepFinished(PackageMetrics.STEP_SCAN);
960         }
961     }
962 
onReconcileStarted()963     public void onReconcileStarted() {
964         if (mPackageMetrics != null) {
965             mPackageMetrics.onStepStarted(PackageMetrics.STEP_RECONCILE);
966         }
967     }
968 
onReconcileFinished()969     public void onReconcileFinished() {
970         if (mPackageMetrics != null) {
971             mPackageMetrics.onStepFinished(PackageMetrics.STEP_RECONCILE);
972         }
973     }
974 
onCommitStarted()975     public void onCommitStarted() {
976         if (mPackageMetrics != null) {
977             mPackageMetrics.onStepStarted(PackageMetrics.STEP_COMMIT);
978         }
979     }
980 
onCommitFinished()981     public void onCommitFinished() {
982         if (mPackageMetrics != null) {
983             mPackageMetrics.onStepFinished(PackageMetrics.STEP_COMMIT);
984         }
985     }
986 
onDexoptFinished(DexoptResult dexoptResult)987     public void onDexoptFinished(DexoptResult dexoptResult) {
988         // Only report external profile warnings when installing from adb. The goal is to warn app
989         // developers if they have provided bad external profiles, so it's not beneficial to report
990         // those warnings in the normal app install workflow.
991         if (isInstallFromAdb()) {
992             var externalProfileErrors = new LinkedHashSet<String>();
993             for (PackageDexoptResult packageResult : dexoptResult.getPackageDexoptResults()) {
994                 for (DexContainerFileDexoptResult fileResult :
995                         packageResult.getDexContainerFileDexoptResults()) {
996                     externalProfileErrors.addAll(fileResult.getExternalProfileErrors());
997                 }
998             }
999             if (!externalProfileErrors.isEmpty()) {
1000                 addWarning("Error occurred during dexopt when processing external profiles:\n  "
1001                         + String.join("\n  ", externalProfileErrors));
1002             }
1003         }
1004 
1005         // Report dexopt metrics.
1006         if (mPackageMetrics != null) {
1007             mDexoptStatus = dexoptResult.getFinalStatus();
1008             if (mDexoptStatus == DexoptResult.DEXOPT_PERFORMED) {
1009                 long durationMillis = 0;
1010                 for (PackageDexoptResult packageResult : dexoptResult.getPackageDexoptResults()) {
1011                     for (DexContainerFileDexoptResult fileResult :
1012                             packageResult.getDexContainerFileDexoptResults()) {
1013                         durationMillis += fileResult.getDex2oatWallTimeMillis();
1014                     }
1015                 }
1016                 mPackageMetrics.onStepFinished(PackageMetrics.STEP_DEXOPT, durationMillis);
1017             }
1018         }
1019     }
1020 
onInstallCompleted()1021     public void onInstallCompleted() {
1022         if (getReturnCode() == INSTALL_SUCCEEDED) {
1023             if (mPackageMetrics != null) {
1024                 mPackageMetrics.onInstallSucceed();
1025             }
1026         }
1027     }
1028 
onFreezeStarted()1029     public void onFreezeStarted() {
1030         if (mPackageMetrics != null && improveInstallFreeze()) {
1031             mPackageMetrics.onStepStarted(PackageMetrics.STEP_FREEZE_INSTALL);
1032         }
1033     }
1034 
onFreezeCompleted()1035     public void onFreezeCompleted() {
1036         if (mPackageMetrics != null && improveInstallFreeze()) {
1037             mPackageMetrics.onStepFinished(PackageMetrics.STEP_FREEZE_INSTALL);
1038         }
1039     }
1040 }
1041