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