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

Commit 82cc533f authored by Steve McKay's avatar Steve McKay
Browse files

Decouple SelectionManager from DocumentsAdapter.

Extract interface for access to stable ids.

Bug: 64847011
Test: hachiko test -b -s && hachiko -m
Change-Id: I92af2e4f2f2ba30257ec2d538c384561a4f673b3
parent e77c0370
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -167,7 +167,7 @@ public final class FocusManager implements FocusHandler {
            return;
        }

        int pos = mScope.adapter.getModelIds().indexOf(mScope.pendingFocusId);
        int pos = mScope.adapter.getStableIds().indexOf(mScope.pendingFocusId);
        if (pos != -1) {
            focusItem(pos);
        }
@@ -558,7 +558,7 @@ public final class FocusManager implements FocusHandler {
            int itemCount = mScope.adapter.getItemCount();
            List<String> index = new ArrayList<>(itemCount);
            for (int i = 0; i < itemCount; i++) {
                String modelId = mScope.adapter.getModelId(i);
                String modelId = mScope.adapter.getStableId(i);
                Cursor cursor = mScope.model.getItem(modelId);
                if (modelId != null && cursor != null) {
                    String title = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
+1 −1
Original line number Diff line number Diff line
@@ -121,7 +121,7 @@ public class Injector<T extends ActionHandler> {

    public SelectionManager getSelectionManager(
            DocumentsAdapter adapter, SelectionPredicate canSetState) {
        return selectionMgr.reset(adapter, canSetState);
        return selectionMgr.reset(adapter, adapter, canSetState);
    }

    public final ActionModeController getActionModeController(
+6 −6
Original line number Diff line number Diff line
@@ -238,8 +238,8 @@ final class DirectoryAddonsAdapter extends DocumentsAdapter {
    }

    @Override
    public List<String> getModelIds() {
        return mDelegate.getModelIds();
    public List<String> getStableIds() {
        return mDelegate.getStableIds();
    }

    @Override
@@ -248,7 +248,7 @@ final class DirectoryAddonsAdapter extends DocumentsAdapter {
    }

    @Override
    public String getModelId(int p) {
    public String getStableId(int p) {
        if (p == mBreakPosition) {
            return null;
        }
@@ -261,12 +261,12 @@ final class DirectoryAddonsAdapter extends DocumentsAdapter {
            return null;
        }

        return mDelegate.getModelId(toDelegatePosition(p));
        return mDelegate.getStableId(toDelegatePosition(p));
    }

    @Override
    public void onItemSelectionChanged(String id) {
        mDelegate.onItemSelectionChanged(id);
    public void onSelectionStateChanged(String id) {
        mDelegate.onSelectionStateChanged(id);
    }

    // Listener we add to our delegate. This allows us to relay events published
+4 −21
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import com.android.documentsui.Model;
import com.android.documentsui.base.EventListener;
import com.android.documentsui.base.Features;
import com.android.documentsui.base.State;
import com.android.documentsui.selection.SelectionManager;

import java.util.List;

@@ -42,7 +43,7 @@ import java.util.List;
 * @see DirectoryAddonsAdapter
 */
public abstract class DocumentsAdapter
        extends RecyclerView.Adapter<DocumentHolder> {
        extends RecyclerView.Adapter<DocumentHolder> implements SelectionManager.Environment {
    // Item types used by ModelBackedDocumentsAdapter
    public static final int ITEM_TYPE_DOCUMENT = 1;
    public static final int ITEM_TYPE_DIRECTORY = 2;
@@ -54,26 +55,8 @@ public abstract class DocumentsAdapter
    // Payloads for notifyItemChange to distinguish between selection and other events.
    static final String SELECTION_CHANGED_MARKER = "Selection-Changed";

    /**
     * Returns a list of model IDs of items currently in the adapter.
     *
     * @return A list of Model IDs.
     */
    public abstract List<String> getModelIds();

    public abstract int getAdapterPosition(String modelId);

    /**
     * Triggers item-change notifications by stable ID (as opposed to position).
     * Passing an unrecognized ID will result in a warning in logcat, but no other error.
     */
    public abstract void onItemSelectionChanged(String id);

    /**
     * @return The model ID of the item at the given adapter position.
     */
    public abstract String getModelId(int position);

    abstract EventListener<Model.Update> getModelUpdateListener();

    /**
@@ -86,7 +69,7 @@ public abstract class DocumentsAdapter
    }

    public boolean hasModelIds() {
        return !getModelIds().isEmpty();
        return !getStableIds().isEmpty();
    }

    static boolean isDirectory(Cursor cursor) {
@@ -95,7 +78,7 @@ public abstract class DocumentsAdapter
    }

    boolean isDirectory(Model model, int position) {
        String modelId = getModelIds().get(position);
        String modelId = getStableIds().get(position);
        Cursor cursor = model.getItem(modelId);
        return isDirectory(cursor);
    }
+3 −3
Original line number Diff line number Diff line
@@ -156,7 +156,7 @@ final class ModelBackedDocumentsAdapter extends DocumentsAdapter {
    }

    @Override
    public String getModelId(int adapterPosition) {
    public String getStableId(int adapterPosition) {
        return mModelIds.get(adapterPosition);
    }

@@ -166,7 +166,7 @@ final class ModelBackedDocumentsAdapter extends DocumentsAdapter {
    }

    @Override
    public List<String> getModelIds() {
    public List<String> getStableIds() {
        return mModelIds;
    }

@@ -191,7 +191,7 @@ final class ModelBackedDocumentsAdapter extends DocumentsAdapter {
    }

    @Override
    public void onItemSelectionChanged(String id) {
    public void onSelectionStateChanged(String id) {
        int position = mModelIds.indexOf(id);

        if (position >= 0) {
Loading