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