From 918f223149547b414afa0df29c8eaabb56410222 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Thu, 20 Feb 2014 14:49:31 -0800 Subject: [PATCH] Add preference to disable the SMS channel all together. 1) If the SMS fallback preference is disabled, no outgoing messages will succeed via the SMS transport. 2) If the SMS fallback preference is disabled, "mirroring" the SMS db state when not the default system SMS app is disabled. --- res/values/strings.xml | 2 ++ res/xml/preferences.xml | 14 +++++--- .../ApplicationPreferencesActivity.java | 32 +++++++++++++++++-- .../securesms/service/MmsListener.java | 7 ++-- .../securesms/service/SmsListener.java | 6 ++-- .../securesms/transport/SmsTransport.java | 6 ++++ .../transport/UniversalTransport.java | 6 ++++ .../securesms/util/TextSecurePreferences.java | 7 +++- 8 files changed, 66 insertions(+), 14 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 61333d5b3d..28e6b53327 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -678,6 +678,8 @@ Use the data channel for communication with other TextSecure users + Allow SMS Fallback + Send and receive SMS messages when push is not available diff --git a/res/xml/preferences.xml b/res/xml/preferences.xml index ff48af2d16..8919e21aaa 100644 --- a/res/xml/preferences.xml +++ b/res/xml/preferences.xml @@ -16,16 +16,22 @@ android:title="@string/preferences__make_default_sms_app" android:summary="@string/preferences__make_textsecure_the_default_sms_mms_app" /> - + + + + + + = Build.VERSION_CODES.KITKAT) { generalCategory.removePreference(findPreference(TextSecurePreferences.ALL_SMS_PREF)); @@ -198,6 +199,29 @@ public class ApplicationPreferencesActivity extends PassphraseRequiredSherlockPr } } + private void initializeSmsFallbackOption() { + CheckBoxPreference allowSmsPreference = + (CheckBoxPreference) findPreference(TextSecurePreferences.ALLOW_SMS_FALLBACK_PREF); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + if (Util.isDefaultSmsProvider(this) || !TextSecurePreferences.isPushRegistered(this)) { + allowSmsPreference.setEnabled(false); + allowSmsPreference.setChecked(true); + } else { + allowSmsPreference.setEnabled(true); + } + } else { + if (TextSecurePreferences.isInterceptAllMmsEnabled(this) || + TextSecurePreferences.isInterceptAllSmsEnabled(this)) + { + allowSmsPreference.setEnabled(false); + allowSmsPreference.setChecked(true); + } else { + allowSmsPreference.setEnabled(true); + } + } + } + private void initializeEditTextSummary(final EditTextPreference preference) { if (preference.getText() == null) { preference.setSummary("Not set"); @@ -268,6 +292,10 @@ public class ApplicationPreferencesActivity extends PassphraseRequiredSherlockPr dynamicTheme.onResume(this); } else if (key.equals(TextSecurePreferences.LANGUAGE_PREF)) { dynamicLanguage.onResume(this); + } else if (key.equals(TextSecurePreferences.ALL_MMS_PREF) || + key.equals(TextSecurePreferences.ALL_SMS_PREF)) + { + initializeSmsFallbackOption(); } } diff --git a/src/org/thoughtcrime/securesms/service/MmsListener.java b/src/org/thoughtcrime/securesms/service/MmsListener.java index 5a95bcb4b6..2de7ed702c 100644 --- a/src/org/thoughtcrime/securesms/service/MmsListener.java +++ b/src/org/thoughtcrime/securesms/service/MmsListener.java @@ -20,14 +20,12 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Build; -import android.preference.PreferenceManager; import android.provider.Telephony; import android.util.Log; -import org.thoughtcrime.securesms.ApplicationPreferencesActivity; import org.thoughtcrime.securesms.protocol.WirePrefix; -import org.thoughtcrime.securesms.util.Util; import org.thoughtcrime.securesms.util.TextSecurePreferences; +import org.thoughtcrime.securesms.util.Util; import ws.com.google.android.mms.pdu.GenericPdu; import ws.com.google.android.mms.pdu.NotificationInd; @@ -52,7 +50,8 @@ public class MmsListener extends BroadcastReceiver { return false; } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT || + if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && + TextSecurePreferences.isSmsFallbackEnabled(context)) || TextSecurePreferences.isInterceptAllMmsEnabled(context)) { return true; diff --git a/src/org/thoughtcrime/securesms/service/SmsListener.java b/src/org/thoughtcrime/securesms/service/SmsListener.java index 02993176dc..5f0c65b435 100644 --- a/src/org/thoughtcrime/securesms/service/SmsListener.java +++ b/src/org/thoughtcrime/securesms/service/SmsListener.java @@ -110,7 +110,8 @@ public class SmsListener extends BroadcastReceiver { return false; } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT || + if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && + TextSecurePreferences.isSmsFallbackEnabled(context)) || TextSecurePreferences.isInterceptAllSmsEnabled(context)) { return true; @@ -120,8 +121,7 @@ public class SmsListener extends BroadcastReceiver { } private boolean isChallenge(Context context, Intent intent) { - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - String messageBody = getSmsMessageBodyFromIntent(intent); + String messageBody = getSmsMessageBodyFromIntent(intent); if (messageBody == null) return false; diff --git a/src/org/thoughtcrime/securesms/transport/SmsTransport.java b/src/org/thoughtcrime/securesms/transport/SmsTransport.java index 7dd2dfd88d..b6f89b9b5a 100644 --- a/src/org/thoughtcrime/securesms/transport/SmsTransport.java +++ b/src/org/thoughtcrime/securesms/transport/SmsTransport.java @@ -48,6 +48,12 @@ public class SmsTransport extends BaseTransport { } public void deliver(SmsMessageRecord message) throws UndeliverableMessageException { + if (TextSecurePreferences.isPushRegistered(context) && + !TextSecurePreferences.isSmsFallbackEnabled(context)) + { + throw new UndeliverableMessageException("SMS Transport is not enabled!"); + } + if (message.isSecure() || message.isKeyExchange() || message.isEndSession()) { deliverSecureMessage(message); } else { diff --git a/src/org/thoughtcrime/securesms/transport/UniversalTransport.java b/src/org/thoughtcrime/securesms/transport/UniversalTransport.java index f226963205..6114567eee 100644 --- a/src/org/thoughtcrime/securesms/transport/UniversalTransport.java +++ b/src/org/thoughtcrime/securesms/transport/UniversalTransport.java @@ -214,6 +214,12 @@ public class UniversalTransport { return false; } + if (TextSecurePreferences.isPushRegistered(context) && + !TextSecurePreferences.isSmsFallbackEnabled(context)) + { + return false; + } + Directory directory = Directory.getInstance(context); return directory.isSmsFallbackSupported(destination); } diff --git a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java index 0fdba0e687..3b07775dbd 100644 --- a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java +++ b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java @@ -44,6 +44,11 @@ public class TextSecurePreferences { private static final String IN_THREAD_NOTIFICATION_PREF = "pref_key_inthread_notifications"; private static final String LOCAL_REGISTRATION_ID_PREF = "pref_local_registration_id"; + public static final String ALLOW_SMS_FALLBACK_PREF = "pref_allow_sms_traffic_out"; + + public static boolean isSmsFallbackEnabled(Context context) { + return getBooleanPreference(context, ALLOW_SMS_FALLBACK_PREF, true); + } public static int getLocalRegistrationId(Context context) { return getIntegerPreference(context, LOCAL_REGISTRATION_ID_PREF, 0); @@ -175,7 +180,7 @@ public class TextSecurePreferences { } public static boolean isSmsDeliveryReportsEnabled(Context context) { - return getBooleanPreference(context, SMS_DELIVERY_REPORT_PREF, false); + return getBooleanPreference(context, SMS_DELIVERY_REPORT_PREF, true); } public static boolean hasPromptedPushRegistration(Context context) {