2013-09-08 18:19:05 -07:00
|
|
|
package org.thoughtcrime.securesms.service;
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.util.Pair;
|
|
|
|
|
2013-11-20 00:45:51 -08:00
|
|
|
import org.thoughtcrime.securesms.Release;
|
2013-09-08 18:19:05 -07:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.EncryptingPartDatabase;
|
|
|
|
import org.thoughtcrime.securesms.database.PartDatabase;
|
2013-11-27 11:08:58 -08:00
|
|
|
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
|
2013-09-08 18:19:05 -07:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2014-04-21 08:40:19 -07:00
|
|
|
import org.whispersystems.libaxolotl.InvalidMessageException;
|
2013-09-08 18:19:05 -07:00
|
|
|
import org.whispersystems.textsecure.crypto.AttachmentCipherInputStream;
|
|
|
|
import org.whispersystems.textsecure.crypto.MasterCipher;
|
|
|
|
import org.whispersystems.textsecure.crypto.MasterSecret;
|
2013-11-19 10:13:24 -08:00
|
|
|
import org.whispersystems.textsecure.push.NotFoundException;
|
2013-09-08 18:19:05 -07:00
|
|
|
import org.whispersystems.textsecure.push.PushServiceSocket;
|
|
|
|
import org.whispersystems.textsecure.util.Base64;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import ws.com.google.android.mms.MmsException;
|
|
|
|
import ws.com.google.android.mms.pdu.PduPart;
|
|
|
|
|
|
|
|
public class PushDownloader {
|
|
|
|
|
|
|
|
private final Context context;
|
|
|
|
|
|
|
|
public PushDownloader(Context context) {
|
|
|
|
this.context = context.getApplicationContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void process(MasterSecret masterSecret, Intent intent) {
|
2013-12-01 18:00:12 -08:00
|
|
|
if (!SendReceiveService.DOWNLOAD_PUSH_ACTION.equals(intent.getAction()))
|
2013-09-08 18:19:05 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
long messageId = intent.getLongExtra("message_id", -1);
|
|
|
|
PartDatabase database = DatabaseFactory.getEncryptingPartDatabase(context, masterSecret);
|
|
|
|
|
|
|
|
Log.w("PushDownloader", "Downloading push parts for: " + messageId);
|
|
|
|
|
|
|
|
if (messageId != -1) {
|
|
|
|
List<Pair<Long, PduPart>> parts = database.getParts(messageId, false);
|
|
|
|
|
|
|
|
for (Pair<Long, PduPart> partPair : parts) {
|
|
|
|
retrievePart(masterSecret, partPair.second, messageId, partPair.first);
|
|
|
|
Log.w("PushDownloader", "Got part: " + partPair.first);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
List<Pair<Long, Pair<Long, PduPart>>> parts = database.getPushPendingParts();
|
|
|
|
|
|
|
|
for (Pair<Long, Pair<Long, PduPart>> partPair : parts) {
|
|
|
|
retrievePart(masterSecret, partPair.second.second, partPair.first, partPair.second.first);
|
|
|
|
Log.w("PushDownloader", "Got part: " + partPair.second.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void retrievePart(MasterSecret masterSecret, PduPart part, long messageId, long partId) {
|
|
|
|
EncryptingPartDatabase database = DatabaseFactory.getEncryptingPartDatabase(context, masterSecret);
|
|
|
|
File attachmentFile = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
MasterCipher masterCipher = new MasterCipher(masterSecret);
|
|
|
|
long contentLocation = Long.parseLong(Util.toIsoString(part.getContentLocation()));
|
|
|
|
byte[] key = masterCipher.decryptBytes(Base64.decode(Util.toIsoString(part.getContentDisposition())));
|
2013-10-19 18:16:11 -07:00
|
|
|
String relay = null;
|
2013-09-08 18:19:05 -07:00
|
|
|
|
2013-10-19 18:16:11 -07:00
|
|
|
if (part.getName() != null) {
|
|
|
|
relay = Util.toIsoString(part.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
attachmentFile = downloadAttachment(relay, contentLocation);
|
2013-09-08 18:19:05 -07:00
|
|
|
InputStream attachmentInput = new AttachmentCipherInputStream(attachmentFile, key);
|
|
|
|
|
|
|
|
database.updateDownloadedPart(messageId, partId, part, attachmentInput);
|
2013-11-19 10:13:24 -08:00
|
|
|
} catch (NotFoundException e) {
|
|
|
|
Log.w("PushDownloader", e);
|
|
|
|
try {
|
|
|
|
database.updateFailedDownloadedPart(messageId, partId, part);
|
|
|
|
} catch (MmsException mme) {
|
|
|
|
Log.w("PushDownloader", mme);
|
|
|
|
}
|
2013-09-08 18:19:05 -07:00
|
|
|
} catch (InvalidMessageException e) {
|
|
|
|
Log.w("PushDownloader", e);
|
|
|
|
try {
|
|
|
|
database.updateFailedDownloadedPart(messageId, partId, part);
|
|
|
|
} catch (MmsException mme) {
|
|
|
|
Log.w("PushDownloader", mme);
|
|
|
|
}
|
|
|
|
} catch (MmsException e) {
|
|
|
|
Log.w("PushDownloader", e);
|
|
|
|
try {
|
|
|
|
database.updateFailedDownloadedPart(messageId, partId, part);
|
|
|
|
} catch (MmsException mme) {
|
|
|
|
Log.w("PushDownloader", mme);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w("PushDownloader", e);
|
|
|
|
/// XXX schedule some kind of soft failure retry action
|
|
|
|
} finally {
|
|
|
|
if (attachmentFile != null)
|
|
|
|
attachmentFile.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-19 18:16:11 -07:00
|
|
|
private File downloadAttachment(String relay, long contentLocation) throws IOException {
|
2013-11-27 11:08:58 -08:00
|
|
|
PushServiceSocket socket = PushServiceSocketFactory.create(context);
|
2013-10-19 18:16:11 -07:00
|
|
|
return socket.retrieveAttachment(relay, contentLocation);
|
2013-09-08 18:19:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|