Garfield Tan | 2010ff7 | 2016-09-30 14:55:32 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2016 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.sorting; |
| 18 | |
| 19 | import static com.android.documentsui.base.DocumentInfo.getCursorLong; |
| 20 | import static com.android.documentsui.base.DocumentInfo.getCursorString; |
| 21 | |
| 22 | import android.database.AbstractCursor; |
| 23 | import android.database.Cursor; |
| 24 | import android.provider.DocumentsContract.Document; |
| 25 | |
| 26 | import com.android.documentsui.base.Shared; |
| 27 | import com.android.documentsui.sorting.SortModel.SortDimensionId; |
| 28 | |
| 29 | /** |
| 30 | * Cursor wrapper that presents a sorted view of the underlying cursor. Handles |
| 31 | * common {@link Document} sorting modes, such as ordering directories first. |
| 32 | */ |
| 33 | class SortingCursorWrapper extends AbstractCursor { |
| 34 | private final Cursor mCursor; |
| 35 | |
| 36 | private final int[] mPosition; |
| 37 | |
| 38 | public SortingCursorWrapper(Cursor cursor, SortDimension dimension) { |
| 39 | mCursor = cursor; |
| 40 | |
| 41 | final int count = cursor.getCount(); |
| 42 | mPosition = new int[count]; |
| 43 | boolean[] isDirs = new boolean[count]; |
| 44 | String[] displayNames = null; |
| 45 | long[] longValues = null; |
| 46 | String[] ids = null; |
| 47 | |
| 48 | final @SortDimensionId int id = dimension.getId(); |
| 49 | switch (id) { |
| 50 | case SortModel.SORT_DIMENSION_ID_TITLE: |
| 51 | displayNames = new String[count]; |
| 52 | break; |
| 53 | case SortModel.SORT_DIMENSION_ID_DATE: |
| 54 | case SortModel.SORT_DIMENSION_ID_SIZE: |
| 55 | longValues = new long[count]; |
| 56 | ids = new String[count]; |
| 57 | break; |
| 58 | } |
| 59 | |
| 60 | cursor.moveToPosition(-1); |
| 61 | for (int i = 0; i < count; i++) { |
| 62 | cursor.moveToNext(); |
| 63 | mPosition[i] = i; |
| 64 | |
| 65 | final String mimeType = getCursorString(mCursor, Document.COLUMN_MIME_TYPE); |
| 66 | isDirs[i] = Document.MIME_TYPE_DIR.equals(mimeType); |
| 67 | |
| 68 | switch(id) { |
| 69 | case SortModel.SORT_DIMENSION_ID_TITLE: |
| 70 | final String displayName = getCursorString( |
| 71 | mCursor, Document.COLUMN_DISPLAY_NAME); |
| 72 | displayNames[i] = displayName; |
| 73 | break; |
| 74 | case SortModel.SORT_DIMENSION_ID_DATE: |
| 75 | longValues[i] = getLastModified(mCursor); |
| 76 | ids[i] = getCursorString(mCursor, Document.COLUMN_DOCUMENT_ID); |
| 77 | break; |
| 78 | case SortModel.SORT_DIMENSION_ID_SIZE: |
| 79 | longValues[i] = getCursorLong(mCursor, Document.COLUMN_SIZE); |
| 80 | ids[i] = getCursorString(mCursor, Document.COLUMN_DOCUMENT_ID); |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | } |
| 85 | |
| 86 | switch (id) { |
| 87 | case SortModel.SORT_DIMENSION_ID_TITLE: |
| 88 | binarySort(displayNames, isDirs, mPosition, dimension.getSortDirection()); |
| 89 | break; |
| 90 | case SortModel.SORT_DIMENSION_ID_DATE: |
| 91 | case SortModel.SORT_DIMENSION_ID_SIZE: |
| 92 | binarySort(longValues, isDirs, mPosition, ids, dimension.getSortDirection()); |
| 93 | break; |
| 94 | } |
| 95 | |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void close() { |
| 100 | super.close(); |
| 101 | mCursor.close(); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public boolean onMove(int oldPosition, int newPosition) { |
| 106 | return mCursor.moveToPosition(mPosition[newPosition]); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public String[] getColumnNames() { |
| 111 | return mCursor.getColumnNames(); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public int getCount() { |
| 116 | return mCursor.getCount(); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public double getDouble(int column) { |
| 121 | return mCursor.getDouble(column); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public float getFloat(int column) { |
| 126 | return mCursor.getFloat(column); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public int getInt(int column) { |
| 131 | return mCursor.getInt(column); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public long getLong(int column) { |
| 136 | return mCursor.getLong(column); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public short getShort(int column) { |
| 141 | return mCursor.getShort(column); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public String getString(int column) { |
| 146 | return mCursor.getString(column); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public int getType(int column) { |
| 151 | return mCursor.getType(column); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public boolean isNull(int column) { |
| 156 | return mCursor.isNull(column); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @return Timestamp for the given document. Some docs (e.g. active downloads) have a null |
| 161 | * timestamp - these will be replaced with MAX_LONG so that such files get sorted to the top |
| 162 | * when sorting descending by date. |
| 163 | */ |
| 164 | private static long getLastModified(Cursor cursor) { |
| 165 | long l = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED); |
| 166 | return (l == -1) ? Long.MAX_VALUE : l; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Borrowed from TimSort.binarySort(), but modified to sort two column |
| 171 | * dataset. |
| 172 | */ |
| 173 | private static void binarySort( |
| 174 | String[] sortKey, |
| 175 | boolean[] isDirs, |
| 176 | int[] positions, |
| 177 | @SortDimension.SortDirection int direction) { |
| 178 | final int count = positions.length; |
| 179 | for (int start = 1; start < count; start++) { |
| 180 | final int pivotPosition = positions[start]; |
| 181 | final String pivotValue = sortKey[start]; |
| 182 | final boolean pivotIsDir = isDirs[start]; |
| 183 | |
| 184 | int left = 0; |
| 185 | int right = start; |
| 186 | |
| 187 | while (left < right) { |
| 188 | int mid = (left + right) >>> 1; |
| 189 | |
| 190 | // Directories always go in front. |
| 191 | int compare = 0; |
| 192 | final boolean rhsIsDir = isDirs[mid]; |
| 193 | if (pivotIsDir && !rhsIsDir) { |
| 194 | compare = -1; |
| 195 | } else if (!pivotIsDir && rhsIsDir) { |
| 196 | compare = 1; |
| 197 | } else { |
| 198 | final String lhs = pivotValue; |
| 199 | final String rhs = sortKey[mid]; |
| 200 | switch (direction) { |
| 201 | case SortDimension.SORT_DIRECTION_ASCENDING: |
| 202 | compare = Shared.compareToIgnoreCaseNullable(lhs, rhs); |
| 203 | break; |
| 204 | case SortDimension.SORT_DIRECTION_DESCENDING: |
| 205 | compare = -Shared.compareToIgnoreCaseNullable(lhs, rhs); |
| 206 | break; |
| 207 | default: |
| 208 | throw new IllegalArgumentException( |
| 209 | "Unknown sorting direction: " + direction); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | if (compare < 0) { |
| 214 | right = mid; |
| 215 | } else { |
| 216 | left = mid + 1; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | int n = start - left; |
| 221 | switch (n) { |
| 222 | case 2: |
| 223 | positions[left + 2] = positions[left + 1]; |
| 224 | sortKey[left + 2] = sortKey[left + 1]; |
| 225 | isDirs[left + 2] = isDirs[left + 1]; |
| 226 | case 1: |
| 227 | positions[left + 1] = positions[left]; |
| 228 | sortKey[left + 1] = sortKey[left]; |
| 229 | isDirs[left + 1] = isDirs[left]; |
| 230 | break; |
| 231 | default: |
| 232 | System.arraycopy(positions, left, positions, left + 1, n); |
| 233 | System.arraycopy(sortKey, left, sortKey, left + 1, n); |
| 234 | System.arraycopy(isDirs, left, isDirs, left + 1, n); |
| 235 | } |
| 236 | |
| 237 | positions[left] = pivotPosition; |
| 238 | sortKey[left] = pivotValue; |
| 239 | isDirs[left] = pivotIsDir; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Borrowed from TimSort.binarySort(), but modified to sort two column |
| 245 | * dataset. |
| 246 | */ |
| 247 | private static void binarySort( |
| 248 | long[] sortKey, |
| 249 | boolean[] isDirs, |
| 250 | int[] positions, |
| 251 | String[] ids, |
| 252 | @SortDimension.SortDirection int direction) { |
| 253 | final int count = positions.length; |
| 254 | for (int start = 1; start < count; start++) { |
| 255 | final int pivotPosition = positions[start]; |
| 256 | final long pivotValue = sortKey[start]; |
| 257 | final boolean pivotIsDir = isDirs[start]; |
| 258 | final String pivotId = ids[start]; |
| 259 | |
| 260 | int left = 0; |
| 261 | int right = start; |
| 262 | |
| 263 | while (left < right) { |
| 264 | int mid = ((left + right) >>> 1); |
| 265 | |
| 266 | // Directories always go in front. |
| 267 | int compare = 0; |
| 268 | final boolean rhsIsDir = isDirs[mid]; |
| 269 | if (pivotIsDir && !rhsIsDir) { |
| 270 | compare = -1; |
| 271 | } else if (!pivotIsDir && rhsIsDir) { |
| 272 | compare = 1; |
| 273 | } else { |
| 274 | final long lhs = pivotValue; |
| 275 | final long rhs = sortKey[mid]; |
| 276 | switch (direction) { |
| 277 | case SortDimension.SORT_DIRECTION_ASCENDING: |
| 278 | compare = Long.compare(lhs, rhs); |
| 279 | break; |
| 280 | case SortDimension.SORT_DIRECTION_DESCENDING: |
| 281 | compare = -Long.compare(lhs, rhs); |
| 282 | break; |
| 283 | default: |
| 284 | throw new IllegalArgumentException( |
| 285 | "Unknown sorting direction: " + direction); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | // If numerical comparison yields a tie, use document ID as a tie breaker. This |
| 290 | // will yield stable results even if incoming items are continually shuffling and |
| 291 | // have identical numerical sort keys. One common example of this scenario is seen |
| 292 | // when sorting a set of active downloads by mod time. |
| 293 | if (compare == 0) { |
| 294 | compare = pivotId.compareTo(ids[mid]); |
| 295 | } |
| 296 | |
| 297 | if (compare < 0) { |
| 298 | right = mid; |
| 299 | } else { |
| 300 | left = mid + 1; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | int n = start - left; |
| 305 | switch (n) { |
| 306 | case 2: |
| 307 | positions[left + 2] = positions[left + 1]; |
| 308 | sortKey[left + 2] = sortKey[left + 1]; |
| 309 | isDirs[left + 2] = isDirs[left + 1]; |
| 310 | ids[left + 2] = ids[left + 1]; |
| 311 | case 1: |
| 312 | positions[left + 1] = positions[left]; |
| 313 | sortKey[left + 1] = sortKey[left]; |
| 314 | isDirs[left + 1] = isDirs[left]; |
| 315 | ids[left + 1] = ids[left]; |
| 316 | break; |
| 317 | default: |
| 318 | System.arraycopy(positions, left, positions, left + 1, n); |
| 319 | System.arraycopy(sortKey, left, sortKey, left + 1, n); |
| 320 | System.arraycopy(isDirs, left, isDirs, left + 1, n); |
| 321 | System.arraycopy(ids, left, ids, left + 1, n); |
| 322 | } |
| 323 | |
| 324 | positions[left] = pivotPosition; |
| 325 | sortKey[left] = pivotValue; |
| 326 | isDirs[left] = pivotIsDir; |
| 327 | ids[left] = pivotId; |
| 328 | } |
| 329 | } |
| 330 | } |