2014-11-03 15:16:04 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.util.Pair;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterCipher;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.EncryptingPartDatabase;
|
|
|
|
import org.thoughtcrime.securesms.database.PartDatabase;
|
2014-11-11 19:57:53 -08:00
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
|
2014-11-12 11:15:05 -08:00
|
|
|
import org.thoughtcrime.securesms.util.Base64;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
|
|
|
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
|
|
|
|
import org.whispersystems.libaxolotl.InvalidMessageException;
|
|
|
|
import org.whispersystems.textsecure.api.TextSecureMessageReceiver;
|
|
|
|
import org.whispersystems.textsecure.api.messages.TextSecureAttachmentPointer;
|
|
|
|
import org.whispersystems.textsecure.push.exceptions.NonSuccessfulResponseCodeException;
|
|
|
|
import org.whispersystems.textsecure.push.exceptions.PushNetworkException;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.List;
|
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
import ws.com.google.android.mms.MmsException;
|
|
|
|
import ws.com.google.android.mms.pdu.PduPart;
|
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
public class AttachmentDownloadJob extends MasterSecretJob implements InjectableType {
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
private static final String TAG = AttachmentDownloadJob.class.getSimpleName();
|
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
@Inject transient TextSecureMessageReceiver messageReceiver;
|
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
private final long messageId;
|
|
|
|
|
|
|
|
public AttachmentDownloadJob(Context context, long messageId) {
|
|
|
|
super(context, JobParameters.newBuilder()
|
|
|
|
.withRequirement(new MasterSecretRequirement(context))
|
|
|
|
.withRequirement(new NetworkRequirement(context))
|
|
|
|
.withPersistence()
|
|
|
|
.create());
|
|
|
|
|
|
|
|
this.messageId = messageId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAdded() {}
|
|
|
|
|
|
|
|
@Override
|
2014-11-11 19:57:53 -08:00
|
|
|
public void onRun(MasterSecret masterSecret) throws IOException {
|
|
|
|
PartDatabase database = DatabaseFactory.getEncryptingPartDatabase(context, masterSecret);
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
Log.w(TAG, "Downloading push parts for: " + messageId);
|
|
|
|
|
|
|
|
List<Pair<Long, PduPart>> parts = database.getParts(messageId, false);
|
|
|
|
|
|
|
|
for (Pair<Long, PduPart> partPair : parts) {
|
|
|
|
retrievePart(masterSecret, partPair.second, messageId, partPair.first);
|
|
|
|
Log.w(TAG, "Got part: " + partPair.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
2014-11-11 19:57:53 -08:00
|
|
|
PartDatabase database = DatabaseFactory.getPartDatabase(context);
|
|
|
|
List<Pair<Long, PduPart>> parts = database.getParts(messageId, false);
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
for (Pair<Long, PduPart> partPair : parts) {
|
|
|
|
markFailed(messageId, partPair.second, partPair.first);
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-11 21:11:57 -08:00
|
|
|
public boolean onShouldRetryThrowable(Exception exception) {
|
|
|
|
if (exception instanceof PushNetworkException) return true;
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void retrievePart(MasterSecret masterSecret, PduPart part, long messageId, long partId)
|
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
EncryptingPartDatabase database = DatabaseFactory.getEncryptingPartDatabase(context, masterSecret);
|
|
|
|
File attachmentFile = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
attachmentFile = createTempFile();
|
2014-11-09 20:35:08 -08:00
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
TextSecureAttachmentPointer pointer = createAttachmentPointer(masterSecret, part);
|
2014-11-11 19:57:53 -08:00
|
|
|
InputStream attachment = messageReceiver.retrieveAttachment(pointer, attachmentFile);
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
database.updateDownloadedPart(messageId, partId, part, attachment);
|
|
|
|
} catch (InvalidPartException | NonSuccessfulResponseCodeException | InvalidMessageException | MmsException e) {
|
|
|
|
Log.w(TAG, e);
|
2014-11-11 19:57:53 -08:00
|
|
|
markFailed(messageId, part, partId);
|
2014-11-03 15:16:04 -08:00
|
|
|
} finally {
|
|
|
|
if (attachmentFile != null)
|
|
|
|
attachmentFile.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private TextSecureAttachmentPointer createAttachmentPointer(MasterSecret masterSecret, PduPart part)
|
|
|
|
throws InvalidPartException
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
MasterCipher masterCipher = new MasterCipher(masterSecret);
|
|
|
|
long id = Long.parseLong(Util.toIsoString(part.getContentLocation()));
|
|
|
|
byte[] key = masterCipher.decryptBytes(Base64.decode(Util.toIsoString(part.getContentDisposition())));
|
|
|
|
String relay = null;
|
|
|
|
|
|
|
|
if (part.getName() != null) {
|
|
|
|
relay = Util.toIsoString(part.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
return new TextSecureAttachmentPointer(id, null, key, relay);
|
|
|
|
} catch (InvalidMessageException | IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
throw new InvalidPartException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private File createTempFile() throws InvalidPartException {
|
|
|
|
try {
|
|
|
|
File file = File.createTempFile("push-attachment", "tmp");
|
|
|
|
file.deleteOnExit();
|
|
|
|
|
|
|
|
return file;
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new InvalidPartException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
private void markFailed(long messageId, PduPart part, long partId) {
|
2014-11-03 15:16:04 -08:00
|
|
|
try {
|
2014-11-11 19:57:53 -08:00
|
|
|
PartDatabase database = DatabaseFactory.getPartDatabase(context);
|
2014-11-03 15:16:04 -08:00
|
|
|
database.updateFailedDownloadedPart(messageId, partId, part);
|
|
|
|
} catch (MmsException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class InvalidPartException extends Exception {
|
|
|
|
public InvalidPartException(Exception e) {super(e);}
|
|
|
|
}
|
|
|
|
}
|