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