Use streams to encode and decode the JSON backups instead of storing the file contents in memory.
This significantly reduces memory usage when importing/exporting backups.
Report OutOfMemoryError when importing and exporting messages. (Less likely to happen after this PR, but still possible.)
Delete exported file on error. (Fixes an existing issue that is somewhat related this PR.)
Implementation notes:
I initially tried to delete using contentResolver.delete(uri, null, null), but that failed with "UnsupportedOperationException: Delete not supported" so I switched to DocumentsContract.deleteDocument().
But when using the Fossify File Manager (FFM) as the file picker, deleting doesn't seem to do anything in both cases.
Correction: With FFM, contentResolver.delete() works but DocumentsContract.deleteDocument() doesn't do anything.
I think it's okay to just use DocumentsContract.deleteDocument(), so that it works well with the native file picker, and accept that if using FFM then the files won't be cleaned up on error. I don't think it's a big deal because I can't even choose FFM as the file picker on Android 14, and because FFM currently allows the user to pick an existing filename so it's probably not a good idea to delete when FFM is used. Also, it's possible that DocumentsContract.deleteDocument() not doing anything is a bug in FFM; if that's the case then that bug should be fixed instead of adding a workaround to Messages.
Before/After Screenshots/Screen Record
N/A
Fixes the following issue(s)
Fixes #6
Other apps
There are several other apps that have a similar JSON import/export feature. Should I port this fix to them?
I can confirm this fixes the issue. Tested exporting on a Pixel 5 and importing on a Pixel 4 and all MMS and SMS were available. Generated archive was ~120MB. It's slow but it works. Please merge.
What is it?
Description of the changes in your PR
Use streams to encode and decode the JSON backups instead of storing the file contents in memory. This significantly reduces memory usage when importing/exporting backups.
Report OutOfMemoryError when importing and exporting messages. (Less likely to happen after this PR, but still possible.)
Delete exported file on error. (Fixes an existing issue that is somewhat related this PR.) Implementation notes: I initially tried to delete using
contentResolver.delete(uri, null, null)
, but that failed with "UnsupportedOperationException: Delete not supported" so I switched to DocumentsContract.deleteDocument(). But when using the Fossify File Manager (FFM) as the file picker,deleting doesn't seem to do anything in both cases.Correction: With FFM, contentResolver.delete() works but DocumentsContract.deleteDocument() doesn't do anything. I think it's okay to just use DocumentsContract.deleteDocument(), so that it works well with the native file picker, and accept that if using FFM then the files won't be cleaned up on error. I don't think it's a big deal because I can't even choose FFM as the file picker on Android 14, and because FFM currently allows the user to pick an existing filename so it's probably not a good idea to delete when FFM is used. Also, it's possible that DocumentsContract.deleteDocument() not doing anything is a bug in FFM; if that's the case then that bug should be fixed instead of adding a workaround to Messages.Before/After Screenshots/Screen Record
N/A
Fixes the following issue(s)
Other apps
There are several other apps that have a similar JSON import/export feature. Should I port this fix to them?
Acknowledgement