Example usage for android.telephony SmsMessage calculateLength

List of usage examples for android.telephony SmsMessage calculateLength

Introduction

In this page you can find the example usage for android.telephony SmsMessage calculateLength.

Prototype

public static int[] calculateLength(String messageBody, boolean use7bitOnly) 

Source Link

Document

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.

Usage

From source file:com.android.mms.ui.ComposeMessageActivity.java

private void updateCounter(CharSequence text, int start, int before, int count) {
    WorkingMessage workingMessage = mWorkingMessage;
    if (workingMessage.requiresMms()) {
        // If we're not removing text (i.e. no chance of converting back to SMS
        // because of this change) and we're in MMS mode, just bail out since we
        // then won't have to calculate the length unnecessarily.
        final boolean textRemoved = (before > count);
        if (!textRemoved) {
            showSmsOrMmsSendButton(workingMessage.requiresMms());
            return;
        }//from w  ww .  ja  v  a2s .  com
    }

    int[] params = SmsMessage.calculateLength(text, false);
    /* SmsMessage.calculateLength returns an int[4] with:
     *   int[0] being the number of SMS's required,
     *   int[1] the number of code units used,
     *   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.
     */
    int msgCount = params[0];
    int remainingInCurrentMessage = params[2];

    if (!MmsConfig.getSplitSmsEnabled() && !MmsConfig.getMultipartSmsEnabled()) {
        // The provider doesn't support multi-part sms's so as soon as the user types
        // an sms longer than one segment, we have to turn the message into an mms.
        mWorkingMessage.setLengthRequiresMms(msgCount > 1, true);
    } else {
        int threshold = MmsConfig.getSmsToMmsTextThreshold();
        mWorkingMessage.setLengthRequiresMms(threshold > 0 && msgCount > threshold, true);
    }

    // Show the counter only if:
    // - We are not in MMS mode
    // - We are going to send more than one message OR we are getting close
    boolean showCounter = false;
    if (!workingMessage.requiresMms()
            && (msgCount > 1 || remainingInCurrentMessage <= CHARS_REMAINING_BEFORE_COUNTER_SHOWN)) {
        showCounter = true;
    }

    showSmsOrMmsSendButton(workingMessage.requiresMms());

    if (showCounter) {
        // Update the remaining characters and number of messages required.
        String counterText = msgCount > 1 ? remainingInCurrentMessage + " / " + msgCount
                : String.valueOf(remainingInCurrentMessage);
        mTextCounter.setText(counterText);
        mTextCounter.setVisibility(View.VISIBLE);
    } else {
        mTextCounter.setVisibility(View.GONE);
    }
}

From source file:com.android.mms.ui.MessageUtils.java

public static final String findSpanishChar(CharSequence s) {
    int sLength = s.length();
    StringBuilder sPs = new StringBuilder();
    for (int i = 0; i < sLength; i++) {
        char tmpC = s.charAt(i);
        int tmpi = (int) tmpC;
        //   Log.e("chenshu","MessageUtils->>findSpanishChar->>tmpi->>"+Integer.toHexString(tmpi));
        //   Log.d("chenshu","MessageUtils->>findSpanishChar->>calculateLength->>"+s.subSequence(i,i+1));

        int[] params = SmsMessage.calculateLength(s.subSequence(i, i + 1), false);
        //            /* SmsMessage.calculateLength returns an int[4] with:
        //             *   int[0] being the number of SMS's required,
        //             *   int[1] the number of code units used,
        //             *   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.
        //             *///  www .  j a  v  a  2s .  c om
        int remainingInCurrentMessage = params[2];
        //   Log.d("chenshu","MessageUtils->>findSpanishChar->>remainingInCurrentMessage->>"+remainingInCurrentMessage);
        if (remainingInCurrentMessage > 70) {
            //   Log.d("chenshu","MessageUtils->>findSpanishChar->>Default 7-bit char");
            sPs.append(tmpC);
            continue;
        }

        switch (tmpi) {
        case 0x00C0:
        case 0x00C1:
        case 0x00C2:
        case 0x00C3:
            //   Log.d("chenshu","MessageUtils->>findSpanishChar->>convert to A");
            sPs.append('A');
            break;
        case 0x00C8:
        case 0x00CA:
        case 0x00CB:
            //   Log.d("chenshu","MessageUtils->>findSpanishChar->>convert to E");
            sPs.append('E');
            break;
        case 0x00CC:
        case 0x00CD:
        case 0x00CE:
        case 0x00CF:
        case 0x0130:
            //   Log.d("chenshu","MessageUtils->>findSpanishChar->>convert to I");
            sPs.append('I');
            break;
        case 0x00D2:
        case 0x00D3:
        case 0x00D4:
        case 0x00D5:
            //   Log.d("chenshu","MessageUtils->>findSpanishChar->>convert to O");
            sPs.append('O');
            break;
        case 0x00D9:
        case 0x00DA:
        case 0x00DB:
            //   Log.d("chenshu","MessageUtils->>findSpanishChar->>convert to U");
            sPs.append('U');
            break;
        case 0x00E1:
        case 0x00E2:
        case 0x00E3:
            //   Log.d("chenshu","MessageUtils->>findSpanishChar->>convert to a");
            sPs.append('a');
            break;
        //case 0x00C7:
        case 0x00E7:
            //   Log.d("chenshu","MessageUtils->>findSpanishChar->>convert to c");
            sPs.append('c');
            break;
        case 0x00EA:
        case 0x00EB:
            //   Log.d("chenshu","MessageUtils->>findSpanishChar->>convert to e");
            sPs.append('e');
            break;
        case 0x00ED:
        case 0x00EE:
        case 0x00EF:
        case 0x0131:
            //   Log.d("chenshu","MessageUtils->>findSpanishChar->>convert to i");
            sPs.append('i');
            break;
        case 0x00F3:
        case 0x00F4:
        case 0x00F5:
            //   Log.d("chenshu","MessageUtils->>findSpanishChar->>convert to o");
            sPs.append('o');
            break;
        case 0x00FA:
        case 0x00FB:
            //   Log.d("chenshu","MessageUtils->>findSpanishChar->>convert to u");
            sPs.append('u');
            break;
        case 0x011E:
            sPs.append('G');
            break;
        case 0x011F:
            sPs.append('g');
            break;
        case 0x015E:
            sPs.append('S');
            break;
        case 0x015F:
            sPs.append('s');
            break;
        default:
            if (tmpC > 127 || tmpC < 0) {
                //      Log.e("chenshu","MessageUtil->>findSpanishChar->>becaus it was not ascii char->>tmpC->>"+tmpC);
                return null;
            } else {
                sPs.append(tmpC);
            }
            break;
        }
        //   Log.e("chenshu","MessageUtil->>findSpanishChar->>tmpC->>"+tmpC);
    }

    if (sPs.toString().endsWith(s.toString())) {
        return null;
    } else {
        //   Log.e("chenshu","MessageUtil->>findSpanishChar->>mRepliceText->>"+sPs.toString());
        return sPs.toString();
    }
}