2017-08-15 01:11:13 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
|
|
|
|
import android.text.TextUtils;
|
2018-08-09 14:15:43 +00:00
|
|
|
|
2020-02-10 23:40:22 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
2020-12-04 23:31:58 +00:00
|
|
|
import org.signal.core.util.logging.Log;
|
2022-03-24 17:23:23 +00:00
|
|
|
import org.signal.libsignal.zkgroup.profiles.ProfileKey;
|
2020-02-10 23:40:22 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.ProfileKeyUtil;
|
2019-06-14 19:32:23 +00:00
|
|
|
import org.thoughtcrime.securesms.database.RecipientDatabase;
|
2021-11-18 17:36:52 +00:00
|
|
|
import org.thoughtcrime.securesms.database.SignalDatabase;
|
2019-07-15 15:12:26 +00:00
|
|
|
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
2019-03-28 15:56:35 +00:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.Data;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.Job;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
|
2021-07-22 16:28:03 +00:00
|
|
|
import org.thoughtcrime.securesms.keyvalue.SignalStore;
|
2017-08-16 04:03:31 +00:00
|
|
|
import org.thoughtcrime.securesms.profiles.AvatarHelper;
|
2017-08-15 01:11:13 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2019-08-07 18:22:51 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientId;
|
2017-08-15 01:11:13 +00:00
|
|
|
import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
|
2019-06-14 19:32:23 +00:00
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException;
|
2017-08-15 01:11:13 +00:00
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2019-03-28 15:56:35 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2017-08-15 01:11:13 +00:00
|
|
|
|
2019-07-15 15:12:26 +00:00
|
|
|
public class RetrieveProfileAvatarJob extends BaseJob {
|
2018-08-09 14:15:43 +00:00
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
public static final String KEY = "RetrieveProfileAvatarJob";
|
2017-08-15 01:11:13 +00:00
|
|
|
|
2021-03-29 22:37:22 +00:00
|
|
|
private static final String TAG = Log.tag(RetrieveProfileAvatarJob.class);
|
2017-08-15 01:11:13 +00:00
|
|
|
|
|
|
|
private static final int MAX_PROFILE_SIZE_BYTES = 20 * 1024 * 1024;
|
|
|
|
|
2018-08-09 14:15:43 +00:00
|
|
|
private static final String KEY_PROFILE_AVATAR = "profile_avatar";
|
2019-08-07 18:22:51 +00:00
|
|
|
private static final String KEY_RECIPIENT = "recipient";
|
2018-08-09 14:15:43 +00:00
|
|
|
|
2020-02-10 23:40:22 +00:00
|
|
|
private final String profileAvatar;
|
|
|
|
private final Recipient recipient;
|
2018-08-09 14:15:43 +00:00
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
public RetrieveProfileAvatarJob(Recipient recipient, String profileAvatar) {
|
|
|
|
this(new Job.Parameters.Builder()
|
2019-08-07 18:22:51 +00:00
|
|
|
.setQueue("RetrieveProfileAvatarJob::" + recipient.getId().toQueueKey())
|
2019-03-28 15:56:35 +00:00
|
|
|
.addConstraint(NetworkConstraint.KEY)
|
|
|
|
.setLifespan(TimeUnit.HOURS.toMillis(1))
|
|
|
|
.build(),
|
|
|
|
recipient,
|
|
|
|
profileAvatar);
|
2018-08-09 14:15:43 +00:00
|
|
|
}
|
2017-08-15 01:11:13 +00:00
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
private RetrieveProfileAvatarJob(@NonNull Job.Parameters parameters, @NonNull Recipient recipient, String profileAvatar) {
|
|
|
|
super(parameters);
|
2017-08-15 01:11:13 +00:00
|
|
|
|
|
|
|
this.recipient = recipient;
|
|
|
|
this.profileAvatar = profileAvatar;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 15:56:35 +00:00
|
|
|
public @NonNull Data serialize() {
|
|
|
|
return new Data.Builder().putString(KEY_PROFILE_AVATAR, profileAvatar)
|
2019-08-07 18:22:51 +00:00
|
|
|
.putString(KEY_RECIPIENT, recipient.getId().serialize())
|
2019-03-28 15:56:35 +00:00
|
|
|
.build();
|
2018-08-09 14:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 15:56:35 +00:00
|
|
|
public @NonNull String getFactoryKey() {
|
|
|
|
return KEY;
|
2018-08-09 14:15:43 +00:00
|
|
|
}
|
2017-08-15 01:11:13 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRun() throws IOException {
|
2021-11-18 17:36:52 +00:00
|
|
|
RecipientDatabase database = SignalDatabase.recipients();
|
2020-02-10 23:40:22 +00:00
|
|
|
ProfileKey profileKey = ProfileKeyUtil.profileKeyOrNull(recipient.resolve().getProfileKey());
|
2017-08-15 01:11:13 +00:00
|
|
|
|
2017-08-22 17:44:04 +00:00
|
|
|
if (profileKey == null) {
|
2017-08-15 01:11:13 +00:00
|
|
|
Log.w(TAG, "Recipient profile key is gone!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-11 23:35:43 +00:00
|
|
|
if (profileAvatar != null && profileAvatar.equals(recipient.resolve().getProfileAvatar())) {
|
2017-08-15 01:11:13 +00:00
|
|
|
Log.w(TAG, "Already retrieved profile avatar: " + profileAvatar);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(profileAvatar)) {
|
2022-03-11 23:35:43 +00:00
|
|
|
if (AvatarHelper.hasAvatar(context, recipient.getId())) {
|
|
|
|
Log.w(TAG, "Removing profile avatar (no url) for: " + recipient.getId().serialize());
|
|
|
|
AvatarHelper.delete(context, recipient.getId());
|
|
|
|
database.setProfileAvatar(recipient.getId(), profileAvatar);
|
|
|
|
}
|
|
|
|
|
2017-08-15 01:11:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-11 23:35:43 +00:00
|
|
|
File downloadDestination = File.createTempFile("avatar", "jpg", context.getCacheDir());
|
2017-08-15 01:11:13 +00:00
|
|
|
|
|
|
|
try {
|
2020-03-26 19:38:27 +00:00
|
|
|
SignalServiceMessageReceiver receiver = ApplicationDependencies.getSignalServiceMessageReceiver();
|
|
|
|
InputStream avatarStream = receiver.retrieveProfileAvatar(profileAvatar, downloadDestination, profileKey, AvatarHelper.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE);
|
2017-08-15 01:11:13 +00:00
|
|
|
|
2020-03-29 22:53:30 +00:00
|
|
|
try {
|
|
|
|
AvatarHelper.setAvatar(context, recipient.getId(), avatarStream);
|
2021-07-22 16:28:03 +00:00
|
|
|
|
|
|
|
if (recipient.isSelf()) {
|
|
|
|
SignalStore.misc().markHasEverHadAnAvatar();
|
|
|
|
}
|
2020-03-29 22:53:30 +00:00
|
|
|
} catch (AssertionError e) {
|
|
|
|
throw new IOException("Failed to copy stream. Likely a Conscrypt issue.", e);
|
|
|
|
}
|
2019-06-14 19:32:23 +00:00
|
|
|
} catch (PushNetworkException e) {
|
|
|
|
if (e.getCause() instanceof NonSuccessfulResponseCodeException) {
|
2019-09-07 03:40:06 +00:00
|
|
|
Log.w(TAG, "Removing profile avatar (no image available) for: " + recipient.getId().serialize());
|
2019-09-23 15:37:01 +00:00
|
|
|
AvatarHelper.delete(context, recipient.getId());
|
2019-06-14 19:32:23 +00:00
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
2017-08-15 01:11:13 +00:00
|
|
|
} finally {
|
|
|
|
if (downloadDestination != null) downloadDestination.delete();
|
|
|
|
}
|
|
|
|
|
2019-08-07 18:22:51 +00:00
|
|
|
database.setProfileAvatar(recipient.getId(), profileAvatar);
|
2017-08-15 01:11:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-22 16:51:56 +00:00
|
|
|
public boolean onShouldRetry(@NonNull Exception e) {
|
2017-08-15 01:11:13 +00:00
|
|
|
if (e instanceof PushNetworkException) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-01-03 19:10:16 +00:00
|
|
|
public void onFailure() {
|
2019-03-28 15:56:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static final class Factory implements Job.Factory<RetrieveProfileAvatarJob> {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public @NonNull RetrieveProfileAvatarJob create(@NonNull Parameters parameters, @NonNull Data data) {
|
|
|
|
return new RetrieveProfileAvatarJob(parameters,
|
2019-08-07 18:22:51 +00:00
|
|
|
Recipient.resolved(RecipientId.from(data.getString(KEY_RECIPIENT))),
|
2019-03-28 15:56:35 +00:00
|
|
|
data.getString(KEY_PROFILE_AVATAR));
|
|
|
|
}
|
2017-08-15 01:11:13 +00:00
|
|
|
}
|
|
|
|
}
|