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.model.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 import android.util.SparseArray; 27 28 import com.android.documentsui.State; 29 30 import 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 */ 41 abstract 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 */ getModelIds()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 */ onItemSelectionChanged(String id)60 abstract void onItemSelectionChanged(String id); 61 62 /** 63 * @return The model ID of the item at the given adapter position. 64 */ getModelId(int position)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 */ hide(String... ids)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 */ createSpanSizeLookup()81 GridLayoutManager.SpanSizeLookup createSpanSizeLookup() { 82 throw new UnsupportedOperationException(); 83 } 84 isDirectory(Cursor cursor)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 isDirectory(Model model, int position)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 { getContext()100 Context getContext(); getColumnCount()101 int getColumnCount(); getDisplayState()102 State getDisplayState(); isSelected(String id)103 boolean isSelected(String id); getModel()104 Model getModel(); isDocumentEnabled(String mimeType, int flags)105 boolean isDocumentEnabled(String mimeType, int flags); initDocumentHolder(DocumentHolder holder)106 void initDocumentHolder(DocumentHolder holder); onBindDocumentHolder(DocumentHolder holder, Cursor cursor)107 void onBindDocumentHolder(DocumentHolder holder, Cursor cursor); 108 } 109 } 110