2014-11-09 20:35:08 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2018-08-09 10:15:43 -04:00
|
|
|
import android.support.annotation.NonNull;
|
2014-11-09 20:35:08 -08:00
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
2014-11-09 20:35:08 -08:00
|
|
|
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.crypto.PreKeyUtil;
|
2014-11-11 19:57:53 -08:00
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2018-06-18 12:27:04 -07:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.JobParameters;
|
2018-08-09 10:15:43 -04:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.SafeData;
|
2018-08-01 11:09:24 -04:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2014-11-09 20:35:08 -08: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.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-09 20:35:08 -08:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.List;
|
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
import androidx.work.Data;
|
2018-11-27 12:34:42 -08:00
|
|
|
import androidx.work.WorkerParameters;
|
2018-08-09 10:15:43 -04:00
|
|
|
|
2018-11-15 12:05:08 -08:00
|
|
|
public class RefreshPreKeysJob extends ContextJob implements InjectableType {
|
2014-11-09 20:35:08 -08:00
|
|
|
|
|
|
|
private static final String TAG = RefreshPreKeysJob.class.getSimpleName();
|
|
|
|
|
|
|
|
private static final int PREKEY_MINIMUM = 10;
|
|
|
|
|
2016-03-23 10:34:41 -07:00
|
|
|
@Inject transient SignalServiceAccountManager accountManager;
|
2014-11-11 19:57:53 -08:00
|
|
|
|
2018-11-27 12:34:42 -08:00
|
|
|
public RefreshPreKeysJob(@NonNull Context context, @NonNull WorkerParameters workerParameters) {
|
|
|
|
super(context, workerParameters);
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
|
|
|
|
2014-11-09 20:35:08 -08:00
|
|
|
public RefreshPreKeysJob(Context context) {
|
|
|
|
super(context, JobParameters.newBuilder()
|
|
|
|
.withGroupId(RefreshPreKeysJob.class.getSimpleName())
|
2018-08-09 10:15:43 -04:00
|
|
|
.withNetworkRequirement()
|
2014-11-09 20:35:08 -08:00
|
|
|
.withRetryCount(5)
|
|
|
|
.create());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-09 10:15:43 -04:00
|
|
|
protected void initialize(@NonNull SafeData data) {
|
|
|
|
}
|
2014-11-09 20:35:08 -08:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
@Override
|
|
|
|
protected @NonNull Data serialize(@NonNull Data.Builder dataBuilder) {
|
|
|
|
return dataBuilder.build();
|
2014-11-09 20:35:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-11-15 12:05:08 -08:00
|
|
|
public void onRun() throws IOException {
|
2014-11-09 20:35:08 -08:00
|
|
|
if (!TextSecurePreferences.isPushRegistered(context)) return;
|
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
int availableKeys = accountManager.getPreKeysCount();
|
2014-11-09 20:35:08 -08:00
|
|
|
|
|
|
|
if (availableKeys >= PREKEY_MINIMUM && TextSecurePreferences.isSignedPreKeyRegistered(context)) {
|
2018-08-02 09:25:33 -04:00
|
|
|
Log.i(TAG, "Available keys sufficient: " + availableKeys);
|
2014-11-09 20:35:08 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
List<PreKeyRecord> preKeyRecords = PreKeyUtil.generatePreKeys(context);
|
|
|
|
IdentityKeyPair identityKey = IdentityKeyUtil.getIdentityKeyPair(context);
|
2017-01-06 09:19:58 -08:00
|
|
|
SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, identityKey, false);
|
2014-11-09 20:35:08 -08:00
|
|
|
|
2018-08-02 09:25:33 -04:00
|
|
|
Log.i(TAG, "Registering new prekeys...");
|
2014-11-09 20:35:08 -08:00
|
|
|
|
2017-07-05 13:12:59 -07:00
|
|
|
accountManager.setPreKeys(identityKey.getPublicKey(), signedPreKeyRecord, preKeyRecords);
|
2014-11-09 20:35:08 -08:00
|
|
|
|
2017-01-06 09:19:58 -08:00
|
|
|
PreKeyUtil.setActiveSignedPreKeyId(context, signedPreKeyRecord.getId());
|
2014-11-09 20:35:08 -08:00
|
|
|
TextSecurePreferences.setSignedPreKeyRegistered(context, true);
|
2014-11-11 19:57:53 -08:00
|
|
|
|
|
|
|
ApplicationContext.getInstance(context)
|
|
|
|
.getJobManager()
|
|
|
|
.add(new CleanPreKeysJob(context));
|
2014-11-09 20:35:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-11-15 12:05:08 -08:00
|
|
|
public boolean onShouldRetry(Exception exception) {
|
2014-11-11 21:11:57 -08:00
|
|
|
if (exception instanceof NonSuccessfulResponseCodeException) return false;
|
|
|
|
if (exception instanceof PushNetworkException) return true;
|
2014-11-09 20:35:08 -08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|