From 6e85677c52e0d44ef44ad42d91743891e2006183 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Wed, 24 Dec 2014 12:40:28 -0800 Subject: [PATCH] Support 'WiFi calling' devices. // FREEBIE Fixes #2254 Fixes #2240 Fixes #2160 Closes #2263 --- res/values/strings.xml | 2 ++ res/xml/preferences_sms_mms.xml | 5 ++++ .../securesms/jobs/SmsSendJob.java | 28 ++++++++++++------ .../NetworkOrServiceRequirement.java | 29 +++++++++++++++++++ .../jobs/requirements/ServiceRequirement.java | 5 ---- .../securesms/util/TextSecurePreferences.java | 5 ++++ 6 files changed, 60 insertions(+), 14 deletions(-) create mode 100644 src/org/thoughtcrime/securesms/jobs/requirements/NetworkOrServiceRequirement.java diff --git a/res/values/strings.xml b/res/values/strings.xml index daa5488e17..612befadf4 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -731,6 +731,8 @@ Never send MMS Non-TextSecure users Nobody + \'WiFi Calling\' compatibility mode + Enable if your device uses SMS/MMS delivery over WiFi (only enable when \'WiFi Calling\' is enabled on your device). diff --git a/res/xml/preferences_sms_mms.xml b/res/xml/preferences_sms_mms.xml index c785fb1461..3acfea7187 100644 --- a/res/xml/preferences_sms_mms.xml +++ b/res/xml/preferences_sms_mms.xml @@ -24,6 +24,11 @@ android:summary="@string/preferences__request_a_delivery_report_for_each_sms_message_you_send" android:title="@string/preferences__sms_delivery_reports" /> + + diff --git a/src/org/thoughtcrime/securesms/jobs/SmsSendJob.java b/src/org/thoughtcrime/securesms/jobs/SmsSendJob.java index 905950ff8a..c0d6048744 100644 --- a/src/org/thoughtcrime/securesms/jobs/SmsSendJob.java +++ b/src/org/thoughtcrime/securesms/jobs/SmsSendJob.java @@ -15,6 +15,7 @@ import org.thoughtcrime.securesms.database.EncryptingSmsDatabase; import org.thoughtcrime.securesms.database.NoSuchMessageException; import org.thoughtcrime.securesms.database.model.SmsMessageRecord; import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement; +import org.thoughtcrime.securesms.jobs.requirements.NetworkOrServiceRequirement; import org.thoughtcrime.securesms.jobs.requirements.ServiceRequirement; import org.thoughtcrime.securesms.notifications.MessageNotifier; import org.thoughtcrime.securesms.recipients.Recipients; @@ -37,13 +38,7 @@ public class SmsSendJob extends MasterSecretJob { private final long messageId; public SmsSendJob(Context context, long messageId, String name) { - super(context, JobParameters.newBuilder() - .withPersistence() - .withRequirement(new MasterSecretRequirement(context)) - .withRequirement(new ServiceRequirement(context)) - .withGroupId(name) - .create()); - + super(context, constructParameters(context, name)); this.messageId = messageId; } @@ -105,7 +100,7 @@ public class SmsSendJob extends MasterSecretJob { throws UndeliverableMessageException, InsecureFallbackApprovalException { MultipartSmsMessageHandler multipartMessageHandler = new MultipartSmsMessageHandler(); - OutgoingTextMessage transportMessage = OutgoingTextMessage.from(message); + OutgoingTextMessage transportMessage = OutgoingTextMessage.from(message); if (message.isSecure() || message.isEndSession()) { transportMessage = getAsymmetricEncrypt(masterSecret, transportMessage); @@ -227,7 +222,7 @@ public class SmsSendJob extends MasterSecretJob { return pending; } - protected Intent constructDeliveredIntent(Context context, long messageId, long type) { + private Intent constructDeliveredIntent(Context context, long messageId, long type) { Intent pending = new Intent(SmsDeliveryListener.DELIVERED_SMS_ACTION, Uri.parse("custom://" + messageId + System.currentTimeMillis()), context, SmsDeliveryListener.class); @@ -237,5 +232,20 @@ public class SmsSendJob extends MasterSecretJob { return pending; } + private static JobParameters constructParameters(Context context, String name) { + JobParameters.Builder builder = JobParameters.newBuilder() + .withPersistence() + .withRequirement(new MasterSecretRequirement(context)) + .withGroupId(name); + + if (TextSecurePreferences.isWifiSmsEnabled(context)) { + builder.withRequirement(new NetworkOrServiceRequirement(context)); + } else { + builder.withRequirement(new ServiceRequirement(context)); + } + + return builder.create(); + } + } diff --git a/src/org/thoughtcrime/securesms/jobs/requirements/NetworkOrServiceRequirement.java b/src/org/thoughtcrime/securesms/jobs/requirements/NetworkOrServiceRequirement.java new file mode 100644 index 0000000000..7a75ba3530 --- /dev/null +++ b/src/org/thoughtcrime/securesms/jobs/requirements/NetworkOrServiceRequirement.java @@ -0,0 +1,29 @@ +package org.thoughtcrime.securesms.jobs.requirements; + +import android.content.Context; + +import org.whispersystems.jobqueue.dependencies.ContextDependent; +import org.whispersystems.jobqueue.requirements.NetworkRequirement; +import org.whispersystems.jobqueue.requirements.Requirement; + +public class NetworkOrServiceRequirement implements Requirement, ContextDependent { + + private transient Context context; + + public NetworkOrServiceRequirement(Context context) { + this.context = context; + } + + @Override + public void setContext(Context context) { + this.context = context; + } + + @Override + public boolean isPresent() { + NetworkRequirement networkRequirement = new NetworkRequirement(context); + ServiceRequirement serviceRequirement = new ServiceRequirement(context); + + return networkRequirement.isPresent() || serviceRequirement.isPresent(); + } +} diff --git a/src/org/thoughtcrime/securesms/jobs/requirements/ServiceRequirement.java b/src/org/thoughtcrime/securesms/jobs/requirements/ServiceRequirement.java index 51fc24e685..c25e96e92d 100644 --- a/src/org/thoughtcrime/securesms/jobs/requirements/ServiceRequirement.java +++ b/src/org/thoughtcrime/securesms/jobs/requirements/ServiceRequirement.java @@ -17,15 +17,10 @@ public class ServiceRequirement implements Requirement, ContextDependent { private static final String TAG = ServiceRequirement.class.getSimpleName(); - private final transient ServiceRequirementProvider provider; - private transient Context context; public ServiceRequirement(Context context) { this.context = context; - this.provider = (ServiceRequirementProvider)ApplicationContext.getInstance(context) - .getJobManager() - .getRequirementProvider("telephony-service"); } @Override diff --git a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java index f5701e4b98..dc743e61fa 100644 --- a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java +++ b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java @@ -57,6 +57,7 @@ public class TextSecurePreferences { private static final String DIRECT_SMS_ALLOWED_PREF = "pref_sms_non_data_out"; private static final String FALLBACK_MMS_ENABLED_PREF = "pref_mms_fallback_enabled"; private static final String SIGNED_PREKEY_REGISTERED_PREF = "pref_signed_prekey_registered"; + private static final String WIFI_SMS_PREF = "pref_wifi_sms"; private static final String GCM_REGISTRATION_ID_PREF = "pref_gcm_registration_id"; private static final String GCM_REGISTRATION_ID_VERSION_PREF = "pref_gcm_registration_id_version"; @@ -64,6 +65,10 @@ public class TextSecurePreferences { private static final String PUSH_REGISTRATION_REMINDER_PREF = "pref_push_registration_reminder"; public static final String REPEAT_ALERTS_PREF = "pref_repeat_alerts"; + public static boolean isWifiSmsEnabled(Context context) { + return getBooleanPreference(context, WIFI_SMS_PREF, false); + } + public static int getRepeatAlertsCount(Context context) { try { return Integer.parseInt(getStringPreference(context, REPEAT_ALERTS_PREF, "0"));