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