session-android/src/org/thoughtcrime/securesms/jobs/CreateSignedPreKeyJob.java

89 lines
2.8 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms.jobs;
import android.content.Context;
import android.support.annotation.NonNull;
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
import org.thoughtcrime.securesms.crypto.PreKeyUtil;
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-08-01 11:09:24 -04:00
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.libsignal.IdentityKeyPair;
import org.whispersystems.libsignal.state.SignedPreKeyRecord;
import org.whispersystems.signalservice.api.SignalServiceAccountManager;
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 CreateSignedPreKeyJob extends BaseJob implements InjectableType {
2019-03-28 08:56:35 -07:00
public static final String KEY = "CreateSignedPreKeyJob";
private static final String TAG = CreateSignedPreKeyJob.class.getSimpleName();
2019-03-28 08:56:35 -07:00
@Inject SignalServiceAccountManager accountManager;
2019-03-28 08:56:35 -07:00
public CreateSignedPreKeyJob(Context context) {
this(new Job.Parameters.Builder()
.addConstraint(NetworkConstraint.KEY)
.setQueue("CreateSignedPreKeyJob")
.setMaxAttempts(25)
.build());
}
2019-03-28 08:56:35 -07:00
private CreateSignedPreKeyJob(@NonNull Job.Parameters parameters) {
super(parameters);
}
@Override
2019-03-28 08:56:35 -07:00
public @NonNull Data serialize() {
return Data.EMPTY;
}
@Override
2019-03-28 08:56:35 -07:00
public @NonNull String getFactoryKey() {
return KEY;
}
@Override
public void onRun() throws IOException {
if (TextSecurePreferences.isSignedPreKeyRegistered(context)) {
Log.w(TAG, "Signed prekey already registered...");
return;
}
if (!TextSecurePreferences.isPushRegistered(context)) {
Log.w(TAG, "Not yet registered...");
return;
}
IdentityKeyPair identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(context);
SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, identityKeyPair, true);
accountManager.setSignedPreKey(signedPreKeyRecord);
TextSecurePreferences.setSignedPreKeyRegistered(context, true);
}
@Override
public void onCanceled() {}
@Override
public boolean onShouldRetry(Exception exception) {
if (exception instanceof PushNetworkException) return true;
return false;
}
2019-03-28 08:56:35 -07:00
public static final class Factory implements Job.Factory<CreateSignedPreKeyJob> {
@Override
public @NonNull CreateSignedPreKeyJob create(@NonNull Parameters parameters, @NonNull Data data) {
return new CreateSignedPreKeyJob(parameters);
}
}
}