summaryrefslogtreecommitdiffstats
path: root/src/com/android/messaging/datamodel/MessageTextStats.java
blob: 2bd24ffb4546d19503a66afaf4424e38df6d7985 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.messaging.datamodel;

import android.telephony.SmsMessage;

import com.android.messaging.sms.MmsConfig;

public class MessageTextStats {
    private boolean mMessageLengthRequiresMms;
    private int mMessageCount;
    private int mCodePointsRemainingInCurrentMessage;

    public MessageTextStats() {
        mCodePointsRemainingInCurrentMessage = Integer.MAX_VALUE;
    }

    public int getNumMessagesToBeSent() {
        return mMessageCount;
    }

    public int getCodePointsRemainingInCurrentMessage() {
        return mCodePointsRemainingInCurrentMessage;
    }

    public boolean getMessageLengthRequiresMms() {
        return mMessageLengthRequiresMms;
    }

    public void updateMessageTextStats(final int selfSubId, final String messageText) {
        final int[] params = SmsMessage.calculateLength(messageText, false);
        /* SmsMessage.calculateLength returns an int[4] with:
         *   int[0] being the number of SMS's required,
         *   int[1] the number of code points used,
         *   int[2] is the number of code points remaining until the next message.
         *   int[3] is the encoding type that should be used for the message.
         */
        mMessageCount = params[0];
        mCodePointsRemainingInCurrentMessage = params[2];

        final MmsConfig mmsConfig = MmsConfig.get(selfSubId);
        if (!mmsConfig.getMultipartSmsEnabled() &&
                !mmsConfig.getSendMultipartSmsAsSeparateMessages()) {
            // The provider doesn't support multi-part sms's and we should use MMS to
            // send multi-part sms, so as soon as the user types
            // an sms longer than one segment, we have to turn the message into an mms.
            mMessageLengthRequiresMms = mMessageCount > 1;
        } else {
            final int threshold = mmsConfig.getSmsToMmsTextThreshold();
            mMessageLengthRequiresMms = threshold > 0 && mMessageCount > threshold;
        }
        // Some carriers require any SMS message longer than 80 to be sent as MMS
        // see b/12122333
        int smsToMmsLengthThreshold = mmsConfig.getSmsToMmsTextLengthThreshold();
        if (smsToMmsLengthThreshold > 0) {
            final int usedInCurrentMessage = params[1];
            /*
             * A little hacky way to find out if we should count characters in double bytes.
             * SmsMessage.calculateLength counts message code units based on the characters
             * in input. If all of them are ascii, the max length is
             * SmsMessage.MAX_USER_DATA_SEPTETS (160). If any of them are double-byte, like
             * Korean or Chinese, the max length is SmsMessage.MAX_USER_DATA_BYTES (140) bytes
             * (70 code units).
             * Here we check if the total code units we can use is smaller than 140. If so,
             * we know we should count threshold in double-byte, so divide the threshold by 2.
             * In this way, we will count Korean text correctly with regard to the length threshold.
             */
            if (usedInCurrentMessage + mCodePointsRemainingInCurrentMessage
                    < SmsMessage.MAX_USER_DATA_BYTES) {
                smsToMmsLengthThreshold /= 2;
            }
            if (usedInCurrentMessage > smsToMmsLengthThreshold) {
                mMessageLengthRequiresMms = true;
            }
        }
    }

}