mirror of
https://github.com/bitwarden/mobile
synced 2026-01-08 11:33:31 +00:00
Fix Progress dialog crash on tombstoning (#1682)
* Changed ProgressDialog because deprecated and improved the dismissal of the dialog in order for it not to crash the app on certain situations * Removed android version check given that our minimum is greater that the check
This commit is contained in:
committed by
GitHub
parent
137c762e40
commit
5a6aec51f3
@@ -13,6 +13,7 @@ using Android.OS;
|
||||
using Android.Provider;
|
||||
using Android.Text;
|
||||
using Android.Text.Method;
|
||||
using Android.Views;
|
||||
using Android.Views.Autofill;
|
||||
using Android.Views.InputMethods;
|
||||
using Android.Webkit;
|
||||
@@ -27,6 +28,7 @@ using Bit.Core.Enums;
|
||||
using Bit.Core.Models.View;
|
||||
using Bit.Core.Utilities;
|
||||
using Bit.Droid.Autofill;
|
||||
using Bit.Droid.Utilities;
|
||||
using Plugin.CurrentActivity;
|
||||
|
||||
namespace Bit.Droid.Services
|
||||
@@ -37,7 +39,9 @@ namespace Bit.Droid.Services
|
||||
private readonly IMessagingService _messagingService;
|
||||
private readonly IBroadcasterService _broadcasterService;
|
||||
private readonly Func<IEventService> _eventServiceFunc;
|
||||
private ProgressDialog _progressDialog;
|
||||
private AlertDialog _progressDialog;
|
||||
object _progressDialogLock = new object();
|
||||
|
||||
private bool _cameraPermissionsDenied;
|
||||
private Toast _toast;
|
||||
private string _userAgent;
|
||||
@@ -108,22 +112,101 @@ namespace Bit.Droid.Services
|
||||
{
|
||||
await HideLoadingAsync();
|
||||
}
|
||||
var activity = (MainActivity)CrossCurrentActivity.Current.Activity;
|
||||
_progressDialog = new ProgressDialog(activity);
|
||||
_progressDialog.SetMessage(text);
|
||||
_progressDialog.SetCancelable(false);
|
||||
|
||||
var activity = CrossCurrentActivity.Current.Activity;
|
||||
var inflater = (LayoutInflater)activity.GetSystemService(Context.LayoutInflaterService);
|
||||
var dialogView = inflater.Inflate(Resource.Layout.progress_dialog_layout, null);
|
||||
|
||||
var txtLoading = dialogView.FindViewById<TextView>(Resource.Id.txtLoading);
|
||||
txtLoading.Text = text;
|
||||
txtLoading.SetTextColor(ThemeHelpers.TextColor);
|
||||
|
||||
_progressDialog = new AlertDialog.Builder(activity)
|
||||
.SetView(dialogView)
|
||||
.SetCancelable(false)
|
||||
.Create();
|
||||
_progressDialog.Show();
|
||||
}
|
||||
|
||||
public Task HideLoadingAsync()
|
||||
{
|
||||
if (_progressDialog != null)
|
||||
// Based on https://github.com/redth-org/AndHUD/blob/master/AndHUD/AndHUD.cs
|
||||
lock (_progressDialogLock)
|
||||
{
|
||||
_progressDialog.Dismiss();
|
||||
_progressDialog.Dispose();
|
||||
_progressDialog = null;
|
||||
if (_progressDialog is null)
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
void actionDismiss()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (IsAlive(_progressDialog) && IsAlive(_progressDialog.Window))
|
||||
{
|
||||
_progressDialog.Hide();
|
||||
_progressDialog.Dismiss();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignore
|
||||
}
|
||||
|
||||
_progressDialog = null;
|
||||
}
|
||||
|
||||
// First try the SynchronizationContext
|
||||
if (Application.SynchronizationContext != null)
|
||||
{
|
||||
Application.SynchronizationContext.Send(state => actionDismiss(), null);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
// Otherwise try OwnerActivity on dialog
|
||||
var ownerActivity = _progressDialog?.OwnerActivity;
|
||||
if (IsAlive(ownerActivity))
|
||||
{
|
||||
ownerActivity.RunOnUiThread(actionDismiss);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
// Otherwise try get it from the Window Context
|
||||
if (_progressDialog?.Window?.Context is Activity windowActivity && IsAlive(windowActivity))
|
||||
{
|
||||
windowActivity.RunOnUiThread(actionDismiss);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
// Finally if all else fails, let's see if current activity is MainActivity
|
||||
if (CrossCurrentActivity.Current.Activity is MainActivity activity && IsAlive(activity))
|
||||
{
|
||||
activity.RunOnUiThread(actionDismiss);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
bool IsAlive(Java.Lang.Object @object)
|
||||
{
|
||||
if (@object == null)
|
||||
return false;
|
||||
|
||||
if (@object.Handle == IntPtr.Zero)
|
||||
return false;
|
||||
|
||||
if (@object is Activity activity)
|
||||
{
|
||||
if (activity.IsFinishing)
|
||||
return false;
|
||||
|
||||
if (activity.IsDestroyed)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool OpenFile(byte[] fileData, string id, string fileName)
|
||||
|
||||
Reference in New Issue
Block a user