Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 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 | |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.base.DocumentInfo.getCursorString; |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 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; |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 26 | |
Garfield Tan | e967033 | 2017-03-06 18:33:23 -0800 | [diff] [blame] | 27 | import com.android.documentsui.Model; |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 28 | import com.android.documentsui.base.EventListener; |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 29 | import com.android.documentsui.base.Features; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 30 | import com.android.documentsui.base.State; |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 31 | |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 32 | import java.util.List; |
| 33 | |
| 34 | /** |
| 35 | * DocumentsAdapter provides glue between a directory Model, and RecylcerView. We've |
| 36 | * abstracted this a bit in order to decompose some specialized support |
| 37 | * for adding dummy layout objects (@see SectionBreakDocumentsAdapter). Handling of the |
| 38 | * dummy layout objects was error prone when interspersed with the core mode / adapter code. |
| 39 | * |
| 40 | * @see ModelBackedDocumentsAdapter |
Ben Lin | 7b38f34 | 2016-12-14 11:52:35 -0800 | [diff] [blame] | 41 | * @see DirectoryAddonsAdapter |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 42 | */ |
Steve McKay | 17b761e | 2016-09-20 17:26:46 -0700 | [diff] [blame] | 43 | public abstract class DocumentsAdapter |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 44 | extends RecyclerView.Adapter<DocumentHolder> { |
Ben Lin | 7b38f34 | 2016-12-14 11:52:35 -0800 | [diff] [blame] | 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; |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 52 | |
| 53 | // Payloads for notifyItemChange to distinguish between selection and other events. |
| 54 | static final String SELECTION_CHANGED_MARKER = "Selection-Changed"; |
| 55 | |
| 56 | /** |
Ben Lin | 7b38f34 | 2016-12-14 11:52:35 -0800 | [diff] [blame] | 57 | * Returns a list of model IDs of items currently in the adapter. |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 58 | * |
| 59 | * @return A list of Model IDs. |
| 60 | */ |
Steve McKay | 17b761e | 2016-09-20 17:26:46 -0700 | [diff] [blame] | 61 | public abstract List<String> getModelIds(); |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Triggers item-change notifications by stable ID (as opposed to position). |
| 65 | * Passing an unrecognized ID will result in a warning in logcat, but no other error. |
| 66 | */ |
Steve McKay | 4f78ba6 | 2016-10-04 16:48:49 -0700 | [diff] [blame] | 67 | public abstract void onItemSelectionChanged(String id); |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * @return The model ID of the item at the given adapter position. |
| 71 | */ |
Steve McKay | 17b761e | 2016-09-20 17:26:46 -0700 | [diff] [blame] | 72 | public abstract String getModelId(int position); |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 73 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 74 | abstract EventListener<Model.Update> getModelUpdateListener(); |
| 75 | |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 76 | /** |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 77 | * Returns a class that yields the span size for a particular element. This is |
Ben Lin | 7b38f34 | 2016-12-14 11:52:35 -0800 | [diff] [blame] | 78 | * primarily useful in {@link DirectoryAddonsAdapter} where |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 79 | * we adjust sizes. |
| 80 | */ |
| 81 | GridLayoutManager.SpanSizeLookup createSpanSizeLookup() { |
Ben Kwa | bc7df44 | 2016-02-02 23:00:02 -0800 | [diff] [blame] | 82 | throw new UnsupportedOperationException(); |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Ben Lin | 7b38f34 | 2016-12-14 11:52:35 -0800 | [diff] [blame] | 85 | public boolean hasModelIds() { |
| 86 | return !getModelIds().isEmpty(); |
| 87 | } |
| 88 | |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 89 | static boolean isDirectory(Cursor cursor) { |
| 90 | final String mimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 91 | return Document.MIME_TYPE_DIR.equals(mimeType); |
| 92 | } |
| 93 | |
| 94 | boolean isDirectory(Model model, int position) { |
| 95 | String modelId = getModelIds().get(position); |
| 96 | Cursor cursor = model.getItem(modelId); |
| 97 | return isDirectory(cursor); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Environmental access for View adapter implementations. |
| 102 | */ |
| 103 | interface Environment { |
| 104 | Context getContext(); |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 105 | Features getFeatures(); |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 106 | int getColumnCount(); |
| 107 | State getDisplayState(); |
Ben Lin | 7b38f34 | 2016-12-14 11:52:35 -0800 | [diff] [blame] | 108 | boolean isInSearchMode(); |
Steve McKay | c5ecf89 | 2015-12-22 18:15:31 -0800 | [diff] [blame] | 109 | boolean isSelected(String id); |
| 110 | Model getModel(); |
| 111 | boolean isDocumentEnabled(String mimeType, int flags); |
| 112 | void initDocumentHolder(DocumentHolder holder); |
| 113 | void onBindDocumentHolder(DocumentHolder holder, Cursor cursor); |
| 114 | } |
| 115 | } |