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 | |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.base.DocumentInfo.getCursorLong; |
| 20 | import static com.android.documentsui.base.DocumentInfo.getCursorString; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 21 | import static com.android.documentsui.base.Shared.DEBUG; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 22 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 23 | import android.annotation.IntDef; |
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; |
Steve McKay | 84769b8 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 26 | import android.net.Uri; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 27 | import android.os.Bundle; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 28 | import android.provider.DocumentsContract; |
| 29 | import android.provider.DocumentsContract.Document; |
| 30 | import android.support.annotation.Nullable; |
| 31 | import android.support.annotation.VisibleForTesting; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 32 | import android.util.Log; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 33 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 34 | import com.android.documentsui.DirectoryResult; |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame] | 35 | import com.android.documentsui.base.DocumentInfo; |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 36 | import com.android.documentsui.base.EventListener; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 37 | import com.android.documentsui.base.Shared; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 38 | import com.android.documentsui.dirlist.MultiSelectManager.Selection; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 39 | import com.android.documentsui.roots.RootCursorWrapper; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 40 | import com.android.documentsui.sorting.SortDimension; |
| 41 | import com.android.documentsui.sorting.SortModel; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 42 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 43 | import java.lang.annotation.Retention; |
| 44 | import java.lang.annotation.RetentionPolicy; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 45 | import java.util.ArrayList; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 46 | import java.util.HashMap; |
| 47 | import java.util.List; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 48 | import java.util.Map; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * The data model for the current loaded directory. |
| 52 | */ |
| 53 | @VisibleForTesting |
Tomasz Mikolajewski | d71bd61 | 2016-02-16 12:28:43 +0900 | [diff] [blame] | 54 | public class Model { |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 55 | private static final String TAG = "Model"; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 56 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 57 | private boolean mIsLoading; |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 58 | private List<EventListener<Update>> mUpdateListeners = new ArrayList<>(); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 59 | @Nullable private Cursor mCursor; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 60 | private int mCursorCount; |
| 61 | /** Maps Model ID to cursor positions, for looking up items by Model ID. */ |
| 62 | private Map<String, Integer> mPositions = new HashMap<>(); |
| 63 | /** |
| 64 | * A sorted array of model IDs for the files currently in the Model. Sort order is determined |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 65 | * by {@link #mSortModel} |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 66 | */ |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 67 | private String mIds[] = new String[0]; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 68 | private SortModel mSortModel; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 69 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 70 | @Nullable String info; |
| 71 | @Nullable String error; |
Tomasz Mikolajewski | e29e341 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 72 | @Nullable DocumentInfo doc; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 73 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 74 | public void addUpdateListener(EventListener<Update> listener) { |
| 75 | mUpdateListeners.add(listener); |
| 76 | } |
| 77 | |
| 78 | public void removeUpdateListener(EventListener<Update> listener) { |
| 79 | mUpdateListeners.remove(listener); |
| 80 | } |
| 81 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 82 | private void notifyUpdateListeners() { |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 83 | for (EventListener<Update> handler: mUpdateListeners) { |
| 84 | handler.accept(Update.UPDATE); |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | private void notifyUpdateListeners(Exception e) { |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 89 | Update error = new Update(e); |
| 90 | for (EventListener<Update> handler: mUpdateListeners) { |
| 91 | handler.accept(error); |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 95 | void onLoaderReset() { |
| 96 | if (mIsLoading) { |
Steve McKay | 7c66209 | 2016-08-26 12:17:41 -0700 | [diff] [blame] | 97 | Log.w(TAG, "Received unexpected loader reset while in loading state for doc: " |
| 98 | + DocumentInfo.debugString(doc)); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 99 | } |
Steve McKay | 7c66209 | 2016-08-26 12:17:41 -0700 | [diff] [blame] | 100 | |
| 101 | reset(); |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | private void reset() { |
| 105 | mCursor = null; |
| 106 | mCursorCount = 0; |
| 107 | mIds = new String[0]; |
| 108 | mPositions.clear(); |
| 109 | info = null; |
| 110 | error = null; |
| 111 | doc = null; |
| 112 | mIsLoading = false; |
| 113 | notifyUpdateListeners(); |
| 114 | } |
| 115 | |
| 116 | void update(DirectoryResult result) { |
| 117 | assert(result != null); |
| 118 | |
| 119 | if (DEBUG) Log.i(TAG, "Updating model with new result set."); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 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(); |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 129 | mSortModel = result.sortModel; |
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 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 160 | final int id = mSortModel.getSortedDimensionId(); |
| 161 | switch (id) { |
| 162 | case SortModel.SORT_DIMENSION_ID_TITLE: |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 163 | displayNames = new String[mCursorCount]; |
| 164 | break; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 165 | case SortModel.SORT_DIMENSION_ID_DATE: |
| 166 | case SortModel.SORT_DIMENSION_ID_SIZE: |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 167 | longValues = new long[mCursorCount]; |
| 168 | break; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 171 | String mimeType; |
| 172 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 173 | mCursor.moveToPosition(-1); |
| 174 | for (int pos = 0; pos < mCursorCount; ++pos) { |
Ben Lin | 2df30e5 | 2016-04-22 16:12:50 -0700 | [diff] [blame] | 175 | if (!mCursor.moveToNext()) { |
| 176 | Log.e(TAG, "Fail to move cursor to next pos: " + pos); |
| 177 | return; |
| 178 | } |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 179 | positions[pos] = pos; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 180 | |
Tomasz Mikolajewski | 985df3d | 2016-03-15 15:38:54 +0900 | [diff] [blame] | 181 | // Generates a Model ID for a cursor entry that refers to a document. The Model ID is a |
| 182 | // 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] | 183 | // If the cursor is a merged cursor over multiple authorities, then prefix the ids |
| 184 | // with the authority to avoid collisions. |
| 185 | if (mCursor instanceof MergeCursor) { |
Tomasz Mikolajewski | 06b036f | 2016-04-26 11:11:17 +0900 | [diff] [blame] | 186 | mIds[pos] = getCursorString(mCursor, RootCursorWrapper.COLUMN_AUTHORITY) + "|" + |
| 187 | getCursorString(mCursor, Document.COLUMN_DOCUMENT_ID); |
Tomasz Mikolajewski | 20c04c5 | 2016-03-15 15:55:46 +0900 | [diff] [blame] | 188 | } else { |
Tomasz Mikolajewski | 06b036f | 2016-04-26 11:11:17 +0900 | [diff] [blame] | 189 | mIds[pos] = getCursorString(mCursor, Document.COLUMN_DOCUMENT_ID); |
Tomasz Mikolajewski | 20c04c5 | 2016-03-15 15:55:46 +0900 | [diff] [blame] | 190 | } |
Tomasz Mikolajewski | 985df3d | 2016-03-15 15:38:54 +0900 | [diff] [blame] | 191 | |
Tomasz Mikolajewski | 06b036f | 2016-04-26 11:11:17 +0900 | [diff] [blame] | 192 | mimeType = getCursorString(mCursor, Document.COLUMN_MIME_TYPE); |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 193 | isDirs[pos] = Document.MIME_TYPE_DIR.equals(mimeType); |
| 194 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 195 | switch(id) { |
| 196 | case SortModel.SORT_DIMENSION_ID_TITLE: |
Tomasz Mikolajewski | 06b036f | 2016-04-26 11:11:17 +0900 | [diff] [blame] | 197 | final String displayName = getCursorString( |
| 198 | mCursor, Document.COLUMN_DISPLAY_NAME); |
| 199 | displayNames[pos] = displayName; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 200 | break; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 201 | case SortModel.SORT_DIMENSION_ID_DATE: |
Tomasz Mikolajewski | 06b036f | 2016-04-26 11:11:17 +0900 | [diff] [blame] | 202 | longValues[pos] = getLastModified(mCursor); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 203 | break; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 204 | case SortModel.SORT_DIMENSION_ID_SIZE: |
Tomasz Mikolajewski | 06b036f | 2016-04-26 11:11:17 +0900 | [diff] [blame] | 205 | longValues[pos] = getCursorLong(mCursor, Document.COLUMN_SIZE); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 206 | break; |
| 207 | } |
| 208 | } |
| 209 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 210 | final SortDimension dimension = mSortModel.getDimensionById(id); |
| 211 | switch (id) { |
| 212 | case SortModel.SORT_DIMENSION_ID_TITLE: |
| 213 | binarySort(displayNames, isDirs, positions, mIds, dimension.getSortDirection()); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 214 | break; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 215 | case SortModel.SORT_DIMENSION_ID_DATE: |
| 216 | case SortModel.SORT_DIMENSION_ID_SIZE: |
| 217 | binarySort(longValues, isDirs, positions, mIds, dimension.getSortDirection()); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 218 | break; |
| 219 | } |
| 220 | |
| 221 | // Populate the positions. |
| 222 | mPositions.clear(); |
| 223 | for (int i = 0; i < mCursorCount; ++i) { |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 224 | mPositions.put(mIds[i], positions[i]); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | /** |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 229 | * 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] | 230 | * sort key. Rows are sorted in ascending alphabetical order on the sort key. |
| 231 | * Directories are always shown first. This code is based on TimSort.binarySort(). |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 232 | * |
| 233 | * @param sortKey Data is sorted in ascending alphabetical order. |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 234 | * @param isDirs Array saying whether an item is a directory or not. |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 235 | * @param positions Cursor positions to be sorted. |
| 236 | * @param ids Model IDs to be sorted. |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 237 | */ |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 238 | private static void binarySort( |
| 239 | String[] sortKey, |
| 240 | boolean[] isDirs, |
| 241 | int[] positions, |
| 242 | String[] ids, |
| 243 | @SortDimension.SortDirection int direction) { |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 244 | final int count = positions.length; |
| 245 | for (int start = 1; start < count; start++) { |
| 246 | final int pivotPosition = positions[start]; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 247 | final String pivotValue = sortKey[start]; |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 248 | final boolean pivotIsDir = isDirs[start]; |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 249 | final String pivotId = ids[start]; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 250 | |
| 251 | int left = 0; |
| 252 | int right = start; |
| 253 | |
| 254 | while (left < right) { |
| 255 | int mid = (left + right) >>> 1; |
| 256 | |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 257 | // Directories always go in front. |
| 258 | int compare = 0; |
| 259 | final boolean rhsIsDir = isDirs[mid]; |
| 260 | if (pivotIsDir && !rhsIsDir) { |
| 261 | compare = -1; |
| 262 | } else if (!pivotIsDir && rhsIsDir) { |
| 263 | compare = 1; |
| 264 | } else { |
| 265 | final String lhs = pivotValue; |
| 266 | final String rhs = sortKey[mid]; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 267 | switch (direction) { |
| 268 | case SortDimension.SORT_DIRECTION_ASCENDING: |
| 269 | compare = Shared.compareToIgnoreCaseNullable(lhs, rhs); |
| 270 | break; |
| 271 | case SortDimension.SORT_DIRECTION_DESCENDING: |
| 272 | compare = -Shared.compareToIgnoreCaseNullable(lhs, rhs); |
| 273 | break; |
| 274 | default: |
| 275 | throw new IllegalArgumentException( |
| 276 | "Unknown sorting direction: " + direction); |
| 277 | } |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 278 | } |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 279 | |
| 280 | if (compare < 0) { |
| 281 | right = mid; |
| 282 | } else { |
| 283 | left = mid + 1; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | int n = start - left; |
| 288 | switch (n) { |
| 289 | case 2: |
| 290 | positions[left + 2] = positions[left + 1]; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 291 | sortKey[left + 2] = sortKey[left + 1]; |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 292 | isDirs[left + 2] = isDirs[left + 1]; |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 293 | ids[left + 2] = ids[left + 1]; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 294 | case 1: |
| 295 | positions[left + 1] = positions[left]; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 296 | sortKey[left + 1] = sortKey[left]; |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 297 | isDirs[left + 1] = isDirs[left]; |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 298 | ids[left + 1] = ids[left]; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 299 | break; |
| 300 | default: |
| 301 | System.arraycopy(positions, left, positions, left + 1, n); |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 302 | System.arraycopy(sortKey, left, sortKey, left + 1, n); |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 303 | System.arraycopy(isDirs, left, isDirs, left + 1, n); |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 304 | System.arraycopy(ids, left, ids, left + 1, n); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | positions[left] = pivotPosition; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 308 | sortKey[left] = pivotValue; |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 309 | isDirs[left] = pivotIsDir; |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 310 | ids[left] = pivotId; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | /** |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 315 | * Sorts model data. Takes four columns of index-corresponded data. The first column is the sort |
| 316 | * key, and the second is an array of mime types. The rows are first bucketed by mime type |
| 317 | * (directories vs documents) and then each bucket is sorted independently in descending |
| 318 | * numerical order on the sort key. This code is based on TimSort.binarySort(). |
| 319 | * |
| 320 | * @param sortKey Data is sorted in descending numerical order. |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 321 | * @param isDirs Array saying whether an item is a directory or not. |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 322 | * @param positions Cursor positions to be sorted. |
| 323 | * @param ids Model IDs to be sorted. |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 324 | */ |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 325 | private static void binarySort( |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 326 | long[] sortKey, |
| 327 | boolean[] isDirs, |
| 328 | int[] positions, |
| 329 | String[] ids, |
| 330 | @SortDimension.SortDirection int direction) { |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 331 | final int count = positions.length; |
| 332 | for (int start = 1; start < count; start++) { |
| 333 | final int pivotPosition = positions[start]; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 334 | final long pivotValue = sortKey[start]; |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 335 | final boolean pivotIsDir = isDirs[start]; |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 336 | final String pivotId = ids[start]; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 337 | |
| 338 | int left = 0; |
| 339 | int right = start; |
| 340 | |
| 341 | while (left < right) { |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 342 | int mid = ((left + right) >>> 1); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 343 | |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 344 | // Directories always go in front. |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 345 | int compare = 0; |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 346 | final boolean rhsIsDir = isDirs[mid]; |
| 347 | if (pivotIsDir && !rhsIsDir) { |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 348 | compare = -1; |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 349 | } else if (!pivotIsDir && rhsIsDir) { |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 350 | compare = 1; |
| 351 | } else { |
| 352 | final long lhs = pivotValue; |
| 353 | final long rhs = sortKey[mid]; |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 354 | switch (direction) { |
| 355 | case SortDimension.SORT_DIRECTION_ASCENDING: |
| 356 | compare = Long.compare(lhs, rhs); |
| 357 | break; |
| 358 | case SortDimension.SORT_DIRECTION_DESCENDING: |
| 359 | compare = -Long.compare(lhs, rhs); |
| 360 | break; |
| 361 | default: |
| 362 | throw new IllegalArgumentException( |
| 363 | "Unknown sorting direction: " + direction); |
| 364 | } |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 365 | } |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 366 | |
Ben Kwa | e433834 | 2016-01-08 15:34:47 -0800 | [diff] [blame] | 367 | // If numerical comparison yields a tie, use document ID as a tie breaker. This |
| 368 | // will yield stable results even if incoming items are continually shuffling and |
| 369 | // have identical numerical sort keys. One common example of this scenario is seen |
| 370 | // when sorting a set of active downloads by mod time. |
| 371 | if (compare == 0) { |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 372 | compare = pivotId.compareTo(ids[mid]); |
Ben Kwa | e433834 | 2016-01-08 15:34:47 -0800 | [diff] [blame] | 373 | } |
| 374 | |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 375 | if (compare < 0) { |
| 376 | right = mid; |
| 377 | } else { |
| 378 | left = mid + 1; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | int n = start - left; |
| 383 | switch (n) { |
| 384 | case 2: |
| 385 | positions[left + 2] = positions[left + 1]; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 386 | sortKey[left + 2] = sortKey[left + 1]; |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 387 | isDirs[left + 2] = isDirs[left + 1]; |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 388 | ids[left + 2] = ids[left + 1]; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 389 | case 1: |
| 390 | positions[left + 1] = positions[left]; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 391 | sortKey[left + 1] = sortKey[left]; |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 392 | isDirs[left + 1] = isDirs[left]; |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 393 | ids[left + 1] = ids[left]; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 394 | break; |
| 395 | default: |
| 396 | System.arraycopy(positions, left, positions, left + 1, n); |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 397 | System.arraycopy(sortKey, left, sortKey, left + 1, n); |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 398 | System.arraycopy(isDirs, left, isDirs, left + 1, n); |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 399 | System.arraycopy(ids, left, ids, left + 1, n); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | positions[left] = pivotPosition; |
Ben Kwa | 6280de0 | 2015-12-16 19:42:08 -0800 | [diff] [blame] | 403 | sortKey[left] = pivotValue; |
Tomasz Mikolajewski | 61e315d | 2016-03-15 13:23:52 +0900 | [diff] [blame] | 404 | isDirs[left] = pivotIsDir; |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 405 | ids[left] = pivotId; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
Ben Kwa | e433834 | 2016-01-08 15:34:47 -0800 | [diff] [blame] | 409 | /** |
Tomasz Mikolajewski | 985df3d | 2016-03-15 15:38:54 +0900 | [diff] [blame] | 410 | * @return Timestamp for the given document. Some docs (e.g. active downloads) have a null |
Tomasz Mikolajewski | 06b036f | 2016-04-26 11:11:17 +0900 | [diff] [blame] | 411 | * timestamp - these will be replaced with MAX_LONG so that such files get sorted to the top |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 412 | * when sorting descending by date. |
Tomasz Mikolajewski | 985df3d | 2016-03-15 15:38:54 +0900 | [diff] [blame] | 413 | */ |
Tomasz Mikolajewski | 06b036f | 2016-04-26 11:11:17 +0900 | [diff] [blame] | 414 | long getLastModified(Cursor cursor) { |
| 415 | long l = getCursorLong(mCursor, Document.COLUMN_LAST_MODIFIED); |
| 416 | return (l == -1) ? Long.MAX_VALUE : l; |
Ben Kwa | e433834 | 2016-01-08 15:34:47 -0800 | [diff] [blame] | 417 | } |
| 418 | |
Tomasz Mikolajewski | d71bd61 | 2016-02-16 12:28:43 +0900 | [diff] [blame] | 419 | public @Nullable Cursor getItem(String modelId) { |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 420 | Integer pos = mPositions.get(modelId); |
Steve McKay | 5a22a11 | 2016-04-12 11:29:10 -0700 | [diff] [blame] | 421 | if (pos == null) { |
| 422 | if (DEBUG) Log.d(TAG, "Unabled to find cursor position for modelId: " + modelId); |
| 423 | return null; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 424 | } |
Steve McKay | 5a22a11 | 2016-04-12 11:29:10 -0700 | [diff] [blame] | 425 | |
| 426 | if (!mCursor.moveToPosition(pos)) { |
| 427 | if (DEBUG) Log.d(TAG, |
| 428 | "Unabled to move cursor to position " + pos + " for modelId: " + modelId); |
| 429 | return null; |
| 430 | } |
| 431 | |
| 432 | return mCursor; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 433 | } |
| 434 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 435 | public boolean isEmpty() { |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 436 | return mCursorCount == 0; |
| 437 | } |
| 438 | |
| 439 | boolean isLoading() { |
| 440 | return mIsLoading; |
| 441 | } |
| 442 | |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame^] | 443 | public List<DocumentInfo> getDocuments(Selection selection) { |
Steve McKay | 84769b8 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 444 | final int size = (selection != null) ? selection.size() : 0; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 445 | |
| 446 | final List<DocumentInfo> docs = new ArrayList<>(size); |
Steve McKay | 84769b8 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 447 | // NOTE: That as this now iterates over only final (non-provisional) selection. |
| 448 | for (String modelId: selection) { |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 449 | DocumentInfo doc = getDocument(modelId); |
| 450 | if (doc == null) { |
| 451 | Log.w(TAG, "Unable to obtain document for modelId: " + modelId); |
Steve McKay | 5a22a11 | 2016-04-12 11:29:10 -0700 | [diff] [blame] | 452 | continue; |
| 453 | } |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 454 | docs.add(doc); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 455 | } |
| 456 | return docs; |
| 457 | } |
| 458 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 459 | public @Nullable DocumentInfo getDocument(String modelId) { |
| 460 | final Cursor cursor = getItem(modelId); |
| 461 | return (cursor == null) |
| 462 | ? null |
| 463 | : DocumentInfo.fromDirectoryCursor(cursor); |
| 464 | } |
| 465 | |
Steve McKay | 84769b8 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 466 | public Uri getItemUri(String modelId) { |
| 467 | final Cursor cursor = getItem(modelId); |
| 468 | return DocumentInfo.getUri(cursor); |
| 469 | } |
| 470 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 471 | /** |
| 472 | * @return An ordered array of model IDs representing the documents in the model. It is sorted |
| 473 | * according to the current sort order, which was set by the last model update. |
| 474 | */ |
| 475 | public String[] getModelIds() { |
| 476 | return mIds; |
| 477 | } |
| 478 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 479 | public static class Update { |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 480 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 481 | public static final Update UPDATE = new Update(); |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 482 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 483 | @IntDef(value = { |
| 484 | TYPE_UPDATE, |
| 485 | TYPE_UPDATE_ERROR |
| 486 | }) |
| 487 | @Retention(RetentionPolicy.SOURCE) |
| 488 | public @interface UpdateType {} |
| 489 | public static final int TYPE_UPDATE = 0; |
| 490 | public static final int TYPE_UPDATE_ERROR = 1; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 491 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 492 | private final @UpdateType int mType; |
| 493 | private final @Nullable Exception mException; |
| 494 | |
| 495 | private Update() { |
| 496 | mType = TYPE_UPDATE; |
| 497 | mException = null; |
| 498 | } |
| 499 | |
| 500 | public Update(Exception exception) { |
| 501 | assert(exception != null); |
| 502 | mType = TYPE_UPDATE_ERROR; |
| 503 | mException = exception; |
| 504 | } |
| 505 | |
| 506 | public boolean isUpdate() { |
| 507 | return mType == TYPE_UPDATE; |
| 508 | } |
| 509 | |
| 510 | public boolean hasError() { |
| 511 | return mType == TYPE_UPDATE_ERROR; |
| 512 | } |
| 513 | |
| 514 | public @Nullable Exception getError() { |
| 515 | return mException; |
| 516 | } |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 517 | } |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 518 | } |