package org.thoughtcrime.securesms.jobs; import android.content.Context; import android.util.Log; import org.thoughtcrime.securesms.crypto.IdentityKeyUtil; import org.thoughtcrime.securesms.crypto.MasterSecret; import org.thoughtcrime.securesms.crypto.PreKeyUtil; import org.thoughtcrime.securesms.push.TextSecureCommunicationFactory; 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; import org.whispersystems.textsecure.api.TextSecureAccountManager; import java.io.IOException; public class CreateSignedPreKeyJob extends ContextJob { private static final String TAG = CreateSignedPreKeyJob.class.getSimpleName(); 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 public void onRun() throws Throwable { MasterSecret masterSecret = ParcelUtil.deserialize(getEncryptionKeys().getEncoded(), MasterSecret.CREATOR); if (TextSecurePreferences.isSignedPreKeyRegistered(context)) { Log.w(TAG, "Signed prekey already registered..."); return; } IdentityKeyPair identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(context, masterSecret); SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, masterSecret, identityKeyPair); TextSecureAccountManager accountManager = TextSecureCommunicationFactory.createManager(context); accountManager.setSignedPreKey(signedPreKeyRecord); TextSecurePreferences.setSignedPreKeyRegistered(context, true); } @Override public void onCanceled() {} @Override public boolean onShouldRetry(Throwable throwable) { if (throwable instanceof IOException) { return true; } Log.w(TAG, throwable); return false; } }