14
14
15
15
package com .google .firebase .crashlytics .internal .common ;
16
16
17
- import static java .util .Objects .requireNonNull ;
18
-
19
17
import android .annotation .SuppressLint ;
20
- import androidx . annotation . NonNull ;
18
+ import android . os . Looper ;
21
19
import com .google .android .gms .tasks .Continuation ;
22
20
import com .google .android .gms .tasks .Task ;
23
21
import com .google .android .gms .tasks .TaskCompletionSource ;
30
28
import java .util .concurrent .TimeoutException ;
31
29
32
30
/** Utils */
31
+ @ SuppressWarnings ({"ResultOfMethodCallIgnored" , "UnusedReturnValue" })
33
32
public final class Utils {
34
-
35
- private Utils () {}
33
+ private static final int TIMEOUT_SEC = 4 ;
36
34
37
35
/** @return A tasks that is resolved when either of the given tasks is resolved. */
38
36
// TODO(b/261014167): Use an explicit executor in continuations.
@@ -43,8 +41,8 @@ public static <T> Task<T> race(Task<T> t1, Task<T> t2) {
43
41
task -> {
44
42
if (task .isSuccessful ()) {
45
43
result .trySetResult (task .getResult ());
46
- } else {
47
- result .trySetException (requireNonNull ( task .getException () ));
44
+ } else if ( task . getException () != null ) {
45
+ result .trySetException (task .getException ());
48
46
}
49
47
return null ;
50
48
};
@@ -60,8 +58,8 @@ public static <T> Task<T> race(Executor executor, Task<T> t1, Task<T> t2) {
60
58
task -> {
61
59
if (task .isSuccessful ()) {
62
60
result .trySetResult (task .getResult ());
63
- } else {
64
- result .trySetException (requireNonNull ( task .getException () ));
61
+ } else if ( task . getException () != null ) {
62
+ result .trySetException (task .getException ());
65
63
}
66
64
return null ;
67
65
};
@@ -72,34 +70,27 @@ public static <T> Task<T> race(Executor executor, Task<T> t1, Task<T> t2) {
72
70
73
71
/** Similar to Tasks.call, but takes a Callable that returns a Task. */
74
72
public static <T > Task <T > callTask (Executor executor , Callable <Task <T >> callable ) {
75
- final TaskCompletionSource <T > tcs = new TaskCompletionSource <T >();
73
+ final TaskCompletionSource <T > result = new TaskCompletionSource <>();
76
74
executor .execute (
77
- new Runnable () {
78
- // TODO(b/261014167): Use an explicit executor in continuations.
79
- @ SuppressLint ("TaskMainThread" )
80
- @ Override
81
- public void run () {
82
- try {
83
- callable
84
- .call ()
85
- .continueWith (
86
- new Continuation <T , Void >() {
87
- @ Override
88
- public Void then (@ NonNull Task <T > task ) throws Exception {
89
- if (task .isSuccessful ()) {
90
- tcs .setResult (task .getResult ());
91
- } else {
92
- tcs .setException (task .getException ());
93
- }
94
- return null ;
95
- }
96
- });
97
- } catch (Exception e ) {
98
- tcs .setException (e );
99
- }
75
+ () -> {
76
+ try {
77
+ callable
78
+ .call ()
79
+ .continueWith (
80
+ executor ,
81
+ task -> {
82
+ if (task .isSuccessful ()) {
83
+ result .setResult (task .getResult ());
84
+ } else if (task .getException () != null ) {
85
+ result .setException (task .getException ());
86
+ }
87
+ return null ;
88
+ });
89
+ } catch (Exception e ) {
90
+ result .setException (e );
100
91
}
101
92
});
102
- return tcs .getTask ();
93
+ return result .getTask ();
103
94
}
104
95
105
96
/**
@@ -126,7 +117,11 @@ public static <T> T awaitEvenIfOnMainThread(Task<T> task)
126
117
return null ;
127
118
});
128
119
129
- latch .await (CrashlyticsCore .DEFAULT_MAIN_HANDLER_TIMEOUT_SEC , TimeUnit .SECONDS );
120
+ if (Looper .getMainLooper () == Looper .myLooper ()) {
121
+ latch .await (CrashlyticsCore .DEFAULT_MAIN_HANDLER_TIMEOUT_SEC , TimeUnit .SECONDS );
122
+ } else {
123
+ latch .await (TIMEOUT_SEC , TimeUnit .SECONDS );
124
+ }
130
125
131
126
if (task .isSuccessful ()) {
132
127
return task .getResult ();
@@ -170,4 +165,6 @@ public static boolean awaitUninterruptibly(CountDownLatch latch, long timeout, T
170
165
private static final ExecutorService TASK_CONTINUATION_EXECUTOR_SERVICE =
171
166
ExecutorUtils .buildSingleThreadExecutorService (
172
167
"awaitEvenIfOnMainThread task continuation executor" );
168
+
169
+ private Utils () {}
173
170
}
0 commit comments