2014-11-03 15:16:04 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.util.Log;
|
|
|
|
|
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;
|
2016-12-21 09:58:45 -08:00
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
|
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;
|
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
|
|
|
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
|
2016-12-20 09:55:52 -08:00
|
|
|
import org.whispersystems.libsignal.InvalidMessageException;
|
|
|
|
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
|
|
|
|
2016-12-21 09:58:45 -08:00
|
|
|
public class AvatarDownloadJob extends MasterSecretJob implements InjectableType {
|
2014-11-03 15:16:04 -08:00
|
|
|
|
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();
|
|
|
|
|
2016-12-20 09:55:52 -08:00
|
|
|
@Inject transient SignalServiceMessageReceiver receiver;
|
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
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-11 19:57:53 -08:00
|
|
|
public void onRun(MasterSecret masterSecret) throws IOException {
|
2014-11-03 15:16:04 -08:00
|
|
|
GroupDatabase database = DatabaseFactory.getGroupDatabase(context);
|
|
|
|
GroupDatabase.GroupRecord record = database.getGroup(groupId);
|
|
|
|
File attachment = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (record != null) {
|
2016-12-20 09:55:52 -08:00
|
|
|
long avatarId = record.getAvatarId();
|
|
|
|
String contentType = record.getAvatarContentType();
|
|
|
|
byte[] key = record.getAvatarKey();
|
|
|
|
String relay = record.getRelay();
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
if (avatarId == -1 || key == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-20 09:55:52 -08:00
|
|
|
|
|
|
|
|
|
|
|
attachment = File.createTempFile("avatar", "tmp", context.getCacheDir());
|
|
|
|
attachment.deleteOnExit();
|
|
|
|
|
|
|
|
SignalServiceAttachmentPointer pointer = new SignalServiceAttachmentPointer(avatarId, contentType, key, relay);
|
|
|
|
InputStream inputStream = receiver.retrieveAttachment(pointer, attachment);
|
|
|
|
Bitmap avatar = BitmapUtil.createScaledBitmap(context, new AttachmentModel(attachment, key), 500, 500);
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
database.updateAvatar(groupId, 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
|
2014-11-11 21:11:57 -08:00
|
|
|
public boolean onShouldRetryThrowable(Exception exception) {
|
|
|
|
if (exception instanceof IOException) return true;
|
2014-11-03 15:16:04 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|