Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | package android.hardware.location; |
| 17 | |
Arthur Ishiguro | e35a6de | 2017-12-20 15:30:51 -0800 | [diff] [blame] | 18 | import android.annotation.CallbackExecutor; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Arthur Ishiguro | f2b6f01 | 2017-11-28 15:21:38 -0800 | [diff] [blame] | 21 | import android.annotation.SystemApi; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 22 | import android.os.Handler; |
Arthur Ishiguro | e35a6de | 2017-12-20 15:30:51 -0800 | [diff] [blame] | 23 | import android.os.HandlerExecutor; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 24 | |
| 25 | import java.lang.annotation.Retention; |
| 26 | import java.lang.annotation.RetentionPolicy; |
Daulet Zhanguzin | fcc8d86 | 2019-12-18 14:42:04 +0000 | [diff] [blame] | 27 | import java.util.Objects; |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 28 | import java.util.concurrent.CountDownLatch; |
Arthur Ishiguro | e35a6de | 2017-12-20 15:30:51 -0800 | [diff] [blame] | 29 | import java.util.concurrent.Executor; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 30 | import java.util.concurrent.TimeUnit; |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 31 | import java.util.concurrent.TimeoutException; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * A class describing a request sent to the Context Hub Service. |
| 35 | * |
| 36 | * This object is generated as a result of an asynchronous request sent to the Context Hub |
| 37 | * through the ContextHubManager APIs. The caller can either retrieve the result |
| 38 | * synchronously through a blocking call ({@link #waitForResponse(long, TimeUnit)}) or |
Arthur Ishiguro | ddb737f | 2017-12-21 10:32:01 -0800 | [diff] [blame] | 39 | * asynchronously through a user-defined listener |
Arthur Ishiguro | b3dce5b | 2018-01-02 09:51:01 -0800 | [diff] [blame] | 40 | * ({@link #setOnCompleteListener(OnCompleteListener, Executor)} )}). |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 41 | * |
| 42 | * @param <T> the type of the contents in the transaction response |
| 43 | * |
| 44 | * @hide |
| 45 | */ |
Arthur Ishiguro | f2b6f01 | 2017-11-28 15:21:38 -0800 | [diff] [blame] | 46 | @SystemApi |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 47 | public class ContextHubTransaction<T> { |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 48 | private static final String TAG = "ContextHubTransaction"; |
| 49 | |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Constants describing the type of a transaction through the Context Hub Service. |
Arthur Ishiguro | f2b6f01 | 2017-11-28 15:21:38 -0800 | [diff] [blame] | 52 | * {@hide} |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 53 | */ |
| 54 | @Retention(RetentionPolicy.SOURCE) |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 55 | @IntDef(prefix = { "TYPE_" }, value = { |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 56 | TYPE_LOAD_NANOAPP, |
| 57 | TYPE_UNLOAD_NANOAPP, |
| 58 | TYPE_ENABLE_NANOAPP, |
| 59 | TYPE_DISABLE_NANOAPP, |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 60 | TYPE_QUERY_NANOAPPS |
| 61 | }) |
| 62 | public @interface Type { } |
| 63 | |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 64 | public static final int TYPE_LOAD_NANOAPP = 0; |
| 65 | public static final int TYPE_UNLOAD_NANOAPP = 1; |
| 66 | public static final int TYPE_ENABLE_NANOAPP = 2; |
| 67 | public static final int TYPE_DISABLE_NANOAPP = 3; |
| 68 | public static final int TYPE_QUERY_NANOAPPS = 4; |
| 69 | |
| 70 | /** |
| 71 | * Constants describing the result of a transaction or request through the Context Hub Service. |
Arthur Ishiguro | f2b6f01 | 2017-11-28 15:21:38 -0800 | [diff] [blame] | 72 | * {@hide} |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 73 | */ |
| 74 | @Retention(RetentionPolicy.SOURCE) |
Arthur Ishiguro | 6100aa7 | 2017-12-20 09:35:00 -0800 | [diff] [blame] | 75 | @IntDef(prefix = { "RESULT_" }, value = { |
| 76 | RESULT_SUCCESS, |
| 77 | RESULT_FAILED_UNKNOWN, |
| 78 | RESULT_FAILED_BAD_PARAMS, |
| 79 | RESULT_FAILED_UNINITIALIZED, |
Arthur Ishiguro | f3691da | 2018-01-02 09:52:21 -0800 | [diff] [blame] | 80 | RESULT_FAILED_BUSY, |
Arthur Ishiguro | 6100aa7 | 2017-12-20 09:35:00 -0800 | [diff] [blame] | 81 | RESULT_FAILED_AT_HUB, |
| 82 | RESULT_FAILED_TIMEOUT, |
| 83 | RESULT_FAILED_SERVICE_INTERNAL_FAILURE, |
| 84 | RESULT_FAILED_HAL_UNAVAILABLE |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 85 | }) |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 86 | public @interface Result {} |
Arthur Ishiguro | 6100aa7 | 2017-12-20 09:35:00 -0800 | [diff] [blame] | 87 | public static final int RESULT_SUCCESS = 0; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 88 | /** |
| 89 | * Generic failure mode. |
| 90 | */ |
Arthur Ishiguro | 6100aa7 | 2017-12-20 09:35:00 -0800 | [diff] [blame] | 91 | public static final int RESULT_FAILED_UNKNOWN = 1; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 92 | /** |
| 93 | * Failure mode when the request parameters were not valid. |
| 94 | */ |
Arthur Ishiguro | 6100aa7 | 2017-12-20 09:35:00 -0800 | [diff] [blame] | 95 | public static final int RESULT_FAILED_BAD_PARAMS = 2; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 96 | /** |
| 97 | * Failure mode when the Context Hub is not initialized. |
| 98 | */ |
Arthur Ishiguro | 6100aa7 | 2017-12-20 09:35:00 -0800 | [diff] [blame] | 99 | public static final int RESULT_FAILED_UNINITIALIZED = 3; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 100 | /** |
| 101 | * Failure mode when there are too many transactions pending. |
| 102 | */ |
Arthur Ishiguro | f3691da | 2018-01-02 09:52:21 -0800 | [diff] [blame] | 103 | public static final int RESULT_FAILED_BUSY = 4; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 104 | /** |
| 105 | * Failure mode when the request went through, but failed asynchronously at the hub. |
| 106 | */ |
Arthur Ishiguro | 6100aa7 | 2017-12-20 09:35:00 -0800 | [diff] [blame] | 107 | public static final int RESULT_FAILED_AT_HUB = 5; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 108 | /** |
| 109 | * Failure mode when the transaction has timed out. |
| 110 | */ |
Arthur Ishiguro | 6100aa7 | 2017-12-20 09:35:00 -0800 | [diff] [blame] | 111 | public static final int RESULT_FAILED_TIMEOUT = 6; |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 112 | /** |
| 113 | * Failure mode when the transaction has failed internally at the service. |
| 114 | */ |
Arthur Ishiguro | 6100aa7 | 2017-12-20 09:35:00 -0800 | [diff] [blame] | 115 | public static final int RESULT_FAILED_SERVICE_INTERNAL_FAILURE = 7; |
Arthur Ishiguro | e1ade43 | 2017-11-27 10:45:33 -0800 | [diff] [blame] | 116 | /** |
| 117 | * Failure mode when the Context Hub HAL was not available. |
| 118 | */ |
Arthur Ishiguro | 6100aa7 | 2017-12-20 09:35:00 -0800 | [diff] [blame] | 119 | public static final int RESULT_FAILED_HAL_UNAVAILABLE = 8; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 120 | |
| 121 | /** |
| 122 | * A class describing the response for a ContextHubTransaction. |
| 123 | * |
| 124 | * @param <R> the type of the contents in the response |
| 125 | */ |
| 126 | public static class Response<R> { |
| 127 | /* |
| 128 | * The result of the transaction. |
| 129 | */ |
| 130 | @ContextHubTransaction.Result |
| 131 | private int mResult; |
| 132 | |
| 133 | /* |
| 134 | * The contents of the response from the Context Hub. |
| 135 | */ |
| 136 | private R mContents; |
| 137 | |
| 138 | Response(@ContextHubTransaction.Result int result, R contents) { |
| 139 | mResult = result; |
| 140 | mContents = contents; |
| 141 | } |
| 142 | |
| 143 | @ContextHubTransaction.Result |
| 144 | public int getResult() { |
| 145 | return mResult; |
| 146 | } |
| 147 | |
| 148 | public R getContents() { |
| 149 | return mContents; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
Arthur Ishiguro | ddb737f | 2017-12-21 10:32:01 -0800 | [diff] [blame] | 154 | * An interface describing the listener for a transaction completion. |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 155 | * |
Arthur Ishiguro | ddb737f | 2017-12-21 10:32:01 -0800 | [diff] [blame] | 156 | * @param <L> the type of the contents in the transaction response |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 157 | */ |
| 158 | @FunctionalInterface |
Arthur Ishiguro | b3dce5b | 2018-01-02 09:51:01 -0800 | [diff] [blame] | 159 | public interface OnCompleteListener<L> { |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 160 | /** |
Arthur Ishiguro | ddb737f | 2017-12-21 10:32:01 -0800 | [diff] [blame] | 161 | * The listener function to invoke when the transaction completes. |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 162 | * |
| 163 | * @param transaction the transaction that this callback was attached to. |
| 164 | * @param response the response of the transaction. |
| 165 | */ |
| 166 | void onComplete( |
Arthur Ishiguro | ddb737f | 2017-12-21 10:32:01 -0800 | [diff] [blame] | 167 | ContextHubTransaction<L> transaction, ContextHubTransaction.Response<L> response); |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /* |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 171 | * The type of the transaction. |
| 172 | */ |
| 173 | @Type |
| 174 | private int mTransactionType; |
| 175 | |
| 176 | /* |
| 177 | * The response of the transaction. |
| 178 | */ |
| 179 | private ContextHubTransaction.Response<T> mResponse; |
| 180 | |
| 181 | /* |
Arthur Ishiguro | e35a6de | 2017-12-20 15:30:51 -0800 | [diff] [blame] | 182 | * The executor to invoke the onComplete async callback. |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 183 | */ |
Arthur Ishiguro | e35a6de | 2017-12-20 15:30:51 -0800 | [diff] [blame] | 184 | private Executor mExecutor = null; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 185 | |
| 186 | /* |
Arthur Ishiguro | ddb737f | 2017-12-21 10:32:01 -0800 | [diff] [blame] | 187 | * The listener to be invoked when the transaction completes. |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 188 | */ |
Arthur Ishiguro | b3dce5b | 2018-01-02 09:51:01 -0800 | [diff] [blame] | 189 | private ContextHubTransaction.OnCompleteListener<T> mListener = null; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 190 | |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 191 | /* |
| 192 | * Synchronization latch used to block on response. |
| 193 | */ |
| 194 | private final CountDownLatch mDoneSignal = new CountDownLatch(1); |
| 195 | |
| 196 | /* |
| 197 | * true if the response has been set throught setResponse, false otherwise. |
| 198 | */ |
| 199 | private boolean mIsResponseSet = false; |
| 200 | |
| 201 | ContextHubTransaction(@Type int type) { |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 202 | mTransactionType = type; |
| 203 | } |
| 204 | |
| 205 | /** |
Arthur Ishiguro | 4493e14 | 2017-11-27 16:26:34 -0800 | [diff] [blame] | 206 | * Converts a transaction type to a human-readable string |
| 207 | * |
| 208 | * @param type the type of a transaction |
| 209 | * @param upperCase {@code true} if upper case the first letter, {@code false} otherwise |
| 210 | * @return a string describing the transaction |
| 211 | */ |
| 212 | public static String typeToString(@Type int type, boolean upperCase) { |
| 213 | switch (type) { |
| 214 | case ContextHubTransaction.TYPE_LOAD_NANOAPP: |
| 215 | return upperCase ? "Load" : "load"; |
| 216 | case ContextHubTransaction.TYPE_UNLOAD_NANOAPP: |
| 217 | return upperCase ? "Unload" : "unload"; |
| 218 | case ContextHubTransaction.TYPE_ENABLE_NANOAPP: |
| 219 | return upperCase ? "Enable" : "enable"; |
| 220 | case ContextHubTransaction.TYPE_DISABLE_NANOAPP: |
| 221 | return upperCase ? "Disable" : "disable"; |
| 222 | case ContextHubTransaction.TYPE_QUERY_NANOAPPS: |
| 223 | return upperCase ? "Query" : "query"; |
| 224 | default: |
| 225 | return upperCase ? "Unknown" : "unknown"; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 230 | * @return the type of the transaction |
| 231 | */ |
| 232 | @Type |
| 233 | public int getType() { |
| 234 | return mTransactionType; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Waits to receive the asynchronous transaction result. |
| 239 | * |
| 240 | * This function blocks until the Context Hub Service has received a response |
| 241 | * for the transaction represented by this object by the Context Hub, or a |
| 242 | * specified timeout period has elapsed. |
| 243 | * |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 244 | * If the specified timeout has passed, a TimeoutException will be thrown and the caller may |
| 245 | * retry the invocation of this method at a later time. |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 246 | * |
| 247 | * @param timeout the timeout duration |
| 248 | * @param unit the unit of the timeout |
| 249 | * |
| 250 | * @return the transaction response |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 251 | * |
| 252 | * @throws InterruptedException if the current thread is interrupted while waiting for response |
| 253 | * @throws TimeoutException if the timeout period has passed |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 254 | */ |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 255 | public ContextHubTransaction.Response<T> waitForResponse( |
| 256 | long timeout, TimeUnit unit) throws InterruptedException, TimeoutException { |
| 257 | boolean success = mDoneSignal.await(timeout, unit); |
| 258 | |
| 259 | if (!success) { |
| 260 | throw new TimeoutException("Timed out while waiting for transaction"); |
| 261 | } |
| 262 | |
| 263 | return mResponse; |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /** |
Arthur Ishiguro | ddb737f | 2017-12-21 10:32:01 -0800 | [diff] [blame] | 267 | * Sets the listener to be invoked invoked when the transaction completes. |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 268 | * |
| 269 | * This function provides an asynchronous approach to retrieve the result of the |
| 270 | * transaction. When the transaction response has been provided by the Context Hub, |
Arthur Ishiguro | ddb737f | 2017-12-21 10:32:01 -0800 | [diff] [blame] | 271 | * the given listener will be invoked. |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 272 | * |
Arthur Ishiguro | ddb737f | 2017-12-21 10:32:01 -0800 | [diff] [blame] | 273 | * If the transaction has already completed at the time of invocation, the listener |
| 274 | * will be immediately invoked. If the transaction has been invalidated, |
| 275 | * the listener will never be invoked. |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 276 | * |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 277 | * A transaction can be invalidated if the process owning the transaction is no longer active |
| 278 | * and the reference to this object is lost. |
| 279 | * |
Arthur Ishiguro | b3dce5b | 2018-01-02 09:51:01 -0800 | [diff] [blame] | 280 | * This method or {@link #setOnCompleteListener(ContextHubTransaction.OnCompleteListener)} can |
| 281 | * only be invoked once, or an IllegalStateException will be thrown. |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 282 | * |
Arthur Ishiguro | ddb737f | 2017-12-21 10:32:01 -0800 | [diff] [blame] | 283 | * @param listener the listener to be invoked upon completion |
Arthur Ishiguro | e35a6de | 2017-12-20 15:30:51 -0800 | [diff] [blame] | 284 | * @param executor the executor to invoke the callback |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 285 | * |
| 286 | * @throws IllegalStateException if this method is called multiple times |
| 287 | * @throws NullPointerException if the callback or handler is null |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 288 | */ |
Arthur Ishiguro | ddb737f | 2017-12-21 10:32:01 -0800 | [diff] [blame] | 289 | public void setOnCompleteListener( |
Arthur Ishiguro | b3dce5b | 2018-01-02 09:51:01 -0800 | [diff] [blame] | 290 | @NonNull ContextHubTransaction.OnCompleteListener<T> listener, |
Arthur Ishiguro | e35a6de | 2017-12-20 15:30:51 -0800 | [diff] [blame] | 291 | @NonNull @CallbackExecutor Executor executor) { |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 292 | synchronized (this) { |
Daulet Zhanguzin | fcc8d86 | 2019-12-18 14:42:04 +0000 | [diff] [blame] | 293 | Objects.requireNonNull(listener, "OnCompleteListener cannot be null"); |
| 294 | Objects.requireNonNull(executor, "Executor cannot be null"); |
Arthur Ishiguro | ddb737f | 2017-12-21 10:32:01 -0800 | [diff] [blame] | 295 | if (mListener != null) { |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 296 | throw new IllegalStateException( |
Arthur Ishiguro | ddb737f | 2017-12-21 10:32:01 -0800 | [diff] [blame] | 297 | "Cannot set ContextHubTransaction listener multiple times"); |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Arthur Ishiguro | ddb737f | 2017-12-21 10:32:01 -0800 | [diff] [blame] | 300 | mListener = listener; |
Arthur Ishiguro | e35a6de | 2017-12-20 15:30:51 -0800 | [diff] [blame] | 301 | mExecutor = executor; |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 302 | |
| 303 | if (mDoneSignal.getCount() == 0) { |
Arthur Ishiguro | ddb737f | 2017-12-21 10:32:01 -0800 | [diff] [blame] | 304 | mExecutor.execute(() -> mListener.onComplete(this, mResponse)); |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 305 | } |
| 306 | } |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 309 | /** |
Arthur Ishiguro | ddb737f | 2017-12-21 10:32:01 -0800 | [diff] [blame] | 310 | * Sets the listener to be invoked invoked when the transaction completes. |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 311 | * |
Arthur Ishiguro | b3dce5b | 2018-01-02 09:51:01 -0800 | [diff] [blame] | 312 | * Equivalent to {@link #setOnCompleteListener(ContextHubTransaction.OnCompleteListener, |
| 313 | * Executor)} with the executor using the main thread's Looper. |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 314 | * |
Arthur Ishiguro | b3dce5b | 2018-01-02 09:51:01 -0800 | [diff] [blame] | 315 | * This method or {@link #setOnCompleteListener(ContextHubTransaction.OnCompleteListener, |
| 316 | * Executor)} can only be invoked once, or an IllegalStateException will be thrown. |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 317 | * |
Arthur Ishiguro | ddb737f | 2017-12-21 10:32:01 -0800 | [diff] [blame] | 318 | * @param listener the listener to be invoked upon completion |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 319 | * |
| 320 | * @throws IllegalStateException if this method is called multiple times |
| 321 | * @throws NullPointerException if the callback is null |
| 322 | */ |
Arthur Ishiguro | b3dce5b | 2018-01-02 09:51:01 -0800 | [diff] [blame] | 323 | public void setOnCompleteListener( |
| 324 | @NonNull ContextHubTransaction.OnCompleteListener<T> listener) { |
Arthur Ishiguro | ddb737f | 2017-12-21 10:32:01 -0800 | [diff] [blame] | 325 | setOnCompleteListener(listener, new HandlerExecutor(Handler.getMain())); |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Sets the response of the transaction. |
| 330 | * |
| 331 | * This method should only be invoked by ContextHubManager as a result of a callback from |
| 332 | * the Context Hub Service indicating the response from a transaction. This method should not be |
| 333 | * invoked more than once. |
| 334 | * |
| 335 | * @param response the response to set |
| 336 | * |
| 337 | * @throws IllegalStateException if this method is invoked multiple times |
| 338 | * @throws NullPointerException if the response is null |
| 339 | */ |
Arthur Ishiguro | e35a6de | 2017-12-20 15:30:51 -0800 | [diff] [blame] | 340 | /* package */ void setResponse(ContextHubTransaction.Response<T> response) { |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 341 | synchronized (this) { |
Daulet Zhanguzin | fcc8d86 | 2019-12-18 14:42:04 +0000 | [diff] [blame] | 342 | Objects.requireNonNull(response, "Response cannot be null"); |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 343 | if (mIsResponseSet) { |
| 344 | throw new IllegalStateException( |
| 345 | "Cannot set response of ContextHubTransaction multiple times"); |
| 346 | } |
| 347 | |
| 348 | mResponse = response; |
| 349 | mIsResponseSet = true; |
| 350 | |
| 351 | mDoneSignal.countDown(); |
Arthur Ishiguro | ddb737f | 2017-12-21 10:32:01 -0800 | [diff] [blame] | 352 | if (mListener != null) { |
| 353 | mExecutor.execute(() -> mListener.onComplete(this, mResponse)); |
Arthur Ishiguro | 6c37fd0 | 2017-10-26 15:36:22 -0700 | [diff] [blame] | 354 | } |
| 355 | } |
Arthur Ishiguro | b9ae7bd | 2017-10-09 12:47:52 -0700 | [diff] [blame] | 356 | } |
| 357 | } |