Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.documentsui.dirlist; |
| 18 | |
| 19 | import static com.android.documentsui.Shared.DEBUG; |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 20 | import static com.android.documentsui.State.SORT_ORDER_DISPLAY_NAME; |
| 21 | import static com.android.documentsui.State.SORT_ORDER_LAST_MODIFIED; |
| 22 | import static com.android.documentsui.State.SORT_ORDER_SIZE; |
| 23 | import static com.android.documentsui.model.DocumentInfo.getCursorLong; |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 24 | import static com.android.documentsui.model.DocumentInfo.getCursorString; |
| 25 | import static com.android.internal.util.Preconditions.checkNotNull; |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 26 | |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 27 | import android.database.Cursor; |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 28 | import android.os.Bundle; |
| 29 | import android.os.Looper; |
| 30 | import android.provider.DocumentsContract; |
| 31 | import android.provider.DocumentsContract.Document; |
| 32 | import android.support.annotation.Nullable; |
| 33 | import android.support.annotation.VisibleForTesting; |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 34 | import android.util.Log; |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 35 | |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 36 | import com.android.documentsui.DirectoryResult; |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 37 | import com.android.documentsui.RootCursorWrapper; |
Steve McKay | 008e948 | 2016-02-18 15:32:16 -0800 | [diff] [blame] | 38 | import com.android.documentsui.Shared; |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 39 | import com.android.documentsui.dirlist.MultiSelectManager.Selection; |
| 40 | import com.android.documentsui.model.DocumentInfo; |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 41 | |
| 42 | import java.util.ArrayList; |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 43 | import java.util.HashMap; |
| 44 | import java.util.List; |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 45 | import java.util.Map; |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * The data model for the current loaded directory. |
| 49 | */ |
| 50 | @VisibleForTesting |
Tomasz Mikolajewski | 3d988a9 | 2016-02-16 12:28:43 +0900 | [diff] [blame] | 51 | public class Model { |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 52 | private static final String TAG = "Model"; |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 53 | |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 54 | private boolean mIsLoading; |
Ben Kwa | 743c7c2 | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 55 | private List<UpdateListener> mUpdateListeners = new ArrayList<>(); |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 56 | @Nullable private Cursor mCursor; |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 57 | private int mCursorCount; |
| 58 | /** Maps Model ID to cursor positions, for looking up items by Model ID. */ |
| 59 | private Map<String, Integer> mPositions = new HashMap<>(); |
| 60 | /** |
| 61 | * A sorted array of model IDs for the files currently in the Model. Sort order is determined |
| 62 | * by {@link #mSortOrder} |
| 63 | */ |
| 64 | private List<String> mIds = new ArrayList<>(); |
| 65 | private int mSortOrder = SORT_ORDER_DISPLAY_NAME; |
| 66 | |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 67 | @Nullable String info; |
| 68 | @Nullable String error; |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 69 | |
Ben Kwa | 743c7c2 | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 70 | /** |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 71 | * Generates a Model ID for a cursor entry that refers to a document. The Model ID is a unique |
| 72 | * string that can be used to identify the document referred to by the cursor. |
Ben Kwa | 743c7c2 | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 73 | * |
| 74 | * @param c A cursor that refers to a document. |
| 75 | */ |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 76 | private static String createModelId(Cursor c) { |
| 77 | // TODO: Maybe more efficient to use just the document ID, in cases where there is only one |
| 78 | // authority (which should be the majority of cases). |
Steve McKay | 955e46d | 2016-01-05 12:53:35 -0800 | [diff] [blame] | 79 | return createModelId( |
| 80 | getCursorString(c, RootCursorWrapper.COLUMN_AUTHORITY), |
| 81 | getCursorString(c, Document.COLUMN_DOCUMENT_ID)); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Generates a Model ID for a cursor entry that refers to a document. The Model ID is a unique |
| 86 | * string that can be used to identify the document referred to by the cursor. |
| 87 | * |
| 88 | * @param c A cursor that refers to a document. |
| 89 | */ |
| 90 | static String createModelId(String authority, String docId) { |
| 91 | return authority + "|" + docId; |
Ben Kwa | 743c7c2 | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Ben Kwa | 743c7c2 | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 94 | private void notifyUpdateListeners() { |
| 95 | for (UpdateListener listener: mUpdateListeners) { |
| 96 | listener.onModelUpdate(this); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | private void notifyUpdateListeners(Exception e) { |
| 101 | for (UpdateListener listener: mUpdateListeners) { |
| 102 | listener.onModelUpdateFailed(e); |
| 103 | } |
| 104 | } |
| 105 | |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 106 | void update(DirectoryResult result) { |
| 107 | if (DEBUG) Log.i(TAG, "Updating model with new result set."); |
| 108 | |
| 109 | if (result == null) { |
| 110 | mCursor = null; |
| 111 | mCursorCount = 0; |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 112 | mIds.clear(); |
| 113 | mPositions.clear(); |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 114 | info = null; |
| 115 | error = null; |
| 116 | mIsLoading = false; |
Ben Kwa | 743c7c2 | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 117 | notifyUpdateListeners(); |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 118 | return; |
| 119 | } |
| 120 | |
| 121 | if (result.exception != null) { |
| 122 | Log.e(TAG, "Error while loading directory contents", result.exception); |
Ben Kwa | 743c7c2 | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 123 | notifyUpdateListeners(result.exception); |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 124 | return; |
| 125 | } |
| 126 | |
| 127 | mCursor = result.cursor; |
| 128 | mCursorCount = mCursor.getCount(); |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 129 | mSortOrder = result.sortOrder; |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 130 | |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 131 | updateModelData(); |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 132 | |
| 133 | final Bundle extras = mCursor.getExtras(); |
| 134 | if (extras != null) { |
| 135 | info = extras.getString(DocumentsContract.EXTRA_INFO); |
| 136 | error = extras.getString(DocumentsContract.EXTRA_ERROR); |
| 137 | mIsLoading = extras.getBoolean(DocumentsContract.EXTRA_LOADING, false); |
| 138 | } |
| 139 | |
Ben Kwa | 743c7c2 | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 140 | notifyUpdateListeners(); |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Ben Kwa | 743c7c2 | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 143 | @VisibleForTesting |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 144 | int getItemCount() { |
Ben Kwa | db65cd5 | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 145 | return mCursorCount; |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 149 | * Scan over the incoming cursor data, generate Model IDs for each row, and sort the IDs |
| 150 | * according to the current sort order. |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 151 | */ |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 152 | private void updateModelData() { |
| 153 | int[] positions = new int[mCursorCount]; |
| 154 | mIds.clear(); |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 155 | String[] stringValues = new String[mCursorCount]; |
| 156 | long[] longValues = null; |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 157 | |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 158 | if (mSortOrder == SORT_ORDER_LAST_MODIFIED || mSortOrder == SORT_ORDER_SIZE) { |
| 159 | longValues = new long[mCursorCount]; |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 162 | mCursor.moveToPosition(-1); |
| 163 | for (int pos = 0; pos < mCursorCount; ++pos) { |
| 164 | mCursor.moveToNext(); |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 165 | positions[pos] = pos; |
| 166 | mIds.add(createModelId(mCursor)); |
| 167 | |
| 168 | switch(mSortOrder) { |
| 169 | case SORT_ORDER_DISPLAY_NAME: |
| 170 | final String mimeType = getCursorString(mCursor, Document.COLUMN_MIME_TYPE); |
| 171 | final String displayName = getCursorString( |
| 172 | mCursor, Document.COLUMN_DISPLAY_NAME); |
| 173 | if (Document.MIME_TYPE_DIR.equals(mimeType)) { |
Steve McKay | 008e948 | 2016-02-18 15:32:16 -0800 | [diff] [blame] | 174 | stringValues[pos] = Shared.DIR_PREFIX + displayName; |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 175 | } else { |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 176 | stringValues[pos] = displayName; |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 177 | } |
| 178 | break; |
| 179 | case SORT_ORDER_LAST_MODIFIED: |
Ben Kwa | e2564f9 | 2016-01-08 15:34:47 -0800 | [diff] [blame] | 180 | longValues[pos] = getLastModified(mCursor); |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 181 | stringValues[pos] = getCursorString(mCursor, Document.COLUMN_MIME_TYPE); |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 182 | break; |
| 183 | case SORT_ORDER_SIZE: |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 184 | longValues[pos] = getCursorLong(mCursor, Document.COLUMN_SIZE); |
| 185 | stringValues[pos] = getCursorString(mCursor, Document.COLUMN_MIME_TYPE); |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 186 | break; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | switch (mSortOrder) { |
| 191 | case SORT_ORDER_DISPLAY_NAME: |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 192 | binarySort(stringValues, positions, mIds); |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 193 | break; |
| 194 | case SORT_ORDER_LAST_MODIFIED: |
| 195 | case SORT_ORDER_SIZE: |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 196 | binarySort(longValues, stringValues, positions, mIds); |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 197 | break; |
| 198 | } |
| 199 | |
| 200 | // Populate the positions. |
| 201 | mPositions.clear(); |
| 202 | for (int i = 0; i < mCursorCount; ++i) { |
| 203 | mPositions.put(mIds.get(i), positions[i]); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 208 | * Sorts model data. Takes three columns of index-corresponded data. The first column is the |
| 209 | * sort key. Rows are sorted in ascending alphabetical order on the sort key. This code is based |
| 210 | * on TimSort.binarySort(). |
| 211 | * |
| 212 | * @param sortKey Data is sorted in ascending alphabetical order. |
| 213 | * @param positions Cursor positions to be sorted. |
| 214 | * @param ids Model IDs to be sorted. |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 215 | */ |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 216 | private static void binarySort(String[] sortKey, int[] positions, List<String> ids) { |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 217 | final int count = positions.length; |
| 218 | for (int start = 1; start < count; start++) { |
| 219 | final int pivotPosition = positions[start]; |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 220 | final String pivotValue = sortKey[start]; |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 221 | final String pivotId = ids.get(start); |
| 222 | |
| 223 | int left = 0; |
| 224 | int right = start; |
| 225 | |
| 226 | while (left < right) { |
| 227 | int mid = (left + right) >>> 1; |
| 228 | |
| 229 | final String lhs = pivotValue; |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 230 | final String rhs = sortKey[mid]; |
Steve McKay | 008e948 | 2016-02-18 15:32:16 -0800 | [diff] [blame] | 231 | final int compare = Shared.compareToIgnoreCaseNullable(lhs, rhs); |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 232 | |
| 233 | if (compare < 0) { |
| 234 | right = mid; |
| 235 | } else { |
| 236 | left = mid + 1; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | int n = start - left; |
| 241 | switch (n) { |
| 242 | case 2: |
| 243 | positions[left + 2] = positions[left + 1]; |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 244 | sortKey[left + 2] = sortKey[left + 1]; |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 245 | ids.set(left + 2, ids.get(left + 1)); |
| 246 | case 1: |
| 247 | positions[left + 1] = positions[left]; |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 248 | sortKey[left + 1] = sortKey[left]; |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 249 | ids.set(left + 1, ids.get(left)); |
| 250 | break; |
| 251 | default: |
| 252 | System.arraycopy(positions, left, positions, left + 1, n); |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 253 | System.arraycopy(sortKey, left, sortKey, left + 1, n); |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 254 | for (int i = n; i >= 1; --i) { |
| 255 | ids.set(left + i, ids.get(left + i - 1)); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | positions[left] = pivotPosition; |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 260 | sortKey[left] = pivotValue; |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 261 | ids.set(left, pivotId); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /** |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 266 | * Sorts model data. Takes four columns of index-corresponded data. The first column is the sort |
| 267 | * key, and the second is an array of mime types. The rows are first bucketed by mime type |
| 268 | * (directories vs documents) and then each bucket is sorted independently in descending |
| 269 | * numerical order on the sort key. This code is based on TimSort.binarySort(). |
| 270 | * |
| 271 | * @param sortKey Data is sorted in descending numerical order. |
| 272 | * @param mimeTypes Corresponding mime types. Directories will be sorted ahead of documents. |
| 273 | * @param positions Cursor positions to be sorted. |
| 274 | * @param ids Model IDs to be sorted. |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 275 | */ |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 276 | private static void binarySort( |
| 277 | long[] sortKey, String[] mimeTypes, int[] positions, List<String> ids) { |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 278 | final int count = positions.length; |
| 279 | for (int start = 1; start < count; start++) { |
| 280 | final int pivotPosition = positions[start]; |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 281 | final long pivotValue = sortKey[start]; |
| 282 | final String pivotMime = mimeTypes[start]; |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 283 | final String pivotId = ids.get(start); |
| 284 | |
| 285 | int left = 0; |
| 286 | int right = start; |
| 287 | |
| 288 | while (left < right) { |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 289 | int mid = ((left + right) >>> 1); |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 290 | |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 291 | // First bucket by mime type. Directories always go in front. |
| 292 | int compare = 0; |
| 293 | final boolean lhsIsDir = Document.MIME_TYPE_DIR.equals(pivotMime); |
| 294 | final boolean rhsIsDir = Document.MIME_TYPE_DIR.equals(mimeTypes[mid]); |
| 295 | if (lhsIsDir && !rhsIsDir) { |
| 296 | compare = -1; |
| 297 | } else if (!lhsIsDir && rhsIsDir) { |
| 298 | compare = 1; |
| 299 | } else { |
| 300 | final long lhs = pivotValue; |
| 301 | final long rhs = sortKey[mid]; |
Ben Kwa | e2564f9 | 2016-01-08 15:34:47 -0800 | [diff] [blame] | 302 | // Sort in descending numerical order. This matches legacy behaviour, which |
| 303 | // yields largest or most recent items on top. |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 304 | compare = -Long.compare(lhs, rhs); |
| 305 | } |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 306 | |
Ben Kwa | e2564f9 | 2016-01-08 15:34:47 -0800 | [diff] [blame] | 307 | // If numerical comparison yields a tie, use document ID as a tie breaker. This |
| 308 | // will yield stable results even if incoming items are continually shuffling and |
| 309 | // have identical numerical sort keys. One common example of this scenario is seen |
| 310 | // when sorting a set of active downloads by mod time. |
| 311 | if (compare == 0) { |
| 312 | compare = pivotId.compareTo(ids.get(mid)); |
| 313 | } |
| 314 | |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 315 | if (compare < 0) { |
| 316 | right = mid; |
| 317 | } else { |
| 318 | left = mid + 1; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | int n = start - left; |
| 323 | switch (n) { |
| 324 | case 2: |
| 325 | positions[left + 2] = positions[left + 1]; |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 326 | sortKey[left + 2] = sortKey[left + 1]; |
| 327 | mimeTypes[left + 2] = mimeTypes[left + 1]; |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 328 | ids.set(left + 2, ids.get(left + 1)); |
| 329 | case 1: |
| 330 | positions[left + 1] = positions[left]; |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 331 | sortKey[left + 1] = sortKey[left]; |
| 332 | mimeTypes[left + 1] = mimeTypes[left]; |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 333 | ids.set(left + 1, ids.get(left)); |
| 334 | break; |
| 335 | default: |
| 336 | System.arraycopy(positions, left, positions, left + 1, n); |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 337 | System.arraycopy(sortKey, left, sortKey, left + 1, n); |
| 338 | System.arraycopy(mimeTypes, left, mimeTypes, left + 1, n); |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 339 | for (int i = n; i >= 1; --i) { |
| 340 | ids.set(left + i, ids.get(left + i - 1)); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | positions[left] = pivotPosition; |
Ben Kwa | c72a2cb | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 345 | sortKey[left] = pivotValue; |
| 346 | mimeTypes[left] = pivotMime; |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 347 | ids.set(left, pivotId); |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
Ben Kwa | e2564f9 | 2016-01-08 15:34:47 -0800 | [diff] [blame] | 351 | /** |
| 352 | * @return Timestamp for the given document. Some docs (e.g. active downloads) have a null |
| 353 | * timestamp - these will be replaced with MAX_LONG so that such files get sorted to the top |
| 354 | * when sorting by date. |
| 355 | */ |
| 356 | long getLastModified(Cursor cursor) { |
| 357 | long l = getCursorLong(mCursor, Document.COLUMN_LAST_MODIFIED); |
| 358 | return (l == -1) ? Long.MAX_VALUE : l; |
| 359 | } |
| 360 | |
Tomasz Mikolajewski | 3d988a9 | 2016-02-16 12:28:43 +0900 | [diff] [blame] | 361 | public @Nullable Cursor getItem(String modelId) { |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 362 | Integer pos = mPositions.get(modelId); |
| 363 | if (pos != null) { |
| 364 | mCursor.moveToPosition(pos); |
| 365 | return mCursor; |
| 366 | } |
| 367 | return null; |
| 368 | } |
| 369 | |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 370 | boolean isEmpty() { |
| 371 | return mCursorCount == 0; |
| 372 | } |
| 373 | |
| 374 | boolean isLoading() { |
| 375 | return mIsLoading; |
| 376 | } |
| 377 | |
| 378 | List<DocumentInfo> getDocuments(Selection items) { |
| 379 | final int size = (items != null) ? items.size() : 0; |
| 380 | |
| 381 | final List<DocumentInfo> docs = new ArrayList<>(size); |
Ben Kwa | 743c7c2 | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 382 | for (String modelId: items.getAll()) { |
| 383 | final Cursor cursor = getItem(modelId); |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 384 | checkNotNull(cursor, "Cursor cannot be null."); |
| 385 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor); |
| 386 | docs.add(doc); |
| 387 | } |
| 388 | return docs; |
| 389 | } |
| 390 | |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 391 | void addUpdateListener(UpdateListener listener) { |
Ben Kwa | 743c7c2 | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 392 | mUpdateListeners.add(listener); |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Ben Kwa | a4acc90 | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 395 | void removeUpdateListener(UpdateListener listener) { |
| 396 | mUpdateListeners.remove(listener); |
| 397 | } |
| 398 | |
Ben Kwa | 743c7c2 | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 399 | static interface UpdateListener { |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 400 | /** |
| 401 | * Called when a successful update has occurred. |
| 402 | */ |
Ben Kwa | 743c7c2 | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 403 | void onModelUpdate(Model model); |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 404 | |
| 405 | /** |
| 406 | * Called when an update has been attempted but failed. |
| 407 | */ |
Ben Kwa | 743c7c2 | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 408 | void onModelUpdateFailed(Exception e); |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 409 | } |
Ben Kwa | 862b9641 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 410 | |
| 411 | /** |
| 412 | * @return An ordered array of model IDs representing the documents in the model. It is sorted |
| 413 | * according to the current sort order, which was set by the last model update. |
| 414 | */ |
| 415 | public List<String> getModelIds() { |
| 416 | return mIds; |
| 417 | } |
Ben Kwa | 003a999 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 418 | } |