summaryrefslogtreecommitdiffstats
path: root/src/com/android/bluetooth/map/BluetoothMapbMessageSms.java
blob: 2da76fd811dda6a85b447e829aeffdd101bc2221 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* Copyright (C) 2013 Samsung System LSI
* 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.bluetooth.map;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import android.util.Log;

import com.android.bluetooth.map.BluetoothMapSmsPdu.SmsPdu;
import com.android.bluetooth.map.BluetoothMapUtils.TYPE;

public class BluetoothMapbMessageSms extends BluetoothMapbMessage {
    private static final boolean D = BluetoothMapService.DEBUG;
    private static final boolean V = Log.isLoggable(BluetoothMapService.LOG_TAG, Log.VERBOSE) ? true : false;

    private ArrayList<SmsPdu> smsBodyPdus = null;
    private String smsBody = null;
    private String PCM_CARKIT = "9C:DF:03";
    private String FORD_SYNC_CARKIT ="00:1E:AE";

    public void setSmsBodyPdus(ArrayList<SmsPdu> smsBodyPdus) {
        this.smsBodyPdus = smsBodyPdus;
        this.charset = null;
        if(smsBodyPdus.size() > 0)
            this.encoding = smsBodyPdus.get(0).getEncodingString();
    }

    public String getSmsBody() {
        return smsBody;
    }

    public void setSmsBody(String smsBody) {
        this.smsBody = smsBody;
        this.charset = "UTF-8";
        this.encoding = null;
    }

    @Override
    public void parseMsgPart(String msgPart) {
        if(appParamCharset == BluetoothMapAppParams.CHARSET_NATIVE) {
            if(D) Log.d(TAG, "Decoding \"" + msgPart + "\" as native PDU");
            byte[] msgBytes = decodeBinary(msgPart);
            if(msgBytes.length > 0 &&
                    msgBytes[0] < msgBytes.length-1 &&
                    (msgBytes[msgBytes[0]+1] & 0x03) != 0x01) {
                if(D) Log.d(TAG, "Only submit PDUs are supported");
                throw new IllegalArgumentException("Only submit PDUs are supported");
            }

            smsBody += BluetoothMapSmsPdu.decodePdu(msgBytes,
                    type == TYPE.SMS_CDMA ? BluetoothMapSmsPdu.SMS_TYPE_CDMA
                                          : BluetoothMapSmsPdu.SMS_TYPE_GSM);
        } else {
            smsBody += msgPart;
        }
    }
    @Override
    public void parseMsgInit() {
        smsBody = "";
    }

    public byte[] encode() throws UnsupportedEncodingException
    {
        ArrayList<byte[]> bodyFragments = new ArrayList<byte[]>();

        /* Store the messages in an ArrayList to be able to handle the different message types in a generic way.
         * We use byte[] since we need to extract the length in bytes.
         */
        if(smsBody != null) {
            String tmpBody = smsBody.replaceAll("END:MSG", "/END\\:MSG"); // Replace any occurrences of END:MSG with \END:MSG
            if(V) Log.v(TAG,"smsBody is" +smsBody);

            /* fix iot issue with PCM carkit where carkit is unable to parse
               message if carriage return is present in it */
            if(BluetoothMapService.getRemoteDevice().getAddress().startsWith(PCM_CARKIT)) {
               tmpBody = tmpBody.replaceAll("\r", "");
            } else if(BluetoothMapService.getRemoteDevice().getAddress().startsWith(FORD_SYNC_CARKIT)) {
               tmpBody = tmpBody.replaceAll("\n", "");
            }

            bodyFragments.add(tmpBody.getBytes("UTF-8"));
        }else if (smsBodyPdus != null && smsBodyPdus.size() > 0) {
            for (SmsPdu pdu : smsBodyPdus) {
                // This cannot(must not) contain END:MSG
                bodyFragments.add(encodeBinary(pdu.getData(),pdu.getScAddress()).getBytes("UTF-8"));
            }
        } else {
            bodyFragments.add(new byte[0]); // TODO: Is this allowed? (An empty message)
        }

        return encodeGeneric(bodyFragments);
    }

}