Skip to content

Commit

Permalink
Large attachment support
Browse files Browse the repository at this point in the history
  • Loading branch information
mcginty authored and BLeQuerrec committed Sep 21, 2015
1 parent fe0f6fd commit 5f65fed
Show file tree
Hide file tree
Showing 14 changed files with 207 additions and 245 deletions.
1 change: 1 addition & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
<string name="ConversationActivity_mms_not_supported_title">MMS not supported</string>
<string name="ConversationActivity_mms_not_supported_message">This message cannot be sent since your carrier doesn\'t support MMS.</string>
<string name="ConversationActivity_specify_recipient">Please choose a contact</string>
<string name="ConversationActivity_attachment_exceeds_size_limits">Attachment exceeds size limits for the type of message you\'re sending.</string>

<!-- ConversationFragment -->
<string name="ConversationFragment_message_details">Message details</string>
Expand Down
105 changes: 43 additions & 62 deletions src/org/smssecure/smssecure/ConversationActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.WindowCompat;
import android.text.Editable;
import android.text.TextWatcher;
Expand Down Expand Up @@ -83,8 +84,9 @@
import org.smssecure.smssecure.database.MmsSmsColumns.Types;
import org.smssecure.smssecure.database.ThreadDatabase;
import org.smssecure.smssecure.mms.AttachmentManager;
import org.smssecure.smssecure.mms.AttachmentManager.MediaType;
import org.smssecure.smssecure.mms.AttachmentTypeSelectorAdapter;
import org.smssecure.smssecure.mms.MediaTooLargeException;
import org.smssecure.smssecure.mms.MediaConstraints;
import org.smssecure.smssecure.mms.MmsMediaConstraints;
import org.smssecure.smssecure.mms.OutgoingMediaMessage;
import org.smssecure.smssecure.mms.OutgoingSecureMediaMessage;
Expand All @@ -108,6 +110,7 @@
import org.smssecure.smssecure.util.DynamicLanguage;
import org.smssecure.smssecure.util.DynamicTheme;
import org.smssecure.smssecure.util.GroupUtil;
import org.smssecure.smssecure.util.MediaUtil;
import org.smssecure.smssecure.util.SMSSecurePreferences;
import org.smssecure.smssecure.util.TelephonyUtil;
import org.smssecure.smssecure.util.Util;
Expand Down Expand Up @@ -275,20 +278,23 @@ public void onActivityResult(int reqCode, int resultCode, Intent data) {

switch (reqCode) {
case PICK_IMAGE:
addAttachmentImage(masterSecret, data.getData());
setMedia(data.getData(),
MediaUtil.isGif(MediaUtil.getMimeType(this, data.getData())) ? MediaType.GIF
: MediaType.IMAGE,
false);
break;
case PICK_VIDEO:
addAttachmentVideo(data.getData());
setMedia(data.getData(), MediaType.VIDEO, false);
break;
case PICK_AUDIO:
addAttachmentAudio(data.getData());
setMedia(data.getData(), MediaType.AUDIO, false);
break;
case PICK_CONTACT_INFO:
addAttachmentContactInfo(data.getData());
break;
case TAKE_PHOTO:
if (attachmentManager.getCaptureUri() != null) {
addAttachmentImage(masterSecret, attachmentManager.getCaptureUri());
setMedia(attachmentManager.getCaptureUri(), MediaType.IMAGE, true);
}
break;
}
Expand Down Expand Up @@ -631,9 +637,10 @@ private void initializeDraft() {
Uri draftVideo = getIntent().getParcelableExtra(DRAFT_VIDEO_EXTRA);

if (draftText != null) composeText.setText(draftText);
if (draftImage != null) addAttachmentImage(masterSecret, draftImage);
if (draftAudio != null) addAttachmentAudio(draftAudio);
if (draftVideo != null) addAttachmentVideo(draftVideo);

if (draftImage != null) setMedia(draftImage, MediaType.IMAGE, false);
else if (draftAudio != null) setMedia(draftAudio, MediaType.AUDIO, false);
else if (draftVideo != null) setMedia(draftVideo, MediaType.VIDEO, false);

if (draftText == null && draftImage == null && draftAudio == null && draftVideo == null) {
initializeDraftFromDatabase();
Expand Down Expand Up @@ -667,11 +674,11 @@ protected void onPostExecute(List<Draft> drafts) {
if (draft.getType().equals(Draft.TEXT)) {
composeText.setText(draft.getValue());
} else if (draft.getType().equals(Draft.IMAGE)) {
addAttachmentImage(masterSecret, Uri.parse(draft.getValue()));
setMedia(Uri.parse(draft.getValue()), MediaType.IMAGE, false);
} else if (draft.getType().equals(Draft.AUDIO)) {
addAttachmentAudio(Uri.parse(draft.getValue()));
setMedia(Uri.parse(draft.getValue()), MediaType.AUDIO, false);
} else if (draft.getType().equals(Draft.VIDEO)) {
addAttachmentVideo(Uri.parse(draft.getValue()));
setMedia(Uri.parse(draft.getValue()), MediaType.VIDEO, false);
}
}

Expand Down Expand Up @@ -880,55 +887,8 @@ private void addAttachment(int type) {
}
}

private void addAttachmentImage(MasterSecret masterSecret, Uri imageUri) {
try {
attachmentManager.setImage(masterSecret, imageUri);
} catch (IOException | BitmapDecodingException e) {
Log.w(TAG, e);
attachmentManager.clear();
Toast.makeText(this, R.string.ConversationActivity_sorry_there_was_an_error_setting_your_attachment,
Toast.LENGTH_LONG).show();
} catch (MediaTooLargeException e) {
attachmentManager.clear();
Toast.makeText(this, getString(R.string.ConversationActivity_the_gif_you_selected_was_too_big),
Toast.LENGTH_LONG).show();
Log.w(TAG, e);
}
}

private void addAttachmentVideo(Uri videoUri) {
try {
attachmentManager.setVideo(videoUri);
} catch (IOException e) {
attachmentManager.clear();
Toast.makeText(this, R.string.ConversationActivity_sorry_there_was_an_error_setting_your_attachment,
Toast.LENGTH_LONG).show();
Log.w("ComposeMessageActivity", e);
} catch (MediaTooLargeException e) {
attachmentManager.clear();

Toast.makeText(this, getString(R.string.ConversationActivity_sorry_the_selected_video_exceeds_message_size_restrictions,
(MmsMediaConstraints.MAX_MESSAGE_SIZE/1024)),
Toast.LENGTH_LONG).show();
Log.w("ComposeMessageActivity", e);
}
}

private void addAttachmentAudio(Uri audioUri) {
try {
attachmentManager.setAudio(audioUri);
} catch (IOException e) {
attachmentManager.clear();
Toast.makeText(this, R.string.ConversationActivity_sorry_there_was_an_error_setting_your_attachment,
Toast.LENGTH_LONG).show();
Log.w("ComposeMessageActivity", e);
} catch (MediaTooLargeException e) {
attachmentManager.clear();
Toast.makeText(this, getString(R.string.ConversationActivity_sorry_the_selected_audio_exceeds_message_size_restrictions,
(MmsMediaConstraints.MAX_MESSAGE_SIZE/1024)),
Toast.LENGTH_LONG).show();
Log.w("ComposeMessageActivity", e);
}
private void setMedia(Uri uri, MediaType mediaType, boolean isCapture) {
attachmentManager.setMedia(masterSecret, uri, mediaType, getCurrentMediaConstraints(), isCapture);
}

private void addAttachmentContactInfo(Uri contactUri) {
Expand Down Expand Up @@ -1103,6 +1063,10 @@ private String getMessage() throws InvalidMessageException {
return rawText;
}

private MediaConstraints getCurrentMediaConstraints() {
return MediaConstraints.MMS_CONSTRAINTS;
}

private void markThreadAsRead() {
new AsyncTask<Long, Void, Void>() {
@Override
Expand Down Expand Up @@ -1169,8 +1133,24 @@ private void sendMediaMessage(final boolean forcePlaintext)
final Context context = getApplicationContext();
SlideDeck slideDeck;

if (attachmentManager.isAttachmentPresent()) slideDeck = new SlideDeck(attachmentManager.getSlideDeck());
else slideDeck = new SlideDeck();
if (attachmentManager.isAttachmentPresent()) {
Slide mediaSlide = attachmentManager.getSlideDeck().getThumbnailSlide();
MediaConstraints constraints = getCurrentMediaConstraints();

if (mediaSlide != null &&
!constraints.isSatisfied(this, masterSecret, mediaSlide.getPart()) &&
!constraints.canResize(mediaSlide.getPart()))
{
Toast.makeText(context,
R.string.ConversationActivity_attachment_exceeds_size_limits,
Toast.LENGTH_SHORT).show();
return;
}

slideDeck = new SlideDeck(attachmentManager.getSlideDeck());
} else {
slideDeck = new SlideDeck();
}

OutgoingMediaMessage outgoingMessage = new OutgoingMediaMessage(this, recipients, slideDeck,
getMessage(), distributionType);
Expand Down Expand Up @@ -1335,4 +1315,5 @@ public void onAttachmentChanged() {
initializeSecurity();
updateToggleButtonState();
}

}
2 changes: 1 addition & 1 deletion src/org/smssecure/smssecure/ImageMediaAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void onBindViewHolder(final ViewHolder viewHolder, final Cursor cursor) {
part.setContentType(imageRecord.getContentType().getBytes());
part.setPartId(imageRecord.getPartId());

Slide slide = MediaUtil.getSlideForPart(getContext(), masterSecret, part, imageRecord.getContentType());
Slide slide = MediaUtil.getSlideForPart(getContext(), part, imageRecord.getContentType());
if (slide != null) {
imageView.setImageResource(slide, masterSecret);
}
Expand Down
12 changes: 11 additions & 1 deletion src/org/smssecure/smssecure/components/ThumbnailView.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,24 @@ public void setDownloadClickListener(ThumbnailClickListener listener) {
}

public void clear() {
if (isContextValid()) Glide.clear(this);
if (isContextValid()) Glide.clear(image);
if (slideDeckFuture != null) slideDeckFuture.removeListener(slideDeckListener);
slide = null;
slideId = null;
slideDeckFuture = null;
slideDeckListener = null;
}

public void hideControls(boolean hideControls) {
this.hideControls = hideControls;
if (hideControls) hideProgressWheel();
}

public void showProgressSpinner() {
getProgressWheel().spin();
getProgressWheel().setVisibility(VISIBLE);
}

@TargetApi(VERSION_CODES.JELLY_BEAN_MR1)
private boolean isContextValid() {
return !(getContext() instanceof Activity) ||
Expand Down
10 changes: 3 additions & 7 deletions src/org/smssecure/smssecure/database/MmsDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ private MediaMmsMessageRecord getMediaMmsMessageRecord(Cursor cursor) {
List<IdentityKeyMismatch> mismatches = getMismatchedIdentities(mismatchDocument);
List<NetworkFailure> networkFailures = getFailures(networkDocument);

ListenableFutureTask<SlideDeck> slideDeck = getSlideDeck(masterSecret, dateReceived, id);
ListenableFutureTask<SlideDeck> slideDeck = getSlideDeck(dateReceived, id);

return new MediaMmsMessageRecord(context, id, recipients, recipients.getPrimaryRecipient(),
addressDeviceId, dateSent, dateReceived, dateDeliveryReceived,
Expand Down Expand Up @@ -1084,8 +1084,7 @@ private DisplayRecord.Body getBody(Cursor cursor) {
}
}

private ListenableFutureTask<SlideDeck> getSlideDeck(final MasterSecret masterSecret,
final long timestamp,
private ListenableFutureTask<SlideDeck> getSlideDeck(final long timestamp,
final long id)
{
ListenableFutureTask<SlideDeck> future = getCachedSlideDeck(timestamp, id);
Expand All @@ -1097,12 +1096,9 @@ private ListenableFutureTask<SlideDeck> getSlideDeck(final MasterSecret masterSe
Callable<SlideDeck> task = new Callable<SlideDeck>() {
@Override
public SlideDeck call() throws Exception {
if (masterSecret == null)
return null;

PartDatabase partDatabase = DatabaseFactory.getPartDatabase(context);
PduBody body = getPartsAsBody(partDatabase.getParts(id));
SlideDeck slideDeck = new SlideDeck(context, masterSecret, body);
SlideDeck slideDeck = new SlideDeck(context, body);

if (!body.containsPushInProgress()) {
slideCache.put(timestamp + "::" + id, new SoftReference<>(slideDeck));
Expand Down
Loading

0 comments on commit 5f65fed

Please sign in to comment.