2017-06-07 01:03:09 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
2018-08-01 15:09:24 +00:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2017-06-07 01:03:09 +00:00
|
|
|
|
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-06-18 19:27:04 +00:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.JobParameters;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.requirements.NetworkRequirement;
|
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;
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
|
|
public class MultiDeviceVerifiedUpdateJob extends ContextJob implements InjectableType {
|
|
|
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
|
|
private static final String TAG = MultiDeviceVerifiedUpdateJob.class.getSimpleName();
|
|
|
|
|
|
|
|
@Inject
|
2017-09-16 05:38:53 +00:00
|
|
|
transient SignalServiceMessageSender messageSender;
|
2017-06-07 01:03:09 +00:00
|
|
|
|
|
|
|
private final String destination;
|
|
|
|
private final byte[] identityKey;
|
|
|
|
private final VerifiedStatus verifiedStatus;
|
2017-06-23 20:57:38 +00:00
|
|
|
private final long timestamp;
|
2017-06-07 01:03:09 +00:00
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
public MultiDeviceVerifiedUpdateJob(Context context, Address destination, IdentityKey identityKey, VerifiedStatus verifiedStatus) {
|
2017-06-07 01:03:09 +00:00
|
|
|
super(context, JobParameters.newBuilder()
|
|
|
|
.withRequirement(new NetworkRequirement(context))
|
|
|
|
.withPersistence()
|
|
|
|
.withGroupId("__MULTI_DEVICE_VERIFIED_UPDATE__")
|
|
|
|
.create());
|
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
this.destination = destination.serialize();
|
2017-06-07 01:03:09 +00:00
|
|
|
this.identityKey = identityKey.serialize();
|
|
|
|
this.verifiedStatus = verifiedStatus;
|
2017-06-23 20:57:38 +00:00
|
|
|
this.timestamp = System.currentTimeMillis();
|
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)) {
|
|
|
|
Log.w(TAG, "Not multi device...");
|
|
|
|
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
|
|
|
|
|
|
|
messageSender.sendMessage(SignalServiceSyncMessage.forVerified(verifiedMessage));
|
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
|
|
|
|
public boolean onShouldRetry(Exception exception) {
|
|
|
|
return exception instanceof PushNetworkException;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAdded() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|