Repository: thundernest/k-9
Issue: Message inadvertently sent twice

When sending emails through K-9 on a Xiaomi Mi Mix2, occasionally the same email is sent twice. This happens both on Wi-Fi and mobile data connections. It is unclear what triggers the issue, but it appears specific to K-9 running on a Xiaomi Mi Mix2, so either the application or the phone, or the combination of the two, may be a cause. The problem does not occur when sending emails through Xiaomi's native email client using the same account, or when using K-9 with the same account on a Xiaomi Mi4 and Xiaomi Mi Max2.

### Steps to reproduce
1. Send email from within K-9.
2. Check "Sent" folder of account where the message is sent from.
3. Check inbox of recipient account.

### Expected behaviour
One email is sent. The "Sent" folder should contain one copy of the message. The recipient should have received one copy of the message.

### Actual behaviour
When the issue occurs, two emails are sent. The "Sent" folder shows two messages with identical subject and body text but with different IDs and different time stamps. The recipient has received the same message twice.

### Environment
K-9 Mail version:

K-9 Mail Version 5.600
Installed via F-Droid
Note: K-9 is given full permissions and battery management is set to "unrestricted".

Android version:

Xiaomi Mi Mix2 (Chinese version)
MIUI 10.2.2.0
Android 8.0.0
Security patch level 2018-12-01
Kernel version 4.4.78-perf-g7ac6a25

Account type:

IMAP account at [FastMail](https://www.fastmail.com/)

### Log
A log illustrating the issue is attached. The problem occurs at timestamp "03-02 11:23" (two emails are sent), but it does not occur at "03-02 11:34" (only one email is sent).

[k9-log-subset.txt](https://github.com/k9mail/k-9/files/2921755/k9-log-subset.txt)


Changed files:
app/ui/legacy/src/main/java/com/fsck/k9/activity/MessageCompose.java

diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/MessageCompose.java b/app/ui/legacy/src/main/java/com/fsck/k9/activity/MessageCompose.java
index 81b55765a..925edd1ed 100644
--- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/MessageCompose.java
+++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/MessageCompose.java
@@ -241,6 +241,8 @@ public class MessageCompose extends K9Activity implements OnClickListener,
 
     private boolean navigateUp;
 
+    private boolean sendMessageHasBeenTriggered = false;
+
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -797,6 +799,12 @@ public class MessageCompose extends K9Activity implements OnClickListener,
     }
 
     public void performSendAfterChecks() {
+        if (sendMessageHasBeenTriggered) {
+            return;
+        }
+
+        sendMessageHasBeenTriggered = true;
+
         currentMessageBuilder = createMessageBuilder(false);
         if (currentMessageBuilder != null) {
             changesMadeSinceLastSave = false;
@@ -1607,6 +1615,7 @@ public class MessageCompose extends K9Activity implements OnClickListener,
 
     @Override
     public void onMessageBuildCancel() {
+        sendMessageHasBeenTriggered = false;
         currentMessageBuilder = null;
         setProgressBarIndeterminateVisibility(false);
     }
@@ -1616,6 +1625,7 @@ public class MessageCompose extends K9Activity implements OnClickListener,
         Timber.e(me, "Error sending message");
         Toast.makeText(MessageCompose.this,
                 getString(R.string.send_failed_reason, me.getLocalizedMessage()), Toast.LENGTH_LONG).show();
+        sendMessageHasBeenTriggered = false;
         currentMessageBuilder = null;
         setProgressBarIndeterminateVisibility(false);
     }