2014-11-03 23:16:04 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2015-05-15 01:14:42 +00:00
|
|
|
import org.thoughtcrime.securesms.BuildConfig;
|
2014-11-12 03:57:53 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.GroupDatabase;
|
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
|
2014-11-10 04:35:08 +00:00
|
|
|
import org.thoughtcrime.securesms.push.TextSecurePushTrustStore;
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.util.BitmapDecodingException;
|
|
|
|
import org.thoughtcrime.securesms.util.BitmapUtil;
|
2014-11-10 04:35:08 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
|
|
|
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
|
|
|
|
import org.whispersystems.libaxolotl.InvalidMessageException;
|
2014-11-08 21:37:57 +00:00
|
|
|
import org.whispersystems.textsecure.api.crypto.AttachmentCipherInputStream;
|
2014-11-12 19:35:54 +00:00
|
|
|
import org.whispersystems.textsecure.api.push.exceptions.NonSuccessfulResponseCodeException;
|
2015-06-09 14:37:20 +00:00
|
|
|
import org.whispersystems.textsecure.internal.push.PushServiceSocket;
|
2015-01-26 01:43:24 +00:00
|
|
|
import org.whispersystems.textsecure.internal.util.StaticCredentialsProvider;
|
2014-11-03 23:16:04 +00:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
public class AvatarDownloadJob extends MasterSecretJob {
|
|
|
|
|
|
|
|
private static final String TAG = AvatarDownloadJob.class.getSimpleName();
|
|
|
|
|
|
|
|
private final byte[] groupId;
|
|
|
|
|
|
|
|
public AvatarDownloadJob(Context context, byte[] groupId) {
|
|
|
|
super(context, JobParameters.newBuilder()
|
|
|
|
.withRequirement(new MasterSecretRequirement(context))
|
|
|
|
.withRequirement(new NetworkRequirement(context))
|
|
|
|
.withPersistence()
|
|
|
|
.create());
|
|
|
|
|
|
|
|
this.groupId = groupId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAdded() {}
|
|
|
|
|
|
|
|
@Override
|
2014-11-12 03:57:53 +00:00
|
|
|
public void onRun(MasterSecret masterSecret) throws IOException {
|
2014-11-03 23:16:04 +00:00
|
|
|
GroupDatabase database = DatabaseFactory.getGroupDatabase(context);
|
|
|
|
GroupDatabase.GroupRecord record = database.getGroup(groupId);
|
|
|
|
File attachment = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (record != null) {
|
|
|
|
long avatarId = record.getAvatarId();
|
|
|
|
byte[] key = record.getAvatarKey();
|
|
|
|
String relay = record.getRelay();
|
|
|
|
|
|
|
|
if (avatarId == -1 || key == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
attachment = downloadAttachment(relay, avatarId);
|
|
|
|
|
|
|
|
InputStream scaleInputStream = new AttachmentCipherInputStream(attachment, key);
|
|
|
|
InputStream measureInputStream = new AttachmentCipherInputStream(attachment, key);
|
|
|
|
Bitmap avatar = BitmapUtil.createScaledBitmap(measureInputStream, scaleInputStream, 500, 500);
|
|
|
|
|
|
|
|
database.updateAvatar(groupId, avatar);
|
|
|
|
}
|
|
|
|
} catch (InvalidMessageException | BitmapDecodingException | NonSuccessfulResponseCodeException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
} finally {
|
|
|
|
if (attachment != null)
|
|
|
|
attachment.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {}
|
|
|
|
|
|
|
|
@Override
|
2014-11-12 05:11:57 +00:00
|
|
|
public boolean onShouldRetryThrowable(Exception exception) {
|
|
|
|
if (exception instanceof IOException) return true;
|
2014-11-03 23:16:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private File downloadAttachment(String relay, long contentLocation) throws IOException {
|
2015-05-15 01:14:42 +00:00
|
|
|
PushServiceSocket socket = new PushServiceSocket(BuildConfig.PUSH_URL,
|
2014-11-10 04:35:08 +00:00
|
|
|
new TextSecurePushTrustStore(context),
|
2015-01-26 01:43:24 +00:00
|
|
|
new StaticCredentialsProvider(TextSecurePreferences.getLocalNumber(context),
|
|
|
|
TextSecurePreferences.getPushServerPassword(context),
|
|
|
|
null));
|
2014-11-10 04:35:08 +00:00
|
|
|
|
|
|
|
File destination = File.createTempFile("avatar", "tmp");
|
2014-11-03 23:16:04 +00:00
|
|
|
|
|
|
|
destination.deleteOnExit();
|
|
|
|
|
|
|
|
socket.retrieveAttachment(relay, contentLocation, destination);
|
|
|
|
|
|
|
|
return destination;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|