65 lines
2.7 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms.jobs;
2018-05-22 02:13:10 -07:00
import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import org.thoughtcrime.securesms.database.Address;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.MessagingDatabase.SyncMessageId;
import org.thoughtcrime.securesms.database.RecipientDatabase;
2019-03-28 08:56:35 -07:00
import org.thoughtcrime.securesms.jobmanager.Job;
2018-08-01 11:09:24 -04:00
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
2019-03-28 08:56:35 -07:00
public abstract class PushReceivedJob extends BaseJob {
private static final String TAG = PushReceivedJob.class.getSimpleName();
public static final Object RECEIVE_LOCK = new Object();
2019-03-28 08:56:35 -07:00
protected PushReceivedJob(Job.Parameters parameters) {
super(parameters);
}
2020-05-22 13:41:36 +10:00
public void processEnvelope(@NonNull SignalServiceEnvelope envelope, boolean isPushNotification) {
synchronized (RECEIVE_LOCK) {
try {
if (envelope.hasSource()) {
Address source = Address.fromExternal(context, envelope.getSource());
Recipient recipient = Recipient.from(context, source, false);
2018-05-22 02:13:10 -07:00
if (!isActiveNumber(recipient)) {
DatabaseFactory.getRecipientDatabase(context).setRegistered(recipient, RecipientDatabase.RegisteredState.REGISTERED);
}
2018-05-22 02:13:10 -07:00
}
if (envelope.isReceipt()) {
handleReceipt(envelope);
} else if (envelope.isPreKeySignalMessage() || envelope.isSignalMessage() || envelope.isUnidentifiedSender() || envelope.isFriendRequest()) {
2020-05-22 13:41:36 +10:00
handleMessage(envelope, isPushNotification);
} else {
Log.w(TAG, "Received envelope of unknown type: " + envelope.getType());
}
} catch (Exception e) {
2020-05-12 16:28:35 +10:00
Log.d("Loki", "Failed to process envelope due to error: " + e);
}
}
}
2020-05-22 13:41:36 +10:00
private void handleMessage(SignalServiceEnvelope envelope, boolean isPushNotification) {
new PushDecryptJob(context).processMessage(envelope, isPushNotification);
}
2018-05-22 02:13:10 -07:00
@SuppressLint("DefaultLocale")
private void handleReceipt(SignalServiceEnvelope envelope) {
2018-08-02 09:25:33 -04:00
Log.i(TAG, String.format("Received receipt: (XXXXX, %d)", envelope.getTimestamp()));
DatabaseFactory.getMmsSmsDatabase(context).incrementDeliveryReceiptCount(new SyncMessageId(Address.fromExternal(context, envelope.getSource()),
envelope.getTimestamp()), System.currentTimeMillis());
}
private boolean isActiveNumber(@NonNull Recipient recipient) {
return recipient.resolve().getRegistered() == RecipientDatabase.RegisteredState.REGISTERED;
}
}