From 2e0ec41a370f80091c818bb5b7eb83b92f9ee0ca Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Sat, 25 Mar 2023 00:41:50 +0100 Subject: [PATCH] fix leaving Delta Chat during backup transfer this fixes leaving Delta Chat during backup transfer by removing other activities that would abort BackupTransfer by getting called with onNewIntent() otherwise. moreover, the permantent notification is removed more reliably and possible crashes in the permantent notification closing (races ...) are catched. see code + comments for details. --- .../ApplicationPreferencesActivity.java | 2 ++ .../securesms/qr/BackupTransferActivity.java | 27 ++++++++++++++++--- .../service/NotificationController.java | 8 ++++-- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/org/thoughtcrime/securesms/ApplicationPreferencesActivity.java b/src/org/thoughtcrime/securesms/ApplicationPreferencesActivity.java index 8ee8c274f..e2be59d37 100644 --- a/src/org/thoughtcrime/securesms/ApplicationPreferencesActivity.java +++ b/src/org/thoughtcrime/securesms/ApplicationPreferencesActivity.java @@ -144,6 +144,8 @@ public class ApplicationPreferencesActivity extends PassphraseRequiredActionBarA Intent intent = new Intent(this, BackupTransferActivity.class); intent.putExtra(BackupTransferActivity.TRANSFER_MODE, BackupTransferActivity.TransferMode.SENDER_SHOW_QR.getInt()); startActivity(intent); + overridePendingTransition(0, 0); // let the activity appear in the same way as the other pages (which are mostly fragments) + finishAffinity(); // see comment (**2) in BackupTransferActivity.doFinish() } public static class ApplicationPreferenceFragment extends CorrectedPreferenceFragment implements DcEventCenter.DcEventDelegate { diff --git a/src/org/thoughtcrime/securesms/qr/BackupTransferActivity.java b/src/org/thoughtcrime/securesms/qr/BackupTransferActivity.java index 7ebceb223..0db6199ca 100644 --- a/src/org/thoughtcrime/securesms/qr/BackupTransferActivity.java +++ b/src/org/thoughtcrime/securesms/qr/BackupTransferActivity.java @@ -14,6 +14,7 @@ import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AlertDialog; +import org.thoughtcrime.securesms.ApplicationPreferencesActivity; import org.thoughtcrime.securesms.BaseActionBarActivity; import org.thoughtcrime.securesms.ConversationListActivity; import org.thoughtcrime.securesms.LogViewActivity; @@ -56,6 +57,7 @@ public class BackupTransferActivity extends BaseActionBarActivity { private final DynamicLanguage dynamicLanguage = new DynamicLanguage(); NotificationController notificationController; + private boolean notificationControllerClosed = false; public boolean warnAboutCopiedQrCodeOnAbort = false; @Override @@ -93,12 +95,12 @@ public class BackupTransferActivity extends BaseActionBarActivity { } @Override - protected void onPause() { - super.onPause(); - if (isFinishing()) { + protected void onDestroy() { + super.onDestroy(); + if (!notificationControllerClosed) { notificationController.close(); - DcHelper.getAccounts(this).startIo(); } + DcHelper.getAccounts(this).startIo(); } @Override @@ -166,8 +168,25 @@ public class BackupTransferActivity extends BaseActionBarActivity { } public void doFinish() { + // the permanent notification will prevent other activities to be started and kill BackupTransferActivity; + // close it before starting other activities + notificationController.close(); + notificationControllerClosed = true; + if (transferMode == TransferMode.RECEIVER_SCAN_QR && transferState == TransferState.TRANSFER_SUCCESS) { startActivity(new Intent(getApplicationContext(), ConversationListActivity.class)); + } else if (transferMode == TransferMode.SENDER_SHOW_QR) { + // restart the activities that were removed when BackupTransferActivity was started at (**2) + // (we removed the activity backstack as otherwise a tap on the Delta Chat icon on the home screen would + // call onNewIntent() which cannot be aborted and will kill BackupTransferActivity. + // if all activities are removed, onCreate() will be called and that can be aborted, so that + // a tap in the home icon just opens BackupTransferActivity. + // (the user can leave Delta Chat during backup transfer :) + // a proper fix would maybe to not rely onNewIntent() at all - but that would require more refactorings + // and needs lots if testing in complicated areas (share ...)) + startActivity(new Intent(getApplicationContext(), ConversationListActivity.class)); + startActivity(new Intent(this, ApplicationPreferencesActivity.class)); + overridePendingTransition(0, 0); } finish(); } diff --git a/src/org/thoughtcrime/securesms/service/NotificationController.java b/src/org/thoughtcrime/securesms/service/NotificationController.java index bf797f65f..b45c8fc49 100644 --- a/src/org/thoughtcrime/securesms/service/NotificationController.java +++ b/src/org/thoughtcrime/securesms/service/NotificationController.java @@ -58,8 +58,12 @@ public final class NotificationController { } public void close() { - GenericForegroundService.stopForegroundTask(context, id); - context.unbindService(serviceConnection); + try { + GenericForegroundService.stopForegroundTask(context, id); + context.unbindService(serviceConnection); + } catch(Exception e) { + e.printStackTrace(); + } } public void setIndeterminateProgress() {