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; |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame^] | 20 | import static com.android.documentsui.base.DocumentInfo.getCursorLong; |
| 21 | import static com.android.documentsui.base.DocumentInfo.getCursorString; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 22 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 23 | import android.database.Cursor; |
Tomasz Mikolajewski | 20c04c5 | 2016-03-15 15:55:46 +0900 | [diff] [blame] | 24 | import android.database.MergeCursor; |
Steve McKay | 84769b8 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 25 | import android.net.Uri; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 26 | import android.os.Bundle; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 27 | import android.provider.DocumentsContract; |
| 28 | import android.provider.DocumentsContract.Document; |
| 29 | import android.support.annotation.Nullable; |
| 30 | import android.support.annotation.VisibleForTesting; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 31 | import android.util.Log; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 32 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 33 | import com.android.documentsui.DirectoryResult; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 34 | import com.android.documentsui.RootCursorWrapper; |
Steve McKay | 55c00e7 | 2016-02-18 15:32:16 -0800 | [diff] [blame] | 35 | import com.android.documentsui.Shared; |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame^] | 36 | import com.android.documentsui.base.DocumentInfo; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 37 | import com.android.documentsui.dirlist.MultiSelectManager.Selection; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 38 | import com.android.documentsui.sorting.SortDimension; |
| 39 | import com.android.documentsui.sorting.SortModel; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 40 | |
| 41 | import java.util.ArrayList; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 42 | import java.util.HashMap; |
| 43 | import java.util.List; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 44 | import java.util.Map; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * The data model for the current loaded directory. |
| 48 | */ |
| 49 | @VisibleForTesting |
Tomasz Mikolajewski | d71bd61 | 2016-02-16 12:28:43 +0900 | [diff] [blame] | 50 | public class Model { |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 51 | private static final String TAG = "Model"; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 52 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 53 | private boolean mIsLoading; |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 54 | private List<UpdateListener> mUpdateListeners = new ArrayList<>(); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 55 | @Nullable private Cursor mCursor; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 56 | 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, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 61 | * by {@link #mSortModel} |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 62 | */ |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 63 | private String mIds[] = new String[0]; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 64 | private SortModel mSortModel; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 65 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 66 | @Nullable String info; |
| 67 | @Nullable String error; |
Tomasz Mikolajewski | e29e341 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 68 | @Nullable DocumentInfo doc; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 69 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 70 | 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 McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 82 | void onLoaderReset() { |
| 83 | if (mIsLoading) { |
Steve McKay | 7c66209 | 2016-08-26 12:17:41 -0700 | [diff] [blame] | 84 | Log.w(TAG, "Received unexpected loader reset while in loading state for doc: " |
| 85 | + DocumentInfo.debugString(doc)); |
| 86 | return; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 87 | } |
Steve McKay | 7c66209 | 2016-08-26 12:17:41 -0700 | [diff] [blame] | 88 | |
| 89 | reset(); |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | private void reset() { |
| 93 | mCursor = null; |
| 94 | mCursorCount = 0; |
| 95 | mIds = new String[0]; |
| 96 | mPositions.clear(); |
| 97 | info = null; |
| 98 | error = null; |
| 99 | doc = null; |
| 100 | mIsLoading = false; |
| 101 | notifyUpdateListeners(); |
| 102 | } |
| 103 | |
| 104 | void update(DirectoryResult result) { |
| 105 | assert(result != null); |
| 106 | |
| 107 | if (DEBUG) Log.i(TAG, "Updating model with new result set."); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 108 | |
| 109 | if (result.exception != null) { |
| 110 | Log.e(TAG, "Error while loading directory contents", result.exception); |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 111 | notifyUpdateListeners(result.exception); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 112 | return; |
| 113 | } |
| 114 | |
| 115 | mCursor = result.cursor; |
| 116 | mCursorCount = mCursor.getCount(); |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 117 | mSortModel = result.sortModel; |
Tomasz Mikolajewski | e29e341 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 118 | doc = result.doc; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 119 | |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 120 | updateModelData(); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 121 | |
| 122 | final Bundle extras = mCursor.getExtras(); |
| 123 | if (extras != null) { |
| 124 | info = extras.getString(DocumentsContract.EXTRA_INFO); |
| 125 | error = extras.getString(DocumentsContract.EXTRA_ERROR); |
| 126 | mIsLoading = extras.getBoolean(DocumentsContract.EXTRA_LOADING, false); |
| 127 | } |
| 128 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 129 | notifyUpdateListeners(); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 132 | @VisibleForTesting |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 133 | int getItemCount() { |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 134 | return mCursorCount; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /** |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 138 | * Scan over the incoming cursor data, generate Model IDs for each row, and sort the IDs |
| 139 | * according to the current sort order. |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 140 | */ |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 141 | private void updateModelData() { |
| 142 | int[] positions = new int[mCursorCount]; |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 143 | mIds = new String[mCursorCount]; |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 144 | boolean[] isDirs = new boolean[mCursorCount]; |
| 145 | String[] displayNames = null; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 146 | long[] longValues = null; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 147 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 148 | final int id = mSortModel.getSortedDimensionId(); |
| 149 | switch (id) { |
| 150 | case SortModel.SORT_DIMENSION_ID_TITLE: |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 151 | displayNames = new String[mCursorCount]; |
| 152 | break; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 153 | case SortModel.SORT_DIMENSION_ID_DATE: |
| 154 | case SortModel.SORT_DIMENSION_ID_SIZE: |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 155 | longValues = new long[mCursorCount]; |
| 156 | break; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 159 | String mimeType; |
| 160 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 161 | mCursor.moveToPosition(-1); |
| 162 | for (int pos = 0; pos < mCursorCount; ++pos) { |
Ben Lin | 2df30e5 | 2016-04-22 16:12:50 -0700 | [diff] [blame] | 163 | if (!mCursor.moveToNext()) { |
| 164 | Log.e(TAG, "Fail to move cursor to next pos: " + pos); |
| 165 | return; |
| 166 | } |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 167 | positions[pos] = pos; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 168 | |
Tomasz Mikolajewski | 985df3d | 2016-03-15 15:38:54 +0900 | [diff] [blame] | 169 | // Generates a Model ID for a cursor entry that refers to a document. The Model ID is a |
| 170 | // unique string that can be used to identify the document referred to by the cursor. |
Tomasz Mikolajewski | 20c04c5 | 2016-03-15 15:55:46 +0900 | [diff] [blame] | 171 | // If the cursor is a merged cursor over multiple authorities, then prefix the ids |
| 172 | // with the authority to avoid collisions. |
| 173 | if (mCursor instanceof MergeCursor) { |
Tomasz Mikolajewski | 06b036f | 2016-04-26 11:11:17 +0900 | [diff] [blame] | 174 | mIds[pos] = getCursorString(mCursor, RootCursorWrapper.COLUMN_AUTHORITY) + "|" + |
| 175 | getCursorString(mCursor, Document.COLUMN_DOCUMENT_ID); |
Tomasz Mikolajewski | 20c04c5 | 2016-03-15 15:55:46 +0900 | [diff] [blame] | 176 | } else { |
Tomasz Mikolajewski | 06b036f | 2016-04-26 11:11:17 +0900 | [diff] [blame] | 177 | mIds[pos] = getCursorString(mCursor, Document.COLUMN_DOCUMENT_ID); |
Tomasz Mikolajewski | 20c04c5 | 2016-03-15 15:55:46 +0900 | [diff] [blame] | 178 | } |
Tomasz Mikolajewski | 985df3d | 2016-03-15 15:38:54 +0900 | [diff] [blame] | 179 | |
Tomasz Mikolajewski | 06b036f | 2016-04-26 11:11:17 +0900 | [diff] [blame] | 180 | mimeType = getCursorString(mCursor, Document.COLUMN_MIME_TYPE); |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 181 | isDirs[pos] = Document.MIME_TYPE_DIR.equals(mimeType); |
| 182 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 183 | switch(id) { |
| 184 | case SortModel.SORT_DIMENSION_ID_TITLE: |
Tomasz Mikolajewski | 06b036f | 2016-04-26 11:11:17 +0900 | [diff] [blame] | 185 | final String displayName = getCursorString( |
| 186 | mCursor, Document.COLUMN_DISPLAY_NAME); |
| 187 | displayNames[pos] = displayName; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 188 | break; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 189 | case SortModel.SORT_DIMENSION_ID_DATE: |
Tomasz Mikolajewski | 06b036f | 2016-04-26 11:11:17 +0900 | [diff] [blame] | 190 | longValues[pos] = getLastModified(mCursor); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 191 | break; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 192 | case SortModel.SORT_DIMENSION_ID_SIZE: |
Tomasz Mikolajewski | 06b036f | 2016-04-26 11:11:17 +0900 | [diff] [blame] | 193 | longValues[pos] = getCursorLong(mCursor, Document.COLUMN_SIZE); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 194 | break; |
| 195 | } |
| 196 | } |
| 197 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 198 | final SortDimension dimension = mSortModel.getDimensionById(id); |
| 199 | switch (id) { |
| 200 | case SortModel.SORT_DIMENSION_ID_TITLE: |
| 201 | binarySort(displayNames, isDirs, positions, mIds, dimension.getSortDirection()); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 202 | break; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 203 | case SortModel.SORT_DIMENSION_ID_DATE: |
| 204 | case SortModel.SORT_DIMENSION_ID_SIZE: |
| 205 | binarySort(longValues, isDirs, positions, mIds, dimension.getSortDirection()); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 206 | break; |
| 207 | } |
| 208 | |
| 209 | // Populate the positions. |
| 210 | mPositions.clear(); |
| 211 | for (int i = 0; i < mCursorCount; ++i) { |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 212 | mPositions.put(mIds[i], positions[i]); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | /** |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 217 | * Sorts model data. Takes three columns of index-corresponded data. The first column is the |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 218 | * sort key. Rows are sorted in ascending alphabetical order on the sort key. |
| 219 | * Directories are always shown first. This code is based on TimSort.binarySort(). |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 220 | * |
| 221 | * @param sortKey Data is sorted in ascending alphabetical order. |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 222 | * @param isDirs Array saying whether an item is a directory or not. |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 223 | * @param positions Cursor positions to be sorted. |
| 224 | * @param ids Model IDs to be sorted. |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 225 | */ |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 226 | private static void binarySort( |
| 227 | String[] sortKey, |
| 228 | boolean[] isDirs, |
| 229 | int[] positions, |
| 230 | String[] ids, |
| 231 | @SortDimension.SortDirection int direction) { |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 232 | final int count = positions.length; |
| 233 | for (int start = 1; start < count; start++) { |
| 234 | final int pivotPosition = positions[start]; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 235 | final String pivotValue = sortKey[start]; |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 236 | final boolean pivotIsDir = isDirs[start]; |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 237 | final String pivotId = ids[start]; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 238 | |
| 239 | int left = 0; |
| 240 | int right = start; |
| 241 | |
| 242 | while (left < right) { |
| 243 | int mid = (left + right) >>> 1; |
| 244 | |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 245 | // Directories always go in front. |
| 246 | int compare = 0; |
| 247 | final boolean rhsIsDir = isDirs[mid]; |
| 248 | if (pivotIsDir && !rhsIsDir) { |
| 249 | compare = -1; |
| 250 | } else if (!pivotIsDir && rhsIsDir) { |
| 251 | compare = 1; |
| 252 | } else { |
| 253 | final String lhs = pivotValue; |
| 254 | final String rhs = sortKey[mid]; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 255 | switch (direction) { |
| 256 | case SortDimension.SORT_DIRECTION_ASCENDING: |
| 257 | compare = Shared.compareToIgnoreCaseNullable(lhs, rhs); |
| 258 | break; |
| 259 | case SortDimension.SORT_DIRECTION_DESCENDING: |
| 260 | compare = -Shared.compareToIgnoreCaseNullable(lhs, rhs); |
| 261 | break; |
| 262 | default: |
| 263 | throw new IllegalArgumentException( |
| 264 | "Unknown sorting direction: " + direction); |
| 265 | } |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 266 | } |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 267 | |
| 268 | if (compare < 0) { |
| 269 | right = mid; |
| 270 | } else { |
| 271 | left = mid + 1; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | int n = start - left; |
| 276 | switch (n) { |
| 277 | case 2: |
| 278 | positions[left + 2] = positions[left + 1]; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 279 | sortKey[left + 2] = sortKey[left + 1]; |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 280 | isDirs[left + 2] = isDirs[left + 1]; |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 281 | ids[left + 2] = ids[left + 1]; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 282 | case 1: |
| 283 | positions[left + 1] = positions[left]; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 284 | sortKey[left + 1] = sortKey[left]; |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 285 | isDirs[left + 1] = isDirs[left]; |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 286 | ids[left + 1] = ids[left]; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 287 | break; |
| 288 | default: |
| 289 | System.arraycopy(positions, left, positions, left + 1, n); |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 290 | System.arraycopy(sortKey, left, sortKey, left + 1, n); |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 291 | System.arraycopy(isDirs, left, isDirs, left + 1, n); |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 292 | System.arraycopy(ids, left, ids, left + 1, n); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | positions[left] = pivotPosition; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 296 | sortKey[left] = pivotValue; |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 297 | isDirs[left] = pivotIsDir; |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 298 | ids[left] = pivotId; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | /** |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 303 | * Sorts model data. Takes four columns of index-corresponded data. The first column is the sort |
| 304 | * key, and the second is an array of mime types. The rows are first bucketed by mime type |
| 305 | * (directories vs documents) and then each bucket is sorted independently in descending |
| 306 | * numerical order on the sort key. This code is based on TimSort.binarySort(). |
| 307 | * |
| 308 | * @param sortKey Data is sorted in descending numerical order. |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 309 | * @param isDirs Array saying whether an item is a directory or not. |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 310 | * @param positions Cursor positions to be sorted. |
| 311 | * @param ids Model IDs to be sorted. |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 312 | */ |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 313 | private static void binarySort( |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 314 | long[] sortKey, |
| 315 | boolean[] isDirs, |
| 316 | int[] positions, |
| 317 | String[] ids, |
| 318 | @SortDimension.SortDirection int direction) { |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 319 | final int count = positions.length; |
| 320 | for (int start = 1; start < count; start++) { |
| 321 | final int pivotPosition = positions[start]; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 322 | final long pivotValue = sortKey[start]; |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 323 | final boolean pivotIsDir = isDirs[start]; |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 324 | final String pivotId = ids[start]; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 325 | |
| 326 | int left = 0; |
| 327 | int right = start; |
| 328 | |
| 329 | while (left < right) { |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 330 | int mid = ((left + right) >>> 1); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 331 | |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 332 | // Directories always go in front. |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 333 | int compare = 0; |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 334 | final boolean rhsIsDir = isDirs[mid]; |
| 335 | if (pivotIsDir && !rhsIsDir) { |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 336 | compare = -1; |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 337 | } else if (!pivotIsDir && rhsIsDir) { |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 338 | compare = 1; |
| 339 | } else { |
| 340 | final long lhs = pivotValue; |
| 341 | final long rhs = sortKey[mid]; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 342 | switch (direction) { |
| 343 | case SortDimension.SORT_DIRECTION_ASCENDING: |
| 344 | compare = Long.compare(lhs, rhs); |
| 345 | break; |
| 346 | case SortDimension.SORT_DIRECTION_DESCENDING: |
| 347 | compare = -Long.compare(lhs, rhs); |
| 348 | break; |
| 349 | default: |
| 350 | throw new IllegalArgumentException( |
| 351 | "Unknown sorting direction: " + direction); |
| 352 | } |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 353 | } |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 354 | |
Ben Kwa | e433834 | 2016-01-08 15:34:47 -0800 | [diff] [blame] | 355 | // If numerical comparison yields a tie, use document ID as a tie breaker. This |
| 356 | // will yield stable results even if incoming items are continually shuffling and |
| 357 | // have identical numerical sort keys. One common example of this scenario is seen |
| 358 | // when sorting a set of active downloads by mod time. |
| 359 | if (compare == 0) { |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 360 | compare = pivotId.compareTo(ids[mid]); |
Ben Kwa | e433834 | 2016-01-08 15:34:47 -0800 | [diff] [blame] | 361 | } |
| 362 | |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 363 | if (compare < 0) { |
| 364 | right = mid; |
| 365 | } else { |
| 366 | left = mid + 1; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | int n = start - left; |
| 371 | switch (n) { |
| 372 | case 2: |
| 373 | positions[left + 2] = positions[left + 1]; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 374 | sortKey[left + 2] = sortKey[left + 1]; |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 375 | isDirs[left + 2] = isDirs[left + 1]; |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 376 | ids[left + 2] = ids[left + 1]; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 377 | case 1: |
| 378 | positions[left + 1] = positions[left]; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 379 | sortKey[left + 1] = sortKey[left]; |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 380 | isDirs[left + 1] = isDirs[left]; |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 381 | ids[left + 1] = ids[left]; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 382 | break; |
| 383 | default: |
| 384 | System.arraycopy(positions, left, positions, left + 1, n); |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 385 | System.arraycopy(sortKey, left, sortKey, left + 1, n); |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 386 | System.arraycopy(isDirs, left, isDirs, left + 1, n); |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 387 | System.arraycopy(ids, left, ids, left + 1, n); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | positions[left] = pivotPosition; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 391 | sortKey[left] = pivotValue; |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 392 | isDirs[left] = pivotIsDir; |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 393 | ids[left] = pivotId; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
Ben Kwa | e433834 | 2016-01-08 15:34:47 -0800 | [diff] [blame] | 397 | /** |
Tomasz Mikolajewski | 985df3d | 2016-03-15 15:38:54 +0900 | [diff] [blame] | 398 | * @return Timestamp for the given document. Some docs (e.g. active downloads) have a null |
Tomasz Mikolajewski | 06b036f | 2016-04-26 11:11:17 +0900 | [diff] [blame] | 399 | * timestamp - these will be replaced with MAX_LONG so that such files get sorted to the top |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 400 | * when sorting descending by date. |
Tomasz Mikolajewski | 985df3d | 2016-03-15 15:38:54 +0900 | [diff] [blame] | 401 | */ |
Tomasz Mikolajewski | 06b036f | 2016-04-26 11:11:17 +0900 | [diff] [blame] | 402 | long getLastModified(Cursor cursor) { |
| 403 | long l = getCursorLong(mCursor, Document.COLUMN_LAST_MODIFIED); |
| 404 | return (l == -1) ? Long.MAX_VALUE : l; |
Ben Kwa | e433834 | 2016-01-08 15:34:47 -0800 | [diff] [blame] | 405 | } |
| 406 | |
Tomasz Mikolajewski | d71bd61 | 2016-02-16 12:28:43 +0900 | [diff] [blame] | 407 | public @Nullable Cursor getItem(String modelId) { |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 408 | Integer pos = mPositions.get(modelId); |
Steve McKay | 5a22a11 | 2016-04-12 11:29:10 -0700 | [diff] [blame] | 409 | if (pos == null) { |
| 410 | if (DEBUG) Log.d(TAG, "Unabled to find cursor position for modelId: " + modelId); |
| 411 | return null; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 412 | } |
Steve McKay | 5a22a11 | 2016-04-12 11:29:10 -0700 | [diff] [blame] | 413 | |
| 414 | if (!mCursor.moveToPosition(pos)) { |
| 415 | if (DEBUG) Log.d(TAG, |
| 416 | "Unabled to move cursor to position " + pos + " for modelId: " + modelId); |
| 417 | return null; |
| 418 | } |
| 419 | |
| 420 | return mCursor; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 423 | boolean isEmpty() { |
| 424 | return mCursorCount == 0; |
| 425 | } |
| 426 | |
| 427 | boolean isLoading() { |
| 428 | return mIsLoading; |
| 429 | } |
| 430 | |
Steve McKay | 84769b8 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 431 | List<DocumentInfo> getDocuments(Selection selection) { |
| 432 | final int size = (selection != null) ? selection.size() : 0; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 433 | |
| 434 | final List<DocumentInfo> docs = new ArrayList<>(size); |
Steve McKay | 84769b8 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 435 | // NOTE: That as this now iterates over only final (non-provisional) selection. |
| 436 | for (String modelId: selection) { |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 437 | final Cursor cursor = getItem(modelId); |
Steve McKay | 5a22a11 | 2016-04-12 11:29:10 -0700 | [diff] [blame] | 438 | if (cursor == null) { |
Steve McKay | 84769b8 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 439 | Log.w(TAG, "Skipping document. Unabled to obtain cursor for modelId: " + modelId); |
Steve McKay | 5a22a11 | 2016-04-12 11:29:10 -0700 | [diff] [blame] | 440 | continue; |
| 441 | } |
Steve McKay | 0af8afd | 2016-02-25 13:34:03 -0800 | [diff] [blame] | 442 | docs.add(DocumentInfo.fromDirectoryCursor(cursor)); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 443 | } |
| 444 | return docs; |
| 445 | } |
| 446 | |
Steve McKay | 84769b8 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 447 | public Uri getItemUri(String modelId) { |
| 448 | final Cursor cursor = getItem(modelId); |
| 449 | return DocumentInfo.getUri(cursor); |
| 450 | } |
| 451 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 452 | /** |
| 453 | * @return An ordered array of model IDs representing the documents in the model. It is sorted |
| 454 | * according to the current sort order, which was set by the last model update. |
| 455 | */ |
| 456 | public String[] getModelIds() { |
| 457 | return mIds; |
| 458 | } |
| 459 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 460 | void addUpdateListener(UpdateListener listener) { |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 461 | mUpdateListeners.add(listener); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 462 | } |
| 463 | |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 464 | void removeUpdateListener(UpdateListener listener) { |
| 465 | mUpdateListeners.remove(listener); |
| 466 | } |
| 467 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 468 | interface UpdateListener { |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 469 | /** |
| 470 | * Called when a successful update has occurred. |
| 471 | */ |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 472 | void onModelUpdate(Model model); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 473 | |
| 474 | /** |
| 475 | * Called when an update has been attempted but failed. |
| 476 | */ |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 477 | void onModelUpdateFailed(Exception e); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 478 | } |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 479 | } |