2015-04-15 19:58:29 +02:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2018-08-09 16:15:43 +02:00
|
|
|
import android.support.annotation.NonNull;
|
2015-04-15 19:58:29 +02:00
|
|
|
|
2018-10-03 18:42:35 +02:00
|
|
|
import org.thoughtcrime.securesms.R;
|
2015-04-15 19:58:29 +02:00
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2018-06-18 21:27:04 +02:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.JobParameters;
|
2018-08-09 16:15:43 +02:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.SafeData;
|
2018-08-01 17:09:24 +02:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2018-10-06 00:23:33 +02:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2016-03-23 18:34:41 +01:00
|
|
|
import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
|
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
|
2015-04-15 19:58:29 +02:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2018-08-09 16:15:43 +02:00
|
|
|
import androidx.work.Data;
|
|
|
|
|
2015-04-15 19:58:29 +02:00
|
|
|
public class PushNotificationReceiveJob extends PushReceivedJob implements InjectableType {
|
|
|
|
|
|
|
|
private static final String TAG = PushNotificationReceiveJob.class.getSimpleName();
|
|
|
|
|
2016-03-23 18:34:41 +01:00
|
|
|
@Inject transient SignalServiceMessageReceiver receiver;
|
2015-04-15 19:58:29 +02:00
|
|
|
|
2018-08-09 16:15:43 +02:00
|
|
|
public PushNotificationReceiveJob() {
|
|
|
|
super(null, null);
|
|
|
|
}
|
|
|
|
|
2015-04-15 19:58:29 +02:00
|
|
|
public PushNotificationReceiveJob(Context context) {
|
|
|
|
super(context, JobParameters.newBuilder()
|
2018-08-09 16:15:43 +02:00
|
|
|
.withNetworkRequirement()
|
2015-04-16 21:22:05 +02:00
|
|
|
.withGroupId("__notification_received")
|
2018-08-09 16:15:43 +02:00
|
|
|
.create());
|
2015-04-15 19:58:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-09 16:15:43 +02:00
|
|
|
protected void initialize(@NonNull SafeData data) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected @NonNull Data serialize(@NonNull Data.Builder dataBuilder) {
|
|
|
|
return dataBuilder.build();
|
|
|
|
}
|
2015-04-15 19:58:29 +02:00
|
|
|
|
2018-10-03 18:42:35 +02:00
|
|
|
@Override
|
|
|
|
protected String getDescription() {
|
|
|
|
return context.getString(R.string.PushNotificationReceiveJob_retrieving_a_message);
|
|
|
|
}
|
|
|
|
|
2015-04-15 19:58:29 +02:00
|
|
|
@Override
|
|
|
|
public void onRun() throws IOException {
|
2018-10-06 00:23:33 +02:00
|
|
|
pullAndProcessMessages(receiver, TAG, System.currentTimeMillis());
|
2015-04-15 19:58:29 +02:00
|
|
|
}
|
|
|
|
|
2018-10-06 00:23:33 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2015-04-15 19:58:29 +02:00
|
|
|
@Override
|
|
|
|
public boolean onShouldRetry(Exception e) {
|
2015-06-11 18:43:34 +02:00
|
|
|
Log.w(TAG, e);
|
2015-04-15 19:58:29 +02:00
|
|
|
return e instanceof PushNetworkException;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
|
|
|
Log.w(TAG, "***** Failed to download pending message!");
|
2017-06-20 19:57:11 +02:00
|
|
|
// MessageNotifier.notifyMessagesPending(getContext());
|
2015-04-15 19:58:29 +02:00
|
|
|
}
|
2018-10-06 00:23:33 +02:00
|
|
|
|
|
|
|
private static String timeSuffix(long startTime) {
|
|
|
|
return " (" + (System.currentTimeMillis() - startTime) + " ms elapsed)";
|
|
|
|
}
|
2015-04-15 19:58:29 +02:00
|
|
|
}
|