1 /* 2 * Copyright (C) 2023 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 android.car; 18 19 import android.annotation.Nullable; 20 import android.annotation.SystemApi; 21 import android.car.annotation.ApiRequirements; 22 23 import java.util.concurrent.CountDownLatch; 24 import java.util.concurrent.TimeUnit; 25 import java.util.concurrent.TimeoutException; 26 import java.util.concurrent.atomic.AtomicReference; 27 28 /** 29 * Synchronous implementation for {@link ResultCallback}. 30 * 31 * <p>Can be used to get the results synchronously where {@link ResultCallback} is required. 32 * {@link #get()} and {@link #get(long, TimeUnit)} methods can be used to get the result. 33 * 34 * @param <V> refer to a Parcelable object. 35 * 36 * @hide 37 */ 38 @SystemApi 39 public final class SyncResultCallback<V> implements ResultCallback<V> { 40 41 private final CountDownLatch mLatch = new CountDownLatch(1); 42 43 private AtomicReference<V> mResult = new AtomicReference<V>(null); 44 45 /** 46 * Waits if necessary for the computation to complete, and then 47 * retrieves its result. 48 * 49 * @return the computed result 50 * @throws InterruptedException if the current thread was interrupted 51 * while waiting 52 */ 53 @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0, 54 minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0) 55 @Nullable get()56 public V get() throws InterruptedException { 57 mLatch.await(); 58 return mResult.get(); 59 } 60 61 /** 62 * Waits if necessary for at most the given time for the computation 63 * to complete, and then retrieves its result, if available. 64 * 65 * @param timeout the maximum time to wait 66 * @param unit the time unit of the timeout argument 67 * @return the computed result 68 * @throws InterruptedException if the current thread was interrupted 69 * while waiting 70 * @throws TimeoutException if the wait timed out 71 */ 72 @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0, 73 minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0) 74 @Nullable get(long timeout, @Nullable TimeUnit unit)75 public V get(long timeout, @Nullable TimeUnit unit) 76 throws InterruptedException, TimeoutException { 77 if (mLatch.await(timeout, unit)) { 78 return mResult.get(); 79 } 80 81 throw new TimeoutException("Failed to receive result after " + timeout + " " + unit); 82 } 83 84 /** 85 * {@inheritDoc} 86 * 87 * <p> 88 * This method should be called internally only. 89 */ 90 @Override 91 @ApiRequirements(minCarVersion = ApiRequirements.CarVersion.UPSIDE_DOWN_CAKE_0, 92 minPlatformVersion = ApiRequirements.PlatformVersion.TIRAMISU_0) onResult(V result)93 public void onResult(V result) { 94 mResult.set(result); 95 mLatch.countDown(); 96 } 97 } 98