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

Commit 954c5e0d authored by Jon Mann's avatar Jon Mann Committed by Android (Google) Code Review
Browse files

Merge "Start to move access to model in to Injector and ActionHandlers,...

Merge "Start to move access to model in to Injector and ActionHandlers, refactor select all and file rename." into arc-apps
parents 24335206 30d8c798
Loading
Loading
Loading
Loading
+62 −8
Original line number Diff line number Diff line
@@ -16,14 +16,17 @@

package com.android.documentsui;

import static com.android.documentsui.base.DocumentInfo.getCursorInt;
import static com.android.documentsui.base.DocumentInfo.getCursorString;

import android.app.Activity;
import android.content.ClipData;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.net.Uri;
import android.os.Parcelable;
import android.provider.DocumentsContract;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.util.Log;

@@ -40,7 +43,9 @@ import com.android.documentsui.base.State;
import com.android.documentsui.dirlist.AnimationView.AnimationType;
import com.android.documentsui.dirlist.AnimationView;
import com.android.documentsui.dirlist.DocumentDetails;
import com.android.documentsui.dirlist.DocumentsAdapter;
import com.android.documentsui.dirlist.FocusHandler;
import com.android.documentsui.dirlist.Model;
import com.android.documentsui.files.LauncherActivity;
import com.android.documentsui.queries.SearchViewManager;
import com.android.documentsui.roots.LoadRootTask;
@@ -49,11 +54,14 @@ import com.android.documentsui.selection.Selection;
import com.android.documentsui.selection.SelectionManager;
import com.android.documentsui.sidebar.EjectRootTask;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.function.Consumer;

import javax.annotation.Nullable;

/**
 * Provides support for specializing the actions (viewDocument etc.) to the host activity.
 */
@@ -71,34 +79,46 @@ public abstract class AbstractActionHandler<T extends Activity & CommonAddons>
    protected final SelectionManager mSelectionMgr;
    protected final SearchViewManager mSearchMgr;
    protected final Lookup<String, Executor> mExecutors;
    protected final Injector mInjector;

    private Runnable mDisplayStateChangedListener;

    @Override
    public void registerDisplayStateChangedListener(Runnable l) {
        mDisplayStateChangedListener = l;
    }
    @Override
    public void unregisterDisplayStateChangedListener(Runnable l) {
        if (mDisplayStateChangedListener == l) {
            mDisplayStateChangedListener = null;
        }
    }

    public AbstractActionHandler(
            T activity,
            State state,
            RootsAccess roots,
            DocumentsAccess docs,
            FocusHandler focusHandler,
            SelectionManager selectionMgr,
            SearchViewManager searchMgr,
            Lookup<String, Executor> executors) {
            Lookup<String, Executor> executors,
            Injector injector) {

        assert(activity != null);
        assert(state != null);
        assert(roots != null);
        assert(focusHandler != null);
        assert(selectionMgr != null);
        assert(searchMgr != null);
        assert(docs != null);
        assert(injector != null);

        mActivity = activity;
        mState = state;
        mRoots = roots;
        mDocs = docs;
        mFocusHandler = focusHandler;
        mSelectionMgr = selectionMgr;
        mFocusHandler = injector.focusManager;
        mSelectionMgr = injector.selectionMgr;
        mSearchMgr = searchMgr;
        mExecutors = executors;
        mInjector = injector;
    }

    @Override
@@ -170,6 +190,40 @@ public abstract class AbstractActionHandler<T extends Activity & CommonAddons>
        throw new UnsupportedOperationException("Can't paste into folder.");
    }

    @Override
    public void selectAllFiles() {
        Metrics.logUserAction(mActivity, Metrics.USER_ACTION_SELECT_ALL);
        Model model = mInjector.getModel();

        // Exclude disabled files
        List<String> enabled = new ArrayList<>();
        for (String id : model.getModelIds()) {
            Cursor cursor = model.getItem(id);
            if (cursor == null) {
                Log.w(TAG, "Skipping selection. Can't obtain cursor for modeId: " + id);
                continue;
            }
            String docMimeType = getCursorString(
                    cursor, DocumentsContract.Document.COLUMN_MIME_TYPE);
            int docFlags = getCursorInt(cursor, DocumentsContract.Document.COLUMN_FLAGS);
            if (mInjector.config.isDocumentEnabled(docMimeType, docFlags, mState)) {
                enabled.add(id);
            }
        }

        // Only select things currently visible in the adapter.
        boolean changed = mSelectionMgr.setItemsSelected(enabled, true);
        if (changed) {
            mDisplayStateChangedListener.run();
        }
    }

    @Override
    @Nullable
    public DocumentInfo renameDocument(String name, DocumentInfo document) {
        throw new UnsupportedOperationException("Can't rename documents.");
    }

    @Override
    public boolean viewDocument(DocumentDetails doc) {
        throw new UnsupportedOperationException("Direct view not supported!");
+9 −0
Original line number Diff line number Diff line
@@ -28,6 +28,8 @@ import com.android.documentsui.base.RootInfo;
import com.android.documentsui.dirlist.DocumentDetails;
import com.android.documentsui.dirlist.Model;

import javax.annotation.Nullable;

public interface ActionHandler {

    void openSettings(RootInfo root);
@@ -62,6 +64,10 @@ public interface ActionHandler {

    void pasteIntoFolder(RootInfo root);

    void selectAllFiles();

    @Nullable DocumentInfo renameDocument(String name, DocumentInfo document);

    boolean viewDocument(DocumentDetails doc);

    boolean previewDocument(DocumentDetails doc);
@@ -97,6 +103,9 @@ public interface ActionHandler {
     */
    void initLocation(Intent intent);

    void registerDisplayStateChangedListener(Runnable l);
    void unregisterDisplayStateChangedListener(Runnable l);

    /**
     * Allow action handler to be initialized in a new scope.
     * @return
+2 −4
Original line number Diff line number Diff line
@@ -52,6 +52,7 @@ import com.android.documentsui.base.State;
import com.android.documentsui.base.State.ViewMode;
import com.android.documentsui.dirlist.AnimationView;
import com.android.documentsui.dirlist.DirectoryFragment;
import com.android.documentsui.dirlist.DocumentsAdapter;
import com.android.documentsui.prefs.LocalPreferences;
import com.android.documentsui.prefs.PreferencesMonitor;
import com.android.documentsui.queries.DebugCommandProcessor;
@@ -337,10 +338,7 @@ public abstract class BaseActivity
                return true;

            case R.id.menu_select_all:
                DirectoryFragment dir = getDirectoryFragment();
                if (dir != null) {
                    dir.selectAllFiles();
                }
                getInjector().actions.selectAllFiles();
                return true;

            default:
+7 −0
Original line number Diff line number Diff line
@@ -60,6 +60,9 @@ public class Injector<T extends ActionHandler> {
    @ContentScoped
    public SelectionManager selectionMgr;

    private final Model mModel = new Model();
    private DocumentsAdapter mAdapter;

    // must be initialized before calling super.onCreate because prefs
    // are used in State initialization.
    public Injector(
@@ -74,6 +77,10 @@ public class Injector<T extends ActionHandler> {
        this.dialogs = dialogs;
    }

    public Model getModel() {
        return mModel;
    }

    public FocusManager getFocusManager(RecyclerView view, Model model) {
        assert (focusManager != null);
        return focusManager.reset(view, model);
+12 −30
Original line number Diff line number Diff line
@@ -147,7 +147,7 @@ public class DirectoryFragment extends Fragment
    private BaseActivity mActivity;

    private State mState;
    private final Model mModel = new Model();
    private Model mModel;
    private final EventListener<Model.Update> mModelUpdateListener = new ModelUpdateListener();
    private final DocumentsAdapter.Environment mAdapterEnv = new AdapterEnvironment();
    private final LoaderCallbacks<DirectoryResult> mLoaderCallbacks = new LoaderBindings();
@@ -205,6 +205,8 @@ public class DirectoryFragment extends Fragment
        }
    };

    private final Runnable mOnDisplayStateChanged = this::onDisplayStateChanged;

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@@ -230,6 +232,11 @@ public class DirectoryFragment extends Fragment
        mFileList = view.findViewById(R.id.file_list);

        mInjector = activity.getInjector();
        mModel = mInjector.getModel();
        mModel.reset();

        mInjector.actions.registerDisplayStateChangedListener(mOnDisplayStateChanged);

        mDragHoverListener = mInjector.config.dragAndDropEnabled()
                ? DragHoverListener.create(new DirectoryDragListener(this), mRecView)
                : null;
@@ -243,6 +250,7 @@ public class DirectoryFragment extends Fragment
    @Override
    public void onDestroyView() {
        mSelectionMgr.clearSelection();
        mInjector.actions.unregisterDisplayStateChangedListener(mOnDisplayStateChanged);

        // Cancel any outstanding thumbnail requests
        final int count = mRecView.getChildCount();
@@ -486,10 +494,10 @@ public class DirectoryFragment extends Fragment

    public void onViewModeChanged() {
        // Mode change is just visual change; no need to kick loader.
        updateDisplayState();
        onDisplayStateChanged();
    }

    private void updateDisplayState() {
    private void onDisplayStateChanged() {
        updateLayout(mState.derivedMode);
        mRecView.setAdapter(mAdapter);
    }
@@ -660,7 +668,7 @@ public class DirectoryFragment extends Fragment
                return true;

            case R.id.menu_select_all:
                selectAllFiles();
                mActions.selectAllFiles();
                return true;

            case R.id.menu_rename:
@@ -882,31 +890,6 @@ public class DirectoryFragment extends Fragment
        getActivity().invalidateOptionsMenu();
    }

    public void selectAllFiles() {
        Metrics.logUserAction(getContext(), Metrics.USER_ACTION_SELECT_ALL);

        // Exclude disabled files
        List<String> enabled = new ArrayList<>();
        for (String id : mAdapter.getModelIds()) {
            Cursor cursor = mModel.getItem(id);
            if (cursor == null) {
                Log.w(TAG, "Skipping selection. Can't obtain cursor for modeId: " + id);
                continue;
            }
            String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
            int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
            if (isDocumentEnabled(docMimeType, docFlags)) {
                enabled.add(id);
            }
        }

        // Only select things currently visible in the adapter.
        boolean changed = mSelectionMgr.setItemsSelected(enabled, true);
        if (changed) {
            updateDisplayState();
        }
    }

    private void setupDragAndDropOnDocumentView(View view, Cursor cursor) {
        final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
        if (Document.MIME_TYPE_DIR.equals(docMimeType)) {
@@ -1339,7 +1322,6 @@ public class DirectoryFragment extends Fragment
        public void onLoaderReset(Loader<DirectoryResult> loader) {
            if (DEBUG) Log.d(TAG, "Resetting loader for: "
                        + DocumentInfo.debugString(mLocalState.mDocument));
            mModel.onLoaderReset();

            mRefreshLayout.setRefreshing(false);
        }
Loading