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

Commit a7322ac1 authored by Tony Huang's avatar Tony Huang
Browse files

Fix progress bar when optimized copy or optimized move

When optimized copy or optimized move, if progress tracker is
ByteCountProgressTracker, it will not record any byte copied.
Add a function to record it.

Fix: 139777488
Test: manual
Test: atest DocumentsUIGoogleTests
Change-Id: I8ebc7eb2822f0b7743dc6b979359d43b0d1de07d
parent 3880fcc8
Loading
Loading
Loading
Loading
+26 −7
Original line number Diff line number Diff line
@@ -59,9 +59,13 @@ import android.system.ErrnoException;
import android.system.Int64Ref;
import android.system.Os;
import android.system.OsConstants;
import android.util.ArrayMap;
import android.util.Log;
import android.webkit.MimeTypeMap;

import androidx.annotation.StringRes;
import androidx.annotation.VisibleForTesting;

import com.android.documentsui.DocumentsApplication;
import com.android.documentsui.MetricConsts;
import com.android.documentsui.Metrics;
@@ -82,13 +86,11 @@ import java.io.InputStream;
import java.io.SyncFailedException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;
import java.util.function.LongSupplier;

import androidx.annotation.StringRes;
import androidx.annotation.VisibleForTesting;

class CopyJob extends ResolvedResourcesJob {

    private static final String TAG = "CopyJob";
@@ -100,6 +102,7 @@ class CopyJob extends ResolvedResourcesJob {

    private final Handler mHandler = new Handler(Looper.getMainLooper());
    private final Messenger mMessenger;
    private final Map<String, Long> mDirSizeMap = new ArrayMap<>();

    private CopyJobProgressTracker mProgressTracker;

@@ -306,6 +309,22 @@ class CopyJob extends ResolvedResourcesJob {
        mProgressTracker.onBytesCopied(bytesCopied);
    }

    /**
     * Logs progress when optimized copy.
     *
     * @param doc the doc current copy.
     */
    protected void makeOptimizedCopyProgress(DocumentInfo doc) {
        long bytes;
        if (doc.isDirectory()) {
            Long byteObject = mDirSizeMap.get(doc.documentId);
            bytes = byteObject == null ? 0 : byteObject.longValue();
        } else {
            bytes = doc.size;
        }
        makeCopyProgress(bytes);
    }

    /**
     * Copies a the given document to the given location.
     *
@@ -318,8 +337,6 @@ class CopyJob extends ResolvedResourcesJob {
     */
    void processDocument(DocumentInfo src, DocumentInfo srcParent,
            DocumentInfo dstDirInfo) throws ResourceException {

        // TODO: When optimized copy kicks in, we'll not making any progress updates.
        // For now. Local storage isn't using optimized copy.

        // When copying within the same provider, try to use optimized copying.
@@ -330,6 +347,7 @@ class CopyJob extends ResolvedResourcesJob {
                    if (DocumentsContract.copyDocument(wrap(getClient(src)), src.derivedUri,
                            dstDirInfo.derivedUri) != null) {
                        Metrics.logFileOperated(operationType, MetricConsts.OPMODE_PROVIDER);
                        makeOptimizedCopyProgress(src);
                        return;
                    }
                } catch (FileNotFoundException | RemoteException | RuntimeException e) {
@@ -657,8 +675,9 @@ class CopyJob extends ResolvedResourcesJob {
                if (src.isDirectory()) {
                    // Directories need to be recursed into.
                    try {
                        bytesRequired +=
                                calculateFileSizesRecursively(getClient(src), src.derivedUri);
                        long size = calculateFileSizesRecursively(getClient(src), src.derivedUri);
                        bytesRequired += size;
                        mDirSizeMap.put(src.documentId, size);
                    } catch (RemoteException e) {
                        Log.w(TAG, "Failed to obtain the client for " + src.derivedUri, e);
                        return new IndeterminateProgressTracker(bytesRequired);
+1 −3
Original line number Diff line number Diff line
@@ -141,9 +141,6 @@ final class MoveJob extends CopyJob {

    void processDocument(DocumentInfo src, DocumentInfo srcParent, DocumentInfo dest)
            throws ResourceException {

        // TODO: When optimized move kicks in, we're not making any progress updates. FIX IT!

        // When moving within the same provider, try to use optimized moving.
        // If not supported, then fallback to byte-by-byte copy/move.
        if (src.authority.equals(dest.authority) && (srcParent != null || mSrcParent != null)) {
@@ -153,6 +150,7 @@ final class MoveJob extends CopyJob {
                            srcParent != null ? srcParent.derivedUri : mSrcParent.derivedUri,
                            dest.derivedUri) != null) {
                        Metrics.logFileOperated(operationType, MetricConsts.OPMODE_PROVIDER);
                        makeOptimizedCopyProgress(src);
                        return;
                    }
                } catch (FileNotFoundException | RemoteException | RuntimeException e) {