From 1b3f0912776a08103ddfec71c01c56fec330ca61 Mon Sep 17 00:00:00 2001 From: JW Wang Date: Thu, 3 Mar 2022 11:39:55 +0800 Subject: [PATCH] Pass 'extras' to dispatchSessionFinished() 'extras' is used to tell if this is a new install. We send broadcast to default launcher only if it's a new install. Bug: 219154632 Test: manual test Change-Id: I33ba87c58f305bf842c977f7b61ed36c462b272c --- .../server/pm/PackageInstallerSession.java | 46 ++++++++++++------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/services/core/java/com/android/server/pm/PackageInstallerSession.java b/services/core/java/com/android/server/pm/PackageInstallerSession.java index ced1c7d136aa6..55fc785996618 100644 --- a/services/core/java/com/android/server/pm/PackageInstallerSession.java +++ b/services/core/java/com/android/server/pm/PackageInstallerSession.java @@ -2301,6 +2301,15 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub { }); } + private static class InstallResult { + public final PackageInstallerSession session; + public final Bundle extras; + InstallResult(PackageInstallerSession session, Bundle extras) { + this.session = session; + this.extras = extras; + } + } + /** * Stages installs and do cleanup accordingly depending on whether the installation is * successful or not. @@ -2308,11 +2317,16 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub { * @return a future that will be completed when the whole process is completed. */ private CompletableFuture install() { - return installNonStaged().whenComplete((r, t) -> { + List> futures = installNonStaged(); + CompletableFuture[] arr = new CompletableFuture[futures.size()]; + return CompletableFuture.allOf(futures.toArray(arr)).whenComplete((r, t) -> { if (t == null) { setSessionApplied(); - dispatchSessionFinished(INSTALL_SUCCEEDED, "Session installed", null); - maybeFinishChildSessions(INSTALL_SUCCEEDED, "Session installed"); + for (CompletableFuture f : futures) { + InstallResult result = f.join(); + result.session.dispatchSessionFinished( + INSTALL_SUCCEEDED, "Session installed", result.extras); + } } else { PackageManagerException e = (PackageManagerException) t.getCause(); setSessionFailed(SessionInfo.SESSION_ACTIVATION_FAILED, @@ -2326,13 +2340,12 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub { /** * Stages sessions (including child sessions if any) for install. * - * @return a future that will be completed when the whole session is completed (could be - * success or failure). + * @return a list of futures to indicate the install results of each session. */ - private CompletableFuture installNonStaged() { + private List> installNonStaged() { try { - List> futures = new ArrayList<>(); - CompletableFuture future = new CompletableFuture<>(); + List> futures = new ArrayList<>(); + CompletableFuture future = new CompletableFuture<>(); futures.add(future); final InstallParams installingSession = makeInstallParams(future); if (isMultiPackage()) { @@ -2354,12 +2367,11 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub { installingSession.installStage(); } - CompletableFuture[] arr = new CompletableFuture[futures.size()]; - return CompletableFuture.allOf(futures.toArray(arr)); + return futures; } catch (PackageManagerException e) { - CompletableFuture future = new CompletableFuture<>(); - future.completeExceptionally(e); - return future; + List> futures = new ArrayList<>(); + futures.add(CompletableFuture.failedFuture(e)); + return futures; } } @@ -2395,7 +2407,7 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub { * @param future a future that will be completed when this session is completed. */ @Nullable - private InstallParams makeInstallParams(CompletableFuture future) + private InstallParams makeInstallParams(CompletableFuture future) throws PackageManagerException { synchronized (mLock) { if (!mSealed) { @@ -2407,10 +2419,10 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub { if (isMultiPackage()) { // Always treat parent session as success for it has nothing to install - future.complete(null); + future.complete(new InstallResult(this, null)); } else if (isApexSession() && params.isStaged) { // Staged apex sessions have been handled by apexd - future.complete(null); + future.complete(new InstallResult(this, null)); return null; } @@ -2424,7 +2436,7 @@ public class PackageInstallerSession extends IPackageInstallerSession.Stub { public void onPackageInstalled(String basePackageName, int returnCode, String msg, Bundle extras) { if (returnCode == INSTALL_SUCCEEDED) { - future.complete(null); + future.complete(new InstallResult(PackageInstallerSession.this, extras)); } else { future.completeExceptionally(new PackageManagerException(returnCode, msg)); }