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; |
Steve McKay | 5a22a11 | 2016-04-12 11:29:10 -0700 | [diff] [blame] | 40 | import java.util.Collections; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 41 | import java.util.HashMap; |
| 42 | import java.util.List; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 43 | import java.util.Map; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * The data model for the current loaded directory. |
| 47 | */ |
| 48 | @VisibleForTesting |
Tomasz Mikolajewski | d71bd61 | 2016-02-16 12:28:43 +0900 | [diff] [blame] | 49 | public class Model { |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 50 | private static final String TAG = "Model"; |
Tomasz Mikolajewski | 985df3d | 2016-03-15 15:38:54 +0900 | [diff] [blame] | 51 | private static final String EMPTY = ""; |
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 |
| 61 | * by {@link #mSortOrder} |
| 62 | */ |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 63 | private String mIds[] = new String[0]; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 64 | private int mSortOrder = SORT_ORDER_DISPLAY_NAME; |
| 65 | |
Tomasz Mikolajewski | 985df3d | 2016-03-15 15:38:54 +0900 | [diff] [blame] | 66 | private int mAuthorityIndex = -1; |
| 67 | private int mDocIdIndex = -1; |
| 68 | private int mMimeTypeIndex = -1; |
| 69 | private int mDisplayNameIndex = -1; |
| 70 | private int mSizeIndex = -1; |
| 71 | private int mLastModifiedIndex = -1; |
| 72 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 73 | @Nullable String info; |
| 74 | @Nullable String error; |
Tomasz Mikolajewski | e29e341 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 75 | @Nullable DocumentInfo doc; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 76 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 77 | private void notifyUpdateListeners() { |
| 78 | for (UpdateListener listener: mUpdateListeners) { |
| 79 | listener.onModelUpdate(this); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | private void notifyUpdateListeners(Exception e) { |
| 84 | for (UpdateListener listener: mUpdateListeners) { |
| 85 | listener.onModelUpdateFailed(e); |
| 86 | } |
| 87 | } |
| 88 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 89 | void update(DirectoryResult result) { |
| 90 | if (DEBUG) Log.i(TAG, "Updating model with new result set."); |
| 91 | |
| 92 | if (result == null) { |
| 93 | mCursor = null; |
| 94 | mCursorCount = 0; |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 95 | mIds = new String[0]; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 96 | mPositions.clear(); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 97 | info = null; |
| 98 | error = null; |
Tomasz Mikolajewski | e29e341 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 99 | doc = null; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 100 | mIsLoading = false; |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 101 | notifyUpdateListeners(); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 102 | return; |
| 103 | } |
| 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(); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 113 | mSortOrder = result.sortOrder; |
Tomasz Mikolajewski | 985df3d | 2016-03-15 15:38:54 +0900 | [diff] [blame] | 114 | mAuthorityIndex = mCursor.getColumnIndex(RootCursorWrapper.COLUMN_AUTHORITY); |
| 115 | assert(mAuthorityIndex != -1); |
| 116 | mDocIdIndex = mCursor.getColumnIndex(Document.COLUMN_DOCUMENT_ID); |
| 117 | mMimeTypeIndex = mCursor.getColumnIndex(Document.COLUMN_MIME_TYPE); |
| 118 | mDisplayNameIndex = mCursor.getColumnIndex(Document.COLUMN_DISPLAY_NAME); |
| 119 | mLastModifiedIndex = mCursor.getColumnIndex(Document.COLUMN_LAST_MODIFIED); |
| 120 | mSizeIndex = mCursor.getColumnIndex(Document.COLUMN_SIZE); |
| 121 | |
Tomasz Mikolajewski | e29e341 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 122 | doc = result.doc; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 123 | |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 124 | updateModelData(); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 125 | |
| 126 | final Bundle extras = mCursor.getExtras(); |
| 127 | if (extras != null) { |
| 128 | info = extras.getString(DocumentsContract.EXTRA_INFO); |
| 129 | error = extras.getString(DocumentsContract.EXTRA_ERROR); |
| 130 | mIsLoading = extras.getBoolean(DocumentsContract.EXTRA_LOADING, false); |
| 131 | } |
| 132 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 133 | notifyUpdateListeners(); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 136 | @VisibleForTesting |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 137 | int getItemCount() { |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 138 | return mCursorCount; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 142 | * Scan over the incoming cursor data, generate Model IDs for each row, and sort the IDs |
| 143 | * according to the current sort order. |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 144 | */ |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 145 | private void updateModelData() { |
| 146 | int[] positions = new int[mCursorCount]; |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 147 | mIds = new String[mCursorCount]; |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 148 | boolean[] isDirs = new boolean[mCursorCount]; |
| 149 | String[] displayNames = null; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 150 | long[] longValues = null; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 151 | |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 152 | switch (mSortOrder) { |
| 153 | case SORT_ORDER_DISPLAY_NAME: |
| 154 | displayNames = new String[mCursorCount]; |
| 155 | break; |
| 156 | case SORT_ORDER_LAST_MODIFIED: |
| 157 | case SORT_ORDER_SIZE: |
| 158 | longValues = new long[mCursorCount]; |
| 159 | break; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 162 | String mimeType; |
| 163 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 164 | mCursor.moveToPosition(-1); |
| 165 | for (int pos = 0; pos < mCursorCount; ++pos) { |
Ben Lin | 2df30e5 | 2016-04-22 16:12:50 -0700 | [diff] [blame^] | 166 | if (!mCursor.moveToNext()) { |
| 167 | Log.e(TAG, "Fail to move cursor to next pos: " + pos); |
| 168 | return; |
| 169 | } |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 170 | positions[pos] = pos; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 171 | |
Tomasz Mikolajewski | 985df3d | 2016-03-15 15:38:54 +0900 | [diff] [blame] | 172 | // Generates a Model ID for a cursor entry that refers to a document. The Model ID is a |
| 173 | // 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] | 174 | // If the cursor is a merged cursor over multiple authorities, then prefix the ids |
| 175 | // with the authority to avoid collisions. |
| 176 | if (mCursor instanceof MergeCursor) { |
| 177 | mIds[pos] = getStringOrEmpty(mAuthorityIndex) + "|" + getStringOrEmpty(mDocIdIndex); |
| 178 | } else { |
| 179 | mIds[pos] = getStringOrEmpty(mDocIdIndex); |
| 180 | } |
Tomasz Mikolajewski | 985df3d | 2016-03-15 15:38:54 +0900 | [diff] [blame] | 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); |
Steve McKay | 5a22a11 | 2016-04-12 11:29:10 -0700 | [diff] [blame] | 410 | if (pos == null) { |
| 411 | if (DEBUG) Log.d(TAG, "Unabled to find cursor position for modelId: " + modelId); |
| 412 | return null; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 413 | } |
Steve McKay | 5a22a11 | 2016-04-12 11:29:10 -0700 | [diff] [blame] | 414 | |
| 415 | if (!mCursor.moveToPosition(pos)) { |
| 416 | if (DEBUG) Log.d(TAG, |
| 417 | "Unabled to move cursor to position " + pos + " for modelId: " + modelId); |
| 418 | return null; |
| 419 | } |
| 420 | |
| 421 | return mCursor; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 422 | } |
| 423 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 424 | boolean isEmpty() { |
| 425 | return mCursorCount == 0; |
| 426 | } |
| 427 | |
| 428 | boolean isLoading() { |
| 429 | return mIsLoading; |
| 430 | } |
| 431 | |
| 432 | List<DocumentInfo> getDocuments(Selection items) { |
| 433 | final int size = (items != null) ? items.size() : 0; |
| 434 | |
| 435 | final List<DocumentInfo> docs = new ArrayList<>(size); |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 436 | for (String modelId: items.getAll()) { |
| 437 | final Cursor cursor = getItem(modelId); |
Steve McKay | 5a22a11 | 2016-04-12 11:29:10 -0700 | [diff] [blame] | 438 | if (cursor == null) { |
| 439 | Log.w(TAG, |
| 440 | "Skipping document. Unabled to obtain cursor for modelId: " + modelId); |
| 441 | continue; |
| 442 | } |
Steve McKay | 0af8afd | 2016-02-25 13:34:03 -0800 | [diff] [blame] | 443 | docs.add(DocumentInfo.fromDirectoryCursor(cursor)); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 444 | } |
| 445 | return docs; |
| 446 | } |
| 447 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 448 | void addUpdateListener(UpdateListener listener) { |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 449 | mUpdateListeners.add(listener); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 450 | } |
| 451 | |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 452 | void removeUpdateListener(UpdateListener listener) { |
| 453 | mUpdateListeners.remove(listener); |
| 454 | } |
| 455 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 456 | static interface UpdateListener { |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 457 | /** |
| 458 | * Called when a successful update has occurred. |
| 459 | */ |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 460 | void onModelUpdate(Model model); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 461 | |
| 462 | /** |
| 463 | * Called when an update has been attempted but failed. |
| 464 | */ |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 465 | void onModelUpdateFailed(Exception e); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 466 | } |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 467 | |
| 468 | /** |
| 469 | * @return An ordered array of model IDs representing the documents in the model. It is sorted |
| 470 | * according to the current sort order, which was set by the last model update. |
| 471 | */ |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 472 | public String[] getModelIds() { |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 473 | return mIds; |
| 474 | } |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 475 | } |