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() { private void calculateCharactersRemaining() {
int charactersSpent = composeText.getText().toString().length(); String messageBody = composeText.getText().toString();
TransportOption transportOption = sendButton.getSelectedTransport(); TransportOption transportOption = sendButton.getSelectedTransport();
CharacterState characterState = transportOption.calculateCharacters(charactersSpent); CharacterState characterState = transportOption.calculateCharacters(messageBody);
if (characterState.charactersRemaining <= 15 || characterState.messagesSpent > 1) { if (characterState.charactersRemaining <= 15 || characterState.messagesSpent > 1) {
charactersLeft.setText(characterState.charactersRemaining + "/" + characterState.maxMessageSize charactersLeft.setText(characterState.charactersRemaining + "/" + characterState.maxMessageSize

View File

@ -66,8 +66,8 @@ public class TransportOption {
return type == Type.SMS; return type == Type.SMS;
} }
public CharacterState calculateCharacters(int charactersSpent) { public CharacterState calculateCharacters(String messageBody) {
return characterCalculator.calculateCharacters(charactersSpent); return characterCalculator.calculateCharacters(messageBody);
} }
public @DrawableRes int getDrawable() { public @DrawableRes int getDrawable() {

View File

@ -18,7 +18,7 @@ package org.thoughtcrime.securesms.util;
public abstract class CharacterCalculator { public abstract class CharacterCalculator {
public abstract CharacterState calculateCharacters(int charactersSpent); public abstract CharacterState calculateCharacters(String messageBody);
public static class CharacterState { public static class CharacterState {
public int charactersRemaining; public int charactersRemaining;

View File

@ -5,7 +5,7 @@ public class MmsCharacterCalculator extends CharacterCalculator {
private static final int MAX_SIZE = 5000; private static final int MAX_SIZE = 5000;
@Override @Override
public CharacterState calculateCharacters(int charactersSpent) { public CharacterState calculateCharacters(String messageBody) {
return new CharacterState(1, MAX_SIZE - charactersSpent, MAX_SIZE); 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 { public class PushCharacterCalculator extends CharacterCalculator {
private static final int MAX_SIZE = 2000; private static final int MAX_SIZE = 2000;
@Override @Override
public CharacterState calculateCharacters(int charactersSpent) { public CharacterState calculateCharacters(String messageBody) {
return new CharacterState(1, MAX_SIZE - charactersSpent, MAX_SIZE); return new CharacterState(1, MAX_SIZE - messageBody.length(), MAX_SIZE);
} }
} }

View File

@ -16,27 +16,18 @@
*/ */
package org.thoughtcrime.securesms.util; package org.thoughtcrime.securesms.util;
import android.telephony.SmsMessage;
public class SmsCharacterCalculator extends CharacterCalculator { public class SmsCharacterCalculator extends CharacterCalculator {
public static final int SMS_SIZE = 160;
public static final int MULTIPART_SMS_SIZE = 153;
@Override @Override
public CharacterState calculateCharacters(int charactersSpent) { public CharacterState calculateCharacters(String messageBody) {
int maxMessageSize;
if (charactersSpent <= SMS_SIZE) { int[] length = SmsMessage.calculateLength(messageBody, false);
maxMessageSize = SMS_SIZE; int messagesSpent = length[0];
} else { int charactersSpent = length[1];
maxMessageSize = MULTIPART_SMS_SIZE; int charactersRemaining = length[2];
} int maxMessageSize = (charactersSpent + charactersRemaining) / messagesSpent;
int messagesSpent = charactersSpent / maxMessageSize;
if (((charactersSpent % maxMessageSize) > 0) || (messagesSpent == 0))
messagesSpent++;
int charactersRemaining = (maxMessageSize * messagesSpent) - charactersSpent;
return new CharacterState(messagesSpent, charactersRemaining, maxMessageSize); return new CharacterState(messagesSpent, charactersRemaining, maxMessageSize);
} }