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;
|
|
|
|
|
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;
|
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
|
|
|
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
|
2016-12-20 17:55:52 +00:00
|
|
|
import org.whispersystems.libsignal.InvalidMessageException;
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
|
|
|
|
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) {
|
2016-12-20 17:55:52 +00:00
|
|
|
long avatarId = record.getAvatarId();
|
|
|
|
String contentType = record.getAvatarContentType();
|
|
|
|
byte[] key = record.getAvatarKey();
|
|
|
|
String relay = record.getRelay();
|
2014-11-03 23:16:04 +00:00
|
|
|
|
|
|
|
if (avatarId == -1 || key == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-20 17:55:52 +00: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 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|