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

Commit ab6fb6fc authored by Steve McKay's avatar Steve McKay Committed by Android (Google) Code Review
Browse files

Merge "Fix event relay to correctly dispatch events."

parents 8885958f 955e46d0
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -58,7 +58,7 @@ abstract class DocumentsAdapter
     * 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.
     */
    abstract void notifyItemSelectionChanged(String id);
    abstract void onItemSelectionChanged(String id);

    /**
     * @return The model ID of the item at the given adapter position.
+13 −2
Original line number Diff line number Diff line
@@ -86,8 +86,19 @@ public class Model implements SiblingProvider {
    private static String createModelId(Cursor c) {
        // TODO: Maybe more efficient to use just the document ID, in cases where there is only one
        // authority (which should be the majority of cases).
        return getCursorString(c, RootCursorWrapper.COLUMN_AUTHORITY) +
                "|" + getCursorString(c, Document.COLUMN_DOCUMENT_ID);
        return createModelId(
                getCursorString(c, RootCursorWrapper.COLUMN_AUTHORITY),
                getCursorString(c, Document.COLUMN_DOCUMENT_ID));
    }

    /**
     * Generates a Model ID for a cursor entry that refers to a document. The Model ID is a unique
     * string that can be used to identify the document referred to by the cursor.
     *
     * @param c A cursor that refers to a document.
     */
    static String createModelId(String authority, String docId) {
        return authority + "|" + docId;
    }

    private void notifyUpdateListeners() {
+12 −5
Original line number Diff line number Diff line
@@ -25,7 +25,6 @@ import static com.android.documentsui.model.DocumentInfo.getCursorString;

import android.database.Cursor;
import android.provider.DocumentsContract.Document;
import android.support.v7.widget.GridLayoutManager;
import android.util.Log;
import android.util.SparseArray;
import android.view.ViewGroup;
@@ -187,9 +186,17 @@ final class ModelBackedDocumentsAdapter extends DocumentsAdapter {
    @Override
    public void unhide(SparseArray<String> ids) {
        if (DEBUG) Log.d(TAG, "Un-iding ids: " + ids);
        // Proceed backwards through the list of items, because each addition causes the
        // positions of all subsequent items to change.
        for (int i = ids.size() - 1; i >= 0; --i) {

        // An ArrayList can shrink at runtime...and in fact
        // it does when we clear it completely.
        // This means we can't call add(pos, id) without
        // first checking the list size.
        List<String> oldIds = mModelIds;
        mModelIds = new ArrayList<>(oldIds.size() + ids.size());
        mModelIds.addAll(oldIds);

        // Finally insert the unhidden items.
        for (int i = 0; i < ids.size(); i++) {
            int pos = ids.keyAt(i);
            String id = ids.get(pos);
            mHiddenIds.remove(id);
@@ -211,7 +218,7 @@ final class ModelBackedDocumentsAdapter extends DocumentsAdapter {
    }

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

        if (position >= 0) {
+1 −1
Original line number Diff line number Diff line
@@ -976,7 +976,7 @@ public final class MultiSelectManager implements View.OnKeyListener {

        @Override
        public void notifyItemChanged(String id) {
            mAdapter.notifyItemSelectionChanged(id);
            mAdapter.onItemSelectionChanged(id);
        }

        @Override
+42 −36
Original line number Diff line number Diff line
@@ -43,40 +43,9 @@ final class SectionBreakDocumentsAdapterWrapper extends DocumentsAdapter {
        mEnv = environment;
        mDelegate = delegate;

        // Events and information flows two ways between recycler view and adapter.
        // So we need to listen to events on our delegate and forward them
        // to our listeners with a corrected position.
        AdapterDataObserver adapterDataObserver = new AdapterDataObserver() {
            public void onChanged() {
                throw new UnsupportedOperationException();
            }

            public void onItemRangeChanged(int positionStart, int itemCount) {
                checkArgument(itemCount == 1);
            }

            public void onItemRangeInserted(int positionStart, int itemCount) {
                checkArgument(itemCount == 1);
                if (positionStart < mBreakPosition) {
                    mBreakPosition++;
                }
                notifyItemRangeInserted(toViewPosition(positionStart), itemCount);
            }

            public void onItemRangeRemoved(int positionStart, int itemCount) {
                checkArgument(itemCount == 1);
                if (positionStart < mBreakPosition) {
                    mBreakPosition--;
                }
                notifyItemRangeRemoved(toViewPosition(positionStart), itemCount);
            }

            public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
                throw new UnsupportedOperationException();
            }
        };

        mDelegate.registerAdapterDataObserver(adapterDataObserver);
        // Relay events published by our delegate to our listeners (presumably RecyclerView)
        // with adjusted positions.
        mDelegate.registerAdapterDataObserver(new EventRelay());
    }

    public GridLayoutManager.SpanSizeLookup createSpanSizeLookup() {
@@ -205,7 +174,44 @@ final class SectionBreakDocumentsAdapterWrapper extends DocumentsAdapter {
    }

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

    // Listener we add to our delegate. This allows us to relay events published
    // by the delegate to our listeners (presumably RecyclerView) with adjusted positions.
    private final class EventRelay extends AdapterDataObserver {
        public void onChanged() {
            throw new UnsupportedOperationException();
        }

        public void onItemRangeChanged(int positionStart, int itemCount) {
            throw new UnsupportedOperationException();
        }

        public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
            checkArgument(itemCount == 1);
            notifyItemRangeChanged(toViewPosition(positionStart), itemCount, payload);
        }

        public void onItemRangeInserted(int positionStart, int itemCount) {
            checkArgument(itemCount == 1);
            if (positionStart < mBreakPosition) {
                mBreakPosition++;
            }
            notifyItemRangeInserted(toViewPosition(positionStart), itemCount);
        }

        public void onItemRangeRemoved(int positionStart, int itemCount) {
            checkArgument(itemCount == 1);
            if (positionStart < mBreakPosition) {
                mBreakPosition--;
            }
            notifyItemRangeRemoved(toViewPosition(positionStart), itemCount);
        }

        public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
            throw new UnsupportedOperationException();
        }
    }
}
Loading