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; |
| 23 | import static com.android.documentsui.model.DocumentInfo.getCursorLong; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 24 | import static com.android.documentsui.model.DocumentInfo.getCursorString; |
| 25 | import static com.android.internal.util.Preconditions.checkNotNull; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 26 | |
| 27 | import android.content.ContentProviderClient; |
| 28 | import android.content.ContentResolver; |
| 29 | import android.content.Context; |
| 30 | import android.database.Cursor; |
| 31 | import android.os.AsyncTask; |
| 32 | import android.os.Bundle; |
| 33 | import android.os.Looper; |
| 34 | import android.provider.DocumentsContract; |
| 35 | import android.provider.DocumentsContract.Document; |
| 36 | import android.support.annotation.Nullable; |
| 37 | import android.support.annotation.VisibleForTesting; |
| 38 | import android.support.v7.widget.RecyclerView; |
| 39 | import android.util.Log; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 40 | |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 41 | import com.android.documentsui.BaseActivity.SiblingProvider; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 42 | import com.android.documentsui.DirectoryResult; |
| 43 | import com.android.documentsui.DocumentsApplication; |
| 44 | import com.android.documentsui.RootCursorWrapper; |
| 45 | import com.android.documentsui.dirlist.MultiSelectManager.Selection; |
| 46 | import com.android.documentsui.model.DocumentInfo; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 47 | |
| 48 | import java.util.ArrayList; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 49 | import java.util.HashMap; |
| 50 | import java.util.List; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 51 | import java.util.Map; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * The data model for the current loaded directory. |
| 55 | */ |
| 56 | @VisibleForTesting |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 57 | public class Model implements SiblingProvider { |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 58 | private static final String TAG = "Model"; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 59 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 60 | private Context mContext; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 61 | private boolean mIsLoading; |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 62 | private List<UpdateListener> mUpdateListeners = new ArrayList<>(); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 63 | @Nullable private Cursor mCursor; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 64 | private int mCursorCount; |
| 65 | /** Maps Model ID to cursor positions, for looking up items by Model ID. */ |
| 66 | private Map<String, Integer> mPositions = new HashMap<>(); |
| 67 | /** |
| 68 | * A sorted array of model IDs for the files currently in the Model. Sort order is determined |
| 69 | * by {@link #mSortOrder} |
| 70 | */ |
| 71 | private List<String> mIds = new ArrayList<>(); |
| 72 | private int mSortOrder = SORT_ORDER_DISPLAY_NAME; |
| 73 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 74 | @Nullable String info; |
| 75 | @Nullable String error; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 76 | |
| 77 | Model(Context context, RecyclerView.Adapter<?> viewAdapter) { |
| 78 | mContext = context; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 81 | /** |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 82 | * Generates a Model ID for a cursor entry that refers to a document. The Model ID is a unique |
| 83 | * string that can be used to identify the document referred to by the cursor. |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 84 | * |
| 85 | * @param c A cursor that refers to a document. |
| 86 | */ |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 87 | private static String createModelId(Cursor c) { |
| 88 | // TODO: Maybe more efficient to use just the document ID, in cases where there is only one |
| 89 | // authority (which should be the majority of cases). |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 90 | return getCursorString(c, RootCursorWrapper.COLUMN_AUTHORITY) + |
| 91 | "|" + getCursorString(c, Document.COLUMN_DOCUMENT_ID); |
| 92 | } |
| 93 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 94 | private void notifyUpdateListeners() { |
| 95 | for (UpdateListener listener: mUpdateListeners) { |
| 96 | listener.onModelUpdate(this); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | private void notifyUpdateListeners(Exception e) { |
| 101 | for (UpdateListener listener: mUpdateListeners) { |
| 102 | listener.onModelUpdateFailed(e); |
| 103 | } |
| 104 | } |
| 105 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 106 | void update(DirectoryResult result) { |
| 107 | if (DEBUG) Log.i(TAG, "Updating model with new result set."); |
| 108 | |
| 109 | if (result == null) { |
| 110 | mCursor = null; |
| 111 | mCursorCount = 0; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 112 | mIds.clear(); |
| 113 | mPositions.clear(); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 114 | info = null; |
| 115 | error = null; |
| 116 | mIsLoading = false; |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 117 | notifyUpdateListeners(); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 118 | return; |
| 119 | } |
| 120 | |
| 121 | if (result.exception != null) { |
| 122 | Log.e(TAG, "Error while loading directory contents", result.exception); |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 123 | notifyUpdateListeners(result.exception); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 124 | return; |
| 125 | } |
| 126 | |
| 127 | mCursor = result.cursor; |
| 128 | mCursorCount = mCursor.getCount(); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 129 | mSortOrder = result.sortOrder; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 130 | |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 131 | updateModelData(); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 132 | |
| 133 | final Bundle extras = mCursor.getExtras(); |
| 134 | if (extras != null) { |
| 135 | info = extras.getString(DocumentsContract.EXTRA_INFO); |
| 136 | error = extras.getString(DocumentsContract.EXTRA_ERROR); |
| 137 | mIsLoading = extras.getBoolean(DocumentsContract.EXTRA_LOADING, false); |
| 138 | } |
| 139 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 140 | notifyUpdateListeners(); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 143 | @VisibleForTesting |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 144 | int getItemCount() { |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 145 | return mCursorCount; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 149 | * Scan over the incoming cursor data, generate Model IDs for each row, and sort the IDs |
| 150 | * according to the current sort order. |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 151 | */ |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 152 | private void updateModelData() { |
| 153 | int[] positions = new int[mCursorCount]; |
| 154 | mIds.clear(); |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 155 | String[] stringValues = new String[mCursorCount]; |
| 156 | long[] longValues = null; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 157 | |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 158 | if (mSortOrder == SORT_ORDER_LAST_MODIFIED || mSortOrder == SORT_ORDER_SIZE) { |
| 159 | longValues = new long[mCursorCount]; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 162 | mCursor.moveToPosition(-1); |
| 163 | for (int pos = 0; pos < mCursorCount; ++pos) { |
| 164 | mCursor.moveToNext(); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 165 | positions[pos] = pos; |
| 166 | mIds.add(createModelId(mCursor)); |
| 167 | |
| 168 | switch(mSortOrder) { |
| 169 | case SORT_ORDER_DISPLAY_NAME: |
| 170 | final String mimeType = getCursorString(mCursor, Document.COLUMN_MIME_TYPE); |
| 171 | final String displayName = getCursorString( |
| 172 | mCursor, Document.COLUMN_DISPLAY_NAME); |
| 173 | if (Document.MIME_TYPE_DIR.equals(mimeType)) { |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 174 | stringValues[pos] = DocumentInfo.DIR_PREFIX + displayName; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 175 | } else { |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 176 | stringValues[pos] = displayName; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 177 | } |
| 178 | break; |
| 179 | case SORT_ORDER_LAST_MODIFIED: |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 180 | longValues[pos] = getCursorLong(mCursor, Document.COLUMN_LAST_MODIFIED); |
| 181 | stringValues[pos] = getCursorString(mCursor, Document.COLUMN_MIME_TYPE); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 182 | break; |
| 183 | case SORT_ORDER_SIZE: |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 184 | longValues[pos] = getCursorLong(mCursor, Document.COLUMN_SIZE); |
| 185 | stringValues[pos] = getCursorString(mCursor, Document.COLUMN_MIME_TYPE); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 186 | break; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | switch (mSortOrder) { |
| 191 | case SORT_ORDER_DISPLAY_NAME: |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 192 | binarySort(stringValues, positions, mIds); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 193 | break; |
| 194 | case SORT_ORDER_LAST_MODIFIED: |
| 195 | case SORT_ORDER_SIZE: |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 196 | binarySort(longValues, stringValues, positions, mIds); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 197 | break; |
| 198 | } |
| 199 | |
| 200 | // Populate the positions. |
| 201 | mPositions.clear(); |
| 202 | for (int i = 0; i < mCursorCount; ++i) { |
| 203 | mPositions.put(mIds.get(i), positions[i]); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 208 | * Sorts model data. Takes three columns of index-corresponded data. The first column is the |
| 209 | * sort key. Rows are sorted in ascending alphabetical order on the sort key. This code is based |
| 210 | * on TimSort.binarySort(). |
| 211 | * |
| 212 | * @param sortKey Data is sorted in ascending alphabetical order. |
| 213 | * @param positions Cursor positions to be sorted. |
| 214 | * @param ids Model IDs to be sorted. |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 215 | */ |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 216 | private static void binarySort(String[] sortKey, int[] positions, List<String> ids) { |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 217 | final int count = positions.length; |
| 218 | for (int start = 1; start < count; start++) { |
| 219 | final int pivotPosition = positions[start]; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 220 | final String pivotValue = sortKey[start]; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 221 | final String pivotId = ids.get(start); |
| 222 | |
| 223 | int left = 0; |
| 224 | int right = start; |
| 225 | |
| 226 | while (left < right) { |
| 227 | int mid = (left + right) >>> 1; |
| 228 | |
| 229 | final String lhs = pivotValue; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 230 | final String rhs = sortKey[mid]; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 231 | final int compare = DocumentInfo.compareToIgnoreCaseNullable(lhs, rhs); |
| 232 | |
| 233 | if (compare < 0) { |
| 234 | right = mid; |
| 235 | } else { |
| 236 | left = mid + 1; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | int n = start - left; |
| 241 | switch (n) { |
| 242 | case 2: |
| 243 | positions[left + 2] = positions[left + 1]; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 244 | sortKey[left + 2] = sortKey[left + 1]; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 245 | ids.set(left + 2, ids.get(left + 1)); |
| 246 | case 1: |
| 247 | positions[left + 1] = positions[left]; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 248 | sortKey[left + 1] = sortKey[left]; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 249 | ids.set(left + 1, ids.get(left)); |
| 250 | break; |
| 251 | default: |
| 252 | System.arraycopy(positions, left, positions, left + 1, n); |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 253 | System.arraycopy(sortKey, left, sortKey, left + 1, n); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 254 | for (int i = n; i >= 1; --i) { |
| 255 | ids.set(left + i, ids.get(left + i - 1)); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | positions[left] = pivotPosition; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 260 | sortKey[left] = pivotValue; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 261 | ids.set(left, pivotId); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /** |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 266 | * Sorts model data. Takes four columns of index-corresponded data. The first column is the sort |
| 267 | * key, and the second is an array of mime types. The rows are first bucketed by mime type |
| 268 | * (directories vs documents) and then each bucket is sorted independently in descending |
| 269 | * numerical order on the sort key. This code is based on TimSort.binarySort(). |
| 270 | * |
| 271 | * @param sortKey Data is sorted in descending numerical order. |
| 272 | * @param mimeTypes Corresponding mime types. Directories will be sorted ahead of documents. |
| 273 | * @param positions Cursor positions to be sorted. |
| 274 | * @param ids Model IDs to be sorted. |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 275 | */ |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 276 | private static void binarySort( |
| 277 | long[] sortKey, String[] mimeTypes, int[] positions, List<String> ids) { |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 278 | final int count = positions.length; |
| 279 | for (int start = 1; start < count; start++) { |
| 280 | final int pivotPosition = positions[start]; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 281 | final long pivotValue = sortKey[start]; |
| 282 | final String pivotMime = mimeTypes[start]; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 283 | final String pivotId = ids.get(start); |
| 284 | |
| 285 | int left = 0; |
| 286 | int right = start; |
| 287 | |
| 288 | while (left < right) { |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 289 | int mid = ((left + right) >>> 1); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 290 | |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 291 | // First bucket by mime type. Directories always go in front. |
| 292 | int compare = 0; |
| 293 | final boolean lhsIsDir = Document.MIME_TYPE_DIR.equals(pivotMime); |
| 294 | final boolean rhsIsDir = Document.MIME_TYPE_DIR.equals(mimeTypes[mid]); |
| 295 | if (lhsIsDir && !rhsIsDir) { |
| 296 | compare = -1; |
| 297 | } else if (!lhsIsDir && rhsIsDir) { |
| 298 | compare = 1; |
| 299 | } else { |
| 300 | final long lhs = pivotValue; |
| 301 | final long rhs = sortKey[mid]; |
| 302 | // Sort in descending numerical order. This matches legacy behaviour, which yields |
| 303 | // largest or most recent items on top. |
| 304 | compare = -Long.compare(lhs, rhs); |
| 305 | } |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 306 | |
| 307 | if (compare < 0) { |
| 308 | right = mid; |
| 309 | } else { |
| 310 | left = mid + 1; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | int n = start - left; |
| 315 | switch (n) { |
| 316 | case 2: |
| 317 | positions[left + 2] = positions[left + 1]; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 318 | sortKey[left + 2] = sortKey[left + 1]; |
| 319 | mimeTypes[left + 2] = mimeTypes[left + 1]; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 320 | ids.set(left + 2, ids.get(left + 1)); |
| 321 | case 1: |
| 322 | positions[left + 1] = positions[left]; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 323 | sortKey[left + 1] = sortKey[left]; |
| 324 | mimeTypes[left + 1] = mimeTypes[left]; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 325 | ids.set(left + 1, ids.get(left)); |
| 326 | break; |
| 327 | default: |
| 328 | System.arraycopy(positions, left, positions, left + 1, n); |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 329 | System.arraycopy(sortKey, left, sortKey, left + 1, n); |
| 330 | System.arraycopy(mimeTypes, left, mimeTypes, left + 1, n); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 331 | for (int i = n; i >= 1; --i) { |
| 332 | ids.set(left + i, ids.get(left + i - 1)); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | positions[left] = pivotPosition; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame^] | 337 | sortKey[left] = pivotValue; |
| 338 | mimeTypes[left] = pivotMime; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 339 | ids.set(left, pivotId); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
| 343 | @Nullable Cursor getItem(String modelId) { |
| 344 | Integer pos = mPositions.get(modelId); |
| 345 | if (pos != null) { |
| 346 | mCursor.moveToPosition(pos); |
| 347 | return mCursor; |
| 348 | } |
| 349 | return null; |
| 350 | } |
| 351 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 352 | boolean isEmpty() { |
| 353 | return mCursorCount == 0; |
| 354 | } |
| 355 | |
| 356 | boolean isLoading() { |
| 357 | return mIsLoading; |
| 358 | } |
| 359 | |
| 360 | List<DocumentInfo> getDocuments(Selection items) { |
| 361 | final int size = (items != null) ? items.size() : 0; |
| 362 | |
| 363 | final List<DocumentInfo> docs = new ArrayList<>(size); |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 364 | for (String modelId: items.getAll()) { |
| 365 | final Cursor cursor = getItem(modelId); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 366 | checkNotNull(cursor, "Cursor cannot be null."); |
| 367 | final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor); |
| 368 | docs.add(doc); |
| 369 | } |
| 370 | return docs; |
| 371 | } |
| 372 | |
| 373 | @Override |
| 374 | public Cursor getCursor() { |
| 375 | if (Looper.myLooper() != Looper.getMainLooper()) { |
| 376 | throw new IllegalStateException("Can't call getCursor from non-main thread."); |
| 377 | } |
| 378 | return mCursor; |
| 379 | } |
| 380 | |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 381 | public void delete(Selection selected, DeletionListener listener) { |
| 382 | final ContentResolver resolver = mContext.getContentResolver(); |
| 383 | new DeleteFilesTask(resolver, listener).execute(selected); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /** |
| 387 | * A Task which collects the DocumentInfo for documents that have been marked for deletion, |
| 388 | * and actually deletes them. |
| 389 | */ |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 390 | private class DeleteFilesTask extends AsyncTask<Selection, Void, Void> { |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 391 | private ContentResolver mResolver; |
| 392 | private DeletionListener mListener; |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 393 | private boolean mHadTrouble; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 394 | |
| 395 | /** |
| 396 | * @param resolver A ContentResolver for performing the actual file deletions. |
| 397 | * @param errorCallback A Runnable that is executed in the event that one or more errors |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 398 | * occurred while copying files. Execution will occur on the UI thread. |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 399 | */ |
| 400 | public DeleteFilesTask(ContentResolver resolver, DeletionListener listener) { |
| 401 | mResolver = resolver; |
| 402 | mListener = listener; |
| 403 | } |
| 404 | |
| 405 | @Override |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 406 | protected Void doInBackground(Selection... selected) { |
| 407 | List<DocumentInfo> toDelete = getDocuments(selected[0]); |
| 408 | mHadTrouble = false; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 409 | |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 410 | for (DocumentInfo doc : toDelete) { |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 411 | if (!doc.isDeleteSupported()) { |
| 412 | Log.w(TAG, doc + " could not be deleted. Skipping..."); |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 413 | mHadTrouble = true; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 414 | continue; |
| 415 | } |
| 416 | |
| 417 | ContentProviderClient client = null; |
| 418 | try { |
| 419 | if (DEBUG) Log.d(TAG, "Deleting: " + doc.displayName); |
| 420 | client = DocumentsApplication.acquireUnstableProviderOrThrow( |
| 421 | mResolver, doc.derivedUri.getAuthority()); |
| 422 | DocumentsContract.deleteDocument(client, doc.derivedUri); |
| 423 | } catch (Exception e) { |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 424 | Log.w(TAG, "Failed to delete " + doc, e); |
| 425 | mHadTrouble = true; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 426 | } finally { |
| 427 | ContentProviderClient.releaseQuietly(client); |
| 428 | } |
| 429 | } |
| 430 | |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 431 | return null; |
| 432 | } |
| 433 | |
| 434 | @Override |
| 435 | protected void onPostExecute(Void _) { |
| 436 | if (mHadTrouble) { |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 437 | // TODO show which files failed? b/23720103 |
| 438 | mListener.onError(); |
| 439 | if (DEBUG) Log.d(TAG, "Deletion task completed. Some deletions failed."); |
| 440 | } else { |
| 441 | if (DEBUG) Log.d(TAG, "Deletion task completed successfully."); |
| 442 | } |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 443 | |
| 444 | mListener.onCompletion(); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | static class DeletionListener { |
| 449 | /** |
| 450 | * Called when deletion has completed (regardless of whether an error occurred). |
| 451 | */ |
| 452 | void onCompletion() {} |
| 453 | |
| 454 | /** |
| 455 | * Called at the end of a deletion operation that produced one or more errors. |
| 456 | */ |
| 457 | void onError() {} |
| 458 | } |
| 459 | |
| 460 | void addUpdateListener(UpdateListener listener) { |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 461 | mUpdateListeners.add(listener); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 462 | } |
| 463 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 464 | static 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 | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 475 | |
| 476 | /** |
| 477 | * @return An ordered array of model IDs representing the documents in the model. It is sorted |
| 478 | * according to the current sort order, which was set by the last model update. |
| 479 | */ |
| 480 | public List<String> getModelIds() { |
| 481 | return mIds; |
| 482 | } |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 483 | } |