109 lines
3.5 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms.jobs;
import android.content.Context;
import android.util.Log;
import org.thoughtcrime.securesms.ApplicationContext;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.jobqueue.JobManager;
import org.whispersystems.jobqueue.JobParameters;
import org.whispersystems.libaxolotl.InvalidVersionException;
2014-11-04 15:01:32 -08:00
import org.whispersystems.textsecure.api.messages.TextSecureEnvelope;
import org.thoughtcrime.securesms.database.TextSecureDirectory;
import org.thoughtcrime.securesms.database.NotInDirectoryException;
import org.whispersystems.textsecure.api.push.ContactTokenDetails;
import java.io.IOException;
public class PushReceiveJob extends ContextJob {
private static final String TAG = PushReceiveJob.class.getSimpleName();
private final String data;
public PushReceiveJob(Context context) {
super(context, JobParameters.newBuilder().create());
this.data = null;
}
public PushReceiveJob(Context context, String data) {
super(context, JobParameters.newBuilder()
.withPersistence()
.withWakeLock(true)
.create());
this.data = data;
}
@Override
public void onAdded() {}
@Override
public void onRun() {
try {
2014-11-04 15:01:32 -08:00
String sessionKey = TextSecurePreferences.getSignalingKey(context);
TextSecureEnvelope envelope = new TextSecureEnvelope(data, sessionKey);
handle(envelope, true);
} catch (IOException | InvalidVersionException e) {
Log.w(TAG, e);
}
}
@Override
public void onCanceled() {
}
@Override
public boolean onShouldRetry(Exception exception) {
return false;
}
public void handle(TextSecureEnvelope envelope, boolean sendExplicitReceipt) {
if (!isActiveNumber(context, envelope.getSource())) {
TextSecureDirectory directory = TextSecureDirectory.getInstance(context);
ContactTokenDetails contactTokenDetails = new ContactTokenDetails();
contactTokenDetails.setNumber(envelope.getSource());
directory.setNumber(contactTokenDetails, true);
}
if (envelope.isReceipt()) handleReceipt(envelope);
else handleMessage(envelope, sendExplicitReceipt);
}
private void handleMessage(TextSecureEnvelope envelope, boolean sendExplicitReceipt) {
JobManager jobManager = ApplicationContext.getInstance(context).getJobManager();
2014-11-04 15:01:32 -08:00
long messageId = DatabaseFactory.getPushDatabase(context).insert(envelope);
if (sendExplicitReceipt) {
jobManager.add(new DeliveryReceiptJob(context, envelope.getSource(),
envelope.getTimestamp(),
envelope.getRelay()));
}
jobManager.add(new PushDecryptJob(context, messageId, envelope.getSource()));
}
2014-11-04 15:01:32 -08:00
private void handleReceipt(TextSecureEnvelope envelope) {
Log.w(TAG, String.format("Received receipt: (XXXXX, %d)", envelope.getTimestamp()));
DatabaseFactory.getMmsSmsDatabase(context).incrementDeliveryReceiptCount(envelope.getSource(),
envelope.getTimestamp());
}
private boolean isActiveNumber(Context context, String e164number) {
boolean isActiveNumber;
try {
isActiveNumber = TextSecureDirectory.getInstance(context).isActiveNumber(e164number);
} catch (NotInDirectoryException e) {
isActiveNumber = false;
}
return isActiveNumber;
}
}