2014-11-10 04:35:08 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2014-11-12 03:57:53 +00:00
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
2014-11-10 04:35:08 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
|
|
|
|
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.jobs.requirements.MasterSecretRequirement;
|
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
|
|
|
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
|
2016-03-23 17:34:41 +00:00
|
|
|
import org.whispersystems.libsignal.IdentityKeyPair;
|
|
|
|
import org.whispersystems.libsignal.state.PreKeyRecord;
|
|
|
|
import org.whispersystems.libsignal.state.SignedPreKeyRecord;
|
|
|
|
import org.whispersystems.signalservice.api.SignalServiceAccountManager;
|
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException;
|
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
|
2014-11-10 04:35:08 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.List;
|
|
|
|
|
2014-11-12 03:57:53 +00:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
|
|
public class RefreshPreKeysJob extends MasterSecretJob implements InjectableType {
|
2014-11-10 04:35:08 +00:00
|
|
|
|
|
|
|
private static final String TAG = RefreshPreKeysJob.class.getSimpleName();
|
|
|
|
|
|
|
|
private static final int PREKEY_MINIMUM = 10;
|
|
|
|
|
2016-03-23 17:34:41 +00:00
|
|
|
@Inject transient SignalServiceAccountManager accountManager;
|
2014-11-12 03:57:53 +00:00
|
|
|
|
2014-11-10 04:35:08 +00:00
|
|
|
public RefreshPreKeysJob(Context context) {
|
|
|
|
super(context, JobParameters.newBuilder()
|
|
|
|
.withGroupId(RefreshPreKeysJob.class.getSimpleName())
|
|
|
|
.withRequirement(new NetworkRequirement(context))
|
|
|
|
.withRequirement(new MasterSecretRequirement(context))
|
|
|
|
.withRetryCount(5)
|
|
|
|
.create());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAdded() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-12 03:57:53 +00:00
|
|
|
public void onRun(MasterSecret masterSecret) throws IOException {
|
2014-11-10 04:35:08 +00:00
|
|
|
if (!TextSecurePreferences.isPushRegistered(context)) return;
|
|
|
|
|
2014-11-12 03:57:53 +00:00
|
|
|
int availableKeys = accountManager.getPreKeysCount();
|
2014-11-10 04:35:08 +00:00
|
|
|
|
|
|
|
if (availableKeys >= PREKEY_MINIMUM && TextSecurePreferences.isSignedPreKeyRegistered(context)) {
|
|
|
|
Log.w(TAG, "Available keys sufficient: " + availableKeys);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-07 00:36:49 +00:00
|
|
|
List<PreKeyRecord> preKeyRecords = PreKeyUtil.generatePreKeys(context);
|
|
|
|
PreKeyRecord lastResortKeyRecord = PreKeyUtil.generateLastResortKey(context);
|
|
|
|
IdentityKeyPair identityKey = IdentityKeyUtil.getIdentityKeyPair(context);
|
|
|
|
SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, identityKey);
|
2014-11-10 04:35:08 +00:00
|
|
|
|
|
|
|
Log.w(TAG, "Registering new prekeys...");
|
|
|
|
|
|
|
|
accountManager.setPreKeys(identityKey.getPublicKey(), lastResortKeyRecord, signedPreKeyRecord, preKeyRecords);
|
|
|
|
|
|
|
|
TextSecurePreferences.setSignedPreKeyRegistered(context, true);
|
2014-11-12 03:57:53 +00:00
|
|
|
|
|
|
|
ApplicationContext.getInstance(context)
|
|
|
|
.getJobManager()
|
|
|
|
.add(new CleanPreKeysJob(context));
|
2014-11-10 04:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-12 05:11:57 +00:00
|
|
|
public boolean onShouldRetryThrowable(Exception exception) {
|
|
|
|
if (exception instanceof NonSuccessfulResponseCodeException) return false;
|
|
|
|
if (exception instanceof PushNetworkException) return true;
|
2014-11-10 04:35:08 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|