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