1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.documentsui.dirlist;
18
19import static com.android.documentsui.model.DocumentInfo.getCursorString;
20
21import android.content.Context;
22import android.database.Cursor;
23import android.provider.DocumentsContract.Document;
24import android.support.v7.widget.GridLayoutManager;
25import android.support.v7.widget.RecyclerView;
26import android.util.SparseArray;
27
28import com.android.documentsui.State;
29
30import java.util.List;
31
32/**
33 * DocumentsAdapter provides glue between a directory Model, and RecylcerView. We've
34 * abstracted this a bit in order to decompose some specialized support
35 * for adding dummy layout objects (@see SectionBreakDocumentsAdapter). Handling of the
36 * dummy layout objects was error prone when interspersed with the core mode / adapter code.
37 *
38 * @see ModelBackedDocumentsAdapter
39 * @see SectionBreakDocumentsAdapter
40 */
41abstract class DocumentsAdapter
42        extends RecyclerView.Adapter<DocumentHolder>
43        implements Model.UpdateListener {
44
45    // Payloads for notifyItemChange to distinguish between selection and other events.
46    static final String SELECTION_CHANGED_MARKER = "Selection-Changed";
47
48    /**
49     * Returns a list of model IDs of items currently in the adapter. Excludes items that are
50     * currently hidden (see {@link #hide(String...)}).
51     *
52     * @return A list of Model IDs.
53     */
54    abstract List<String> getModelIds();
55
56    /**
57     * Triggers item-change notifications by stable ID (as opposed to position).
58     * Passing an unrecognized ID will result in a warning in logcat, but no other error.
59     */
60    abstract void onItemSelectionChanged(String id);
61
62    /**
63     * @return The model ID of the item at the given adapter position.
64     */
65    abstract String getModelId(int position);
66
67    /**
68     * Hides a set of items from the associated RecyclerView.
69     *
70     * @param ids The Model IDs of the items to hide.
71     * @return A SparseArray that maps the hidden IDs to their old positions. This can be used
72     *         to {@link #unhide} the items if necessary.
73     */
74    abstract public SparseArray<String> hide(String... ids);
75
76    /**
77     * Returns a class that yields the span size for a particular element. This is
78     * primarily useful in {@link SectionBreakDocumentsAdapterWrapper} where
79     * we adjust sizes.
80     */
81    GridLayoutManager.SpanSizeLookup createSpanSizeLookup() {
82        throw new UnsupportedOperationException();
83    }
84
85    static boolean isDirectory(Cursor cursor) {
86        final String mimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
87        return Document.MIME_TYPE_DIR.equals(mimeType);
88    }
89
90    boolean isDirectory(Model model, int position) {
91        String modelId = getModelIds().get(position);
92        Cursor cursor = model.getItem(modelId);
93        return isDirectory(cursor);
94    }
95
96    /**
97     * Environmental access for View adapter implementations.
98     */
99    interface Environment {
100        Context getContext();
101        int getColumnCount();
102        State getDisplayState();
103        boolean isSelected(String id);
104        Model getModel();
105        boolean isDocumentEnabled(String mimeType, int flags);
106        void initDocumentHolder(DocumentHolder holder);
107        void onBindDocumentHolder(DocumentHolder holder, Cursor cursor);
108    }
109}
110