2017-01-06 09:19:58 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
import android.support.annotation.NonNull;
|
2017-01-06 09:19:58 -08:00
|
|
|
|
|
|
|
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;
|
2017-01-06 09:19:58 -08:00
|
|
|
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 {
|
2018-08-09 10:15:43 -04:00
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public static final String KEY = "RotateSignedPreKeyJob";
|
2017-01-06 09:19:58 -08:00
|
|
|
|
2018-12-06 12:14:20 -08:00
|
|
|
private static final String TAG = RotateSignedPreKeyJob.class.getSimpleName();
|
2017-01-06 09:19:58 -08:00
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
@Inject SignalServiceAccountManager accountManager;
|
2017-01-06 09:19:58 -08:00
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public RotateSignedPreKeyJob() {
|
|
|
|
this(new Job.Parameters.Builder()
|
|
|
|
.addConstraint(NetworkConstraint.KEY)
|
|
|
|
.setMaxAttempts(5)
|
|
|
|
.build());
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
private RotateSignedPreKeyJob(@NonNull Job.Parameters parameters) {
|
|
|
|
super(parameters);
|
2017-01-06 09:19:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 08:56:35 -07:00
|
|
|
public @NonNull Data serialize() {
|
|
|
|
return Data.EMPTY;
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
2017-01-06 09:19:58 -08:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
@Override
|
2019-03-28 08:56:35 -07:00
|
|
|
public @NonNull String getFactoryKey() {
|
|
|
|
return KEY;
|
2017-01-06 09:19:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-11-15 12:05:08 -08:00
|
|
|
public void onRun() throws Exception {
|
2018-08-02 09:25:33 -04:00
|
|
|
Log.i(TAG, "Rotating signed prekey...");
|
2017-01-06 09:19:58 -08:00
|
|
|
|
|
|
|
IdentityKeyPair identityKey = IdentityKeyUtil.getIdentityKeyPair(context);
|
|
|
|
SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, identityKey, false);
|
|
|
|
|
2019-06-26 14:31:38 +10:00
|
|
|
// accountManager.setSignedPreKey(signedPreKeyRecord);
|
2017-01-06 09:19:58 -08:00
|
|
|
|
|
|
|
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());
|
2017-01-06 09:19:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-22 13:51:56 -03:00
|
|
|
public boolean onShouldRetry(@NonNull Exception exception) {
|
2017-01-06 09:19:58 -08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2017-01-06 09:19:58 -08:00
|
|
|
}
|