2017-06-07 01:03:09 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
|
2018-08-09 14:15:43 +00:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
|
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;
|
2018-08-01 15:09:24 +00:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2017-06-07 01:03:09 +00:00
|
|
|
|
2018-05-22 09:13:10 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.UnidentifiedAccessUtil;
|
2017-07-26 16:59:15 +00:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2017-06-07 01:03:09 +00:00
|
|
|
import org.thoughtcrime.securesms.database.IdentityDatabase.VerifiedStatus;
|
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2018-08-09 14:15:43 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Base64;
|
2018-05-22 09:13:10 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2017-06-07 01:03:09 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
|
|
|
import org.whispersystems.libsignal.IdentityKey;
|
|
|
|
import org.whispersystems.libsignal.InvalidKeyException;
|
|
|
|
import org.whispersystems.signalservice.api.SignalServiceMessageSender;
|
|
|
|
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
|
|
|
|
import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage;
|
|
|
|
import org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage;
|
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2019-03-28 15:56:35 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2017-06-07 01:03:09 +00:00
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
public class MultiDeviceVerifiedUpdateJob extends BaseJob implements InjectableType {
|
2018-08-09 14:15:43 +00:00
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
public static final String KEY = "MultiDeviceVerifiedUpdateJob";
|
2017-06-07 01:03:09 +00:00
|
|
|
|
|
|
|
private static final String TAG = MultiDeviceVerifiedUpdateJob.class.getSimpleName();
|
|
|
|
|
2018-08-09 14:15:43 +00:00
|
|
|
private static final String KEY_DESTINATION = "destination";
|
|
|
|
private static final String KEY_IDENTITY_KEY = "identity_key";
|
|
|
|
private static final String KEY_VERIFIED_STATUS = "verified_status";
|
|
|
|
private static final String KEY_TIMESTAMP = "timestamp";
|
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
@Inject SignalServiceMessageSender messageSender;
|
2017-06-07 01:03:09 +00:00
|
|
|
|
2018-08-09 14:15:43 +00:00
|
|
|
private String destination;
|
|
|
|
private byte[] identityKey;
|
|
|
|
private VerifiedStatus verifiedStatus;
|
|
|
|
private long timestamp;
|
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
public MultiDeviceVerifiedUpdateJob(Address destination, IdentityKey identityKey, VerifiedStatus verifiedStatus) {
|
|
|
|
this(new Job.Parameters.Builder()
|
|
|
|
.addConstraint(NetworkConstraint.KEY)
|
|
|
|
.setQueue("__MULTI_DEVICE_VERIFIED_UPDATE__")
|
|
|
|
.setLifespan(TimeUnit.DAYS.toMillis(1))
|
|
|
|
.setMaxAttempts(Parameters.UNLIMITED)
|
|
|
|
.build(),
|
|
|
|
destination,
|
|
|
|
identityKey.serialize(),
|
|
|
|
verifiedStatus,
|
|
|
|
System.currentTimeMillis());
|
2018-08-09 14:15:43 +00:00
|
|
|
}
|
2017-06-07 01:03:09 +00:00
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
private MultiDeviceVerifiedUpdateJob(@NonNull Job.Parameters parameters,
|
|
|
|
@NonNull Address destination,
|
|
|
|
@NonNull byte[] identityKey,
|
|
|
|
@NonNull VerifiedStatus verifiedStatus,
|
|
|
|
long timestamp)
|
|
|
|
{
|
|
|
|
super(parameters);
|
2017-06-07 01:03:09 +00:00
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
this.destination = destination.serialize();
|
2019-03-28 15:56:35 +00:00
|
|
|
this.identityKey = identityKey;
|
2017-06-07 01:03:09 +00:00
|
|
|
this.verifiedStatus = verifiedStatus;
|
2019-03-28 15:56:35 +00:00
|
|
|
this.timestamp = timestamp;
|
2017-06-07 01:03:09 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 14:15:43 +00:00
|
|
|
@Override
|
2019-03-28 15:56:35 +00:00
|
|
|
public @NonNull Data serialize() {
|
|
|
|
return new Data.Builder().putString(KEY_DESTINATION, destination)
|
|
|
|
.putString(KEY_IDENTITY_KEY, Base64.encodeBytes(identityKey))
|
|
|
|
.putInt(KEY_VERIFIED_STATUS, verifiedStatus.toInt())
|
|
|
|
.putLong(KEY_TIMESTAMP, timestamp)
|
|
|
|
.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-06-07 01:03:09 +00:00
|
|
|
@Override
|
|
|
|
public void onRun() throws IOException, UntrustedIdentityException {
|
|
|
|
try {
|
2017-06-23 20:57:38 +00:00
|
|
|
if (!TextSecurePreferences.isMultiDevice(context)) {
|
2018-10-11 23:45:22 +00:00
|
|
|
Log.i(TAG, "Not multi device...");
|
2017-06-23 20:57:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-06-07 01:03:09 +00:00
|
|
|
|
2017-06-23 20:57:38 +00:00
|
|
|
if (destination == null) {
|
|
|
|
Log.w(TAG, "No destination...");
|
|
|
|
return;
|
2017-06-07 01:03:09 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
Address canonicalDestination = Address.fromSerialized(destination);
|
2017-06-23 20:57:38 +00:00
|
|
|
VerifiedMessage.VerifiedState verifiedState = getVerifiedState(verifiedStatus);
|
2017-07-26 16:59:15 +00:00
|
|
|
VerifiedMessage verifiedMessage = new VerifiedMessage(canonicalDestination.toPhoneString(), new IdentityKey(identityKey, 0), verifiedState, timestamp);
|
2017-06-23 20:57:38 +00:00
|
|
|
|
2019-06-24 06:00:15 +00:00
|
|
|
// TODO: Message ID
|
|
|
|
messageSender.sendMessage(0, SignalServiceSyncMessage.forVerified(verifiedMessage),
|
2018-05-22 09:13:10 +00:00
|
|
|
UnidentifiedAccessUtil.getAccessFor(context, Recipient.from(context, Address.fromSerialized(destination), false)));
|
2017-07-26 16:59:15 +00:00
|
|
|
} catch (InvalidKeyException e) {
|
2017-06-23 20:57:38 +00:00
|
|
|
throw new IOException(e);
|
2017-06-07 01:03:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private VerifiedMessage.VerifiedState getVerifiedState(VerifiedStatus status) {
|
|
|
|
VerifiedMessage.VerifiedState verifiedState;
|
|
|
|
|
|
|
|
switch (status) {
|
2017-06-23 20:57:38 +00:00
|
|
|
case DEFAULT: verifiedState = VerifiedMessage.VerifiedState.DEFAULT; break;
|
|
|
|
case VERIFIED: verifiedState = VerifiedMessage.VerifiedState.VERIFIED; break;
|
2017-06-07 01:03:09 +00:00
|
|
|
case UNVERIFIED: verifiedState = VerifiedMessage.VerifiedState.UNVERIFIED; break;
|
|
|
|
default: throw new AssertionError("Unknown status: " + verifiedStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
return verifiedState;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-22 16:51:56 +00:00
|
|
|
public boolean onShouldRetry(@NonNull Exception exception) {
|
2017-06-07 01:03:09 +00:00
|
|
|
return exception instanceof PushNetworkException;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
|
|
|
|
|
|
|
}
|
2019-03-28 15:56:35 +00:00
|
|
|
|
|
|
|
public static final class Factory implements Job.Factory<MultiDeviceVerifiedUpdateJob> {
|
|
|
|
@Override
|
|
|
|
public @NonNull MultiDeviceVerifiedUpdateJob create(@NonNull Parameters parameters, @NonNull Data data) {
|
|
|
|
try {
|
|
|
|
Address destination = Address.fromSerialized(data.getString(KEY_DESTINATION));
|
|
|
|
VerifiedStatus verifiedStatus = VerifiedStatus.forState(data.getInt(KEY_VERIFIED_STATUS));
|
|
|
|
long timestamp = data.getLong(KEY_TIMESTAMP);
|
|
|
|
byte[] identityKey = Base64.decode(data.getString(KEY_IDENTITY_KEY));
|
|
|
|
|
|
|
|
return new MultiDeviceVerifiedUpdateJob(parameters, destination, identityKey, verifiedStatus, timestamp);
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-07 01:03:09 +00:00
|
|
|
}
|