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

Commit e77c0370 authored by Steve McKay's avatar Steve McKay
Browse files

Unify selection listeners under single interface.

One of likely many selection manager > support-lib preparation CLs.
Move SelectionMetadata off into dirlist.

Bug: 64847011
Test: hachiko test -b -s && hachiko -m
Change-Id: Ie90b783c537710ee52e77a600fe75f3ac155fb30
parent 9e8b9c02
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -45,7 +45,7 @@ import java.util.function.IntConsumer;
 * A controller that listens to selection changes and manages life cycles of action modes.
 */
public class ActionModeController
        implements SelectionManager.Callback, ActionMode.Callback, ActionModeAddons {
        implements SelectionManager.EventListener, ActionMode.Callback, ActionModeAddons {

    private static final String TAG = "ActionModeController";

@@ -122,6 +122,16 @@ public class ActionModeController
        }
    }

    @Override
    public void onItemStateChanged(String id, boolean selected) {
        // Not utilized.
    }

    @Override
    public void onSelectionReset() {
        // Not utilized.
    }

    // Called when the user exits the action mode
    @Override
    public void onDestroyActionMode(ActionMode mode) {
+2 −3
Original line number Diff line number Diff line
@@ -94,7 +94,6 @@ import com.android.documentsui.selection.BandController;
import com.android.documentsui.selection.GestureSelector;
import com.android.documentsui.selection.Selection;
import com.android.documentsui.selection.SelectionManager;
import com.android.documentsui.selection.SelectionMetadata;
import com.android.documentsui.services.FileOperation;
import com.android.documentsui.services.FileOperationService;
import com.android.documentsui.services.FileOperationService.OpType;
@@ -324,7 +323,7 @@ public class DirectoryFragment extends Fragment implements SwipeRefreshLayout.On
                new AccessibilityEventRouter(mRecView,
                        (View child) -> onAccessibilityClick(child)));
        mSelectionMetadata = new SelectionMetadata(mModel::getItem);
        mSelectionMgr.addItemCallback(mSelectionMetadata);
        mSelectionMgr.addEventListener(mSelectionMetadata);

        GestureSelector gestureSel = GestureSelector.create(mSelectionMgr, mRecView, mReloadLock);

@@ -386,7 +385,7 @@ public class DirectoryFragment extends Fragment implements SwipeRefreshLayout.On
                mSelectionMetadata,
                this::handleMenuItemClick);

        mSelectionMgr.addCallback(mActionModeController);
        mSelectionMgr.addEventListener(mActionModeController);

        final ActivityManager am = (ActivityManager) mActivity.getSystemService(
                Context.ACTIVITY_SERVICE);
+13 −5
Original line number Diff line number Diff line
@@ -14,14 +14,11 @@
 * limitations under the License.
 */

package com.android.documentsui.selection;
package com.android.documentsui.dirlist;

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

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.database.Cursor;
import android.provider.DocumentsContract.Document;
import android.util.Log;
@@ -30,6 +27,7 @@ import com.android.documentsui.MenuManager;
import com.android.documentsui.archives.ArchivesProvider;
import com.android.documentsui.base.MimeTypes;
import com.android.documentsui.roots.RootCursorWrapper;
import com.android.documentsui.selection.SelectionManager;

import java.util.function.Function;

@@ -37,7 +35,7 @@ import java.util.function.Function;
 * A class that holds metadata
 */
public class SelectionMetadata
        implements MenuManager.SelectionDetails, SelectionManager.ItemCallback {
        implements MenuManager.SelectionDetails, SelectionManager.EventListener {

    private static final String TAG = "SelectionMetadata";
    private final static int FLAG_CAN_DELETE =
@@ -104,6 +102,16 @@ public class SelectionMetadata
        }
    }

    @Override
    public void onSelectionChanged() {
        // Not utilized.
    }

    @Override
    public void onSelectionRestored() {
        // Not utilized.
    }

    @Override
    public void onSelectionReset() {
        mFileCount = 0;
+29 −25
Original line number Diff line number Diff line
@@ -61,8 +61,7 @@ public final class SelectionManager {

    private final Selection mSelection = new Selection();

    private final List<Callback> mCallbacks = new ArrayList<>(1);
    private final List<ItemCallback> mItemCallbacks = new ArrayList<>(1);
    private final List<EventListener> mEventListeners = new ArrayList<>(1);

    private @Nullable DocumentsAdapter mAdapter;
    private @Nullable Range mRanger;
@@ -77,8 +76,7 @@ public final class SelectionManager {

    public SelectionManager reset(DocumentsAdapter adapter, SelectionPredicate canSetState) {

        mCallbacks.clear();
        mItemCallbacks.clear();
        mEventListeners.clear();
        if (mAdapter != null && mAdapterObserver != null) {
            mAdapter.unregisterAdapterDataObserver(mAdapterObserver);
        }
@@ -148,14 +146,9 @@ public final class SelectionManager {
     *
     * @param callback
     */
    public void addCallback(Callback callback) {
    public void addEventListener(EventListener callback) {
        assert(callback != null);
        mCallbacks.add(callback);
    }

    public void addItemCallback(ItemCallback itemCallback) {
        assert(itemCallback != null);
        mItemCallbacks.add(itemCallback);
        mEventListeners.add(callback);
    }

    public boolean hasSelection() {
@@ -435,18 +428,17 @@ public final class SelectionManager {
    }

    private void notifyDataChanged() {
        final int lastListener = mItemCallbacks.size() - 1;

        for (int i = lastListener; i >= 0; i--) {
            mItemCallbacks.get(i).onSelectionReset();
        }
        notifySelectionReset();

        final int lastListener = mEventListeners.size() - 1;
        for (String id : mSelection) {
            // TODO: Why do we deselect in notify changed.
            if (!canSetState(id, true)) {
                attemptDeselect(id);
            } else {
                for (int i = lastListener; i >= 0; i--) {
                    mItemCallbacks.get(i).onItemStateChanged(id, true);
                    mEventListeners.get(i).onItemStateChanged(id, true);
                }
            }
        }
@@ -458,9 +450,9 @@ public final class SelectionManager {
     */
    void notifyItemStateChanged(String id, boolean selected) {
        assert(id != null);
        int lastListener = mItemCallbacks.size() - 1;
        int lastListener = mEventListeners.size() - 1;
        for (int i = lastListener; i >= 0; i--) {
            mItemCallbacks.get(i).onItemStateChanged(id, selected);
            mEventListeners.get(i).onItemStateChanged(id, selected);
        }
        mAdapter.onItemSelectionChanged(id);
    }
@@ -472,16 +464,23 @@ public final class SelectionManager {
     * selection from one item to another.
     */
    void notifySelectionChanged() {
        int lastListener = mCallbacks.size() - 1;
        int lastListener = mEventListeners.size() - 1;
        for (int i = lastListener; i > -1; i--) {
            mCallbacks.get(i).onSelectionChanged();
            mEventListeners.get(i).onSelectionChanged();
        }
    }

    private void notifySelectionRestored() {
        int lastListener = mCallbacks.size() - 1;
        int lastListener = mEventListeners.size() - 1;
        for (int i = lastListener; i > -1; i--) {
            mEventListeners.get(i).onSelectionRestored();
        }
    }

    private void notifySelectionReset() {
        int lastListener = mEventListeners.size() - 1;
        for (int i = lastListener; i > -1; i--) {
            mCallbacks.get(i).onSelectionRestored();
            mEventListeners.get(i).onSelectionReset();
        }
    }

@@ -549,13 +548,18 @@ public final class SelectionManager {
        notifySelectionChanged();
    }

    public interface ItemCallback {
    public interface EventListener {

        /**
         * Called when state of an item has been changed.
         */
        void onItemStateChanged(String id, boolean selected);

        /**
         * Called when selection is reset (cleared).
         */
        void onSelectionReset();
    }

    public interface Callback {
        /**
         * Called immediately after completion of any set of changes.
         */
+11 −12
Original line number Diff line number Diff line
@@ -26,23 +26,22 @@ import com.android.documentsui.selection.Selection;
/**
 * Helper class for making assertions against the state of a {@link SelectionManager} instance and
 * the consistency of states between {@link SelectionManager} and
 * {@link SelectionManager.ItemCallback}.
 * {@link SelectionManager.ItemEventCallback}.
 */
public final class SelectionProbe {

    private final SelectionManager mMgr;
    private final TestItemSelectionListener mTestCallback;
    private final TestSelectionEventListener mSelectionListener;

    public SelectionProbe(SelectionManager mgr) {
        mMgr = mgr;
        mTestCallback = new TestItemSelectionListener();

        mMgr.addItemCallback(mTestCallback);
        mSelectionListener = new TestSelectionEventListener();
        mMgr.addEventListener(mSelectionListener);
    }

    public SelectionProbe(SelectionManager mgr, TestItemSelectionListener testCallback) {
    public SelectionProbe(SelectionManager mgr, TestSelectionEventListener selectionListener) {
        mMgr = mgr;
        mTestCallback = testCallback;
        mSelectionListener = selectionListener;
    }

    public void assertRangeSelected(int begin, int end) {
@@ -66,20 +65,20 @@ public final class SelectionProbe {
        Selection selection = mMgr.getSelection();
        assertEquals(selection.toString(), expected, selection.size());

        mTestCallback.assertSelectionSize(expected);
        mSelectionListener.assertSelectionSize(expected);
    }

    public void assertNoSelection() {
        assertSelectionSize(0);

        mTestCallback.assertNoSelection();
        mSelectionListener.assertNoSelection();
    }

    public void assertSelection(int... ids) {
        assertSelected(ids);
        assertEquals(ids.length, mMgr.getSelection().size());

        mTestCallback.assertSelectionSize(ids.length);
        mSelectionListener.assertSelectionSize(ids.length);
    }

    public void assertSelected(int... ids) {
@@ -88,7 +87,7 @@ public final class SelectionProbe {
            String sid = String.valueOf(id);
            assertTrue(sid + " is not in selection " + sel, sel.contains(sid));

            mTestCallback.assertSelected(sid);
            mSelectionListener.assertSelected(sid);
        }
    }

@@ -98,7 +97,7 @@ public final class SelectionProbe {
            String sid = String.valueOf(id);
            assertFalse(sid + " is in selection " + sel, sel.contains(sid));

            mTestCallback.assertNotSelected(sid);
            mSelectionListener.assertNotSelected(sid);
        }
    }

Loading