@@ -215,34 +215,40 @@ private Task<Void> showSignInConfirmationDialog(Activity hostActivity) {
215
215
showSignInDialogTask = new TaskCompletionSource <>();
216
216
}
217
217
218
- signInConfirmationDialog = new AlertDialog .Builder (hostActivity ).create ();
219
218
dialogHostActivity = hostActivity ;
220
219
221
- Context context = firebaseApp .getApplicationContext ();
222
- signInConfirmationDialog .setTitle (context .getString (R .string .signin_dialog_title ));
223
- signInConfirmationDialog .setMessage (context .getString (R .string .singin_dialog_message ));
224
-
225
- signInConfirmationDialog .setButton (
226
- AlertDialog .BUTTON_POSITIVE ,
227
- context .getString (R .string .singin_yes_button ),
228
- (dialogInterface , i ) -> showSignInDialogTask .setResult (null ));
229
-
230
- signInConfirmationDialog .setButton (
231
- AlertDialog .BUTTON_NEGATIVE ,
232
- context .getString (R .string .singin_no_button ),
233
- (dialogInterface , i ) ->
234
- showSignInDialogTask .setException (
235
- new FirebaseAppDistributionException (
236
- ErrorMessages .AUTHENTICATION_CANCELED , AUTHENTICATION_CANCELED )));
237
-
238
- signInConfirmationDialog .setOnCancelListener (
239
- dialogInterface ->
240
- showSignInDialogTask .setException (
241
- new FirebaseAppDistributionException (
242
- ErrorMessages .AUTHENTICATION_CANCELED , AUTHENTICATION_CANCELED )));
243
-
244
- signInConfirmationDialog .show ();
245
-
220
+ // We may not be on the main (UI) thread in some cases, specifically if the developer calls
221
+ // the basic config from the background. If we are already on the main thread, this will
222
+ // execute immediately.
223
+ hostActivity .runOnUiThread (
224
+ () -> {
225
+ signInConfirmationDialog = new AlertDialog .Builder (hostActivity ).create ();
226
+
227
+ Context context = firebaseApp .getApplicationContext ();
228
+ signInConfirmationDialog .setTitle (context .getString (R .string .signin_dialog_title ));
229
+ signInConfirmationDialog .setMessage (context .getString (R .string .singin_dialog_message ));
230
+
231
+ signInConfirmationDialog .setButton (
232
+ AlertDialog .BUTTON_POSITIVE ,
233
+ context .getString (R .string .singin_yes_button ),
234
+ (dialogInterface , i ) -> showSignInDialogTask .setResult (null ));
235
+
236
+ signInConfirmationDialog .setButton (
237
+ AlertDialog .BUTTON_NEGATIVE ,
238
+ context .getString (R .string .singin_no_button ),
239
+ (dialogInterface , i ) ->
240
+ showSignInDialogTask .setException (
241
+ new FirebaseAppDistributionException (
242
+ ErrorMessages .AUTHENTICATION_CANCELED , AUTHENTICATION_CANCELED )));
243
+
244
+ signInConfirmationDialog .setOnCancelListener (
245
+ dialogInterface ->
246
+ showSignInDialogTask .setException (
247
+ new FirebaseAppDistributionException (
248
+ ErrorMessages .AUTHENTICATION_CANCELED , AUTHENTICATION_CANCELED )));
249
+
250
+ signInConfirmationDialog .show ();
251
+ });
246
252
return showSignInDialogTask .getTask ();
247
253
}
248
254
@@ -420,42 +426,47 @@ private Task<Void> showUpdateConfirmationDialog(
420
426
}
421
427
422
428
Context context = firebaseApp .getApplicationContext ();
423
-
424
- updateConfirmationDialog = new AlertDialog .Builder (hostActivity ).create ();
425
429
dialogHostActivity = hostActivity ;
426
- updateConfirmationDialog .setTitle (context .getString (R .string .update_dialog_title ));
427
430
428
- StringBuilder message =
429
- new StringBuilder (
430
- String .format (
431
- "Version %s (%s) is available." ,
432
- newRelease .getDisplayVersion (), newRelease .getVersionCode ()));
433
-
434
- if (newRelease .getReleaseNotes () != null && !newRelease .getReleaseNotes ().isEmpty ()) {
435
- message .append (String .format ("\n \n Release notes: %s" , newRelease .getReleaseNotes ()));
436
- }
437
- updateConfirmationDialog .setMessage (message );
438
-
439
- updateConfirmationDialog .setButton (
440
- AlertDialog .BUTTON_POSITIVE ,
441
- context .getString (R .string .update_yes_button ),
442
- (dialogInterface , i ) -> showUpdateDialogTask .setResult (null ));
443
-
444
- updateConfirmationDialog .setButton (
445
- AlertDialog .BUTTON_NEGATIVE ,
446
- context .getString (R .string .update_no_button ),
447
- (dialogInterface , i ) ->
448
- showUpdateDialogTask .setException (
449
- new FirebaseAppDistributionException (
450
- ErrorMessages .UPDATE_CANCELED , Status .INSTALLATION_CANCELED )));
451
-
452
- updateConfirmationDialog .setOnCancelListener (
453
- dialogInterface ->
454
- showUpdateDialogTask .setException (
455
- new FirebaseAppDistributionException (
456
- ErrorMessages .UPDATE_CANCELED , Status .INSTALLATION_CANCELED )));
457
-
458
- updateConfirmationDialog .show ();
431
+ // We should already be on the main (UI) thread here, but be explicit just to be safe. If we are
432
+ // already on the main thread, this will execute immediately.
433
+ hostActivity .runOnUiThread (
434
+ () -> {
435
+ updateConfirmationDialog = new AlertDialog .Builder (hostActivity ).create ();
436
+ updateConfirmationDialog .setTitle (context .getString (R .string .update_dialog_title ));
437
+
438
+ StringBuilder message =
439
+ new StringBuilder (
440
+ String .format (
441
+ "Version %s (%s) is available." ,
442
+ newRelease .getDisplayVersion (), newRelease .getVersionCode ()));
443
+
444
+ if (newRelease .getReleaseNotes () != null && !newRelease .getReleaseNotes ().isEmpty ()) {
445
+ message .append (String .format ("\n \n Release notes: %s" , newRelease .getReleaseNotes ()));
446
+ }
447
+ updateConfirmationDialog .setMessage (message );
448
+
449
+ updateConfirmationDialog .setButton (
450
+ AlertDialog .BUTTON_POSITIVE ,
451
+ context .getString (R .string .update_yes_button ),
452
+ (dialogInterface , i ) -> showUpdateDialogTask .setResult (null ));
453
+
454
+ updateConfirmationDialog .setButton (
455
+ AlertDialog .BUTTON_NEGATIVE ,
456
+ context .getString (R .string .update_no_button ),
457
+ (dialogInterface , i ) ->
458
+ showUpdateDialogTask .setException (
459
+ new FirebaseAppDistributionException (
460
+ ErrorMessages .UPDATE_CANCELED , Status .INSTALLATION_CANCELED )));
461
+
462
+ updateConfirmationDialog .setOnCancelListener (
463
+ dialogInterface ->
464
+ showUpdateDialogTask .setException (
465
+ new FirebaseAppDistributionException (
466
+ ErrorMessages .UPDATE_CANCELED , Status .INSTALLATION_CANCELED )));
467
+
468
+ updateConfirmationDialog .show ();
469
+ });
459
470
460
471
return showUpdateDialogTask .getTask ();
461
472
}
0 commit comments