Skip to content

Commit dbcffbd

Browse files
Tidy up MediaSendFragment (#1068)
1 parent f2cf756 commit dbcffbd

File tree

12 files changed

+319
-347
lines changed

12 files changed

+319
-347
lines changed

app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ import org.session.libsignal.utilities.AccountId
107107
import org.session.libsignal.utilities.IdPrefix
108108
import org.session.libsignal.utilities.ListenableFuture
109109
import org.session.libsignal.utilities.Log
110-
import org.session.libsignal.utilities.guava.Optional
111110
import org.session.libsignal.utilities.hexEncodedPrivateKey
112111
import org.thoughtcrime.securesms.ApplicationContext
113112
import org.thoughtcrime.securesms.ScreenLockActionBarActivity
@@ -197,7 +196,6 @@ import org.thoughtcrime.securesms.util.isFullyScrolled
197196
import org.thoughtcrime.securesms.util.isScrolledToBottom
198197
import org.thoughtcrime.securesms.util.isScrolledToWithin30dpOfBottom
199198
import org.thoughtcrime.securesms.util.push
200-
import org.thoughtcrime.securesms.util.scrollAmount
201199
import org.thoughtcrime.securesms.util.show
202200
import org.thoughtcrime.securesms.util.toPx
203201
import org.thoughtcrime.securesms.webrtc.WebRtcCallActivity
@@ -825,7 +823,7 @@ class ConversationActivityV2 : ScreenLockActionBarActivity(), InputBarDelegate,
825823
AttachmentManager.MediaType.GIF == mediaType ||
826824
AttachmentManager.MediaType.VIDEO == mediaType)
827825
) {
828-
val media = Media(mediaURI, filename, mimeType, 0, 0, 0, 0, Optional.absent(), Optional.absent())
826+
val media = Media(mediaURI, filename, mimeType, 0, 0, 0, 0, null, null)
829827
startActivityForResult(MediaSendActivity.buildEditorIntent(this, listOf( media ), viewModel.recipient!!, ""), PICK_FROM_LIBRARY)
830828
return
831829
} else {
@@ -1908,7 +1906,7 @@ class ConversationActivityV2 : ScreenLockActionBarActivity(), InputBarDelegate,
19081906
val recipient = viewModel.recipient ?: return
19091907
val mimeType = MediaUtil.getMimeType(this, contentUri)!!
19101908
val filename = FilenameUtils.getFilenameFromUri(this, contentUri, mimeType)
1911-
val media = Media(contentUri, filename, mimeType, 0, 0, 0, 0, Optional.absent(), Optional.absent())
1909+
val media = Media(contentUri, filename, mimeType, 0, 0, 0, 0, null, null)
19121910
startActivityForResult(MediaSendActivity.buildEditorIntent(this, listOf( media ), recipient, getMessageBody()), PICK_FROM_LIBRARY)
19131911
}
19141912

@@ -2121,9 +2119,9 @@ class ConversationActivityV2 : ScreenLockActionBarActivity(), InputBarDelegate,
21212119
for (media in mediaList) {
21222120
val mediaFilename: String? = media.filename
21232121
when {
2124-
MediaUtil.isVideoType(media.mimeType) -> { slideDeck.addSlide(VideoSlide(this, media.uri, mediaFilename, 0, media.caption.orNull())) }
2125-
MediaUtil.isGif(media.mimeType) -> { slideDeck.addSlide(GifSlide(this, media.uri, mediaFilename, 0, media.width, media.height, media.caption.orNull())) }
2126-
MediaUtil.isImageType(media.mimeType) -> { slideDeck.addSlide(ImageSlide(this, media.uri, mediaFilename, 0, media.width, media.height, media.caption.orNull())) }
2122+
MediaUtil.isVideoType(media.mimeType) -> { slideDeck.addSlide(VideoSlide(this, media.uri, mediaFilename, 0, media.caption)) }
2123+
MediaUtil.isGif(media.mimeType) -> { slideDeck.addSlide(GifSlide(this, media.uri, mediaFilename, 0, media.width, media.height, media.caption)) }
2124+
MediaUtil.isImageType(media.mimeType) -> { slideDeck.addSlide(ImageSlide(this, media.uri, mediaFilename, 0, media.width, media.height, media.caption)) }
21272125
else -> {
21282126
Log.d(TAG, "Asked to send an unexpected media type: '" + media.mimeType + "'. Skipping.")
21292127
}

app/src/main/java/org/thoughtcrime/securesms/database/AttachmentDatabase.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -562,17 +562,28 @@ public void handleFailedAttachmentUpload(@NonNull AttachmentId id) {
562562
throw new MmsException("No attachment data found!");
563563
}
564564

565-
dataInfo = setAttachmentData(dataInfo.file, mediaStream.getStream());
565+
final File oldFile = dataInfo.file;
566+
567+
dataInfo = setAttachmentData(mediaStream.getStream());
566568

567569
ContentValues contentValues = new ContentValues();
568570
contentValues.put(SIZE, dataInfo.length);
569571
contentValues.put(CONTENT_TYPE, mediaStream.getMimeType());
570572
contentValues.put(WIDTH, mediaStream.getWidth());
571573
contentValues.put(HEIGHT, mediaStream.getHeight());
572574
contentValues.put(DATA_RANDOM, dataInfo.random);
575+
contentValues.put(DATA, dataInfo.file.getAbsolutePath());
573576

574577
database.update(TABLE_NAME, contentValues, PART_ID_WHERE, databaseAttachment.getAttachmentId().toStrings());
575578

579+
if (oldFile != null && oldFile.exists()) {
580+
try {
581+
oldFile.delete();
582+
} catch (Exception e) {
583+
Log.w(TAG, "Error deleting an old attachment file", e);
584+
}
585+
}
586+
576587
return new DatabaseAttachment(databaseAttachment.getAttachmentId(),
577588
databaseAttachment.getMmsId(),
578589
databaseAttachment.hasData(),
@@ -696,20 +707,12 @@ public void setTransferState(long messageId, @NonNull AttachmentId attachmentId,
696707
try {
697708
File partsDirectory = context.getDir(DIRECTORY, Context.MODE_PRIVATE);
698709
File dataFile = File.createTempFile("part", ".mms", partsDirectory);
699-
return setAttachmentData(dataFile, in);
700-
} catch (IOException e) {
701-
throw new MmsException(e);
702-
}
703-
}
704710

705-
private @NonNull DataInfo setAttachmentData(@NonNull File destination, @NonNull InputStream in)
706-
throws MmsException
707-
{
708-
try {
709-
Pair<byte[], OutputStream> out = ModernEncryptingPartOutputStream.createFor(attachmentSecret, destination, false);
711+
Log.d("AttachmentDatabase", "Writing attachment data to: " + dataFile.getAbsolutePath());
712+
Pair<byte[], OutputStream> out = ModernEncryptingPartOutputStream.createFor(attachmentSecret, dataFile, false);
710713
long length = Util.copy(in, out.second);
711714

712-
return new DataInfo(destination, length, out.first);
715+
return new DataInfo(dataFile, length, out.first);
713716
} catch (IOException e) {
714717
throw new MmsException(e);
715718
}

app/src/main/java/org/thoughtcrime/securesms/mediapreview/MediaPreviewViewModel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ private int getCursorPosition(int position) {
119119
mediaRecord.getAttachment().getWidth(),
120120
mediaRecord.getAttachment().getHeight(),
121121
mediaRecord.getAttachment().getSize(),
122-
Optional.absent(),
123-
Optional.fromNullable(mediaRecord.getAttachment().getCaption())
122+
null,
123+
mediaRecord.getAttachment().getCaption()
124124
);
125125
}
126126

app/src/main/java/org/thoughtcrime/securesms/mediasend/Media.java

Lines changed: 0 additions & 95 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.thoughtcrime.securesms.mediasend
2+
3+
import android.net.Uri
4+
import android.os.Parcelable
5+
import kotlinx.parcelize.Parcelize
6+
7+
/**
8+
* Represents a piece of media that the user has on their device.
9+
*/
10+
@Parcelize
11+
data class Media(
12+
val uri: Uri,
13+
val filename: String,
14+
val mimeType: String,
15+
val date: Long,
16+
val width: Int,
17+
val height: Int,
18+
val size: Long,
19+
val bucketId: String?,
20+
val caption: String?,
21+
) : Parcelable {
22+
23+
// The equality check here is performed based only on the URI of the media.
24+
// This behavior very opinionated and shouldn't really be in a generic equality check in the first place.
25+
// However there are too much code working under this assumption and we can't simply change it to
26+
// a generic solution.
27+
//
28+
// To later dev: once sufficient refactors are done, we can remove this equality
29+
// check and rely on the data class default equality check instead.
30+
override fun equals(other: Any?): Boolean {
31+
if (this === other) return true
32+
if (other !is Media) return false
33+
34+
if (uri != other.uri) return false
35+
36+
return true
37+
}
38+
39+
override fun hashCode(): Int {
40+
return uri.hashCode()
41+
}
42+
43+
44+
companion object {
45+
const val ALL_MEDIA_BUCKET_ID: String = "org.thoughtcrime.securesms.ALL_MEDIA"
46+
}
47+
48+
49+
}

app/src/main/java/org/thoughtcrime/securesms/mediasend/MediaRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ void getPopulatedMedia(@NonNull Context context, @NonNull List<Media> media, @No
194194
long size = cursor.getLong(cursor.getColumnIndexOrThrow(Images.Media.SIZE));
195195
String filename = cursor.getString(cursor.getColumnIndexOrThrow(Images.Media.DISPLAY_NAME));
196196

197-
media.add(new Media(uri, filename, mimetype, date, width, height, size, Optional.of(bucketId), Optional.absent()));
197+
media.add(new Media(uri, filename, mimetype, date, width, height, size, bucketId, null));
198198
}
199199
}
200200

app/src/main/java/org/thoughtcrime/securesms/mediasend/MediaSendActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ class MediaSendActivity : ScreenLockActionBarActivity(), MediaPickerFolderFragme
254254
width,
255255
height,
256256
data.size.toLong(),
257-
Optional.of<String>(Media.ALL_MEDIA_BUCKET_ID),
258-
Optional.absent<String>()
257+
Media.ALL_MEDIA_BUCKET_ID,
258+
null
259259
)
260260
} catch (e: Exception) {
261261
return@run null

0 commit comments

Comments
 (0)