Skip to content

Commit 37bd9f4

Browse files
committed
Some cleanup and better error handling
1 parent abeab3a commit 37bd9f4

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ interface OnActivityDestroyedListener {
8383
void onDestroyed(Activity activity);
8484
}
8585

86+
/**
87+
* Get a {@link Task} that will succeed with a result of the app's foregrounded {@link Activity},
88+
* when one is available.
89+
*
90+
* <p>The returned task will never fail. It will instead remain pending indefinitely until some
91+
* activity comes to the foreground.
92+
*/
8693
Task<Activity> getForegroundActivity() {
8794
synchronized (lock) {
8895
if (currentActivity != null) {

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package com.google.firebase.appdistribution;
1616

1717
import static com.google.firebase.appdistribution.FirebaseAppDistributionException.Status.AUTHENTICATION_CANCELED;
18-
import static com.google.firebase.appdistribution.FirebaseAppDistributionException.Status.AUTHENTICATION_FAILURE;
1918
import static com.google.firebase.appdistribution.TaskUtils.combineWithResultOf;
2019
import static com.google.firebase.appdistribution.TaskUtils.safeSetTaskException;
2120
import static com.google.firebase.appdistribution.TaskUtils.safeSetTaskResult;
@@ -29,11 +28,13 @@
2928
import androidx.annotation.NonNull;
3029
import androidx.annotation.VisibleForTesting;
3130
import androidx.browser.customtabs.CustomTabsIntent;
31+
import com.google.android.gms.tasks.OnFailureListener;
3232
import com.google.android.gms.tasks.Task;
3333
import com.google.android.gms.tasks.TaskCompletionSource;
3434
import com.google.android.gms.tasks.Tasks;
3535
import com.google.firebase.FirebaseApp;
3636
import com.google.firebase.appdistribution.Constants.ErrorMessages;
37+
import com.google.firebase.appdistribution.FirebaseAppDistributionException.Status;
3738
import com.google.firebase.appdistribution.internal.InstallActivity;
3839
import com.google.firebase.appdistribution.internal.LogWrapper;
3940
import com.google.firebase.appdistribution.internal.SignInResultActivity;
@@ -120,7 +121,6 @@ void onActivityStarted(Activity activity) {
120121

121122
@NonNull
122123
public Task<Void> signInTester() {
123-
124124
if (signInStorage.getSignInStatus()) {
125125
LogWrapper.getInstance().v(TAG + "Tester is already signed in.");
126126
return Tasks.forResult(null);
@@ -140,6 +140,7 @@ public Task<Void> signInTester() {
140140
firebaseInstallationsApiProvider
141141
.get()
142142
.getId()
143+
.addOnFailureListener(handleTaskFailure(ErrorMessages.AUTHENTICATION_ERROR, Status.AUTHENTICATION_FAILURE))
143144
.onSuccessTask(combineWithResultOf(lifecycleNotifier.getForegroundActivity()))
144145
.addOnSuccessListener(
145146
fidAndActivity -> {
@@ -148,18 +149,22 @@ public Task<Void> signInTester() {
148149
hasBeenSentToBrowserForCurrentTask = true;
149150
}
150151
})
151-
.addOnFailureListener(
152-
e -> {
153-
LogWrapper.getInstance().e(TAG + "Fid retrieval failed.", e);
154-
setSignInTaskCompletionError(
155-
new FirebaseAppDistributionException(
156-
ErrorMessages.AUTHENTICATION_ERROR, AUTHENTICATION_FAILURE, e));
157-
});
152+
// No failures expected here, since getForegroundActivity() will wait indefinitely for a
153+
// foreground activity, but catch any unexpected failures to be safe.
154+
.addOnFailureListener(handleTaskFailure(ErrorMessages.UNKNOWN_ERROR, Status.UNKNOWN));
158155

159156
return signInTaskCompletionSource.getTask();
160157
}
161158
}
162159

160+
private OnFailureListener handleTaskFailure(String message, Status status) {
161+
return e -> {
162+
LogWrapper.getInstance().e(TAG + message, e);
163+
setSignInTaskCompletionError(
164+
new FirebaseAppDistributionException(message, status, e));
165+
};
166+
}
167+
163168
private boolean awaitingResultFromBrowser() {
164169
synchronized (signInTaskLock) {
165170
return signInTaskCompletionSource != null
@@ -168,15 +173,15 @@ private boolean awaitingResultFromBrowser() {
168173
}
169174
}
170175

171-
private void setSuccessfulSignInResult() {
176+
private void setSignInTaskCompletionError(FirebaseAppDistributionException e) {
172177
synchronized (signInTaskLock) {
173-
safeSetTaskResult(signInTaskCompletionSource, null);
178+
safeSetTaskException(signInTaskCompletionSource, e);
174179
}
175180
}
176181

177-
private void setSignInTaskCompletionError(FirebaseAppDistributionException e) {
182+
private void setSuccessfulSignInResult() {
178183
synchronized (signInTaskLock) {
179-
safeSetTaskException(signInTaskCompletionSource, e);
184+
safeSetTaskResult(signInTaskCompletionSource, null);
180185
}
181186
}
182187

0 commit comments

Comments
 (0)