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

115 lines
3.7 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms.mms;
2014-11-12 20:15:05 +01:00
import android.text.TextUtils;
import android.util.Log;
2014-11-12 20:15:05 +01:00
import org.thoughtcrime.securesms.crypto.MasterSecretUnion;
import org.thoughtcrime.securesms.crypto.MediaKey;
import org.thoughtcrime.securesms.database.PartDatabase;
2014-11-05 00:01:32 +01: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 09:26:43 +01:00
private final String groupId;
private final boolean push;
public IncomingMediaMessage(RetrieveConf retrieved) {
this.headers = retrieved.getPduHeaders();
this.body = retrieved.getBody();
2014-02-21 03:27:43 +01:00
this.groupId = null;
this.push = false;
}
public IncomingMediaMessage(MasterSecretUnion 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 03:27:43 +01: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 03:27:43 +01:00
2014-11-12 20:15:05 +01: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();
String encryptedKey = MediaKey.getEncrypted(masterSecret, attachment.asPointer().getKey());
media.setContentType(Util.toIsoBytes(attachment.getContentType()));
media.setContentLocation(Util.toIsoBytes(String.valueOf(attachment.asPointer().getId())));
media.setContentDisposition(Util.toIsoBytes(encryptedKey));
if (relay.isPresent()) {
media.setName(Util.toIsoBytes(relay.get()));
}
media.setTransferProgress(PartDatabase.TRANSFER_PROGRESS_AUTO_PENDING);
this.body.addPart(media);
}
}
}
}
public PduHeaders getPduHeaders() {
return headers;
}
public PduBody getBody() {
return body;
}
2014-01-14 09:26:43 +01:00
public String getGroupId() {
return groupId;
}
public boolean isPushMessage() {
return push;
}
public boolean isGroupMessage() {
2014-01-14 09:26:43 +01:00
return groupId != null ||
!Util.isEmpty(headers.getEncodedStringValues(PduHeaders.CC)) ||
(headers.getEncodedStringValues(PduHeaders.TO) != null &&
headers.getEncodedStringValues(PduHeaders.TO).length > 1);
}
}