2017-08-14 18:11:13 -07:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
import android.app.Application;
|
2019-06-05 15:47:14 -04:00
|
|
|
import androidx.annotation.NonNull;
|
2017-08-14 18:11:13 -07:00
|
|
|
import android.text.TextUtils;
|
2018-08-09 10:15:43 -04:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
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;
|
2017-08-14 18:11:13 -07:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2017-08-21 18:37:39 -07:00
|
|
|
import org.thoughtcrime.securesms.database.RecipientDatabase;
|
2017-08-14 18:11:13 -07:00
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2017-08-15 21:03:31 -07:00
|
|
|
import org.thoughtcrime.securesms.profiles.AvatarHelper;
|
2017-08-14 18:11:13 -07:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
|
|
|
import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
|
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2019-03-28 08:56:35 -07:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2017-08-14 18:11:13 -07:00
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public class RetrieveProfileAvatarJob 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 = "RetrieveProfileAvatarJob";
|
2017-08-14 18:11:13 -07:00
|
|
|
|
|
|
|
private static final String TAG = RetrieveProfileAvatarJob.class.getSimpleName();
|
|
|
|
|
|
|
|
private static final int MAX_PROFILE_SIZE_BYTES = 20 * 1024 * 1024;
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
private static final String KEY_PROFILE_AVATAR = "profile_avatar";
|
|
|
|
private static final String KEY_ADDRESS = "address";
|
|
|
|
|
2017-08-14 18:11:13 -07:00
|
|
|
@Inject SignalServiceMessageReceiver receiver;
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
private String profileAvatar;
|
|
|
|
private Recipient recipient;
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public RetrieveProfileAvatarJob(Recipient recipient, String profileAvatar) {
|
|
|
|
this(new Job.Parameters.Builder()
|
|
|
|
.setQueue("RetrieveProfileAvatarJob" + recipient.getAddress().serialize())
|
|
|
|
.addConstraint(NetworkConstraint.KEY)
|
|
|
|
.setLifespan(TimeUnit.HOURS.toMillis(1))
|
|
|
|
.setMaxInstances(1)
|
|
|
|
.build(),
|
|
|
|
recipient,
|
|
|
|
profileAvatar);
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
2017-08-14 18:11:13 -07:00
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
private RetrieveProfileAvatarJob(@NonNull Job.Parameters parameters, @NonNull Recipient recipient, String profileAvatar) {
|
|
|
|
super(parameters);
|
2017-08-14 18:11:13 -07:00
|
|
|
|
|
|
|
this.recipient = recipient;
|
|
|
|
this.profileAvatar = profileAvatar;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 08:56:35 -07:00
|
|
|
public @NonNull Data serialize() {
|
|
|
|
return new Data.Builder().putString(KEY_PROFILE_AVATAR, profileAvatar)
|
|
|
|
.putString(KEY_ADDRESS, recipient.getAddress().serialize())
|
|
|
|
.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
|
|
|
}
|
2017-08-14 18:11:13 -07:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRun() throws IOException {
|
2017-08-22 10:44:04 -07:00
|
|
|
RecipientDatabase database = DatabaseFactory.getRecipientDatabase(context);
|
|
|
|
byte[] profileKey = recipient.resolve().getProfileKey();
|
2017-08-14 18:11:13 -07:00
|
|
|
|
2017-08-22 10:44:04 -07:00
|
|
|
if (profileKey == null) {
|
2017-08-14 18:11:13 -07:00
|
|
|
Log.w(TAG, "Recipient profile key is gone!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-22 10:44:04 -07:00
|
|
|
if (Util.equals(profileAvatar, recipient.resolve().getProfileAvatar())) {
|
2017-08-14 18:11:13 -07:00
|
|
|
Log.w(TAG, "Already retrieved profile avatar: " + profileAvatar);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(profileAvatar)) {
|
|
|
|
Log.w(TAG, "Removing profile avatar for: " + recipient.getAddress().serialize());
|
2017-08-15 21:03:31 -07:00
|
|
|
AvatarHelper.delete(context, recipient.getAddress());
|
2017-08-22 10:44:04 -07:00
|
|
|
database.setProfileAvatar(recipient, profileAvatar);
|
2017-08-14 18:11:13 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
File downloadDestination = File.createTempFile("avatar", "jpg", context.getCacheDir());
|
|
|
|
|
|
|
|
try {
|
2017-08-22 10:44:04 -07:00
|
|
|
InputStream avatarStream = receiver.retrieveProfileAvatar(profileAvatar, downloadDestination, profileKey, MAX_PROFILE_SIZE_BYTES);
|
2017-08-14 18:11:13 -07:00
|
|
|
File decryptDestination = File.createTempFile("avatar", "jpg", context.getCacheDir());
|
|
|
|
|
|
|
|
Util.copy(avatarStream, new FileOutputStream(decryptDestination));
|
2017-08-15 21:03:31 -07:00
|
|
|
decryptDestination.renameTo(AvatarHelper.getAvatarFile(context, recipient.getAddress()));
|
2017-08-14 18:11:13 -07:00
|
|
|
} finally {
|
|
|
|
if (downloadDestination != null) downloadDestination.delete();
|
|
|
|
}
|
|
|
|
|
2017-08-22 10:44:04 -07:00
|
|
|
database.setProfileAvatar(recipient, profileAvatar);
|
2017-08-14 18:11:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-22 13:51:56 -03:00
|
|
|
public boolean onShouldRetry(@NonNull Exception e) {
|
2017-08-14 18:11:13 -07:00
|
|
|
if (e instanceof PushNetworkException) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
2019-03-28 08:56:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public static final class Factory implements Job.Factory<RetrieveProfileAvatarJob> {
|
|
|
|
|
|
|
|
private final Application application;
|
2017-08-14 18:11:13 -07:00
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public Factory(Application application) {
|
|
|
|
this.application = application;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public @NonNull RetrieveProfileAvatarJob create(@NonNull Parameters parameters, @NonNull Data data) {
|
|
|
|
return new RetrieveProfileAvatarJob(parameters,
|
|
|
|
Recipient.from(application, Address.fromSerialized(data.getString(KEY_ADDRESS)), true),
|
|
|
|
data.getString(KEY_PROFILE_AVATAR));
|
|
|
|
}
|
2017-08-14 18:11:13 -07:00
|
|
|
}
|
|
|
|
}
|