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