23
23
import android .content .Context ;
24
24
import androidx .annotation .GuardedBy ;
25
25
import androidx .annotation .NonNull ;
26
+ import androidx .annotation .Nullable ;
26
27
import androidx .annotation .VisibleForTesting ;
27
28
import com .google .android .gms .common .internal .Preconditions ;
28
29
import com .google .android .gms .tasks .Task ;
35
36
import com .google .firebase .installations .FirebaseInstallationsApi ;
36
37
37
38
public class FirebaseAppDistribution {
39
+ private static final int UNKNOWN_RELEASE_FILE_SIZE = -1 ;
38
40
39
41
private final FirebaseApp firebaseApp ;
40
42
private final TesterSignInClient testerSignInClient ;
41
43
private final CheckForNewReleaseClient checkForNewReleaseClient ;
42
44
private final UpdateAppClient updateAppClient ;
43
45
private final FirebaseAppDistributionLifecycleNotifier lifecycleNotifier ;
44
- private static final int UNKNOWN_RELEASE_FILE_SIZE = - 1 ;
46
+ private final SignInStorage signInStorage ;
45
47
46
- @ GuardedBy ("updateTaskLock" )
48
+ private final Object updateIfNewReleaseTaskLock = new Object ();
49
+
50
+ @ GuardedBy ("updateIfNewReleaseTaskLock" )
47
51
private UpdateTaskImpl cachedUpdateIfNewReleaseTask ;
48
52
49
- private final Object updateTaskLock = new Object ();
50
- private Task <AppDistributionRelease > cachedCheckForNewReleaseTask ;
53
+ private final Object cachedNewReleaseLock = new Object ();
51
54
55
+ @ GuardedBy ("cachedNewReleaseLock" )
52
56
private AppDistributionReleaseInternal cachedNewRelease ;
57
+
58
+ private Task <AppDistributionRelease > cachedCheckForNewReleaseTask ;
53
59
private AlertDialog updateDialog ;
54
60
private boolean updateDialogShown ;
55
- private final SignInStorage signInStorage ;
56
61
57
62
/** Constructor for FirebaseAppDistribution */
58
63
@ VisibleForTesting
@@ -126,8 +131,8 @@ public static FirebaseAppDistribution getInstance(@NonNull FirebaseApp app) {
126
131
* to complete the download and installation.
127
132
*/
128
133
@ NonNull
129
- public synchronized UpdateTask updateIfNewReleaseAvailable () {
130
- synchronized (updateTaskLock ) {
134
+ public UpdateTask updateIfNewReleaseAvailable () {
135
+ synchronized (updateIfNewReleaseTaskLock ) {
131
136
if (cachedUpdateIfNewReleaseTask != null && !cachedUpdateIfNewReleaseTask .isComplete ()) {
132
137
return cachedUpdateIfNewReleaseTask ;
133
138
}
@@ -163,7 +168,8 @@ public synchronized UpdateTask updateIfNewReleaseAvailable() {
163
168
Constants .ErrorMessages .NETWORK_ERROR ,
164
169
FirebaseAppDistributionException .Status .NETWORK_FAILURE ));
165
170
});
166
- synchronized (updateTaskLock ) {
171
+
172
+ synchronized (updateIfNewReleaseTaskLock ) {
167
173
return cachedUpdateIfNewReleaseTask ;
168
174
}
169
175
}
@@ -218,15 +224,15 @@ public synchronized Task<AppDistributionRelease> checkForNewRelease() {
218
224
* new release is cached from checkForNewRelease
219
225
*/
220
226
@ NonNull
221
- public synchronized UpdateTask updateApp () {
227
+ public UpdateTask updateApp () {
222
228
return updateApp (false );
223
229
}
224
230
225
231
/**
226
232
* Overloaded updateApp with boolean input showDownloadInNotificationsManager. Set to true for
227
233
* basic configuration and false for advanced configuration.
228
234
*/
229
- private synchronized UpdateTask updateApp (boolean showDownloadInNotificationManager ) {
235
+ private UpdateTask updateApp (boolean showDownloadInNotificationManager ) {
230
236
if (!isTesterSignedIn ()) {
231
237
UpdateTaskImpl updateTask = new UpdateTaskImpl ();
232
238
updateTask .setException (
@@ -235,7 +241,9 @@ private synchronized UpdateTask updateApp(boolean showDownloadInNotificationMana
235
241
return updateTask ;
236
242
}
237
243
238
- return this .updateAppClient .updateApp (cachedNewRelease , showDownloadInNotificationManager );
244
+ synchronized (cachedNewReleaseLock ) {
245
+ return this .updateAppClient .updateApp (cachedNewRelease , showDownloadInNotificationManager );
246
+ }
239
247
}
240
248
241
249
/** Returns true if the App Distribution tester is signed in */
@@ -245,7 +253,7 @@ public boolean isTesterSignedIn() {
245
253
246
254
/** Signs out the App Distribution tester */
247
255
public void signOutTester () {
248
- this . cachedNewRelease = null ;
256
+ setCachedNewRelease ( null ) ;
249
257
this .signInStorage .setSignInStatus (false );
250
258
}
251
259
@@ -263,13 +271,17 @@ void onActivityDestroyed(@NonNull Activity activity) {
263
271
}
264
272
265
273
@ VisibleForTesting
266
- void setCachedNewRelease (AppDistributionReleaseInternal newRelease ) {
267
- this .cachedNewRelease = newRelease ;
274
+ void setCachedNewRelease (@ Nullable AppDistributionReleaseInternal newRelease ) {
275
+ synchronized (cachedNewReleaseLock ) {
276
+ this .cachedNewRelease = newRelease ;
277
+ }
268
278
}
269
279
270
280
@ VisibleForTesting
271
281
AppDistributionReleaseInternal getCachedNewRelease () {
272
- return this .cachedNewRelease ;
282
+ synchronized (cachedNewReleaseLock ) {
283
+ return this .cachedNewRelease ;
284
+ }
273
285
}
274
286
275
287
private UpdateTaskImpl showUpdateAlertDialog (AppDistributionRelease newRelease ) {
@@ -302,7 +314,7 @@ private UpdateTaskImpl showUpdateAlertDialog(AppDistributionRelease newRelease)
302
314
AlertDialog .BUTTON_POSITIVE ,
303
315
context .getString (R .string .update_yes_button ),
304
316
(dialogInterface , i ) -> {
305
- synchronized (updateTaskLock ) {
317
+ synchronized (updateIfNewReleaseTaskLock ) {
306
318
// show download progress in notification manager
307
319
updateApp (true )
308
320
.addOnProgressListener (this ::postProgressToCachedUpdateIfNewReleaseTask )
@@ -316,7 +328,7 @@ private UpdateTaskImpl showUpdateAlertDialog(AppDistributionRelease newRelease)
316
328
context .getString (R .string .update_no_button ),
317
329
(dialogInterface , i ) -> {
318
330
dialogInterface .dismiss ();
319
- synchronized (updateTaskLock ) {
331
+ synchronized (updateIfNewReleaseTaskLock ) {
320
332
postProgressToCachedUpdateIfNewReleaseTask (
321
333
UpdateProgress .builder ()
322
334
.setApkFileTotalBytes (UNKNOWN_RELEASE_FILE_SIZE )
@@ -331,16 +343,16 @@ private UpdateTaskImpl showUpdateAlertDialog(AppDistributionRelease newRelease)
331
343
332
344
updateDialog .show ();
333
345
updateDialogShown = true ;
334
- synchronized (updateTaskLock ) {
346
+ synchronized (updateIfNewReleaseTaskLock ) {
335
347
return cachedUpdateIfNewReleaseTask ;
336
348
}
337
349
}
338
350
339
351
private void setCachedUpdateIfNewReleaseCompletionError (FirebaseAppDistributionException e ) {
340
- synchronized (updateTaskLock ) {
352
+ synchronized (updateIfNewReleaseTaskLock ) {
341
353
safeSetTaskException (cachedUpdateIfNewReleaseTask , e );
342
- dismissUpdateDialog ();
343
354
}
355
+ dismissUpdateDialog ();
344
356
}
345
357
346
358
private void setCachedUpdateIfNewReleaseCompletionError (
@@ -353,16 +365,16 @@ private void setCachedUpdateIfNewReleaseCompletionError(
353
365
}
354
366
355
367
private void postProgressToCachedUpdateIfNewReleaseTask (UpdateProgress progress ) {
356
- synchronized (updateTaskLock ) {
368
+ synchronized (updateIfNewReleaseTaskLock ) {
357
369
cachedUpdateIfNewReleaseTask .updateProgress (progress );
358
370
}
359
371
}
360
372
361
373
private void setCachedUpdateIfNewReleaseResult () {
362
- synchronized (updateTaskLock ) {
374
+ synchronized (updateIfNewReleaseTaskLock ) {
363
375
safeSetTaskResult (cachedUpdateIfNewReleaseTask );
364
- dismissUpdateDialog ();
365
376
}
377
+ dismissUpdateDialog ();
366
378
}
367
379
368
380
private void dismissUpdateDialog () {
0 commit comments