session-android/src/org/thoughtcrime/securesms/jobs/RefreshPreKeysJob.java
2019-06-11 13:41:16 +10:00

117 lines
3.9 KiB
Java

package org.thoughtcrime.securesms.jobs;
import android.support.annotation.NonNull;
import org.thoughtcrime.securesms.ApplicationContext;
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
import org.thoughtcrime.securesms.crypto.PreKeyUtil;
import org.thoughtcrime.securesms.dependencies.InjectableType;
import org.thoughtcrime.securesms.jobmanager.Data;
import org.thoughtcrime.securesms.jobmanager.Job;
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.libsignal.IdentityKeyPair;
import org.whispersystems.signalservice.api.SignalServiceAccountManager;
import org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException;
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
import java.io.IOException;
import javax.inject.Inject;
public class RefreshPreKeysJob extends BaseJob implements InjectableType {
public static final String KEY = "RefreshPreKeysJob";
private static final String TAG = RefreshPreKeysJob.class.getSimpleName();
private static final int PREKEY_MINIMUM = 10;
@Inject SignalServiceAccountManager accountManager;
public RefreshPreKeysJob() {
this(new Job.Parameters.Builder()
.setQueue("RefreshPreKeysJob")
.addConstraint(NetworkConstraint.KEY)
.setMaxAttempts(5)
.build());
}
private RefreshPreKeysJob(@NonNull Job.Parameters parameters) {
super(parameters);
}
@Override
public @NonNull Data serialize() {
return Data.EMPTY;
}
@Override
public @NonNull String getFactoryKey() {
return KEY;
}
@Override
public void onRun() throws IOException {
if (TextSecurePreferences.isSignedPreKeyRegistered(context)) {
Log.i(TAG, "Already have a signed pre key registered.");
return;
}
Log.i(TAG, "Registering new signed pre key...");
IdentityKeyPair identityKey = IdentityKeyUtil.getIdentityKeyPair(context);
PreKeyUtil.generateSignedPreKey(context, identityKey, true);
TextSecurePreferences.setSignedPreKeyRegistered(context, true);
ApplicationContext.getInstance(context).getJobManager().add(new CleanPreKeysJob());
}
/* Loki - Original code
@Override
public void onRun() throws IOException {
if (!TextSecurePreferences.isPushRegistered(context)) return;
int availableKeys = accountManager.getPreKeysCount();
if (availableKeys >= PREKEY_MINIMUM && TextSecurePreferences.isSignedPreKeyRegistered(context)) {
Log.i(TAG, "Available keys sufficient: " + availableKeys);
return;
}
List<PreKeyRecord> preKeyRecords = PreKeyUtil.generatePreKeys(context);
IdentityKeyPair identityKey = IdentityKeyUtil.getIdentityKeyPair(context);
SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, identityKey, false);
Log.i(TAG, "Registering new prekeys...");
accountManager.setPreKeys(identityKey.getPublicKey(), signedPreKeyRecord, preKeyRecords);
PreKeyUtil.setActiveSignedPreKeyId(context, signedPreKeyRecord.getId());
TextSecurePreferences.setSignedPreKeyRegistered(context, true);
ApplicationContext.getInstance(context)
.getJobManager()
.add(new CleanPreKeysJob());
}
*/
@Override
public boolean onShouldRetry(Exception exception) {
if (exception instanceof NonSuccessfulResponseCodeException) return false;
if (exception instanceof PushNetworkException) return true;
return false;
}
@Override
public void onCanceled() {
}
public static final class Factory implements Job.Factory<RefreshPreKeysJob> {
@Override
public @NonNull RefreshPreKeysJob create(@NonNull Parameters parameters, @NonNull Data data) {
return new RefreshPreKeysJob(parameters);
}
}
}