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  */
15 package com.android.bluetooth.map;
16 
17 import android.bluetooth.BluetoothProfile;
18 import android.bluetooth.BluetoothProtoEnums;
19 import android.util.Log;
20 
21 import com.android.bluetooth.BluetoothStatsLog;
22 import com.android.bluetooth.content_profiles.ContentProfileErrorReportUtils;
23 
24 import java.io.UnsupportedEncodingException;
25 import java.util.ArrayList;
26 
27 // Next tag value for ContentProfileErrorReportUtils.report(): 1
28 public class BluetoothMapbMessageEmail extends BluetoothMapbMessage {
29 
30     private String mEmailBody = null;
31 
setEmailBody(String emailBody)32     public void setEmailBody(String emailBody) {
33         this.mEmailBody = emailBody;
34         this.mCharset = "UTF-8";
35         this.mEncoding = "8bit";
36     }
37 
getEmailBody()38     public String getEmailBody() {
39         return mEmailBody;
40     }
41 
42     @Override
parseMsgPart(String msgPart)43     public void parseMsgPart(String msgPart) {
44         if (mEmailBody == null) {
45             mEmailBody = msgPart;
46         } else {
47             mEmailBody += msgPart;
48         }
49     }
50 
51     /**
52      * Set initial values before parsing - will be called is a message body is found during parsing.
53      */
54     @Override
parseMsgInit()55     public void parseMsgInit() {
56         // Not used for e-mail
57     }
58 
59     @Override
encode()60     public byte[] encode() throws UnsupportedEncodingException {
61         ArrayList<byte[]> bodyFragments = new ArrayList<byte[]>();
62 
63         /* Store the messages in an ArrayList to be able to handle the different message types in
64         a generic way.
65         * We use byte[] since we need to extract the length in bytes. */
66         if (mEmailBody != null) {
67             String tmpBody =
68                     mEmailBody.replaceAll(
69                             "END:MSG",
70                             "/END\\:MSG"); // Replace any occurrences of END:MSG with \END:MSG
71             bodyFragments.add(tmpBody.getBytes("UTF-8"));
72         } else {
73             Log.e(TAG, "Email has no body - this should not be possible");
74             ContentProfileErrorReportUtils.report(
75                     BluetoothProfile.MAP,
76                     BluetoothProtoEnums.BLUETOOTH_MAP_BMESSAGE_EMAIL,
77                     BluetoothStatsLog.BLUETOOTH_CONTENT_PROFILE_ERROR_REPORTED__TYPE__LOG_ERROR,
78                     0);
79             bodyFragments.add(new byte[0]); // An empty message - this should not be possible
80         }
81         return encodeGeneric(bodyFragments);
82     }
83 }
84