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;
|
|
|
|
|
2015-05-15 01:14:42 +00:00
|
|
|
import org.thoughtcrime.securesms.BuildConfig;
|
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;
|
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
|
2015-07-25 00:07:33 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.AttachmentStreamUriLoader.AttachmentModel;
|
2014-11-10 04:35:08 +00:00
|
|
|
import org.thoughtcrime.securesms.push.TextSecurePushTrustStore;
|
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;
|
2014-11-10 04:35:08 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
|
|
|
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
|
2016-03-23 17:34:41 +00:00
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException;
|
|
|
|
import org.whispersystems.signalservice.internal.push.PushServiceSocket;
|
|
|
|
import org.whispersystems.signalservice.internal.util.StaticCredentialsProvider;
|
2014-11-03 23:16:04 +00:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2015-07-25 00:07:33 +00:00
|
|
|
import java.util.concurrent.ExecutionException;
|
2014-11-03 23:16:04 +00:00
|
|
|
|
|
|
|
public class AvatarDownloadJob extends MasterSecretJob {
|
|
|
|
|
|
|
|
private static final String TAG = AvatarDownloadJob.class.getSimpleName();
|
|
|
|
|
|
|
|
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) {
|
|
|
|
long avatarId = record.getAvatarId();
|
|
|
|
byte[] key = record.getAvatarKey();
|
|
|
|
String relay = record.getRelay();
|
|
|
|
|
|
|
|
if (avatarId == -1 || key == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
attachment = downloadAttachment(relay, avatarId);
|
2015-07-25 00:07:33 +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-01-30 23:22:55 +00:00
|
|
|
} catch (BitmapDecodingException | NonSuccessfulResponseCodeException 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private File downloadAttachment(String relay, long contentLocation) throws IOException {
|
2015-09-09 20:54:29 +00:00
|
|
|
PushServiceSocket socket = new PushServiceSocket(BuildConfig.TEXTSECURE_URL,
|
2014-11-10 04:35:08 +00:00
|
|
|
new TextSecurePushTrustStore(context),
|
2015-01-26 01:43:24 +00:00
|
|
|
new StaticCredentialsProvider(TextSecurePreferences.getLocalNumber(context),
|
|
|
|
TextSecurePreferences.getPushServerPassword(context),
|
2015-09-09 20:54:29 +00:00
|
|
|
null),
|
|
|
|
BuildConfig.USER_AGENT);
|
2014-11-10 04:35:08 +00:00
|
|
|
|
|
|
|
File destination = File.createTempFile("avatar", "tmp");
|
2014-11-03 23:16:04 +00:00
|
|
|
|
|
|
|
destination.deleteOnExit();
|
|
|
|
|
2015-06-27 03:14:51 +00:00
|
|
|
socket.retrieveAttachment(relay, contentLocation, destination, null);
|
2014-11-03 23:16:04 +00:00
|
|
|
|
|
|
|
return destination;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|