From e4fccbe26e0ad69f1d13e69fbd294d82a2bc3d0a Mon Sep 17 00:00:00 2001 From: FeuRenard Date: Sun, 17 Apr 2016 18:09:31 +0200 Subject: [PATCH] Calculate SMS characters respecting encoding Fixes #1171 Closes #5448 // FREEBIE --- .../securesms/ConversationActivity.java | 4 +-- .../securesms/TransportOption.java | 4 +-- .../securesms/util/CharacterCalculator.java | 2 +- .../util/MmsCharacterCalculator.java | 4 +-- .../util/PushCharacterCalculator.java | 4 +-- .../util/SmsCharacterCalculator.java | 25 ++++++------------- 6 files changed, 17 insertions(+), 26 deletions(-) diff --git a/src/org/thoughtcrime/securesms/ConversationActivity.java b/src/org/thoughtcrime/securesms/ConversationActivity.java index 135d311fe1..9ae7aa14a0 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 de21cb0f66..22ba41d62c 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 8c21fe7c6a..c58bb0d4f8 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 c9099e847c..9d4698c207 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 c5096d2699..0faa63920b 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 9198c7ce5e..844d44ac25 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); }