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 androidx.recyclerview.widget.GridLayoutManager;
25 import androidx.recyclerview.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 RecyclerView. 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 extends RecyclerView.Adapter<DocumentHolder> {
45     // Item types used by ModelBackedDocumentsAdapter
46     public static final int ITEM_TYPE_DOCUMENT = 1;
47     public static final int ITEM_TYPE_DIRECTORY = 2;
48     // Item types used by SectionBreakDocumentsAdapterWrapper
49     public static final int ITEM_TYPE_SECTION_BREAK = Integer.MAX_VALUE;
50     public static final int ITEM_TYPE_HEADER_MESSAGE = Integer.MAX_VALUE - 1;
51     public static final int ITEM_TYPE_INFLATED_MESSAGE = Integer.MAX_VALUE - 2;
52 
getAdapterPosition(String modelId)53     public abstract int getAdapterPosition(String modelId);
getStableId(int adapterPosition)54     public abstract String getStableId(int adapterPosition);
getStableIds()55     public abstract List<String> getStableIds();
getPosition(String id)56     public abstract int getPosition(String id);
57 
getModelUpdateListener()58     abstract EventListener<Model.Update> getModelUpdateListener();
59 
60     /**
61      * Returns a class that yields the span size for a particular element. This is
62      * primarily useful in {@link DirectoryAddonsAdapter} where
63      * we adjust sizes.
64      */
createSpanSizeLookup()65     GridLayoutManager.SpanSizeLookup createSpanSizeLookup() {
66         throw new UnsupportedOperationException();
67     }
68 
isDirectory(Cursor cursor)69     static boolean isDirectory(Cursor cursor) {
70         final String mimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
71         return Document.MIME_TYPE_DIR.equals(mimeType);
72     }
73 
isDirectory(Model model, int position)74     boolean isDirectory(Model model, int position) {
75         String modelId = getStableIds().get(position);
76         Cursor cursor = model.getItem(modelId);
77         return isDirectory(cursor);
78     }
79 
80     /**
81      * Environmental access for View adapter implementations.
82      */
83     interface Environment {
getContext()84         Context getContext();
getFeatures()85         Features getFeatures();
getActionHandler()86         ActionHandler getActionHandler();
getColumnCount()87         int getColumnCount();
getDisplayState()88         State getDisplayState();
isInSearchMode()89         boolean isInSearchMode();
isSelected(String id)90         boolean isSelected(String id);
getModel()91         Model getModel();
isDocumentEnabled(String mimeType, int flags)92         boolean isDocumentEnabled(String mimeType, int flags);
initDocumentHolder(DocumentHolder holder)93         void initDocumentHolder(DocumentHolder holder);
onBindDocumentHolder(DocumentHolder holder, Cursor cursor)94         void onBindDocumentHolder(DocumentHolder holder, Cursor cursor);
95     }
96 }
97