blob: 5ddad31cfa4ca50f903a3490d4d3d4dbd70421ba [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
19import static com.android.documentsui.Shared.DEBUG;
Tomasz Mikolajewski06b036f2016-04-26 11:11:17 +090020import static com.android.documentsui.model.DocumentInfo.getCursorLong;
21import static com.android.documentsui.model.DocumentInfo.getCursorString;
Ben Kwa0497da82015-11-30 23:00:02 -080022
Ben Kwa0497da82015-11-30 23:00:02 -080023import android.database.Cursor;
Tomasz Mikolajewski20c04c52016-03-15 15:55:46 +090024import android.database.MergeCursor;
Steve McKay84769b82016-06-09 10:46:07 -070025import android.net.Uri;
Ben Kwa0497da82015-11-30 23:00:02 -080026import android.os.Bundle;
Ben Kwa0497da82015-11-30 23:00:02 -080027import android.provider.DocumentsContract;
28import android.provider.DocumentsContract.Document;
29import android.support.annotation.Nullable;
30import android.support.annotation.VisibleForTesting;
Ben Kwa0497da82015-11-30 23:00:02 -080031import android.util.Log;
Ben Kwa0497da82015-11-30 23:00:02 -080032
Ben Kwa0497da82015-11-30 23:00:02 -080033import com.android.documentsui.DirectoryResult;
Ben Kwa0497da82015-11-30 23:00:02 -080034import com.android.documentsui.RootCursorWrapper;
Steve McKay55c00e72016-02-18 15:32:16 -080035import com.android.documentsui.Shared;
Ben Kwa0497da82015-11-30 23:00:02 -080036import com.android.documentsui.dirlist.MultiSelectManager.Selection;
37import com.android.documentsui.model.DocumentInfo;
Garfield, Tan11d23482016-08-05 09:33:29 -070038import com.android.documentsui.sorting.SortDimension;
39import com.android.documentsui.sorting.SortModel;
Ben Kwa0497da82015-11-30 23:00:02 -080040
41import java.util.ArrayList;
Ben Kwa0497da82015-11-30 23:00:02 -080042import java.util.HashMap;
43import java.util.List;
Ben Kwab8a5e082015-12-07 13:25:27 -080044import java.util.Map;
Ben Kwa0497da82015-11-30 23:00:02 -080045
46/**
47 * The data model for the current loaded directory.
48 */
49@VisibleForTesting
Tomasz Mikolajewskid71bd612016-02-16 12:28:43 +090050public class Model {
Ben Kwa0497da82015-11-30 23:00:02 -080051 private static final String TAG = "Model";
Ben Kwab8a5e082015-12-07 13:25:27 -080052
Ben Kwa0497da82015-11-30 23:00:02 -080053 private boolean mIsLoading;
Ben Kwad72a1da2015-12-01 19:56:57 -080054 private List<UpdateListener> mUpdateListeners = new ArrayList<>();
Ben Kwa0497da82015-11-30 23:00:02 -080055 @Nullable private Cursor mCursor;
Ben Kwab8a5e082015-12-07 13:25:27 -080056 private int mCursorCount;
57 /** Maps Model ID to cursor positions, for looking up items by Model ID. */
58 private Map<String, Integer> mPositions = new HashMap<>();
59 /**
60 * A sorted array of model IDs for the files currently in the Model. Sort order is determined
Garfield, Tan11d23482016-08-05 09:33:29 -070061 * by {@link #mSortModel}
Ben Kwab8a5e082015-12-07 13:25:27 -080062 */
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +090063 private String mIds[] = new String[0];
Garfield, Tan11d23482016-08-05 09:33:29 -070064 private SortModel mSortModel;
Ben Kwab8a5e082015-12-07 13:25:27 -080065
Ben Kwa0497da82015-11-30 23:00:02 -080066 @Nullable String info;
67 @Nullable String error;
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +090068 @Nullable DocumentInfo doc;
Ben Kwa0497da82015-11-30 23:00:02 -080069
Ben Kwad72a1da2015-12-01 19:56:57 -080070 private void notifyUpdateListeners() {
71 for (UpdateListener listener: mUpdateListeners) {
72 listener.onModelUpdate(this);
73 }
74 }
75
76 private void notifyUpdateListeners(Exception e) {
77 for (UpdateListener listener: mUpdateListeners) {
78 listener.onModelUpdateFailed(e);
79 }
80 }
81
Steve McKay9de0da62016-08-25 15:18:23 -070082 void onLoaderReset() {
83 if (mIsLoading) {
84 if (DEBUG) Log.w(TAG, "Received unexpected loader reset while in loading state.");
Ben Kwa0497da82015-11-30 23:00:02 -080085 }
Steve McKay9de0da62016-08-25 15:18:23 -070086 }
87
88 private void reset() {
89 mCursor = null;
90 mCursorCount = 0;
91 mIds = new String[0];
92 mPositions.clear();
93 info = null;
94 error = null;
95 doc = null;
96 mIsLoading = false;
97 notifyUpdateListeners();
98 }
99
100 void update(DirectoryResult result) {
101 assert(result != null);
102
103 if (DEBUG) Log.i(TAG, "Updating model with new result set.");
Ben Kwa0497da82015-11-30 23:00:02 -0800104
105 if (result.exception != null) {
106 Log.e(TAG, "Error while loading directory contents", result.exception);
Ben Kwad72a1da2015-12-01 19:56:57 -0800107 notifyUpdateListeners(result.exception);
Ben Kwa0497da82015-11-30 23:00:02 -0800108 return;
109 }
110
111 mCursor = result.cursor;
112 mCursorCount = mCursor.getCount();
Garfield, Tan11d23482016-08-05 09:33:29 -0700113 mSortModel = result.sortModel;
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +0900114 doc = result.doc;
Ben Kwa0497da82015-11-30 23:00:02 -0800115
Ben Kwab8a5e082015-12-07 13:25:27 -0800116 updateModelData();
Ben Kwa0497da82015-11-30 23:00:02 -0800117
118 final Bundle extras = mCursor.getExtras();
119 if (extras != null) {
120 info = extras.getString(DocumentsContract.EXTRA_INFO);
121 error = extras.getString(DocumentsContract.EXTRA_ERROR);
122 mIsLoading = extras.getBoolean(DocumentsContract.EXTRA_LOADING, false);
123 }
124
Ben Kwad72a1da2015-12-01 19:56:57 -0800125 notifyUpdateListeners();
Ben Kwa0497da82015-11-30 23:00:02 -0800126 }
127
Ben Kwad72a1da2015-12-01 19:56:57 -0800128 @VisibleForTesting
Ben Kwa0497da82015-11-30 23:00:02 -0800129 int getItemCount() {
Ben Kwada858bf2015-12-09 14:33:49 -0800130 return mCursorCount;
Ben Kwa0497da82015-11-30 23:00:02 -0800131 }
132
133 /**
Ben Kwab8a5e082015-12-07 13:25:27 -0800134 * Scan over the incoming cursor data, generate Model IDs for each row, and sort the IDs
135 * according to the current sort order.
Ben Kwa0497da82015-11-30 23:00:02 -0800136 */
Ben Kwab8a5e082015-12-07 13:25:27 -0800137 private void updateModelData() {
138 int[] positions = new int[mCursorCount];
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900139 mIds = new String[mCursorCount];
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900140 boolean[] isDirs = new boolean[mCursorCount];
141 String[] displayNames = null;
Ben Kwa6280de02015-12-16 19:42:08 -0800142 long[] longValues = null;
Ben Kwab8a5e082015-12-07 13:25:27 -0800143
Garfield, Tan11d23482016-08-05 09:33:29 -0700144 final int id = mSortModel.getSortedDimensionId();
145 switch (id) {
146 case SortModel.SORT_DIMENSION_ID_TITLE:
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900147 displayNames = new String[mCursorCount];
148 break;
Garfield, Tan11d23482016-08-05 09:33:29 -0700149 case SortModel.SORT_DIMENSION_ID_DATE:
150 case SortModel.SORT_DIMENSION_ID_SIZE:
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900151 longValues = new long[mCursorCount];
152 break;
Ben Kwab8a5e082015-12-07 13:25:27 -0800153 }
154
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900155 String mimeType;
156
Ben Kwa0497da82015-11-30 23:00:02 -0800157 mCursor.moveToPosition(-1);
158 for (int pos = 0; pos < mCursorCount; ++pos) {
Ben Lin2df30e52016-04-22 16:12:50 -0700159 if (!mCursor.moveToNext()) {
160 Log.e(TAG, "Fail to move cursor to next pos: " + pos);
161 return;
162 }
Ben Kwab8a5e082015-12-07 13:25:27 -0800163 positions[pos] = pos;
Ben Kwab8a5e082015-12-07 13:25:27 -0800164
Tomasz Mikolajewski985df3d2016-03-15 15:38:54 +0900165 // Generates a Model ID for a cursor entry that refers to a document. The Model ID is a
166 // unique string that can be used to identify the document referred to by the cursor.
Tomasz Mikolajewski20c04c52016-03-15 15:55:46 +0900167 // If the cursor is a merged cursor over multiple authorities, then prefix the ids
168 // with the authority to avoid collisions.
169 if (mCursor instanceof MergeCursor) {
Tomasz Mikolajewski06b036f2016-04-26 11:11:17 +0900170 mIds[pos] = getCursorString(mCursor, RootCursorWrapper.COLUMN_AUTHORITY) + "|" +
171 getCursorString(mCursor, Document.COLUMN_DOCUMENT_ID);
Tomasz Mikolajewski20c04c52016-03-15 15:55:46 +0900172 } else {
Tomasz Mikolajewski06b036f2016-04-26 11:11:17 +0900173 mIds[pos] = getCursorString(mCursor, Document.COLUMN_DOCUMENT_ID);
Tomasz Mikolajewski20c04c52016-03-15 15:55:46 +0900174 }
Tomasz Mikolajewski985df3d2016-03-15 15:38:54 +0900175
Tomasz Mikolajewski06b036f2016-04-26 11:11:17 +0900176 mimeType = getCursorString(mCursor, Document.COLUMN_MIME_TYPE);
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900177 isDirs[pos] = Document.MIME_TYPE_DIR.equals(mimeType);
178
Garfield, Tan11d23482016-08-05 09:33:29 -0700179 switch(id) {
180 case SortModel.SORT_DIMENSION_ID_TITLE:
Tomasz Mikolajewski06b036f2016-04-26 11:11:17 +0900181 final String displayName = getCursorString(
182 mCursor, Document.COLUMN_DISPLAY_NAME);
183 displayNames[pos] = displayName;
Ben Kwab8a5e082015-12-07 13:25:27 -0800184 break;
Garfield, Tan11d23482016-08-05 09:33:29 -0700185 case SortModel.SORT_DIMENSION_ID_DATE:
Tomasz Mikolajewski06b036f2016-04-26 11:11:17 +0900186 longValues[pos] = getLastModified(mCursor);
Ben Kwab8a5e082015-12-07 13:25:27 -0800187 break;
Garfield, Tan11d23482016-08-05 09:33:29 -0700188 case SortModel.SORT_DIMENSION_ID_SIZE:
Tomasz Mikolajewski06b036f2016-04-26 11:11:17 +0900189 longValues[pos] = getCursorLong(mCursor, Document.COLUMN_SIZE);
Ben Kwab8a5e082015-12-07 13:25:27 -0800190 break;
191 }
192 }
193
Garfield, Tan11d23482016-08-05 09:33:29 -0700194 final SortDimension dimension = mSortModel.getDimensionById(id);
195 switch (id) {
196 case SortModel.SORT_DIMENSION_ID_TITLE:
197 binarySort(displayNames, isDirs, positions, mIds, dimension.getSortDirection());
Ben Kwab8a5e082015-12-07 13:25:27 -0800198 break;
Garfield, Tan11d23482016-08-05 09:33:29 -0700199 case SortModel.SORT_DIMENSION_ID_DATE:
200 case SortModel.SORT_DIMENSION_ID_SIZE:
201 binarySort(longValues, isDirs, positions, mIds, dimension.getSortDirection());
Ben Kwab8a5e082015-12-07 13:25:27 -0800202 break;
203 }
204
205 // Populate the positions.
206 mPositions.clear();
207 for (int i = 0; i < mCursorCount; ++i) {
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900208 mPositions.put(mIds[i], positions[i]);
Ben Kwab8a5e082015-12-07 13:25:27 -0800209 }
210 }
211
212 /**
Ben Kwa6280de02015-12-16 19:42:08 -0800213 * Sorts model data. Takes three columns of index-corresponded data. The first column is the
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900214 * sort key. Rows are sorted in ascending alphabetical order on the sort key.
215 * Directories are always shown first. This code is based on TimSort.binarySort().
Ben Kwa6280de02015-12-16 19:42:08 -0800216 *
217 * @param sortKey Data is sorted in ascending alphabetical order.
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900218 * @param isDirs Array saying whether an item is a directory or not.
Ben Kwa6280de02015-12-16 19:42:08 -0800219 * @param positions Cursor positions to be sorted.
220 * @param ids Model IDs to be sorted.
Ben Kwab8a5e082015-12-07 13:25:27 -0800221 */
Garfield, Tan11d23482016-08-05 09:33:29 -0700222 private static void binarySort(
223 String[] sortKey,
224 boolean[] isDirs,
225 int[] positions,
226 String[] ids,
227 @SortDimension.SortDirection int direction) {
Ben Kwab8a5e082015-12-07 13:25:27 -0800228 final int count = positions.length;
229 for (int start = 1; start < count; start++) {
230 final int pivotPosition = positions[start];
Ben Kwa6280de02015-12-16 19:42:08 -0800231 final String pivotValue = sortKey[start];
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900232 final boolean pivotIsDir = isDirs[start];
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900233 final String pivotId = ids[start];
Ben Kwab8a5e082015-12-07 13:25:27 -0800234
235 int left = 0;
236 int right = start;
237
238 while (left < right) {
239 int mid = (left + right) >>> 1;
240
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900241 // Directories always go in front.
242 int compare = 0;
243 final boolean rhsIsDir = isDirs[mid];
244 if (pivotIsDir && !rhsIsDir) {
245 compare = -1;
246 } else if (!pivotIsDir && rhsIsDir) {
247 compare = 1;
248 } else {
249 final String lhs = pivotValue;
250 final String rhs = sortKey[mid];
Garfield, Tan11d23482016-08-05 09:33:29 -0700251 switch (direction) {
252 case SortDimension.SORT_DIRECTION_ASCENDING:
253 compare = Shared.compareToIgnoreCaseNullable(lhs, rhs);
254 break;
255 case SortDimension.SORT_DIRECTION_DESCENDING:
256 compare = -Shared.compareToIgnoreCaseNullable(lhs, rhs);
257 break;
258 default:
259 throw new IllegalArgumentException(
260 "Unknown sorting direction: " + direction);
261 }
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900262 }
Ben Kwab8a5e082015-12-07 13:25:27 -0800263
264 if (compare < 0) {
265 right = mid;
266 } else {
267 left = mid + 1;
268 }
269 }
270
271 int n = start - left;
272 switch (n) {
273 case 2:
274 positions[left + 2] = positions[left + 1];
Ben Kwa6280de02015-12-16 19:42:08 -0800275 sortKey[left + 2] = sortKey[left + 1];
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900276 isDirs[left + 2] = isDirs[left + 1];
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900277 ids[left + 2] = ids[left + 1];
Ben Kwab8a5e082015-12-07 13:25:27 -0800278 case 1:
279 positions[left + 1] = positions[left];
Ben Kwa6280de02015-12-16 19:42:08 -0800280 sortKey[left + 1] = sortKey[left];
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900281 isDirs[left + 1] = isDirs[left];
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900282 ids[left + 1] = ids[left];
Ben Kwab8a5e082015-12-07 13:25:27 -0800283 break;
284 default:
285 System.arraycopy(positions, left, positions, left + 1, n);
Ben Kwa6280de02015-12-16 19:42:08 -0800286 System.arraycopy(sortKey, left, sortKey, left + 1, n);
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900287 System.arraycopy(isDirs, left, isDirs, left + 1, n);
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900288 System.arraycopy(ids, left, ids, left + 1, n);
Ben Kwab8a5e082015-12-07 13:25:27 -0800289 }
290
291 positions[left] = pivotPosition;
Ben Kwa6280de02015-12-16 19:42:08 -0800292 sortKey[left] = pivotValue;
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900293 isDirs[left] = pivotIsDir;
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900294 ids[left] = pivotId;
Ben Kwab8a5e082015-12-07 13:25:27 -0800295 }
296 }
297
298 /**
Ben Kwa6280de02015-12-16 19:42:08 -0800299 * Sorts model data. Takes four columns of index-corresponded data. The first column is the sort
300 * key, and the second is an array of mime types. The rows are first bucketed by mime type
301 * (directories vs documents) and then each bucket is sorted independently in descending
302 * numerical order on the sort key. This code is based on TimSort.binarySort().
303 *
304 * @param sortKey Data is sorted in descending numerical order.
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900305 * @param isDirs Array saying whether an item is a directory or not.
Ben Kwa6280de02015-12-16 19:42:08 -0800306 * @param positions Cursor positions to be sorted.
307 * @param ids Model IDs to be sorted.
Ben Kwab8a5e082015-12-07 13:25:27 -0800308 */
Ben Kwa6280de02015-12-16 19:42:08 -0800309 private static void binarySort(
Garfield, Tan11d23482016-08-05 09:33:29 -0700310 long[] sortKey,
311 boolean[] isDirs,
312 int[] positions,
313 String[] ids,
314 @SortDimension.SortDirection int direction) {
Ben Kwab8a5e082015-12-07 13:25:27 -0800315 final int count = positions.length;
316 for (int start = 1; start < count; start++) {
317 final int pivotPosition = positions[start];
Ben Kwa6280de02015-12-16 19:42:08 -0800318 final long pivotValue = sortKey[start];
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900319 final boolean pivotIsDir = isDirs[start];
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900320 final String pivotId = ids[start];
Ben Kwab8a5e082015-12-07 13:25:27 -0800321
322 int left = 0;
323 int right = start;
324
325 while (left < right) {
Ben Kwa6280de02015-12-16 19:42:08 -0800326 int mid = ((left + right) >>> 1);
Ben Kwab8a5e082015-12-07 13:25:27 -0800327
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900328 // Directories always go in front.
Ben Kwa6280de02015-12-16 19:42:08 -0800329 int compare = 0;
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900330 final boolean rhsIsDir = isDirs[mid];
331 if (pivotIsDir && !rhsIsDir) {
Ben Kwa6280de02015-12-16 19:42:08 -0800332 compare = -1;
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900333 } else if (!pivotIsDir && rhsIsDir) {
Ben Kwa6280de02015-12-16 19:42:08 -0800334 compare = 1;
335 } else {
336 final long lhs = pivotValue;
337 final long rhs = sortKey[mid];
Garfield, Tan11d23482016-08-05 09:33:29 -0700338 switch (direction) {
339 case SortDimension.SORT_DIRECTION_ASCENDING:
340 compare = Long.compare(lhs, rhs);
341 break;
342 case SortDimension.SORT_DIRECTION_DESCENDING:
343 compare = -Long.compare(lhs, rhs);
344 break;
345 default:
346 throw new IllegalArgumentException(
347 "Unknown sorting direction: " + direction);
348 }
Ben Kwa6280de02015-12-16 19:42:08 -0800349 }
Ben Kwab8a5e082015-12-07 13:25:27 -0800350
Ben Kwae4338342016-01-08 15:34:47 -0800351 // If numerical comparison yields a tie, use document ID as a tie breaker. This
352 // will yield stable results even if incoming items are continually shuffling and
353 // have identical numerical sort keys. One common example of this scenario is seen
354 // when sorting a set of active downloads by mod time.
355 if (compare == 0) {
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900356 compare = pivotId.compareTo(ids[mid]);
Ben Kwae4338342016-01-08 15:34:47 -0800357 }
358
Ben Kwab8a5e082015-12-07 13:25:27 -0800359 if (compare < 0) {
360 right = mid;
361 } else {
362 left = mid + 1;
363 }
364 }
365
366 int n = start - left;
367 switch (n) {
368 case 2:
369 positions[left + 2] = positions[left + 1];
Ben Kwa6280de02015-12-16 19:42:08 -0800370 sortKey[left + 2] = sortKey[left + 1];
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900371 isDirs[left + 2] = isDirs[left + 1];
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900372 ids[left + 2] = ids[left + 1];
Ben Kwab8a5e082015-12-07 13:25:27 -0800373 case 1:
374 positions[left + 1] = positions[left];
Ben Kwa6280de02015-12-16 19:42:08 -0800375 sortKey[left + 1] = sortKey[left];
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900376 isDirs[left + 1] = isDirs[left];
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900377 ids[left + 1] = ids[left];
Ben Kwab8a5e082015-12-07 13:25:27 -0800378 break;
379 default:
380 System.arraycopy(positions, left, positions, left + 1, n);
Ben Kwa6280de02015-12-16 19:42:08 -0800381 System.arraycopy(sortKey, left, sortKey, left + 1, n);
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900382 System.arraycopy(isDirs, left, isDirs, left + 1, n);
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900383 System.arraycopy(ids, left, ids, left + 1, n);
Ben Kwab8a5e082015-12-07 13:25:27 -0800384 }
385
386 positions[left] = pivotPosition;
Ben Kwa6280de02015-12-16 19:42:08 -0800387 sortKey[left] = pivotValue;
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900388 isDirs[left] = pivotIsDir;
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900389 ids[left] = pivotId;
Ben Kwa0497da82015-11-30 23:00:02 -0800390 }
391 }
392
Ben Kwae4338342016-01-08 15:34:47 -0800393 /**
Tomasz Mikolajewski985df3d2016-03-15 15:38:54 +0900394 * @return Timestamp for the given document. Some docs (e.g. active downloads) have a null
Tomasz Mikolajewski06b036f2016-04-26 11:11:17 +0900395 * timestamp - these will be replaced with MAX_LONG so that such files get sorted to the top
Garfield, Tan11d23482016-08-05 09:33:29 -0700396 * when sorting descending by date.
Tomasz Mikolajewski985df3d2016-03-15 15:38:54 +0900397 */
Tomasz Mikolajewski06b036f2016-04-26 11:11:17 +0900398 long getLastModified(Cursor cursor) {
399 long l = getCursorLong(mCursor, Document.COLUMN_LAST_MODIFIED);
400 return (l == -1) ? Long.MAX_VALUE : l;
Ben Kwae4338342016-01-08 15:34:47 -0800401 }
402
Tomasz Mikolajewskid71bd612016-02-16 12:28:43 +0900403 public @Nullable Cursor getItem(String modelId) {
Ben Kwa0497da82015-11-30 23:00:02 -0800404 Integer pos = mPositions.get(modelId);
Steve McKay5a22a112016-04-12 11:29:10 -0700405 if (pos == null) {
406 if (DEBUG) Log.d(TAG, "Unabled to find cursor position for modelId: " + modelId);
407 return null;
Ben Kwa0497da82015-11-30 23:00:02 -0800408 }
Steve McKay5a22a112016-04-12 11:29:10 -0700409
410 if (!mCursor.moveToPosition(pos)) {
411 if (DEBUG) Log.d(TAG,
412 "Unabled to move cursor to position " + pos + " for modelId: " + modelId);
413 return null;
414 }
415
416 return mCursor;
Ben Kwa0497da82015-11-30 23:00:02 -0800417 }
418
Ben Kwa0497da82015-11-30 23:00:02 -0800419 boolean isEmpty() {
420 return mCursorCount == 0;
421 }
422
423 boolean isLoading() {
424 return mIsLoading;
425 }
426
Steve McKay84769b82016-06-09 10:46:07 -0700427 List<DocumentInfo> getDocuments(Selection selection) {
428 final int size = (selection != null) ? selection.size() : 0;
Ben Kwa0497da82015-11-30 23:00:02 -0800429
430 final List<DocumentInfo> docs = new ArrayList<>(size);
Steve McKay84769b82016-06-09 10:46:07 -0700431 // NOTE: That as this now iterates over only final (non-provisional) selection.
432 for (String modelId: selection) {
Ben Kwad72a1da2015-12-01 19:56:57 -0800433 final Cursor cursor = getItem(modelId);
Steve McKay5a22a112016-04-12 11:29:10 -0700434 if (cursor == null) {
Steve McKay84769b82016-06-09 10:46:07 -0700435 Log.w(TAG, "Skipping document. Unabled to obtain cursor for modelId: " + modelId);
Steve McKay5a22a112016-04-12 11:29:10 -0700436 continue;
437 }
Steve McKay0af8afd2016-02-25 13:34:03 -0800438 docs.add(DocumentInfo.fromDirectoryCursor(cursor));
Ben Kwa0497da82015-11-30 23:00:02 -0800439 }
440 return docs;
441 }
442
Steve McKay84769b82016-06-09 10:46:07 -0700443 public Uri getItemUri(String modelId) {
444 final Cursor cursor = getItem(modelId);
445 return DocumentInfo.getUri(cursor);
446 }
447
Garfield, Tan11d23482016-08-05 09:33:29 -0700448 /**
449 * @return An ordered array of model IDs representing the documents in the model. It is sorted
450 * according to the current sort order, which was set by the last model update.
451 */
452 public String[] getModelIds() {
453 return mIds;
454 }
455
Ben Kwa0497da82015-11-30 23:00:02 -0800456 void addUpdateListener(UpdateListener listener) {
Ben Kwad72a1da2015-12-01 19:56:57 -0800457 mUpdateListeners.add(listener);
Ben Kwa0497da82015-11-30 23:00:02 -0800458 }
459
Ben Kwa472103f2016-02-10 15:48:25 -0800460 void removeUpdateListener(UpdateListener listener) {
461 mUpdateListeners.remove(listener);
462 }
463
Garfield, Tan11d23482016-08-05 09:33:29 -0700464 interface UpdateListener {
Ben Kwa0497da82015-11-30 23:00:02 -0800465 /**
466 * Called when a successful update has occurred.
467 */
Ben Kwad72a1da2015-12-01 19:56:57 -0800468 void onModelUpdate(Model model);
Ben Kwa0497da82015-11-30 23:00:02 -0800469
470 /**
471 * Called when an update has been attempted but failed.
472 */
Ben Kwad72a1da2015-12-01 19:56:57 -0800473 void onModelUpdateFailed(Exception e);
Ben Kwa0497da82015-11-30 23:00:02 -0800474 }
Ben Kwa0497da82015-11-30 23:00:02 -0800475}