blob: 43aed9a5ec2e86d232b829cc989d8dcbe99604cb [file] [log] [blame]
Ben Kwa0497da82015-11-30 23:00:02 -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 McKayd9caa6a2016-09-15 16:36:45 -070020import static com.android.documentsui.base.Shared.DEBUG;
Ben Linf8f06e92017-01-27 17:15:48 -080021import static com.android.documentsui.base.Shared.VERBOSE;
Ben Kwa0497da82015-11-30 23:00:02 -080022
Steve McKay990f76e2016-09-16 12:36:58 -070023import android.annotation.IntDef;
Ben Linf8f06e92017-01-27 17:15:48 -080024import android.app.RecoverableSecurityException;
Ben Kwa0497da82015-11-30 23:00:02 -080025import android.database.Cursor;
Tomasz Mikolajewski20c04c52016-03-15 15:55:46 +090026import android.database.MergeCursor;
Steve McKay84769b82016-06-09 10:46:07 -070027import android.net.Uri;
Ben Kwa0497da82015-11-30 23:00:02 -080028import android.os.Bundle;
Ben Kwa0497da82015-11-30 23:00:02 -080029import android.provider.DocumentsContract;
30import android.provider.DocumentsContract.Document;
31import android.support.annotation.Nullable;
32import android.support.annotation.VisibleForTesting;
Ben Kwa0497da82015-11-30 23:00:02 -080033import android.util.Log;
Ben Kwa0497da82015-11-30 23:00:02 -080034
Ben Kwa0497da82015-11-30 23:00:02 -080035import com.android.documentsui.DirectoryResult;
Steve McKay98f8c5f2017-03-03 13:52:14 -080036import com.android.documentsui.base.DocumentFilters;
Steve McKayd0805062016-09-15 14:30:38 -070037import com.android.documentsui.base.DocumentInfo;
Steve McKay990f76e2016-09-16 12:36:58 -070038import com.android.documentsui.base.EventListener;
Steve McKay98f8c5f2017-03-03 13:52:14 -080039import com.android.documentsui.base.Features;
Steve McKayd9caa6a2016-09-15 16:36:45 -070040import com.android.documentsui.roots.RootCursorWrapper;
Steve McKay4f78ba62016-10-04 16:48:49 -070041import com.android.documentsui.selection.Selection;
Ben Kwa0497da82015-11-30 23:00:02 -080042
Steve McKay990f76e2016-09-16 12:36:58 -070043import java.lang.annotation.Retention;
44import java.lang.annotation.RetentionPolicy;
Ben Kwa0497da82015-11-30 23:00:02 -080045import java.util.ArrayList;
Ben Kwa0497da82015-11-30 23:00:02 -080046import java.util.HashMap;
Jon Mann38f14392017-01-25 13:51:55 -080047import java.util.HashSet;
Ben Kwa0497da82015-11-30 23:00:02 -080048import java.util.List;
Ben Kwab8a5e082015-12-07 13:25:27 -080049import java.util.Map;
Jon Mann38f14392017-01-25 13:51:55 -080050import java.util.Set;
Steve McKayd0718952016-10-10 13:43:36 -070051import java.util.function.Predicate;
Ben Kwa0497da82015-11-30 23:00:02 -080052
53/**
54 * The data model for the current loaded directory.
55 */
56@VisibleForTesting
Tomasz Mikolajewskid71bd612016-02-16 12:28:43 +090057public class Model {
Steve McKayd0718952016-10-10 13:43:36 -070058
Ben Kwa0497da82015-11-30 23:00:02 -080059 private static final String TAG = "Model";
Ben Kwab8a5e082015-12-07 13:25:27 -080060
Steve McKay98f8c5f2017-03-03 13:52:14 -080061 @Nullable String info;
62 @Nullable String error;
63 @Nullable DocumentInfo doc;
64
65 private final Features mFeatures;
Jon Mann38f14392017-01-25 13:51:55 -080066 /** Maps Model ID to cursor positions, for looking up items by Model ID. */
67 private final Map<String, Integer> mPositions = new HashMap<>();
68 private final Set<String> mFileNames = new HashSet<>();
69
Ben Kwa0497da82015-11-30 23:00:02 -080070 private boolean mIsLoading;
Steve McKay990f76e2016-09-16 12:36:58 -070071 private List<EventListener<Update>> mUpdateListeners = new ArrayList<>();
Steve McKay98f8c5f2017-03-03 13:52:14 -080072 private @Nullable Cursor mCursor;
Ben Kwab8a5e082015-12-07 13:25:27 -080073 private int mCursorCount;
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +090074 private String mIds[] = new String[0];
Ben Kwab8a5e082015-12-07 13:25:27 -080075
Steve McKay98f8c5f2017-03-03 13:52:14 -080076 public Model(Features features) {
77 mFeatures = features;
78 }
Ben Kwa0497da82015-11-30 23:00:02 -080079
Steve McKay990f76e2016-09-16 12:36:58 -070080 public void addUpdateListener(EventListener<Update> listener) {
81 mUpdateListeners.add(listener);
82 }
83
84 public void removeUpdateListener(EventListener<Update> listener) {
85 mUpdateListeners.remove(listener);
86 }
87
Ben Kwad72a1da2015-12-01 19:56:57 -080088 private void notifyUpdateListeners() {
Steve McKay990f76e2016-09-16 12:36:58 -070089 for (EventListener<Update> handler: mUpdateListeners) {
90 handler.accept(Update.UPDATE);
Ben Kwad72a1da2015-12-01 19:56:57 -080091 }
92 }
93
94 private void notifyUpdateListeners(Exception e) {
Steve McKay98f8c5f2017-03-03 13:52:14 -080095 Update error = new Update(e, mFeatures.isRemoteActionsEnabled());
Steve McKay990f76e2016-09-16 12:36:58 -070096 for (EventListener<Update> handler: mUpdateListeners) {
97 handler.accept(error);
Ben Kwad72a1da2015-12-01 19:56:57 -080098 }
99 }
100
Jon Mann30d8c792017-02-21 17:44:49 -0800101 void reset() {
Steve McKay9de0da62016-08-25 15:18:23 -0700102 mCursor = null;
103 mCursorCount = 0;
104 mIds = new String[0];
105 mPositions.clear();
106 info = null;
107 error = null;
108 doc = null;
109 mIsLoading = false;
Jon Mann38f14392017-01-25 13:51:55 -0800110 mFileNames.clear();
Steve McKay9de0da62016-08-25 15:18:23 -0700111 notifyUpdateListeners();
112 }
113
114 void update(DirectoryResult result) {
115 assert(result != null);
116
117 if (DEBUG) Log.i(TAG, "Updating model with new result set.");
Ben Kwa0497da82015-11-30 23:00:02 -0800118
119 if (result.exception != null) {
120 Log.e(TAG, "Error while loading directory contents", result.exception);
Ben Kwad72a1da2015-12-01 19:56:57 -0800121 notifyUpdateListeners(result.exception);
Ben Kwa0497da82015-11-30 23:00:02 -0800122 return;
123 }
124
125 mCursor = result.cursor;
126 mCursorCount = mCursor.getCount();
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +0900127 doc = result.doc;
Ben Kwa0497da82015-11-30 23:00:02 -0800128
Ben Kwab8a5e082015-12-07 13:25:27 -0800129 updateModelData();
Ben Kwa0497da82015-11-30 23:00:02 -0800130
131 final Bundle extras = mCursor.getExtras();
132 if (extras != null) {
133 info = extras.getString(DocumentsContract.EXTRA_INFO);
134 error = extras.getString(DocumentsContract.EXTRA_ERROR);
135 mIsLoading = extras.getBoolean(DocumentsContract.EXTRA_LOADING, false);
136 }
137
Ben Kwad72a1da2015-12-01 19:56:57 -0800138 notifyUpdateListeners();
Ben Kwa0497da82015-11-30 23:00:02 -0800139 }
140
Ben Kwad72a1da2015-12-01 19:56:57 -0800141 @VisibleForTesting
Ben Kwa0497da82015-11-30 23:00:02 -0800142 int getItemCount() {
Ben Kwada858bf2015-12-09 14:33:49 -0800143 return mCursorCount;
Ben Kwa0497da82015-11-30 23:00:02 -0800144 }
145
146 /**
Ben Kwab8a5e082015-12-07 13:25:27 -0800147 * Scan over the incoming cursor data, generate Model IDs for each row, and sort the IDs
148 * according to the current sort order.
Ben Kwa0497da82015-11-30 23:00:02 -0800149 */
Ben Kwab8a5e082015-12-07 13:25:27 -0800150 private void updateModelData() {
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900151 mIds = new String[mCursorCount];
Jon Mann38f14392017-01-25 13:51:55 -0800152 mFileNames.clear();
Ben Kwa0497da82015-11-30 23:00:02 -0800153 mCursor.moveToPosition(-1);
154 for (int pos = 0; pos < mCursorCount; ++pos) {
Ben Lin2df30e52016-04-22 16:12:50 -0700155 if (!mCursor.moveToNext()) {
156 Log.e(TAG, "Fail to move cursor to next pos: " + pos);
157 return;
158 }
Tomasz Mikolajewski985df3d2016-03-15 15:38:54 +0900159 // Generates a Model ID for a cursor entry that refers to a document. The Model ID is a
160 // unique string that can be used to identify the document referred to by the cursor.
Tomasz Mikolajewski20c04c52016-03-15 15:55:46 +0900161 // If the cursor is a merged cursor over multiple authorities, then prefix the ids
162 // with the authority to avoid collisions.
163 if (mCursor instanceof MergeCursor) {
Garfield Tan2010ff72016-09-30 14:55:32 -0700164 mIds[pos] = getCursorString(mCursor, RootCursorWrapper.COLUMN_AUTHORITY)
165 + "|" + getCursorString(mCursor, Document.COLUMN_DOCUMENT_ID);
Tomasz Mikolajewski20c04c52016-03-15 15:55:46 +0900166 } else {
Tomasz Mikolajewski06b036f2016-04-26 11:11:17 +0900167 mIds[pos] = getCursorString(mCursor, Document.COLUMN_DOCUMENT_ID);
Tomasz Mikolajewski20c04c52016-03-15 15:55:46 +0900168 }
Jon Mann38f14392017-01-25 13:51:55 -0800169 mFileNames.add(getCursorString(mCursor, Document.COLUMN_DISPLAY_NAME));
Ben Kwab8a5e082015-12-07 13:25:27 -0800170 }
171
172 // Populate the positions.
173 mPositions.clear();
174 for (int i = 0; i < mCursorCount; ++i) {
Garfield Tan2010ff72016-09-30 14:55:32 -0700175 mPositions.put(mIds[i], i);
Ben Kwab8a5e082015-12-07 13:25:27 -0800176 }
177 }
178
Jon Mann38f14392017-01-25 13:51:55 -0800179 public boolean hasFileWithName(String name) {
180 return mFileNames.contains(name);
181 }
182
Tomasz Mikolajewskid71bd612016-02-16 12:28:43 +0900183 public @Nullable Cursor getItem(String modelId) {
Ben Kwa0497da82015-11-30 23:00:02 -0800184 Integer pos = mPositions.get(modelId);
Steve McKay5a22a112016-04-12 11:29:10 -0700185 if (pos == null) {
186 if (DEBUG) Log.d(TAG, "Unabled to find cursor position for modelId: " + modelId);
187 return null;
Ben Kwa0497da82015-11-30 23:00:02 -0800188 }
Steve McKay5a22a112016-04-12 11:29:10 -0700189
190 if (!mCursor.moveToPosition(pos)) {
191 if (DEBUG) Log.d(TAG,
192 "Unabled to move cursor to position " + pos + " for modelId: " + modelId);
193 return null;
194 }
195
196 return mCursor;
Ben Kwa0497da82015-11-30 23:00:02 -0800197 }
198
Steve McKay990f76e2016-09-16 12:36:58 -0700199 public boolean isEmpty() {
Ben Kwa0497da82015-11-30 23:00:02 -0800200 return mCursorCount == 0;
201 }
202
203 boolean isLoading() {
204 return mIsLoading;
205 }
206
Steve McKayc8889af2016-09-23 11:22:41 -0700207 public List<DocumentInfo> getDocuments(Selection selection) {
Steve McKay98f8c5f2017-03-03 13:52:14 -0800208 return loadDocuments(selection, DocumentFilters.ANY);
Ben Kwa0497da82015-11-30 23:00:02 -0800209 }
210
Steve McKay990f76e2016-09-16 12:36:58 -0700211 public @Nullable DocumentInfo getDocument(String modelId) {
212 final Cursor cursor = getItem(modelId);
213 return (cursor == null)
214 ? null
215 : DocumentInfo.fromDirectoryCursor(cursor);
216 }
217
Steve McKayd0718952016-10-10 13:43:36 -0700218 public List<DocumentInfo> loadDocuments(Selection selection, Predicate<Cursor> filter) {
219 final int size = (selection != null) ? selection.size() : 0;
220
221 final List<DocumentInfo> docs = new ArrayList<>(size);
Tomasz Mikolajewski1abcfd12016-12-12 10:08:49 +0900222 DocumentInfo doc;
Steve McKayd0718952016-10-10 13:43:36 -0700223 for (String modelId: selection) {
Tomasz Mikolajewski1abcfd12016-12-12 10:08:49 +0900224 doc = loadDocument(modelId, filter);
225 if (doc != null) {
226 docs.add(doc);
227 }
Steve McKayd0718952016-10-10 13:43:36 -0700228 }
229 return docs;
230 }
231
Tomasz Mikolajewski1abcfd12016-12-12 10:08:49 +0900232 public boolean hasDocuments(Selection selection, Predicate<Cursor> filter) {
233 for (String modelId: selection) {
234 if (loadDocument(modelId, filter) != null) {
235 return true;
236 }
237 }
238 return false;
239 }
240
Steve McKayd0718952016-10-10 13:43:36 -0700241 /**
Steve McKayd0718952016-10-10 13:43:36 -0700242 * @return DocumentInfo, or null. If filter returns false, null will be returned.
243 */
Tomasz Mikolajewski1abcfd12016-12-12 10:08:49 +0900244 private @Nullable DocumentInfo loadDocument(String modelId, Predicate<Cursor> filter) {
Steve McKayd0718952016-10-10 13:43:36 -0700245 final Cursor cursor = getItem(modelId);
246
247 if (cursor == null) {
248 Log.w(TAG, "Unable to obtain document for modelId: " + modelId);
Tomasz Mikolajewski1abcfd12016-12-12 10:08:49 +0900249 return null;
Steve McKayd0718952016-10-10 13:43:36 -0700250 }
251
252 if (filter.test(cursor)) {
Tomasz Mikolajewski1abcfd12016-12-12 10:08:49 +0900253 return DocumentInfo.fromDirectoryCursor(cursor);
Steve McKayd0718952016-10-10 13:43:36 -0700254 }
Tomasz Mikolajewski1abcfd12016-12-12 10:08:49 +0900255
256 if (VERBOSE) Log.v(TAG, "Filtered out document from results: " + modelId);
257 return null;
Steve McKayd0718952016-10-10 13:43:36 -0700258 }
259
Steve McKay84769b82016-06-09 10:46:07 -0700260 public Uri getItemUri(String modelId) {
261 final Cursor cursor = getItem(modelId);
262 return DocumentInfo.getUri(cursor);
263 }
264
Garfield, Tan11d23482016-08-05 09:33:29 -0700265 /**
266 * @return An ordered array of model IDs representing the documents in the model. It is sorted
267 * according to the current sort order, which was set by the last model update.
268 */
269 public String[] getModelIds() {
270 return mIds;
271 }
272
Steve McKay990f76e2016-09-16 12:36:58 -0700273 public static class Update {
Ben Kwa0497da82015-11-30 23:00:02 -0800274
Steve McKay990f76e2016-09-16 12:36:58 -0700275 public static final Update UPDATE = new Update();
Ben Kwa472103f2016-02-10 15:48:25 -0800276
Steve McKay990f76e2016-09-16 12:36:58 -0700277 @IntDef(value = {
278 TYPE_UPDATE,
Ben Linf8f06e92017-01-27 17:15:48 -0800279 TYPE_UPDATE_EXCEPTION
Steve McKay990f76e2016-09-16 12:36:58 -0700280 })
281 @Retention(RetentionPolicy.SOURCE)
282 public @interface UpdateType {}
283 public static final int TYPE_UPDATE = 0;
Ben Linf8f06e92017-01-27 17:15:48 -0800284 public static final int TYPE_UPDATE_EXCEPTION = 1;
Ben Kwa0497da82015-11-30 23:00:02 -0800285
Ben Linf8f06e92017-01-27 17:15:48 -0800286 private final @UpdateType int mUpdateType;
Steve McKay990f76e2016-09-16 12:36:58 -0700287 private final @Nullable Exception mException;
Steve McKay98f8c5f2017-03-03 13:52:14 -0800288 private final boolean mRemoteActionEnabled;
Steve McKay990f76e2016-09-16 12:36:58 -0700289
290 private Update() {
Ben Linf8f06e92017-01-27 17:15:48 -0800291 mUpdateType = TYPE_UPDATE;
Steve McKay990f76e2016-09-16 12:36:58 -0700292 mException = null;
Steve McKay98f8c5f2017-03-03 13:52:14 -0800293 mRemoteActionEnabled = false;
Steve McKay990f76e2016-09-16 12:36:58 -0700294 }
295
Steve McKay98f8c5f2017-03-03 13:52:14 -0800296 public Update(Exception exception, boolean remoteActionsEnabled) {
Steve McKay990f76e2016-09-16 12:36:58 -0700297 assert(exception != null);
Ben Linf8f06e92017-01-27 17:15:48 -0800298 mUpdateType = TYPE_UPDATE_EXCEPTION;
Steve McKay990f76e2016-09-16 12:36:58 -0700299 mException = exception;
Steve McKay98f8c5f2017-03-03 13:52:14 -0800300 mRemoteActionEnabled = remoteActionsEnabled;
Steve McKay990f76e2016-09-16 12:36:58 -0700301 }
302
303 public boolean isUpdate() {
Ben Linf8f06e92017-01-27 17:15:48 -0800304 return mUpdateType == TYPE_UPDATE;
Steve McKay990f76e2016-09-16 12:36:58 -0700305 }
306
Ben Linf8f06e92017-01-27 17:15:48 -0800307 public boolean hasException() {
308 return mUpdateType == TYPE_UPDATE_EXCEPTION;
Steve McKay990f76e2016-09-16 12:36:58 -0700309 }
310
Ben Linf8f06e92017-01-27 17:15:48 -0800311 public boolean hasRecoverableException() {
Steve McKay98f8c5f2017-03-03 13:52:14 -0800312 return mRemoteActionEnabled
313 && hasException()
Ben Lin38b41122017-02-03 17:52:43 -0800314 && mException instanceof RecoverableSecurityException;
Ben Linf8f06e92017-01-27 17:15:48 -0800315 }
316
317 public @Nullable Exception getException() {
Steve McKay990f76e2016-09-16 12:36:58 -0700318 return mException;
319 }
Ben Kwa0497da82015-11-30 23:00:02 -0800320 }
Ben Kwa0497da82015-11-30 23:00:02 -0800321}