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

Commit a8edfaac authored by Ben Lin's avatar Ben Lin Committed by android-build-merger
Browse files

On-demand fetch for DocumentInfo when right-click on a root.

am: 8e912584

Change-Id: Iaa0f449b4a81bb861755c6739682afcba14fdd79
parents 1b82a7bd 8e912584
Loading
Loading
Loading
Loading
+12 −4
Original line number Diff line number Diff line
@@ -33,13 +33,14 @@ import java.util.function.Consumer;
 * {@link DocumentInfo} of its root document and call supplied callback to handle the
 * {@link DocumentInfo}.
 */
public class GetRootDocumentTask extends CheckedTask<Void, DocumentInfo> {
public class GetRootDocumentTask extends TimeoutTask<Void, DocumentInfo> {

    private final static String TAG = "GetRootDocumentTask";

    private final RootInfo mRootInfo;
    private final Context mContext;
    private final Consumer<DocumentInfo> mCallback;
    private boolean mForceCallback;

    public GetRootDocumentTask(
            RootInfo rootInfo, Activity activity, Consumer<DocumentInfo> callback) {
@@ -59,6 +60,10 @@ public class GetRootDocumentTask extends CheckedTask<Void, DocumentInfo> {
        mCallback = callback;
    }

    public void setForceCallback(boolean forceCallback) {
        mForceCallback = forceCallback;
    }

    @Override
    public @Nullable DocumentInfo run(Void... rootInfo) {
        return mRootInfo.getRootDocumentBlocking(mContext);
@@ -66,10 +71,13 @@ public class GetRootDocumentTask extends CheckedTask<Void, DocumentInfo> {

    @Override
    public void finish(@Nullable DocumentInfo documentInfo) {
        if (documentInfo != null) {
        if (documentInfo == null) {
            Log.e(TAG,
                    "Cannot find document info for root: " + mRootInfo + " in the given timeout");
        }

        if (documentInfo != null || mForceCallback) {
            mCallback.accept(documentInfo);
        } else {
            Log.e(TAG, "Cannot find document info for root: " + mRootInfo);
        }
    }
}
+4 −3
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import com.android.documentsui.base.DocumentInfo;
import com.android.documentsui.base.Menus;
import com.android.documentsui.base.RootInfo;
import com.android.documentsui.base.Shared;
@@ -181,14 +182,14 @@ public abstract class MenuManager {
    /**
     * @see RootsFragment#onCreateContextMenu
     */
    public void updateRootContextMenu(Menu menu, RootInfo root) {
    public void updateRootContextMenu(Menu menu, RootInfo root, DocumentInfo docInfo) {
        MenuItem eject = menu.findItem(R.id.menu_eject_root);
        MenuItem pasteInto = menu.findItem(R.id.menu_paste_into_folder);
        MenuItem openInNewWindow = menu.findItem(R.id.menu_open_in_new_window);
        MenuItem settings = menu.findItem(R.id.menu_settings);

        updateEject(eject, root);
        updatePasteInto(pasteInto, root);
        updatePasteInto(pasteInto, root, docInfo);
        updateOpenInNewWindow(openInNewWindow, root);
        updateSettings(settings, root);
    }
@@ -262,7 +263,7 @@ public abstract class MenuManager {
        pasteInto.setVisible(false);
    }

    protected void updatePasteInto(MenuItem pasteInto, RootInfo root) {
    protected void updatePasteInto(MenuItem pasteInto, RootInfo root, DocumentInfo docInfo) {
        pasteInto.setVisible(false);
    }

+60 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.documentsui;

import android.annotation.CallSuper;
import android.os.AsyncTask;
import android.os.Handler;

import com.android.documentsui.base.CheckedTask;
import com.android.documentsui.base.DocumentInfo;

/**
 * A {@link CheckedTask} that takes  and query SAF to obtain the
 * {@link DocumentInfo} of its root document and call supplied callback to handle the
 * {@link DocumentInfo}.
 */
public abstract class TimeoutTask<Input, Output> extends CheckedTask<Input, Output> {
    private static final int DEFAULT_TIMEOUT = -1;

    private long mTimeout = DEFAULT_TIMEOUT;

    public TimeoutTask(Check check) {
        super(check);
    }

    public void setTimeout(long timeout) {
        mTimeout = timeout;
    }

    @CallSuper
    @Override
    protected void prepare() {
        if (mTimeout < 0) {
            return;
        }

        Handler handler = new Handler();
        handler.postDelayed(() -> {
            if (getStatus() == AsyncTask.Status.RUNNING) {
                cancel(true);
                this.finish(null);
            }
        }, mTimeout);
    }

}
+6 −3
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.view.View;

import com.android.documentsui.R;
import com.android.documentsui.SearchViewManager;
import com.android.documentsui.base.DocumentInfo;
import com.android.documentsui.base.RootInfo;
import com.android.documentsui.base.State;

@@ -145,9 +146,11 @@ public final class MenuManager extends com.android.documentsui.MenuManager {
    }

    @Override
    protected void updatePasteInto(MenuItem pasteInto, RootInfo root) {
        // TODO(b/31658763): Check the root document as well.
        pasteInto.setEnabled(root.supportsCreate() && mDirDetails.hasItemsToPaste());
    protected void updatePasteInto(MenuItem pasteInto, RootInfo root, DocumentInfo docInfo) {
        pasteInto.setEnabled(root.supportsCreate()
                && docInfo != null
                && docInfo.isCreateSupported()
                && mDirDetails.hasItemsToPaste());
    }

    @Override
+4 −1
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.documentsui.sidebar;

import android.annotation.Nullable;
import android.content.ClipData;
import android.content.Context;
import android.provider.DocumentsProvider;
@@ -31,6 +32,7 @@ import android.widget.TextView;
import com.android.documentsui.ActionHandler;
import com.android.documentsui.MenuManager;
import com.android.documentsui.R;
import com.android.documentsui.base.DocumentInfo;
import com.android.documentsui.base.RootInfo;

/**
@@ -40,6 +42,7 @@ class RootItem extends Item {
    private static final String STRING_ID_FORMAT = "RootItem{%s/%s}";

    public final RootInfo root;
    public @Nullable DocumentInfo docInfo;

    private final ActionHandler mActionHandler;

@@ -110,6 +113,6 @@ class RootItem extends Item {
    @Override
    void createContextMenu(Menu menu, MenuInflater inflater, MenuManager menuManager) {
        inflater.inflate(R.menu.root_context_menu, menu);
        menuManager.updateRootContextMenu(menu, root);
        menuManager.updateRootContextMenu(menu, root, docInfo);
    }
}
Loading