2018-10-11 16:45:22 -07:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.crypto.ProfileKeyUtil;
|
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2019-03-28 08:56:35 -07:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.Data;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.Job;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
|
2018-10-11 16:45:22 -07:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
|
|
|
import org.thoughtcrime.securesms.service.IncomingMessageObserver;
|
|
|
|
import org.thoughtcrime.securesms.util.Base64;
|
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
|
|
|
import org.whispersystems.signalservice.api.SignalServiceMessagePipe;
|
|
|
|
import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
|
|
|
|
import org.whispersystems.signalservice.api.crypto.ProfileCipher;
|
|
|
|
import org.whispersystems.signalservice.api.profiles.SignalServiceProfile;
|
|
|
|
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public class RefreshUnidentifiedDeliveryAbilityJob extends BaseJob implements InjectableType {
|
2018-10-11 16:45:22 -07:00
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public static final String KEY = "RefreshUnidentifiedDeliveryAbilityJob";
|
2018-10-11 16:45:22 -07:00
|
|
|
|
|
|
|
private static final String TAG = RefreshUnidentifiedDeliveryAbilityJob.class.getSimpleName();
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
@Inject SignalServiceMessageReceiver receiver;
|
2018-10-11 16:45:22 -07:00
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public RefreshUnidentifiedDeliveryAbilityJob() {
|
|
|
|
this(new Job.Parameters.Builder()
|
|
|
|
.addConstraint(NetworkConstraint.KEY)
|
|
|
|
.setMaxAttempts(10)
|
|
|
|
.build());
|
2018-10-11 16:45:22 -07:00
|
|
|
}
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
private RefreshUnidentifiedDeliveryAbilityJob(@NonNull Job.Parameters parameters) {
|
|
|
|
super(parameters);
|
2018-10-11 16:45:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 08:56:35 -07:00
|
|
|
public @NonNull Data serialize() {
|
|
|
|
return Data.EMPTY;
|
|
|
|
}
|
2018-10-11 16:45:22 -07:00
|
|
|
|
|
|
|
@Override
|
2019-03-28 08:56:35 -07:00
|
|
|
public @NonNull String getFactoryKey() {
|
|
|
|
return KEY;
|
2018-10-11 16:45:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRun() throws Exception {
|
|
|
|
byte[] profileKey = ProfileKeyUtil.getProfileKey(context);
|
|
|
|
SignalServiceProfile profile = retrieveProfile(TextSecurePreferences.getLocalNumber(context));
|
|
|
|
|
|
|
|
boolean enabled = profile.getUnidentifiedAccess() != null && isValidVerifier(profileKey, profile.getUnidentifiedAccess());
|
|
|
|
|
|
|
|
TextSecurePreferences.setIsUnidentifiedDeliveryEnabled(context, enabled);
|
|
|
|
Log.i(TAG, "Set UD status to: " + enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 08:56:35 -07:00
|
|
|
public void onCanceled() {
|
2018-10-11 16:45:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-22 13:51:56 -03:00
|
|
|
protected boolean onShouldRetry(@NonNull Exception exception) {
|
2018-10-11 16:45:22 -07:00
|
|
|
return exception instanceof PushNetworkException;
|
|
|
|
}
|
|
|
|
|
|
|
|
private SignalServiceProfile retrieveProfile(@NonNull String number) throws IOException {
|
|
|
|
SignalServiceMessagePipe pipe = IncomingMessageObserver.getPipe();
|
|
|
|
|
|
|
|
if (pipe != null) {
|
|
|
|
try {
|
|
|
|
return pipe.getProfile(new SignalServiceAddress(number), Optional.absent());
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return receiver.retrieveProfile(new SignalServiceAddress(number), Optional.absent());
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isValidVerifier(@NonNull byte[] profileKey, @NonNull String verifier) {
|
|
|
|
ProfileCipher profileCipher = new ProfileCipher(profileKey);
|
|
|
|
try {
|
|
|
|
return profileCipher.verifyUnidentifiedAccess(Base64.decode(verifier));
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-03-28 08:56:35 -07:00
|
|
|
|
|
|
|
public static class Factory implements Job.Factory<RefreshUnidentifiedDeliveryAbilityJob> {
|
|
|
|
@Override
|
|
|
|
public @NonNull RefreshUnidentifiedDeliveryAbilityJob create(@NonNull Parameters parameters, @NonNull Data data) {
|
|
|
|
return new RefreshUnidentifiedDeliveryAbilityJob(parameters);
|
|
|
|
}
|
|
|
|
}
|
2018-10-11 16:45:22 -07:00
|
|
|
}
|