2014-11-03 23:16:04 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Bitmap;
|
2017-02-26 18:06:27 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2014-11-03 23:16:04 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
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;
|
2016-12-21 17:58:45 +00:00
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
|
2015-07-25 00:07:33 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.AttachmentStreamUriLoader.AttachmentModel;
|
2016-01-30 23:22:55 +00:00
|
|
|
import org.thoughtcrime.securesms.util.BitmapDecodingException;
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.util.BitmapUtil;
|
2017-02-26 18:06:27 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Hex;
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
|
|
|
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
|
2016-12-20 17:55:52 +00:00
|
|
|
import org.whispersystems.libsignal.InvalidMessageException;
|
2017-02-26 18:06:27 +00:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2016-12-20 17:55:52 +00:00
|
|
|
import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer;
|
2016-03-23 17:34:41 +00:00
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException;
|
2014-11-03 23:16:04 +00:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2016-12-20 17:55:52 +00:00
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
2014-11-03 23:16:04 +00:00
|
|
|
|
2016-12-21 17:58:45 +00:00
|
|
|
public class AvatarDownloadJob extends MasterSecretJob implements InjectableType {
|
2014-11-03 23:16:04 +00:00
|
|
|
|
2017-03-09 23:04:16 +00:00
|
|
|
private static final int MAX_AVATAR_SIZE = 20 * 1024 * 1024;
|
2016-12-20 17:55:52 +00:00
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
2014-11-03 23:16:04 +00:00
|
|
|
private static final String TAG = AvatarDownloadJob.class.getSimpleName();
|
|
|
|
|
2016-12-20 17:55:52 +00:00
|
|
|
@Inject transient SignalServiceMessageReceiver receiver;
|
|
|
|
|
2014-11-03 23:16:04 +00:00
|
|
|
private final byte[] groupId;
|
|
|
|
|
2017-02-26 18:06:27 +00:00
|
|
|
public AvatarDownloadJob(Context context, @NonNull byte[] groupId) {
|
2014-11-03 23:16:04 +00:00
|
|
|
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) {
|
2017-02-26 18:06:27 +00:00
|
|
|
long avatarId = record.getAvatarId();
|
|
|
|
String contentType = record.getAvatarContentType();
|
|
|
|
byte[] key = record.getAvatarKey();
|
|
|
|
String relay = record.getRelay();
|
|
|
|
Optional<byte[]> digest = Optional.fromNullable(record.getAvatarDigest());
|
2017-03-28 19:05:30 +00:00
|
|
|
Optional<String> fileName = Optional.absent();
|
2014-11-03 23:16:04 +00:00
|
|
|
|
|
|
|
if (avatarId == -1 || key == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-26 18:06:27 +00:00
|
|
|
if (digest.isPresent()) {
|
|
|
|
Log.w(TAG, "Downloading group avatar with digest: " + Hex.toString(digest.get()));
|
|
|
|
}
|
2016-12-20 17:55:52 +00:00
|
|
|
|
|
|
|
attachment = File.createTempFile("avatar", "tmp", context.getCacheDir());
|
|
|
|
attachment.deleteOnExit();
|
|
|
|
|
2017-05-12 05:46:35 +00:00
|
|
|
SignalServiceAttachmentPointer pointer = new SignalServiceAttachmentPointer(avatarId, contentType, key, relay, digest, fileName, false);
|
2017-03-09 23:04:16 +00:00
|
|
|
InputStream inputStream = receiver.retrieveAttachment(pointer, attachment, MAX_AVATAR_SIZE);
|
2016-12-20 17:55:52 +00:00
|
|
|
Bitmap avatar = BitmapUtil.createScaledBitmap(context, new AttachmentModel(attachment, key), 500, 500);
|
2014-11-03 23:16:04 +00:00
|
|
|
|
|
|
|
database.updateAvatar(groupId, avatar);
|
2016-12-20 17:55:52 +00:00
|
|
|
inputStream.close();
|
2014-11-03 23:16:04 +00:00
|
|
|
}
|
2016-12-20 17:55:52 +00:00
|
|
|
} catch (BitmapDecodingException | NonSuccessfulResponseCodeException | InvalidMessageException e) {
|
2014-11-03 23:16:04 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|