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 java.io.UnsupportedEncodingException; 18 import java.util.ArrayList; 19 20 import android.util.Log; 21 22 import com.android.bluetooth.map.BluetoothMapSmsPdu.SmsPdu; 23 import com.android.bluetooth.map.BluetoothMapUtils.TYPE; 24 25 public class BluetoothMapbMessageSms extends BluetoothMapbMessage { 26 27 private ArrayList<SmsPdu> smsBodyPdus = null; 28 private String smsBody = null; 29 30 public void setSmsBodyPdus(ArrayList<SmsPdu> smsBodyPdus) { 31 this.smsBodyPdus = smsBodyPdus; 32 this.charset = null; 33 if(smsBodyPdus.size() > 0) 34 this.encoding = smsBodyPdus.get(0).getEncodingString(); 35 } 36 37 public String getSmsBody() { 38 return smsBody; 39 } 40 41 public void setSmsBody(String smsBody) { 42 this.smsBody = smsBody; 43 this.charset = "UTF-8"; 44 this.encoding = null; 45 } 46 47 @Override 48 public void parseMsgPart(String msgPart) { 49 if(appParamCharset == BluetoothMapAppParams.CHARSET_NATIVE) { 50 if(D) Log.d(TAG, "Decoding \"" + msgPart + "\" as native PDU"); 51 byte[] msgBytes = decodeBinary(msgPart); 52 if(msgBytes.length > 0 && 53 msgBytes[0] < msgBytes.length-1 && 54 (msgBytes[msgBytes[0]+1] & 0x03) != 0x01) { 55 if(D) Log.d(TAG, "Only submit PDUs are supported"); 56 throw new IllegalArgumentException("Only submit PDUs are supported"); 57 } 58 59 smsBody += BluetoothMapSmsPdu.decodePdu(msgBytes, 60 type == TYPE.SMS_CDMA ? BluetoothMapSmsPdu.SMS_TYPE_CDMA 61 : BluetoothMapSmsPdu.SMS_TYPE_GSM); 62 } else { 63 smsBody += msgPart; 64 } 65 } 66 @Override 67 public void parseMsgInit() { 68 smsBody = ""; 69 } 70 71 public byte[] encode() throws UnsupportedEncodingException 72 { 73 ArrayList<byte[]> bodyFragments = new ArrayList<byte[]>(); 74 75 /* Store the messages in an ArrayList to be able to handle the different message types in a generic way. 76 * We use byte[] since we need to extract the length in bytes. 77 */ 78 if(smsBody != null) { 79 String tmpBody = smsBody.replaceAll("END:MSG", "/END\\:MSG"); // Replace any occurrences of END:MSG with \END:MSG 80 bodyFragments.add(tmpBody.getBytes("UTF-8")); 81 }else if (smsBodyPdus != null && smsBodyPdus.size() > 0) { 82 for (SmsPdu pdu : smsBodyPdus) { 83 // This cannot(must not) contain END:MSG 84 bodyFragments.add(encodeBinary(pdu.getData(),pdu.getScAddress()).getBytes("UTF-8")); 85 } 86 } else { 87 bodyFragments.add(new byte[0]); // TODO: Is this allowed? (An empty message) 88 } 89 90 return encodeGeneric(bodyFragments); 91 } 92 93 } 94