diff --git a/src/org/thoughtcrime/securesms/ConversationActivity.java b/src/org/thoughtcrime/securesms/ConversationActivity.java index 135d311fe..9ae7aa14a 100644 --- a/src/org/thoughtcrime/securesms/ConversationActivity.java +++ b/src/org/thoughtcrime/securesms/ConversationActivity.java @@ -1222,9 +1222,9 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity } private void calculateCharactersRemaining() { - int charactersSpent = composeText.getText().toString().length(); + String messageBody = composeText.getText().toString(); TransportOption transportOption = sendButton.getSelectedTransport(); - CharacterState characterState = transportOption.calculateCharacters(charactersSpent); + CharacterState characterState = transportOption.calculateCharacters(messageBody); if (characterState.charactersRemaining <= 15 || characterState.messagesSpent > 1) { charactersLeft.setText(characterState.charactersRemaining + "/" + characterState.maxMessageSize diff --git a/src/org/thoughtcrime/securesms/TransportOption.java b/src/org/thoughtcrime/securesms/TransportOption.java index de21cb0f6..22ba41d62 100644 --- a/src/org/thoughtcrime/securesms/TransportOption.java +++ b/src/org/thoughtcrime/securesms/TransportOption.java @@ -66,8 +66,8 @@ public class TransportOption { return type == Type.SMS; } - public CharacterState calculateCharacters(int charactersSpent) { - return characterCalculator.calculateCharacters(charactersSpent); + public CharacterState calculateCharacters(String messageBody) { + return characterCalculator.calculateCharacters(messageBody); } public @DrawableRes int getDrawable() { diff --git a/src/org/thoughtcrime/securesms/util/CharacterCalculator.java b/src/org/thoughtcrime/securesms/util/CharacterCalculator.java index 8c21fe7c6..c58bb0d4f 100644 --- a/src/org/thoughtcrime/securesms/util/CharacterCalculator.java +++ b/src/org/thoughtcrime/securesms/util/CharacterCalculator.java @@ -18,7 +18,7 @@ package org.thoughtcrime.securesms.util; public abstract class CharacterCalculator { - public abstract CharacterState calculateCharacters(int charactersSpent); + public abstract CharacterState calculateCharacters(String messageBody); public static class CharacterState { public int charactersRemaining; diff --git a/src/org/thoughtcrime/securesms/util/MmsCharacterCalculator.java b/src/org/thoughtcrime/securesms/util/MmsCharacterCalculator.java index c9099e847..9d4698c20 100644 --- a/src/org/thoughtcrime/securesms/util/MmsCharacterCalculator.java +++ b/src/org/thoughtcrime/securesms/util/MmsCharacterCalculator.java @@ -5,7 +5,7 @@ public class MmsCharacterCalculator extends CharacterCalculator { private static final int MAX_SIZE = 5000; @Override - public CharacterState calculateCharacters(int charactersSpent) { - return new CharacterState(1, MAX_SIZE - charactersSpent, MAX_SIZE); + public CharacterState calculateCharacters(String messageBody) { + return new CharacterState(1, MAX_SIZE - messageBody.length(), MAX_SIZE); } } diff --git a/src/org/thoughtcrime/securesms/util/PushCharacterCalculator.java b/src/org/thoughtcrime/securesms/util/PushCharacterCalculator.java index c5096d269..0faa63920 100644 --- a/src/org/thoughtcrime/securesms/util/PushCharacterCalculator.java +++ b/src/org/thoughtcrime/securesms/util/PushCharacterCalculator.java @@ -19,8 +19,8 @@ package org.thoughtcrime.securesms.util; public class PushCharacterCalculator extends CharacterCalculator { private static final int MAX_SIZE = 2000; @Override - public CharacterState calculateCharacters(int charactersSpent) { - return new CharacterState(1, MAX_SIZE - charactersSpent, MAX_SIZE); + public CharacterState calculateCharacters(String messageBody) { + return new CharacterState(1, MAX_SIZE - messageBody.length(), MAX_SIZE); } } diff --git a/src/org/thoughtcrime/securesms/util/SmsCharacterCalculator.java b/src/org/thoughtcrime/securesms/util/SmsCharacterCalculator.java index 9198c7ce5..844d44ac2 100644 --- a/src/org/thoughtcrime/securesms/util/SmsCharacterCalculator.java +++ b/src/org/thoughtcrime/securesms/util/SmsCharacterCalculator.java @@ -16,27 +16,18 @@ */ package org.thoughtcrime.securesms.util; +import android.telephony.SmsMessage; + public class SmsCharacterCalculator extends CharacterCalculator { - public static final int SMS_SIZE = 160; - public static final int MULTIPART_SMS_SIZE = 153; - @Override - public CharacterState calculateCharacters(int charactersSpent) { - int maxMessageSize; + public CharacterState calculateCharacters(String messageBody) { - if (charactersSpent <= SMS_SIZE) { - maxMessageSize = SMS_SIZE; - } else { - maxMessageSize = MULTIPART_SMS_SIZE; - } - - int messagesSpent = charactersSpent / maxMessageSize; - - if (((charactersSpent % maxMessageSize) > 0) || (messagesSpent == 0)) - messagesSpent++; - - int charactersRemaining = (maxMessageSize * messagesSpent) - charactersSpent; + int[] length = SmsMessage.calculateLength(messageBody, false); + int messagesSpent = length[0]; + int charactersSpent = length[1]; + int charactersRemaining = length[2]; + int maxMessageSize = (charactersSpent + charactersRemaining) / messagesSpent; return new CharacterState(messagesSpent, charactersRemaining, maxMessageSize); }