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