make sure, only one error alert is shown (#2526)

* make sure, only one error alert is shown

DcBackupProvider.unref() will soon also emit errors,
see https://github.com/deltachat/deltachat-core-rust/pull/4242 ,
but also before, this was only correct by coincidence

* do not handle events on shutdown

DcBackupProvider.unref() may emit events,
but that may happen also for other reasons.

be explicit by a flag,
relying on activity.isFinishing() or by the already removed observer seems
error prone, esp. on future changes. do not make me think :)
This commit is contained in:
bjoern
2023-04-04 10:28:32 +02:00
committed by GitHub
parent 661d54cd03
commit 08265bf0f9
3 changed files with 33 additions and 27 deletions
@@ -40,6 +40,7 @@ public class BackupProviderFragment extends Fragment implements DcEventCenter.Dc
private View topText;
private SVGImageView qrImageView;
private View bottomText;
private boolean isFinishing;
@Override
public void onCreate(Bundle bundle) {
@@ -74,8 +75,7 @@ public class BackupProviderFragment extends Fragment implements DcEventCenter.Dc
}
progressBar.setVisibility(View.GONE);
if (!dcBackupProvider.isOk()) {
activity.setTransferState(BackupTransferActivity.TransferState.TRANSFER_ERROR);
activity.showLastErrorAlert("Cannot create backup provider");
activity.setTransferError("Cannot create backup provider");
return;
}
statusLine.setVisibility(View.GONE);
@@ -102,12 +102,13 @@ public class BackupProviderFragment extends Fragment implements DcEventCenter.Dc
@Override
public void onDestroyView() {
isFinishing = true;
DcHelper.getEventCenter(getActivity()).removeObservers(this);
dcContext.stopOngoingProcess();
if (dcBackupProvider != null) {
dcBackupProvider.unref();
}
super.onDestroyView();
DcHelper.getEventCenter(getActivity()).removeObservers(this);
}
@Override
@@ -136,6 +137,10 @@ public class BackupProviderFragment extends Fragment implements DcEventCenter.Dc
@Override
public void handleEvent(@NonNull DcEvent event) {
if (event.getId() == DcContext.DC_EVENT_IMEX_PROGRESS) {
if (isFinishing) {
return;
}
int permille = event.getData1Int();
int percent = 0;
int percentMax = 0;
@@ -144,8 +149,7 @@ public class BackupProviderFragment extends Fragment implements DcEventCenter.Dc
Log.i(TAG,"DC_EVENT_IMEX_PROGRESS, " + permille);
if (permille == 0) {
getTransferActivity().setTransferState(BackupTransferActivity.TransferState.TRANSFER_ERROR);
getTransferActivity().showLastErrorAlert("Sending Error");
getTransferActivity().setTransferError("Sending Error");
hideQrCode = true;
} else if(permille <= 350) {
statusLineText = getString(R.string.preparing_account);
@@ -80,8 +80,7 @@ public class BackupReceiverFragment extends Fragment implements DcEventCenter.Dc
Log.i(TAG,"DC_EVENT_IMEX_PROGRESS, " + permille);
if (permille == 0) {
getTransferActivity().setTransferState(BackupTransferActivity.TransferState.TRANSFER_ERROR);
getTransferActivity().showLastErrorAlert("Receiving Error");
getTransferActivity().setTransferError("Receiving Error");
} else if (permille <= 100) {
statusLineText = getString(R.string.preparing_account);
hideSameNetworkHint = true;
@@ -15,7 +15,6 @@ import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import org.thoughtcrime.securesms.ApplicationContext;
import org.thoughtcrime.securesms.ApplicationPreferencesActivity;
import org.thoughtcrime.securesms.BaseActionBarActivity;
import org.thoughtcrime.securesms.ConversationListActivity;
@@ -148,6 +147,29 @@ public class BackupTransferActivity extends BaseActionBarActivity {
this.transferState = transferState;
}
public void setTransferError(@NonNull String errorContext) {
if (this.transferState != TransferState.TRANSFER_ERROR) {
this.transferState = TransferState.TRANSFER_ERROR;
String lastError = DcHelper.getContext(this).getLastError();
if (lastError.isEmpty()) {
lastError = "<last error not set>";
}
String error = errorContext;
if (!error.isEmpty()) {
error += ": ";
}
error += lastError;
new AlertDialog.Builder(this)
.setMessage(error)
.setPositiveButton(android.R.string.ok, null)
.setCancelable(false)
.show();
}
}
private void finishOrAskToFinish() {
switch (transferState) {
case TRANSFER_ERROR:
@@ -193,25 +215,6 @@ public class BackupTransferActivity extends BaseActionBarActivity {
finish();
}
public void showLastErrorAlert(@NonNull String errorContext) {
String lastError = DcHelper.getContext(this).getLastError();
if (lastError.isEmpty()) {
lastError = "<last error not set>";
}
String error = errorContext;
if (!error.isEmpty()) {
error += ": ";
}
error += lastError;
new AlertDialog.Builder(this)
.setMessage(error)
.setPositiveButton(android.R.string.ok, null)
.setCancelable(false)
.show();
}
public static void appendSSID(Activity activity, final TextView textView) {
if (textView != null && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
new Thread(() -> {