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

84 lines
2.7 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms.jobs;
import android.content.Context;
import android.support.annotation.NonNull;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.dependencies.InjectableType;
import org.thoughtcrime.securesms.jobmanager.JobParameters;
import org.thoughtcrime.securesms.jobmanager.SafeData;
2018-08-01 17:09:24 +02: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;
import androidx.work.Data;
2018-11-27 21:34:42 +01:00
import androidx.work.WorkerParameters;
public class PushNotificationReceiveJob extends PushReceivedJob implements InjectableType {
private static final String TAG = PushNotificationReceiveJob.class.getSimpleName();
@Inject transient SignalServiceMessageReceiver receiver;
2018-11-27 21:34:42 +01:00
public PushNotificationReceiveJob(@NonNull Context context, @NonNull WorkerParameters workerParameters) {
super(context, workerParameters);
}
public PushNotificationReceiveJob(Context context) {
super(context, JobParameters.newBuilder()
.withNetworkRequirement()
.withGroupId("__notification_received")
.create());
}
@Override
protected void initialize(@NonNull SafeData data) {
}
@Override
protected @NonNull Data serialize(@NonNull Data.Builder dataBuilder) {
return dataBuilder.build();
}
@Override
protected String getDescription() {
return context.getString(R.string.PushNotificationReceiveJob_retrieving_a_message);
}
@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(Exception e) {
2015-06-11 18:43:34 +02: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)";
}
}