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

Commit 77f3e752 authored by Austin Tankiang's avatar Austin Tankiang
Browse files

Add functions to Jobs to retrieve current progress

There is no change in behaviour as this patch just adds functions.

Due to limitations in the current job implementations, there are a few
shortcominings:

* Failure reasons aren't exposed.
* Transfers of zero bytes (e.g. folder only or empty file) show as
  indeterminate progress.
* In cases where file size can't be determined, the job keeps track via
  files processed, but these will show as indeterminate progress.

Bug: 385841721
Test: atest -c 'DocumentsUIGoogleTests:com.android.documentsui.services'
Flag: com.android.documentsui.flags.visual_signals

Change-Id: I7b0d863bf7b974090c0e1a17f06c0f7160c59699
parent e5ed5939
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -409,6 +409,23 @@
         during a copy. [CHAR LIMIT=48] -->
    <string name="notification_copy_files_converted_title">Some files were converted</string>

    <string name="copy_in_progress" translatable="false">{count, plural,
        =1 {Copying <xliff:g id="filename" example="foobar.txt">{filename}</xliff:g> to <xliff:g id="directory" example="example folder">{directory}</xliff:g>}
        other {Copying # files to <xliff:g id="directory" example="example folder">{directory}</xliff:g>}
    }</string>
    <string name="move_in_progress" translatable="false">{count, plural,
        =1 {Moving <xliff:g id="filename" example="foobar.txt">{filename}</xliff:g> to <xliff:g id="directory" example="example folder">{directory}</xliff:g>}
        other {Moving # files to <xliff:g id="directory" example="example folder">{directory}</xliff:g>}
    }</string>
    <string name="delete_in_progress" translatable="false">{count, plural,
        =1 {Deleting <xliff:g id="filename" example="foobar.txt">{filename}</xliff:g>}
        other {Deleting # files}
    }</string>
    <string name="compress_in_progress" translatable="false">{count, plural,
        =1 {Zipping <xliff:g id="filename" example="foobar.txt">{filename}</xliff:g>}
        other {Zipping # files}
    }</string>

    <!-- Text in an alert dialog asking user to grant app access to a given directory in an external storage volume -->
    <string name="open_external_dialog_request">Grant <xliff:g id="appName" example="System Settings"><b>^1</b></xliff:g>
        access to <xliff:g id="directory" example="Pictures"><i>^2</i></xliff:g> directory on
+25 −0
Original line number Diff line number Diff line
@@ -24,11 +24,13 @@ import android.app.Notification;
import android.app.Notification.Builder;
import android.content.ContentResolver;
import android.content.Context;
import android.icu.text.MessageFormat;
import android.net.Uri;
import android.os.Messenger;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.provider.DocumentsContract;
import android.text.BidiFormatter;
import android.util.Log;

import com.android.documentsui.R;
@@ -40,6 +42,9 @@ import com.android.documentsui.base.UserId;
import com.android.documentsui.clipping.UrisSupplier;

import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

// TODO: Stop extending CopyJob.
final class CompressJob extends CopyJob {
@@ -86,6 +91,26 @@ final class CompressJob extends CopyJob {
                R.plurals.compress_error_notification_title, R.drawable.ic_menu_compress);
    }

    @Override
    protected String getProgressMessage() {
        switch (getState()) {
            case Job.STATE_SET_UP:
            case Job.STATE_COMPLETED:
            case Job.STATE_CANCELED:
                Map<String, Object> formatArgs = new HashMap<>();
                formatArgs.put("count", mResolvedDocs.size());
                if (mResolvedDocs.size() == 1) {
                    formatArgs.put("filename", BidiFormatter.getInstance().unicodeWrap(
                            mResolvedDocs.get(0).displayName));
                }
                return (new MessageFormat(
                        service.getString(R.string.compress_in_progress), Locale.getDefault()))
                        .format(formatArgs);
            default:
                return "";
        }
    }

    @Override
    public boolean setUp() {
        if (!super.setUp()) {
+61 −0
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.database.ContentObserver;
import android.database.Cursor;
import android.icu.text.MessageFormat;
import android.net.Uri;
import android.os.DeadObjectException;
import android.os.FileUtils;
@@ -66,6 +67,7 @@ import android.system.Int64Ref;
import android.system.Os;
import android.system.OsConstants;
import android.system.StructStat;
import android.text.BidiFormatter;
import android.util.ArrayMap;
import android.util.Log;
import android.webkit.MimeTypeMap;
@@ -93,6 +95,8 @@ import java.io.InputStream;
import java.io.SyncFailedException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;
@@ -195,6 +199,49 @@ class CopyJob extends ResolvedResourcesJob {
        return warningBuilder.build();
    }

    protected String getProgressMessage() {
        switch (getState()) {
            case Job.STATE_SET_UP:
            case Job.STATE_COMPLETED:
            case Job.STATE_CANCELED:
                Map<String, Object> formatArgs = new HashMap<>();
                formatArgs.put("count", mResolvedDocs.size());
                formatArgs.put("directory",
                        BidiFormatter.getInstance().unicodeWrap(mDstInfo.displayName));
                if (mResolvedDocs.size() == 1) {
                    formatArgs.put("filename",
                            BidiFormatter.getInstance().unicodeWrap(
                                    mResolvedDocs.get(0).displayName));
                }
                return (new MessageFormat(
                        service.getString(R.string.copy_in_progress), Locale.getDefault()))
                        .format(formatArgs);

            default:
                return "";
        }
    }

    @Override
    JobProgress getJobProgress() {
        if (mProgressTracker == null) {
            return new JobProgress(
                    id,
                    getState(),
                    getProgressMessage(),
                    hasFailures());
        }
        mProgressTracker.updateEstimateRemainingTime();
        return new JobProgress(
                id,
                getState(),
                getProgressMessage(),
                hasFailures(),
                mProgressTracker.getCurrentBytes(),
                mProgressTracker.getRequiredBytes(),
                mProgressTracker.getRemainingTimeEstimate());
    }

    @Override
    boolean setUp() {
        if (!super.setUp()) {
@@ -986,6 +1033,10 @@ class CopyJob extends ResolvedResourcesJob {
            return -1;
        }

        protected long getCurrentBytes() {
            return -1;
        }

        protected void start() {
            mStartTime = mElapsedRealTimeSupplier.getAsLong();
        }
@@ -1057,6 +1108,16 @@ class CopyJob extends ResolvedResourcesJob {
            return mBytesRequired > 0;
        }

        @Override
        protected long getRequiredBytes() {
            return mBytesRequired;
        }

        @Override
        protected long getCurrentBytes() {
            return mBytesCopied.get();
        }

        @Override
        public void onBytesCopied(long numBytes) {
            mBytesCopied.getAndAdd(numBytes);
+33 −0
Original line number Diff line number Diff line
@@ -23,7 +23,9 @@ import android.app.Notification;
import android.app.Notification.Builder;
import android.content.ContentResolver;
import android.content.Context;
import android.icu.text.MessageFormat;
import android.net.Uri;
import android.text.BidiFormatter;
import android.util.Log;

import com.android.documentsui.MetricConsts;
@@ -36,6 +38,9 @@ import com.android.documentsui.base.UserId;
import com.android.documentsui.clipping.UrisSupplier;

import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.annotation.Nullable;

@@ -97,6 +102,34 @@ final class DeleteJob extends ResolvedResourcesJob {
        throw new UnsupportedOperationException();
    }

    private String getProgressMessage() {
        switch (getState()) {
            case Job.STATE_SET_UP:
            case Job.STATE_COMPLETED:
            case Job.STATE_CANCELED:
                Map<String, Object> formatArgs = new HashMap<>();
                formatArgs.put("count", mResolvedDocs.size());
                if (mResolvedDocs.size() == 1) {
                    formatArgs.put("filename", BidiFormatter.getInstance().unicodeWrap(
                            mResolvedDocs.get(0).displayName));
                }
                return (new MessageFormat(
                        service.getString(R.string.delete_in_progress), Locale.getDefault()))
                        .format(formatArgs);
            default:
                return "";
        }
    }

    @Override
    JobProgress getJobProgress() {
        return new JobProgress(
                id,
                getState(),
                getProgressMessage(),
                hasFailures());
    }

    @Override
    void start() {
        ContentResolver resolver = appContext.getContentResolver();
+2 −0
Original line number Diff line number Diff line
@@ -190,6 +190,8 @@ abstract public class Job implements Runnable {

    abstract Notification getWarningNotification();

    abstract JobProgress getJobProgress();

    Uri getDataUriForIntent(String tag) {
        return Uri.parse(String.format("data,%s-%s", tag, id));
    }
Loading