2014-10-21 17:39:25 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
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;
|
2014-11-10 04:35:08 +00:00
|
|
|
import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory;
|
2014-10-21 17:39:25 +00:00
|
|
|
import org.thoughtcrime.securesms.util.ParcelUtil;
|
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
|
|
|
import org.whispersystems.jobqueue.EncryptionKeys;
|
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
|
|
|
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
|
|
|
|
import org.whispersystems.libaxolotl.IdentityKeyPair;
|
|
|
|
import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
|
2014-11-10 04:35:08 +00:00
|
|
|
import org.whispersystems.textsecure.api.TextSecureAccountManager;
|
2014-11-12 03:57:53 +00:00
|
|
|
import org.whispersystems.textsecure.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;
|
|
|
|
|
|
|
|
public class CreateSignedPreKeyJob extends ContextJob implements InjectableType {
|
2014-10-21 17:39:25 +00:00
|
|
|
|
|
|
|
private static final String TAG = CreateSignedPreKeyJob.class.getSimpleName();
|
|
|
|
|
2014-11-12 03:57:53 +00:00
|
|
|
@Inject transient TextSecureAccountManager accountManager;
|
|
|
|
|
2014-10-21 17:39:25 +00:00
|
|
|
public CreateSignedPreKeyJob(Context context, MasterSecret masterSecret) {
|
|
|
|
super(context, JobParameters.newBuilder()
|
|
|
|
.withPersistence()
|
|
|
|
.withRequirement(new NetworkRequirement(context))
|
|
|
|
.withEncryption(new EncryptionKeys(ParcelUtil.serialize(masterSecret)))
|
|
|
|
.withGroupId(CreateSignedPreKeyJob.class.getSimpleName())
|
|
|
|
.create());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAdded() {}
|
|
|
|
|
|
|
|
@Override
|
2014-11-12 03:57:53 +00:00
|
|
|
public void onRun() throws IOException {
|
2014-10-21 17:39:25 +00:00
|
|
|
MasterSecret masterSecret = ParcelUtil.deserialize(getEncryptionKeys().getEncoded(), MasterSecret.CREATOR);
|
|
|
|
|
|
|
|
if (TextSecurePreferences.isSignedPreKeyRegistered(context)) {
|
|
|
|
Log.w(TAG, "Signed prekey already registered...");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-12 03:57:53 +00:00
|
|
|
IdentityKeyPair identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(context, masterSecret);
|
|
|
|
SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, masterSecret, identityKeyPair);
|
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
|
|
|
|
public boolean onShouldRetry(Throwable throwable) {
|
2014-11-12 03:57:53 +00:00
|
|
|
if (throwable instanceof PushNetworkException) return true;
|
2014-10-21 17:39:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|