BluetoothMapbMessageSms.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 22import com.android.bluetooth.map.BluetoothMapSmsPdu.SmsPdu; 23import com.android.bluetooth.map.BluetoothMapUtils.TYPE; 24 25public class BluetoothMapbMessageSms extends BluetoothMapbMessage { 26 27 private ArrayList<SmsPdu> mSmsBodyPdus = null; 28 private String mSmsBody = null; 29 30 public void setSmsBodyPdus(ArrayList<SmsPdu> smsBodyPdus) { 31 this.mSmsBodyPdus = smsBodyPdus; 32 this.mCharset = null; 33 if(smsBodyPdus.size() > 0) 34 this.mEncoding = smsBodyPdus.get(0).getEncodingString(); 35 } 36 37 public String getSmsBody() { 38 return mSmsBody; 39 } 40 41 public void setSmsBody(String smsBody) { 42 this.mSmsBody = smsBody; 43 this.mCharset = "UTF-8"; 44 this.mEncoding = null; 45 } 46 47 @Override 48 public void parseMsgPart(String msgPart) { 49 if(mAppParamCharset == 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 mSmsBody += BluetoothMapSmsPdu.decodePdu(msgBytes, 60 mType == TYPE.SMS_CDMA ? BluetoothMapSmsPdu.SMS_TYPE_CDMA 61 : BluetoothMapSmsPdu.SMS_TYPE_GSM); 62 } else { 63 mSmsBody += msgPart; 64 } 65 } 66 @Override 67 public void parseMsgInit() { 68 mSmsBody = ""; 69 } 70 71 @Override 72 public byte[] encode() throws UnsupportedEncodingException 73 { 74 ArrayList<byte[]> bodyFragments = new ArrayList<byte[]>(); 75 76 /* Store the messages in an ArrayList to be able to handle the different message types in a generic way. 77 * We use byte[] since we need to extract the length in bytes. 78 */ 79 if(mSmsBody != null) { 80 String tmpBody = mSmsBody.replaceAll("END:MSG", "/END\\:MSG"); // Replace any occurrences of END:MSG with \END:MSG 81 bodyFragments.add(tmpBody.getBytes("UTF-8")); 82 }else if (mSmsBodyPdus != null && mSmsBodyPdus.size() > 0) { 83 for (SmsPdu pdu : mSmsBodyPdus) { 84 // This cannot(must not) contain END:MSG 85 bodyFragments.add(encodeBinary(pdu.getData(),pdu.getScAddress()).getBytes("UTF-8")); 86 } 87 } else { 88 bodyFragments.add(new byte[0]); // An empty message - no text 89 } 90 91 return encodeGeneric(bodyFragments); 92 } 93 94} 95