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 
25 import androidx.recyclerview.widget.GridLayoutManager;
26 import androidx.recyclerview.widget.RecyclerView;
27 
28 import com.android.documentsui.ActionHandler;
29 import com.android.documentsui.Model;
30 import com.android.documentsui.base.EventListener;
31 import com.android.documentsui.base.Features;
32 import com.android.documentsui.base.State;
33 
34 import java.util.List;
35 
36 /**
37  * DocumentsAdapter provides glue between a directory Model, and RecyclerView. We've
38  * abstracted this a bit in order to decompose some specialized support
39  * for adding dummy layout objects (@see SectionBreakDocumentsAdapter). Handling of the
40  * dummy layout objects was error prone when interspersed with the core mode / adapter code.
41  *
42  * @see ModelBackedDocumentsAdapter
43  * @see DirectoryAddonsAdapter
44  */
45 public abstract class DocumentsAdapter 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 
getAdapterPosition(String modelId)54     public abstract int getAdapterPosition(String modelId);
getStableId(int adapterPosition)55     public abstract String getStableId(int adapterPosition);
getStableIds()56     public abstract List<String> getStableIds();
getPosition(String id)57     public abstract int getPosition(String id);
58 
getModelUpdateListener()59     abstract EventListener<Model.Update> getModelUpdateListener();
60 
61     /**
62      * Returns a class that yields the span size for a particular element. This is
63      * primarily useful in {@link DirectoryAddonsAdapter} where
64      * we adjust sizes.
65      */
createSpanSizeLookup()66     GridLayoutManager.SpanSizeLookup createSpanSizeLookup() {
67         throw new UnsupportedOperationException();
68     }
69 
isDirectory(Cursor cursor)70     static boolean isDirectory(Cursor cursor) {
71         final String mimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
72         return Document.MIME_TYPE_DIR.equals(mimeType);
73     }
74 
isDirectory(Model model, int position)75     boolean isDirectory(Model model, int position) {
76         String modelId = getStableIds().get(position);
77         Cursor cursor = model.getItem(modelId);
78         return isDirectory(cursor);
79     }
80 
81     /**
82      * Environmental access for View adapter implementations.
83      */
84     interface Environment {
getContext()85         Context getContext();
getFeatures()86         Features getFeatures();
getActionHandler()87         ActionHandler getActionHandler();
getColumnCount()88         int getColumnCount();
getDisplayState()89         State getDisplayState();
isInSearchMode()90         boolean isInSearchMode();
isSelected(String id)91         boolean isSelected(String id);
getModel()92         Model getModel();
getCallingAppName()93         String getCallingAppName();
isDocumentEnabled(String mimeType, int flags)94         boolean isDocumentEnabled(String mimeType, int flags);
initDocumentHolder(DocumentHolder holder)95         void initDocumentHolder(DocumentHolder holder);
onBindDocumentHolder(DocumentHolder holder, Cursor cursor)96         void onBindDocumentHolder(DocumentHolder holder, Cursor cursor);
97     }
98 }
99