Merge change 3015 into donut

* changes:
  make sms calculateLength radio-independent
This commit is contained in:
Android (Google) Code Review
2009-06-11 11:27:53 -07:00
5 changed files with 94 additions and 25 deletions

View File

@@ -224,25 +224,29 @@ public class SmsMessage {
/**
* Calculates the number of SMS's required to encode the message body and
* the number of characters remaining until the next message, given the
* current encoding.
* the number of characters remaining until the next message.
*
* @param messageBody the message to encode
* @param use7bitOnly if true, characters that are not part of the GSM
* alphabet are counted as a single space char. If false, a
* messageBody containing non-GSM alphabet characters is calculated
* for 16-bit encoding.
* @param msgBody the message to encode
* @param use7bitOnly if true, characters that are not part of the
* radio-specific 7-bit encoding are counted as single
* space chars. If false, and if the messageBody contains
* non-7-bit encodable characters, length is calculated
* using a 16-bit encoding.
* @return an int[4] with int[0] being the number of SMS's required, int[1]
* the number of code units used, and int[2] is the number of code
* units remaining until the next message. int[3] is the encoding
* type that should be used for the message.
*/
public static int[] calculateLength(CharSequence messageBody, boolean use7bitOnly) {
public static int[] calculateLength(CharSequence msgBody, boolean use7bitOnly) {
int activePhone = TelephonyManager.getDefault().getPhoneType();
int ret[] = new int[4];
try {
// Try GSM alphabet
int septets = GsmAlphabet.countGsmSeptets(messageBody, !use7bitOnly);
int septets = (PHONE_TYPE_CDMA == activePhone) ?
com.android.internal.telephony.cdma.SmsMessage.calc7bitEncodedLength(msgBody,
use7bitOnly) :
com.android.internal.telephony.gsm.SmsMessage.calc7bitEncodedLength(msgBody,
use7bitOnly);
if (septets != -1) {
ret[1] = septets;
if (septets > MAX_USER_DATA_SEPTETS) {
ret[0] = (septets / MAX_USER_DATA_SEPTETS_WITH_HEADER) + 1;
@@ -253,12 +257,10 @@ public class SmsMessage {
ret[2] = MAX_USER_DATA_SEPTETS - septets;
}
ret[3] = ENCODING_7BIT;
} catch (EncodeException ex) {
// fall back to UCS-2
int octets = messageBody.length() * 2;
ret[1] = messageBody.length();
} else {
int octets = msgBody.length() * 2;
ret[1] = msgBody.length();
if (octets > MAX_USER_DATA_BYTES) {
// 6 is the size of the user data header
ret[0] = (octets / MAX_USER_DATA_BYTES_WITH_HEADER) + 1;
ret[2] = (MAX_USER_DATA_BYTES_WITH_HEADER
- (octets % MAX_USER_DATA_BYTES_WITH_HEADER))/2;

View File

@@ -356,6 +356,17 @@ public class SmsMessage extends SmsMessageBase {
return privateGetSubmitPdu(destAddr, statusReportRequested, uData);
}
/**
* Calculate the number of septets needed to encode the message.
*
* @param messageBody the message to encode
* @param force ignore (but still count) illegal characters if true
* @return septet count, or -1 on failure
*/
public static int calc7bitEncodedLength(CharSequence msgBody, boolean force) {
return BearerData.calc7bitEncodedLength(msgBody.toString(), force);
}
/**
* Note: This function is a GSM specific functionality which is not supported in CDMA mode.
*/

View File

@@ -385,7 +385,17 @@ public final class BearerData{
outStream.skip(3);
}
private static byte[] encode7bitAscii(String msg)
private static class SeptetData {
byte data[];
int septetCount;
SeptetData(byte[] data, int septetCount) {
this.data = data;
this.septetCount = septetCount;
}
}
private static SeptetData encode7bitAscii(String msg, boolean force)
throws CodingException
{
try {
@@ -396,23 +406,43 @@ public final class BearerData{
// Test ourselves for ASCII membership, since Java seems not to care.
if ((charCode < UserData.PRINTABLE_ASCII_MIN_INDEX) ||
(charCode > UserData.PRINTABLE_ASCII_MAX_INDEX)) {
throw new CodingException("illegal ASCII code (" + charCode + ")");
if (force) {
outStream.write(7, UserData.UNENCODABLE_7_BIT_CHAR);
} else {
throw new CodingException("illegal ASCII code (" + charCode + ")");
}
} else {
outStream.write(7, expandedData[i]);
}
outStream.write(7, expandedData[i]);
}
return outStream.toByteArray();
} catch (java.io.UnsupportedEncodingException ex) {
return new SeptetData(outStream.toByteArray(), expandedData.length);
} catch (java.io.UnsupportedEncodingException ex) {
throw new CodingException("7bit ASCII encode failed: " + ex);
} catch (BitwiseOutputStream.AccessException ex) {
} catch (BitwiseOutputStream.AccessException ex) {
throw new CodingException("7bit ASCII encode failed: " + ex);
}
}
/**
* Calculate the number of septets needed to encode the message.
*
* @param force ignore (but still count) illegal characters if true
* @return septet count, or -1 on failure
*/
public static int calc7bitEncodedLength(String msg, boolean force) {
try {
SeptetData data = encode7bitAscii(msg, force);
return data.septetCount;
} catch (CodingException ex) {
return -1;
}
}
private static byte[] encodeUtf16(String msg)
throws CodingException
{
try {
return msg.getBytes("utf-16be"); // XXX(do not submit) -- make sure decode matches
return msg.getBytes("utf-16be");
} catch (java.io.UnsupportedEncodingException ex) {
throw new CodingException("UTF-16 encode failed: " + ex);
}
@@ -462,7 +492,8 @@ public final class BearerData{
if (uData.msgEncoding == UserData.ENCODING_GSM_7BIT_ALPHABET) {
payloadData = encode7bitGsm(uData.payloadStr);
} else if (uData.msgEncoding == UserData.ENCODING_7BIT_ASCII) {
payloadData = encode7bitAscii(uData.payloadStr);
SeptetData septetData = encode7bitAscii(uData.payloadStr, true);
payloadData = septetData.data;
} else if (uData.msgEncoding == UserData.ENCODING_UNICODE_16) {
payloadData = encodeUtf16(uData.payloadStr);
} else {
@@ -478,7 +509,8 @@ public final class BearerData{
uData.payloadStr = "";
}
try {
payloadData = encode7bitAscii(uData.payloadStr);
SeptetData septetData = encode7bitAscii(uData.payloadStr, false);
payloadData = septetData.data;
uData.msgEncoding = UserData.ENCODING_7BIT_ASCII;
} catch (CodingException ex) {
payloadData = encodeUtf16(uData.payloadStr);

View File

@@ -49,6 +49,14 @@ public class UserData {
'`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~'};
/**
* Character to use when forced to encode otherwise unencodable
* characters, meaning those not in the respective ASCII or GSM
* 7-bit encoding tables. Current choice is SPACE, which is 0x20
* in both the GSM-7bit and ASCII-7bit encodings.
*/
static final byte UNENCODABLE_7_BIT_CHAR = 0x20;
/**
* Only elements between these indices in the ASCII table are printable.
*/

View File

@@ -739,6 +739,22 @@ public class SmsMessage extends SmsMessageBase{
}
}
/**
* Calculate the number of septets needed to encode the message.
*
* @param messageBody the message to encode
* @param force ignore (but still count) illegal characters if true
* @return septet count, or -1 on failure
*/
public static int calc7bitEncodedLength(CharSequence messageBody, boolean force) {
try {
return GsmAlphabet.countGsmSeptets(messageBody, !force);
} catch (EncodeException ex) {
/* Just fall through to the -1 error result below. */
}
return -1;
}
/** {@inheritDoc} */
public int getProtocolIdentifier() {
return protocolIdentifier;