Repository: thunderbird/thunderbird-android (language: kotlin)

Issue:
Impossible to import settings
### Checklist

- [X] I have used the search function to see if someone else has already submitted the same bug report.
- [X] I will describe the problem with as much detail as possible.

### App version

5.913

### Where did you get the app from?

F-Droid

### Android version

11

### Device model

Xiaomi MI9

### Steps to reproduce

Parameters
Import parameters


### Expected behavior

Make it possible to import parameters

### Actual behavior

Hello,
i'm using K-9 mail 5.913 on Xiaomi MI9 (Android 11 - MIUI 12.5.1) and on a fresh reinstall i've got an error to import settings (all settings)
I've got this error message : "Echec de lecture du fichier de paramètres" (Failed to read parameter file)

### Logs

```
 Error reading settings file
 com.fsck.k9.preferences.SettingsImportExportException: org.xmlpull.v1.XmlPullParserException: Unexpected token (position:TEXT lue key="pushMod...@727:45 in java.io.InputStreamReader@e5a72f1) 
 	at com.fsck.k9.preferences.SettingsImporter.parseSettings(SettingsImporter.java:685)
 	at com.fsck.k9.preferences.SettingsImporter.getImportStreamContents(SettingsImporter.java:129)
 	at com.fsck.k9.ui.settings.import.SettingsImportViewModel.readSettings(SettingsImportViewModel.kt:357)
 	at com.fsck.k9.ui.settings.import.SettingsImportViewModel.access$readSettings(SettingsImportViewModel.kt:28)
 	at com.fsck.k9.ui.settings.import.SettingsImportViewModel$startReadSettingsFile$1$1$1.invokeSuspend(SettingsImportViewModel.kt:238)
 	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
 	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
 	at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:39)
 	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
 	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
 	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
 Caused by: org.xmlpull.v1.XmlPullParserException: Unexpected token (position:TEXT lue key="pushMod...@727:45 in java.io.InputStreamReader@e5a72f1) 
 	at com.android.org.kxml2.io.KXmlParser.next(KXmlParser.java:440)
 	at com.android.org.kxml2.io.KXmlParser.next(KXmlParser.java:321)
 	at com.fsck.k9.preferences.SettingsImporter.parseSettings(SettingsImporter.java:676)
 	... 12 more
```


Discussion hints:
I add here the part of my file concerned by the error (@727:45 )
I insist on the fact that I did not edit this file and that it is the result of the action "Export parameters"

```
726  </accounts>
727</k9settings>lue key="pushMode">SECOND_CLASS</value>
728        </folder>

```
Since a new reinstallation, the generated xml file seems to be correct, 
I didn't dare to try a new import for fear of having to redo all my configuration.
It's a bit incomprehensible.
Thanks for reporting this.

Looks like this happens when overwriting a file and writing less data than there was before.

Pull request: Truncate file when overwriting existing settings file
Fixes #5988

Test command: ./gradlew :app:core:testDebugUnitTest --tests "com.fsck.k9.logging.LogcatLogFileWriterTest"

Fix patch:
diff --git a/app/core/src/main/java/com/fsck/k9/logging/LogFileWriter.kt b/app/core/src/main/java/com/fsck/k9/logging/LogFileWriter.kt
index d81c9e16611..831637708c6 100644
--- a/app/core/src/main/java/com/fsck/k9/logging/LogFileWriter.kt
+++ b/app/core/src/main/java/com/fsck/k9/logging/LogFileWriter.kt
@@ -26,7 +26,7 @@ class LogcatLogFileWriter(
     private fun writeLogBlocking(contentUri: Uri) {
         Timber.v("Writing logcat output to content URI: %s", contentUri)
 
-        val outputStream = contentResolver.openOutputStream(contentUri)
+        val outputStream = contentResolver.openOutputStream(contentUri, "wt")
             ?: error("Error opening contentUri for writing")
 
         outputStream.use {
diff --git a/app/core/src/main/java/com/fsck/k9/preferences/SettingsExporter.kt b/app/core/src/main/java/com/fsck/k9/preferences/SettingsExporter.kt
index 39983b13918..08bc5bbefc0 100644
--- a/app/core/src/main/java/com/fsck/k9/preferences/SettingsExporter.kt
+++ b/app/core/src/main/java/com/fsck/k9/preferences/SettingsExporter.kt
@@ -33,7 +33,7 @@ class SettingsExporter(
         updateNotificationSettings(accountUuids)
 
         try {
-            contentResolver.openOutputStream(uri)!!.use { outputStream ->
+            contentResolver.openOutputStream(uri, "wt")!!.use { outputStream ->
                 exportPreferences(outputStream, includeGlobals, accountUuids)
             }
         } catch (e: Exception) {
diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/ui/messageview/AttachmentController.java b/app/ui/legacy/src/main/java/com/fsck/k9/ui/messageview/AttachmentController.java
index 1248e19e462..0d90de27b98 100644
--- a/app/ui/legacy/src/main/java/com/fsck/k9/ui/messageview/AttachmentController.java
+++ b/app/ui/legacy/src/main/java/com/fsck/k9/ui/messageview/AttachmentController.java
@@ -117,7 +117,7 @@ private void writeAttachment(Uri documentUri) throws IOException {
         ContentResolver contentResolver = context.getContentResolver();
         InputStream in = contentResolver.openInputStream(attachment.internalUri);
         try {
-            OutputStream out = contentResolver.openOutputStream(documentUri);
+            OutputStream out = contentResolver.openOutputStream(documentUri, "wt");
             try {
                 IOUtils.copy(in, out);
                 out.flush();


Test patch:
diff --git a/app/core/src/test/java/com/fsck/k9/logging/LogcatLogFileWriterTest.kt b/app/core/src/test/java/com/fsck/k9/logging/LogcatLogFileWriterTest.kt
index 2fcc6e93ff9..a4229bbb0d8 100644
--- a/app/core/src/test/java/com/fsck/k9/logging/LogcatLogFileWriterTest.kt
+++ b/app/core/src/test/java/com/fsck/k9/logging/LogcatLogFileWriterTest.kt
@@ -57,13 +57,13 @@ class LogcatLogFileWriterTest {
 
     private fun createContentResolver(): ContentResolver {
         return mock {
-            on { openOutputStream(contentUri) } doReturn outputStream
+            on { openOutputStream(contentUri, "wt") } doReturn outputStream
         }
     }
 
     private fun createThrowingContentResolver(exception: Exception): ContentResolver {
         return mock {
-            on { openOutputStream(contentUri) } doAnswer { throw exception }
+            on { openOutputStream(contentUri, "wt") } doAnswer { throw exception }
         }
     }