blob: 199b06088f17ed0afb24ffedcee6f8664cb6143f [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
Tomasz Mikolajewskie535c572016-11-25 13:58:35 +090019import static com.android.documentsui.base.DocumentInfo.getCursorInt;
Steve McKayd0805062016-09-15 14:30:38 -070020import static com.android.documentsui.base.DocumentInfo.getCursorString;
Steve McKayd9caa6a2016-09-15 16:36:45 -070021import static com.android.documentsui.base.Shared.DEBUG;
Tomasz Mikolajewskifd2b4dc2016-12-15 16:39:45 +090022import static com.android.documentsui.base.Shared.ENABLE_OMC_API_FEATURES;
Ben Linf8f06e92017-01-27 17:15:48 -080023import static com.android.documentsui.base.Shared.VERBOSE;
Ben Kwa0497da82015-11-30 23:00:02 -080024
Steve McKay990f76e2016-09-16 12:36:58 -070025import android.annotation.IntDef;
Ben Linf8f06e92017-01-27 17:15:48 -080026import android.app.RecoverableSecurityException;
Ben Kwa0497da82015-11-30 23:00:02 -080027import android.database.Cursor;
Tomasz Mikolajewski20c04c52016-03-15 15:55:46 +090028import android.database.MergeCursor;
Steve McKay84769b82016-06-09 10:46:07 -070029import android.net.Uri;
Ben Kwa0497da82015-11-30 23:00:02 -080030import android.os.Bundle;
Ben Kwa0497da82015-11-30 23:00:02 -080031import android.provider.DocumentsContract;
32import android.provider.DocumentsContract.Document;
33import android.support.annotation.Nullable;
34import android.support.annotation.VisibleForTesting;
Ben Kwa0497da82015-11-30 23:00:02 -080035import android.util.Log;
Ben Kwa0497da82015-11-30 23:00:02 -080036
Ben Kwa0497da82015-11-30 23:00:02 -080037import com.android.documentsui.DirectoryResult;
Tomasz Mikolajewskie535c572016-11-25 13:58:35 +090038import com.android.documentsui.archives.ArchivesProvider;
Steve McKayd0805062016-09-15 14:30:38 -070039import com.android.documentsui.base.DocumentInfo;
Steve McKay990f76e2016-09-16 12:36:58 -070040import com.android.documentsui.base.EventListener;
Ben Lin38b41122017-02-03 17:52:43 -080041import com.android.documentsui.base.Shared;
Steve McKayd9caa6a2016-09-15 16:36:45 -070042import com.android.documentsui.roots.RootCursorWrapper;
Steve McKay4f78ba62016-10-04 16:48:49 -070043import com.android.documentsui.selection.Selection;
Ben Kwa0497da82015-11-30 23:00:02 -080044
Steve McKay990f76e2016-09-16 12:36:58 -070045import java.lang.annotation.Retention;
46import java.lang.annotation.RetentionPolicy;
Ben Kwa0497da82015-11-30 23:00:02 -080047import java.util.ArrayList;
Ben Kwa0497da82015-11-30 23:00:02 -080048import java.util.HashMap;
49import java.util.List;
Ben Kwab8a5e082015-12-07 13:25:27 -080050import java.util.Map;
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
59 /**
Tomasz Mikolajewskifd2b4dc2016-12-15 16:39:45 +090060 * Filter that passes (returns true) for all files which can be shared.
Steve McKayd0718952016-10-10 13:43:36 -070061 */
Tomasz Mikolajewski1abcfd12016-12-12 10:08:49 +090062 public static final Predicate<Cursor> SHARABLE_FILE_FILTER = (Cursor c) -> {
Tomasz Mikolajewskie535c572016-11-25 13:58:35 +090063 int flags = getCursorInt(c, Document.COLUMN_FLAGS);
64 String authority = getCursorString(c, RootCursorWrapper.COLUMN_AUTHORITY);
Tomasz Mikolajewskifd2b4dc2016-12-15 16:39:45 +090065 if (!ENABLE_OMC_API_FEATURES) {
66 return (flags & Document.FLAG_PARTIAL) == 0
67 && (flags & Document.FLAG_VIRTUAL_DOCUMENT) == 0
68 && !ArchivesProvider.AUTHORITY.equals(authority);
69 }
Tomasz Mikolajewski5b127ac2016-10-24 18:24:58 +090070 return (flags & Document.FLAG_PARTIAL) == 0
Tomasz Mikolajewskie535c572016-11-25 13:58:35 +090071 && !ArchivesProvider.AUTHORITY.equals(authority);
Steve McKayd0718952016-10-10 13:43:36 -070072 };
73
Tomasz Mikolajewski5b127ac2016-10-24 18:24:58 +090074 /**
75 * Filter that passes (returns true) only virtual documents.
76 */
77 public static final Predicate<Cursor> VIRTUAL_DOCUMENT_FILTER = (Cursor c) -> {
78 int flags = getCursorInt(c, Document.COLUMN_FLAGS);
79 return (flags & Document.FLAG_VIRTUAL_DOCUMENT) != 0;
80 };
81
Steve McKayd79cc4a2016-10-11 16:34:40 -070082 private static final Predicate<Cursor> ANY_FILE_FILTER = (Cursor c) -> true;
83
Ben Kwa0497da82015-11-30 23:00:02 -080084 private static final String TAG = "Model";
Ben Kwab8a5e082015-12-07 13:25:27 -080085
Ben Kwa0497da82015-11-30 23:00:02 -080086 private boolean mIsLoading;
Steve McKay990f76e2016-09-16 12:36:58 -070087 private List<EventListener<Update>> mUpdateListeners = new ArrayList<>();
Ben Kwa0497da82015-11-30 23:00:02 -080088 @Nullable private Cursor mCursor;
Ben Kwab8a5e082015-12-07 13:25:27 -080089 private int mCursorCount;
90 /** Maps Model ID to cursor positions, for looking up items by Model ID. */
91 private Map<String, Integer> mPositions = new HashMap<>();
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +090092 private String mIds[] = new String[0];
Ben Kwab8a5e082015-12-07 13:25:27 -080093
Ben Kwa0497da82015-11-30 23:00:02 -080094 @Nullable String info;
95 @Nullable String error;
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +090096 @Nullable DocumentInfo doc;
Ben Kwa0497da82015-11-30 23:00:02 -080097
Steve McKay990f76e2016-09-16 12:36:58 -070098 public void addUpdateListener(EventListener<Update> listener) {
99 mUpdateListeners.add(listener);
100 }
101
102 public void removeUpdateListener(EventListener<Update> listener) {
103 mUpdateListeners.remove(listener);
104 }
105
Ben Kwad72a1da2015-12-01 19:56:57 -0800106 private void notifyUpdateListeners() {
Steve McKay990f76e2016-09-16 12:36:58 -0700107 for (EventListener<Update> handler: mUpdateListeners) {
108 handler.accept(Update.UPDATE);
Ben Kwad72a1da2015-12-01 19:56:57 -0800109 }
110 }
111
112 private void notifyUpdateListeners(Exception e) {
Steve McKay990f76e2016-09-16 12:36:58 -0700113 Update error = new Update(e);
114 for (EventListener<Update> handler: mUpdateListeners) {
115 handler.accept(error);
Ben Kwad72a1da2015-12-01 19:56:57 -0800116 }
117 }
118
Steve McKay9de0da62016-08-25 15:18:23 -0700119 void onLoaderReset() {
120 if (mIsLoading) {
Steve McKay7c662092016-08-26 12:17:41 -0700121 Log.w(TAG, "Received unexpected loader reset while in loading state for doc: "
122 + DocumentInfo.debugString(doc));
Ben Kwa0497da82015-11-30 23:00:02 -0800123 }
Steve McKay7c662092016-08-26 12:17:41 -0700124
125 reset();
Steve McKay9de0da62016-08-25 15:18:23 -0700126 }
127
128 private void reset() {
129 mCursor = null;
130 mCursorCount = 0;
131 mIds = new String[0];
132 mPositions.clear();
133 info = null;
134 error = null;
135 doc = null;
136 mIsLoading = false;
137 notifyUpdateListeners();
138 }
139
140 void update(DirectoryResult result) {
141 assert(result != null);
142
143 if (DEBUG) Log.i(TAG, "Updating model with new result set.");
Ben Kwa0497da82015-11-30 23:00:02 -0800144
145 if (result.exception != null) {
146 Log.e(TAG, "Error while loading directory contents", result.exception);
Ben Kwad72a1da2015-12-01 19:56:57 -0800147 notifyUpdateListeners(result.exception);
Ben Kwa0497da82015-11-30 23:00:02 -0800148 return;
149 }
150
151 mCursor = result.cursor;
152 mCursorCount = mCursor.getCount();
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +0900153 doc = result.doc;
Ben Kwa0497da82015-11-30 23:00:02 -0800154
Ben Kwab8a5e082015-12-07 13:25:27 -0800155 updateModelData();
Ben Kwa0497da82015-11-30 23:00:02 -0800156
157 final Bundle extras = mCursor.getExtras();
158 if (extras != null) {
159 info = extras.getString(DocumentsContract.EXTRA_INFO);
160 error = extras.getString(DocumentsContract.EXTRA_ERROR);
161 mIsLoading = extras.getBoolean(DocumentsContract.EXTRA_LOADING, false);
162 }
163
Ben Kwad72a1da2015-12-01 19:56:57 -0800164 notifyUpdateListeners();
Ben Kwa0497da82015-11-30 23:00:02 -0800165 }
166
Ben Kwad72a1da2015-12-01 19:56:57 -0800167 @VisibleForTesting
Ben Kwa0497da82015-11-30 23:00:02 -0800168 int getItemCount() {
Ben Kwada858bf2015-12-09 14:33:49 -0800169 return mCursorCount;
Ben Kwa0497da82015-11-30 23:00:02 -0800170 }
171
172 /**
Ben Kwab8a5e082015-12-07 13:25:27 -0800173 * Scan over the incoming cursor data, generate Model IDs for each row, and sort the IDs
174 * according to the current sort order.
Ben Kwa0497da82015-11-30 23:00:02 -0800175 */
Ben Kwab8a5e082015-12-07 13:25:27 -0800176 private void updateModelData() {
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900177 mIds = new String[mCursorCount];
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900178
Ben Kwa0497da82015-11-30 23:00:02 -0800179 mCursor.moveToPosition(-1);
180 for (int pos = 0; pos < mCursorCount; ++pos) {
Ben Lin2df30e52016-04-22 16:12:50 -0700181 if (!mCursor.moveToNext()) {
182 Log.e(TAG, "Fail to move cursor to next pos: " + pos);
183 return;
184 }
Tomasz Mikolajewski985df3d2016-03-15 15:38:54 +0900185 // Generates a Model ID for a cursor entry that refers to a document. The Model ID is a
186 // unique string that can be used to identify the document referred to by the cursor.
Tomasz Mikolajewski20c04c52016-03-15 15:55:46 +0900187 // If the cursor is a merged cursor over multiple authorities, then prefix the ids
188 // with the authority to avoid collisions.
189 if (mCursor instanceof MergeCursor) {
Garfield Tan2010ff72016-09-30 14:55:32 -0700190 mIds[pos] = getCursorString(mCursor, RootCursorWrapper.COLUMN_AUTHORITY)
191 + "|" + getCursorString(mCursor, Document.COLUMN_DOCUMENT_ID);
Tomasz Mikolajewski20c04c52016-03-15 15:55:46 +0900192 } else {
Tomasz Mikolajewski06b036f2016-04-26 11:11:17 +0900193 mIds[pos] = getCursorString(mCursor, Document.COLUMN_DOCUMENT_ID);
Tomasz Mikolajewski20c04c52016-03-15 15:55:46 +0900194 }
Ben Kwab8a5e082015-12-07 13:25:27 -0800195 }
196
197 // Populate the positions.
198 mPositions.clear();
199 for (int i = 0; i < mCursorCount; ++i) {
Garfield Tan2010ff72016-09-30 14:55:32 -0700200 mPositions.put(mIds[i], i);
Ben Kwab8a5e082015-12-07 13:25:27 -0800201 }
202 }
203
Tomasz Mikolajewskid71bd612016-02-16 12:28:43 +0900204 public @Nullable Cursor getItem(String modelId) {
Ben Kwa0497da82015-11-30 23:00:02 -0800205 Integer pos = mPositions.get(modelId);
Steve McKay5a22a112016-04-12 11:29:10 -0700206 if (pos == null) {
207 if (DEBUG) Log.d(TAG, "Unabled to find cursor position for modelId: " + modelId);
208 return null;
Ben Kwa0497da82015-11-30 23:00:02 -0800209 }
Steve McKay5a22a112016-04-12 11:29:10 -0700210
211 if (!mCursor.moveToPosition(pos)) {
212 if (DEBUG) Log.d(TAG,
213 "Unabled to move cursor to position " + pos + " for modelId: " + modelId);
214 return null;
215 }
216
217 return mCursor;
Ben Kwa0497da82015-11-30 23:00:02 -0800218 }
219
Steve McKay990f76e2016-09-16 12:36:58 -0700220 public boolean isEmpty() {
Ben Kwa0497da82015-11-30 23:00:02 -0800221 return mCursorCount == 0;
222 }
223
224 boolean isLoading() {
225 return mIsLoading;
226 }
227
Steve McKayc8889af2016-09-23 11:22:41 -0700228 public List<DocumentInfo> getDocuments(Selection selection) {
Steve McKayd79cc4a2016-10-11 16:34:40 -0700229 return loadDocuments(selection, ANY_FILE_FILTER);
Ben Kwa0497da82015-11-30 23:00:02 -0800230 }
231
Steve McKay990f76e2016-09-16 12:36:58 -0700232 public @Nullable DocumentInfo getDocument(String modelId) {
233 final Cursor cursor = getItem(modelId);
234 return (cursor == null)
235 ? null
236 : DocumentInfo.fromDirectoryCursor(cursor);
237 }
238
Steve McKayd0718952016-10-10 13:43:36 -0700239 public List<DocumentInfo> loadDocuments(Selection selection, Predicate<Cursor> filter) {
240 final int size = (selection != null) ? selection.size() : 0;
241
242 final List<DocumentInfo> docs = new ArrayList<>(size);
Tomasz Mikolajewski1abcfd12016-12-12 10:08:49 +0900243 DocumentInfo doc;
Steve McKayd0718952016-10-10 13:43:36 -0700244 for (String modelId: selection) {
Tomasz Mikolajewski1abcfd12016-12-12 10:08:49 +0900245 doc = loadDocument(modelId, filter);
246 if (doc != null) {
247 docs.add(doc);
248 }
Steve McKayd0718952016-10-10 13:43:36 -0700249 }
250 return docs;
251 }
252
Tomasz Mikolajewski1abcfd12016-12-12 10:08:49 +0900253 public boolean hasDocuments(Selection selection, Predicate<Cursor> filter) {
254 for (String modelId: selection) {
255 if (loadDocument(modelId, filter) != null) {
256 return true;
257 }
258 }
259 return false;
260 }
261
Steve McKayd0718952016-10-10 13:43:36 -0700262 /**
Steve McKayd0718952016-10-10 13:43:36 -0700263 * @return DocumentInfo, or null. If filter returns false, null will be returned.
264 */
Tomasz Mikolajewski1abcfd12016-12-12 10:08:49 +0900265 private @Nullable DocumentInfo loadDocument(String modelId, Predicate<Cursor> filter) {
Steve McKayd0718952016-10-10 13:43:36 -0700266 final Cursor cursor = getItem(modelId);
267
268 if (cursor == null) {
269 Log.w(TAG, "Unable to obtain document for modelId: " + modelId);
Tomasz Mikolajewski1abcfd12016-12-12 10:08:49 +0900270 return null;
Steve McKayd0718952016-10-10 13:43:36 -0700271 }
272
273 if (filter.test(cursor)) {
Tomasz Mikolajewski1abcfd12016-12-12 10:08:49 +0900274 return DocumentInfo.fromDirectoryCursor(cursor);
Steve McKayd0718952016-10-10 13:43:36 -0700275 }
Tomasz Mikolajewski1abcfd12016-12-12 10:08:49 +0900276
277 if (VERBOSE) Log.v(TAG, "Filtered out document from results: " + modelId);
278 return null;
Steve McKayd0718952016-10-10 13:43:36 -0700279 }
280
Steve McKay84769b82016-06-09 10:46:07 -0700281 public Uri getItemUri(String modelId) {
282 final Cursor cursor = getItem(modelId);
283 return DocumentInfo.getUri(cursor);
284 }
285
Garfield, Tan11d23482016-08-05 09:33:29 -0700286 /**
287 * @return An ordered array of model IDs representing the documents in the model. It is sorted
288 * according to the current sort order, which was set by the last model update.
289 */
290 public String[] getModelIds() {
291 return mIds;
292 }
293
Steve McKay990f76e2016-09-16 12:36:58 -0700294 public static class Update {
Ben Kwa0497da82015-11-30 23:00:02 -0800295
Steve McKay990f76e2016-09-16 12:36:58 -0700296 public static final Update UPDATE = new Update();
Ben Kwa472103f2016-02-10 15:48:25 -0800297
Steve McKay990f76e2016-09-16 12:36:58 -0700298 @IntDef(value = {
299 TYPE_UPDATE,
Ben Linf8f06e92017-01-27 17:15:48 -0800300 TYPE_UPDATE_EXCEPTION
Steve McKay990f76e2016-09-16 12:36:58 -0700301 })
302 @Retention(RetentionPolicy.SOURCE)
303 public @interface UpdateType {}
304 public static final int TYPE_UPDATE = 0;
Ben Linf8f06e92017-01-27 17:15:48 -0800305 public static final int TYPE_UPDATE_EXCEPTION = 1;
Ben Kwa0497da82015-11-30 23:00:02 -0800306
Ben Linf8f06e92017-01-27 17:15:48 -0800307 private final @UpdateType int mUpdateType;
Steve McKay990f76e2016-09-16 12:36:58 -0700308 private final @Nullable Exception mException;
309
310 private Update() {
Ben Linf8f06e92017-01-27 17:15:48 -0800311 mUpdateType = TYPE_UPDATE;
Steve McKay990f76e2016-09-16 12:36:58 -0700312 mException = null;
313 }
314
315 public Update(Exception exception) {
316 assert(exception != null);
Ben Linf8f06e92017-01-27 17:15:48 -0800317 mUpdateType = TYPE_UPDATE_EXCEPTION;
Steve McKay990f76e2016-09-16 12:36:58 -0700318 mException = exception;
319 }
320
321 public boolean isUpdate() {
Ben Linf8f06e92017-01-27 17:15:48 -0800322 return mUpdateType == TYPE_UPDATE;
Steve McKay990f76e2016-09-16 12:36:58 -0700323 }
324
Ben Linf8f06e92017-01-27 17:15:48 -0800325 public boolean hasException() {
326 return mUpdateType == TYPE_UPDATE_EXCEPTION;
Steve McKay990f76e2016-09-16 12:36:58 -0700327 }
328
Ben Linf8f06e92017-01-27 17:15:48 -0800329 public boolean hasRecoverableException() {
Ben Lin38b41122017-02-03 17:52:43 -0800330 return Shared.ENABLE_OMC_API_FEATURES && hasException()
331 && mException instanceof RecoverableSecurityException;
Ben Linf8f06e92017-01-27 17:15:48 -0800332 }
333
334 public @Nullable Exception getException() {
Steve McKay990f76e2016-09-16 12:36:58 -0700335 return mException;
336 }
Ben Kwa0497da82015-11-30 23:00:02 -0800337 }
Ben Kwa0497da82015-11-30 23:00:02 -0800338}