15
15
package com .google .firebase .appdistribution ;
16
16
17
17
import static com .google .firebase .appdistribution .FirebaseAppDistributionException .Status .AUTHENTICATION_CANCELED ;
18
- import static com .google .firebase .appdistribution .FirebaseAppDistributionException .Status .AUTHENTICATION_FAILURE ;
19
18
import static com .google .firebase .appdistribution .TaskUtils .combineWithResultOf ;
20
19
import static com .google .firebase .appdistribution .TaskUtils .safeSetTaskException ;
21
20
import static com .google .firebase .appdistribution .TaskUtils .safeSetTaskResult ;
29
28
import androidx .annotation .NonNull ;
30
29
import androidx .annotation .VisibleForTesting ;
31
30
import androidx .browser .customtabs .CustomTabsIntent ;
31
+ import com .google .android .gms .tasks .OnFailureListener ;
32
32
import com .google .android .gms .tasks .Task ;
33
33
import com .google .android .gms .tasks .TaskCompletionSource ;
34
34
import com .google .android .gms .tasks .Tasks ;
35
35
import com .google .firebase .FirebaseApp ;
36
36
import com .google .firebase .appdistribution .Constants .ErrorMessages ;
37
+ import com .google .firebase .appdistribution .FirebaseAppDistributionException .Status ;
37
38
import com .google .firebase .appdistribution .internal .InstallActivity ;
38
39
import com .google .firebase .appdistribution .internal .LogWrapper ;
39
40
import com .google .firebase .appdistribution .internal .SignInResultActivity ;
@@ -107,20 +108,17 @@ void onActivityStarted(Activity activity) {
107
108
return ;
108
109
} else {
109
110
// Throw error if app reentered during sign in
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
- }
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 ));
117
116
}
118
117
}
119
118
}
120
119
121
120
@ NonNull
122
121
public Task <Void > signInTester () {
123
-
124
122
if (signInStorage .getSignInStatus ()) {
125
123
LogWrapper .getInstance ().v (TAG + "Tester is already signed in." );
126
124
return Tasks .forResult (null );
@@ -140,6 +138,7 @@ public Task<Void> signInTester() {
140
138
firebaseInstallationsApiProvider
141
139
.get ()
142
140
.getId ()
141
+ .addOnFailureListener (handleTaskFailure (ErrorMessages .AUTHENTICATION_ERROR , Status .AUTHENTICATION_FAILURE ))
143
142
.onSuccessTask (combineWithResultOf (lifecycleNotifier .getForegroundActivity ()))
144
143
.addOnSuccessListener (
145
144
fidAndActivity -> {
@@ -148,18 +147,22 @@ public Task<Void> signInTester() {
148
147
hasBeenSentToBrowserForCurrentTask = true ;
149
148
}
150
149
})
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
- });
150
+ // No failures expected here, since getForegroundActivity() will wait indefinitely for a
151
+ // foreground activity, but catch any unexpected failures to be safe.
152
+ .addOnFailureListener (handleTaskFailure (ErrorMessages .UNKNOWN_ERROR , Status .UNKNOWN ));
158
153
159
154
return signInTaskCompletionSource .getTask ();
160
155
}
161
156
}
162
157
158
+ private OnFailureListener handleTaskFailure (String message , Status status ) {
159
+ return e -> {
160
+ LogWrapper .getInstance ().e (TAG + message , e );
161
+ setSignInTaskCompletionError (
162
+ new FirebaseAppDistributionException (message , status , e ));
163
+ };
164
+ }
165
+
163
166
private boolean awaitingResultFromBrowser () {
164
167
synchronized (signInTaskLock ) {
165
168
return signInTaskCompletionSource != null
@@ -168,15 +171,15 @@ private boolean awaitingResultFromBrowser() {
168
171
}
169
172
}
170
173
171
- private void setSuccessfulSignInResult ( ) {
174
+ private void setSignInTaskCompletionError ( FirebaseAppDistributionException e ) {
172
175
synchronized (signInTaskLock ) {
173
- safeSetTaskResult (signInTaskCompletionSource , null );
176
+ safeSetTaskException (signInTaskCompletionSource , e );
174
177
}
175
178
}
176
179
177
- private void setSignInTaskCompletionError ( FirebaseAppDistributionException e ) {
180
+ private void setSuccessfulSignInResult ( ) {
178
181
synchronized (signInTaskLock ) {
179
- safeSetTaskException (signInTaskCompletionSource , e );
182
+ safeSetTaskResult (signInTaskCompletionSource , null );
180
183
}
181
184
}
182
185
@@ -207,7 +210,6 @@ private void openSignInFlowInBrowser(String fid, Activity activity) {
207
210
intent .addFlags (Intent .FLAG_ACTIVITY_NO_HISTORY );
208
211
intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
209
212
customTabsIntent .launchUrl (activity , uri );
210
-
211
213
} else {
212
214
// If we can't launch a chrome view try to launch anything that can handle a URL.
213
215
Intent browserIntent = new Intent (Intent .ACTION_VIEW , uri );
0 commit comments