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

85 lines
2.7 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms.mms;
import android.util.Log;
2013-07-19 20:24:18 +00:00
import android.util.Pair;
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)
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) {
PduPart text = new PduPart();
2013-08-31 16:28:49 +00:00
text.setData(message.getBody());
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) {
PduPart media = new PduPart();
2013-07-19 20:24:18 +00:00
FileInputStream fin = new FileInputStream(attachment.first);
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 20:24:18 +00:00
media.setContentType(attachment.second.getBytes(CharacterSets.MIMENAME_ISO_8859_1));
media.setData(data);
body.addPart(media);
2013-07-19 20:24:18 +00:00
attachment.first.delete();
}
}
}
public PduHeaders getPduHeaders() {
return headers;
}
public PduBody getBody() {
return body;
}
public boolean isGroupMessage() {
return !Util.isEmpty(headers.getEncodedStringValues(PduHeaders.CC));
}
}