Skip to content

Fad only show UI for basic config #3260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions firebase-app-distribution/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ package com.google.firebase.app.distribution {
enum_constant public static final com.google.firebase.app.distribution.FirebaseAppDistributionException.Status AUTHENTICATION_CANCELED;
enum_constant public static final com.google.firebase.app.distribution.FirebaseAppDistributionException.Status AUTHENTICATION_FAILURE;
enum_constant public static final com.google.firebase.app.distribution.FirebaseAppDistributionException.Status DOWNLOAD_FAILURE;
enum_constant public static final com.google.firebase.app.distribution.FirebaseAppDistributionException.Status FOREGROUND_ACTIVITY_NOT_AVAILABLE;
enum_constant public static final com.google.firebase.app.distribution.FirebaseAppDistributionException.Status INSTALLATION_CANCELED;
enum_constant public static final com.google.firebase.app.distribution.FirebaseAppDistributionException.Status INSTALLATION_FAILURE;
enum_constant public static final com.google.firebase.app.distribution.FirebaseAppDistributionException.Status INSTALLATION_FAILURE_SIGNATURE_MISMATCH;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ HttpsURLConnection openHttpsUrlConnection(String downloadUrl)

private void redirectToPlayForAabUpdate(String downloadUrl) {
synchronized (updateAabLock) {
// TODO(rachelprince): change this to getCurrentNonNullActivity
if (lifecycleNotifier.getCurrentActivity() == null) {
safeSetTaskException(
cachedUpdateTask,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.firebase.app.distribution;

import static com.google.firebase.app.distribution.FirebaseAppDistributionException.Status.AUTHENTICATION_CANCELED;
import static com.google.firebase.app.distribution.FirebaseAppDistributionException.Status.AUTHENTICATION_FAILURE;
import static com.google.firebase.app.distribution.FirebaseAppDistributionException.Status.UPDATE_NOT_AVAILABLE;
import static com.google.firebase.app.distribution.TaskUtils.safeSetTaskException;
Expand All @@ -27,6 +28,7 @@
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskCompletionSource;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.FirebaseApp;
import com.google.firebase.app.distribution.Constants.ErrorMessages;
Expand All @@ -37,6 +39,7 @@
import com.google.firebase.installations.FirebaseInstallationsApi;

public class FirebaseAppDistribution {

private static final int UNKNOWN_RELEASE_FILE_SIZE = -1;

private final FirebaseApp firebaseApp;
Expand All @@ -59,7 +62,7 @@ public class FirebaseAppDistribution {

private Task<AppDistributionRelease> cachedCheckForNewReleaseTask;
private AlertDialog updateDialog;
private boolean updateDialogShown;
private AlertDialog signInDialog;

/** Constructor for FirebaseAppDistribution */
@VisibleForTesting
Expand Down Expand Up @@ -129,10 +132,12 @@ public UpdateTask updateIfNewReleaseAvailable() {
if (cachedUpdateIfNewReleaseTask != null && !cachedUpdateIfNewReleaseTask.isComplete()) {
return cachedUpdateIfNewReleaseTask;
}

cachedUpdateIfNewReleaseTask = new UpdateTaskImpl();
}
checkForNewRelease()

showSignInDialog()
.onSuccessTask(unused -> signInTester())
.onSuccessTask(unused -> checkForNewRelease())
.onSuccessTask(
release -> {
if (release == null) {
Expand All @@ -147,26 +152,110 @@ public UpdateTask updateIfNewReleaseAvailable() {
}
return showUpdateAlertDialog(release);
})
.addOnFailureListener(
e -> {
postProgressToCachedUpdateIfNewReleaseTask(
UpdateProgress.builder()
.setApkFileTotalBytes(UNKNOWN_RELEASE_FILE_SIZE)
.setApkBytesDownloaded(UNKNOWN_RELEASE_FILE_SIZE)
.setUpdateStatus(UpdateStatus.NEW_RELEASE_CHECK_FAILED)
.build());
setCachedUpdateIfNewReleaseCompletionError(
e,
new FirebaseAppDistributionException(
Constants.ErrorMessages.NETWORK_ERROR,
FirebaseAppDistributionException.Status.NETWORK_FAILURE));
});
.onSuccessTask(
unused ->
updateApp(true)
.addOnProgressListener(this::postProgressToCachedUpdateIfNewReleaseTask)
.addOnFailureListener(this::setCachedUpdateIfNewReleaseCompletionError))
.addOnFailureListener(this::setCachedUpdateIfNewReleaseCompletionError);

synchronized (updateIfNewReleaseTaskLock) {
return cachedUpdateIfNewReleaseTask;
}
}

private Task<Void> showSignInDialog() {
if (isTesterSignedIn()) {
return Tasks.forResult(null);
}

TaskCompletionSource<Void> showDialogTask = new TaskCompletionSource<>();

Activity currentActivity;
try {
currentActivity = lifecycleNotifier.getNonNullCurrentActivity();
} catch (FirebaseAppDistributionException e) {
return Tasks.forException(e);
}

signInDialog = new AlertDialog.Builder(currentActivity).create();
Context context = firebaseApp.getApplicationContext();
signInDialog.setTitle(context.getString(R.string.signin_dialog_title));
signInDialog.setMessage(context.getString(R.string.singin_dialog_message));

signInDialog.setButton(
AlertDialog.BUTTON_POSITIVE,
context.getString(R.string.singin_yes_button),
(dialogInterface, i) -> showDialogTask.setResult(null));

signInDialog.setButton(
AlertDialog.BUTTON_NEGATIVE,
context.getString(R.string.singin_no_button),
(dialogInterface, i) ->
showDialogTask.setException(
new FirebaseAppDistributionException(
ErrorMessages.AUTHENTICATION_CANCELED, AUTHENTICATION_CANCELED)));

signInDialog.setOnCancelListener(
dialogInterface ->
showDialogTask.setException(
new FirebaseAppDistributionException(
ErrorMessages.AUTHENTICATION_CANCELED, AUTHENTICATION_CANCELED)));

signInDialog.show();
return showDialogTask.getTask();
}

private Task<Void> showUpdateAlertDialog(AppDistributionRelease newRelease) {
TaskCompletionSource<Void> showUpdateDialogTask = new TaskCompletionSource<>();

Activity currentActivity;
try {
currentActivity = lifecycleNotifier.getNonNullCurrentActivity();
} catch (FirebaseAppDistributionException e) {
return Tasks.forException(e);
}

Context context = firebaseApp.getApplicationContext();

updateDialog = new AlertDialog.Builder(currentActivity).create();
updateDialog.setTitle(context.getString(R.string.update_dialog_title));

StringBuilder message =
new StringBuilder(
String.format(
"Version %s (%s) is available.",
newRelease.getDisplayVersion(), newRelease.getVersionCode()));

if (newRelease.getReleaseNotes() != null && !newRelease.getReleaseNotes().isEmpty()) {
message.append(String.format("\n\nRelease notes: %s", newRelease.getReleaseNotes()));
}
updateDialog.setMessage(message);

updateDialog.setButton(
AlertDialog.BUTTON_POSITIVE,
context.getString(R.string.update_yes_button),
(dialogInterface, i) -> showUpdateDialogTask.setResult(null));

updateDialog.setButton(
AlertDialog.BUTTON_NEGATIVE,
context.getString(R.string.update_no_button),
(dialogInterface, i) ->
showUpdateDialogTask.setException(
new FirebaseAppDistributionException(
ErrorMessages.UPDATE_CANCELED, Status.INSTALLATION_CANCELED)));

updateDialog.setOnCancelListener(
dialogInterface ->
showUpdateDialogTask.setException(
new FirebaseAppDistributionException(
ErrorMessages.UPDATE_CANCELED, Status.INSTALLATION_CANCELED)));

updateDialog.show();

return showUpdateDialogTask.getTask();
}

/** Signs in the App Distribution tester. Presents the tester with a Google sign in UI */
@NonNull
public Task<Void> signInTester() {
Expand Down Expand Up @@ -195,6 +284,13 @@ public synchronized Task<AppDistributionRelease> checkForNewRelease() {
})
.addOnFailureListener(
e -> {
postProgressToCachedUpdateIfNewReleaseTask(
UpdateProgress.builder()
.setApkBytesDownloaded(UNKNOWN_RELEASE_FILE_SIZE)
.setApkFileTotalBytes(UNKNOWN_RELEASE_FILE_SIZE)
.setUpdateStatus(UpdateStatus.NEW_RELEASE_CHECK_FAILED)
.build());

if (e instanceof FirebaseAppDistributionException
&& ((FirebaseAppDistributionException) e).getErrorCode()
== AUTHENTICATION_FAILURE) {
Expand Down Expand Up @@ -273,7 +369,7 @@ void onActivityDestroyed(@NonNull Activity activity) {
// SignInResult is internal to the SDK and is destroyed after creation
return;
}
if (updateDialogShown) {
if (updateDialog != null && updateDialog.isShowing()) {
setCachedUpdateIfNewReleaseCompletionError(
new FirebaseAppDistributionException(
ErrorMessages.UPDATE_CANCELED, Status.INSTALLATION_CANCELED));
Expand All @@ -294,113 +390,38 @@ AppDistributionReleaseInternal getCachedNewRelease() {
}
}

private UpdateTaskImpl showUpdateAlertDialog(AppDistributionRelease newRelease) {
Context context = firebaseApp.getApplicationContext();
Activity currentActivity = lifecycleNotifier.getCurrentActivity();
if (currentActivity == null) {
LogWrapper.getInstance().e("No foreground activity found.");
UpdateTaskImpl updateTask = new UpdateTaskImpl();
updateTask.setException(
new FirebaseAppDistributionException(
ErrorMessages.APP_BACKGROUNDED,
FirebaseAppDistributionException.Status.UPDATE_NOT_AVAILABLE));
return updateTask;
}
updateDialog = new AlertDialog.Builder(currentActivity).create();
updateDialog.setTitle(context.getString(R.string.update_dialog_title));

StringBuilder message =
new StringBuilder(
String.format(
"Version %s (%s) is available.",
newRelease.getDisplayVersion(), newRelease.getVersionCode()));

if (newRelease.getReleaseNotes() != null && !newRelease.getReleaseNotes().isEmpty()) {
message.append(String.format("\n\nRelease notes: %s", newRelease.getReleaseNotes()));
}

updateDialog.setMessage(message);
updateDialog.setButton(
AlertDialog.BUTTON_POSITIVE,
context.getString(R.string.update_yes_button),
(dialogInterface, i) -> {
synchronized (updateIfNewReleaseTaskLock) {
// show download progress in notification manager
updateApp(true)
.addOnProgressListener(this::postProgressToCachedUpdateIfNewReleaseTask)
.addOnSuccessListener(unused -> setCachedUpdateIfNewReleaseResult())
.addOnFailureListener(cachedUpdateIfNewReleaseTask::setException);
}
});

updateDialog.setButton(
AlertDialog.BUTTON_NEGATIVE,
context.getString(R.string.update_no_button),
(dialogInterface, i) -> dismissUpdateDialogCallback());

updateDialog.setOnCancelListener(
dialogInterface -> {
dismissUpdateDialogCallback();
});

updateDialog.show();
updateDialogShown = true;
synchronized (updateIfNewReleaseTaskLock) {
return cachedUpdateIfNewReleaseTask;
}
}

private void dismissUpdateDialogCallback() {
synchronized (updateIfNewReleaseTaskLock) {
postProgressToCachedUpdateIfNewReleaseTask(
UpdateProgress.builder()
.setApkFileTotalBytes(UNKNOWN_RELEASE_FILE_SIZE)
.setApkBytesDownloaded(UNKNOWN_RELEASE_FILE_SIZE)
.setUpdateStatus(UpdateStatus.UPDATE_CANCELED)
.build());
setCachedUpdateIfNewReleaseCompletionError(
new FirebaseAppDistributionException(
ErrorMessages.UPDATE_CANCELED, Status.INSTALLATION_CANCELED));
}
}

private void setCachedUpdateIfNewReleaseCompletionError(FirebaseAppDistributionException e) {
private void setCachedUpdateIfNewReleaseCompletionError(Exception e) {
synchronized (updateIfNewReleaseTaskLock) {
safeSetTaskException(cachedUpdateIfNewReleaseTask, e);
}
dismissUpdateDialog();
}

private void setCachedUpdateIfNewReleaseCompletionError(
Exception e, FirebaseAppDistributionException defaultFirebaseException) {
if (e instanceof FirebaseAppDistributionException) {
setCachedUpdateIfNewReleaseCompletionError((FirebaseAppDistributionException) e);
} else {
setCachedUpdateIfNewReleaseCompletionError(defaultFirebaseException);
}
dismissDialogs();
}

private void postProgressToCachedUpdateIfNewReleaseTask(UpdateProgress progress) {
synchronized (updateIfNewReleaseTaskLock) {
cachedUpdateIfNewReleaseTask.updateProgress(progress);
if (cachedUpdateIfNewReleaseTask != null && !cachedUpdateIfNewReleaseTask.isComplete()) {
cachedUpdateIfNewReleaseTask.updateProgress(progress);
}
}
}

private void setCachedUpdateIfNewReleaseResult() {
synchronized (updateIfNewReleaseTaskLock) {
safeSetTaskResult(cachedUpdateIfNewReleaseTask);
}
dismissUpdateDialog();
dismissDialogs();
}

private void dismissUpdateDialog() {
if (updateDialog != null) {
private void dismissDialogs() {
if (signInDialog != null && signInDialog.isShowing()) {
signInDialog.dismiss();
}
if (updateDialog != null && updateDialog.isShowing()) {
updateDialog.dismiss();
updateDialogShown = false;
}
}

private UpdateTask getErrorUpdateTask(Exception e) {
private UpdateTaskImpl getErrorUpdateTask(Exception e) {
UpdateTaskImpl updateTask = new UpdateTaskImpl();
updateTask.setException(e);
return updateTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public enum Status {
/** Installation canceled */
INSTALLATION_CANCELED,

/** No foreground activity available for a given intent */
// TODO(rachelprince): add this to API council review
FOREGROUND_ACTIVITY_NOT_AVAILABLE,

/** Update not available for the current tester and app */
UPDATE_NOT_AVAILABLE,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import androidx.annotation.GuardedBy;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.firebase.app.distribution.Constants.ErrorMessages;
import com.google.firebase.app.distribution.FirebaseAppDistributionException.Status;
import java.util.ArrayDeque;
import java.util.Queue;

Expand Down Expand Up @@ -78,6 +80,16 @@ Activity getCurrentActivity() {
}
}

Activity getNonNullCurrentActivity() throws FirebaseAppDistributionException {
synchronized (lock) {
if (currentActivity == null) {
throw new FirebaseAppDistributionException(
ErrorMessages.APP_BACKGROUNDED, Status.FOREGROUND_ACTIVITY_NOT_AVAILABLE);
}
return currentActivity;
}
}

void addOnActivityCreatedListener(@NonNull OnActivityCreatedListener listener) {
synchronized (lock) {
this.onActivityCreatedListeners.add(listener);
Expand Down
Loading