Search in sources :

Example 1 with MediaConstraints

use of org.thoughtcrime.securesms.mms.MediaConstraints in project Signal-Android by signalapp.

the class PushGroupSendJob method deliver.

private void deliver(OutgoingMediaMessage message, @Nullable Address filterAddress) throws IOException, RecipientFormattingException, InvalidNumberException, EncapsulatedExceptions, UndeliverableMessageException {
    String groupId = message.getRecipient().getAddress().toGroupString();
    Optional<byte[]> profileKey = getProfileKey(message.getRecipient());
    List<Address> recipients = getGroupMessageRecipients(groupId, messageId);
    MediaConstraints mediaConstraints = MediaConstraints.getPushMediaConstraints();
    List<Attachment> scaledAttachments = scaleAttachments(mediaConstraints, message.getAttachments());
    List<SignalServiceAttachment> attachmentStreams = getAttachmentsFor(scaledAttachments);
    List<SignalServiceAddress> addresses;
    if (filterAddress != null)
        addresses = getPushAddresses(filterAddress);
    else
        addresses = getPushAddresses(recipients);
    if (message.isGroup()) {
        OutgoingGroupMediaMessage groupMessage = (OutgoingGroupMediaMessage) message;
        GroupContext groupContext = groupMessage.getGroupContext();
        SignalServiceAttachment avatar = attachmentStreams.isEmpty() ? null : attachmentStreams.get(0);
        SignalServiceGroup.Type type = groupMessage.isGroupQuit() ? SignalServiceGroup.Type.QUIT : SignalServiceGroup.Type.UPDATE;
        SignalServiceGroup group = new SignalServiceGroup(type, GroupUtil.getDecodedId(groupId), groupContext.getName(), groupContext.getMembersList(), avatar);
        SignalServiceDataMessage groupDataMessage = SignalServiceDataMessage.newBuilder().withTimestamp(message.getSentTimeMillis()).asGroupMessage(group).build();
        messageSender.sendMessage(addresses, groupDataMessage);
    } else {
        SignalServiceGroup group = new SignalServiceGroup(GroupUtil.getDecodedId(groupId));
        SignalServiceDataMessage groupMessage = SignalServiceDataMessage.newBuilder().withTimestamp(message.getSentTimeMillis()).asGroupMessage(group).withAttachments(attachmentStreams).withBody(message.getBody()).withExpiration((int) (message.getExpiresIn() / 1000)).asExpirationUpdate(message.isExpirationUpdate()).withProfileKey(profileKey.orNull()).build();
        messageSender.sendMessage(addresses, groupMessage);
    }
}
Also used : Address(org.thoughtcrime.securesms.database.Address) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) SignalServiceAttachment(org.whispersystems.signalservice.api.messages.SignalServiceAttachment) Attachment(org.thoughtcrime.securesms.attachments.Attachment) SignalServiceAttachment(org.whispersystems.signalservice.api.messages.SignalServiceAttachment) SignalServiceDataMessage(org.whispersystems.signalservice.api.messages.SignalServiceDataMessage) OutgoingGroupMediaMessage(org.thoughtcrime.securesms.mms.OutgoingGroupMediaMessage) MediaConstraints(org.thoughtcrime.securesms.mms.MediaConstraints) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) SignalServiceGroup(org.whispersystems.signalservice.api.messages.SignalServiceGroup) GroupContext(org.whispersystems.signalservice.internal.push.SignalServiceProtos.GroupContext)

Example 2 with MediaConstraints

use of org.thoughtcrime.securesms.mms.MediaConstraints in project Signal-Android by WhisperSystems.

the class AttachmentCompressionJob method onRun.

@Override
public void onRun() throws Exception {
    Log.d(TAG, "Running for: " + attachmentId);
    AttachmentDatabase database = SignalDatabase.attachments();
    DatabaseAttachment databaseAttachment = database.getAttachment(attachmentId);
    if (databaseAttachment == null) {
        throw new UndeliverableMessageException("Cannot find the specified attachment.");
    }
    if (databaseAttachment.getTransformProperties().shouldSkipTransform()) {
        Log.i(TAG, "Skipping at the direction of the TransformProperties.");
        return;
    }
    MediaConstraints mediaConstraints = mms ? MediaConstraints.getMmsMediaConstraints(mmsSubscriptionId) : MediaConstraints.getPushMediaConstraints(SentMediaQuality.fromCode(databaseAttachment.getTransformProperties().getSentMediaQuality()));
    compress(database, mediaConstraints, databaseAttachment);
}
Also used : MediaConstraints(org.thoughtcrime.securesms.mms.MediaConstraints) UndeliverableMessageException(org.thoughtcrime.securesms.transport.UndeliverableMessageException) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) AttachmentDatabase(org.thoughtcrime.securesms.database.AttachmentDatabase)

Example 3 with MediaConstraints

use of org.thoughtcrime.securesms.mms.MediaConstraints in project Signal-Android by WhisperSystems.

the class ShareRepository method isMmsSupported.

private boolean isMmsSupported(@NonNull Context context, @NonNull Attachment attachment) {
    boolean canReadPhoneState = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED;
    if (!Util.isDefaultSmsProvider(context) || !canReadPhoneState || !Util.isMmsCapable(context)) {
        return false;
    }
    TransportOptions options = new TransportOptions(context, true);
    options.setDefaultTransport(TransportOption.Type.SMS);
    MediaConstraints mmsConstraints = MediaConstraints.getMmsMediaConstraints(options.getSelectedTransport().getSimSubscriptionId().or(-1));
    return mmsConstraints.isSatisfied(context, attachment) || mmsConstraints.canResize(attachment);
}
Also used : MediaConstraints(org.thoughtcrime.securesms.mms.MediaConstraints) TransportOptions(org.thoughtcrime.securesms.TransportOptions)

Example 4 with MediaConstraints

use of org.thoughtcrime.securesms.mms.MediaConstraints in project Signal-Android by signalapp.

the class MmsSendJob method constructSendPdu.

private SendReq constructSendPdu(OutgoingMediaMessage message) throws UndeliverableMessageException {
    SendReq req = new SendReq();
    String lineNumber = getMyNumber(context);
    Address destination = message.getRecipient().getAddress();
    MediaConstraints mediaConstraints = MediaConstraints.getMmsMediaConstraints(message.getSubscriptionId());
    List<Attachment> scaledAttachments = scaleAttachments(mediaConstraints, message.getAttachments());
    if (!TextUtils.isEmpty(lineNumber)) {
        req.setFrom(new EncodedStringValue(lineNumber));
    } else {
        req.setFrom(new EncodedStringValue(TextSecurePreferences.getLocalNumber(context)));
    }
    if (destination.isMmsGroup()) {
        List<Recipient> members = DatabaseFactory.getGroupDatabase(context).getGroupMembers(destination.toGroupString(), false);
        for (Recipient member : members) {
            if (message.getDistributionType() == ThreadDatabase.DistributionTypes.BROADCAST) {
                req.addBcc(new EncodedStringValue(member.getAddress().serialize()));
            } else {
                req.addTo(new EncodedStringValue(member.getAddress().serialize()));
            }
        }
    } else {
        req.addTo(new EncodedStringValue(destination.serialize()));
    }
    req.setDate(System.currentTimeMillis() / 1000);
    PduBody body = new PduBody();
    int size = 0;
    if (!TextUtils.isEmpty(message.getBody())) {
        PduPart part = new PduPart();
        String name = String.valueOf(System.currentTimeMillis());
        part.setData(Util.toUtf8Bytes(message.getBody()));
        part.setCharset(CharacterSets.UTF_8);
        part.setContentType(ContentType.TEXT_PLAIN.getBytes());
        part.setContentId(name.getBytes());
        part.setContentLocation((name + ".txt").getBytes());
        part.setName((name + ".txt").getBytes());
        body.addPart(part);
        size += getPartSize(part);
    }
    for (Attachment attachment : scaledAttachments) {
        try {
            if (attachment.getDataUri() == null)
                throw new IOException("Assertion failed, attachment for outgoing MMS has no data!");
            String fileName = attachment.getFileName();
            PduPart part = new PduPart();
            if (fileName == null) {
                fileName = String.valueOf(Math.abs(Util.getSecureRandom().nextLong()));
                String fileExtension = MimeTypeMap.getSingleton().getExtensionFromMimeType(attachment.getContentType());
                if (fileExtension != null)
                    fileName = fileName + "." + fileExtension;
            }
            if (attachment.getContentType().startsWith("text")) {
                part.setCharset(CharacterSets.UTF_8);
            }
            part.setContentType(attachment.getContentType().getBytes());
            part.setContentLocation(fileName.getBytes());
            part.setName(fileName.getBytes());
            int index = fileName.lastIndexOf(".");
            String contentId = (index == -1) ? fileName : fileName.substring(0, index);
            part.setContentId(contentId.getBytes());
            part.setData(Util.readFully(PartAuthority.getAttachmentStream(context, attachment.getDataUri())));
            body.addPart(part);
            size += getPartSize(part);
        } catch (IOException e) {
            Log.w(TAG, e);
        }
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    SmilXmlSerializer.serialize(SmilHelper.createSmilDocument(body), out);
    PduPart smilPart = new PduPart();
    smilPart.setContentId("smil".getBytes());
    smilPart.setContentLocation("smil.xml".getBytes());
    smilPart.setContentType(ContentType.APP_SMIL.getBytes());
    smilPart.setData(out.toByteArray());
    body.addPart(0, smilPart);
    req.setBody(body);
    req.setMessageSize(size);
    req.setMessageClass(PduHeaders.MESSAGE_CLASS_PERSONAL_STR.getBytes());
    req.setExpiry(7 * 24 * 60 * 60);
    try {
        req.setPriority(PduHeaders.PRIORITY_NORMAL);
        req.setDeliveryReport(PduHeaders.VALUE_NO);
        req.setReadReport(PduHeaders.VALUE_NO);
    } catch (InvalidHeaderValueException e) {
    }
    return req;
}
Also used : EncodedStringValue(com.google.android.mms.pdu_alt.EncodedStringValue) Address(org.thoughtcrime.securesms.database.Address) PduBody(com.google.android.mms.pdu_alt.PduBody) Attachment(org.thoughtcrime.securesms.attachments.Attachment) Recipient(org.thoughtcrime.securesms.recipients.Recipient) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SendReq(com.google.android.mms.pdu_alt.SendReq) MediaConstraints(org.thoughtcrime.securesms.mms.MediaConstraints) InvalidHeaderValueException(com.google.android.mms.InvalidHeaderValueException) PduPart(com.google.android.mms.pdu_alt.PduPart)

Example 5 with MediaConstraints

use of org.thoughtcrime.securesms.mms.MediaConstraints in project Signal-Android by signalapp.

the class PushMediaSendJob method deliver.

private void deliver(OutgoingMediaMessage message) throws RetryLaterException, InsecureFallbackApprovalException, UntrustedIdentityException, UndeliverableMessageException {
    if (message.getRecipient() == null) {
        throw new UndeliverableMessageException("No destination address.");
    }
    try {
        SignalServiceAddress address = getPushAddress(message.getRecipient().getAddress());
        MediaConstraints mediaConstraints = MediaConstraints.getPushMediaConstraints();
        List<Attachment> scaledAttachments = scaleAttachments(mediaConstraints, message.getAttachments());
        List<SignalServiceAttachment> attachmentStreams = getAttachmentsFor(scaledAttachments);
        Optional<byte[]> profileKey = getProfileKey(message.getRecipient());
        SignalServiceDataMessage mediaMessage = SignalServiceDataMessage.newBuilder().withBody(message.getBody()).withAttachments(attachmentStreams).withTimestamp(message.getSentTimeMillis()).withExpiration((int) (message.getExpiresIn() / 1000)).withProfileKey(profileKey.orNull()).asExpirationUpdate(message.isExpirationUpdate()).build();
        messageSender.sendMessage(address, mediaMessage);
    } catch (UnregisteredUserException e) {
        Log.w(TAG, e);
        throw new InsecureFallbackApprovalException(e);
    } catch (FileNotFoundException e) {
        Log.w(TAG, e);
        throw new UndeliverableMessageException(e);
    } catch (IOException e) {
        Log.w(TAG, e);
        throw new RetryLaterException(e);
    }
}
Also used : UnregisteredUserException(org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException) FileNotFoundException(java.io.FileNotFoundException) SignalServiceAttachment(org.whispersystems.signalservice.api.messages.SignalServiceAttachment) Attachment(org.thoughtcrime.securesms.attachments.Attachment) IOException(java.io.IOException) InsecureFallbackApprovalException(org.thoughtcrime.securesms.transport.InsecureFallbackApprovalException) SignalServiceAttachment(org.whispersystems.signalservice.api.messages.SignalServiceAttachment) SignalServiceDataMessage(org.whispersystems.signalservice.api.messages.SignalServiceDataMessage) MediaConstraints(org.thoughtcrime.securesms.mms.MediaConstraints) UndeliverableMessageException(org.thoughtcrime.securesms.transport.UndeliverableMessageException) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) RetryLaterException(org.thoughtcrime.securesms.transport.RetryLaterException)

Aggregations

MediaConstraints (org.thoughtcrime.securesms.mms.MediaConstraints)7 Attachment (org.thoughtcrime.securesms.attachments.Attachment)4 IOException (java.io.IOException)3 UndeliverableMessageException (org.thoughtcrime.securesms.transport.UndeliverableMessageException)3 InvalidHeaderValueException (com.google.android.mms.InvalidHeaderValueException)2 EncodedStringValue (com.google.android.mms.pdu_alt.EncodedStringValue)2 PduBody (com.google.android.mms.pdu_alt.PduBody)2 PduPart (com.google.android.mms.pdu_alt.PduPart)2 SendReq (com.google.android.mms.pdu_alt.SendReq)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DatabaseAttachment (org.thoughtcrime.securesms.attachments.DatabaseAttachment)2 Address (org.thoughtcrime.securesms.database.Address)2 Recipient (org.thoughtcrime.securesms.recipients.Recipient)2 SignalServiceAttachment (org.whispersystems.signalservice.api.messages.SignalServiceAttachment)2 SignalServiceDataMessage (org.whispersystems.signalservice.api.messages.SignalServiceDataMessage)2 SignalServiceAddress (org.whispersystems.signalservice.api.push.SignalServiceAddress)2 Bundle (android.os.Bundle)1 Fragment (androidx.fragment.app.Fragment)1 FileNotFoundException (java.io.FileNotFoundException)1 SecureRandom (java.security.SecureRandom)1