2014-10-21 10:39:25 -07:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2018-08-09 10:15:43 -04:00
|
|
|
import android.support.annotation.NonNull;
|
2014-10-21 10:39:25 -07:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
|
2014-11-09 20:35:08 -08:00
|
|
|
import org.thoughtcrime.securesms.crypto.PreKeyUtil;
|
2014-11-11 19:57:53 -08:00
|
|
|
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;
|
2014-10-21 10:39:25 -07:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2016-03-23 10:34:41 -07: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 10:39:25 -07:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public class CreateSignedPreKeyJob extends BaseJob implements InjectableType {
|
2015-07-06 17:36:49 -07:00
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public static final String KEY = "CreateSignedPreKeyJob";
|
2014-10-21 10:39:25 -07:00
|
|
|
|
|
|
|
private static final String TAG = CreateSignedPreKeyJob.class.getSimpleName();
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
@Inject SignalServiceAccountManager accountManager;
|
2014-11-11 19:57:53 -08:00
|
|
|
|
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());
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
private CreateSignedPreKeyJob(@NonNull Job.Parameters parameters) {
|
|
|
|
super(parameters);
|
2014-10-21 10:39:25 -07: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
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 08:56:35 -07:00
|
|
|
public @NonNull String getFactoryKey() {
|
|
|
|
return KEY;
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
2014-10-21 10:39:25 -07:00
|
|
|
|
|
|
|
@Override
|
2018-11-15 12:05:08 -08:00
|
|
|
public void onRun() throws IOException {
|
2014-10-21 10:39:25 -07:00
|
|
|
if (TextSecurePreferences.isSignedPreKeyRegistered(context)) {
|
|
|
|
Log.w(TAG, "Signed prekey already registered...");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-16 09:10:44 -08:00
|
|
|
if (!TextSecurePreferences.isPushRegistered(context)) {
|
|
|
|
Log.w(TAG, "Not yet registered...");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
IdentityKeyPair identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(context);
|
2017-01-06 09:19:58 -08:00
|
|
|
SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, identityKeyPair, true);
|
2014-10-21 10:39:25 -07:00
|
|
|
|
2014-11-09 20:35:08 -08:00
|
|
|
accountManager.setSignedPreKey(signedPreKeyRecord);
|
2014-10-21 10:39:25 -07:00
|
|
|
TextSecurePreferences.setSignedPreKeyRegistered(context, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {}
|
|
|
|
|
|
|
|
@Override
|
2019-05-22 13:51:56 -03:00
|
|
|
public boolean onShouldRetry(@NonNull Exception exception) {
|
2014-11-11 21:11:57 -08:00
|
|
|
if (exception instanceof PushNetworkException) return true;
|
2014-10-21 10:39:25 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2014-10-21 10:39:25 -07:00
|
|
|
}
|