BluetoothMapbMessageEmail.java revision 2fbb1d97d08d5d72fe824e543c714e56cd7be236
1/*
2* Copyright (C) 2013 Samsung System LSI
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7*      http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14*/
15package com.android.bluetooth.map;
16
17import java.io.UnsupportedEncodingException;
18import java.util.ArrayList;
19
20import android.util.Log;
21
22
23public class BluetoothMapbMessageEmail extends BluetoothMapbMessage {
24
25    private String mEmailBody = null;
26
27    public void setEmailBody(String emailBody) {
28        this.mEmailBody = emailBody;
29        this.mCharset = "UTF-8";
30        this.mEncoding = "8bit";
31    }
32
33    public String getEmailBody() {
34        return mEmailBody;
35    }
36
37    @Override
38    public void parseMsgPart(String msgPart) {
39        if (mEmailBody == null)
40            mEmailBody = msgPart;
41        else
42            mEmailBody += msgPart;
43    }
44
45    /**
46     * Set initial values before parsing - will be called is a message body is found
47     * during parsing.
48     */
49    @Override
50    public void parseMsgInit() {
51        // Not used for e-mail
52    }
53
54    @Override
55    public byte[] encode() throws UnsupportedEncodingException
56    {
57        ArrayList<byte[]> bodyFragments = new ArrayList<byte[]>();
58
59        /* Store the messages in an ArrayList to be able to handle the different message types in a generic way.
60         * We use byte[] since we need to extract the length in bytes. */
61        if(mEmailBody != null) {
62            String tmpBody = mEmailBody.replaceAll("END:MSG", "/END\\:MSG"); // Replace any occurrences of END:MSG with \END:MSG
63            bodyFragments.add(tmpBody.getBytes("UTF-8"));
64        } else {
65            Log.e(TAG, "Email has no body - this should not be possible");
66            bodyFragments.add(new byte[0]); // An empty message - this should not be possible
67        }
68        return encodeGeneric(bodyFragments);
69    }
70
71}
72