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