1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.internal.telephony.cdma;
18
19import android.hardware.radio.V1_0.CdmaSmsMessage;
20import android.os.Parcel;
21import android.os.SystemProperties;
22import android.telephony.PhoneNumberUtils;
23import android.telephony.SmsCbLocation;
24import android.telephony.SmsCbMessage;
25import android.telephony.TelephonyManager;
26import android.telephony.cdma.CdmaSmsCbProgramData;
27import android.telephony.Rlog;
28import android.util.Log;
29import android.text.TextUtils;
30import android.content.res.Resources;
31
32import com.android.internal.telephony.GsmAlphabet.TextEncodingDetails;
33import com.android.internal.telephony.SmsConstants;
34import com.android.internal.telephony.SmsHeader;
35import com.android.internal.telephony.SmsMessageBase;
36import com.android.internal.telephony.TelephonyProperties;
37import com.android.internal.telephony.cdma.sms.BearerData;
38import com.android.internal.telephony.cdma.sms.CdmaSmsAddress;
39import com.android.internal.telephony.cdma.sms.CdmaSmsSubaddress;
40import com.android.internal.telephony.cdma.sms.SmsEnvelope;
41import com.android.internal.telephony.cdma.sms.UserData;
42import com.android.internal.telephony.uicc.IccUtils;
43import com.android.internal.util.BitwiseInputStream;
44import com.android.internal.util.HexDump;
45import com.android.internal.telephony.Sms7BitEncodingTranslator;
46
47import java.io.BufferedOutputStream;
48import java.io.ByteArrayInputStream;
49import java.io.ByteArrayOutputStream;
50import java.io.DataInputStream;
51import java.io.DataOutputStream;
52import java.io.IOException;
53import java.util.ArrayList;
54
55/**
56 * A Factory class to convert from RIL to Framework SMS
57 *
58 */
59public class SmsMessageConverter {
60    static final String LOG_TAG = "SmsMessageConverter";
61    static private final String LOGGABLE_TAG = "CDMA:SMS";
62    private static final boolean VDBG = false;
63
64    /**
65     *  Create a "raw" CDMA SmsMessage from a Parcel that was forged in ril.cpp.
66     *  Note: Only primitive fields are set.
67     */
68    public static SmsMessage newCdmaSmsMessageFromRil(
69            CdmaSmsMessage cdmaSmsMessage) {
70        // Note: Parcel.readByte actually reads one Int and masks to byte
71        SmsEnvelope env = new SmsEnvelope();
72        CdmaSmsAddress addr = new CdmaSmsAddress();
73        CdmaSmsSubaddress subaddr = new CdmaSmsSubaddress();
74        byte[] data;
75        byte count;
76        int countInt;
77        int addressDigitMode;
78
79        //currently not supported by the modem-lib: env.mMessageType
80        env.teleService = cdmaSmsMessage.teleserviceId;
81
82        if (cdmaSmsMessage.isServicePresent) {
83            env.messageType = SmsEnvelope.MESSAGE_TYPE_BROADCAST;
84        }
85        else {
86            if (SmsEnvelope.TELESERVICE_NOT_SET == env.teleService) {
87                // assume type ACK
88                env.messageType = SmsEnvelope.MESSAGE_TYPE_ACKNOWLEDGE;
89            } else {
90                env.messageType = SmsEnvelope.MESSAGE_TYPE_POINT_TO_POINT;
91            }
92        }
93        env.serviceCategory = cdmaSmsMessage.serviceCategory;
94
95        // address
96        addressDigitMode = cdmaSmsMessage.address.digitMode;
97        addr.digitMode = (byte) (0xFF & addressDigitMode);
98        addr.numberMode = (byte) (0xFF & cdmaSmsMessage.address.numberMode);
99        addr.ton = cdmaSmsMessage.address.numberType;
100        addr.numberPlan = (byte) (0xFF & cdmaSmsMessage.address.numberPlan);
101        count = (byte) cdmaSmsMessage.address.digits.size();
102        addr.numberOfDigits = count;
103        data = new byte[count];
104        for (int index=0; index < count; index++) {
105            data[index] = cdmaSmsMessage.address.digits.get(index);
106
107            // convert the value if it is 4-bit DTMF to 8 bit
108            if (addressDigitMode == CdmaSmsAddress.DIGIT_MODE_4BIT_DTMF) {
109                data[index] = SmsMessage.convertDtmfToAscii(data[index]);
110            }
111        }
112
113        addr.origBytes = data;
114
115        subaddr.type = cdmaSmsMessage.subAddress.subaddressType;
116        subaddr.odd = (byte) (cdmaSmsMessage.subAddress.odd ? 1 : 0);
117        count = (byte) cdmaSmsMessage.subAddress.digits.size();
118
119        if (count < 0) {
120            count = 0;
121        }
122
123        // p_cur->sSubAddress.digits[digitCount] :
124
125        data = new byte[count];
126
127        for (int index = 0; index < count; ++index) {
128            data[index] = cdmaSmsMessage.subAddress.digits.get(index);
129        }
130
131        subaddr.origBytes = data;
132
133        /* currently not supported by the modem-lib:
134            env.bearerReply
135            env.replySeqNo
136            env.errorClass
137            env.causeCode
138        */
139
140        // bearer data
141        countInt = cdmaSmsMessage.bearerData.size();
142        if (countInt < 0) {
143            countInt = 0;
144        }
145
146        data = new byte[countInt];
147        for (int index=0; index < countInt; index++) {
148            data[index] = cdmaSmsMessage.bearerData.get(index);
149        }
150        // BD gets further decoded when accessed in SMSDispatcher
151        env.bearerData = data;
152
153        // link the the filled objects to the SMS
154        env.origAddress = addr;
155        env.origSubaddress = subaddr;
156
157        SmsMessage msg = new SmsMessage(addr, env);
158
159        return msg;
160    }
161
162    public static android.telephony.SmsMessage newSmsMessageFromCdmaSmsMessage(
163            CdmaSmsMessage msg) {
164        return new android.telephony.SmsMessage((SmsMessageBase)newCdmaSmsMessageFromRil(msg));
165    }
166}
167