2014-11-03 15:16:04 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.graphics.Bitmap;
|
2019-06-05 15:47:14 -04:00
|
|
|
import androidx.annotation.NonNull;
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.GroupDatabase;
|
2017-08-07 16:47:38 -07:00
|
|
|
import org.thoughtcrime.securesms.database.GroupDatabase.GroupRecord;
|
2016-12-21 09:58:45 -08:00
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2019-03-28 08:56:35 -07:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.Data;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.Job;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
|
2018-08-01 11:09:24 -04:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2015-07-24 17:07:33 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.AttachmentStreamUriLoader.AttachmentModel;
|
2016-01-30 15:22:55 -08:00
|
|
|
import org.thoughtcrime.securesms.util.BitmapDecodingException;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.thoughtcrime.securesms.util.BitmapUtil;
|
2017-08-01 08:56:00 -07:00
|
|
|
import org.thoughtcrime.securesms.util.GroupUtil;
|
2017-02-26 10:06:27 -08:00
|
|
|
import org.thoughtcrime.securesms.util.Hex;
|
2016-12-20 09:55:52 -08:00
|
|
|
import org.whispersystems.libsignal.InvalidMessageException;
|
2017-02-26 10:06:27 -08:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2016-12-20 09:55:52 -08:00
|
|
|
import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException;
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2016-12-20 09:55:52 -08:00
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public class AvatarDownloadJob extends BaseJob implements InjectableType {
|
2018-08-09 10:15:43 -04:00
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public static final String KEY = "AvatarDownloadJob";
|
2016-12-20 09:55:52 -08:00
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
private static final String TAG = AvatarDownloadJob.class.getSimpleName();
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
private static final int MAX_AVATAR_SIZE = 20 * 1024 * 1024;
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
private static final String KEY_GROUP_ID = "group_id";
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
@Inject SignalServiceMessageReceiver receiver;
|
2016-12-20 09:55:52 -08:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
private byte[] groupId;
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public AvatarDownloadJob(@NonNull byte[] groupId) {
|
|
|
|
this(new Job.Parameters.Builder()
|
|
|
|
.addConstraint(NetworkConstraint.KEY)
|
|
|
|
.setMaxAttempts(10)
|
|
|
|
.build(),
|
|
|
|
groupId);
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
private AvatarDownloadJob(@NonNull Job.Parameters parameters, @NonNull byte[] groupId) {
|
|
|
|
super(parameters);
|
2014-11-03 15:16:04 -08:00
|
|
|
this.groupId = groupId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 08:56:35 -07:00
|
|
|
public @NonNull Data serialize() {
|
|
|
|
return new Data.Builder().putString(KEY_GROUP_ID, GroupUtil.getEncodedId(groupId, false)).build();
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 08:56:35 -07:00
|
|
|
public @NonNull String getFactoryKey() {
|
|
|
|
return KEY;
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
@Override
|
2018-11-15 12:05:08 -08:00
|
|
|
public void onRun() throws IOException {
|
2017-08-07 16:47:38 -07:00
|
|
|
String encodeId = GroupUtil.getEncodedId(groupId, false);
|
|
|
|
GroupDatabase database = DatabaseFactory.getGroupDatabase(context);
|
|
|
|
Optional<GroupRecord> record = database.getGroup(encodeId);
|
|
|
|
File attachment = null;
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
try {
|
2017-08-07 16:47:38 -07:00
|
|
|
if (record.isPresent()) {
|
|
|
|
long avatarId = record.get().getAvatarId();
|
|
|
|
String contentType = record.get().getAvatarContentType();
|
|
|
|
byte[] key = record.get().getAvatarKey();
|
|
|
|
String relay = record.get().getRelay();
|
|
|
|
Optional<byte[]> digest = Optional.fromNullable(record.get().getAvatarDigest());
|
2017-03-28 12:05:30 -07:00
|
|
|
Optional<String> fileName = Optional.absent();
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
if (avatarId == -1 || key == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-26 10:06:27 -08:00
|
|
|
if (digest.isPresent()) {
|
2018-08-02 09:25:33 -04:00
|
|
|
Log.i(TAG, "Downloading group avatar with digest: " + Hex.toString(digest.get()));
|
2017-02-26 10:06:27 -08:00
|
|
|
}
|
2016-12-20 09:55:52 -08:00
|
|
|
|
|
|
|
attachment = File.createTempFile("avatar", "tmp", context.getCacheDir());
|
|
|
|
attachment.deleteOnExit();
|
|
|
|
|
2018-11-09 12:17:14 -08:00
|
|
|
SignalServiceAttachmentPointer pointer = new SignalServiceAttachmentPointer(avatarId, contentType, key, Optional.of(0), Optional.absent(), 0, 0, digest, fileName, false, Optional.absent());
|
2017-03-09 15:04:16 -08:00
|
|
|
InputStream inputStream = receiver.retrieveAttachment(pointer, attachment, MAX_AVATAR_SIZE);
|
2017-11-01 15:56:31 -07:00
|
|
|
Bitmap avatar = BitmapUtil.createScaledBitmap(context, new AttachmentModel(attachment, key, 0, digest), 500, 500);
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2017-08-01 08:56:00 -07:00
|
|
|
database.updateAvatar(encodeId, avatar);
|
2016-12-20 09:55:52 -08:00
|
|
|
inputStream.close();
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
2016-12-20 09:55:52 -08:00
|
|
|
} catch (BitmapDecodingException | NonSuccessfulResponseCodeException | InvalidMessageException e) {
|
2014-11-03 15:16:04 -08:00
|
|
|
Log.w(TAG, e);
|
|
|
|
} finally {
|
|
|
|
if (attachment != null)
|
|
|
|
attachment.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {}
|
|
|
|
|
|
|
|
@Override
|
2019-05-22 13:51:56 -03:00
|
|
|
public boolean onShouldRetry(@NonNull Exception exception) {
|
2014-11-11 21:11:57 -08:00
|
|
|
if (exception instanceof IOException) return true;
|
2014-11-03 15:16:04 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public static final class Factory implements Job.Factory<AvatarDownloadJob> {
|
|
|
|
@Override
|
|
|
|
public @NonNull AvatarDownloadJob create(@NonNull Parameters parameters, @NonNull Data data) {
|
|
|
|
try {
|
|
|
|
return new AvatarDownloadJob(parameters, GroupUtil.getDecodedId(data.getString(KEY_GROUP_ID)));
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|