blob: ab45a9f39e5a53388f4e2d72c4fef70a1b8887c8 [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;
Ben Kwab8a5e082015-12-07 13:25:27 -080020import static com.android.documentsui.State.SORT_ORDER_DISPLAY_NAME;
21import static com.android.documentsui.State.SORT_ORDER_LAST_MODIFIED;
22import static com.android.documentsui.State.SORT_ORDER_SIZE;
Ben Kwa0497da82015-11-30 23:00:02 -080023
Ben Kwa0497da82015-11-30 23:00:02 -080024import android.database.Cursor;
Tomasz Mikolajewski20c04c52016-03-15 15:55:46 +090025import android.database.MergeCursor;
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;
Ben Kwa0497da82015-11-30 23:00:02 -080038
39import java.util.ArrayList;
Steve McKay5a22a112016-04-12 11:29:10 -070040import java.util.Collections;
Ben Kwa0497da82015-11-30 23:00:02 -080041import java.util.HashMap;
42import java.util.List;
Ben Kwab8a5e082015-12-07 13:25:27 -080043import java.util.Map;
Ben Kwa0497da82015-11-30 23:00:02 -080044
45/**
46 * The data model for the current loaded directory.
47 */
48@VisibleForTesting
Tomasz Mikolajewskid71bd612016-02-16 12:28:43 +090049public class Model {
Ben Kwa0497da82015-11-30 23:00:02 -080050 private static final String TAG = "Model";
Tomasz Mikolajewski985df3d2016-03-15 15:38:54 +090051 private static final String EMPTY = "";
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
61 * by {@link #mSortOrder}
62 */
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +090063 private String mIds[] = new String[0];
Ben Kwab8a5e082015-12-07 13:25:27 -080064 private int mSortOrder = SORT_ORDER_DISPLAY_NAME;
65
Tomasz Mikolajewski985df3d2016-03-15 15:38:54 +090066 private int mAuthorityIndex = -1;
67 private int mDocIdIndex = -1;
68 private int mMimeTypeIndex = -1;
69 private int mDisplayNameIndex = -1;
70 private int mSizeIndex = -1;
71 private int mLastModifiedIndex = -1;
72
Ben Kwa0497da82015-11-30 23:00:02 -080073 @Nullable String info;
74 @Nullable String error;
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +090075 @Nullable DocumentInfo doc;
Ben Kwa0497da82015-11-30 23:00:02 -080076
Ben Kwad72a1da2015-12-01 19:56:57 -080077 private void notifyUpdateListeners() {
78 for (UpdateListener listener: mUpdateListeners) {
79 listener.onModelUpdate(this);
80 }
81 }
82
83 private void notifyUpdateListeners(Exception e) {
84 for (UpdateListener listener: mUpdateListeners) {
85 listener.onModelUpdateFailed(e);
86 }
87 }
88
Ben Kwa0497da82015-11-30 23:00:02 -080089 void update(DirectoryResult result) {
90 if (DEBUG) Log.i(TAG, "Updating model with new result set.");
91
92 if (result == null) {
93 mCursor = null;
94 mCursorCount = 0;
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +090095 mIds = new String[0];
Ben Kwab8a5e082015-12-07 13:25:27 -080096 mPositions.clear();
Ben Kwa0497da82015-11-30 23:00:02 -080097 info = null;
98 error = null;
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +090099 doc = null;
Ben Kwa0497da82015-11-30 23:00:02 -0800100 mIsLoading = false;
Ben Kwad72a1da2015-12-01 19:56:57 -0800101 notifyUpdateListeners();
Ben Kwa0497da82015-11-30 23:00:02 -0800102 return;
103 }
104
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();
Ben Kwab8a5e082015-12-07 13:25:27 -0800113 mSortOrder = result.sortOrder;
Tomasz Mikolajewski985df3d2016-03-15 15:38:54 +0900114 mAuthorityIndex = mCursor.getColumnIndex(RootCursorWrapper.COLUMN_AUTHORITY);
115 assert(mAuthorityIndex != -1);
116 mDocIdIndex = mCursor.getColumnIndex(Document.COLUMN_DOCUMENT_ID);
117 mMimeTypeIndex = mCursor.getColumnIndex(Document.COLUMN_MIME_TYPE);
118 mDisplayNameIndex = mCursor.getColumnIndex(Document.COLUMN_DISPLAY_NAME);
119 mLastModifiedIndex = mCursor.getColumnIndex(Document.COLUMN_LAST_MODIFIED);
120 mSizeIndex = mCursor.getColumnIndex(Document.COLUMN_SIZE);
121
Tomasz Mikolajewskie29e3412016-02-24 12:53:44 +0900122 doc = result.doc;
Ben Kwa0497da82015-11-30 23:00:02 -0800123
Ben Kwab8a5e082015-12-07 13:25:27 -0800124 updateModelData();
Ben Kwa0497da82015-11-30 23:00:02 -0800125
126 final Bundle extras = mCursor.getExtras();
127 if (extras != null) {
128 info = extras.getString(DocumentsContract.EXTRA_INFO);
129 error = extras.getString(DocumentsContract.EXTRA_ERROR);
130 mIsLoading = extras.getBoolean(DocumentsContract.EXTRA_LOADING, false);
131 }
132
Ben Kwad72a1da2015-12-01 19:56:57 -0800133 notifyUpdateListeners();
Ben Kwa0497da82015-11-30 23:00:02 -0800134 }
135
Ben Kwad72a1da2015-12-01 19:56:57 -0800136 @VisibleForTesting
Ben Kwa0497da82015-11-30 23:00:02 -0800137 int getItemCount() {
Ben Kwada858bf2015-12-09 14:33:49 -0800138 return mCursorCount;
Ben Kwa0497da82015-11-30 23:00:02 -0800139 }
140
141 /**
Ben Kwab8a5e082015-12-07 13:25:27 -0800142 * Scan over the incoming cursor data, generate Model IDs for each row, and sort the IDs
143 * according to the current sort order.
Ben Kwa0497da82015-11-30 23:00:02 -0800144 */
Ben Kwab8a5e082015-12-07 13:25:27 -0800145 private void updateModelData() {
146 int[] positions = new int[mCursorCount];
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900147 mIds = new String[mCursorCount];
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900148 boolean[] isDirs = new boolean[mCursorCount];
149 String[] displayNames = null;
Ben Kwa6280de02015-12-16 19:42:08 -0800150 long[] longValues = null;
Ben Kwab8a5e082015-12-07 13:25:27 -0800151
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900152 switch (mSortOrder) {
153 case SORT_ORDER_DISPLAY_NAME:
154 displayNames = new String[mCursorCount];
155 break;
156 case SORT_ORDER_LAST_MODIFIED:
157 case SORT_ORDER_SIZE:
158 longValues = new long[mCursorCount];
159 break;
Ben Kwab8a5e082015-12-07 13:25:27 -0800160 }
161
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900162 String mimeType;
163
Ben Kwa0497da82015-11-30 23:00:02 -0800164 mCursor.moveToPosition(-1);
165 for (int pos = 0; pos < mCursorCount; ++pos) {
Ben Lin2df30e52016-04-22 16:12:50 -0700166 if (!mCursor.moveToNext()) {
167 Log.e(TAG, "Fail to move cursor to next pos: " + pos);
168 return;
169 }
Ben Kwab8a5e082015-12-07 13:25:27 -0800170 positions[pos] = pos;
Ben Kwab8a5e082015-12-07 13:25:27 -0800171
Tomasz Mikolajewski985df3d2016-03-15 15:38:54 +0900172 // Generates a Model ID for a cursor entry that refers to a document. The Model ID is a
173 // unique string that can be used to identify the document referred to by the cursor.
Tomasz Mikolajewski20c04c52016-03-15 15:55:46 +0900174 // If the cursor is a merged cursor over multiple authorities, then prefix the ids
175 // with the authority to avoid collisions.
176 if (mCursor instanceof MergeCursor) {
177 mIds[pos] = getStringOrEmpty(mAuthorityIndex) + "|" + getStringOrEmpty(mDocIdIndex);
178 } else {
179 mIds[pos] = getStringOrEmpty(mDocIdIndex);
180 }
Tomasz Mikolajewski985df3d2016-03-15 15:38:54 +0900181
182 mimeType = getStringOrEmpty(mMimeTypeIndex);
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900183 isDirs[pos] = Document.MIME_TYPE_DIR.equals(mimeType);
184
Tomasz Mikolajewski985df3d2016-03-15 15:38:54 +0900185 switch (mSortOrder) {
Ben Kwab8a5e082015-12-07 13:25:27 -0800186 case SORT_ORDER_DISPLAY_NAME:
Tomasz Mikolajewski985df3d2016-03-15 15:38:54 +0900187 displayNames[pos] = getStringOrEmpty(mDisplayNameIndex);
Ben Kwab8a5e082015-12-07 13:25:27 -0800188 break;
189 case SORT_ORDER_LAST_MODIFIED:
Tomasz Mikolajewski985df3d2016-03-15 15:38:54 +0900190 longValues[pos] = getLastModified();
Ben Kwab8a5e082015-12-07 13:25:27 -0800191 break;
192 case SORT_ORDER_SIZE:
Tomasz Mikolajewski985df3d2016-03-15 15:38:54 +0900193 longValues[pos] = getDocSize();
Ben Kwab8a5e082015-12-07 13:25:27 -0800194 break;
195 }
196 }
197
198 switch (mSortOrder) {
199 case SORT_ORDER_DISPLAY_NAME:
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900200 binarySort(displayNames, isDirs, positions, mIds);
Ben Kwab8a5e082015-12-07 13:25:27 -0800201 break;
202 case SORT_ORDER_LAST_MODIFIED:
203 case SORT_ORDER_SIZE:
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900204 binarySort(longValues, isDirs, positions, mIds);
Ben Kwab8a5e082015-12-07 13:25:27 -0800205 break;
206 }
207
208 // Populate the positions.
209 mPositions.clear();
210 for (int i = 0; i < mCursorCount; ++i) {
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900211 mPositions.put(mIds[i], positions[i]);
Ben Kwab8a5e082015-12-07 13:25:27 -0800212 }
213 }
214
215 /**
Ben Kwa6280de02015-12-16 19:42:08 -0800216 * Sorts model data. Takes three columns of index-corresponded data. The first column is the
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900217 * sort key. Rows are sorted in ascending alphabetical order on the sort key.
218 * Directories are always shown first. This code is based on TimSort.binarySort().
Ben Kwa6280de02015-12-16 19:42:08 -0800219 *
220 * @param sortKey Data is sorted in ascending alphabetical order.
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900221 * @param isDirs Array saying whether an item is a directory or not.
Ben Kwa6280de02015-12-16 19:42:08 -0800222 * @param positions Cursor positions to be sorted.
223 * @param ids Model IDs to be sorted.
Ben Kwab8a5e082015-12-07 13:25:27 -0800224 */
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900225 private static void binarySort(String[] sortKey, boolean[] isDirs, int[] positions, String[] ids) {
Ben Kwab8a5e082015-12-07 13:25:27 -0800226 final int count = positions.length;
227 for (int start = 1; start < count; start++) {
228 final int pivotPosition = positions[start];
Ben Kwa6280de02015-12-16 19:42:08 -0800229 final String pivotValue = sortKey[start];
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900230 final boolean pivotIsDir = isDirs[start];
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900231 final String pivotId = ids[start];
Ben Kwab8a5e082015-12-07 13:25:27 -0800232
233 int left = 0;
234 int right = start;
235
236 while (left < right) {
237 int mid = (left + right) >>> 1;
238
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900239 // Directories always go in front.
240 int compare = 0;
241 final boolean rhsIsDir = isDirs[mid];
242 if (pivotIsDir && !rhsIsDir) {
243 compare = -1;
244 } else if (!pivotIsDir && rhsIsDir) {
245 compare = 1;
246 } else {
247 final String lhs = pivotValue;
248 final String rhs = sortKey[mid];
Tomasz Mikolajewski985df3d2016-03-15 15:38:54 +0900249 compare = Shared.compareToIgnoreCase(lhs, rhs);
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900250 }
Ben Kwab8a5e082015-12-07 13:25:27 -0800251
252 if (compare < 0) {
253 right = mid;
254 } else {
255 left = mid + 1;
256 }
257 }
258
259 int n = start - left;
260 switch (n) {
261 case 2:
262 positions[left + 2] = positions[left + 1];
Ben Kwa6280de02015-12-16 19:42:08 -0800263 sortKey[left + 2] = sortKey[left + 1];
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900264 isDirs[left + 2] = isDirs[left + 1];
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900265 ids[left + 2] = ids[left + 1];
Ben Kwab8a5e082015-12-07 13:25:27 -0800266 case 1:
267 positions[left + 1] = positions[left];
Ben Kwa6280de02015-12-16 19:42:08 -0800268 sortKey[left + 1] = sortKey[left];
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900269 isDirs[left + 1] = isDirs[left];
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900270 ids[left + 1] = ids[left];
Ben Kwab8a5e082015-12-07 13:25:27 -0800271 break;
272 default:
273 System.arraycopy(positions, left, positions, left + 1, n);
Ben Kwa6280de02015-12-16 19:42:08 -0800274 System.arraycopy(sortKey, left, sortKey, left + 1, n);
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900275 System.arraycopy(isDirs, left, isDirs, left + 1, n);
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900276 System.arraycopy(ids, left, ids, left + 1, n);
Ben Kwab8a5e082015-12-07 13:25:27 -0800277 }
278
279 positions[left] = pivotPosition;
Ben Kwa6280de02015-12-16 19:42:08 -0800280 sortKey[left] = pivotValue;
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900281 isDirs[left] = pivotIsDir;
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900282 ids[left] = pivotId;
Ben Kwab8a5e082015-12-07 13:25:27 -0800283 }
284 }
285
286 /**
Ben Kwa6280de02015-12-16 19:42:08 -0800287 * Sorts model data. Takes four columns of index-corresponded data. The first column is the sort
288 * key, and the second is an array of mime types. The rows are first bucketed by mime type
289 * (directories vs documents) and then each bucket is sorted independently in descending
290 * numerical order on the sort key. This code is based on TimSort.binarySort().
291 *
292 * @param sortKey Data is sorted in descending numerical order.
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900293 * @param isDirs Array saying whether an item is a directory or not.
Ben Kwa6280de02015-12-16 19:42:08 -0800294 * @param positions Cursor positions to be sorted.
295 * @param ids Model IDs to be sorted.
Ben Kwab8a5e082015-12-07 13:25:27 -0800296 */
Ben Kwa6280de02015-12-16 19:42:08 -0800297 private static void binarySort(
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900298 long[] sortKey, boolean[] isDirs, int[] positions, String[] ids) {
Ben Kwab8a5e082015-12-07 13:25:27 -0800299 final int count = positions.length;
300 for (int start = 1; start < count; start++) {
301 final int pivotPosition = positions[start];
Ben Kwa6280de02015-12-16 19:42:08 -0800302 final long pivotValue = sortKey[start];
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900303 final boolean pivotIsDir = isDirs[start];
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900304 final String pivotId = ids[start];
Ben Kwab8a5e082015-12-07 13:25:27 -0800305
306 int left = 0;
307 int right = start;
308
309 while (left < right) {
Ben Kwa6280de02015-12-16 19:42:08 -0800310 int mid = ((left + right) >>> 1);
Ben Kwab8a5e082015-12-07 13:25:27 -0800311
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900312 // Directories always go in front.
Ben Kwa6280de02015-12-16 19:42:08 -0800313 int compare = 0;
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900314 final boolean rhsIsDir = isDirs[mid];
315 if (pivotIsDir && !rhsIsDir) {
Ben Kwa6280de02015-12-16 19:42:08 -0800316 compare = -1;
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900317 } else if (!pivotIsDir && rhsIsDir) {
Ben Kwa6280de02015-12-16 19:42:08 -0800318 compare = 1;
319 } else {
320 final long lhs = pivotValue;
321 final long rhs = sortKey[mid];
Ben Kwae4338342016-01-08 15:34:47 -0800322 // Sort in descending numerical order. This matches legacy behaviour, which
323 // yields largest or most recent items on top.
Ben Kwa6280de02015-12-16 19:42:08 -0800324 compare = -Long.compare(lhs, rhs);
325 }
Ben Kwab8a5e082015-12-07 13:25:27 -0800326
Ben Kwae4338342016-01-08 15:34:47 -0800327 // If numerical comparison yields a tie, use document ID as a tie breaker. This
328 // will yield stable results even if incoming items are continually shuffling and
329 // have identical numerical sort keys. One common example of this scenario is seen
330 // when sorting a set of active downloads by mod time.
331 if (compare == 0) {
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900332 compare = pivotId.compareTo(ids[mid]);
Ben Kwae4338342016-01-08 15:34:47 -0800333 }
334
Ben Kwab8a5e082015-12-07 13:25:27 -0800335 if (compare < 0) {
336 right = mid;
337 } else {
338 left = mid + 1;
339 }
340 }
341
342 int n = start - left;
343 switch (n) {
344 case 2:
345 positions[left + 2] = positions[left + 1];
Ben Kwa6280de02015-12-16 19:42:08 -0800346 sortKey[left + 2] = sortKey[left + 1];
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900347 isDirs[left + 2] = isDirs[left + 1];
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900348 ids[left + 2] = ids[left + 1];
Ben Kwab8a5e082015-12-07 13:25:27 -0800349 case 1:
350 positions[left + 1] = positions[left];
Ben Kwa6280de02015-12-16 19:42:08 -0800351 sortKey[left + 1] = sortKey[left];
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900352 isDirs[left + 1] = isDirs[left];
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900353 ids[left + 1] = ids[left];
Ben Kwab8a5e082015-12-07 13:25:27 -0800354 break;
355 default:
356 System.arraycopy(positions, left, positions, left + 1, n);
Ben Kwa6280de02015-12-16 19:42:08 -0800357 System.arraycopy(sortKey, left, sortKey, left + 1, n);
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900358 System.arraycopy(isDirs, left, isDirs, left + 1, n);
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900359 System.arraycopy(ids, left, ids, left + 1, n);
Ben Kwab8a5e082015-12-07 13:25:27 -0800360 }
361
362 positions[left] = pivotPosition;
Ben Kwa6280de02015-12-16 19:42:08 -0800363 sortKey[left] = pivotValue;
Tomasz Mikolajewski61e315d2016-03-15 13:23:52 +0900364 isDirs[left] = pivotIsDir;
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900365 ids[left] = pivotId;
Ben Kwa0497da82015-11-30 23:00:02 -0800366 }
367 }
368
Ben Kwae4338342016-01-08 15:34:47 -0800369 /**
Tomasz Mikolajewski985df3d2016-03-15 15:38:54 +0900370 * @return Value of the string column, or an empty string if no value, or empty value.
Ben Kwae4338342016-01-08 15:34:47 -0800371 */
Tomasz Mikolajewski985df3d2016-03-15 15:38:54 +0900372 private String getStringOrEmpty(int columnIndex) {
373 if (columnIndex == -1)
374 return EMPTY;
375 final String result = mCursor.getString(columnIndex);
376 return result != null ? result : EMPTY;
377 }
378
379 /**
380 * @return Timestamp for the given document. Some docs (e.g. active downloads) have a null
381 * or missing timestamp - these will be replaced with MAX_LONG so that such files get sorted to
382 * the top when sorting by date.
383 */
384 private long getLastModified() {
385 if (mLastModifiedIndex == -1)
386 return Long.MAX_VALUE;
387 try {
388 final long result = mCursor.getLong(mLastModifiedIndex);
389 return result > 0 ? result : Long.MAX_VALUE;
390 } catch (NumberFormatException e) {
391 return Long.MAX_VALUE;
392 }
393 }
394
395 /**
396 * @return Size for the given document. If the size is unknown or invalid, returns 0.
397 */
398 private long getDocSize() {
399 if (mSizeIndex == -1)
400 return 0;
401 try {
402 return mCursor.getLong(mSizeIndex);
403 } catch (NumberFormatException e) {
404 return 0;
405 }
Ben Kwae4338342016-01-08 15:34:47 -0800406 }
407
Tomasz Mikolajewskid71bd612016-02-16 12:28:43 +0900408 public @Nullable Cursor getItem(String modelId) {
Ben Kwa0497da82015-11-30 23:00:02 -0800409 Integer pos = mPositions.get(modelId);
Steve McKay5a22a112016-04-12 11:29:10 -0700410 if (pos == null) {
411 if (DEBUG) Log.d(TAG, "Unabled to find cursor position for modelId: " + modelId);
412 return null;
Ben Kwa0497da82015-11-30 23:00:02 -0800413 }
Steve McKay5a22a112016-04-12 11:29:10 -0700414
415 if (!mCursor.moveToPosition(pos)) {
416 if (DEBUG) Log.d(TAG,
417 "Unabled to move cursor to position " + pos + " for modelId: " + modelId);
418 return null;
419 }
420
421 return mCursor;
Ben Kwa0497da82015-11-30 23:00:02 -0800422 }
423
Ben Kwa0497da82015-11-30 23:00:02 -0800424 boolean isEmpty() {
425 return mCursorCount == 0;
426 }
427
428 boolean isLoading() {
429 return mIsLoading;
430 }
431
432 List<DocumentInfo> getDocuments(Selection items) {
433 final int size = (items != null) ? items.size() : 0;
434
435 final List<DocumentInfo> docs = new ArrayList<>(size);
Ben Kwad72a1da2015-12-01 19:56:57 -0800436 for (String modelId: items.getAll()) {
437 final Cursor cursor = getItem(modelId);
Steve McKay5a22a112016-04-12 11:29:10 -0700438 if (cursor == null) {
439 Log.w(TAG,
440 "Skipping document. Unabled to obtain cursor for modelId: " + modelId);
441 continue;
442 }
Steve McKay0af8afd2016-02-25 13:34:03 -0800443 docs.add(DocumentInfo.fromDirectoryCursor(cursor));
Ben Kwa0497da82015-11-30 23:00:02 -0800444 }
445 return docs;
446 }
447
Ben Kwa0497da82015-11-30 23:00:02 -0800448 void addUpdateListener(UpdateListener listener) {
Ben Kwad72a1da2015-12-01 19:56:57 -0800449 mUpdateListeners.add(listener);
Ben Kwa0497da82015-11-30 23:00:02 -0800450 }
451
Ben Kwa472103f2016-02-10 15:48:25 -0800452 void removeUpdateListener(UpdateListener listener) {
453 mUpdateListeners.remove(listener);
454 }
455
Ben Kwad72a1da2015-12-01 19:56:57 -0800456 static interface UpdateListener {
Ben Kwa0497da82015-11-30 23:00:02 -0800457 /**
458 * Called when a successful update has occurred.
459 */
Ben Kwad72a1da2015-12-01 19:56:57 -0800460 void onModelUpdate(Model model);
Ben Kwa0497da82015-11-30 23:00:02 -0800461
462 /**
463 * Called when an update has been attempted but failed.
464 */
Ben Kwad72a1da2015-12-01 19:56:57 -0800465 void onModelUpdateFailed(Exception e);
Ben Kwa0497da82015-11-30 23:00:02 -0800466 }
Ben Kwab8a5e082015-12-07 13:25:27 -0800467
468 /**
469 * @return An ordered array of model IDs representing the documents in the model. It is sorted
470 * according to the current sort order, which was set by the last model update.
471 */
Tomasz Mikolajewskif27c2742016-03-07 18:01:45 +0900472 public String[] getModelIds() {
Ben Kwab8a5e082015-12-07 13:25:27 -0800473 return mIds;
474 }
Ben Kwa0497da82015-11-30 23:00:02 -0800475}