blob: 7a760982bfad7c36f0aed34a3ee9b9b76bde77e3 [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;
Steve McKayd9caa6a2016-09-15 16:36:45 -070041import com.android.documentsui.roots.RootCursorWrapper;
Steve McKay4f78ba62016-10-04 16:48:49 -070042import com.android.documentsui.selection.Selection;
Ben Kwa0497da82015-11-30 23:00:02 -080043
Steve McKay990f76e2016-09-16 12:36:58 -070044import java.lang.annotation.Retention;
45import java.lang.annotation.RetentionPolicy;
Ben Kwa0497da82015-11-30 23:00:02 -080046import java.util.ArrayList;
Ben Kwa0497da82015-11-30 23:00:02 -080047import java.util.HashMap;
48import java.util.List;
Ben Kwab8a5e082015-12-07 13:25:27 -080049import java.util.Map;
Steve McKayd0718952016-10-10 13:43:36 -070050import java.util.function.Predicate;
Ben Kwa0497da82015-11-30 23:00:02 -080051
52/**
53 * The data model for the current loaded directory.
54 */
55@VisibleForTesting
Tomasz Mikolajewskid71bd612016-02-16 12:28:43 +090056public class Model {
Steve McKayd0718952016-10-10 13:43:36 -070057
58 /**
Tomasz Mikolajewskifd2b4dc2016-12-15 16:39:45 +090059 * Filter that passes (returns true) for all files which can be shared.
Steve McKayd0718952016-10-10 13:43:36 -070060 */
Tomasz Mikolajewski1abcfd12016-12-12 10:08:49 +090061 public static final Predicate<Cursor> SHARABLE_FILE_FILTER = (Cursor c) -> {
Tomasz Mikolajewskie535c572016-11-25 13:58:35 +090062 int flags = getCursorInt(c, Document.COLUMN_FLAGS);
63 String authority = getCursorString(c, RootCursorWrapper.COLUMN_AUTHORITY);
Tomasz Mikolajewskifd2b4dc2016-12-15 16:39:45 +090064 if (!ENABLE_OMC_API_FEATURES) {
65 return (flags & Document.FLAG_PARTIAL) == 0
66 && (flags & Document.FLAG_VIRTUAL_DOCUMENT) == 0
67 && !ArchivesProvider.AUTHORITY.equals(authority);
68 }
Tomasz Mikolajewski5b127ac2016-10-24 18:24:58 +090069 return (flags & Document.FLAG_PARTIAL) == 0
Tomasz Mikolajewskie535c572016-11-25 13:58:35 +090070 && !ArchivesProvider.AUTHORITY.equals(authority);
Steve McKayd0718952016-10-10 13:43:36 -070071 };
72
Tomasz Mikolajewski5b127ac2016-10-24 18:24:58 +090073 /**
74 * Filter that passes (returns true) only virtual documents.
75 */
76 public static final Predicate<Cursor> VIRTUAL_DOCUMENT_FILTER = (Cursor c) -> {
77 int flags = getCursorInt(c, Document.COLUMN_FLAGS);
78 return (flags & Document.FLAG_VIRTUAL_DOCUMENT) != 0;
79 };
80
Steve McKayd79cc4a2016-10-11 16:34:40 -070081 private static final Predicate<Cursor> ANY_FILE_FILTER = (Cursor c) -> true;
82
Ben Kwa0497da82015-11-30 23:00:02 -080083 private static final String TAG = "Model";
Ben Kwab8a5e082015-12-07 13:25:27 -080084
Ben Kwa0497da82015-11-30 23:00:02 -080085 private boolean mIsLoading;
Steve McKay990f76e2016-09-16 12:36:58 -070086 private List<EventListener<Update>> mUpdateListeners = new ArrayList<>();
Ben Kwa0497da82015-11-30 23:00:02 -080087 @Nullable private Cursor mCursor;
Ben Kwab8a5e082015-12-07 13:25:27 -080088 private int mCursorCount;
89 /** Maps Model ID to cursor positions, for looking up items by Model ID. */
90 private Map<String, Integer> mPositions = new HashMap<>();
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +090091 private String mIds[] = new String[0];
Ben Kwab8a5e082015-12-07 13:25:27 -080092
Ben Kwa0497da82015-11-30 23:00:02 -080093 @Nullable String info;
94 @Nullable String error;
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +090095 @Nullable DocumentInfo doc;
Ben Kwa0497da82015-11-30 23:00:02 -080096
Steve McKay990f76e2016-09-16 12:36:58 -070097 public void addUpdateListener(EventListener<Update> listener) {
98 mUpdateListeners.add(listener);
99 }
100
101 public void removeUpdateListener(EventListener<Update> listener) {
102 mUpdateListeners.remove(listener);
103 }
104
Ben Kwad72a1da2015-12-01 19:56:57 -0800105 private void notifyUpdateListeners() {
Steve McKay990f76e2016-09-16 12:36:58 -0700106 for (EventListener<Update> handler: mUpdateListeners) {
107 handler.accept(Update.UPDATE);
Ben Kwad72a1da2015-12-01 19:56:57 -0800108 }
109 }
110
111 private void notifyUpdateListeners(Exception e) {
Steve McKay990f76e2016-09-16 12:36:58 -0700112 Update error = new Update(e);
113 for (EventListener<Update> handler: mUpdateListeners) {
114 handler.accept(error);
Ben Kwad72a1da2015-12-01 19:56:57 -0800115 }
116 }
117
Steve McKay9de0da62016-08-25 15:18:23 -0700118 void onLoaderReset() {
119 if (mIsLoading) {
Steve McKay7c662092016-08-26 12:17:41 -0700120 Log.w(TAG, "Received unexpected loader reset while in loading state for doc: "
121 + DocumentInfo.debugString(doc));
Ben Kwa0497da82015-11-30 23:00:02 -0800122 }
Steve McKay7c662092016-08-26 12:17:41 -0700123
124 reset();
Steve McKay9de0da62016-08-25 15:18:23 -0700125 }
126
127 private void reset() {
128 mCursor = null;
129 mCursorCount = 0;
130 mIds = new String[0];
131 mPositions.clear();
132 info = null;
133 error = null;
134 doc = null;
135 mIsLoading = false;
136 notifyUpdateListeners();
137 }
138
139 void update(DirectoryResult result) {
140 assert(result != null);
141
142 if (DEBUG) Log.i(TAG, "Updating model with new result set.");
Ben Kwa0497da82015-11-30 23:00:02 -0800143
144 if (result.exception != null) {
145 Log.e(TAG, "Error while loading directory contents", result.exception);
Ben Kwad72a1da2015-12-01 19:56:57 -0800146 notifyUpdateListeners(result.exception);
Ben Kwa0497da82015-11-30 23:00:02 -0800147 return;
148 }
149
150 mCursor = result.cursor;
151 mCursorCount = mCursor.getCount();
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +0900152 doc = result.doc;
Ben Kwa0497da82015-11-30 23:00:02 -0800153
Ben Kwab8a5e082015-12-07 13:25:27 -0800154 updateModelData();
Ben Kwa0497da82015-11-30 23:00:02 -0800155
156 final Bundle extras = mCursor.getExtras();
157 if (extras != null) {
158 info = extras.getString(DocumentsContract.EXTRA_INFO);
159 error = extras.getString(DocumentsContract.EXTRA_ERROR);
160 mIsLoading = extras.getBoolean(DocumentsContract.EXTRA_LOADING, false);
161 }
162
Ben Kwad72a1da2015-12-01 19:56:57 -0800163 notifyUpdateListeners();
Ben Kwa0497da82015-11-30 23:00:02 -0800164 }
165
Ben Kwad72a1da2015-12-01 19:56:57 -0800166 @VisibleForTesting
Ben Kwa0497da82015-11-30 23:00:02 -0800167 int getItemCount() {
Ben Kwada858bf2015-12-09 14:33:49 -0800168 return mCursorCount;
Ben Kwa0497da82015-11-30 23:00:02 -0800169 }
170
171 /**
Ben Kwab8a5e082015-12-07 13:25:27 -0800172 * Scan over the incoming cursor data, generate Model IDs for each row, and sort the IDs
173 * according to the current sort order.
Ben Kwa0497da82015-11-30 23:00:02 -0800174 */
Ben Kwab8a5e082015-12-07 13:25:27 -0800175 private void updateModelData() {
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900176 mIds = new String[mCursorCount];
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900177
Ben Kwa0497da82015-11-30 23:00:02 -0800178 mCursor.moveToPosition(-1);
179 for (int pos = 0; pos < mCursorCount; ++pos) {
Ben Lin2df30e52016-04-22 16:12:50 -0700180 if (!mCursor.moveToNext()) {
181 Log.e(TAG, "Fail to move cursor to next pos: " + pos);
182 return;
183 }
Tomasz Mikolajewski985df3d2016-03-15 15:38:54 +0900184 // Generates a Model ID for a cursor entry that refers to a document. The Model ID is a
185 // unique string that can be used to identify the document referred to by the cursor.
Tomasz Mikolajewski20c04c52016-03-15 15:55:46 +0900186 // If the cursor is a merged cursor over multiple authorities, then prefix the ids
187 // with the authority to avoid collisions.
188 if (mCursor instanceof MergeCursor) {
Garfield Tan2010ff72016-09-30 14:55:32 -0700189 mIds[pos] = getCursorString(mCursor, RootCursorWrapper.COLUMN_AUTHORITY)
190 + "|" + getCursorString(mCursor, Document.COLUMN_DOCUMENT_ID);
Tomasz Mikolajewski20c04c52016-03-15 15:55:46 +0900191 } else {
Tomasz Mikolajewski06b036f2016-04-26 11:11:17 +0900192 mIds[pos] = getCursorString(mCursor, Document.COLUMN_DOCUMENT_ID);
Tomasz Mikolajewski20c04c52016-03-15 15:55:46 +0900193 }
Ben Kwab8a5e082015-12-07 13:25:27 -0800194 }
195
196 // Populate the positions.
197 mPositions.clear();
198 for (int i = 0; i < mCursorCount; ++i) {
Garfield Tan2010ff72016-09-30 14:55:32 -0700199 mPositions.put(mIds[i], i);
Ben Kwab8a5e082015-12-07 13:25:27 -0800200 }
201 }
202
Tomasz Mikolajewskid71bd612016-02-16 12:28:43 +0900203 public @Nullable Cursor getItem(String modelId) {
Ben Kwa0497da82015-11-30 23:00:02 -0800204 Integer pos = mPositions.get(modelId);
Steve McKay5a22a112016-04-12 11:29:10 -0700205 if (pos == null) {
206 if (DEBUG) Log.d(TAG, "Unabled to find cursor position for modelId: " + modelId);
207 return null;
Ben Kwa0497da82015-11-30 23:00:02 -0800208 }
Steve McKay5a22a112016-04-12 11:29:10 -0700209
210 if (!mCursor.moveToPosition(pos)) {
211 if (DEBUG) Log.d(TAG,
212 "Unabled to move cursor to position " + pos + " for modelId: " + modelId);
213 return null;
214 }
215
216 return mCursor;
Ben Kwa0497da82015-11-30 23:00:02 -0800217 }
218
Steve McKay990f76e2016-09-16 12:36:58 -0700219 public boolean isEmpty() {
Ben Kwa0497da82015-11-30 23:00:02 -0800220 return mCursorCount == 0;
221 }
222
223 boolean isLoading() {
224 return mIsLoading;
225 }
226
Steve McKayc8889af2016-09-23 11:22:41 -0700227 public List<DocumentInfo> getDocuments(Selection selection) {
Steve McKayd79cc4a2016-10-11 16:34:40 -0700228 return loadDocuments(selection, ANY_FILE_FILTER);
Ben Kwa0497da82015-11-30 23:00:02 -0800229 }
230
Steve McKay990f76e2016-09-16 12:36:58 -0700231 public @Nullable DocumentInfo getDocument(String modelId) {
232 final Cursor cursor = getItem(modelId);
233 return (cursor == null)
234 ? null
235 : DocumentInfo.fromDirectoryCursor(cursor);
236 }
237
Steve McKayd0718952016-10-10 13:43:36 -0700238 public List<DocumentInfo> loadDocuments(Selection selection, Predicate<Cursor> filter) {
239 final int size = (selection != null) ? selection.size() : 0;
240
241 final List<DocumentInfo> docs = new ArrayList<>(size);
Tomasz Mikolajewski1abcfd12016-12-12 10:08:49 +0900242 DocumentInfo doc;
Steve McKayd0718952016-10-10 13:43:36 -0700243 for (String modelId: selection) {
Tomasz Mikolajewski1abcfd12016-12-12 10:08:49 +0900244 doc = loadDocument(modelId, filter);
245 if (doc != null) {
246 docs.add(doc);
247 }
Steve McKayd0718952016-10-10 13:43:36 -0700248 }
249 return docs;
250 }
251
Tomasz Mikolajewski1abcfd12016-12-12 10:08:49 +0900252 public boolean hasDocuments(Selection selection, Predicate<Cursor> filter) {
253 for (String modelId: selection) {
254 if (loadDocument(modelId, filter) != null) {
255 return true;
256 }
257 }
258 return false;
259 }
260
Steve McKayd0718952016-10-10 13:43:36 -0700261 /**
Steve McKayd0718952016-10-10 13:43:36 -0700262 * @return DocumentInfo, or null. If filter returns false, null will be returned.
263 */
Tomasz Mikolajewski1abcfd12016-12-12 10:08:49 +0900264 private @Nullable DocumentInfo loadDocument(String modelId, Predicate<Cursor> filter) {
Steve McKayd0718952016-10-10 13:43:36 -0700265 final Cursor cursor = getItem(modelId);
266
267 if (cursor == null) {
268 Log.w(TAG, "Unable to obtain document for modelId: " + modelId);
Tomasz Mikolajewski1abcfd12016-12-12 10:08:49 +0900269 return null;
Steve McKayd0718952016-10-10 13:43:36 -0700270 }
271
272 if (filter.test(cursor)) {
Tomasz Mikolajewski1abcfd12016-12-12 10:08:49 +0900273 return DocumentInfo.fromDirectoryCursor(cursor);
Steve McKayd0718952016-10-10 13:43:36 -0700274 }
Tomasz Mikolajewski1abcfd12016-12-12 10:08:49 +0900275
276 if (VERBOSE) Log.v(TAG, "Filtered out document from results: " + modelId);
277 return null;
Steve McKayd0718952016-10-10 13:43:36 -0700278 }
279
Steve McKay84769b82016-06-09 10:46:07 -0700280 public Uri getItemUri(String modelId) {
281 final Cursor cursor = getItem(modelId);
282 return DocumentInfo.getUri(cursor);
283 }
284
Garfield, Tan11d23482016-08-05 09:33:29 -0700285 /**
286 * @return An ordered array of model IDs representing the documents in the model. It is sorted
287 * according to the current sort order, which was set by the last model update.
288 */
289 public String[] getModelIds() {
290 return mIds;
291 }
292
Steve McKay990f76e2016-09-16 12:36:58 -0700293 public static class Update {
Ben Kwa0497da82015-11-30 23:00:02 -0800294
Steve McKay990f76e2016-09-16 12:36:58 -0700295 public static final Update UPDATE = new Update();
Ben Kwa472103f2016-02-10 15:48:25 -0800296
Steve McKay990f76e2016-09-16 12:36:58 -0700297 @IntDef(value = {
298 TYPE_UPDATE,
Ben Linf8f06e92017-01-27 17:15:48 -0800299 TYPE_UPDATE_EXCEPTION
Steve McKay990f76e2016-09-16 12:36:58 -0700300 })
301 @Retention(RetentionPolicy.SOURCE)
302 public @interface UpdateType {}
303 public static final int TYPE_UPDATE = 0;
Ben Linf8f06e92017-01-27 17:15:48 -0800304 public static final int TYPE_UPDATE_EXCEPTION = 1;
Ben Kwa0497da82015-11-30 23:00:02 -0800305
Ben Linf8f06e92017-01-27 17:15:48 -0800306 private final @UpdateType int mUpdateType;
Steve McKay990f76e2016-09-16 12:36:58 -0700307 private final @Nullable Exception mException;
308
309 private Update() {
Ben Linf8f06e92017-01-27 17:15:48 -0800310 mUpdateType = TYPE_UPDATE;
Steve McKay990f76e2016-09-16 12:36:58 -0700311 mException = null;
312 }
313
314 public Update(Exception exception) {
315 assert(exception != null);
Ben Linf8f06e92017-01-27 17:15:48 -0800316 mUpdateType = TYPE_UPDATE_EXCEPTION;
Steve McKay990f76e2016-09-16 12:36:58 -0700317 mException = exception;
318 }
319
320 public boolean isUpdate() {
Ben Linf8f06e92017-01-27 17:15:48 -0800321 return mUpdateType == TYPE_UPDATE;
Steve McKay990f76e2016-09-16 12:36:58 -0700322 }
323
Ben Linf8f06e92017-01-27 17:15:48 -0800324 public boolean hasException() {
325 return mUpdateType == TYPE_UPDATE_EXCEPTION;
Steve McKay990f76e2016-09-16 12:36:58 -0700326 }
327
Ben Linf8f06e92017-01-27 17:15:48 -0800328 public boolean hasRecoverableException() {
329 return hasException() && mException instanceof RecoverableSecurityException;
330 }
331
332 public @Nullable Exception getException() {
Steve McKay990f76e2016-09-16 12:36:58 -0700333 return mException;
334 }
Ben Kwa0497da82015-11-30 23:00:02 -0800335 }
Ben Kwa0497da82015-11-30 23:00:02 -0800336}