From ca3c82f581834ab553d45146f42cfbe727a5d791 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Sat, 19 Oct 2013 11:15:45 -0700 Subject: [PATCH] Simply PushServiceSocket constructor. --- .../textsecure/push/PushServiceSocket.java | 8 +++++++ .../ApplicationPreferencesActivity.java | 7 +++--- .../securesms/gcm/GcmIntentService.java | 13 ++++------ .../service/DirectoryRefreshService.java | 9 ++++--- .../securesms/service/PushDownloader.java | 6 ++--- .../securesms/transport/PushTransport.java | 10 ++++---- .../transport/UniversalTransport.java | 5 ++-- .../util/TextSecurePushCredentials.java | 24 +++++++++++++++++++ 8 files changed, 52 insertions(+), 30 deletions(-) create mode 100644 src/org/thoughtcrime/securesms/util/TextSecurePushCredentials.java diff --git a/library/src/org/whispersystems/textsecure/push/PushServiceSocket.java b/library/src/org/whispersystems/textsecure/push/PushServiceSocket.java index 5b3b3c1893..dea2e580de 100644 --- a/library/src/org/whispersystems/textsecure/push/PushServiceSocket.java +++ b/library/src/org/whispersystems/textsecure/push/PushServiceSocket.java @@ -61,6 +61,10 @@ public class PushServiceSocket { this.trustManagerFactory = initializeTrustManagerFactory(context); } + public PushServiceSocket(Context context, PushCredentials credentials) { + this(context, credentials.getLocalNumber(context), credentials.getPassword(context)); + } + public void createAccount(boolean voice) throws IOException { String path = voice ? CREATE_ACCOUNT_VOICE_PATH : CREATE_ACCOUNT_SMS_PATH; makeRequest(String.format(path, localNumber), "GET", null); @@ -400,4 +404,8 @@ public class PushServiceSocket { } } + public interface PushCredentials { + public String getLocalNumber(Context context); + public String getPassword(Context context); + } } diff --git a/src/org/thoughtcrime/securesms/ApplicationPreferencesActivity.java b/src/org/thoughtcrime/securesms/ApplicationPreferencesActivity.java index 6aff35cfc0..ba8534fe4f 100644 --- a/src/org/thoughtcrime/securesms/ApplicationPreferencesActivity.java +++ b/src/org/thoughtcrime/securesms/ApplicationPreferencesActivity.java @@ -47,6 +47,7 @@ import com.actionbarsherlock.view.MenuItem; import com.google.android.gcm.GCMRegistrar; import org.thoughtcrime.securesms.contacts.ContactAccessor; import org.thoughtcrime.securesms.contacts.ContactIdentityManager; +import org.thoughtcrime.securesms.util.TextSecurePushCredentials; import org.whispersystems.textsecure.crypto.MasterSecret; import org.thoughtcrime.securesms.crypto.MasterSecretUtil; import org.thoughtcrime.securesms.service.KeyCachingService; @@ -351,10 +352,8 @@ public class ApplicationPreferencesActivity extends PassphraseRequiredSherlockPr @Override protected Integer doInBackground(Void... params) { try { - Context context = ApplicationPreferencesActivity.this; - String localNumber = TextSecurePreferences.getLocalNumber(context); - String pushPassword = TextSecurePreferences.getPushServerPassword(context); - PushServiceSocket socket = new PushServiceSocket(context, localNumber, pushPassword); + Context context = ApplicationPreferencesActivity.this; + PushServiceSocket socket = new PushServiceSocket(context, TextSecurePushCredentials.getInstance()); socket.unregisterGcmId(); GCMRegistrar.unregister(context); diff --git a/src/org/thoughtcrime/securesms/gcm/GcmIntentService.java b/src/org/thoughtcrime/securesms/gcm/GcmIntentService.java index 6bc4371b49..1f6618542a 100644 --- a/src/org/thoughtcrime/securesms/gcm/GcmIntentService.java +++ b/src/org/thoughtcrime/securesms/gcm/GcmIntentService.java @@ -8,6 +8,7 @@ import com.google.android.gcm.GCMBaseIntentService; import org.thoughtcrime.securesms.service.RegistrationService; import org.thoughtcrime.securesms.service.SendReceiveService; import org.thoughtcrime.securesms.util.TextSecurePreferences; +import org.thoughtcrime.securesms.util.TextSecurePushCredentials; import org.whispersystems.textsecure.crypto.InvalidVersionException; import org.whispersystems.textsecure.directory.Directory; import org.whispersystems.textsecure.directory.NotInDirectoryException; @@ -31,7 +32,8 @@ public class GcmIntentService extends GCMBaseIntentService { sendBroadcast(intent); } else { try { - getGcmSocket(context).registerGcmId(registrationId); + PushServiceSocket pushSocket = new PushServiceSocket(context, TextSecurePushCredentials.getInstance()); + pushSocket.registerGcmId(registrationId); } catch (IOException e) { Log.w("GcmIntentService", e); } @@ -41,7 +43,8 @@ public class GcmIntentService extends GCMBaseIntentService { @Override protected void onUnregistered(Context context, String registrationId) { try { - getGcmSocket(context).unregisterGcmId(); + PushServiceSocket pushSocket = new PushServiceSocket(context, TextSecurePushCredentials.getInstance()); + pushSocket.unregisterGcmId(); } catch (IOException ioe) { Log.w("GcmIntentService", ioe); } @@ -84,12 +87,6 @@ public class GcmIntentService extends GCMBaseIntentService { Log.w("GcmIntentService", "GCM Error: " + s); } - private PushServiceSocket getGcmSocket(Context context) { - String localNumber = TextSecurePreferences.getLocalNumber(context); - String password = TextSecurePreferences.getPushServerPassword(context); - return new PushServiceSocket(context, localNumber, password); - } - private boolean isActiveNumber(Context context, String e164number) { boolean isActiveNumber; diff --git a/src/org/thoughtcrime/securesms/service/DirectoryRefreshService.java b/src/org/thoughtcrime/securesms/service/DirectoryRefreshService.java index 3ee33e7ff0..8f39a54e54 100644 --- a/src/org/thoughtcrime/securesms/service/DirectoryRefreshService.java +++ b/src/org/thoughtcrime/securesms/service/DirectoryRefreshService.java @@ -9,6 +9,7 @@ import android.os.PowerManager; import android.util.Log; import org.thoughtcrime.securesms.util.TextSecurePreferences; +import org.thoughtcrime.securesms.util.TextSecurePushCredentials; import org.whispersystems.textsecure.directory.Directory; import org.whispersystems.textsecure.push.ContactTokenDetails; import org.whispersystems.textsecure.push.PushServiceSocket; @@ -57,12 +58,10 @@ public class DirectoryRefreshService extends Service { public void run() { try { Log.w("DirectoryRefreshService", "Refreshing directory..."); - Directory directory = Directory.getInstance(context); - String localNumber = TextSecurePreferences.getLocalNumber(context); - String password = TextSecurePreferences.getPushServerPassword(context); - PushServiceSocket socket = new PushServiceSocket(context, localNumber, password); + Directory directory = Directory.getInstance(context); + PushServiceSocket socket = new PushServiceSocket(context, TextSecurePushCredentials.getInstance()); - Set eligibleContactTokens = directory.getPushEligibleContactTokens(localNumber); + Set eligibleContactTokens = directory.getPushEligibleContactTokens(TextSecurePreferences.getLocalNumber(context)); List activeTokens = socket.retrieveDirectory(eligibleContactTokens); if (activeTokens != null) { diff --git a/src/org/thoughtcrime/securesms/service/PushDownloader.java b/src/org/thoughtcrime/securesms/service/PushDownloader.java index 506a74c62c..c40656d571 100644 --- a/src/org/thoughtcrime/securesms/service/PushDownloader.java +++ b/src/org/thoughtcrime/securesms/service/PushDownloader.java @@ -10,6 +10,7 @@ import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.database.EncryptingPartDatabase; import org.thoughtcrime.securesms.database.PartDatabase; import org.thoughtcrime.securesms.util.TextSecurePreferences; +import org.thoughtcrime.securesms.util.TextSecurePushCredentials; import org.thoughtcrime.securesms.util.Util; import org.whispersystems.textsecure.crypto.AttachmentCipherInputStream; import org.whispersystems.textsecure.crypto.InvalidMessageException; @@ -97,10 +98,7 @@ public class PushDownloader { } private File downloadAttachment(long contentLocation) throws IOException { - String localNumber = TextSecurePreferences.getLocalNumber(context); - String password = TextSecurePreferences.getPushServerPassword(context); - PushServiceSocket socket = new PushServiceSocket(context, localNumber, password); - + PushServiceSocket socket = new PushServiceSocket(context, TextSecurePushCredentials.getInstance()); return socket.retrieveAttachment(contentLocation); } diff --git a/src/org/thoughtcrime/securesms/transport/PushTransport.java b/src/org/thoughtcrime/securesms/transport/PushTransport.java index f8e3dfdbea..eae8d0ead8 100644 --- a/src/org/thoughtcrime/securesms/transport/PushTransport.java +++ b/src/org/thoughtcrime/securesms/transport/PushTransport.java @@ -14,6 +14,7 @@ import org.thoughtcrime.securesms.recipients.RecipientFactory; import org.thoughtcrime.securesms.recipients.RecipientFormattingException; import org.thoughtcrime.securesms.recipients.Recipients; import org.thoughtcrime.securesms.util.TextSecurePreferences; +import org.thoughtcrime.securesms.util.TextSecurePushCredentials; import org.thoughtcrime.securesms.util.Util; import org.whispersystems.textsecure.crypto.AttachmentCipher; import org.whispersystems.textsecure.crypto.IdentityKey; @@ -54,10 +55,9 @@ public class PushTransport extends BaseTransport { public void deliver(SmsMessageRecord message) throws IOException { try { - String localNumber = TextSecurePreferences.getLocalNumber(context); - String password = TextSecurePreferences.getPushServerPassword(context); - PushServiceSocket socket = new PushServiceSocket(context, localNumber, password); + PushServiceSocket socket = new PushServiceSocket(context, TextSecurePushCredentials.getInstance()); + String localNumber = TextSecurePreferences.getLocalNumber(context); Recipient recipient = message.getIndividualRecipient(); String plaintextBody = message.getBody().getBody(); PushMessageContent.Builder builder = PushMessageContent.newBuilder(); @@ -78,9 +78,7 @@ public class PushTransport extends BaseTransport { public void deliver(SendReq message, List destinations) throws IOException { try { - String localNumber = TextSecurePreferences.getLocalNumber(context); - String password = TextSecurePreferences.getPushServerPassword(context); - PushServiceSocket socket = new PushServiceSocket(context, localNumber, password); + PushServiceSocket socket = new PushServiceSocket(context, TextSecurePushCredentials.getInstance()); String messageBody = PartParser.getMessageText(message.getBody()); List relays = new LinkedList(); List ciphertext = new LinkedList(); diff --git a/src/org/thoughtcrime/securesms/transport/UniversalTransport.java b/src/org/thoughtcrime/securesms/transport/UniversalTransport.java index 95cce2df1e..7beff2a0ba 100644 --- a/src/org/thoughtcrime/securesms/transport/UniversalTransport.java +++ b/src/org/thoughtcrime/securesms/transport/UniversalTransport.java @@ -23,6 +23,7 @@ import android.util.Pair; import org.thoughtcrime.securesms.database.model.SmsMessageRecord; import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.util.TextSecurePreferences; +import org.thoughtcrime.securesms.util.TextSecurePushCredentials; import org.thoughtcrime.securesms.util.Util; import org.whispersystems.textsecure.crypto.MasterSecret; import org.whispersystems.textsecure.directory.Directory; @@ -127,10 +128,8 @@ public class UniversalTransport { return directory.isActiveNumber(destination); } catch (NotInDirectoryException e) { try { - String localNumber = TextSecurePreferences.getLocalNumber(context); - String pushPassword = TextSecurePreferences.getPushServerPassword(context); + PushServiceSocket socket = new PushServiceSocket(context, TextSecurePushCredentials.getInstance()); String contactToken = directory.getToken(destination); - PushServiceSocket socket = new PushServiceSocket(context, localNumber, pushPassword); ContactTokenDetails registeredUser = socket.getContactTokenDetails(contactToken); if (registeredUser == null) { diff --git a/src/org/thoughtcrime/securesms/util/TextSecurePushCredentials.java b/src/org/thoughtcrime/securesms/util/TextSecurePushCredentials.java new file mode 100644 index 0000000000..c4adfb51c2 --- /dev/null +++ b/src/org/thoughtcrime/securesms/util/TextSecurePushCredentials.java @@ -0,0 +1,24 @@ +package org.thoughtcrime.securesms.util; + +import android.content.Context; + +import org.whispersystems.textsecure.push.PushServiceSocket; + +public class TextSecurePushCredentials implements PushServiceSocket.PushCredentials { + + private static final TextSecurePushCredentials instance = new TextSecurePushCredentials(); + + public static TextSecurePushCredentials getInstance() { + return instance; + } + + @Override + public String getLocalNumber(Context context) { + return TextSecurePreferences.getLocalNumber(context); + } + + @Override + public String getPassword(Context context) { + return TextSecurePreferences.getPushServerPassword(context); + } +}