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;
|
|
|
|
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;
|
2014-11-09 20:35:08 -08:00
|
|
|
import org.thoughtcrime.securesms.database.TextSecureDirectory;
|
|
|
|
import org.thoughtcrime.securesms.database.NotInDirectoryException;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.whispersystems.textsecure.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, String data) {
|
|
|
|
super(context, JobParameters.newBuilder()
|
|
|
|
.withPersistence()
|
|
|
|
.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);
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2014-11-04 15:01:32 -08:00
|
|
|
if (!isActiveNumber(context, envelope.getSource())) {
|
2014-11-09 20:35:08 -08:00
|
|
|
TextSecureDirectory directory = TextSecureDirectory.getInstance(context);
|
2014-11-03 15:16:04 -08:00
|
|
|
ContactTokenDetails contactTokenDetails = new ContactTokenDetails();
|
2014-11-04 15:01:32 -08:00
|
|
|
contactTokenDetails.setNumber(envelope.getSource());
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
directory.setNumber(contactTokenDetails, true);
|
|
|
|
}
|
|
|
|
|
2014-11-04 15:01:32 -08:00
|
|
|
if (envelope.isReceipt()) handleReceipt(envelope);
|
|
|
|
else handleMessage(envelope);
|
2014-11-03 15:16:04 -08:00
|
|
|
} catch (IOException | InvalidVersionException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onShouldRetry(Throwable throwable) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-04 15:01:32 -08:00
|
|
|
private void handleMessage(TextSecureEnvelope envelope) {
|
2014-11-03 15:16:04 -08:00
|
|
|
JobManager jobManager = ApplicationContext.getInstance(context).getJobManager();
|
2014-11-04 15:01:32 -08:00
|
|
|
long messageId = DatabaseFactory.getPushDatabase(context).insert(envelope);
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2014-11-04 15:01:32 -08:00
|
|
|
jobManager.add(new DeliveryReceiptJob(context, envelope.getSource(),
|
|
|
|
envelope.getTimestamp(),
|
|
|
|
envelope.getRelay()));
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
jobManager.add(new PushDecryptJob(context, messageId));
|
|
|
|
}
|
|
|
|
|
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());
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isActiveNumber(Context context, String e164number) {
|
|
|
|
boolean isActiveNumber;
|
|
|
|
|
|
|
|
try {
|
2014-11-09 20:35:08 -08:00
|
|
|
isActiveNumber = TextSecureDirectory.getInstance(context).isActiveNumber(e164number);
|
2014-11-03 15:16:04 -08:00
|
|
|
} catch (NotInDirectoryException e) {
|
|
|
|
isActiveNumber = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isActiveNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|