2013-07-19 00:42:45 +00:00
|
|
|
package org.thoughtcrime.securesms.mms;
|
|
|
|
|
|
|
|
import android.util.Log;
|
2013-07-19 20:24:18 +00:00
|
|
|
import android.util.Pair;
|
2013-07-19 00:42:45 +00:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
|
|
|
import org.whispersystems.textsecure.push.IncomingPushMessage;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
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;
|
|
|
|
|
|
|
|
public IncomingMediaMessage(RetrieveConf retreived) {
|
|
|
|
this.headers = retreived.getPduHeaders();
|
|
|
|
this.body = retreived.getBody();
|
|
|
|
}
|
|
|
|
|
2013-07-19 20:24:18 +00:00
|
|
|
public IncomingMediaMessage(String localNumber, IncomingPushMessage message,
|
|
|
|
List<Pair<File, String>> attachments)
|
2013-07-19 00:42:45 +00:00
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
this.headers = new PduHeaders();
|
|
|
|
this.body = new PduBody();
|
|
|
|
|
|
|
|
this.headers.setEncodedStringValue(new EncodedStringValue(message.getSource()), PduHeaders.FROM);
|
|
|
|
this.headers.appendEncodedStringValue(new EncodedStringValue(localNumber), PduHeaders.TO);
|
|
|
|
|
|
|
|
for (String destination : message.getDestinations()) {
|
|
|
|
if (!destination.equals(localNumber)) {
|
|
|
|
this.headers.appendEncodedStringValue(new EncodedStringValue(destination), PduHeaders.CC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.headers.setLongInteger(message.getTimestampMillis() / 1000, PduHeaders.DATE);
|
|
|
|
|
2013-08-31 16:28:49 +00:00
|
|
|
if (message.getBody() != null && message.getBody().length > 0) {
|
2013-07-19 00:42:45 +00:00
|
|
|
PduPart text = new PduPart();
|
2013-08-31 16:28:49 +00:00
|
|
|
text.setData(message.getBody());
|
2013-07-19 00:42:45 +00:00
|
|
|
text.setContentType("text/plain".getBytes(CharacterSets.MIMENAME_ISO_8859_1));
|
|
|
|
body.addPart(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attachments != null) {
|
2013-07-19 20:24:18 +00:00
|
|
|
for (Pair<File, String> attachment : attachments) {
|
2013-07-19 00:42:45 +00:00
|
|
|
PduPart media = new PduPart();
|
2013-07-19 20:24:18 +00:00
|
|
|
FileInputStream fin = new FileInputStream(attachment.first);
|
2013-07-19 00:42:45 +00:00
|
|
|
byte[] data = Util.readFully(fin);
|
|
|
|
|
2013-07-19 20:24:18 +00:00
|
|
|
Log.w("IncomingMediaMessage", "Adding part: " + attachment.second + " with length: " + data.length);
|
2013-07-19 00:42:45 +00:00
|
|
|
|
2013-07-19 20:24:18 +00:00
|
|
|
media.setContentType(attachment.second.getBytes(CharacterSets.MIMENAME_ISO_8859_1));
|
2013-07-19 00:42:45 +00:00
|
|
|
media.setData(data);
|
|
|
|
body.addPart(media);
|
2013-07-19 20:24:18 +00:00
|
|
|
attachment.first.delete();
|
2013-07-19 00:42:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public PduHeaders getPduHeaders() {
|
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
public PduBody getBody() {
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isGroupMessage() {
|
|
|
|
return !Util.isEmpty(headers.getEncodedStringValues(PduHeaders.CC));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|