Skip to content

Commit d318e06

Browse files
author
rachaprince
authored
Update API and add methods to UpdateTaskImpl (#2882)
1 parent acf713a commit d318e06

File tree

11 files changed

+178
-115
lines changed

11 files changed

+178
-115
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public static Builder builder() {
3939
@NonNull
4040
public abstract String getDisplayVersion();
4141

42-
/** The bundle version of this build (example: 123) */
42+
/** The version code of this build (example: 123) */
4343
@NonNull
44-
public abstract String getBuildVersion();
44+
public abstract long getVersionCode();
4545

4646
/** The release notes for this build */
4747
@Nullable
@@ -59,7 +59,7 @@ public abstract static class Builder {
5959
public abstract Builder setDisplayVersion(@NonNull String value);
6060

6161
@NonNull
62-
public abstract Builder setBuildVersion(@NonNull String value);
62+
public abstract Builder setVersionCode(@NonNull long value);
6363

6464
@NonNull
6565
public abstract Builder setReleaseNotes(@Nullable String value);

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class FirebaseAppDistribution implements Application.ActivityLifecycleCal
4343
private final UpdateAppClient updateAppClient;
4444
private Activity currentActivity;
4545

46-
private Task<AppDistributionRelease> cachedUpdateToLatestReleaseTask;
46+
private Task<Void> cachedUpdateToLatestReleaseTask;
4747
private Task<AppDistributionRelease> cachedCheckForUpdateTask;
4848
private AppDistributionReleaseInternal cachedLatestRelease;
4949
private final SignInStorage signInStorage;
@@ -114,7 +114,7 @@ public static FirebaseAppDistribution getInstance(@NonNull FirebaseApp app) {
114114
* to complete the download and installation.
115115
*/
116116
@NonNull
117-
public synchronized Task<AppDistributionRelease> updateToLatestRelease() {
117+
public synchronized Task<Void> updateToLatestRelease() {
118118
if (cachedUpdateToLatestReleaseTask != null && !cachedUpdateToLatestReleaseTask.isComplete()) {
119119
return cachedUpdateToLatestReleaseTask;
120120
}
@@ -264,8 +264,14 @@ private AppDistributionRelease convertToAppDistributionRelease(
264264
if (internalRelease == null) {
265265
return null;
266266
}
267+
long versionCode;
268+
try {
269+
versionCode = Long.parseLong(internalRelease.getBuildVersion());
270+
} catch (NumberFormatException e) {
271+
versionCode = 0;
272+
}
267273
return AppDistributionRelease.builder()
268-
.setBuildVersion(internalRelease.getBuildVersion())
274+
.setVersionCode(versionCode)
269275
.setDisplayVersion(internalRelease.getDisplayVersion())
270276
.setReleaseNotes(internalRelease.getReleaseNotes())
271277
.setBinaryType(internalRelease.getBinaryType())
@@ -282,9 +288,8 @@ AppDistributionReleaseInternal getCachedLatestRelease() {
282288
return this.cachedLatestRelease;
283289
}
284290

285-
private Task<AppDistributionRelease> showUpdateAlertDialog(AppDistributionRelease latestRelease) {
286-
TaskCompletionSource<AppDistributionRelease> updateAlertDialogTask =
287-
new TaskCompletionSource<>();
291+
private Task<Void> showUpdateAlertDialog(AppDistributionRelease latestRelease) {
292+
TaskCompletionSource<Void> updateAlertDialogTask = new TaskCompletionSource<>();
288293
Context context = firebaseApp.getApplicationContext();
289294
AlertDialog alertDialog = new AlertDialog.Builder(currentActivity).create();
290295
alertDialog.setTitle(context.getString(R.string.update_dialog_title));
@@ -293,7 +298,7 @@ private Task<AppDistributionRelease> showUpdateAlertDialog(AppDistributionReleas
293298
new StringBuilder(
294299
String.format(
295300
"Version %s (%s) is available.",
296-
latestRelease.getDisplayVersion(), latestRelease.getBuildVersion()));
301+
latestRelease.getDisplayVersion(), latestRelease.getVersionCode()));
297302

298303
if (latestRelease.getReleaseNotes() != null && !latestRelease.getReleaseNotes().isEmpty()) {
299304
message.append(String.format("\n\nRelease notes: %s", latestRelease.getReleaseNotes()));
@@ -306,7 +311,7 @@ private Task<AppDistributionRelease> showUpdateAlertDialog(AppDistributionReleas
306311
(dialogInterface, i) -> {
307312
try {
308313
updateApp()
309-
.addOnSuccessListener(unused -> updateAlertDialogTask.setResult(latestRelease))
314+
.addOnSuccessListener(unused -> updateAlertDialogTask.setResult(null))
310315
.addOnFailureListener(updateAlertDialogTask::setException);
311316
} catch (FirebaseAppDistributionException e) {
312317
updateAlertDialogTask.setException(e);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818

1919
/** A listener that is called periodically during execution of the {@link UpdateTask}. */
2020
public interface OnProgressListener {
21-
void onProgressUpdate(@NonNull UpdateState updateState);
21+
void onProgressUpdate(@NonNull UpdateProgress updateState);
2222
}

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

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
/** Client class that handles updateApp functionality for APKs in {@link UpdateAppClient}. */
4242
class UpdateApkClient {
4343

44-
private int UPDATE_INTERVAL_MS = 250;
44+
private final int UPDATE_INTERVAL_MS = 250;
4545
private static final String TAG = "FADUpdateAppClient";
4646
private static final String REQUEST_METHOD = "GET";
4747
private TaskCompletionSource<File> downloadTaskCompletionSource;
@@ -68,28 +68,26 @@ public void updateApk(
6868
this.updateAppTaskCompletionSource = updateAppTaskCompletionSource;
6969
downloadApk(downloadUrl)
7070
.addOnSuccessListener(
71-
file -> {
72-
install(file.getPath(), currentActivity)
73-
.addOnSuccessListener(
74-
downloadExecutor,
75-
Void -> {
76-
UpdateState updateState =
77-
UpdateState.builder()
78-
.setApkTotalBytesToDownload(file.length())
79-
.setApkBytesDownloaded(file.length())
80-
.setUpdateStatus(UpdateStatus.INSTALLED)
81-
.build();
82-
updateTask.updateProgress(updateState);
83-
updateAppTaskCompletionSource.setResult(updateState);
84-
})
85-
.addOnFailureListener(
86-
e ->
87-
setUpdateAppErrorWithDefault(
88-
e,
89-
new FirebaseAppDistributionException(
90-
Constants.ErrorMessages.NETWORK_ERROR,
91-
FirebaseAppDistributionException.Status.INSTALLATION_FAILURE)));
92-
})
71+
file ->
72+
install(file.getPath(), currentActivity)
73+
.addOnSuccessListener(
74+
downloadExecutor,
75+
Void -> {
76+
updateTask.updateProgress(
77+
UpdateProgress.builder()
78+
.setApkFileTotalBytes(file.length())
79+
.setApkBytesDownloaded(file.length())
80+
.setUpdateStatus(UpdateStatus.DOWNLOADED)
81+
.build());
82+
updateAppTaskCompletionSource.setResult(null);
83+
})
84+
.addOnFailureListener(
85+
e ->
86+
setUpdateAppErrorWithDefault(
87+
e,
88+
new FirebaseAppDistributionException(
89+
Constants.ErrorMessages.NETWORK_ERROR,
90+
FirebaseAppDistributionException.Status.INSTALLATION_FAILURE))))
9391
.addOnFailureListener(
9492
e ->
9593
setUpdateAppErrorWithDefault(
@@ -277,13 +275,12 @@ private void setUpdateAppErrorWithDefault(
277275

278276
private void postUpdateProgress(long totalBytes, long downloadedBytes, UpdateStatus status) {
279277
downloadHandler.post(
280-
() -> {
281-
updateTask.updateProgress(
282-
UpdateState.builder()
283-
.setApkTotalBytesToDownload(totalBytes)
284-
.setApkBytesDownloaded(downloadedBytes)
285-
.setUpdateStatus(status)
286-
.build());
287-
});
278+
() ->
279+
updateTask.updateProgress(
280+
UpdateProgress.builder()
281+
.setApkFileTotalBytes(totalBytes)
282+
.setApkBytesDownloaded(downloadedBytes)
283+
.setUpdateStatus(status)
284+
.build()));
288285
}
289286
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/** Client class for updateApp functionality in {@link FirebaseAppDistribution}. */
2727
public class UpdateAppClient {
2828

29-
private TaskCompletionSource<UpdateState> updateAppTaskCompletionSource = null;
29+
private TaskCompletionSource<Void> updateAppTaskCompletionSource = null;
3030
private CancellationTokenSource updateAppCancellationSource;
3131
private UpdateTaskImpl updateTask;
3232

@@ -53,6 +53,12 @@ public UpdateTask getUpdateTask(
5353
this.updateTask = new UpdateTaskImpl(updateAppTaskCompletionSource.getTask());
5454

5555
if (latestRelease.getBinaryType() == BinaryType.AAB) {
56+
updateTask.updateProgress(
57+
UpdateProgress.builder()
58+
.setUpdateStatus(UpdateStatus.REDIRECTED_TO_PLAY)
59+
.setApkBytesDownloaded(-1)
60+
.setApkFileTotalBytes(-1)
61+
.build());
5662
redirectToPlayForAabUpdate(latestRelease.getDownloadUrl(), currentActivity);
5763
} else {
5864
this.updateApkClient.updateApk(
@@ -76,17 +82,11 @@ private void redirectToPlayForAabUpdate(String downloadUrl, Activity currentActi
7682
updateIntent.setData(uri);
7783
updateIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
7884
currentActivity.startActivity(updateIntent);
79-
UpdateState updateState =
80-
UpdateState.builder()
81-
.setApkBytesDownloaded(-1)
82-
.setApkTotalBytesToDownload(-1)
83-
.setUpdateStatus(UpdateStatus.REDIRECTED_TO_PLAY)
84-
.build();
85-
updateAppTaskCompletionSource.setResult(updateState);
86-
this.updateTask.updateProgress(updateState);
85+
updateAppTaskCompletionSource.setResult(null);
8786
}
8887

8988
void setInstallationResult(int resultCode) {
9089
this.updateApkClient.setInstallationResult(resultCode);
90+
updateAppTaskCompletionSource.setResult(null);
9191
}
9292
}

firebase-app-distribution/src/main/java/com/google/firebase/appdistribution/UpdateState.java renamed to firebase-app-distribution/src/main/java/com/google/firebase/appdistribution/UpdateProgress.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,39 @@
2020

2121
/** Data class to get download progress for APKs and the status of the update. Used in updateApp. */
2222
@AutoValue
23-
public abstract class UpdateState {
23+
public abstract class UpdateProgress {
2424

2525
@NonNull
26-
public static UpdateState.Builder builder() {
27-
return new AutoValue_UpdateState.Builder();
26+
public static UpdateProgress.Builder builder() {
27+
return new AutoValue_UpdateProgress.Builder();
2828
}
2929

3030
/** The number of bytes downloaded so far for the APK. Returns -1 if called on an AAB. */
3131
@NonNull
3232
public abstract long getApkBytesDownloaded();
3333

34-
/** The total number of bytes to download for the APK. Returns -1 if called on an AAB. */
34+
/** The file size of the APK file to download in bytes. Returns -1 if called on an AAB. */
3535
@NonNull
36-
public abstract long getApkTotalBytesToDownload();
36+
public abstract long getApkFileTotalBytes();
3737

3838
@NonNull
3939
/** returns the current state of the update */
4040
public abstract UpdateStatus getUpdateStatus();
4141

42-
/** Builder for {@link UpdateState}. */
42+
/** Builder for {@link UpdateProgress}. */
4343
@AutoValue.Builder
4444
public abstract static class Builder {
4545

4646
@NonNull
47-
public abstract UpdateState.Builder setApkBytesDownloaded(@NonNull long value);
47+
public abstract UpdateProgress.Builder setApkBytesDownloaded(@NonNull long value);
4848

4949
@NonNull
50-
public abstract UpdateState.Builder setApkTotalBytesToDownload(@NonNull long value);
50+
public abstract UpdateProgress.Builder setApkFileTotalBytes(@NonNull long value);
5151

5252
@NonNull
53-
public abstract UpdateState.Builder setUpdateStatus(@Nullable UpdateStatus value);
53+
public abstract UpdateProgress.Builder setUpdateStatus(@Nullable UpdateStatus value);
5454

5555
@NonNull
56-
public abstract UpdateState build();
56+
public abstract UpdateProgress build();
5757
}
5858
}

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,6 @@ public enum UpdateStatus {
2525
/** Download completed */
2626
DOWNLOADED,
2727

28-
/** Download failed */
29-
DOWNLOAD_FAILED,
30-
31-
/** Update installed */
32-
INSTALLED,
33-
34-
/** Installation canceled */
35-
INSTALL_CANCELED,
36-
37-
/** Installation failed */
38-
INSTALL_FAILED,
39-
4028
/** AAB flow (directed to Play) */
4129
REDIRECTED_TO_PLAY,
4230
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
package com.google.firebase.appdistribution;
1616

1717
import androidx.annotation.NonNull;
18+
import androidx.annotation.Nullable;
1819
import com.google.android.gms.tasks.Task;
20+
import java.util.concurrent.Executor;
1921

20-
public abstract class UpdateTask extends Task<UpdateState> {
22+
public abstract class UpdateTask extends Task<Void> {
2123

2224
/**
2325
* Adds a listener that is called periodically while the UpdateTask executes.
@@ -26,4 +28,8 @@ public abstract class UpdateTask extends Task<UpdateState> {
2628
*/
2729
@NonNull
2830
public abstract UpdateTask addOnProgressListener(@NonNull OnProgressListener listener);
31+
32+
@NonNull
33+
public abstract UpdateTask addOnProgressListener(
34+
@Nullable Executor executor, @NonNull OnProgressListener listener);
2935
}

0 commit comments

Comments
 (0)