2014-11-03 15:16:04 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
2018-05-22 02:13:10 -07:00
|
|
|
import android.annotation.SuppressLint;
|
2017-08-22 10:44:04 -07:00
|
|
|
import android.support.annotation.NonNull;
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
2017-07-26 09:59:15 -07:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2016-02-19 17:07:41 -08:00
|
|
|
import org.thoughtcrime.securesms.database.MessagingDatabase.SyncMessageId;
|
2017-08-22 10:44:04 -07:00
|
|
|
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;
|
2017-08-01 08:56:00 -07:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
|
2017-08-07 14:24:53 -07:00
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public abstract class PushReceivedJob extends BaseJob {
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2015-04-15 10:58:29 -07:00
|
|
|
private static final String TAG = PushReceivedJob.class.getSimpleName();
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2018-10-05 15:23:33 -07:00
|
|
|
public static final Object RECEIVE_LOCK = new Object();
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
protected PushReceivedJob(Job.Parameters parameters) {
|
|
|
|
super(parameters);
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
2018-10-05 15:23:33 -07:00
|
|
|
public void processEnvelope(@NonNull SignalServiceEnvelope envelope) {
|
|
|
|
synchronized (RECEIVE_LOCK) {
|
2018-05-22 02:13:10 -07:00
|
|
|
if (envelope.hasSource()) {
|
|
|
|
Address source = Address.fromExternal(context, envelope.getSource());
|
|
|
|
Recipient recipient = Recipient.from(context, source, false);
|
|
|
|
|
|
|
|
if (!isActiveNumber(recipient)) {
|
|
|
|
DatabaseFactory.getRecipientDatabase(context).setRegistered(recipient, RecipientDatabase.RegisteredState.REGISTERED);
|
2019-03-28 08:56:35 -07:00
|
|
|
ApplicationContext.getInstance(context).getJobManager().add(new DirectoryRefreshJob(recipient, false));
|
2018-05-22 02:13:10 -07:00
|
|
|
}
|
2018-10-05 15:23:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (envelope.isReceipt()) {
|
|
|
|
handleReceipt(envelope);
|
2019-06-03 16:11:22 +10:00
|
|
|
} else if (envelope.isPreKeySignalMessage() || envelope.isSignalMessage() || envelope.isUnidentifiedSender() || envelope.isFriendRequest()) {
|
2018-10-05 15:23:33 -07:00
|
|
|
handleMessage(envelope);
|
|
|
|
} else {
|
|
|
|
Log.w(TAG, "Received envelope of unknown type: " + envelope.getType());
|
|
|
|
}
|
2015-10-29 09:06:28 -07:00
|
|
|
}
|
2015-01-25 17:43:24 -08:00
|
|
|
}
|
|
|
|
|
2018-09-10 08:40:00 -07:00
|
|
|
private void handleMessage(SignalServiceEnvelope envelope) {
|
2018-10-05 15:23:33 -07:00
|
|
|
new PushDecryptJob(context).processMessage(envelope);
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
2018-05-22 02:13:10 -07:00
|
|
|
@SuppressLint("DefaultLocale")
|
2016-03-23 10:34:41 -07:00
|
|
|
private void handleReceipt(SignalServiceEnvelope envelope) {
|
2018-08-02 09:25:33 -04:00
|
|
|
Log.i(TAG, String.format("Received receipt: (XXXXX, %d)", envelope.getTimestamp()));
|
2017-07-26 09:59:15 -07:00
|
|
|
DatabaseFactory.getMmsSmsDatabase(context).incrementDeliveryReceiptCount(new SyncMessageId(Address.fromExternal(context, envelope.getSource()),
|
2017-09-30 08:45:45 -07:00
|
|
|
envelope.getTimestamp()), System.currentTimeMillis());
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
2017-08-22 10:44:04 -07:00
|
|
|
private boolean isActiveNumber(@NonNull Recipient recipient) {
|
|
|
|
return recipient.resolve().getRegistered() == RecipientDatabase.RegisteredState.REGISTERED;
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
}
|