1/*
2 * Copyright (C) 2014 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.bluetooth.mapclient;
18
19import com.android.vcard.VCardEntry;
20import com.android.vcard.VCardEntry.EmailData;
21import com.android.vcard.VCardEntry.NameData;
22import com.android.vcard.VCardEntry.PhoneData;
23
24import java.util.List;
25/* BMessage as defined by MAP_SPEC_V101 Section 3.1.3 Message format (x-bt/message) */
26class BmessageBuilder {
27    private final static String CRLF = "\r\n";
28    private final static String BMSG_BEGIN = "BEGIN:BMSG";
29    private final static String BMSG_VERSION = "VERSION:1.0";
30    private final static String BMSG_STATUS = "STATUS:";
31    private final static String BMSG_TYPE = "TYPE:";
32    private final static String BMSG_FOLDER = "FOLDER:";
33    private final static String BMSG_END = "END:BMSG";
34
35    private final static String BENV_BEGIN = "BEGIN:BENV";
36    private final static String BENV_END = "END:BENV";
37
38    private final static String BBODY_BEGIN = "BEGIN:BBODY";
39    private final static String BBODY_ENCODING = "ENCODING:";
40    private final static String BBODY_CHARSET = "CHARSET:";
41    private final static String BBODY_LANGUAGE = "LANGUAGE:";
42    private final static String BBODY_LENGTH = "LENGTH:";
43    private final static String BBODY_END = "END:BBODY";
44
45    private final static String MSG_BEGIN = "BEGIN:MSG";
46    private final static String MSG_END = "END:MSG";
47
48    private final static String VCARD_BEGIN = "BEGIN:VCARD";
49    private final static String VCARD_VERSION = "VERSION:2.1";
50    private final static String VCARD_N = "N:";
51    private final static String VCARD_EMAIL = "EMAIL:";
52    private final static String VCARD_TEL = "TEL:";
53    private final static String VCARD_END = "END:VCARD";
54
55    private final StringBuilder mBmsg;
56
57    private BmessageBuilder() {
58        mBmsg = new StringBuilder();
59    }
60
61    static public String createBmessage(Bmessage bmsg) {
62        BmessageBuilder b = new BmessageBuilder();
63
64        b.build(bmsg);
65
66        return b.mBmsg.toString();
67    }
68
69    private void build(Bmessage bmsg) {
70        int bodyLen = MSG_BEGIN.length() + MSG_END.length() + 3 * CRLF.length()
71                + bmsg.mMessage.getBytes().length;
72
73        mBmsg.append(BMSG_BEGIN).append(CRLF);
74
75        mBmsg.append(BMSG_VERSION).append(CRLF);
76        mBmsg.append(BMSG_STATUS).append(bmsg.mBmsgStatus).append(CRLF);
77        mBmsg.append(BMSG_TYPE).append(bmsg.mBmsgType).append(CRLF);
78        mBmsg.append(BMSG_FOLDER).append(bmsg.mBmsgFolder).append(CRLF);
79
80        for (VCardEntry vcard : bmsg.mOriginators) {
81            buildVcard(vcard);
82        }
83
84        {
85            mBmsg.append(BENV_BEGIN).append(CRLF);
86
87            for (VCardEntry vcard : bmsg.mRecipients) {
88                buildVcard(vcard);
89            }
90
91            {
92                mBmsg.append(BBODY_BEGIN).append(CRLF);
93
94                if (bmsg.mBbodyEncoding != null) {
95                    mBmsg.append(BBODY_ENCODING).append(bmsg.mBbodyEncoding).append(CRLF);
96                }
97
98                if (bmsg.mBbodyCharset != null) {
99                    mBmsg.append(BBODY_CHARSET).append(bmsg.mBbodyCharset).append(CRLF);
100                }
101
102                if (bmsg.mBbodyLanguage != null) {
103                    mBmsg.append(BBODY_LANGUAGE).append(bmsg.mBbodyLanguage).append(CRLF);
104                }
105
106                mBmsg.append(BBODY_LENGTH).append(bodyLen).append(CRLF);
107
108                {
109                    mBmsg.append(MSG_BEGIN).append(CRLF);
110
111                    mBmsg.append(bmsg.mMessage).append(CRLF);
112
113                    mBmsg.append(MSG_END).append(CRLF);
114                }
115
116                mBmsg.append(BBODY_END).append(CRLF);
117            }
118
119            mBmsg.append(BENV_END).append(CRLF);
120        }
121
122        mBmsg.append(BMSG_END).append(CRLF);
123    }
124
125    private void buildVcard(VCardEntry vcard) {
126        String n = buildVcardN(vcard);
127        List<PhoneData> tel = vcard.getPhoneList();
128        List<EmailData> email = vcard.getEmailList();
129
130        mBmsg.append(VCARD_BEGIN).append(CRLF);
131
132        mBmsg.append(VCARD_VERSION).append(CRLF);
133
134        mBmsg.append(VCARD_N).append(n).append(CRLF);
135
136        if (tel != null && tel.size() > 0) {
137            mBmsg.append(VCARD_TEL).append(tel.get(0).getNumber()).append(CRLF);
138        }
139
140        if (email != null && email.size() > 0) {
141            mBmsg.append(VCARD_EMAIL).append(email.get(0).getAddress()).append(CRLF);
142        }
143
144        mBmsg.append(VCARD_END).append(CRLF);
145    }
146
147    private String buildVcardN(VCardEntry vcard) {
148        NameData nd = vcard.getNameData();
149        StringBuilder sb = new StringBuilder();
150
151        sb.append(nd.getFamily()).append(";");
152        sb.append(nd.getGiven() == null ? "" : nd.getGiven()).append(";");
153        sb.append(nd.getMiddle() == null ? "" : nd.getMiddle()).append(";");
154        sb.append(nd.getPrefix() == null ? "" : nd.getPrefix()).append(";");
155        sb.append(nd.getSuffix() == null ? "" : nd.getSuffix());
156
157        return sb.toString();
158    }
159}
160