2014-11-03 15:16:04 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
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;
|
2015-04-15 10:58:29 -07:00
|
|
|
import org.thoughtcrime.securesms.database.NotInDirectoryException;
|
|
|
|
import org.thoughtcrime.securesms.database.TextSecureDirectory;
|
2017-08-01 08:56:00 -07:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2015-06-09 07:37:20 -07:00
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
2015-11-09 14:51:53 -08:00
|
|
|
import org.thoughtcrime.securesms.service.KeyCachingService;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.whispersystems.jobqueue.JobManager;
|
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
|
|
|
|
import org.whispersystems.signalservice.api.push.ContactTokenDetails;
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2015-04-15 10:58:29 -07:00
|
|
|
public abstract class PushReceivedJob extends ContextJob {
|
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
|
|
|
|
2015-04-15 10:58:29 -07:00
|
|
|
protected PushReceivedJob(Context context, JobParameters parameters) {
|
|
|
|
super(context, parameters);
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
2016-03-23 10:34:41 -07:00
|
|
|
public void handle(SignalServiceEnvelope envelope, boolean sendExplicitReceipt) {
|
2017-07-26 09:59:15 -07:00
|
|
|
Address source = Address.fromExternal(context, envelope.getSource());
|
|
|
|
|
|
|
|
if (!isActiveNumber(context, source)) {
|
2015-01-25 17:43:24 -08:00
|
|
|
TextSecureDirectory directory = TextSecureDirectory.getInstance(context);
|
|
|
|
ContactTokenDetails contactTokenDetails = new ContactTokenDetails();
|
|
|
|
contactTokenDetails.setNumber(envelope.getSource());
|
|
|
|
|
|
|
|
directory.setNumber(contactTokenDetails, true);
|
2015-11-09 14:51:53 -08:00
|
|
|
|
2017-08-01 08:56:00 -07:00
|
|
|
Recipient recipient = RecipientFactory.getRecipientFor(context, source, false);
|
|
|
|
ApplicationContext.getInstance(context).getJobManager().add(new DirectoryRefreshJob(context, KeyCachingService.getMasterSecret(context), recipient));
|
2015-01-25 17:43:24 -08:00
|
|
|
}
|
|
|
|
|
2015-10-29 09:06:28 -07:00
|
|
|
if (envelope.isReceipt()) {
|
|
|
|
handleReceipt(envelope);
|
2016-03-23 10:34:41 -07:00
|
|
|
} else if (envelope.isPreKeySignalMessage() || envelope.isSignalMessage()) {
|
2017-07-26 09:59:15 -07:00
|
|
|
handleMessage(envelope, source, sendExplicitReceipt);
|
2015-10-29 09:06:28 -07:00
|
|
|
} else {
|
|
|
|
Log.w(TAG, "Received envelope of unknown type: " + envelope.getType());
|
|
|
|
}
|
2015-01-25 17:43:24 -08:00
|
|
|
}
|
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
private void handleMessage(SignalServiceEnvelope envelope, Address source, boolean sendExplicitReceipt) {
|
2017-08-01 08:56:00 -07:00
|
|
|
Recipient recipients = RecipientFactory.getRecipientFor(context, source, false);
|
2014-11-03 15:16:04 -08:00
|
|
|
JobManager jobManager = ApplicationContext.getInstance(context).getJobManager();
|
2015-06-09 07:37:20 -07:00
|
|
|
|
|
|
|
if (!recipients.isBlocked()) {
|
|
|
|
long messageId = DatabaseFactory.getPushDatabase(context).insert(envelope);
|
2017-07-26 09:59:15 -07:00
|
|
|
jobManager.add(new PushDecryptJob(context, messageId));
|
2015-06-09 07:37:20 -07:00
|
|
|
} else {
|
|
|
|
Log.w(TAG, "*** Received blocked push message, ignoring...");
|
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2015-01-25 17:43:24 -08:00
|
|
|
if (sendExplicitReceipt) {
|
|
|
|
jobManager.add(new DeliveryReceiptJob(context, envelope.getSource(),
|
|
|
|
envelope.getTimestamp(),
|
|
|
|
envelope.getRelay()));
|
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
2016-03-23 10:34:41 -07:00
|
|
|
private void handleReceipt(SignalServiceEnvelope envelope) {
|
2014-11-04 15:01:32 -08:00
|
|
|
Log.w(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()),
|
2016-02-19 17:07:41 -08:00
|
|
|
envelope.getTimestamp()));
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
private boolean isActiveNumber(Context context, Address address) {
|
2014-11-03 15:16:04 -08:00
|
|
|
boolean isActiveNumber;
|
|
|
|
|
|
|
|
try {
|
2017-07-26 09:59:15 -07:00
|
|
|
isActiveNumber = TextSecureDirectory.getInstance(context).isSecureTextSupported(address);
|
2014-11-03 15:16:04 -08:00
|
|
|
} catch (NotInDirectoryException e) {
|
|
|
|
isActiveNumber = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isActiveNumber;
|
|
|
|
}
|
|
|
|
|
2015-04-15 10:58:29 -07:00
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|