Skip to content

Commit c14fd98

Browse files
committed
Don't kick off second task until first is finished
1 parent 53c9110 commit c14fd98

File tree

3 files changed

+37
-21
lines changed

3 files changed

+37
-21
lines changed

firebase-appdistribution/src/main/java/com/google/firebase/appdistribution/FirebaseAppDistributionException.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import androidx.annotation.NonNull;
1818
import androidx.annotation.Nullable;
1919
import com.google.firebase.FirebaseException;
20+
import com.google.firebase.appdistribution.Constants.ErrorMessages;
2021

2122
/** Possible exceptions thrown in FirebaseAppDistribution */
2223
public class FirebaseAppDistributionException extends FirebaseException {
@@ -98,4 +99,13 @@ public AppDistributionRelease getRelease() {
9899
public Status getErrorCode() {
99100
return status;
100101
}
102+
103+
static FirebaseAppDistributionException wrap(Throwable t) {
104+
// We never want to wrap a FirebaseAppDistributionException
105+
if (t instanceof FirebaseAppDistributionException) {
106+
return (FirebaseAppDistributionException) t;
107+
}
108+
return new FirebaseAppDistributionException(
109+
String.format("%s: %s", ErrorMessages.UNKNOWN_ERROR, t.getMessage()), Status.UNKNOWN, t);
110+
}
101111
}

firebase-appdistribution/src/main/java/com/google/firebase/appdistribution/TaskUtils.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,26 @@
1919
import com.google.android.gms.tasks.TaskCompletionSource;
2020
import com.google.android.gms.tasks.Tasks;
2121
import com.google.auto.value.AutoValue;
22-
import com.google.firebase.appdistribution.Constants.ErrorMessages;
2322
import com.google.firebase.appdistribution.FirebaseAppDistributionException.Status;
2423
import com.google.firebase.appdistribution.internal.LogWrapper;
2524
import java.util.concurrent.Executor;
2625

2726
class TaskUtils {
2827
private static final String TAG = "TaskUtils:";
2928

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+
*/
3033
interface Operation<TResult> {
3134
TResult run() throws FirebaseAppDistributionException;
3235
}
3336

37+
/** A functional interface to wrap a function that produces a {@link Task}. */
38+
interface TaskSource<TResult> {
39+
Task<TResult> get();
40+
}
41+
3442
/**
3543
* Runs a long running operation inside a {@link Task}, wrapping any errors in {@link
3644
* FirebaseAppDistributionException}.
@@ -53,10 +61,8 @@ static <TResult> Task<TResult> runAsyncInTask(Executor executor, Operation<TResu
5361
() -> {
5462
try {
5563
taskCompletionSource.setResult(operation.run());
56-
} catch (FirebaseAppDistributionException e) {
57-
taskCompletionSource.setException(e);
5864
} catch (Throwable t) {
59-
taskCompletionSource.setException(wrapException(t));
65+
taskCompletionSource.setException(FirebaseAppDistributionException.wrap(t));
6066
}
6167
});
6268
return taskCompletionSource.getTask();
@@ -81,7 +87,7 @@ static <TResult> Task<TResult> handleTaskFailure(Task<TResult> task) {
8187
LogWrapper.getInstance().e(TAG + "Task failed to complete due to " + e.getMessage(), e);
8288
return e instanceof FirebaseAppDistributionException
8389
? task
84-
: Tasks.forException(wrapException(e));
90+
: Tasks.forException(FirebaseAppDistributionException.wrap(e));
8591
}
8692
return task;
8793
}
@@ -116,28 +122,26 @@ static <T1, T2> CombinedTaskResults<T1, T2> create(T1 first, T2 second) {
116122
*
117123
* <pre>{@code
118124
* runFirstAsyncTask()
119-
* .onSuccessTask(combineWithResultOf(runSecondAsyncTask())
125+
* .onSuccessTask(combineWithResultOf(() -> startSecondAsyncTask())
120126
* .addOnSuccessListener(
121127
* results ->
122128
* doSomethingWithBothResults(results.result1(), results.result2()));
123129
* }</pre>
124130
*
125-
* @param secondTask The next task to run
131+
* @param secondTaskSource A {@link TaskSource} providing the next task to run
126132
* @param <T1> The result type of the first task
127133
* @param <T2> The result type of the second task
128134
* @return A {@link SuccessContinuation} that will return a new task with result type {@link
129135
* CombinedTaskResults}, combining the results of both tasks
130136
*/
131137
static <T1, T2> SuccessContinuation<T1, CombinedTaskResults<T1, T2>> combineWithResultOf(
132-
Task<T2> secondTask) {
138+
TaskSource<T2> secondTaskSource) {
133139
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)));
141145
}
142146

143147
static void safeSetTaskException(TaskCompletionSource taskCompletionSource, Exception e) {

firebase-appdistribution/src/main/java/com/google/firebase/appdistribution/TesterSignInManager.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,13 @@ void onActivityStarted(Activity activity) {
107107
return;
108108
} else {
109109
// Throw error if app reentered during sign in
110-
if (awaitingResultFromBrowser()) {
111-
LogWrapper.getInstance().e("App Resumed without sign in flow completing.");
112-
setSignInTaskCompletionError(
113-
new FirebaseAppDistributionException(
114-
Constants.ErrorMessages.AUTHENTICATION_CANCELED, AUTHENTICATION_CANCELED));
110+
synchronized (signInTaskLock) {
111+
if (awaitingResultFromBrowser()) {
112+
LogWrapper.getInstance().e("App Resumed without sign in flow completing.");
113+
setSignInTaskCompletionError(
114+
new FirebaseAppDistributionException(
115+
Constants.ErrorMessages.AUTHENTICATION_CANCELED, AUTHENTICATION_CANCELED));
116+
}
115117
}
116118
}
117119
}
@@ -139,7 +141,7 @@ public Task<Void> signInTester() {
139141
.getId()
140142
.addOnFailureListener(
141143
handleTaskFailure(ErrorMessages.AUTHENTICATION_ERROR, Status.AUTHENTICATION_FAILURE))
142-
.onSuccessTask(combineWithResultOf(lifecycleNotifier.getForegroundActivity()))
144+
.onSuccessTask(combineWithResultOf(() -> lifecycleNotifier.getForegroundActivity()))
143145
.addOnSuccessListener(
144146
fidAndActivity -> {
145147
synchronized (signInTaskLock) {

0 commit comments

Comments
 (0)