2017-05-20 01:01:40 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.support.annotation.NonNull;
|
2017-06-14 16:35:32 +00:00
|
|
|
import android.text.TextUtils;
|
2018-08-09 14:15:43 +00:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.SafeData;
|
2018-08-01 15:09:24 +00:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2017-05-20 01:01:40 +00:00
|
|
|
|
2017-08-15 01:11:13 +00:00
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
2017-05-20 01:01:40 +00:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2018-06-18 19:27:04 +00:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.JobParameters;
|
2017-05-20 01:01:40 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2018-10-15 20:27:21 +00:00
|
|
|
import org.thoughtcrime.securesms.service.IncomingMessageObserver;
|
2017-05-20 01:01:40 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Base64;
|
2017-06-23 20:57:38 +00:00
|
|
|
import org.thoughtcrime.securesms.util.IdentityUtil;
|
2017-08-15 01:11:13 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2017-05-20 01:01:40 +00:00
|
|
|
import org.whispersystems.libsignal.IdentityKey;
|
|
|
|
import org.whispersystems.libsignal.InvalidKeyException;
|
|
|
|
import org.whispersystems.signalservice.api.SignalServiceMessagePipe;
|
|
|
|
import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
|
2017-08-15 01:11:13 +00:00
|
|
|
import org.whispersystems.signalservice.api.crypto.ProfileCipher;
|
2017-08-08 23:37:15 +00:00
|
|
|
import org.whispersystems.signalservice.api.profiles.SignalServiceProfile;
|
2017-05-20 01:01:40 +00:00
|
|
|
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
2017-05-31 21:51:23 +00:00
|
|
|
import org.whispersystems.signalservice.api.util.InvalidNumberException;
|
2017-05-20 01:01:40 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2017-08-01 15:56:00 +00:00
|
|
|
import java.util.List;
|
2017-05-20 01:01:40 +00:00
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2018-08-09 14:15:43 +00:00
|
|
|
import androidx.work.Data;
|
|
|
|
|
2017-05-20 01:01:40 +00:00
|
|
|
public class RetrieveProfileJob extends ContextJob implements InjectableType {
|
|
|
|
|
|
|
|
private static final String TAG = RetrieveProfileJob.class.getSimpleName();
|
|
|
|
|
2018-08-09 14:15:43 +00:00
|
|
|
private static final String KEY_ADDRESS = "address";
|
|
|
|
|
2017-05-20 01:01:40 +00:00
|
|
|
@Inject transient SignalServiceMessageReceiver receiver;
|
|
|
|
|
2018-08-09 14:15:43 +00:00
|
|
|
private Recipient recipient;
|
|
|
|
|
|
|
|
public RetrieveProfileJob() {
|
|
|
|
super(null, null);
|
|
|
|
}
|
2017-05-20 01:01:40 +00:00
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
public RetrieveProfileJob(Context context, Recipient recipient) {
|
2017-05-20 01:01:40 +00:00
|
|
|
super(context, JobParameters.newBuilder()
|
2018-08-09 14:15:43 +00:00
|
|
|
.withNetworkRequirement()
|
2017-05-20 01:01:40 +00:00
|
|
|
.withRetryCount(3)
|
|
|
|
.create());
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
this.recipient = recipient;
|
2017-05-20 01:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-09 14:15:43 +00:00
|
|
|
protected void initialize(@NonNull SafeData data) {
|
|
|
|
recipient = Recipient.from(context, Address.fromSerialized(data.getString(KEY_ADDRESS)), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected @NonNull Data serialize(@NonNull Data.Builder dataBuilder) {
|
|
|
|
return dataBuilder.putString(KEY_ADDRESS, recipient.getAddress().serialize()).build();
|
|
|
|
}
|
2017-05-20 01:01:40 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRun() throws IOException, InvalidKeyException {
|
2017-05-31 21:51:23 +00:00
|
|
|
try {
|
2017-08-01 15:56:00 +00:00
|
|
|
if (recipient.isGroupRecipient()) handleGroupRecipient(recipient);
|
|
|
|
else handleIndividualRecipient(recipient);
|
2017-05-31 21:51:23 +00:00
|
|
|
} catch (InvalidNumberException e) {
|
|
|
|
Log.w(TAG, e);
|
2017-05-20 01:01:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onShouldRetry(Exception e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {}
|
|
|
|
|
|
|
|
private void handleIndividualRecipient(Recipient recipient)
|
2017-05-31 21:51:23 +00:00
|
|
|
throws IOException, InvalidKeyException, InvalidNumberException
|
2017-05-20 01:01:40 +00:00
|
|
|
{
|
2017-08-22 17:44:04 +00:00
|
|
|
String number = recipient.getAddress().toPhoneString();
|
|
|
|
SignalServiceProfile profile = retrieveProfile(number);
|
2017-06-14 16:35:32 +00:00
|
|
|
|
2017-08-15 01:11:13 +00:00
|
|
|
setIdentityKey(recipient, profile.getIdentityKey());
|
2017-08-22 17:44:04 +00:00
|
|
|
setProfileName(recipient, profile.getName());
|
|
|
|
setProfileAvatar(recipient, profile.getAvatar());
|
2017-05-20 01:01:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void handleGroupRecipient(Recipient group)
|
2017-05-31 21:51:23 +00:00
|
|
|
throws IOException, InvalidKeyException, InvalidNumberException
|
2017-05-20 01:01:40 +00:00
|
|
|
{
|
2017-08-01 15:56:00 +00:00
|
|
|
List<Recipient> recipients = DatabaseFactory.getGroupDatabase(context).getGroupMembers(group.getAddress().toGroupString(), false);
|
2017-05-20 01:01:40 +00:00
|
|
|
|
|
|
|
for (Recipient recipient : recipients) {
|
|
|
|
handleIndividualRecipient(recipient);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private SignalServiceProfile retrieveProfile(@NonNull String number) throws IOException {
|
2018-10-15 20:27:21 +00:00
|
|
|
SignalServiceMessagePipe pipe = IncomingMessageObserver.getPipe();
|
2017-05-20 01:01:40 +00:00
|
|
|
|
|
|
|
if (pipe != null) {
|
|
|
|
try {
|
|
|
|
return pipe.getProfile(new SignalServiceAddress(number));
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return receiver.retrieveProfile(new SignalServiceAddress(number));
|
|
|
|
}
|
2017-08-15 01:11:13 +00:00
|
|
|
|
|
|
|
private void setIdentityKey(Recipient recipient, String identityKeyValue) {
|
|
|
|
try {
|
|
|
|
if (TextUtils.isEmpty(identityKeyValue)) {
|
|
|
|
Log.w(TAG, "Identity key is missing on profile!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
IdentityKey identityKey = new IdentityKey(Base64.decode(identityKeyValue), 0);
|
|
|
|
|
|
|
|
if (!DatabaseFactory.getIdentityDatabase(context)
|
|
|
|
.getIdentity(recipient.getAddress())
|
|
|
|
.isPresent())
|
|
|
|
{
|
|
|
|
Log.w(TAG, "Still first use...");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
IdentityUtil.saveIdentity(context, recipient.getAddress().toPhoneString(), identityKey);
|
|
|
|
} catch (InvalidKeyException | IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 17:44:04 +00:00
|
|
|
private void setProfileName(Recipient recipient, String profileName) {
|
2017-08-15 01:11:13 +00:00
|
|
|
try {
|
2017-08-22 17:44:04 +00:00
|
|
|
byte[] profileKey = recipient.getProfileKey();
|
|
|
|
if (profileKey == null) return;
|
2017-08-15 01:11:13 +00:00
|
|
|
|
|
|
|
String plaintextProfileName = null;
|
|
|
|
|
|
|
|
if (profileName != null) {
|
2017-08-22 17:44:04 +00:00
|
|
|
ProfileCipher profileCipher = new ProfileCipher(profileKey);
|
2017-08-15 01:11:13 +00:00
|
|
|
plaintextProfileName = new String(profileCipher.decryptName(Base64.decode(profileName)));
|
|
|
|
}
|
|
|
|
|
2017-08-22 17:44:04 +00:00
|
|
|
if (!Util.equals(plaintextProfileName, recipient.getProfileName())) {
|
|
|
|
DatabaseFactory.getRecipientDatabase(context).setProfileName(recipient, plaintextProfileName);
|
2017-08-15 01:11:13 +00:00
|
|
|
}
|
|
|
|
} catch (ProfileCipher.InvalidCiphertextException | IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 17:44:04 +00:00
|
|
|
private void setProfileAvatar(Recipient recipient, String profileAvatar) {
|
|
|
|
if (recipient.getProfileKey() == null) return;
|
2017-08-15 01:11:13 +00:00
|
|
|
|
2017-08-22 17:44:04 +00:00
|
|
|
if (!Util.equals(profileAvatar, recipient.getProfileAvatar())) {
|
2017-08-15 01:11:13 +00:00
|
|
|
ApplicationContext.getInstance(context)
|
|
|
|
.getJobManager()
|
|
|
|
.add(new RetrieveProfileAvatarJob(context, recipient, profileAvatar));
|
|
|
|
}
|
|
|
|
}
|
2017-05-20 01:01:40 +00:00
|
|
|
}
|