session-android/app/src/main/java/org/thoughtcrime/securesms/jobs/RetrieveProfileAvatarJob.java

142 lines
4.8 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms.jobs;
2019-03-28 16:56:35 +01:00
import android.app.Application;
import androidx.annotation.NonNull;
import android.text.TextUtils;
import org.session.libsession.messaging.jobs.Data;
2021-01-13 07:11:30 +01:00
import org.session.libsession.messaging.threads.Address;
import org.session.libsession.messaging.threads.recipients.Recipient;
2021-02-02 05:40:43 +01:00
import org.session.libsession.messaging.avatars.AvatarHelper;
2021-01-13 07:11:30 +01:00
import org.session.libsession.utilities.Util;
2019-03-28 16:56:35 +01:00
import org.thoughtcrime.securesms.jobmanager.Job;
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
2021-02-03 02:22:40 +01:00
import org.session.libsignal.utilities.logging.Log;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.RecipientDatabase;
import org.thoughtcrime.securesms.dependencies.InjectableType;
import org.session.libsignal.service.api.SignalServiceMessageReceiver;
import org.session.libsignal.service.api.push.exceptions.PushNetworkException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
2019-03-28 16:56:35 +01:00
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
2019-03-28 16:56:35 +01:00
public class RetrieveProfileAvatarJob extends BaseJob implements InjectableType {
2019-03-28 16:56:35 +01:00
public static final String KEY = "RetrieveProfileAvatarJob";
private static final String TAG = RetrieveProfileAvatarJob.class.getSimpleName();
private static final int MAX_PROFILE_SIZE_BYTES = 20 * 1024 * 1024;
private static final String KEY_PROFILE_AVATAR = "profile_avatar";
private static final String KEY_ADDRESS = "address";
@Inject SignalServiceMessageReceiver receiver;
private String profileAvatar;
private Recipient recipient;
2019-03-28 16:56:35 +01: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))
.setMaxAttempts(3)
2019-03-28 16:56:35 +01:00
.build(),
recipient,
profileAvatar);
}
2019-03-28 16:56:35 +01:00
private RetrieveProfileAvatarJob(@NonNull Job.Parameters parameters, @NonNull Recipient recipient, String profileAvatar) {
super(parameters);
this.recipient = recipient;
this.profileAvatar = profileAvatar;
}
@Override
public @NonNull
Data serialize() {
2019-03-28 16:56:35 +01:00
return new Data.Builder().putString(KEY_PROFILE_AVATAR, profileAvatar)
.putString(KEY_ADDRESS, recipient.getAddress().serialize())
.build();
}
@Override
2019-03-28 16:56:35 +01:00
public @NonNull String getFactoryKey() {
return KEY;
}
@Override
public void onRun() throws IOException {
RecipientDatabase database = DatabaseFactory.getRecipientDatabase(context);
byte[] profileKey = recipient.resolve().getProfileKey();
if (profileKey == null) {
Log.w(TAG, "Recipient profile key is gone!");
return;
}
if (Util.equals(profileAvatar, recipient.resolve().getProfileAvatar())) {
Log.w(TAG, "Already retrieved profile avatar: " + profileAvatar);
return;
}
if (TextUtils.isEmpty(profileAvatar)) {
Log.w(TAG, "Removing profile avatar for: " + recipient.getAddress().serialize());
AvatarHelper.delete(context, recipient.getAddress());
database.setProfileAvatar(recipient, profileAvatar);
return;
}
File downloadDestination = File.createTempFile("avatar", ".jpg", context.getCacheDir());
try {
2020-05-22 01:53:04 +02:00
InputStream avatarStream = receiver.retrieveProfileAvatar(profileAvatar, downloadDestination, profileKey, MAX_PROFILE_SIZE_BYTES);
File decryptDestination = File.createTempFile("avatar", ".jpg", context.getCacheDir());
Util.copy(avatarStream, new FileOutputStream(decryptDestination));
decryptDestination.renameTo(AvatarHelper.getAvatarFile(context, recipient.getAddress()));
} finally {
if (downloadDestination != null) downloadDestination.delete();
}
database.setProfileAvatar(recipient, profileAvatar);
}
@Override
public boolean onShouldRetry(@NonNull Exception e) {
if (e instanceof PushNetworkException) return true;
return false;
}
@Override
public void onCanceled() {
2019-03-28 16:56:35 +01:00
}
public static final class Factory implements Job.Factory<RetrieveProfileAvatarJob> {
private final Application application;
2019-03-28 16:56:35 +01:00
public Factory(Application application) {
this.application = application;
}
@Override
public @NonNull RetrieveProfileAvatarJob create(@NonNull Parameters parameters, @NonNull Data data) {
return new RetrieveProfileAvatarJob(parameters,
2021-02-17 06:09:36 +01:00
Recipient.from(application, Address.fromSerialized(data.getString(KEY_ADDRESS)), true),
2019-03-28 16:56:35 +01:00
data.getString(KEY_PROFILE_AVATAR));
}
}
}