blob: abf5d40ded2d812e1fd77c613b3bbf5973ded34b [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.getCursorLong;
20import static com.android.documentsui.base.DocumentInfo.getCursorString;
Steve McKayd9caa6a2016-09-15 16:36:45 -070021import static com.android.documentsui.base.Shared.DEBUG;
Ben Kwa0497da82015-11-30 23:00:02 -080022
Steve McKay990f76e2016-09-16 12:36:58 -070023import android.annotation.IntDef;
Ben Kwa0497da82015-11-30 23:00:02 -080024import android.database.Cursor;
Tomasz Mikolajewski20c04c52016-03-15 15:55:46 +090025import android.database.MergeCursor;
Steve McKay84769b82016-06-09 10:46:07 -070026import android.net.Uri;
Ben Kwa0497da82015-11-30 23:00:02 -080027import android.os.Bundle;
Ben Kwa0497da82015-11-30 23:00:02 -080028import android.provider.DocumentsContract;
29import android.provider.DocumentsContract.Document;
30import android.support.annotation.Nullable;
31import android.support.annotation.VisibleForTesting;
Ben Kwa0497da82015-11-30 23:00:02 -080032import android.util.Log;
Ben Kwa0497da82015-11-30 23:00:02 -080033
Ben Kwa0497da82015-11-30 23:00:02 -080034import com.android.documentsui.DirectoryResult;
Steve McKayd0805062016-09-15 14:30:38 -070035import com.android.documentsui.base.DocumentInfo;
Steve McKay990f76e2016-09-16 12:36:58 -070036import com.android.documentsui.base.EventListener;
Steve McKayd9caa6a2016-09-15 16:36:45 -070037import com.android.documentsui.base.Shared;
Ben Kwa0497da82015-11-30 23:00:02 -080038import com.android.documentsui.dirlist.MultiSelectManager.Selection;
Steve McKayd9caa6a2016-09-15 16:36:45 -070039import com.android.documentsui.roots.RootCursorWrapper;
Garfield, Tan11d23482016-08-05 09:33:29 -070040import com.android.documentsui.sorting.SortDimension;
41import com.android.documentsui.sorting.SortModel;
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;
47import java.util.List;
Ben Kwab8a5e082015-12-07 13:25:27 -080048import java.util.Map;
Ben Kwa0497da82015-11-30 23:00:02 -080049
50/**
51 * The data model for the current loaded directory.
52 */
53@VisibleForTesting
Tomasz Mikolajewskid71bd612016-02-16 12:28:43 +090054public class Model {
Ben Kwa0497da82015-11-30 23:00:02 -080055 private static final String TAG = "Model";
Ben Kwab8a5e082015-12-07 13:25:27 -080056
Ben Kwa0497da82015-11-30 23:00:02 -080057 private boolean mIsLoading;
Steve McKay990f76e2016-09-16 12:36:58 -070058 private List<EventListener<Update>> mUpdateListeners = new ArrayList<>();
Ben Kwa0497da82015-11-30 23:00:02 -080059 @Nullable private Cursor mCursor;
Ben Kwab8a5e082015-12-07 13:25:27 -080060 private int mCursorCount;
61 /** Maps Model ID to cursor positions, for looking up items by Model ID. */
62 private Map<String, Integer> mPositions = new HashMap<>();
63 /**
64 * A sorted array of model IDs for the files currently in the Model. Sort order is determined
Garfield, Tan11d23482016-08-05 09:33:29 -070065 * by {@link #mSortModel}
Ben Kwab8a5e082015-12-07 13:25:27 -080066 */
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +090067 private String mIds[] = new String[0];
Garfield, Tan11d23482016-08-05 09:33:29 -070068 private SortModel mSortModel;
Ben Kwab8a5e082015-12-07 13:25:27 -080069
Ben Kwa0497da82015-11-30 23:00:02 -080070 @Nullable String info;
71 @Nullable String error;
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +090072 @Nullable DocumentInfo doc;
Ben Kwa0497da82015-11-30 23:00:02 -080073
Steve McKay990f76e2016-09-16 12:36:58 -070074 public void addUpdateListener(EventListener<Update> listener) {
75 mUpdateListeners.add(listener);
76 }
77
78 public void removeUpdateListener(EventListener<Update> listener) {
79 mUpdateListeners.remove(listener);
80 }
81
Ben Kwad72a1da2015-12-01 19:56:57 -080082 private void notifyUpdateListeners() {
Steve McKay990f76e2016-09-16 12:36:58 -070083 for (EventListener<Update> handler: mUpdateListeners) {
84 handler.accept(Update.UPDATE);
Ben Kwad72a1da2015-12-01 19:56:57 -080085 }
86 }
87
88 private void notifyUpdateListeners(Exception e) {
Steve McKay990f76e2016-09-16 12:36:58 -070089 Update error = new Update(e);
90 for (EventListener<Update> handler: mUpdateListeners) {
91 handler.accept(error);
Ben Kwad72a1da2015-12-01 19:56:57 -080092 }
93 }
94
Steve McKay9de0da62016-08-25 15:18:23 -070095 void onLoaderReset() {
96 if (mIsLoading) {
Steve McKay7c662092016-08-26 12:17:41 -070097 Log.w(TAG, "Received unexpected loader reset while in loading state for doc: "
98 + DocumentInfo.debugString(doc));
99 return;
Ben Kwa0497da82015-11-30 23:00:02 -0800100 }
Steve McKay7c662092016-08-26 12:17:41 -0700101
102 reset();
Steve McKay9de0da62016-08-25 15:18:23 -0700103 }
104
105 private void reset() {
106 mCursor = null;
107 mCursorCount = 0;
108 mIds = new String[0];
109 mPositions.clear();
110 info = null;
111 error = null;
112 doc = null;
113 mIsLoading = false;
114 notifyUpdateListeners();
115 }
116
117 void update(DirectoryResult result) {
118 assert(result != null);
119
120 if (DEBUG) Log.i(TAG, "Updating model with new result set.");
Ben Kwa0497da82015-11-30 23:00:02 -0800121
122 if (result.exception != null) {
123 Log.e(TAG, "Error while loading directory contents", result.exception);
Ben Kwad72a1da2015-12-01 19:56:57 -0800124 notifyUpdateListeners(result.exception);
Ben Kwa0497da82015-11-30 23:00:02 -0800125 return;
126 }
127
128 mCursor = result.cursor;
129 mCursorCount = mCursor.getCount();
Garfield, Tan11d23482016-08-05 09:33:29 -0700130 mSortModel = result.sortModel;
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +0900131 doc = result.doc;
Ben Kwa0497da82015-11-30 23:00:02 -0800132
Ben Kwab8a5e082015-12-07 13:25:27 -0800133 updateModelData();
Ben Kwa0497da82015-11-30 23:00:02 -0800134
135 final Bundle extras = mCursor.getExtras();
136 if (extras != null) {
137 info = extras.getString(DocumentsContract.EXTRA_INFO);
138 error = extras.getString(DocumentsContract.EXTRA_ERROR);
139 mIsLoading = extras.getBoolean(DocumentsContract.EXTRA_LOADING, false);
140 }
141
Ben Kwad72a1da2015-12-01 19:56:57 -0800142 notifyUpdateListeners();
Ben Kwa0497da82015-11-30 23:00:02 -0800143 }
144
Ben Kwad72a1da2015-12-01 19:56:57 -0800145 @VisibleForTesting
Ben Kwa0497da82015-11-30 23:00:02 -0800146 int getItemCount() {
Ben Kwada858bf2015-12-09 14:33:49 -0800147 return mCursorCount;
Ben Kwa0497da82015-11-30 23:00:02 -0800148 }
149
150 /**
Ben Kwab8a5e082015-12-07 13:25:27 -0800151 * Scan over the incoming cursor data, generate Model IDs for each row, and sort the IDs
152 * according to the current sort order.
Ben Kwa0497da82015-11-30 23:00:02 -0800153 */
Ben Kwab8a5e082015-12-07 13:25:27 -0800154 private void updateModelData() {
155 int[] positions = new int[mCursorCount];
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900156 mIds = new String[mCursorCount];
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900157 boolean[] isDirs = new boolean[mCursorCount];
158 String[] displayNames = null;
Ben Kwa6280de02015-12-16 19:42:08 -0800159 long[] longValues = null;
Ben Kwab8a5e082015-12-07 13:25:27 -0800160
Garfield, Tan11d23482016-08-05 09:33:29 -0700161 final int id = mSortModel.getSortedDimensionId();
162 switch (id) {
163 case SortModel.SORT_DIMENSION_ID_TITLE:
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900164 displayNames = new String[mCursorCount];
165 break;
Garfield, Tan11d23482016-08-05 09:33:29 -0700166 case SortModel.SORT_DIMENSION_ID_DATE:
167 case SortModel.SORT_DIMENSION_ID_SIZE:
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900168 longValues = new long[mCursorCount];
169 break;
Ben Kwab8a5e082015-12-07 13:25:27 -0800170 }
171
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900172 String mimeType;
173
Ben Kwa0497da82015-11-30 23:00:02 -0800174 mCursor.moveToPosition(-1);
175 for (int pos = 0; pos < mCursorCount; ++pos) {
Ben Lin2df30e52016-04-22 16:12:50 -0700176 if (!mCursor.moveToNext()) {
177 Log.e(TAG, "Fail to move cursor to next pos: " + pos);
178 return;
179 }
Ben Kwab8a5e082015-12-07 13:25:27 -0800180 positions[pos] = pos;
Ben Kwab8a5e082015-12-07 13:25:27 -0800181
Tomasz Mikolajewski985df3d2016-03-15 15:38:54 +0900182 // Generates a Model ID for a cursor entry that refers to a document. The Model ID is a
183 // unique string that can be used to identify the document referred to by the cursor.
Tomasz Mikolajewski20c04c52016-03-15 15:55:46 +0900184 // If the cursor is a merged cursor over multiple authorities, then prefix the ids
185 // with the authority to avoid collisions.
186 if (mCursor instanceof MergeCursor) {
Tomasz Mikolajewski06b036f2016-04-26 11:11:17 +0900187 mIds[pos] = getCursorString(mCursor, RootCursorWrapper.COLUMN_AUTHORITY) + "|" +
188 getCursorString(mCursor, Document.COLUMN_DOCUMENT_ID);
Tomasz Mikolajewski20c04c52016-03-15 15:55:46 +0900189 } else {
Tomasz Mikolajewski06b036f2016-04-26 11:11:17 +0900190 mIds[pos] = getCursorString(mCursor, Document.COLUMN_DOCUMENT_ID);
Tomasz Mikolajewski20c04c52016-03-15 15:55:46 +0900191 }
Tomasz Mikolajewski985df3d2016-03-15 15:38:54 +0900192
Tomasz Mikolajewski06b036f2016-04-26 11:11:17 +0900193 mimeType = getCursorString(mCursor, Document.COLUMN_MIME_TYPE);
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900194 isDirs[pos] = Document.MIME_TYPE_DIR.equals(mimeType);
195
Garfield, Tan11d23482016-08-05 09:33:29 -0700196 switch(id) {
197 case SortModel.SORT_DIMENSION_ID_TITLE:
Tomasz Mikolajewski06b036f2016-04-26 11:11:17 +0900198 final String displayName = getCursorString(
199 mCursor, Document.COLUMN_DISPLAY_NAME);
200 displayNames[pos] = displayName;
Ben Kwab8a5e082015-12-07 13:25:27 -0800201 break;
Garfield, Tan11d23482016-08-05 09:33:29 -0700202 case SortModel.SORT_DIMENSION_ID_DATE:
Tomasz Mikolajewski06b036f2016-04-26 11:11:17 +0900203 longValues[pos] = getLastModified(mCursor);
Ben Kwab8a5e082015-12-07 13:25:27 -0800204 break;
Garfield, Tan11d23482016-08-05 09:33:29 -0700205 case SortModel.SORT_DIMENSION_ID_SIZE:
Tomasz Mikolajewski06b036f2016-04-26 11:11:17 +0900206 longValues[pos] = getCursorLong(mCursor, Document.COLUMN_SIZE);
Ben Kwab8a5e082015-12-07 13:25:27 -0800207 break;
208 }
209 }
210
Garfield, Tan11d23482016-08-05 09:33:29 -0700211 final SortDimension dimension = mSortModel.getDimensionById(id);
212 switch (id) {
213 case SortModel.SORT_DIMENSION_ID_TITLE:
214 binarySort(displayNames, isDirs, positions, mIds, dimension.getSortDirection());
Ben Kwab8a5e082015-12-07 13:25:27 -0800215 break;
Garfield, Tan11d23482016-08-05 09:33:29 -0700216 case SortModel.SORT_DIMENSION_ID_DATE:
217 case SortModel.SORT_DIMENSION_ID_SIZE:
218 binarySort(longValues, isDirs, positions, mIds, dimension.getSortDirection());
Ben Kwab8a5e082015-12-07 13:25:27 -0800219 break;
220 }
221
222 // Populate the positions.
223 mPositions.clear();
224 for (int i = 0; i < mCursorCount; ++i) {
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900225 mPositions.put(mIds[i], positions[i]);
Ben Kwab8a5e082015-12-07 13:25:27 -0800226 }
227 }
228
229 /**
Ben Kwa6280de02015-12-16 19:42:08 -0800230 * Sorts model data. Takes three columns of index-corresponded data. The first column is the
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900231 * sort key. Rows are sorted in ascending alphabetical order on the sort key.
232 * Directories are always shown first. This code is based on TimSort.binarySort().
Ben Kwa6280de02015-12-16 19:42:08 -0800233 *
234 * @param sortKey Data is sorted in ascending alphabetical order.
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900235 * @param isDirs Array saying whether an item is a directory or not.
Ben Kwa6280de02015-12-16 19:42:08 -0800236 * @param positions Cursor positions to be sorted.
237 * @param ids Model IDs to be sorted.
Ben Kwab8a5e082015-12-07 13:25:27 -0800238 */
Garfield, Tan11d23482016-08-05 09:33:29 -0700239 private static void binarySort(
240 String[] sortKey,
241 boolean[] isDirs,
242 int[] positions,
243 String[] ids,
244 @SortDimension.SortDirection int direction) {
Ben Kwab8a5e082015-12-07 13:25:27 -0800245 final int count = positions.length;
246 for (int start = 1; start < count; start++) {
247 final int pivotPosition = positions[start];
Ben Kwa6280de02015-12-16 19:42:08 -0800248 final String pivotValue = sortKey[start];
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900249 final boolean pivotIsDir = isDirs[start];
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900250 final String pivotId = ids[start];
Ben Kwab8a5e082015-12-07 13:25:27 -0800251
252 int left = 0;
253 int right = start;
254
255 while (left < right) {
256 int mid = (left + right) >>> 1;
257
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900258 // Directories always go in front.
259 int compare = 0;
260 final boolean rhsIsDir = isDirs[mid];
261 if (pivotIsDir && !rhsIsDir) {
262 compare = -1;
263 } else if (!pivotIsDir && rhsIsDir) {
264 compare = 1;
265 } else {
266 final String lhs = pivotValue;
267 final String rhs = sortKey[mid];
Garfield, Tan11d23482016-08-05 09:33:29 -0700268 switch (direction) {
269 case SortDimension.SORT_DIRECTION_ASCENDING:
270 compare = Shared.compareToIgnoreCaseNullable(lhs, rhs);
271 break;
272 case SortDimension.SORT_DIRECTION_DESCENDING:
273 compare = -Shared.compareToIgnoreCaseNullable(lhs, rhs);
274 break;
275 default:
276 throw new IllegalArgumentException(
277 "Unknown sorting direction: " + direction);
278 }
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900279 }
Ben Kwab8a5e082015-12-07 13:25:27 -0800280
281 if (compare < 0) {
282 right = mid;
283 } else {
284 left = mid + 1;
285 }
286 }
287
288 int n = start - left;
289 switch (n) {
290 case 2:
291 positions[left + 2] = positions[left + 1];
Ben Kwa6280de02015-12-16 19:42:08 -0800292 sortKey[left + 2] = sortKey[left + 1];
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900293 isDirs[left + 2] = isDirs[left + 1];
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900294 ids[left + 2] = ids[left + 1];
Ben Kwab8a5e082015-12-07 13:25:27 -0800295 case 1:
296 positions[left + 1] = positions[left];
Ben Kwa6280de02015-12-16 19:42:08 -0800297 sortKey[left + 1] = sortKey[left];
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900298 isDirs[left + 1] = isDirs[left];
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900299 ids[left + 1] = ids[left];
Ben Kwab8a5e082015-12-07 13:25:27 -0800300 break;
301 default:
302 System.arraycopy(positions, left, positions, left + 1, n);
Ben Kwa6280de02015-12-16 19:42:08 -0800303 System.arraycopy(sortKey, left, sortKey, left + 1, n);
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900304 System.arraycopy(isDirs, left, isDirs, left + 1, n);
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900305 System.arraycopy(ids, left, ids, left + 1, n);
Ben Kwab8a5e082015-12-07 13:25:27 -0800306 }
307
308 positions[left] = pivotPosition;
Ben Kwa6280de02015-12-16 19:42:08 -0800309 sortKey[left] = pivotValue;
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900310 isDirs[left] = pivotIsDir;
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900311 ids[left] = pivotId;
Ben Kwab8a5e082015-12-07 13:25:27 -0800312 }
313 }
314
315 /**
Ben Kwa6280de02015-12-16 19:42:08 -0800316 * Sorts model data. Takes four columns of index-corresponded data. The first column is the sort
317 * key, and the second is an array of mime types. The rows are first bucketed by mime type
318 * (directories vs documents) and then each bucket is sorted independently in descending
319 * numerical order on the sort key. This code is based on TimSort.binarySort().
320 *
321 * @param sortKey Data is sorted in descending numerical order.
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900322 * @param isDirs Array saying whether an item is a directory or not.
Ben Kwa6280de02015-12-16 19:42:08 -0800323 * @param positions Cursor positions to be sorted.
324 * @param ids Model IDs to be sorted.
Ben Kwab8a5e082015-12-07 13:25:27 -0800325 */
Ben Kwa6280de02015-12-16 19:42:08 -0800326 private static void binarySort(
Garfield, Tan11d23482016-08-05 09:33:29 -0700327 long[] sortKey,
328 boolean[] isDirs,
329 int[] positions,
330 String[] ids,
331 @SortDimension.SortDirection int direction) {
Ben Kwab8a5e082015-12-07 13:25:27 -0800332 final int count = positions.length;
333 for (int start = 1; start < count; start++) {
334 final int pivotPosition = positions[start];
Ben Kwa6280de02015-12-16 19:42:08 -0800335 final long pivotValue = sortKey[start];
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900336 final boolean pivotIsDir = isDirs[start];
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900337 final String pivotId = ids[start];
Ben Kwab8a5e082015-12-07 13:25:27 -0800338
339 int left = 0;
340 int right = start;
341
342 while (left < right) {
Ben Kwa6280de02015-12-16 19:42:08 -0800343 int mid = ((left + right) >>> 1);
Ben Kwab8a5e082015-12-07 13:25:27 -0800344
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900345 // Directories always go in front.
Ben Kwa6280de02015-12-16 19:42:08 -0800346 int compare = 0;
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900347 final boolean rhsIsDir = isDirs[mid];
348 if (pivotIsDir && !rhsIsDir) {
Ben Kwa6280de02015-12-16 19:42:08 -0800349 compare = -1;
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900350 } else if (!pivotIsDir && rhsIsDir) {
Ben Kwa6280de02015-12-16 19:42:08 -0800351 compare = 1;
352 } else {
353 final long lhs = pivotValue;
354 final long rhs = sortKey[mid];
Garfield, Tan11d23482016-08-05 09:33:29 -0700355 switch (direction) {
356 case SortDimension.SORT_DIRECTION_ASCENDING:
357 compare = Long.compare(lhs, rhs);
358 break;
359 case SortDimension.SORT_DIRECTION_DESCENDING:
360 compare = -Long.compare(lhs, rhs);
361 break;
362 default:
363 throw new IllegalArgumentException(
364 "Unknown sorting direction: " + direction);
365 }
Ben Kwa6280de02015-12-16 19:42:08 -0800366 }
Ben Kwab8a5e082015-12-07 13:25:27 -0800367
Ben Kwae4338342016-01-08 15:34:47 -0800368 // If numerical comparison yields a tie, use document ID as a tie breaker. This
369 // will yield stable results even if incoming items are continually shuffling and
370 // have identical numerical sort keys. One common example of this scenario is seen
371 // when sorting a set of active downloads by mod time.
372 if (compare == 0) {
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900373 compare = pivotId.compareTo(ids[mid]);
Ben Kwae4338342016-01-08 15:34:47 -0800374 }
375
Ben Kwab8a5e082015-12-07 13:25:27 -0800376 if (compare < 0) {
377 right = mid;
378 } else {
379 left = mid + 1;
380 }
381 }
382
383 int n = start - left;
384 switch (n) {
385 case 2:
386 positions[left + 2] = positions[left + 1];
Ben Kwa6280de02015-12-16 19:42:08 -0800387 sortKey[left + 2] = sortKey[left + 1];
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900388 isDirs[left + 2] = isDirs[left + 1];
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900389 ids[left + 2] = ids[left + 1];
Ben Kwab8a5e082015-12-07 13:25:27 -0800390 case 1:
391 positions[left + 1] = positions[left];
Ben Kwa6280de02015-12-16 19:42:08 -0800392 sortKey[left + 1] = sortKey[left];
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900393 isDirs[left + 1] = isDirs[left];
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900394 ids[left + 1] = ids[left];
Ben Kwab8a5e082015-12-07 13:25:27 -0800395 break;
396 default:
397 System.arraycopy(positions, left, positions, left + 1, n);
Ben Kwa6280de02015-12-16 19:42:08 -0800398 System.arraycopy(sortKey, left, sortKey, left + 1, n);
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900399 System.arraycopy(isDirs, left, isDirs, left + 1, n);
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900400 System.arraycopy(ids, left, ids, left + 1, n);
Ben Kwab8a5e082015-12-07 13:25:27 -0800401 }
402
403 positions[left] = pivotPosition;
Ben Kwa6280de02015-12-16 19:42:08 -0800404 sortKey[left] = pivotValue;
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900405 isDirs[left] = pivotIsDir;
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900406 ids[left] = pivotId;
Ben Kwa0497da82015-11-30 23:00:02 -0800407 }
408 }
409
Ben Kwae4338342016-01-08 15:34:47 -0800410 /**
Tomasz Mikolajewski985df3d2016-03-15 15:38:54 +0900411 * @return Timestamp for the given document. Some docs (e.g. active downloads) have a null
Tomasz Mikolajewski06b036f2016-04-26 11:11:17 +0900412 * timestamp - these will be replaced with MAX_LONG so that such files get sorted to the top
Garfield, Tan11d23482016-08-05 09:33:29 -0700413 * when sorting descending by date.
Tomasz Mikolajewski985df3d2016-03-15 15:38:54 +0900414 */
Tomasz Mikolajewski06b036f2016-04-26 11:11:17 +0900415 long getLastModified(Cursor cursor) {
416 long l = getCursorLong(mCursor, Document.COLUMN_LAST_MODIFIED);
417 return (l == -1) ? Long.MAX_VALUE : l;
Ben Kwae4338342016-01-08 15:34:47 -0800418 }
419
Tomasz Mikolajewskid71bd612016-02-16 12:28:43 +0900420 public @Nullable Cursor getItem(String modelId) {
Ben Kwa0497da82015-11-30 23:00:02 -0800421 Integer pos = mPositions.get(modelId);
Steve McKay5a22a112016-04-12 11:29:10 -0700422 if (pos == null) {
423 if (DEBUG) Log.d(TAG, "Unabled to find cursor position for modelId: " + modelId);
424 return null;
Ben Kwa0497da82015-11-30 23:00:02 -0800425 }
Steve McKay5a22a112016-04-12 11:29:10 -0700426
427 if (!mCursor.moveToPosition(pos)) {
428 if (DEBUG) Log.d(TAG,
429 "Unabled to move cursor to position " + pos + " for modelId: " + modelId);
430 return null;
431 }
432
433 return mCursor;
Ben Kwa0497da82015-11-30 23:00:02 -0800434 }
435
Steve McKay990f76e2016-09-16 12:36:58 -0700436 public boolean isEmpty() {
Ben Kwa0497da82015-11-30 23:00:02 -0800437 return mCursorCount == 0;
438 }
439
440 boolean isLoading() {
441 return mIsLoading;
442 }
443
Steve McKay84769b82016-06-09 10:46:07 -0700444 List<DocumentInfo> getDocuments(Selection selection) {
445 final int size = (selection != null) ? selection.size() : 0;
Ben Kwa0497da82015-11-30 23:00:02 -0800446
447 final List<DocumentInfo> docs = new ArrayList<>(size);
Steve McKay84769b82016-06-09 10:46:07 -0700448 // NOTE: That as this now iterates over only final (non-provisional) selection.
449 for (String modelId: selection) {
Steve McKay990f76e2016-09-16 12:36:58 -0700450 DocumentInfo doc = getDocument(modelId);
451 if (doc == null) {
452 Log.w(TAG, "Unable to obtain document for modelId: " + modelId);
Steve McKay5a22a112016-04-12 11:29:10 -0700453 continue;
454 }
Steve McKay990f76e2016-09-16 12:36:58 -0700455 docs.add(doc);
Ben Kwa0497da82015-11-30 23:00:02 -0800456 }
457 return docs;
458 }
459
Steve McKay990f76e2016-09-16 12:36:58 -0700460 public @Nullable DocumentInfo getDocument(String modelId) {
461 final Cursor cursor = getItem(modelId);
462 return (cursor == null)
463 ? null
464 : DocumentInfo.fromDirectoryCursor(cursor);
465 }
466
Steve McKay84769b82016-06-09 10:46:07 -0700467 public Uri getItemUri(String modelId) {
468 final Cursor cursor = getItem(modelId);
469 return DocumentInfo.getUri(cursor);
470 }
471
Garfield, Tan11d23482016-08-05 09:33:29 -0700472 /**
473 * @return An ordered array of model IDs representing the documents in the model. It is sorted
474 * according to the current sort order, which was set by the last model update.
475 */
476 public String[] getModelIds() {
477 return mIds;
478 }
479
Steve McKay990f76e2016-09-16 12:36:58 -0700480 public static class Update {
Ben Kwa0497da82015-11-30 23:00:02 -0800481
Steve McKay990f76e2016-09-16 12:36:58 -0700482 public static final Update UPDATE = new Update();
Ben Kwa472103f2016-02-10 15:48:25 -0800483
Steve McKay990f76e2016-09-16 12:36:58 -0700484 @IntDef(value = {
485 TYPE_UPDATE,
486 TYPE_UPDATE_ERROR
487 })
488 @Retention(RetentionPolicy.SOURCE)
489 public @interface UpdateType {}
490 public static final int TYPE_UPDATE = 0;
491 public static final int TYPE_UPDATE_ERROR = 1;
Ben Kwa0497da82015-11-30 23:00:02 -0800492
Steve McKay990f76e2016-09-16 12:36:58 -0700493 private final @UpdateType int mType;
494 private final @Nullable Exception mException;
495
496 private Update() {
497 mType = TYPE_UPDATE;
498 mException = null;
499 }
500
501 public Update(Exception exception) {
502 assert(exception != null);
503 mType = TYPE_UPDATE_ERROR;
504 mException = exception;
505 }
506
507 public boolean isUpdate() {
508 return mType == TYPE_UPDATE;
509 }
510
511 public boolean hasError() {
512 return mType == TYPE_UPDATE_ERROR;
513 }
514
515 public @Nullable Exception getError() {
516 return mException;
517 }
Ben Kwa0497da82015-11-30 23:00:02 -0800518 }
Ben Kwa0497da82015-11-30 23:00:02 -0800519}