2017-01-06 09:19:58 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
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.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.crypto.PreKeyUtil;
|
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2018-06-18 12:27:04 -07:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.JobParameters;
|
2018-08-09 10:15:43 -04:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.SafeData;
|
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;
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
import androidx.work.Data;
|
|
|
|
|
2018-11-15 12:05:08 -08:00
|
|
|
public class RotateSignedPreKeyJob extends ContextJob implements InjectableType {
|
2017-01-06 09:19:58 -08:00
|
|
|
|
|
|
|
private static final String TAG = RotateSignedPreKeyJob.class.getName();
|
|
|
|
|
|
|
|
@Inject transient SignalServiceAccountManager accountManager;
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
public RotateSignedPreKeyJob() {
|
|
|
|
super(null, null);
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:19:58 -08:00
|
|
|
public RotateSignedPreKeyJob(Context context) {
|
|
|
|
super(context, JobParameters.newBuilder()
|
2018-08-09 10:15:43 -04:00
|
|
|
.withNetworkRequirement()
|
2017-01-06 09:19:58 -08:00
|
|
|
.withRetryCount(5)
|
|
|
|
.create());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-09 10:15:43 -04:00
|
|
|
protected void initialize(@NonNull SafeData data) {
|
|
|
|
}
|
2017-01-06 09:19:58 -08:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
@Override
|
|
|
|
protected @NonNull Data serialize(@NonNull Data.Builder dataBuilder) {
|
|
|
|
return dataBuilder.build();
|
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);
|
|
|
|
|
|
|
|
accountManager.setSignedPreKey(signedPreKeyRecord);
|
|
|
|
|
|
|
|
PreKeyUtil.setActiveSignedPreKeyId(context, signedPreKeyRecord.getId());
|
|
|
|
TextSecurePreferences.setSignedPreKeyRegistered(context, true);
|
|
|
|
TextSecurePreferences.setSignedPreKeyFailureCount(context, 0);
|
|
|
|
|
|
|
|
ApplicationContext.getInstance(context)
|
|
|
|
.getJobManager()
|
|
|
|
.add(new CleanPreKeysJob(context));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-11-15 12:05:08 -08:00
|
|
|
public boolean onShouldRetry(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);
|
|
|
|
}
|
|
|
|
}
|