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

86 lines
2.8 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms.jobs;
import android.support.annotation.NonNull;
import org.thoughtcrime.securesms.ApplicationContext;
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 javax.inject.Inject;
2019-03-28 08:56:35 -07:00
public class RotateSignedPreKeyJob extends BaseJob implements InjectableType {
2019-03-28 08:56:35 -07:00
public static final String KEY = "RotateSignedPreKeyJob";
2018-12-06 12:14:20 -08:00
private static final String TAG = RotateSignedPreKeyJob.class.getSimpleName();
2019-03-28 08:56:35 -07:00
@Inject SignalServiceAccountManager accountManager;
2019-03-28 08:56:35 -07:00
public RotateSignedPreKeyJob() {
this(new Job.Parameters.Builder()
.addConstraint(NetworkConstraint.KEY)
.setMaxAttempts(5)
.build());
}
2019-03-28 08:56:35 -07:00
private RotateSignedPreKeyJob(@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 Exception {
2018-08-02 09:25:33 -04:00
Log.i(TAG, "Rotating signed prekey...");
IdentityKeyPair identityKey = IdentityKeyUtil.getIdentityKeyPair(context);
SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, identityKey, false);
accountManager.setSignedPreKey(signedPreKeyRecord);
PreKeyUtil.setActiveSignedPreKeyId(context, signedPreKeyRecord.getId());
TextSecurePreferences.setSignedPreKeyRegistered(context, true);
TextSecurePreferences.setSignedPreKeyFailureCount(context, 0);
ApplicationContext.getInstance(context)
.getJobManager()
2019-03-28 08:56:35 -07:00
.add(new CleanPreKeysJob());
}
@Override
public boolean onShouldRetry(Exception exception) {
return exception instanceof PushNetworkException;
}
@Override
public void onCanceled() {
TextSecurePreferences.setSignedPreKeyFailureCount(context, TextSecurePreferences.getSignedPreKeyFailureCount(context) + 1);
}
2019-03-28 08:56:35 -07:00
public static final class Factory implements Job.Factory<RotateSignedPreKeyJob> {
@Override
public @NonNull RotateSignedPreKeyJob create(@NonNull Parameters parameters, @NonNull Data data) {
return new RotateSignedPreKeyJob(parameters);
}
}
}