2014-11-27 15:24:26 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.os.PowerManager;
|
2015-11-09 14:51:53 -08:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
2015-11-06 10:50:47 -08:00
|
|
|
import android.util.Log;
|
2014-11-27 15:24:26 -08:00
|
|
|
|
2018-06-18 12:27:04 -07:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.JobParameters;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.requirements.NetworkRequirement;
|
2017-08-01 08:56:00 -07:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2014-11-27 15:24:26 -08:00
|
|
|
import org.thoughtcrime.securesms.util.DirectoryHelper;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
|
2014-11-27 15:24:26 -08:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
public class DirectoryRefreshJob extends ContextJob {
|
|
|
|
|
2017-08-01 08:56:00 -07:00
|
|
|
@Nullable private transient Recipient recipient;
|
2017-11-21 11:54:18 -08:00
|
|
|
private transient boolean notifyOfNewUsers;
|
2015-11-09 14:51:53 -08:00
|
|
|
|
2017-11-21 11:54:18 -08:00
|
|
|
public DirectoryRefreshJob(@NonNull Context context, boolean notifyOfNewUsers) {
|
2018-01-24 19:17:44 -08:00
|
|
|
this(context, null, notifyOfNewUsers);
|
2015-11-09 14:51:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public DirectoryRefreshJob(@NonNull Context context,
|
2017-11-21 11:54:18 -08:00
|
|
|
@Nullable Recipient recipient,
|
|
|
|
boolean notifyOfNewUsers)
|
2015-11-09 14:51:53 -08:00
|
|
|
{
|
2014-11-27 15:24:26 -08:00
|
|
|
super(context, JobParameters.newBuilder()
|
|
|
|
.withGroupId(DirectoryRefreshJob.class.getSimpleName())
|
|
|
|
.withRequirement(new NetworkRequirement(context))
|
|
|
|
.create());
|
2015-11-09 14:51:53 -08:00
|
|
|
|
2017-11-21 11:54:18 -08:00
|
|
|
this.recipient = recipient;
|
|
|
|
this.notifyOfNewUsers = notifyOfNewUsers;
|
2014-11-27 15:24:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAdded() {}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRun() throws IOException {
|
2015-11-06 10:50:47 -08:00
|
|
|
Log.w("DirectoryRefreshJob", "DirectoryRefreshJob.onRun()");
|
2014-11-27 15:24:26 -08:00
|
|
|
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
|
|
|
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Directory Refresh");
|
|
|
|
|
|
|
|
try {
|
|
|
|
wakeLock.acquire();
|
2017-08-01 08:56:00 -07:00
|
|
|
if (recipient == null) {
|
2018-01-24 19:17:44 -08:00
|
|
|
DirectoryHelper.refreshDirectory(context, notifyOfNewUsers);
|
2015-11-09 14:51:53 -08:00
|
|
|
} else {
|
2018-01-24 19:17:44 -08:00
|
|
|
DirectoryHelper.refreshDirectoryFor(context, recipient);
|
2015-11-09 14:51:53 -08:00
|
|
|
}
|
2014-11-27 15:24:26 -08:00
|
|
|
} finally {
|
|
|
|
if (wakeLock.isHeld()) wakeLock.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onShouldRetry(Exception exception) {
|
|
|
|
if (exception instanceof PushNetworkException) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {}
|
|
|
|
}
|