blob: 297584f832ed895a64b51bab998a3c805e162531 [file] [log] [blame]
Steve McKayc5ecf892015-12-22 18:15:31 -08001/*
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
17package com.android.documentsui.dirlist;
18
Steve McKayd0805062016-09-15 14:30:38 -070019import static com.android.documentsui.base.DocumentInfo.getCursorString;
Steve McKayc5ecf892015-12-22 18:15:31 -080020
21import android.content.Context;
22import android.database.Cursor;
23import android.provider.DocumentsContract.Document;
24import android.support.v7.widget.GridLayoutManager;
25import android.support.v7.widget.RecyclerView;
Steve McKayc5ecf892015-12-22 18:15:31 -080026
Garfield Tane9670332017-03-06 18:33:23 -080027import com.android.documentsui.Model;
Steve McKay990f76e2016-09-16 12:36:58 -070028import com.android.documentsui.base.EventListener;
Steve McKay98f8c5f2017-03-03 13:52:14 -080029import com.android.documentsui.base.Features;
Steve McKayd9caa6a2016-09-15 16:36:45 -070030import com.android.documentsui.base.State;
Steve McKayc5ecf892015-12-22 18:15:31 -080031
Steve McKayc5ecf892015-12-22 18:15:31 -080032import 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 Lin7b38f342016-12-14 11:52:35 -080041 * @see DirectoryAddonsAdapter
Steve McKayc5ecf892015-12-22 18:15:31 -080042 */
Steve McKay17b761e2016-09-20 17:26:46 -070043public abstract class DocumentsAdapter
Steve McKay990f76e2016-09-16 12:36:58 -070044 extends RecyclerView.Adapter<DocumentHolder> {
Ben Lin7b38f342016-12-14 11:52:35 -080045 // 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 McKayc5ecf892015-12-22 18:15:31 -080052
53 // Payloads for notifyItemChange to distinguish between selection and other events.
54 static final String SELECTION_CHANGED_MARKER = "Selection-Changed";
55
56 /**
Ben Lin7b38f342016-12-14 11:52:35 -080057 * Returns a list of model IDs of items currently in the adapter.
Steve McKayc5ecf892015-12-22 18:15:31 -080058 *
59 * @return A list of Model IDs.
60 */
Steve McKay17b761e2016-09-20 17:26:46 -070061 public abstract List<String> getModelIds();
Steve McKayc5ecf892015-12-22 18:15:31 -080062
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 McKay4f78ba62016-10-04 16:48:49 -070067 public abstract void onItemSelectionChanged(String id);
Steve McKayc5ecf892015-12-22 18:15:31 -080068
69 /**
70 * @return The model ID of the item at the given adapter position.
71 */
Steve McKay17b761e2016-09-20 17:26:46 -070072 public abstract String getModelId(int position);
Steve McKayc5ecf892015-12-22 18:15:31 -080073
Steve McKay990f76e2016-09-16 12:36:58 -070074 abstract EventListener<Model.Update> getModelUpdateListener();
75
Steve McKayc5ecf892015-12-22 18:15:31 -080076 /**
Steve McKayc5ecf892015-12-22 18:15:31 -080077 * Returns a class that yields the span size for a particular element. This is
Ben Lin7b38f342016-12-14 11:52:35 -080078 * primarily useful in {@link DirectoryAddonsAdapter} where
Steve McKayc5ecf892015-12-22 18:15:31 -080079 * we adjust sizes.
80 */
81 GridLayoutManager.SpanSizeLookup createSpanSizeLookup() {
Ben Kwabc7df442016-02-02 23:00:02 -080082 throw new UnsupportedOperationException();
Steve McKayc5ecf892015-12-22 18:15:31 -080083 }
84
Ben Lin7b38f342016-12-14 11:52:35 -080085 public boolean hasModelIds() {
86 return !getModelIds().isEmpty();
87 }
88
Steve McKayc5ecf892015-12-22 18:15:31 -080089 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 McKay98f8c5f2017-03-03 13:52:14 -0800105 Features getFeatures();
Steve McKayc5ecf892015-12-22 18:15:31 -0800106 int getColumnCount();
107 State getDisplayState();
Ben Lin7b38f342016-12-14 11:52:35 -0800108 boolean isInSearchMode();
Steve McKayc5ecf892015-12-22 18:15:31 -0800109 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}