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

89 lines
2.9 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms.jobs;
import android.content.Context;
import android.support.annotation.NonNull;
import org.thoughtcrime.securesms.dependencies.InjectableType;
2019-03-28 08:56:35 -07:00
import org.thoughtcrime.securesms.jobmanager.Data;
import org.thoughtcrime.securesms.jobmanager.Job;
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
2018-08-01 11:09:24 -04:00
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
import java.io.IOException;
import javax.inject.Inject;
public class PushNotificationReceiveJob extends PushReceivedJob implements InjectableType {
2019-03-28 08:56:35 -07:00
public static final String KEY = "PushNotificationReceiveJob";
private static final String TAG = PushNotificationReceiveJob.class.getSimpleName();
2019-03-28 08:56:35 -07:00
@Inject SignalServiceMessageReceiver receiver;
2019-03-28 08:56:35 -07:00
public PushNotificationReceiveJob(Context context) {
this(new Job.Parameters.Builder()
.addConstraint(NetworkConstraint.KEY)
.setQueue("__notification_received")
.setMaxAttempts(3)
.setMaxInstances(1)
.build());
setContext(context);
}
2019-03-28 08:56:35 -07:00
private PushNotificationReceiveJob(@NonNull Job.Parameters parameters) {
super(parameters);
}
@Override
2019-03-28 08:56:35 -07:00
public @NonNull Data serialize() {
return Data.EMPTY;
}
@Override
2019-03-28 08:56:35 -07:00
public @NonNull String getFactoryKey() {
return KEY;
}
@Override
public void onRun() throws IOException {
pullAndProcessMessages(receiver, TAG, System.currentTimeMillis());
}
public void pullAndProcessMessages(SignalServiceMessageReceiver receiver, String tag, long startTime) throws IOException {
synchronized (PushReceivedJob.RECEIVE_LOCK) {
receiver.retrieveMessages(envelope -> {
Log.i(tag, "Retrieved an envelope." + timeSuffix(startTime));
processEnvelope(envelope);
Log.i(tag, "Successfully processed an envelope." + timeSuffix(startTime));
});
TextSecurePreferences.setNeedsMessagePull(context, false);
}
}
@Override
public boolean onShouldRetry(@NonNull Exception e) {
2015-06-11 09:43:34 -07:00
Log.w(TAG, e);
return e instanceof PushNetworkException;
}
@Override
public void onCanceled() {
Log.w(TAG, "***** Failed to download pending message!");
// MessageNotifier.notifyMessagesPending(getContext());
}
private static String timeSuffix(long startTime) {
return " (" + (System.currentTimeMillis() - startTime) + " ms elapsed)";
}
2019-03-28 08:56:35 -07:00
public static final class Factory implements Job.Factory<PushNotificationReceiveJob> {
@Override
public @NonNull PushNotificationReceiveJob create(@NonNull Parameters parameters, @NonNull Data data) {
return new PushNotificationReceiveJob(parameters);
}
}
}