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