19
19
import com .google .android .gms .tasks .TaskCompletionSource ;
20
20
import com .google .android .gms .tasks .Tasks ;
21
21
import com .google .auto .value .AutoValue ;
22
- import com .google .firebase .appdistribution .Constants .ErrorMessages ;
23
22
import com .google .firebase .appdistribution .FirebaseAppDistributionException .Status ;
24
23
import com .google .firebase .appdistribution .internal .LogWrapper ;
25
24
import java .util .concurrent .Executor ;
26
25
27
26
class TaskUtils {
28
27
private static final String TAG = "TaskUtils:" ;
29
28
29
+ /**
30
+ * A functional interface to wrap a function that returns some result of a possibly long-running
31
+ * operation, and could potentially throw a {@link FirebaseAppDistributionException}.
32
+ */
30
33
interface Operation <TResult > {
31
34
TResult run () throws FirebaseAppDistributionException ;
32
35
}
33
36
37
+ /** A functional interface to wrap a function that produces a {@link Task}. */
38
+ interface TaskSource <TResult > {
39
+ Task <TResult > get ();
40
+ }
41
+
34
42
/**
35
43
* Runs a long running operation inside a {@link Task}, wrapping any errors in {@link
36
44
* FirebaseAppDistributionException}.
@@ -53,10 +61,8 @@ static <TResult> Task<TResult> runAsyncInTask(Executor executor, Operation<TResu
53
61
() -> {
54
62
try {
55
63
taskCompletionSource .setResult (operation .run ());
56
- } catch (FirebaseAppDistributionException e ) {
57
- taskCompletionSource .setException (e );
58
64
} catch (Throwable t ) {
59
- taskCompletionSource .setException (wrapException (t ));
65
+ taskCompletionSource .setException (FirebaseAppDistributionException . wrap (t ));
60
66
}
61
67
});
62
68
return taskCompletionSource .getTask ();
@@ -81,7 +87,7 @@ static <TResult> Task<TResult> handleTaskFailure(Task<TResult> task) {
81
87
LogWrapper .getInstance ().e (TAG + "Task failed to complete due to " + e .getMessage (), e );
82
88
return e instanceof FirebaseAppDistributionException
83
89
? task
84
- : Tasks .forException (wrapException (e ));
90
+ : Tasks .forException (FirebaseAppDistributionException . wrap (e ));
85
91
}
86
92
return task ;
87
93
}
@@ -116,28 +122,26 @@ static <T1, T2> CombinedTaskResults<T1, T2> create(T1 first, T2 second) {
116
122
*
117
123
* <pre>{@code
118
124
* runFirstAsyncTask()
119
- * .onSuccessTask(combineWithResultOf(runSecondAsyncTask ())
125
+ * .onSuccessTask(combineWithResultOf(() -> startSecondAsyncTask ())
120
126
* .addOnSuccessListener(
121
127
* results ->
122
128
* doSomethingWithBothResults(results.result1(), results.result2()));
123
129
* }</pre>
124
130
*
125
- * @param secondTask The next task to run
131
+ * @param secondTaskSource A {@link TaskSource} providing the next task to run
126
132
* @param <T1> The result type of the first task
127
133
* @param <T2> The result type of the second task
128
134
* @return A {@link SuccessContinuation} that will return a new task with result type {@link
129
135
* CombinedTaskResults}, combining the results of both tasks
130
136
*/
131
137
static <T1 , T2 > SuccessContinuation <T1 , CombinedTaskResults <T1 , T2 >> combineWithResultOf (
132
- Task <T2 > secondTask ) {
138
+ TaskSource <T2 > secondTaskSource ) {
133
139
return firstResult ->
134
- secondTask .onSuccessTask (
135
- secondResult -> Tasks .forResult (CombinedTaskResults .create (firstResult , secondResult )));
136
- }
137
-
138
- private static FirebaseAppDistributionException wrapException (Throwable t ) {
139
- return new FirebaseAppDistributionException (
140
- String .format ("%s: %s" , ErrorMessages .UNKNOWN_ERROR , t .getMessage ()), Status .UNKNOWN , t );
140
+ secondTaskSource
141
+ .get ()
142
+ .onSuccessTask (
143
+ secondResult ->
144
+ Tasks .forResult (CombinedTaskResults .create (firstResult , secondResult )));
141
145
}
142
146
143
147
static void safeSetTaskException (TaskCompletionSource taskCompletionSource , Exception e ) {
0 commit comments