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