srctree

Roee Hershberg parent 72073e1e 9d7dbd8f
Fix crash when exporting tox save

Fix crash when exporting tox save and the file doesn't exist. This isdue to bad implementation of the DocumentsUI app and can be reproducedby:

1. Exporting the Tox save to some file2. Clicking on export once again3. Clicking in the file manager on the previously exported Tox save4. Going to another file manager and deleting the previously exported Tox save5. Returning back to the app and clicking SAVE6. Clicking OK on the dialog of the overwrite

inlinesplit
atox/src/main/kotlin/ui/contactlist/ContactListViewModel.kt added: 18, removed: 5, total 13
@@ -12,6 +12,7 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileOutputStream
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
@@ -80,10 +81,21 @@ class ContactListViewModel @Inject constructor(
 
fun saveToxBackupTo(uri: Uri) = scope.launch(Dispatchers.IO) {
// Export the save.
resolver.openFileDescriptor(uri, "w")!!.use { fd ->
FileOutputStream(fd.fileDescriptor).use { out ->
out.write(tox.getSaveData())
try {
resolver.openFileDescriptor(uri, "w")!!.use { fd ->
FileOutputStream(fd.fileDescriptor).use { out ->
out.write(tox.getSaveData())
}
}
} catch (_: FileNotFoundException) {
withContext(Dispatchers.Main) {
Toast.makeText(
context,
context.getString(R.string.tox_save_export_failure, context.getString(R.string.file_not_found)),
Toast.LENGTH_LONG
).show()
}
return@launch
}
 
// Verify that the exported save can be imported.
 
atox/src/main/res/values/strings.xml added: 18, removed: 5, total 13
@@ -186,4 +186,5 @@
<string name="tap_to_unlock_and_start_atox">Tap to unlock your profile and start aTox</string>
<string name="share">Share</string>
<string name="mark_as_read">Mark as read</string>
<string name="file_not_found">File not found</string>
</resources>
No newline at end of file