Calculate SMS characters respecting encoding

Fixes #1171
Closes #5448

// FREEBIE
This commit is contained in:
FeuRenard 2016-04-17 18:09:31 +02:00 committed by Moxie Marlinspike
parent 4242ae1b70
commit e4fccbe26e
6 changed files with 17 additions and 26 deletions

View file

@ -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

View file

@ -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() {

View file

@ -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;

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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);
}