package org.thoughtcrime.securesms.jobs; import android.content.Context; import android.util.Log; import org.thoughtcrime.securesms.ApplicationContext; import org.thoughtcrime.securesms.database.Address; import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.database.MessagingDatabase.SyncMessageId; import org.thoughtcrime.securesms.database.NotInDirectoryException; import org.thoughtcrime.securesms.database.TextSecureDirectory; import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.recipients.RecipientFactory; import org.thoughtcrime.securesms.service.KeyCachingService; import org.whispersystems.jobqueue.JobManager; import org.whispersystems.jobqueue.JobParameters; import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope; import org.whispersystems.signalservice.api.push.ContactTokenDetails; public abstract class PushReceivedJob extends ContextJob { private static final String TAG = PushReceivedJob.class.getSimpleName(); protected PushReceivedJob(Context context, JobParameters parameters) { super(context, parameters); } public void handle(SignalServiceEnvelope envelope, boolean sendExplicitReceipt) { Address source = Address.fromExternal(context, envelope.getSource()); if (!isActiveNumber(context, source)) { TextSecureDirectory directory = TextSecureDirectory.getInstance(context); ContactTokenDetails contactTokenDetails = new ContactTokenDetails(); contactTokenDetails.setNumber(envelope.getSource()); directory.setNumber(contactTokenDetails, true); Recipient recipient = RecipientFactory.getRecipientFor(context, source, false); ApplicationContext.getInstance(context).getJobManager().add(new DirectoryRefreshJob(context, KeyCachingService.getMasterSecret(context), recipient)); } if (envelope.isReceipt()) { handleReceipt(envelope); } else if (envelope.isPreKeySignalMessage() || envelope.isSignalMessage()) { handleMessage(envelope, source, sendExplicitReceipt); } else { Log.w(TAG, "Received envelope of unknown type: " + envelope.getType()); } } private void handleMessage(SignalServiceEnvelope envelope, Address source, boolean sendExplicitReceipt) { Recipient recipients = RecipientFactory.getRecipientFor(context, source, false); JobManager jobManager = ApplicationContext.getInstance(context).getJobManager(); if (!recipients.isBlocked()) { long messageId = DatabaseFactory.getPushDatabase(context).insert(envelope); jobManager.add(new PushDecryptJob(context, messageId)); } else { Log.w(TAG, "*** Received blocked push message, ignoring..."); } if (sendExplicitReceipt) { jobManager.add(new DeliveryReceiptJob(context, envelope.getSource(), envelope.getTimestamp(), envelope.getRelay())); } } private void handleReceipt(SignalServiceEnvelope envelope) { Log.w(TAG, String.format("Received receipt: (XXXXX, %d)", envelope.getTimestamp())); DatabaseFactory.getMmsSmsDatabase(context).incrementDeliveryReceiptCount(new SyncMessageId(Address.fromExternal(context, envelope.getSource()), envelope.getTimestamp())); } private boolean isActiveNumber(Context context, Address address) { boolean isActiveNumber; try { isActiveNumber = TextSecureDirectory.getInstance(context).isSecureTextSupported(address); } catch (NotInDirectoryException e) { isActiveNumber = false; } return isActiveNumber; } }