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