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.getCursorString; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 20 | import static com.android.documentsui.base.Shared.DEBUG; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 21 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 22 | import android.annotation.IntDef; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 23 | import android.database.Cursor; |
Tomasz Mikolajewski | 20c04c5 | 2016-03-15 15:55:46 +0900 | [diff] [blame] | 24 | import android.database.MergeCursor; |
Steve McKay | 84769b8 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 25 | import android.net.Uri; |
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; |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame] | 34 | import com.android.documentsui.base.DocumentInfo; |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 35 | import com.android.documentsui.base.EventListener; |
Steve McKay | d9caa6a | 2016-09-15 16:36:45 -0700 | [diff] [blame] | 36 | import com.android.documentsui.roots.RootCursorWrapper; |
Steve McKay | 4f78ba6 | 2016-10-04 16:48:49 -0700 | [diff] [blame] | 37 | import com.android.documentsui.selection.Selection; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 38 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 39 | import java.lang.annotation.Retention; |
| 40 | import java.lang.annotation.RetentionPolicy; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 41 | import java.util.ArrayList; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 42 | import java.util.HashMap; |
| 43 | import java.util.List; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 44 | import java.util.Map; |
Steve McKay | d071895 | 2016-10-10 13:43:36 -0700 | [diff] [blame^] | 45 | import java.util.function.Predicate; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * The data model for the current loaded directory. |
| 49 | */ |
| 50 | @VisibleForTesting |
Tomasz Mikolajewski | d71bd61 | 2016-02-16 12:28:43 +0900 | [diff] [blame] | 51 | public class Model { |
Steve McKay | d071895 | 2016-10-10 13:43:36 -0700 | [diff] [blame^] | 52 | |
| 53 | /** |
| 54 | * Filter that passes (returns true) all non-virtual, non-partial files. |
| 55 | */ |
| 56 | public static final Predicate<Cursor> CONCRETE_FILE_FILTER = (Cursor c) -> { |
| 57 | int flags = DocumentInfo.getCursorInt(c, Document.COLUMN_FLAGS); |
| 58 | return (flags & Document.FLAG_VIRTUAL_DOCUMENT) == 0 |
| 59 | && (flags & Document.FLAG_PARTIAL) == 0; |
| 60 | }; |
| 61 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 62 | private static final String TAG = "Model"; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 63 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 64 | private boolean mIsLoading; |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 65 | private List<EventListener<Update>> mUpdateListeners = new ArrayList<>(); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 66 | @Nullable private Cursor mCursor; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 67 | private int mCursorCount; |
| 68 | /** Maps Model ID to cursor positions, for looking up items by Model ID. */ |
| 69 | private Map<String, Integer> mPositions = new HashMap<>(); |
Tomasz Mikolajewski | f27c274 | 2016-03-07 18:01:45 +0900 | [diff] [blame] | 70 | private String mIds[] = new String[0]; |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 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 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 76 | public void addUpdateListener(EventListener<Update> listener) { |
| 77 | mUpdateListeners.add(listener); |
| 78 | } |
| 79 | |
| 80 | public void removeUpdateListener(EventListener<Update> listener) { |
| 81 | mUpdateListeners.remove(listener); |
| 82 | } |
| 83 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 84 | private void notifyUpdateListeners() { |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 85 | for (EventListener<Update> handler: mUpdateListeners) { |
| 86 | handler.accept(Update.UPDATE); |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | private void notifyUpdateListeners(Exception e) { |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 91 | Update error = new Update(e); |
| 92 | for (EventListener<Update> handler: mUpdateListeners) { |
| 93 | handler.accept(error); |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 97 | void onLoaderReset() { |
| 98 | if (mIsLoading) { |
Steve McKay | 7c66209 | 2016-08-26 12:17:41 -0700 | [diff] [blame] | 99 | Log.w(TAG, "Received unexpected loader reset while in loading state for doc: " |
| 100 | + DocumentInfo.debugString(doc)); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 101 | } |
Steve McKay | 7c66209 | 2016-08-26 12:17:41 -0700 | [diff] [blame] | 102 | |
| 103 | reset(); |
Steve McKay | 9de0da6 | 2016-08-25 15:18:23 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | private void reset() { |
| 107 | mCursor = null; |
| 108 | mCursorCount = 0; |
| 109 | mIds = new String[0]; |
| 110 | mPositions.clear(); |
| 111 | info = null; |
| 112 | error = null; |
| 113 | doc = null; |
| 114 | mIsLoading = false; |
| 115 | notifyUpdateListeners(); |
| 116 | } |
| 117 | |
| 118 | void update(DirectoryResult result) { |
| 119 | assert(result != null); |
| 120 | |
| 121 | if (DEBUG) Log.i(TAG, "Updating model with new result set."); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 122 | |
| 123 | if (result.exception != null) { |
| 124 | Log.e(TAG, "Error while loading directory contents", result.exception); |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 125 | notifyUpdateListeners(result.exception); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 126 | return; |
| 127 | } |
| 128 | |
| 129 | mCursor = result.cursor; |
| 130 | mCursorCount = mCursor.getCount(); |
Tomasz Mikolajewski | e29e341 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 131 | doc = result.doc; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 132 | |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 133 | updateModelData(); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 134 | |
| 135 | final Bundle extras = mCursor.getExtras(); |
| 136 | if (extras != null) { |
| 137 | info = extras.getString(DocumentsContract.EXTRA_INFO); |
| 138 | error = extras.getString(DocumentsContract.EXTRA_ERROR); |
| 139 | mIsLoading = extras.getBoolean(DocumentsContract.EXTRA_LOADING, false); |
| 140 | } |
| 141 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 142 | notifyUpdateListeners(); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Ben Kwa | d72a1da | 2015-12-01 19:56:57 -0800 | [diff] [blame] | 145 | @VisibleForTesting |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 146 | int getItemCount() { |
Ben Kwa | da858bf | 2015-12-09 14:33:49 -0800 | [diff] [blame] | 147 | return mCursorCount; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 151 | * Scan over the incoming cursor data, generate Model IDs for each row, and sort the IDs |
| 152 | * according to the current sort order. |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 153 | */ |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 154 | private void updateModelData() { |
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 | |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 157 | mCursor.moveToPosition(-1); |
| 158 | for (int pos = 0; pos < mCursorCount; ++pos) { |
Ben Lin | 2df30e5 | 2016-04-22 16:12:50 -0700 | [diff] [blame] | 159 | if (!mCursor.moveToNext()) { |
| 160 | Log.e(TAG, "Fail to move cursor to next pos: " + pos); |
| 161 | return; |
| 162 | } |
Tomasz Mikolajewski | 985df3d | 2016-03-15 15:38:54 +0900 | [diff] [blame] | 163 | // Generates a Model ID for a cursor entry that refers to a document. The Model ID is a |
| 164 | // 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] | 165 | // If the cursor is a merged cursor over multiple authorities, then prefix the ids |
| 166 | // with the authority to avoid collisions. |
| 167 | if (mCursor instanceof MergeCursor) { |
Garfield Tan | 2010ff7 | 2016-09-30 14:55:32 -0700 | [diff] [blame] | 168 | mIds[pos] = getCursorString(mCursor, RootCursorWrapper.COLUMN_AUTHORITY) |
| 169 | + "|" + getCursorString(mCursor, Document.COLUMN_DOCUMENT_ID); |
Tomasz Mikolajewski | 20c04c5 | 2016-03-15 15:55:46 +0900 | [diff] [blame] | 170 | } else { |
Tomasz Mikolajewski | 06b036f | 2016-04-26 11:11:17 +0900 | [diff] [blame] | 171 | mIds[pos] = getCursorString(mCursor, Document.COLUMN_DOCUMENT_ID); |
Tomasz Mikolajewski | 20c04c5 | 2016-03-15 15:55:46 +0900 | [diff] [blame] | 172 | } |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // Populate the positions. |
| 176 | mPositions.clear(); |
| 177 | for (int i = 0; i < mCursorCount; ++i) { |
Garfield Tan | 2010ff7 | 2016-09-30 14:55:32 -0700 | [diff] [blame] | 178 | mPositions.put(mIds[i], i); |
Ben Kwa | b8a5e08 | 2015-12-07 13:25:27 -0800 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
Tomasz Mikolajewski | d71bd61 | 2016-02-16 12:28:43 +0900 | [diff] [blame] | 182 | public @Nullable Cursor getItem(String modelId) { |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 183 | Integer pos = mPositions.get(modelId); |
Steve McKay | 5a22a11 | 2016-04-12 11:29:10 -0700 | [diff] [blame] | 184 | if (pos == null) { |
| 185 | if (DEBUG) Log.d(TAG, "Unabled to find cursor position for modelId: " + modelId); |
| 186 | return null; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 187 | } |
Steve McKay | 5a22a11 | 2016-04-12 11:29:10 -0700 | [diff] [blame] | 188 | |
| 189 | if (!mCursor.moveToPosition(pos)) { |
| 190 | if (DEBUG) Log.d(TAG, |
| 191 | "Unabled to move cursor to position " + pos + " for modelId: " + modelId); |
| 192 | return null; |
| 193 | } |
| 194 | |
| 195 | return mCursor; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 198 | public boolean isEmpty() { |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 199 | return mCursorCount == 0; |
| 200 | } |
| 201 | |
| 202 | boolean isLoading() { |
| 203 | return mIsLoading; |
| 204 | } |
| 205 | |
Steve McKay | c8889af | 2016-09-23 11:22:41 -0700 | [diff] [blame] | 206 | public List<DocumentInfo> getDocuments(Selection selection) { |
Steve McKay | 84769b8 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 207 | final int size = (selection != null) ? selection.size() : 0; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 208 | |
| 209 | final List<DocumentInfo> docs = new ArrayList<>(size); |
Steve McKay | 84769b8 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 210 | // NOTE: That as this now iterates over only final (non-provisional) selection. |
| 211 | for (String modelId: selection) { |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 212 | DocumentInfo doc = getDocument(modelId); |
| 213 | if (doc == null) { |
| 214 | Log.w(TAG, "Unable to obtain document for modelId: " + modelId); |
Steve McKay | 5a22a11 | 2016-04-12 11:29:10 -0700 | [diff] [blame] | 215 | continue; |
| 216 | } |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 217 | docs.add(doc); |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 218 | } |
| 219 | return docs; |
| 220 | } |
| 221 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 222 | public @Nullable DocumentInfo getDocument(String modelId) { |
| 223 | final Cursor cursor = getItem(modelId); |
| 224 | return (cursor == null) |
| 225 | ? null |
| 226 | : DocumentInfo.fromDirectoryCursor(cursor); |
| 227 | } |
| 228 | |
Steve McKay | d071895 | 2016-10-10 13:43:36 -0700 | [diff] [blame^] | 229 | public List<DocumentInfo> loadDocuments(Selection selection, Predicate<Cursor> filter) { |
| 230 | final int size = (selection != null) ? selection.size() : 0; |
| 231 | |
| 232 | final List<DocumentInfo> docs = new ArrayList<>(size); |
| 233 | for (String modelId: selection) { |
| 234 | loadDocument(docs, modelId, filter); |
| 235 | } |
| 236 | return docs; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * @param docs |
| 241 | * @return DocumentInfo, or null. If filter returns false, null will be returned. |
| 242 | */ |
| 243 | private void loadDocument( |
| 244 | List<DocumentInfo> docs, String modelId, Predicate<Cursor> filter) { |
| 245 | final Cursor cursor = getItem(modelId); |
| 246 | |
| 247 | if (cursor == null) { |
| 248 | Log.w(TAG, "Unable to obtain document for modelId: " + modelId); |
| 249 | } |
| 250 | |
| 251 | if (filter.test(cursor)) { |
| 252 | docs.add(DocumentInfo.fromDirectoryCursor(cursor)); |
| 253 | } else { |
| 254 | if (DEBUG) Log.v(TAG, "Filtered document from results: " + modelId); |
| 255 | } |
| 256 | } |
| 257 | |
Steve McKay | 84769b8 | 2016-06-09 10:46:07 -0700 | [diff] [blame] | 258 | public Uri getItemUri(String modelId) { |
| 259 | final Cursor cursor = getItem(modelId); |
| 260 | return DocumentInfo.getUri(cursor); |
| 261 | } |
| 262 | |
Garfield, Tan | 11d2348 | 2016-08-05 09:33:29 -0700 | [diff] [blame] | 263 | /** |
| 264 | * @return An ordered array of model IDs representing the documents in the model. It is sorted |
| 265 | * according to the current sort order, which was set by the last model update. |
| 266 | */ |
| 267 | public String[] getModelIds() { |
| 268 | return mIds; |
| 269 | } |
| 270 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 271 | public static class Update { |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 272 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 273 | public static final Update UPDATE = new Update(); |
Ben Kwa | 472103f | 2016-02-10 15:48:25 -0800 | [diff] [blame] | 274 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 275 | @IntDef(value = { |
| 276 | TYPE_UPDATE, |
| 277 | TYPE_UPDATE_ERROR |
| 278 | }) |
| 279 | @Retention(RetentionPolicy.SOURCE) |
| 280 | public @interface UpdateType {} |
| 281 | public static final int TYPE_UPDATE = 0; |
| 282 | public static final int TYPE_UPDATE_ERROR = 1; |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 283 | |
Steve McKay | 990f76e | 2016-09-16 12:36:58 -0700 | [diff] [blame] | 284 | private final @UpdateType int mType; |
| 285 | private final @Nullable Exception mException; |
| 286 | |
| 287 | private Update() { |
| 288 | mType = TYPE_UPDATE; |
| 289 | mException = null; |
| 290 | } |
| 291 | |
| 292 | public Update(Exception exception) { |
| 293 | assert(exception != null); |
| 294 | mType = TYPE_UPDATE_ERROR; |
| 295 | mException = exception; |
| 296 | } |
| 297 | |
| 298 | public boolean isUpdate() { |
| 299 | return mType == TYPE_UPDATE; |
| 300 | } |
| 301 | |
| 302 | public boolean hasError() { |
| 303 | return mType == TYPE_UPDATE_ERROR; |
| 304 | } |
| 305 | |
| 306 | public @Nullable Exception getError() { |
| 307 | return mException; |
| 308 | } |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 309 | } |
Ben Kwa | 0497da8 | 2015-11-30 23:00:02 -0800 | [diff] [blame] | 310 | } |