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