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