session-android/src/org/thoughtcrime/securesms/jobs/DirectoryRefreshJob.java

105 lines
3.4 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms.jobs;
2019-03-28 15:56:35 +00:00
import android.app.Application;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import org.thoughtcrime.securesms.database.Address;
2019-03-28 15:56:35 +00:00
import org.thoughtcrime.securesms.jobmanager.Data;
import org.thoughtcrime.securesms.jobmanager.Job;
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
2018-08-01 15:09:24 +00:00
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.util.DirectoryHelper;
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
import java.io.IOException;
2019-03-28 15:56:35 +00:00
public class DirectoryRefreshJob extends BaseJob {
2019-03-28 15:56:35 +00:00
public static final String KEY = "DirectoryRefreshJob";
2018-08-02 13:25:33 +00:00
private static final String TAG = DirectoryRefreshJob.class.getSimpleName();
private static final String KEY_ADDRESS = "address";
private static final String KEY_NOTIFY_OF_NEW_USERS = "notify_of_new_users";
2019-03-28 15:56:35 +00:00
@Nullable private Recipient recipient;
private boolean notifyOfNewUsers;
2019-03-28 15:56:35 +00:00
public DirectoryRefreshJob(boolean notifyOfNewUsers) {
this(null, notifyOfNewUsers);
}
2019-03-28 15:56:35 +00:00
public DirectoryRefreshJob(@Nullable Recipient recipient,
boolean notifyOfNewUsers)
{
this(new Job.Parameters.Builder()
.setQueue("DirectoryRefreshJob")
.addConstraint(NetworkConstraint.KEY)
.setMaxAttempts(10)
.build(),
recipient,
notifyOfNewUsers);
}
2019-03-28 15:56:35 +00:00
private DirectoryRefreshJob(@NonNull Job.Parameters parameters, @Nullable Recipient recipient, boolean notifyOfNewUsers) {
super(parameters);
this.recipient = recipient;
this.notifyOfNewUsers = notifyOfNewUsers;
}
@Override
2019-03-28 15:56:35 +00:00
public @NonNull Data serialize() {
return new Data.Builder().putString(KEY_ADDRESS, recipient != null ? recipient.getAddress().serialize() : null)
.putBoolean(KEY_NOTIFY_OF_NEW_USERS, notifyOfNewUsers)
.build();
}
@Override
2019-03-28 15:56:35 +00:00
public @NonNull String getFactoryKey() {
return KEY;
}
@Override
public void onRun() throws IOException {
2018-08-02 13:25:33 +00:00
Log.i(TAG, "DirectoryRefreshJob.onRun()");
if (recipient == null) {
DirectoryHelper.refreshDirectory(context, notifyOfNewUsers);
} else {
DirectoryHelper.refreshDirectoryFor(context, recipient);
}
}
@Override
public boolean onShouldRetry(@NonNull Exception exception) {
if (exception instanceof PushNetworkException) return true;
return false;
}
@Override
public void onCanceled() {}
2019-03-28 15:56:35 +00:00
public static final class Factory implements Job.Factory<DirectoryRefreshJob> {
private final Application application;
public Factory(@NonNull Application application) {
this.application = application;
}
@Override
public @NonNull DirectoryRefreshJob create(@NonNull Parameters parameters, @NonNull Data data) {
String serializedAddress = data.getString(KEY_ADDRESS);
Address address = serializedAddress != null ? Address.fromSerialized(serializedAddress) : null;
Recipient recipient = address != null ? Recipient.from(application, address, true) : null;
boolean notifyOfNewUsers = data.getBoolean(KEY_NOTIFY_OF_NEW_USERS);
return new DirectoryRefreshJob(parameters, recipient, notifyOfNewUsers);
}
}
}