session-android/src/org/thoughtcrime/securesms/transport/UniversalTransport.java

51 lines
1.7 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms.transport;
import android.content.Context;
2013-07-13 00:40:41 +00:00
import android.util.Log;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.database.model.SmsMessageRecord;
2013-07-13 00:40:41 +00:00
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.textsecure.directory.NumberFilter;
import org.whispersystems.textsecure.util.PhoneNumberFormatter;
import java.io.IOException;
public class UniversalTransport {
2013-07-13 00:40:41 +00:00
private final Context context;
private final PushTransport pushTransport;
private final SmsTransport smsTransport;
public UniversalTransport(Context context, MasterSecret masterSecret) {
2013-07-13 00:40:41 +00:00
this.context = context;
this.pushTransport = new PushTransport(context, masterSecret);
this.smsTransport = new SmsTransport(context, masterSecret);
}
public void deliver(SmsMessageRecord message) throws UndeliverableMessageException {
2013-07-13 00:40:41 +00:00
if (!TextSecurePreferences.isPushRegistered(context)) {
smsTransport.deliver(message);
return;
}
Recipient recipient = message.getIndividualRecipient();
String localNumber = TextSecurePreferences.getLocalNumber(context);
String number = PhoneNumberFormatter.formatNumber(recipient.getNumber(), localNumber);
if (NumberFilter.getInstance(context).containsNumber(number)) {
try {
Log.w("UniversalTransport", "Delivering with GCM...");
pushTransport.deliver(message);
} catch (IOException ioe) {
Log.w("UniversalTransport", ioe);
smsTransport.deliver(message);
}
} else {
Log.w("UniversalTransport", "Delivering with SMS...");
smsTransport.deliver(message);
}
}
}