Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bd02f42a authored by Mohammad Samiul Islam's avatar Mohammad Samiul Islam
Browse files

Add flag for staged session to get ready during installation

Currently, the PackageManagerShellCommand returns success when a
staged session is successfully commited, regardless of the result
from pre-reboot verification. The flag will force the adb shell to
wait for the verification to complete and output result accordingly.
This will ensure a better experience for the testers.

Bug: 130039562
Test: manual testing by installing apex and observing new detailed
      success message
      $ adb install --apex --wait-for-staged-session-ready
      --wait 2000 com.android.tzdata.apex
Change-Id: Ib7aa9b5dfb683956e48e55461b29278069573aec
parent 36259ba0
Loading
Loading
Loading
Loading
+55 −1
Original line number Diff line number Diff line
@@ -129,6 +129,7 @@ class PackageManagerShellCommand extends ShellCommand {
    private static final String STDIN_PATH = "-";
    /** Path where ART profiles snapshots are dumped for the shell user */
    private final static String ART_PROFILE_SNAPSHOT_DEBUG_LOCATION = "/data/misc/profman/";
    private static final int DEFAULT_WAIT_MS = 60 * 1000;

    final IPackageManager mInterface;
    final private WeakHashMap<String, Resources> mResourceCache =
@@ -1078,6 +1079,45 @@ class PackageManagerShellCommand extends ShellCommand {
                return 1;
            }
            abandonSession = false;

            if (!params.sessionParams.isStaged || !params.waitForStagedSessionReady) {
                pw.println("Success");
                return 0;
            }

            long timeoutMs = params.timeoutMs <= 0
                    ? DEFAULT_WAIT_MS
                    : params.timeoutMs;
            PackageInstaller.SessionInfo si = mInterface.getPackageInstaller()
                    .getSessionInfo(sessionId);
            long currentTime = System.currentTimeMillis();
            long endTime = currentTime + timeoutMs;
            // Using a loop instead of BroadcastReceiver since we can receive session update
            // broadcast only if packageInstallerName is "android". We can't always force
            // "android" as packageIntallerName, e.g, rollback auto implies
            // "-i com.android.shell".
            while (currentTime < endTime) {
                if (si != null
                        && (si.isStagedSessionReady() || si.isStagedSessionFailed())) {
                    break;
                }
                SystemClock.sleep(Math.min(endTime - currentTime, 100));
                currentTime = System.currentTimeMillis();
                si = mInterface.getPackageInstaller().getSessionInfo(sessionId);
            }
            if (si == null) {
                pw.println("Failure [failed to retrieve SessionInfo]");
                return 1;
            }
            if (!si.isStagedSessionReady() && !si.isStagedSessionFailed()) {
                pw.println("Failure [timed out after " + timeoutMs + " ms]");
                return 1;
            }
            if (!si.isStagedSessionReady()) {
                pw.println("Error [" + si.getStagedSessionErrorCode() + "] ["
                        + si.getStagedSessionErrorMessage() + "]");
                return 1;
            }
            pw.println("Success");
            return 0;
        } finally {
@@ -2368,6 +2408,8 @@ class PackageManagerShellCommand extends ShellCommand {
        SessionParams sessionParams;
        String installerPackageName;
        int userId = UserHandle.USER_ALL;
        boolean waitForStagedSessionReady = false;
        long timeoutMs = DEFAULT_WAIT_MS;
    }

    private InstallParams makeInstallParams() {
@@ -2493,6 +2535,14 @@ class PackageManagerShellCommand extends ShellCommand {
                    }
                    sessionParams.installFlags |= PackageManager.INSTALL_ENABLE_ROLLBACK;
                    break;
                case "--wait":
                    params.waitForStagedSessionReady = true;
                    try {
                        params.timeoutMs = Long.parseLong(peekNextArg());
                        getNextArg();
                    } catch (NumberFormatException ignore) {
                    }
                    break;
                default:
                    throw new IllegalArgumentException("Unknown option " + opt);
            }
@@ -3048,7 +3098,8 @@ class PackageManagerShellCommand extends ShellCommand {
        pw.println("       [--referrer URI] [--abi ABI_NAME] [--force-sdk]");
        pw.println("       [--preload] [--instantapp] [--full] [--dont-kill]");
        pw.println("       [--enable-rollback]");
        pw.println("       [--force-uuid internal|UUID] [--pkg PACKAGE] [-S BYTES] [--apex]");
        pw.println("       [--force-uuid internal|UUID] [--pkg PACKAGE] [-S BYTES]");
        pw.println("       [--apex] [--wait TIMEOUT]");
        pw.println("       [PATH|-]");
        pw.println("    Install an application.  Must provide the apk data to install, either as a");
        pw.println("    file path or '-' to read from stdin.  Options are:");
@@ -3078,6 +3129,9 @@ class PackageManagerShellCommand extends ShellCommand {
        pw.println("          3=device setup, 4=user request");
        pw.println("      --force-uuid: force install on to disk volume with given UUID");
        pw.println("      --apex: install an .apex file, not an .apk");
        pw.println("      --wait: when performing staged install, wait TIMEOUT milliseconds");
        pw.println("          for pre-reboot verification to complete. If TIMEOUT is not");
        pw.println("          specified it will wait for " + DEFAULT_WAIT_MS + " milliseconds.");
        pw.println("");
        pw.println("  install-create [-lrtsfdg] [-i PACKAGE] [--user USER_ID|all|current]");
        pw.println("       [-p INHERIT_PACKAGE] [--install-location 0/1/2]");