@@ -41,35 +41,40 @@ class ApkUpdater {
41
41
private static final int UPDATE_INTERVAL_MS = 250 ;
42
42
private static final String TAG = "UpdateApkClient:" ;
43
43
private static final String REQUEST_METHOD = "GET" ;
44
- private final FirebaseAppDistributionNotificationsManager appDistributionNotificationsManager ;
45
44
46
45
private TaskCompletionSource <File > downloadTaskCompletionSource ;
47
46
private final Executor taskExecutor ; // Executor to run task listeners on a background thread
48
47
private final FirebaseApp firebaseApp ;
49
48
private final ApkInstaller apkInstaller ;
49
+ private final FirebaseAppDistributionNotificationsManager appDistributionNotificationsManager ;
50
50
51
51
@ GuardedBy ("updateTaskLock" )
52
52
private UpdateTaskImpl cachedUpdateTask ;
53
53
54
54
private final Object updateTaskLock = new Object ();
55
55
56
56
public ApkUpdater (@ NonNull FirebaseApp firebaseApp , @ NonNull ApkInstaller apkInstaller ) {
57
- this (Executors .newSingleThreadExecutor (), firebaseApp , apkInstaller );
57
+ this (
58
+ Executors .newSingleThreadExecutor (),
59
+ firebaseApp ,
60
+ apkInstaller ,
61
+ new FirebaseAppDistributionNotificationsManager (firebaseApp .getApplicationContext ()));
58
62
}
59
63
64
+ @ VisibleForTesting
60
65
public ApkUpdater (
61
66
@ NonNull Executor taskExecutor ,
62
67
@ NonNull FirebaseApp firebaseApp ,
63
- @ NonNull ApkInstaller apkInstaller ) {
64
- this .appDistributionNotificationsManager =
65
- new FirebaseAppDistributionNotificationsManager (firebaseApp );
68
+ @ NonNull ApkInstaller apkInstaller ,
69
+ @ NonNull FirebaseAppDistributionNotificationsManager appDistributionNotificationsManager ) {
66
70
this .taskExecutor = taskExecutor ;
67
71
this .firebaseApp = firebaseApp ;
68
72
this .apkInstaller = apkInstaller ;
73
+ this .appDistributionNotificationsManager = appDistributionNotificationsManager ;
69
74
}
70
75
71
76
UpdateTaskImpl updateApk (
72
- @ NonNull AppDistributionReleaseInternal newRelease , boolean showDownloadNotificationManager ) {
77
+ @ NonNull AppDistributionReleaseInternal newRelease , boolean showNotification ) {
73
78
synchronized (updateTaskLock ) {
74
79
if (cachedUpdateTask != null && !cachedUpdateTask .isComplete ()) {
75
80
return cachedUpdateTask ;
@@ -78,7 +83,7 @@ UpdateTaskImpl updateApk(
78
83
cachedUpdateTask = new UpdateTaskImpl ();
79
84
}
80
85
81
- downloadApk (newRelease , showDownloadNotificationManager )
86
+ downloadApk (newRelease , showNotification )
82
87
// Using onSuccess task to ensure that all install errors get cascaded to the Failure
83
88
// listener down below
84
89
.addOnSuccessListener (
@@ -94,7 +99,7 @@ UpdateTaskImpl updateApk(
94
99
file .length (),
95
100
file .length (),
96
101
UpdateStatus .INSTALL_FAILED ,
97
- showDownloadNotificationManager );
102
+ showNotification );
98
103
setUpdateTaskCompletionErrorWithDefault (
99
104
e ,
100
105
new FirebaseAppDistributionException (
@@ -128,20 +133,20 @@ UpdateTaskImpl updateApk(
128
133
@ VisibleForTesting
129
134
@ NonNull
130
135
Task <File > downloadApk (
131
- @ NonNull AppDistributionReleaseInternal newRelease , boolean showDownloadNotificationManager ) {
136
+ @ NonNull AppDistributionReleaseInternal newRelease , boolean showNotification ) {
132
137
if (downloadTaskCompletionSource != null
133
138
&& !downloadTaskCompletionSource .getTask ().isComplete ()) {
134
139
return downloadTaskCompletionSource .getTask ();
135
140
}
136
141
137
142
downloadTaskCompletionSource = new TaskCompletionSource <>();
138
143
139
- makeApkDownloadRequest (newRelease , showDownloadNotificationManager );
144
+ makeApkDownloadRequest (newRelease , showNotification );
140
145
return downloadTaskCompletionSource .getTask ();
141
146
}
142
147
143
148
private void makeApkDownloadRequest (
144
- @ NonNull AppDistributionReleaseInternal newRelease , boolean showDownloadNotificationManager ) {
149
+ @ NonNull AppDistributionReleaseInternal newRelease , boolean showNotification ) {
145
150
taskExecutor .execute (
146
151
() -> {
147
152
try {
@@ -154,16 +159,12 @@ private void makeApkDownloadRequest(
154
159
FirebaseAppDistributionException .Status .DOWNLOAD_FAILURE ));
155
160
} else {
156
161
long responseLength = connection .getContentLength ();
157
- postUpdateProgress (
158
- responseLength , 0 , UpdateStatus .PENDING , showDownloadNotificationManager );
162
+ postUpdateProgress (responseLength , 0 , UpdateStatus .PENDING , showNotification );
159
163
String fileName = getApplicationName () + ".apk" ;
160
164
LogWrapper .getInstance ().v (TAG + "Attempting to download to disk" );
161
165
162
166
downloadToDisk (
163
- connection .getInputStream (),
164
- responseLength ,
165
- fileName ,
166
- showDownloadNotificationManager );
167
+ connection .getInputStream (), responseLength , fileName , showNotification );
167
168
}
168
169
} catch (IOException | FirebaseAppDistributionException e ) {
169
170
setDownloadTaskCompletionErrorWithDefault (
@@ -176,7 +177,7 @@ private void makeApkDownloadRequest(
176
177
}
177
178
178
179
private void downloadToDisk (
179
- InputStream input , long totalSize , String fileName , boolean showDownloadNotificationManager ) {
180
+ InputStream input , long totalSize , String fileName , boolean showNotification ) {
180
181
181
182
File apkFile = getApkFileForApp (fileName );
182
183
apkFile .delete ();
@@ -205,19 +206,13 @@ private void downloadToDisk(
205
206
if (currentTimeMs - lastMsUpdated > UPDATE_INTERVAL_MS ) {
206
207
lastMsUpdated = currentTimeMs ;
207
208
postUpdateProgress (
208
- totalSize ,
209
- bytesDownloaded ,
210
- UpdateStatus .DOWNLOADING ,
211
- showDownloadNotificationManager );
209
+ totalSize , bytesDownloaded , UpdateStatus .DOWNLOADING , showNotification );
212
210
}
213
211
}
214
212
215
213
} catch (IOException e ) {
216
214
postUpdateProgress (
217
- totalSize ,
218
- bytesDownloaded ,
219
- UpdateStatus .DOWNLOAD_FAILED ,
220
- showDownloadNotificationManager );
215
+ totalSize , bytesDownloaded , UpdateStatus .DOWNLOAD_FAILED , showNotification );
221
216
setDownloadTaskCompletionError (
222
217
new FirebaseAppDistributionException (
223
218
Constants .ErrorMessages .NETWORK_ERROR ,
@@ -230,19 +225,15 @@ private void downloadToDisk(
230
225
231
226
} catch (Exception e ) {
232
227
postUpdateProgress (
233
- totalSize ,
234
- bytesDownloaded ,
235
- UpdateStatus .DOWNLOAD_FAILED ,
236
- showDownloadNotificationManager );
228
+ totalSize , bytesDownloaded , UpdateStatus .DOWNLOAD_FAILED , showNotification );
237
229
setDownloadTaskCompletionError (
238
230
new FirebaseAppDistributionException (
239
231
Constants .ErrorMessages .NETWORK_ERROR ,
240
232
FirebaseAppDistributionException .Status .DOWNLOAD_FAILURE ));
241
233
}
242
234
243
235
// completion
244
- postUpdateProgress (
245
- totalSize , totalSize , UpdateStatus .DOWNLOADED , showDownloadNotificationManager );
236
+ postUpdateProgress (totalSize , totalSize , UpdateStatus .DOWNLOADED , showNotification );
246
237
247
238
safeSetTaskResult (downloadTaskCompletionSource , apkFile );
248
239
}
@@ -304,12 +295,8 @@ private void setUpdateTaskCompletionErrorWithDefault(
304
295
}
305
296
}
306
297
307
- @ VisibleForTesting
308
- void postUpdateProgress (
309
- long totalBytes ,
310
- long downloadedBytes ,
311
- UpdateStatus status ,
312
- boolean showDownloadNotificationManager ) {
298
+ private void postUpdateProgress (
299
+ long totalBytes , long downloadedBytes , UpdateStatus status , boolean showNotification ) {
313
300
synchronized (updateTaskLock ) {
314
301
cachedUpdateTask .updateProgress (
315
302
UpdateProgress .builder ()
@@ -318,7 +305,7 @@ void postUpdateProgress(
318
305
.setUpdateStatus (status )
319
306
.build ());
320
307
}
321
- if (showDownloadNotificationManager ) {
308
+ if (showNotification ) {
322
309
appDistributionNotificationsManager .updateNotification (totalBytes , downloadedBytes , status );
323
310
}
324
311
}
0 commit comments