2014-11-27 23:24:26 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
import android.app.Application;
|
2015-11-09 22:51:53 +00:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
2018-08-09 14:15:43 +00:00
|
|
|
|
|
|
|
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;
|
2014-11-27 23:24:26 +00:00
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2014-11-27 23:24:26 +00:00
|
|
|
import org.thoughtcrime.securesms.util.DirectoryHelper;
|
2016-03-23 17:34:41 +00:00
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
|
2014-11-27 23:24:26 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
public class DirectoryRefreshJob extends BaseJob {
|
2018-08-09 14:15:43 +00:00
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
public static final String KEY = "DirectoryRefreshJob";
|
2014-11-27 23:24:26 +00:00
|
|
|
|
2018-08-02 13:25:33 +00:00
|
|
|
private static final String TAG = DirectoryRefreshJob.class.getSimpleName();
|
|
|
|
|
2018-08-09 14:15:43 +00:00
|
|
|
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;
|
2015-11-09 22:51:53 +00:00
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
public DirectoryRefreshJob(boolean notifyOfNewUsers) {
|
|
|
|
this(null, notifyOfNewUsers);
|
2018-08-09 14:15:43 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2015-11-09 22:51:53 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
private DirectoryRefreshJob(@NonNull Job.Parameters parameters, @Nullable Recipient recipient, boolean notifyOfNewUsers) {
|
|
|
|
super(parameters);
|
2015-11-09 22:51:53 +00:00
|
|
|
|
2017-11-21 19:54:18 +00:00
|
|
|
this.recipient = recipient;
|
|
|
|
this.notifyOfNewUsers = notifyOfNewUsers;
|
2014-11-27 23:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@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();
|
2018-08-09 14:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 15:56:35 +00:00
|
|
|
public @NonNull String getFactoryKey() {
|
|
|
|
return KEY;
|
2018-08-09 14:15:43 +00:00
|
|
|
}
|
2014-11-27 23:24:26 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRun() throws IOException {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i(TAG, "DirectoryRefreshJob.onRun()");
|
2018-08-09 14:15:43 +00:00
|
|
|
|
|
|
|
if (recipient == null) {
|
|
|
|
DirectoryHelper.refreshDirectory(context, notifyOfNewUsers);
|
|
|
|
} else {
|
|
|
|
DirectoryHelper.refreshDirectoryFor(context, recipient);
|
2014-11-27 23:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onShouldRetry(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);
|
|
|
|
}
|
|
|
|
}
|
2014-11-27 23:24:26 +00:00
|
|
|
}
|