Skip to content

Tidy up MediaSendFragment #1068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ import org.session.libsignal.utilities.AccountId
import org.session.libsignal.utilities.IdPrefix
import org.session.libsignal.utilities.ListenableFuture
import org.session.libsignal.utilities.Log
import org.session.libsignal.utilities.guava.Optional
import org.session.libsignal.utilities.hexEncodedPrivateKey
import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.ScreenLockActionBarActivity
Expand Down Expand Up @@ -197,7 +196,6 @@ import org.thoughtcrime.securesms.util.isFullyScrolled
import org.thoughtcrime.securesms.util.isScrolledToBottom
import org.thoughtcrime.securesms.util.isScrolledToWithin30dpOfBottom
import org.thoughtcrime.securesms.util.push
import org.thoughtcrime.securesms.util.scrollAmount
import org.thoughtcrime.securesms.util.show
import org.thoughtcrime.securesms.util.toPx
import org.thoughtcrime.securesms.webrtc.WebRtcCallActivity
Expand Down Expand Up @@ -825,7 +823,7 @@ class ConversationActivityV2 : ScreenLockActionBarActivity(), InputBarDelegate,
AttachmentManager.MediaType.GIF == mediaType ||
AttachmentManager.MediaType.VIDEO == mediaType)
) {
val media = Media(mediaURI, filename, mimeType, 0, 0, 0, 0, Optional.absent(), Optional.absent())
val media = Media(mediaURI, filename, mimeType, 0, 0, 0, 0, null, null)
startActivityForResult(MediaSendActivity.buildEditorIntent(this, listOf( media ), viewModel.recipient!!, ""), PICK_FROM_LIBRARY)
return
} else {
Expand Down Expand Up @@ -1908,7 +1906,7 @@ class ConversationActivityV2 : ScreenLockActionBarActivity(), InputBarDelegate,
val recipient = viewModel.recipient ?: return
val mimeType = MediaUtil.getMimeType(this, contentUri)!!
val filename = FilenameUtils.getFilenameFromUri(this, contentUri, mimeType)
val media = Media(contentUri, filename, mimeType, 0, 0, 0, 0, Optional.absent(), Optional.absent())
val media = Media(contentUri, filename, mimeType, 0, 0, 0, 0, null, null)
startActivityForResult(MediaSendActivity.buildEditorIntent(this, listOf( media ), recipient, getMessageBody()), PICK_FROM_LIBRARY)
}

Expand Down Expand Up @@ -2121,9 +2119,9 @@ class ConversationActivityV2 : ScreenLockActionBarActivity(), InputBarDelegate,
for (media in mediaList) {
val mediaFilename: String? = media.filename
when {
MediaUtil.isVideoType(media.mimeType) -> { slideDeck.addSlide(VideoSlide(this, media.uri, mediaFilename, 0, media.caption.orNull())) }
MediaUtil.isGif(media.mimeType) -> { slideDeck.addSlide(GifSlide(this, media.uri, mediaFilename, 0, media.width, media.height, media.caption.orNull())) }
MediaUtil.isImageType(media.mimeType) -> { slideDeck.addSlide(ImageSlide(this, media.uri, mediaFilename, 0, media.width, media.height, media.caption.orNull())) }
MediaUtil.isVideoType(media.mimeType) -> { slideDeck.addSlide(VideoSlide(this, media.uri, mediaFilename, 0, media.caption)) }
MediaUtil.isGif(media.mimeType) -> { slideDeck.addSlide(GifSlide(this, media.uri, mediaFilename, 0, media.width, media.height, media.caption)) }
MediaUtil.isImageType(media.mimeType) -> { slideDeck.addSlide(ImageSlide(this, media.uri, mediaFilename, 0, media.width, media.height, media.caption)) }
else -> {
Log.d(TAG, "Asked to send an unexpected media type: '" + media.mimeType + "'. Skipping.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,17 +562,28 @@ public void handleFailedAttachmentUpload(@NonNull AttachmentId id) {
throw new MmsException("No attachment data found!");
}

dataInfo = setAttachmentData(dataInfo.file, mediaStream.getStream());
final File oldFile = dataInfo.file;

dataInfo = setAttachmentData(mediaStream.getStream());

ContentValues contentValues = new ContentValues();
contentValues.put(SIZE, dataInfo.length);
contentValues.put(CONTENT_TYPE, mediaStream.getMimeType());
contentValues.put(WIDTH, mediaStream.getWidth());
contentValues.put(HEIGHT, mediaStream.getHeight());
contentValues.put(DATA_RANDOM, dataInfo.random);
contentValues.put(DATA, dataInfo.file.getAbsolutePath());

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

if (oldFile != null && oldFile.exists()) {
try {
oldFile.delete();
} catch (Exception e) {
Log.w(TAG, "Error deleting an old attachment file", e);
}
}

return new DatabaseAttachment(databaseAttachment.getAttachmentId(),
databaseAttachment.getMmsId(),
databaseAttachment.hasData(),
Expand Down Expand Up @@ -696,20 +707,12 @@ public void setTransferState(long messageId, @NonNull AttachmentId attachmentId,
try {
File partsDirectory = context.getDir(DIRECTORY, Context.MODE_PRIVATE);
File dataFile = File.createTempFile("part", ".mms", partsDirectory);
return setAttachmentData(dataFile, in);
} catch (IOException e) {
throw new MmsException(e);
}
}

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

return new DataInfo(destination, length, out.first);
return new DataInfo(dataFile, length, out.first);
} catch (IOException e) {
throw new MmsException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ private int getCursorPosition(int position) {
mediaRecord.getAttachment().getWidth(),
mediaRecord.getAttachment().getHeight(),
mediaRecord.getAttachment().getSize(),
Optional.absent(),
Optional.fromNullable(mediaRecord.getAttachment().getCaption())
null,
mediaRecord.getAttachment().getCaption()
);
}

Expand Down
95 changes: 0 additions & 95 deletions app/src/main/java/org/thoughtcrime/securesms/mediasend/Media.java

This file was deleted.

49 changes: 49 additions & 0 deletions app/src/main/java/org/thoughtcrime/securesms/mediasend/Media.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.thoughtcrime.securesms.mediasend

import android.net.Uri
import android.os.Parcelable
import kotlinx.parcelize.Parcelize

/**
* Represents a piece of media that the user has on their device.
*/
@Parcelize
data class Media(
val uri: Uri,
val filename: String,
val mimeType: String,
val date: Long,
val width: Int,
val height: Int,
val size: Long,
val bucketId: String?,
val caption: String?,
) : Parcelable {

// The equality check here is performed based only on the URI of the media.
// This behavior very opinionated and shouldn't really be in a generic equality check in the first place.
// However there are too much code working under this assumption and we can't simply change it to
// a generic solution.
//
// To later dev: once sufficient refactors are done, we can remove this equality
// check and rely on the data class default equality check instead.
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Media) return false

if (uri != other.uri) return false

return true
}

override fun hashCode(): Int {
return uri.hashCode()
}


companion object {
const val ALL_MEDIA_BUCKET_ID: String = "org.thoughtcrime.securesms.ALL_MEDIA"
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ void getPopulatedMedia(@NonNull Context context, @NonNull List<Media> media, @No
long size = cursor.getLong(cursor.getColumnIndexOrThrow(Images.Media.SIZE));
String filename = cursor.getString(cursor.getColumnIndexOrThrow(Images.Media.DISPLAY_NAME));

media.add(new Media(uri, filename, mimetype, date, width, height, size, Optional.of(bucketId), Optional.absent()));
media.add(new Media(uri, filename, mimetype, date, width, height, size, bucketId, null));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ class MediaSendActivity : ScreenLockActionBarActivity(), MediaPickerFolderFragme
width,
height,
data.size.toLong(),
Optional.of<String>(Media.ALL_MEDIA_BUCKET_ID),
Optional.absent<String>()
Media.ALL_MEDIA_BUCKET_ID,
null
)
} catch (e: Exception) {
return@run null
Expand Down
Loading