| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.hardware.location; |
| 17 |
|
| 18 |
import android.annotation.CallbackExecutor; |
| 19 |
import android.annotation.IntDef; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.SystemApi; |
| 22 |
import android.os.Handler; |
| 23 |
import android.os.HandlerExecutor; |
| 24 |
|
| 25 |
import java.lang.annotation.Retention; |
| 26 |
import java.lang.annotation.RetentionPolicy; |
| 27 |
import java.util.Objects; |
| 28 |
import java.util.concurrent.CountDownLatch; |
| 29 |
import java.util.concurrent.Executor; |
| 30 |
import java.util.concurrent.TimeUnit; |
| 31 |
import java.util.concurrent.TimeoutException; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
@SystemApi |
| 47 |
public class ContextHubTransaction<T> { |
| 48 |
private static final String TAG = "ContextHubTransaction"; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
@Retention(RetentionPolicy.SOURCE) |
| 55 |
@IntDef(prefix = { "TYPE_" }, value = { |
| 56 |
TYPE_LOAD_NANOAPP, |
| 57 |
TYPE_UNLOAD_NANOAPP, |
| 58 |
TYPE_ENABLE_NANOAPP, |
| 59 |
TYPE_DISABLE_NANOAPP, |
| 60 |
TYPE_QUERY_NANOAPPS |
| 61 |
}) |
| 62 |
public @interface Type { } |
| 63 |
|
| 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 |
|
| 72 |
|
| 73 |
|
| 74 |
@Retention(RetentionPolicy.SOURCE) |
| 75 |
@IntDef(prefix = { "RESULT_" }, value = { |
| 76 |
RESULT_SUCCESS, |
| 77 |
RESULT_FAILED_UNKNOWN, |
| 78 |
RESULT_FAILED_BAD_PARAMS, |
| 79 |
RESULT_FAILED_UNINITIALIZED, |
| 80 |
RESULT_FAILED_BUSY, |
| 81 |
RESULT_FAILED_AT_HUB, |
| 82 |
RESULT_FAILED_TIMEOUT, |
| 83 |
RESULT_FAILED_SERVICE_INTERNAL_FAILURE, |
| 84 |
RESULT_FAILED_HAL_UNAVAILABLE |
| 85 |
}) |
| 86 |
public @interface Result {} |
| 87 |
public static final int RESULT_SUCCESS = 0; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public static final int RESULT_FAILED_UNKNOWN = 1; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public static final int RESULT_FAILED_BAD_PARAMS = 2; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public static final int RESULT_FAILED_UNINITIALIZED = 3; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public static final int RESULT_FAILED_BUSY = 4; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public static final int RESULT_FAILED_AT_HUB = 5; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public static final int RESULT_FAILED_TIMEOUT = 6; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public static final int RESULT_FAILED_SERVICE_INTERNAL_FAILURE = 7; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public static final int RESULT_FAILED_HAL_UNAVAILABLE = 8; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public static class Response<R> { |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
@ContextHubTransaction.Result |
| 131 |
private int mResult; |
| 132 |
|
| 133 |
|
| 134 |
|
| 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 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
@FunctionalInterface |
| 159 |
public interface OnCompleteListener<L> { |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
void onComplete( |
| 167 |
ContextHubTransaction<L> transaction, ContextHubTransaction.Response<L> response); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
@Type |
| 174 |
private int mTransactionType; |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
private ContextHubTransaction.Response<T> mResponse; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
private Executor mExecutor = null; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
private ContextHubTransaction.OnCompleteListener<T> mListener = null; |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
private final CountDownLatch mDoneSignal = new CountDownLatch(1); |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
private boolean mIsResponseSet = false; |
| 200 |
|
| 201 |
ContextHubTransaction(@Type int type) { |
| 202 |
mTransactionType = type; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 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 |
|
| 230 |
|
| 231 |
|
| 232 |
@Type |
| 233 |
public int getType() { |
| 234 |
return mTransactionType; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 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; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
public void setOnCompleteListener( |
| 290 |
@NonNull ContextHubTransaction.OnCompleteListener<T> listener, |
| 291 |
@NonNull @CallbackExecutor Executor executor) { |
| 292 |
synchronized (this) { |
| 293 |
Objects.requireNonNull(listener, "OnCompleteListener cannot be null"); |
| 294 |
Objects.requireNonNull(executor, "Executor cannot be null"); |
| 295 |
if (mListener != null) { |
| 296 |
throw new IllegalStateException( |
| 297 |
"Cannot set ContextHubTransaction listener multiple times"); |
| 298 |
} |
| 299 |
|
| 300 |
mListener = listener; |
| 301 |
mExecutor = executor; |
| 302 |
|
| 303 |
if (mDoneSignal.getCount() == 0) { |
| 304 |
mExecutor.execute(() -> mListener.onComplete(this, mResponse)); |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
public void setOnCompleteListener( |
| 324 |
@NonNull ContextHubTransaction.OnCompleteListener<T> listener) { |
| 325 |
setOnCompleteListener(listener, new HandlerExecutor(Handler.getMain())); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
void setResponse(ContextHubTransaction.Response<T> response) { |
| 341 |
synchronized (this) { |
| 342 |
Objects.requireNonNull(response, "Response cannot be null"); |
| 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(); |
| 352 |
if (mListener != null) { |
| 353 |
mExecutor.execute(() -> mListener.onComplete(this, mResponse)); |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
} |