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

Commit 9c1500f4 authored by Po-Chien Hsueh's avatar Po-Chien Hsueh
Browse files

Use GSID progress API during startInstallation()

Use getInstallationProgress() while startInstallation() is running.

startIntallation() takes up to 90 seconds. So, move it to another
thread and allow user to cancel it.

Bug: 124614460
Test: adb shell am, then cancel it from notification
Change-Id: I685f7fff9992a99b8c033760a406922b41b058f6
parent 5af401bd
Loading
Loading
Loading
Loading
+7 −4
Original line number Diff line number Diff line
@@ -114,6 +114,7 @@ public class DynamicAndroidInstallationService extends Service
    private NotificationManager mNM;

    private long mSystemSize;
    private long mUserdataSize;
    private long mInstalledSize;
    private boolean mJustCancelledByUser;

@@ -220,9 +221,11 @@ public class DynamicAndroidInstallationService extends Service

        String url = intent.getStringExtra(DynamicAndroidClient.KEY_SYSTEM_URL);
        mSystemSize = intent.getLongExtra(DynamicAndroidClient.KEY_SYSTEM_SIZE, 0);
        long userdata = intent.getLongExtra(DynamicAndroidClient.KEY_USERDATA_SIZE, 0);
        mUserdataSize = intent.getLongExtra(DynamicAndroidClient.KEY_USERDATA_SIZE, 0);

        mInstallTask = new InstallationAsyncTask(
                url, mSystemSize, mUserdataSize, mDynAndroid, this);

        mInstallTask = new InstallationAsyncTask(url, mSystemSize, userdata, mDynAndroid, this);
        mInstallTask.execute();

        // start fore ground
@@ -332,8 +335,8 @@ public class DynamicAndroidInstallationService extends Service
            case STATUS_IN_PROGRESS:
                builder.setContentText(getString(R.string.notification_install_inprogress));

                int max = (int) Math.max(mSystemSize >> 20, 1);
                int progress = (int) mInstalledSize >> 20;
                int max = (int) Math.max((mSystemSize + mUserdataSize) >> 20, 1);
                int progress = (int) (mInstalledSize >> 20);

                builder.setProgress(max, progress, false);

+40 −10
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.dynandroid;

import android.gsi.GsiProgress;
import android.os.AsyncTask;
import android.os.DynamicAndroidManager;
import android.util.Log;
@@ -63,8 +64,6 @@ class InstallationAsyncTask extends AsyncTask<String, Long, Integer> {
    private final InstallStatusListener mListener;
    private DynamicAndroidManager.Session mInstallationSession;

    private long mInstalledSize;
    private long mReportedInstalledSize;
    private int mResult = NO_RESULT;

    private InputStream mStream;
@@ -89,8 +88,40 @@ class InstallationAsyncTask extends AsyncTask<String, Long, Integer> {
        Log.d(TAG, "Start doInBackground(), URL: " + mUrl);

        try {
            // call start in background
            mInstallationSession = mDynamicAndroid.startInstallation(mSystemSize, mUserdataSize);
            long installedSize = 0;
            long reportedInstalledSize = 0;

            long minStepToReport = (mSystemSize + mUserdataSize) / 100;

            // init input stream before calling startInstallation(), which takes 90 seconds.
            initInputStream();

            Thread thread = new Thread(() -> {
                mInstallationSession =
                        mDynamicAndroid.startInstallation(mSystemSize, mUserdataSize);
            });


            thread.start();

            while (thread.isAlive()) {
                if (isCancelled()) {
                    boolean aborted = mDynamicAndroid.abort();
                    Log.d(TAG, "Called DynamicAndroidManager.abort(), result = " + aborted);
                    return RESULT_OK;
                }

                GsiProgress progress = mDynamicAndroid.getInstallationProgress();
                installedSize = progress.bytes_processed;

                if (installedSize > reportedInstalledSize + minStepToReport) {
                    publishProgress(installedSize);
                    reportedInstalledSize = installedSize;
                }

                Thread.sleep(10);
            }


            if (mInstallationSession == null) {
                Log.e(TAG, "Failed to start installation with requested size: "
@@ -99,12 +130,11 @@ class InstallationAsyncTask extends AsyncTask<String, Long, Integer> {
                return RESULT_ERROR_IO;
            }

            initInputStream();
            installedSize = mUserdataSize;

            byte[] bytes = new byte[READ_BUFFER_SIZE];

            int numBytesRead;
            long minStepToReport = mSystemSize / 100;

            Log.d(TAG, "Start installation loop");
            while ((numBytesRead = mStream.read(bytes, 0, READ_BUFFER_SIZE)) != -1) {
@@ -119,11 +149,11 @@ class InstallationAsyncTask extends AsyncTask<String, Long, Integer> {
                    throw new IOException("Failed write() to DynamicAndroid");
                }

                mInstalledSize += numBytesRead;
                installedSize += numBytesRead;

                if (mInstalledSize > mReportedInstalledSize + minStepToReport) {
                    publishProgress(mInstalledSize);
                    mReportedInstalledSize = mInstalledSize;
                if (installedSize > reportedInstalledSize + minStepToReport) {
                    publishProgress(installedSize);
                    reportedInstalledSize = installedSize;
                }
            }