2014-10-21 17:39:25 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2018-08-09 14:15:43 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2014-10-21 17:39:25 +00:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
|
2014-11-10 04:35:08 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.crypto.PreKeyUtil;
|
2014-11-12 03:57:53 +00:00
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2018-06-18 19:27:04 +00:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.JobParameters;
|
2018-08-09 14:15:43 +00:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.SafeData;
|
2018-08-01 15:09:24 +00:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2014-10-21 17:39:25 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2016-03-23 17:34:41 +00:00
|
|
|
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;
|
2014-10-21 17:39:25 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
2014-11-12 03:57:53 +00:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2018-08-09 14:15:43 +00:00
|
|
|
import androidx.work.Data;
|
|
|
|
|
2015-07-07 00:36:49 +00:00
|
|
|
public class CreateSignedPreKeyJob extends MasterSecretJob implements InjectableType {
|
|
|
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
2014-10-21 17:39:25 +00:00
|
|
|
|
|
|
|
private static final String TAG = CreateSignedPreKeyJob.class.getSimpleName();
|
|
|
|
|
2016-03-23 17:34:41 +00:00
|
|
|
@Inject transient SignalServiceAccountManager accountManager;
|
2014-11-12 03:57:53 +00:00
|
|
|
|
2018-08-09 14:15:43 +00:00
|
|
|
public CreateSignedPreKeyJob() {
|
|
|
|
super(null, null);
|
|
|
|
}
|
|
|
|
|
2015-07-07 00:36:49 +00:00
|
|
|
public CreateSignedPreKeyJob(Context context) {
|
2014-10-21 17:39:25 +00:00
|
|
|
super(context, JobParameters.newBuilder()
|
2018-08-09 14:15:43 +00:00
|
|
|
.withNetworkRequirement()
|
|
|
|
.withMasterSecretRequirement()
|
2014-10-21 17:39:25 +00:00
|
|
|
.withGroupId(CreateSignedPreKeyJob.class.getSimpleName())
|
|
|
|
.create());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-09 14:15:43 +00:00
|
|
|
protected void initialize(@NonNull SafeData data) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected @NonNull Data serialize(@NonNull Data.Builder dataBuilder) {
|
|
|
|
return dataBuilder.build();
|
|
|
|
}
|
2014-10-21 17:39:25 +00:00
|
|
|
|
|
|
|
@Override
|
2015-07-07 00:36:49 +00:00
|
|
|
public void onRun(MasterSecret masterSecret) throws IOException {
|
2014-10-21 17:39:25 +00:00
|
|
|
if (TextSecurePreferences.isSignedPreKeyRegistered(context)) {
|
|
|
|
Log.w(TAG, "Signed prekey already registered...");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-16 17:10:44 +00:00
|
|
|
if (!TextSecurePreferences.isPushRegistered(context)) {
|
|
|
|
Log.w(TAG, "Not yet registered...");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-07 00:36:49 +00:00
|
|
|
IdentityKeyPair identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(context);
|
2017-01-06 17:19:58 +00:00
|
|
|
SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, identityKeyPair, true);
|
2014-10-21 17:39:25 +00:00
|
|
|
|
2014-11-10 04:35:08 +00:00
|
|
|
accountManager.setSignedPreKey(signedPreKeyRecord);
|
2014-10-21 17:39:25 +00:00
|
|
|
TextSecurePreferences.setSignedPreKeyRegistered(context, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {}
|
|
|
|
|
|
|
|
@Override
|
2015-07-07 00:36:49 +00:00
|
|
|
public boolean onShouldRetryThrowable(Exception exception) {
|
2014-11-12 05:11:57 +00:00
|
|
|
if (exception instanceof PushNetworkException) return true;
|
2014-10-21 17:39:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|