Skip to content

Commit 03f3a0b

Browse files
Address PR feedback: add/fix comments, refactor code
1 parent 02522c8 commit 03f3a0b

File tree

4 files changed

+60
-51
lines changed

4 files changed

+60
-51
lines changed

firebase-app-distribution/src/main/java/com/google/firebase/appdistribution/FirebaseAppDistribution.java

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
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+
115
package com.google.firebase.appdistribution;
216

317
import static com.google.firebase.appdistribution.FirebaseAppDistributionException.Status.AUTHENTICATION_CANCELED;
@@ -35,7 +49,7 @@ public class FirebaseAppDistribution implements Application.ActivityLifecycleCal
3549
private static final String TAG = "FirebaseAppDistribution";
3650
private Activity currentActivity;
3751
@VisibleForTesting private boolean currentlySigningIn = false;
38-
private TaskCompletionSource<Void> signInTaskCompletionSource;
52+
private TaskCompletionSource<Void> signInTaskCompletionSource = null;
3953

4054
/** Constructor for FirebaseAppDistribution */
4155
public FirebaseAppDistribution(
@@ -73,15 +87,11 @@ public static FirebaseAppDistribution getInstance(@NonNull FirebaseApp app) {
7387
*/
7488
@NonNull
7589
public Task<AppDistributionRelease> updateToLatestRelease() {
76-
Log.v("updateToLatestRelease", "called");
77-
Log.v("updateToLatestRelease", currentActivity.getClass().getName());
7890

7991
TaskCompletionSource<AppDistributionRelease> taskCompletionSource =
8092
new TaskCompletionSource<>();
8193

82-
Task<Void> signInTask = signInTester();
83-
84-
signInTask
94+
signInTester()
8595
.addOnSuccessListener(
8696
new OnSuccessListener<Void>() {
8797
@Override
@@ -131,7 +141,7 @@ private boolean supportsCustomTabs(Context context) {
131141
return resolveInfos != null && !resolveInfos.isEmpty();
132142
}
133143

134-
public static @NonNull String getApplicationName(@NonNull Context context) {
144+
private static String getApplicationName(Context context) {
135145
try {
136146
return context.getApplicationInfo().loadLabel(context.getPackageManager()).toString();
137147
} catch (Exception e) {
@@ -140,23 +150,14 @@ private boolean supportsCustomTabs(Context context) {
140150
}
141151
}
142152

143-
private void openSignIn(Uri uri) {
153+
private void openSignInFlowInBrowser(Uri uri) {
144154
currentlySigningIn = true;
145155
if (supportsCustomTabs(firebaseApp.getApplicationContext())) {
146156
// 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();
156158
Intent intent = customTabsIntent.intent;
157159
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
158160
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
159-
160161
customTabsIntent.launchUrl(currentActivity, uri);
161162

162163
} else {
@@ -169,14 +170,22 @@ private void openSignIn(Uri uri) {
169170
}
170171
}
171172

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+
}
179187

188+
private AlertDialog getSignInAlertDialog(Context context) {
180189
AlertDialog alertDialog = new AlertDialog.Builder(currentActivity).create();
181190
alertDialog.setTitle(context.getString(R.string.signin_dialog_title));
182191
alertDialog.setMessage(context.getString(R.string.singin_dialog_message));
@@ -188,25 +197,7 @@ public Task<Void> signInTester() {
188197
public void onClick(DialogInterface dialogInterface, int i) {
189198
firebaseInstallationsApi
190199
.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))
210201
.addOnFailureListener(
211202
new OnFailureListener() {
212203
@Override
@@ -228,14 +219,23 @@ public void onClick(DialogInterface dialogInterface, int i) {
228219
dialogInterface.dismiss();
229220
}
230221
});
222+
return alertDialog;
223+
}
231224

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);
232232
alertDialog.show();
233233

234234
return signInTaskCompletionSource.getTask();
235235
}
236236

237237
private void setSignInTaskCompletionError(FirebaseAppDistributionException e) {
238-
if (!signInTaskCompletionSource.getTask().isComplete()) {
238+
if (signInTaskCompletionSource != null && !signInTaskCompletionSource.getTask().isComplete()) {
239239
this.signInTaskCompletionSource.setException(e);
240240
}
241241
}
@@ -269,9 +269,12 @@ public void onActivityStarted(@NonNull @NotNull Activity activity) {
269269
public void onActivityResumed(@NonNull @NotNull Activity activity) {
270270
Log.d(TAG, "Resumed activity: " + activity.getClass().getName());
271271

272+
// signInActivity is only opened after successful redirection from signIn flow,
273+
// should not be treated as reentering the app
272274
if (activity instanceof SignInResultActivity) {
273275
return;
274276
}
277+
275278
// throw error if app reentered during signin
276279
if (currentlySigningIn) {
277280
currentlySigningIn = false;
@@ -299,7 +302,6 @@ public void onActivitySaveInstanceState(
299302
@Override
300303
public void onActivityDestroyed(@NonNull @NotNull Activity activity) {
301304
Log.d(TAG, "Destroyed activity: " + activity.getClass().getName());
302-
// destroyed comes after resumed
303305
if (this.currentActivity == activity) {
304306
this.currentActivity = null;
305307
}

firebase-app-distribution/src/main/java/com/google/firebase/appdistribution/FirebaseAppDistributionRegistrar.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
2525
import java.util.Arrays;
2626
import java.util.List;
2727

28-
/** Registers FirebaseAppDistribution */
28+
/**
29+
* Registers FirebaseAppDistribution
30+
*
31+
* @hide
32+
*/
2933
@Keep
3034
public class FirebaseAppDistributionRegistrar implements ComponentRegistrar {
3135
@Override

firebase-app-distribution/src/main/java/com/google/firebase/appdistribution/SignInResultActivity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
import androidx.annotation.NonNull;
1919
import androidx.appcompat.app.AppCompatActivity;
2020

21+
/**
22+
* Activity opened through Deep Link when returning from web signIn flow. SignIn task is successful
23+
* when SignInResultActivity is created.
24+
*/
2125
public class SignInResultActivity extends AppCompatActivity {
2226

2327
@Override

firebase-app-distribution/src/test/java/com/google/firebase/appdistribution/FirebaseAppDistributionTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,14 @@ public void setup() {
9898
}
9999

100100
@Test
101-
public void signInTester_whenDialogConfirmed_andChromeAvailable_opensCustomTab() {
101+
public void signInTester_whenDialogConfirmedAndChromeAvailable_opensCustomTab() {
102102

103103
firebaseAppDistribution.onActivityResumed(activity);
104104
final ResolveInfo resolveInfo = new ResolveInfo();
105105
resolveInfo.resolvePackageName = "garbage";
106106
final Intent customTabIntent =
107107
new Intent("android.support.customtabs.action.CustomTabsService");
108108
customTabIntent.setPackage("com.android.chrome");
109-
// todo: change to addActivityForIntent
110109
shadowPackageManager.addResolveInfoForIntent(customTabIntent, resolveInfo);
111110

112111
Task<Void> signInTask = firebaseAppDistribution.signInTester();
@@ -122,7 +121,7 @@ public void signInTester_whenDialogConfirmed_andChromeAvailable_opensCustomTab()
122121
}
123122

124123
@Test
125-
public void signInTester_whenReopenApp_duringSignIn_taskFails() {
124+
public void signInTester_whenReopenAppDuringSignIn_taskFails() {
126125
firebaseAppDistribution.onActivityResumed(activity);
127126
Task<Void> signInTask = firebaseAppDistribution.signInTester();
128127
if (ShadowAlertDialog.getLatestDialog() instanceof AlertDialog) {
@@ -138,7 +137,7 @@ public void signInTester_whenReopenApp_duringSignIn_taskFails() {
138137
}
139138

140139
@Test
141-
public void signInTester_whenReturn_fromSignIn_taskSucceeds() {
140+
public void signInTester_whenReturnFromSignIn_taskSucceeds() {
142141
firebaseAppDistribution.onActivityResumed(activity);
143142
Task<Void> signInTask = firebaseAppDistribution.signInTester();
144143
if (ShadowAlertDialog.getLatestDialog() instanceof AlertDialog) {

0 commit comments

Comments
 (0)