mirror of
https://github.com/oxen-io/session-android.git
synced 2025-12-03 15:53:56 +00:00
Calculate SMS characters respecting encoding
Fixes #1171 Closes #5448 // FREEBIE
This commit is contained in:
committed by
Moxie Marlinspike
parent
4242ae1b70
commit
e4fccbe26e
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user