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