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 
17 package com.android.documentsui.dirlist;
18 
19 import static com.android.documentsui.base.DocumentInfo.getCursorString;
20 
21 import android.content.Context;
22 import android.database.Cursor;
23 import android.provider.DocumentsContract.Document;
24 import android.support.v7.widget.GridLayoutManager;
25 import android.support.v7.widget.RecyclerView;
26 
27 import com.android.documentsui.ActionHandler;
28 import com.android.documentsui.Model;
29 import com.android.documentsui.base.EventListener;
30 import com.android.documentsui.base.Features;
31 import com.android.documentsui.base.State;
32 
33 import java.util.List;
34 
35 /**
36  * DocumentsAdapter provides glue between a directory Model, and RecylcerView. We've
37  * abstracted this a bit in order to decompose some specialized support
38  * for adding dummy layout objects (@see SectionBreakDocumentsAdapter). Handling of the
39  * dummy layout objects was error prone when interspersed with the core mode / adapter code.
40  *
41  * @see ModelBackedDocumentsAdapter
42  * @see DirectoryAddonsAdapter
43  */
44 public abstract class DocumentsAdapter
45         extends RecyclerView.Adapter<DocumentHolder> {
46     // Item types used by ModelBackedDocumentsAdapter
47     public static final int ITEM_TYPE_DOCUMENT = 1;
48     public static final int ITEM_TYPE_DIRECTORY = 2;
49     // Item types used by SectionBreakDocumentsAdapterWrapper
50     public static final int ITEM_TYPE_SECTION_BREAK = Integer.MAX_VALUE;
51     public static final int ITEM_TYPE_HEADER_MESSAGE = Integer.MAX_VALUE - 1;
52     public static final int ITEM_TYPE_INFLATED_MESSAGE = Integer.MAX_VALUE - 2;
53 
54     // Payloads for notifyItemChange to distinguish between selection and other events.
55     static final String SELECTION_CHANGED_MARKER = "Selection-Changed";
56 
57     /**
58      * Returns a list of model IDs of items currently in the adapter.
59      *
60      * @return A list of Model IDs.
61      */
getModelIds()62     public abstract List<String> getModelIds();
63 
64     /**
65      * Triggers item-change notifications by stable ID (as opposed to position).
66      * Passing an unrecognized ID will result in a warning in logcat, but no other error.
67      */
onItemSelectionChanged(String id)68     public abstract void onItemSelectionChanged(String id);
69 
70     /**
71      * @return The model ID of the item at the given adapter position.
72      */
getModelId(int position)73     public abstract String getModelId(int position);
74 
getModelUpdateListener()75     abstract EventListener<Model.Update> getModelUpdateListener();
76 
77     /**
78      * Returns a class that yields the span size for a particular element. This is
79      * primarily useful in {@link DirectoryAddonsAdapter} where
80      * we adjust sizes.
81      */
createSpanSizeLookup()82     GridLayoutManager.SpanSizeLookup createSpanSizeLookup() {
83         throw new UnsupportedOperationException();
84     }
85 
hasModelIds()86     public boolean hasModelIds() {
87         return !getModelIds().isEmpty();
88     }
89 
isDirectory(Cursor cursor)90     static boolean isDirectory(Cursor cursor) {
91         final String mimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
92         return Document.MIME_TYPE_DIR.equals(mimeType);
93     }
94 
isDirectory(Model model, int position)95     boolean isDirectory(Model model, int position) {
96         String modelId = getModelIds().get(position);
97         Cursor cursor = model.getItem(modelId);
98         return isDirectory(cursor);
99     }
100 
101     /**
102      * Environmental access for View adapter implementations.
103      */
104     interface Environment {
getContext()105         Context getContext();
getFeatures()106         Features getFeatures();
getActionHandler()107         ActionHandler getActionHandler();
getColumnCount()108         int getColumnCount();
getDisplayState()109         State getDisplayState();
isInSearchMode()110         boolean isInSearchMode();
isSelected(String id)111         boolean isSelected(String id);
getModel()112         Model getModel();
isDocumentEnabled(String mimeType, int flags)113         boolean isDocumentEnabled(String mimeType, int flags);
initDocumentHolder(DocumentHolder holder)114         void initDocumentHolder(DocumentHolder holder);
onBindDocumentHolder(DocumentHolder holder, Cursor cursor)115         void onBindDocumentHolder(DocumentHolder holder, Cursor cursor);
116     }
117 }
118