session-android/src/org/thoughtcrime/securesms/mms/IncomingMediaMessage.java

118 lines
3.8 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms.mms;
2014-11-12 19:15:05 +00:00
import android.text.TextUtils;
import org.thoughtcrime.securesms.crypto.MasterCipher;
import org.thoughtcrime.securesms.crypto.MasterSecret;
2014-11-12 19:15:05 +00:00
import org.thoughtcrime.securesms.util.Base64;
2014-11-04 23:01:32 +00:00
import org.thoughtcrime.securesms.util.GroupUtil;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.libaxolotl.util.guava.Optional;
import org.whispersystems.textsecure.api.messages.TextSecureAttachment;
import org.whispersystems.textsecure.api.messages.TextSecureGroup;
import java.util.List;
import ws.com.google.android.mms.pdu.CharacterSets;
import ws.com.google.android.mms.pdu.EncodedStringValue;
import ws.com.google.android.mms.pdu.PduBody;
import ws.com.google.android.mms.pdu.PduHeaders;
import ws.com.google.android.mms.pdu.PduPart;
import ws.com.google.android.mms.pdu.RetrieveConf;
public class IncomingMediaMessage {
private final PduHeaders headers;
private final PduBody body;
2014-01-14 08:26:43 +00:00
private final String groupId;
private final boolean push;
public IncomingMediaMessage(RetrieveConf retreived) {
2014-02-21 02:27:43 +00:00
this.headers = retreived.getPduHeaders();
this.body = retreived.getBody();
this.groupId = null;
this.push = false;
for (int i=0;i<body.getPartsNum();i++) {
body.getPart(i).setContentId(String.valueOf(System.currentTimeMillis()).getBytes());
}
}
public IncomingMediaMessage(MasterSecret masterSecret,
String from,
String to,
long sentTimeMillis,
Optional<String> relay,
Optional<String> body,
Optional<TextSecureGroup> group,
Optional<List<TextSecureAttachment>> attachments)
{
this.headers = new PduHeaders();
this.body = new PduBody();
this.push = true;
if (group.isPresent()) {
this.groupId = GroupUtil.getEncodedId(group.get().getGroupId());
} else {
2014-02-21 02:27:43 +00:00
this.groupId = null;
}
this.headers.setEncodedStringValue(new EncodedStringValue(from), PduHeaders.FROM);
this.headers.appendEncodedStringValue(new EncodedStringValue(to), PduHeaders.TO);
this.headers.setLongInteger(sentTimeMillis / 1000, PduHeaders.DATE);
2014-02-21 02:27:43 +00:00
2014-11-12 19:15:05 +00:00
if (body.isPresent() && !TextUtils.isEmpty(body.get())) {
PduPart text = new PduPart();
text.setData(Util.toUtf8Bytes(body.get()));
text.setContentType(Util.toIsoBytes("text/plain"));
text.setCharset(CharacterSets.UTF_8);
this.body.addPart(text);
}
if (attachments.isPresent()) {
for (TextSecureAttachment attachment : attachments.get()) {
if (attachment.isPointer()) {
PduPart media = new PduPart();
byte[] encryptedKey = new MasterCipher(masterSecret).encryptBytes(attachment.asPointer().getKey());
media.setContentType(Util.toIsoBytes(attachment.getContentType()));
media.setContentLocation(Util.toIsoBytes(String.valueOf(attachment.asPointer().getId())));
media.setContentDisposition(Util.toIsoBytes(Base64.encodeBytes(encryptedKey)));
if (relay.isPresent()) {
media.setName(Util.toIsoBytes(relay.get()));
}
media.setPendingPush(true);
this.body.addPart(media);
}
}
}
}
public PduHeaders getPduHeaders() {
return headers;
}
public PduBody getBody() {
return body;
}
2014-01-14 08:26:43 +00:00
public String getGroupId() {
return groupId;
}
public boolean isPushMessage() {
return push;
}
public boolean isGroupMessage() {
2014-01-14 08:26:43 +00:00
return groupId != null ||
!Util.isEmpty(headers.getEncodedStringValues(PduHeaders.CC)) ||
(headers.getEncodedStringValues(PduHeaders.TO) != null &&
headers.getEncodedStringValues(PduHeaders.TO).length > 1);
}
}