From 6164152b15149ec285c63531b2cf18683fbb12bb Mon Sep 17 00:00:00 2001 From: Cody Henthorne Date: Tue, 15 Feb 2022 09:33:04 -0500 Subject: [PATCH] Fix crash when attempting to save octet-stream data to a media directory. --- .../securesms/util/SaveAttachmentTask.java | 103 +++++++++++------- 1 file changed, 66 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/SaveAttachmentTask.java b/app/src/main/java/org/thoughtcrime/securesms/util/SaveAttachmentTask.java index c99ba74353..861e742fdf 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/util/SaveAttachmentTask.java +++ b/app/src/main/java/org/thoughtcrime/securesms/util/SaveAttachmentTask.java @@ -48,7 +48,7 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask contextReference; + private final WeakReference contextReference; private final int attachmentCount; @@ -62,8 +62,8 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask(context); - this.attachmentCount = count; + this.contextReference = new WeakReference<>(context); + this.attachmentCount = count; } @Override @@ -73,8 +73,8 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask(WRITE_ACCESS_FAILURE, null); @@ -87,12 +87,17 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask(FAILURE, null); + if (directory == null) { + return new Pair<>(FAILURE, null); + } } } - if (attachments.length > 1) return new Pair<>(SUCCESS, null); - else return new Pair<>(SUCCESS, directory); + if (attachments.length > 1) { + return new Pair<>(SUCCESS, null); + } else { + return new Pair<>(SUCCESS, directory); + } } catch (IOException ioe) { Log.w(TAG, ioe); return new Pair<>(FAILURE, null); @@ -101,8 +106,8 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask 0) { updateValues.put(MediaStore.MediaColumns.SIZE, total); @@ -145,10 +144,10 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask 0) { - getContext().getContentResolver().update(mediaUri, updateValues, null, null); + getContext().getContentResolver().update(result.mediaUri, updateValues, null, null); } - return outputUri.getLastPathSegment(); + return result.outputUri.getLastPathSegment(); } private @NonNull Uri getMediaStoreContentUriForType(@NonNull String contentType) { @@ -186,7 +185,7 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask * Note that this method attempts to create a directory if the path returned from * Environment object does not exist yet. The attempt may fail in which case it attempts * to return the default "Document" path. It finally returns null if it also fails. @@ -228,7 +227,7 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask 28) { - int i = 0; + int i = 0; String displayName = fileName; + while (pathInCache(outputUri, displayName) || displayNameTaken(outputUri, displayName)) { displayName = base + "-" + (++i) + "." + extension; } @@ -271,7 +281,7 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask 1) result[1] = tokens[1]; - else result[1] = ""; + if (tokens.length > 1) { + result[1] = tokens[1]; + } else { + result[1] = ""; + } return result; } @@ -361,18 +383,15 @@ public class SaveAttachmentTask extends ProgressDialogAsyncTask