Support for retrieving and storing profile information
Initial support for sharing profile keys // FREEBIEpull/1/head
parent
1893047a78
commit
77a216b705
@ -0,0 +1,109 @@
|
||||
package org.thoughtcrime.securesms.jobs;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase;
|
||||
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.RecipientsPreferences;
|
||||
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
import org.whispersystems.jobqueue.JobParameters;
|
||||
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
|
||||
import org.whispersystems.libsignal.util.guava.Optional;
|
||||
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;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class RetrieveProfileAvatarJob extends ContextJob implements InjectableType {
|
||||
|
||||
private static final String TAG = RetrieveProfileAvatarJob.class.getSimpleName();
|
||||
|
||||
private static final int MAX_PROFILE_SIZE_BYTES = 20 * 1024 * 1024;
|
||||
|
||||
@Inject SignalServiceMessageReceiver receiver;
|
||||
|
||||
private final String profileAvatar;
|
||||
private final Recipient recipient;
|
||||
|
||||
public RetrieveProfileAvatarJob(Context context, Recipient recipient, String profileAvatar) {
|
||||
super(context, JobParameters.newBuilder()
|
||||
.withGroupId(RetrieveProfileAvatarJob.class.getSimpleName() + recipient.getAddress().serialize())
|
||||
.withRequirement(new NetworkRequirement(context))
|
||||
.create());
|
||||
|
||||
this.recipient = recipient;
|
||||
this.profileAvatar = profileAvatar;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAdded() {}
|
||||
|
||||
@Override
|
||||
public void onRun() throws IOException {
|
||||
RecipientPreferenceDatabase database = DatabaseFactory.getRecipientPreferenceDatabase(context);
|
||||
Optional<RecipientsPreferences> recipientsPreferences = database.getRecipientsPreferences(recipient.getAddress());
|
||||
File avatarDirectory = new File(context.getFilesDir(), "avatars");
|
||||
File avatarFile = new File(avatarDirectory, new File(recipient.getAddress().serialize()).getName());
|
||||
|
||||
avatarDirectory.mkdirs();
|
||||
|
||||
if (!recipientsPreferences.isPresent()) {
|
||||
Log.w(TAG, "Recipient preference row is gone!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (recipientsPreferences.get().getProfileKey() == null) {
|
||||
Log.w(TAG, "Recipient profile key is gone!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (Util.equals(profileAvatar, recipientsPreferences.get().getProfileAvatar())) {
|
||||
Log.w(TAG, "Already retrieved profile avatar: " + profileAvatar);
|
||||
return;
|
||||
}
|
||||
|
||||
if (TextUtils.isEmpty(profileAvatar)) {
|
||||
Log.w(TAG, "Removing profile avatar for: " + recipient.getAddress().serialize());
|
||||
avatarFile.delete();
|
||||
return;
|
||||
}
|
||||
|
||||
File downloadDestination = File.createTempFile("avatar", "jpg", context.getCacheDir());
|
||||
|
||||
try {
|
||||
InputStream avatarStream = receiver.retrieveProfileAvatar(profileAvatar, downloadDestination, recipientsPreferences.get().getProfileKey(), MAX_PROFILE_SIZE_BYTES);
|
||||
File decryptDestination = File.createTempFile("avatar", "jpg", context.getCacheDir());
|
||||
|
||||
Util.copy(avatarStream, new FileOutputStream(decryptDestination));
|
||||
decryptDestination.renameTo(avatarFile);
|
||||
} finally {
|
||||
if (downloadDestination != null) downloadDestination.delete();
|
||||
}
|
||||
|
||||
database.setProfileAvatar(recipient.getAddress(), profileAvatar);
|
||||
RecipientFactory.clearCache(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onShouldRetry(Exception e) {
|
||||
Log.w(TAG, e);
|
||||
if (e instanceof PushNetworkException) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCanceled() {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package org.thoughtcrime.securesms.profiles;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.bumptech.glide.Priority;
|
||||
import com.bumptech.glide.load.data.DataFetcher;
|
||||
|
||||
import org.thoughtcrime.securesms.database.Address;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class AvatarPhotoUriFetcher implements DataFetcher<InputStream> {
|
||||
|
||||
private final Context context;
|
||||
private final Address address;
|
||||
|
||||
private InputStream inputStream;
|
||||
|
||||
public AvatarPhotoUriFetcher(@NonNull Context context, @NonNull Address address) {
|
||||
this.context = context.getApplicationContext();
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream loadData(Priority priority) throws FileNotFoundException {
|
||||
File avatarsDir = new File(context.getFilesDir(), "avatars");
|
||||
inputStream = new FileInputStream(new File(avatarsDir, address.serialize()));
|
||||
|
||||
return inputStream;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanup() {
|
||||
try {
|
||||
if (inputStream != null) inputStream.close();
|
||||
} catch (IOException e) {}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return address.serialize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancel() {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package org.thoughtcrime.securesms.profiles;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.bumptech.glide.load.data.DataFetcher;
|
||||
import com.bumptech.glide.load.model.GenericLoaderFactory;
|
||||
import com.bumptech.glide.load.model.ModelLoaderFactory;
|
||||
import com.bumptech.glide.load.model.stream.StreamModelLoader;
|
||||
|
||||
import org.thoughtcrime.securesms.database.Address;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
public class AvatarPhotoUriLoader implements StreamModelLoader<AvatarPhotoUriLoader.AvatarPhotoUri> {
|
||||
|
||||
private final Context context;
|
||||
|
||||
public static class Factory implements ModelLoaderFactory<AvatarPhotoUri, InputStream> {
|
||||
|
||||
@Override
|
||||
public StreamModelLoader<AvatarPhotoUri> build(Context context, GenericLoaderFactory factories) {
|
||||
return new AvatarPhotoUriLoader(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void teardown() {}
|
||||
}
|
||||
|
||||
public AvatarPhotoUriLoader(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataFetcher<InputStream> getResourceFetcher(AvatarPhotoUri model, int width, int height) {
|
||||
return new AvatarPhotoUriFetcher(context, model.address);
|
||||
}
|
||||
|
||||
public static class AvatarPhotoUri {
|
||||
public @NonNull Address address;
|
||||
|
||||
public AvatarPhotoUri(@NonNull Address address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue