2014-11-03 15:16:04 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Bitmap;
|
2017-02-26 10:06:27 -08:00
|
|
|
import android.support.annotation.NonNull;
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
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;
|
2018-06-18 12:27:04 -07:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.JobParameters;
|
2018-08-09 10:15:43 -04:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.SafeData;
|
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
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
import androidx.work.Data;
|
|
|
|
|
2018-11-15 12:05:08 -08:00
|
|
|
public class AvatarDownloadJob extends ContextJob implements InjectableType {
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2017-03-09 15:04:16 -08:00
|
|
|
private static final int MAX_AVATAR_SIZE = 20 * 1024 * 1024;
|
2016-12-20 09:55:52 -08:00
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
private static final String TAG = AvatarDownloadJob.class.getSimpleName();
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
private static final String KEY_GROUP_ID = "group_id";
|
|
|
|
|
2016-12-20 09:55:52 -08:00
|
|
|
@Inject transient SignalServiceMessageReceiver receiver;
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
private byte[] groupId;
|
|
|
|
|
|
|
|
public AvatarDownloadJob() {
|
|
|
|
super(null, null);
|
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2017-02-26 10:06:27 -08:00
|
|
|
public AvatarDownloadJob(Context context, @NonNull byte[] groupId) {
|
2014-11-03 15:16:04 -08:00
|
|
|
super(context, JobParameters.newBuilder()
|
2018-08-09 10:15:43 -04:00
|
|
|
.withNetworkRequirement()
|
2014-11-03 15:16:04 -08:00
|
|
|
.create());
|
|
|
|
|
|
|
|
this.groupId = groupId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-09 10:15:43 -04:00
|
|
|
protected void initialize(@NonNull SafeData data) {
|
|
|
|
try {
|
|
|
|
groupId = GroupUtil.getDecodedId(data.getString(KEY_GROUP_ID));
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected @NonNull Data serialize(@NonNull Data.Builder dataBuilder) {
|
|
|
|
return dataBuilder.putString(KEY_GROUP_ID, GroupUtil.getEncodedId(groupId, false)).build();
|
|
|
|
}
|
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
|
2018-11-15 12:05:08 -08:00
|
|
|
public boolean onShouldRetry(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|