| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.inputmethod; |
| 18 |
|
| 19 |
import android.annotation.AnyThread; |
| 20 |
import android.annotation.DurationMillisLong; |
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.util.Log; |
| 24 |
|
| 25 |
import java.util.concurrent.CancellationException; |
| 26 |
import java.util.concurrent.CompletableFuture; |
| 27 |
import java.util.concurrent.CompletionException; |
| 28 |
import java.util.concurrent.ExecutionException; |
| 29 |
import java.util.concurrent.TimeUnit; |
| 30 |
import java.util.concurrent.TimeoutException; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public final class CompletableFutureUtil { |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
private CompletableFutureUtil() { |
| 40 |
} |
| 41 |
|
| 42 |
@AnyThread |
| 43 |
@Nullable |
| 44 |
private static <T> T getValueOrRethrowErrorInternal(@NonNull CompletableFuture<T> future) { |
| 45 |
boolean interrupted = false; |
| 46 |
try { |
| 47 |
while (true) { |
| 48 |
try { |
| 49 |
return future.get(); |
| 50 |
} catch (ExecutionException e) { |
| 51 |
final Throwable cause = e.getCause(); |
| 52 |
throw new RuntimeException(cause.getMessage(), cause.getCause()); |
| 53 |
} catch (InterruptedException e) { |
| 54 |
interrupted = true; |
| 55 |
} |
| 56 |
} |
| 57 |
} finally { |
| 58 |
if (interrupted) { |
| 59 |
Thread.currentThread().interrupt(); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
@AnyThread |
| 65 |
@Nullable |
| 66 |
private static <T> T getValueOrNullInternal(@NonNull CompletableFuture<T> future, |
| 67 |
@Nullable String tag, @Nullable String methodName, |
| 68 |
@DurationMillisLong long timeoutMillis, @Nullable CancellationGroup cancellationGroup) { |
| 69 |
|
| 70 |
|
| 71 |
final boolean needsToUnregister = cancellationGroup != null |
| 72 |
&& cancellationGroup.tryRegisterFutureOrCancelImmediately(future); |
| 73 |
boolean interrupted = false; |
| 74 |
try { |
| 75 |
while (true) { |
| 76 |
try { |
| 77 |
return future.get(timeoutMillis, TimeUnit.MILLISECONDS); |
| 78 |
} catch (CompletionException e) { |
| 79 |
if (e.getCause() instanceof CancellationException) { |
| 80 |
logCancellationInternal(tag, methodName); |
| 81 |
return null; |
| 82 |
} |
| 83 |
logErrorInternal(tag, methodName, e.getMessage()); |
| 84 |
return null; |
| 85 |
} catch (CancellationException e) { |
| 86 |
logCancellationInternal(tag, methodName); |
| 87 |
return null; |
| 88 |
} catch (InterruptedException e) { |
| 89 |
interrupted = true; |
| 90 |
} catch (TimeoutException e) { |
| 91 |
logTimeoutInternal(tag, methodName, timeoutMillis); |
| 92 |
return null; |
| 93 |
} catch (Throwable e) { |
| 94 |
logErrorInternal(tag, methodName, e.getMessage()); |
| 95 |
return null; |
| 96 |
} |
| 97 |
} |
| 98 |
} finally { |
| 99 |
if (needsToUnregister) { |
| 100 |
cancellationGroup.unregisterFuture(future); |
| 101 |
} |
| 102 |
if (interrupted) { |
| 103 |
Thread.currentThread().interrupt(); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
@AnyThread |
| 109 |
private static void logTimeoutInternal(@Nullable String tag, @Nullable String methodName, |
| 110 |
@DurationMillisLong long timeout) { |
| 111 |
if (tag == null || methodName == null) { |
| 112 |
return; |
| 113 |
} |
| 114 |
Log.w(tag, methodName + " didn't respond in " + timeout + " msec."); |
| 115 |
} |
| 116 |
|
| 117 |
@AnyThread |
| 118 |
private static void logErrorInternal(@Nullable String tag, @Nullable String methodName, |
| 119 |
@Nullable String errorString) { |
| 120 |
if (tag == null || methodName == null) { |
| 121 |
return; |
| 122 |
} |
| 123 |
Log.w(tag, methodName + " was failed with an exception=" + errorString); |
| 124 |
} |
| 125 |
|
| 126 |
@AnyThread |
| 127 |
private static void logCancellationInternal(@Nullable String tag, @Nullable String methodName) { |
| 128 |
if (tag == null || methodName == null) { |
| 129 |
return; |
| 130 |
} |
| 131 |
Log.w(tag, methodName + " was cancelled."); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
@AnyThread |
| 144 |
@Nullable |
| 145 |
public static <T> T getResult(@NonNull CompletableFuture<T> future) { |
| 146 |
return getValueOrRethrowErrorInternal(future); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
@AnyThread |
| 158 |
public static boolean getBooleanResult(@NonNull CompletableFuture<Boolean> future) { |
| 159 |
return getValueOrRethrowErrorInternal(future); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
@AnyThread |
| 171 |
public static int getIntegerResult(@NonNull CompletableFuture<Integer> future) { |
| 172 |
return getValueOrRethrowErrorInternal(future); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
@AnyThread |
| 193 |
public static boolean getResultOrFalse(@NonNull CompletableFuture<Boolean> future, |
| 194 |
@Nullable String tag, @Nullable String methodName, |
| 195 |
@Nullable CancellationGroup cancellationGroup, |
| 196 |
@DurationMillisLong long timeoutMillis) { |
| 197 |
final Boolean obj = getValueOrNullInternal(future, tag, methodName, timeoutMillis, |
| 198 |
cancellationGroup); |
| 199 |
return obj != null ? obj : false; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
@AnyThread |
| 220 |
public static int getResultOrZero(@NonNull CompletableFuture<Integer> future, |
| 221 |
@Nullable String tag, @Nullable String methodName, |
| 222 |
@Nullable CancellationGroup cancellationGroup, @DurationMillisLong long timeoutMillis) { |
| 223 |
final Integer obj = getValueOrNullInternal(future, tag, methodName, timeoutMillis, |
| 224 |
cancellationGroup); |
| 225 |
return obj != null ? obj : 0; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
@AnyThread |
| 247 |
@Nullable |
| 248 |
public static <T> T getResultOrNull(@NonNull CompletableFuture<T> future, @Nullable String tag, |
| 249 |
@Nullable String methodName, @Nullable CancellationGroup cancellationGroup, |
| 250 |
@DurationMillisLong long timeoutMillis) { |
| 251 |
return getValueOrNullInternal(future, tag, methodName, timeoutMillis, cancellationGroup); |
| 252 |
} |
| 253 |
} |