1
+ // Copyright 2021 Google LLC
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
1
15
package com .google .firebase .appdistribution ;
2
16
3
17
import static com .google .firebase .appdistribution .FirebaseAppDistributionException .Status .AUTHENTICATION_CANCELED ;
@@ -35,7 +49,7 @@ public class FirebaseAppDistribution implements Application.ActivityLifecycleCal
35
49
private static final String TAG = "FirebaseAppDistribution" ;
36
50
private Activity currentActivity ;
37
51
@ VisibleForTesting private boolean currentlySigningIn = false ;
38
- private TaskCompletionSource <Void > signInTaskCompletionSource ;
52
+ private TaskCompletionSource <Void > signInTaskCompletionSource = null ;
39
53
40
54
/** Constructor for FirebaseAppDistribution */
41
55
public FirebaseAppDistribution (
@@ -73,15 +87,11 @@ public static FirebaseAppDistribution getInstance(@NonNull FirebaseApp app) {
73
87
*/
74
88
@ NonNull
75
89
public Task <AppDistributionRelease > updateToLatestRelease () {
76
- Log .v ("updateToLatestRelease" , "called" );
77
- Log .v ("updateToLatestRelease" , currentActivity .getClass ().getName ());
78
90
79
91
TaskCompletionSource <AppDistributionRelease > taskCompletionSource =
80
92
new TaskCompletionSource <>();
81
93
82
- Task <Void > signInTask = signInTester ();
83
-
84
- signInTask
94
+ signInTester ()
85
95
.addOnSuccessListener (
86
96
new OnSuccessListener <Void >() {
87
97
@ Override
@@ -131,7 +141,7 @@ private boolean supportsCustomTabs(Context context) {
131
141
return resolveInfos != null && !resolveInfos .isEmpty ();
132
142
}
133
143
134
- public static @ NonNull String getApplicationName (@ NonNull Context context ) {
144
+ private static String getApplicationName (Context context ) {
135
145
try {
136
146
return context .getApplicationInfo ().loadLabel (context .getPackageManager ()).toString ();
137
147
} catch (Exception e ) {
@@ -140,23 +150,14 @@ private boolean supportsCustomTabs(Context context) {
140
150
}
141
151
}
142
152
143
- private void openSignIn (Uri uri ) {
153
+ private void openSignInFlowInBrowser (Uri uri ) {
144
154
currentlySigningIn = true ;
145
155
if (supportsCustomTabs (firebaseApp .getApplicationContext ())) {
146
156
// If we can launch a chrome view, try that.
147
- CustomTabsIntent .Builder builder = new CustomTabsIntent .Builder ();
148
-
149
- builder .setStartAnimations (
150
- currentActivity .getApplicationContext (), R .anim .slide_in_right , R .anim .slide_out_left );
151
-
152
- builder .setExitAnimations (
153
- currentActivity .getApplicationContext (), R .anim .slide_in_left , R .anim .slide_out_right );
154
-
155
- CustomTabsIntent customTabsIntent = builder .build ();
157
+ CustomTabsIntent customTabsIntent = new CustomTabsIntent .Builder ().build ();
156
158
Intent intent = customTabsIntent .intent ;
157
159
intent .addFlags (Intent .FLAG_ACTIVITY_NO_HISTORY );
158
160
intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
159
-
160
161
customTabsIntent .launchUrl (currentActivity , uri );
161
162
162
163
} else {
@@ -169,14 +170,22 @@ private void openSignIn(Uri uri) {
169
170
}
170
171
}
171
172
172
- /** Signs in the App Distribution tester. Presents the tester with a Google sign in UI */
173
- @ NonNull
174
- public Task <Void > signInTester () {
175
- // TaskCompletionSource<Void> taskCompletionSource = new TaskCompletionSource<>();
176
- this .signInTaskCompletionSource = new TaskCompletionSource <>();
177
-
178
- Context context = firebaseApp .getApplicationContext ();
173
+ private OnSuccessListener <String > getFidGenerationOnSuccessListener (Context context ) {
174
+ return new OnSuccessListener <String >() {
175
+ @ Override
176
+ public void onSuccess (String fid ) {
177
+ Uri uri =
178
+ Uri .parse (
179
+ String .format (
180
+ "https://appdistribution.firebase.dev/nba/pub/apps/"
181
+ + "%s/installations/%s/buildalerts?appName=%s" ,
182
+ firebaseApp .getOptions ().getApplicationId (), fid , getApplicationName (context )));
183
+ openSignInFlowInBrowser (uri );
184
+ }
185
+ };
186
+ }
179
187
188
+ private AlertDialog getSignInAlertDialog (Context context ) {
180
189
AlertDialog alertDialog = new AlertDialog .Builder (currentActivity ).create ();
181
190
alertDialog .setTitle (context .getString (R .string .signin_dialog_title ));
182
191
alertDialog .setMessage (context .getString (R .string .singin_dialog_message ));
@@ -188,25 +197,7 @@ public Task<Void> signInTester() {
188
197
public void onClick (DialogInterface dialogInterface , int i ) {
189
198
firebaseInstallationsApi
190
199
.getId ()
191
- .addOnSuccessListener (
192
- new OnSuccessListener <String >() {
193
- @ Override
194
- public void onSuccess (String fid ) {
195
-
196
- Uri uri =
197
- Uri .parse (
198
- String .format (
199
- "https://appdistribution.firebase.dev/nba/pub/apps/"
200
- + "%s/installations/%s/buildalerts?appName=%s" ,
201
- firebaseApp .getOptions ().getApplicationId (),
202
- fid ,
203
- getApplicationName (context )));
204
-
205
- Log .v ("FAD Url" , uri .toString ());
206
- Log .v ("FirebaseApp name" , firebaseApp .getName ());
207
- openSignIn (uri );
208
- }
209
- })
200
+ .addOnSuccessListener (getFidGenerationOnSuccessListener (context ))
210
201
.addOnFailureListener (
211
202
new OnFailureListener () {
212
203
@ Override
@@ -228,14 +219,23 @@ public void onClick(DialogInterface dialogInterface, int i) {
228
219
dialogInterface .dismiss ();
229
220
}
230
221
});
222
+ return alertDialog ;
223
+ }
231
224
225
+ /** Signs in the App Distribution tester. Presents the tester with a Google sign in UI */
226
+ @ NonNull
227
+ public Task <Void > signInTester () {
228
+
229
+ this .signInTaskCompletionSource = new TaskCompletionSource <>();
230
+ Context context = firebaseApp .getApplicationContext ();
231
+ AlertDialog alertDialog = getSignInAlertDialog (context );
232
232
alertDialog .show ();
233
233
234
234
return signInTaskCompletionSource .getTask ();
235
235
}
236
236
237
237
private void setSignInTaskCompletionError (FirebaseAppDistributionException e ) {
238
- if (!signInTaskCompletionSource .getTask ().isComplete ()) {
238
+ if (signInTaskCompletionSource != null && !signInTaskCompletionSource .getTask ().isComplete ()) {
239
239
this .signInTaskCompletionSource .setException (e );
240
240
}
241
241
}
@@ -269,9 +269,12 @@ public void onActivityStarted(@NonNull @NotNull Activity activity) {
269
269
public void onActivityResumed (@ NonNull @ NotNull Activity activity ) {
270
270
Log .d (TAG , "Resumed activity: " + activity .getClass ().getName ());
271
271
272
+ // signInActivity is only opened after successful redirection from signIn flow,
273
+ // should not be treated as reentering the app
272
274
if (activity instanceof SignInResultActivity ) {
273
275
return ;
274
276
}
277
+
275
278
// throw error if app reentered during signin
276
279
if (currentlySigningIn ) {
277
280
currentlySigningIn = false ;
@@ -299,7 +302,6 @@ public void onActivitySaveInstanceState(
299
302
@ Override
300
303
public void onActivityDestroyed (@ NonNull @ NotNull Activity activity ) {
301
304
Log .d (TAG , "Destroyed activity: " + activity .getClass ().getName ());
302
- // destroyed comes after resumed
303
305
if (this .currentActivity == activity ) {
304
306
this .currentActivity = null ;
305
307
}
0 commit comments