PageRenderTime 1605ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/core/java/com/google/android/mms/pdu/PduComposer.java

http://github.com/android/platform_frameworks_base
Java | 1181 lines | 634 code | 183 blank | 364 comment | 163 complexity | 6b846b66f9fcf48c039bc10974c1408f MD5 | raw file
Possible License(s): BitTorrent-1.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, CC0-1.0
  1. /*
  2. * Copyright (C) 2007-2008 Esmertec AG.
  3. * Copyright (C) 2007-2008 The Android Open Source Project
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.google.android.mms.pdu;
  18. import android.content.ContentResolver;
  19. import android.content.Context;
  20. import android.util.Log;
  21. import android.text.TextUtils;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.FileNotFoundException;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.util.Arrays;
  27. import java.util.HashMap;
  28. public class PduComposer {
  29. /**
  30. * Address type.
  31. */
  32. static private final int PDU_PHONE_NUMBER_ADDRESS_TYPE = 1;
  33. static private final int PDU_EMAIL_ADDRESS_TYPE = 2;
  34. static private final int PDU_IPV4_ADDRESS_TYPE = 3;
  35. static private final int PDU_IPV6_ADDRESS_TYPE = 4;
  36. static private final int PDU_UNKNOWN_ADDRESS_TYPE = 5;
  37. /**
  38. * Address regular expression string.
  39. */
  40. static final String REGEXP_PHONE_NUMBER_ADDRESS_TYPE = "\\+?[0-9|\\.|\\-]+";
  41. static final String REGEXP_EMAIL_ADDRESS_TYPE = "[a-zA-Z| ]*\\<{0,1}[a-zA-Z| ]+@{1}" +
  42. "[a-zA-Z| ]+\\.{1}[a-zA-Z| ]+\\>{0,1}";
  43. static final String REGEXP_IPV6_ADDRESS_TYPE =
  44. "[a-fA-F]{4}\\:{1}[a-fA-F0-9]{4}\\:{1}[a-fA-F0-9]{4}\\:{1}" +
  45. "[a-fA-F0-9]{4}\\:{1}[a-fA-F0-9]{4}\\:{1}[a-fA-F0-9]{4}\\:{1}" +
  46. "[a-fA-F0-9]{4}\\:{1}[a-fA-F0-9]{4}";
  47. static final String REGEXP_IPV4_ADDRESS_TYPE = "[0-9]{1,3}\\.{1}[0-9]{1,3}\\.{1}" +
  48. "[0-9]{1,3}\\.{1}[0-9]{1,3}";
  49. /**
  50. * The postfix strings of address.
  51. */
  52. static final String STRING_PHONE_NUMBER_ADDRESS_TYPE = "/TYPE=PLMN";
  53. static final String STRING_IPV4_ADDRESS_TYPE = "/TYPE=IPV4";
  54. static final String STRING_IPV6_ADDRESS_TYPE = "/TYPE=IPV6";
  55. /**
  56. * Error values.
  57. */
  58. static private final int PDU_COMPOSE_SUCCESS = 0;
  59. static private final int PDU_COMPOSE_CONTENT_ERROR = 1;
  60. static private final int PDU_COMPOSE_FIELD_NOT_SET = 2;
  61. static private final int PDU_COMPOSE_FIELD_NOT_SUPPORTED = 3;
  62. /**
  63. * WAP values defined in WSP spec.
  64. */
  65. static private final int QUOTED_STRING_FLAG = 34;
  66. static private final int END_STRING_FLAG = 0;
  67. static private final int LENGTH_QUOTE = 31;
  68. static private final int TEXT_MAX = 127;
  69. static private final int SHORT_INTEGER_MAX = 127;
  70. static private final int LONG_INTEGER_LENGTH_MAX = 8;
  71. /**
  72. * Block size when read data from InputStream.
  73. */
  74. static private final int PDU_COMPOSER_BLOCK_SIZE = 1024;
  75. /**
  76. * The output message.
  77. */
  78. protected ByteArrayOutputStream mMessage = null;
  79. /**
  80. * The PDU.
  81. */
  82. private GenericPdu mPdu = null;
  83. /**
  84. * Current visiting position of the mMessage.
  85. */
  86. protected int mPosition = 0;
  87. /**
  88. * Message compose buffer stack.
  89. */
  90. private BufferStack mStack = null;
  91. /**
  92. * Content resolver.
  93. */
  94. private final ContentResolver mResolver;
  95. /**
  96. * Header of this pdu.
  97. */
  98. private PduHeaders mPduHeader = null;
  99. /**
  100. * Map of all content type
  101. */
  102. private static HashMap<String, Integer> mContentTypeMap = null;
  103. static {
  104. mContentTypeMap = new HashMap<String, Integer>();
  105. int i;
  106. for (i = 0; i < PduContentTypes.contentTypes.length; i++) {
  107. mContentTypeMap.put(PduContentTypes.contentTypes[i], i);
  108. }
  109. }
  110. /**
  111. * Constructor.
  112. *
  113. * @param context the context
  114. * @param pdu the pdu to be composed
  115. */
  116. public PduComposer(Context context, GenericPdu pdu) {
  117. mPdu = pdu;
  118. mResolver = context.getContentResolver();
  119. mPduHeader = pdu.getPduHeaders();
  120. mStack = new BufferStack();
  121. mMessage = new ByteArrayOutputStream();
  122. mPosition = 0;
  123. }
  124. /**
  125. * Make the message. No need to check whether mandatory fields are set,
  126. * because the constructors of outgoing pdus are taking care of this.
  127. *
  128. * @return OutputStream of maked message. Return null if
  129. * the PDU is invalid.
  130. */
  131. public byte[] make() {
  132. // Get Message-type.
  133. int type = mPdu.getMessageType();
  134. /* make the message */
  135. switch (type) {
  136. case PduHeaders.MESSAGE_TYPE_SEND_REQ:
  137. if (makeSendReqPdu() != PDU_COMPOSE_SUCCESS) {
  138. return null;
  139. }
  140. break;
  141. case PduHeaders.MESSAGE_TYPE_NOTIFYRESP_IND:
  142. if (makeNotifyResp() != PDU_COMPOSE_SUCCESS) {
  143. return null;
  144. }
  145. break;
  146. case PduHeaders.MESSAGE_TYPE_ACKNOWLEDGE_IND:
  147. if (makeAckInd() != PDU_COMPOSE_SUCCESS) {
  148. return null;
  149. }
  150. break;
  151. case PduHeaders.MESSAGE_TYPE_READ_REC_IND:
  152. if (makeReadRecInd() != PDU_COMPOSE_SUCCESS) {
  153. return null;
  154. }
  155. break;
  156. default:
  157. return null;
  158. }
  159. return mMessage.toByteArray();
  160. }
  161. /**
  162. * Copy buf to mMessage.
  163. */
  164. protected void arraycopy(byte[] buf, int pos, int length) {
  165. mMessage.write(buf, pos, length);
  166. mPosition = mPosition + length;
  167. }
  168. /**
  169. * Append a byte to mMessage.
  170. */
  171. protected void append(int value) {
  172. mMessage.write(value);
  173. mPosition ++;
  174. }
  175. /**
  176. * Append short integer value to mMessage.
  177. * This implementation doesn't check the validity of parameter, since it
  178. * assumes that the values are validated in the GenericPdu setter methods.
  179. */
  180. protected void appendShortInteger(int value) {
  181. /*
  182. * From WAP-230-WSP-20010705-a:
  183. * Short-integer = OCTET
  184. * ; Integers in range 0-127 shall be encoded as a one octet value
  185. * ; with the most significant bit set to one (1xxx xxxx) and with
  186. * ; the value in the remaining least significant bits.
  187. * In our implementation, only low 7 bits are stored and otherwise
  188. * bits are ignored.
  189. */
  190. append((value | 0x80) & 0xff);
  191. }
  192. /**
  193. * Append an octet number between 128 and 255 into mMessage.
  194. * NOTE:
  195. * A value between 0 and 127 should be appended by using appendShortInteger.
  196. * This implementation doesn't check the validity of parameter, since it
  197. * assumes that the values are validated in the GenericPdu setter methods.
  198. */
  199. protected void appendOctet(int number) {
  200. append(number);
  201. }
  202. /**
  203. * Append a short length into mMessage.
  204. * This implementation doesn't check the validity of parameter, since it
  205. * assumes that the values are validated in the GenericPdu setter methods.
  206. */
  207. protected void appendShortLength(int value) {
  208. /*
  209. * From WAP-230-WSP-20010705-a:
  210. * Short-length = <Any octet 0-30>
  211. */
  212. append(value);
  213. }
  214. /**
  215. * Append long integer into mMessage. it's used for really long integers.
  216. * This implementation doesn't check the validity of parameter, since it
  217. * assumes that the values are validated in the GenericPdu setter methods.
  218. */
  219. protected void appendLongInteger(long longInt) {
  220. /*
  221. * From WAP-230-WSP-20010705-a:
  222. * Long-integer = Short-length Multi-octet-integer
  223. * ; The Short-length indicates the length of the Multi-octet-integer
  224. * Multi-octet-integer = 1*30 OCTET
  225. * ; The content octets shall be an unsigned integer value with the
  226. * ; most significant octet encoded first (big-endian representation).
  227. * ; The minimum number of octets must be used to encode the value.
  228. */
  229. int size;
  230. long temp = longInt;
  231. // Count the length of the long integer.
  232. for(size = 0; (temp != 0) && (size < LONG_INTEGER_LENGTH_MAX); size++) {
  233. temp = (temp >>> 8);
  234. }
  235. // Set Length.
  236. appendShortLength(size);
  237. // Count and set the long integer.
  238. int i;
  239. int shift = (size -1) * 8;
  240. for (i = 0; i < size; i++) {
  241. append((int)((longInt >>> shift) & 0xff));
  242. shift = shift - 8;
  243. }
  244. }
  245. /**
  246. * Append text string into mMessage.
  247. * This implementation doesn't check the validity of parameter, since it
  248. * assumes that the values are validated in the GenericPdu setter methods.
  249. */
  250. protected void appendTextString(byte[] text) {
  251. /*
  252. * From WAP-230-WSP-20010705-a:
  253. * Text-string = [Quote] *TEXT End-of-string
  254. * ; If the first character in the TEXT is in the range of 128-255,
  255. * ; a Quote character must precede it. Otherwise the Quote character
  256. * ;must be omitted. The Quote is not part of the contents.
  257. */
  258. if (((text[0])&0xff) > TEXT_MAX) { // No need to check for <= 255
  259. append(TEXT_MAX);
  260. }
  261. arraycopy(text, 0, text.length);
  262. append(0);
  263. }
  264. /**
  265. * Append text string into mMessage.
  266. * This implementation doesn't check the validity of parameter, since it
  267. * assumes that the values are validated in the GenericPdu setter methods.
  268. */
  269. protected void appendTextString(String str) {
  270. /*
  271. * From WAP-230-WSP-20010705-a:
  272. * Text-string = [Quote] *TEXT End-of-string
  273. * ; If the first character in the TEXT is in the range of 128-255,
  274. * ; a Quote character must precede it. Otherwise the Quote character
  275. * ;must be omitted. The Quote is not part of the contents.
  276. */
  277. appendTextString(str.getBytes());
  278. }
  279. /**
  280. * Append encoded string value to mMessage.
  281. * This implementation doesn't check the validity of parameter, since it
  282. * assumes that the values are validated in the GenericPdu setter methods.
  283. */
  284. protected void appendEncodedString(EncodedStringValue enStr) {
  285. /*
  286. * From OMA-TS-MMS-ENC-V1_3-20050927-C:
  287. * Encoded-string-value = Text-string | Value-length Char-set Text-string
  288. */
  289. assert(enStr != null);
  290. int charset = enStr.getCharacterSet();
  291. byte[] textString = enStr.getTextString();
  292. if (null == textString) {
  293. return;
  294. }
  295. /*
  296. * In the implementation of EncodedStringValue, the charset field will
  297. * never be 0. It will always be composed as
  298. * Encoded-string-value = Value-length Char-set Text-string
  299. */
  300. mStack.newbuf();
  301. PositionMarker start = mStack.mark();
  302. appendShortInteger(charset);
  303. appendTextString(textString);
  304. int len = start.getLength();
  305. mStack.pop();
  306. appendValueLength(len);
  307. mStack.copy();
  308. }
  309. /**
  310. * Append uintvar integer into mMessage.
  311. * This implementation doesn't check the validity of parameter, since it
  312. * assumes that the values are validated in the GenericPdu setter methods.
  313. */
  314. protected void appendUintvarInteger(long value) {
  315. /*
  316. * From WAP-230-WSP-20010705-a:
  317. * To encode a large unsigned integer, split it into 7-bit fragments
  318. * and place them in the payloads of multiple octets. The most significant
  319. * bits are placed in the first octets with the least significant bits
  320. * ending up in the last octet. All octets MUST set the Continue bit to 1
  321. * except the last octet, which MUST set the Continue bit to 0.
  322. */
  323. int i;
  324. long max = SHORT_INTEGER_MAX;
  325. for (i = 0; i < 5; i++) {
  326. if (value < max) {
  327. break;
  328. }
  329. max = (max << 7) | 0x7fl;
  330. }
  331. while(i > 0) {
  332. long temp = value >>> (i * 7);
  333. temp = temp & 0x7f;
  334. append((int)((temp | 0x80) & 0xff));
  335. i--;
  336. }
  337. append((int)(value & 0x7f));
  338. }
  339. /**
  340. * Append date value into mMessage.
  341. * This implementation doesn't check the validity of parameter, since it
  342. * assumes that the values are validated in the GenericPdu setter methods.
  343. */
  344. protected void appendDateValue(long date) {
  345. /*
  346. * From OMA-TS-MMS-ENC-V1_3-20050927-C:
  347. * Date-value = Long-integer
  348. */
  349. appendLongInteger(date);
  350. }
  351. /**
  352. * Append value length to mMessage.
  353. * This implementation doesn't check the validity of parameter, since it
  354. * assumes that the values are validated in the GenericPdu setter methods.
  355. */
  356. protected void appendValueLength(long value) {
  357. /*
  358. * From WAP-230-WSP-20010705-a:
  359. * Value-length = Short-length | (Length-quote Length)
  360. * ; Value length is used to indicate the length of the value to follow
  361. * Short-length = <Any octet 0-30>
  362. * Length-quote = <Octet 31>
  363. * Length = Uintvar-integer
  364. */
  365. if (value < LENGTH_QUOTE) {
  366. appendShortLength((int) value);
  367. return;
  368. }
  369. append(LENGTH_QUOTE);
  370. appendUintvarInteger(value);
  371. }
  372. /**
  373. * Append quoted string to mMessage.
  374. * This implementation doesn't check the validity of parameter, since it
  375. * assumes that the values are validated in the GenericPdu setter methods.
  376. */
  377. protected void appendQuotedString(byte[] text) {
  378. /*
  379. * From WAP-230-WSP-20010705-a:
  380. * Quoted-string = <Octet 34> *TEXT End-of-string
  381. * ;The TEXT encodes an RFC2616 Quoted-string with the enclosing
  382. * ;quotation-marks <"> removed.
  383. */
  384. append(QUOTED_STRING_FLAG);
  385. arraycopy(text, 0, text.length);
  386. append(END_STRING_FLAG);
  387. }
  388. /**
  389. * Append quoted string to mMessage.
  390. * This implementation doesn't check the validity of parameter, since it
  391. * assumes that the values are validated in the GenericPdu setter methods.
  392. */
  393. protected void appendQuotedString(String str) {
  394. /*
  395. * From WAP-230-WSP-20010705-a:
  396. * Quoted-string = <Octet 34> *TEXT End-of-string
  397. * ;The TEXT encodes an RFC2616 Quoted-string with the enclosing
  398. * ;quotation-marks <"> removed.
  399. */
  400. appendQuotedString(str.getBytes());
  401. }
  402. private EncodedStringValue appendAddressType(EncodedStringValue address) {
  403. EncodedStringValue temp = null;
  404. try {
  405. int addressType = checkAddressType(address.getString());
  406. temp = EncodedStringValue.copy(address);
  407. if (PDU_PHONE_NUMBER_ADDRESS_TYPE == addressType) {
  408. // Phone number.
  409. temp.appendTextString(STRING_PHONE_NUMBER_ADDRESS_TYPE.getBytes());
  410. } else if (PDU_IPV4_ADDRESS_TYPE == addressType) {
  411. // Ipv4 address.
  412. temp.appendTextString(STRING_IPV4_ADDRESS_TYPE.getBytes());
  413. } else if (PDU_IPV6_ADDRESS_TYPE == addressType) {
  414. // Ipv6 address.
  415. temp.appendTextString(STRING_IPV6_ADDRESS_TYPE.getBytes());
  416. }
  417. } catch (NullPointerException e) {
  418. return null;
  419. }
  420. return temp;
  421. }
  422. /**
  423. * Append header to mMessage.
  424. */
  425. private int appendHeader(int field) {
  426. switch (field) {
  427. case PduHeaders.MMS_VERSION:
  428. appendOctet(field);
  429. int version = mPduHeader.getOctet(field);
  430. if (0 == version) {
  431. appendShortInteger(PduHeaders.CURRENT_MMS_VERSION);
  432. } else {
  433. appendShortInteger(version);
  434. }
  435. break;
  436. case PduHeaders.MESSAGE_ID:
  437. case PduHeaders.TRANSACTION_ID:
  438. byte[] textString = mPduHeader.getTextString(field);
  439. if (null == textString) {
  440. return PDU_COMPOSE_FIELD_NOT_SET;
  441. }
  442. appendOctet(field);
  443. appendTextString(textString);
  444. break;
  445. case PduHeaders.TO:
  446. case PduHeaders.BCC:
  447. case PduHeaders.CC:
  448. EncodedStringValue[] addr = mPduHeader.getEncodedStringValues(field);
  449. if (null == addr) {
  450. return PDU_COMPOSE_FIELD_NOT_SET;
  451. }
  452. EncodedStringValue temp;
  453. for (int i = 0; i < addr.length; i++) {
  454. temp = appendAddressType(addr[i]);
  455. if (temp == null) {
  456. return PDU_COMPOSE_CONTENT_ERROR;
  457. }
  458. appendOctet(field);
  459. appendEncodedString(temp);
  460. }
  461. break;
  462. case PduHeaders.FROM:
  463. // Value-length (Address-present-token Encoded-string-value | Insert-address-token)
  464. appendOctet(field);
  465. EncodedStringValue from = mPduHeader.getEncodedStringValue(field);
  466. if ((from == null)
  467. || TextUtils.isEmpty(from.getString())
  468. || new String(from.getTextString()).equals(
  469. PduHeaders.FROM_INSERT_ADDRESS_TOKEN_STR)) {
  470. // Length of from = 1
  471. append(1);
  472. // Insert-address-token = <Octet 129>
  473. append(PduHeaders.FROM_INSERT_ADDRESS_TOKEN);
  474. } else {
  475. mStack.newbuf();
  476. PositionMarker fstart = mStack.mark();
  477. // Address-present-token = <Octet 128>
  478. append(PduHeaders.FROM_ADDRESS_PRESENT_TOKEN);
  479. temp = appendAddressType(from);
  480. if (temp == null) {
  481. return PDU_COMPOSE_CONTENT_ERROR;
  482. }
  483. appendEncodedString(temp);
  484. int flen = fstart.getLength();
  485. mStack.pop();
  486. appendValueLength(flen);
  487. mStack.copy();
  488. }
  489. break;
  490. case PduHeaders.READ_STATUS:
  491. case PduHeaders.STATUS:
  492. case PduHeaders.REPORT_ALLOWED:
  493. case PduHeaders.PRIORITY:
  494. case PduHeaders.DELIVERY_REPORT:
  495. case PduHeaders.READ_REPORT:
  496. int octet = mPduHeader.getOctet(field);
  497. if (0 == octet) {
  498. return PDU_COMPOSE_FIELD_NOT_SET;
  499. }
  500. appendOctet(field);
  501. appendOctet(octet);
  502. break;
  503. case PduHeaders.DATE:
  504. long date = mPduHeader.getLongInteger(field);
  505. if (-1 == date) {
  506. return PDU_COMPOSE_FIELD_NOT_SET;
  507. }
  508. appendOctet(field);
  509. appendDateValue(date);
  510. break;
  511. case PduHeaders.SUBJECT:
  512. EncodedStringValue enString =
  513. mPduHeader.getEncodedStringValue(field);
  514. if (null == enString) {
  515. return PDU_COMPOSE_FIELD_NOT_SET;
  516. }
  517. appendOctet(field);
  518. appendEncodedString(enString);
  519. break;
  520. case PduHeaders.MESSAGE_CLASS:
  521. byte[] messageClass = mPduHeader.getTextString(field);
  522. if (null == messageClass) {
  523. return PDU_COMPOSE_FIELD_NOT_SET;
  524. }
  525. appendOctet(field);
  526. if (Arrays.equals(messageClass,
  527. PduHeaders.MESSAGE_CLASS_ADVERTISEMENT_STR.getBytes())) {
  528. appendOctet(PduHeaders.MESSAGE_CLASS_ADVERTISEMENT);
  529. } else if (Arrays.equals(messageClass,
  530. PduHeaders.MESSAGE_CLASS_AUTO_STR.getBytes())) {
  531. appendOctet(PduHeaders.MESSAGE_CLASS_AUTO);
  532. } else if (Arrays.equals(messageClass,
  533. PduHeaders.MESSAGE_CLASS_PERSONAL_STR.getBytes())) {
  534. appendOctet(PduHeaders.MESSAGE_CLASS_PERSONAL);
  535. } else if (Arrays.equals(messageClass,
  536. PduHeaders.MESSAGE_CLASS_INFORMATIONAL_STR.getBytes())) {
  537. appendOctet(PduHeaders.MESSAGE_CLASS_INFORMATIONAL);
  538. } else {
  539. appendTextString(messageClass);
  540. }
  541. break;
  542. case PduHeaders.EXPIRY:
  543. long expiry = mPduHeader.getLongInteger(field);
  544. if (-1 == expiry) {
  545. return PDU_COMPOSE_FIELD_NOT_SET;
  546. }
  547. appendOctet(field);
  548. mStack.newbuf();
  549. PositionMarker expiryStart = mStack.mark();
  550. append(PduHeaders.VALUE_RELATIVE_TOKEN);
  551. appendLongInteger(expiry);
  552. int expiryLength = expiryStart.getLength();
  553. mStack.pop();
  554. appendValueLength(expiryLength);
  555. mStack.copy();
  556. break;
  557. default:
  558. return PDU_COMPOSE_FIELD_NOT_SUPPORTED;
  559. }
  560. return PDU_COMPOSE_SUCCESS;
  561. }
  562. /**
  563. * Make ReadRec.Ind.
  564. */
  565. private int makeReadRecInd() {
  566. if (mMessage == null) {
  567. mMessage = new ByteArrayOutputStream();
  568. mPosition = 0;
  569. }
  570. // X-Mms-Message-Type
  571. appendOctet(PduHeaders.MESSAGE_TYPE);
  572. appendOctet(PduHeaders.MESSAGE_TYPE_READ_REC_IND);
  573. // X-Mms-MMS-Version
  574. if (appendHeader(PduHeaders.MMS_VERSION) != PDU_COMPOSE_SUCCESS) {
  575. return PDU_COMPOSE_CONTENT_ERROR;
  576. }
  577. // Message-ID
  578. if (appendHeader(PduHeaders.MESSAGE_ID) != PDU_COMPOSE_SUCCESS) {
  579. return PDU_COMPOSE_CONTENT_ERROR;
  580. }
  581. // To
  582. if (appendHeader(PduHeaders.TO) != PDU_COMPOSE_SUCCESS) {
  583. return PDU_COMPOSE_CONTENT_ERROR;
  584. }
  585. // From
  586. if (appendHeader(PduHeaders.FROM) != PDU_COMPOSE_SUCCESS) {
  587. return PDU_COMPOSE_CONTENT_ERROR;
  588. }
  589. // Date Optional
  590. appendHeader(PduHeaders.DATE);
  591. // X-Mms-Read-Status
  592. if (appendHeader(PduHeaders.READ_STATUS) != PDU_COMPOSE_SUCCESS) {
  593. return PDU_COMPOSE_CONTENT_ERROR;
  594. }
  595. // X-Mms-Applic-ID Optional(not support)
  596. // X-Mms-Reply-Applic-ID Optional(not support)
  597. // X-Mms-Aux-Applic-Info Optional(not support)
  598. return PDU_COMPOSE_SUCCESS;
  599. }
  600. /**
  601. * Make NotifyResp.Ind.
  602. */
  603. private int makeNotifyResp() {
  604. if (mMessage == null) {
  605. mMessage = new ByteArrayOutputStream();
  606. mPosition = 0;
  607. }
  608. // X-Mms-Message-Type
  609. appendOctet(PduHeaders.MESSAGE_TYPE);
  610. appendOctet(PduHeaders.MESSAGE_TYPE_NOTIFYRESP_IND);
  611. // X-Mms-Transaction-ID
  612. if (appendHeader(PduHeaders.TRANSACTION_ID) != PDU_COMPOSE_SUCCESS) {
  613. return PDU_COMPOSE_CONTENT_ERROR;
  614. }
  615. // X-Mms-MMS-Version
  616. if (appendHeader(PduHeaders.MMS_VERSION) != PDU_COMPOSE_SUCCESS) {
  617. return PDU_COMPOSE_CONTENT_ERROR;
  618. }
  619. // X-Mms-Status
  620. if (appendHeader(PduHeaders.STATUS) != PDU_COMPOSE_SUCCESS) {
  621. return PDU_COMPOSE_CONTENT_ERROR;
  622. }
  623. // X-Mms-Report-Allowed Optional (not support)
  624. return PDU_COMPOSE_SUCCESS;
  625. }
  626. /**
  627. * Make Acknowledge.Ind.
  628. */
  629. private int makeAckInd() {
  630. if (mMessage == null) {
  631. mMessage = new ByteArrayOutputStream();
  632. mPosition = 0;
  633. }
  634. // X-Mms-Message-Type
  635. appendOctet(PduHeaders.MESSAGE_TYPE);
  636. appendOctet(PduHeaders.MESSAGE_TYPE_ACKNOWLEDGE_IND);
  637. // X-Mms-Transaction-ID
  638. if (appendHeader(PduHeaders.TRANSACTION_ID) != PDU_COMPOSE_SUCCESS) {
  639. return PDU_COMPOSE_CONTENT_ERROR;
  640. }
  641. // X-Mms-MMS-Version
  642. if (appendHeader(PduHeaders.MMS_VERSION) != PDU_COMPOSE_SUCCESS) {
  643. return PDU_COMPOSE_CONTENT_ERROR;
  644. }
  645. // X-Mms-Report-Allowed Optional
  646. appendHeader(PduHeaders.REPORT_ALLOWED);
  647. return PDU_COMPOSE_SUCCESS;
  648. }
  649. /**
  650. * Make Send.req.
  651. */
  652. private int makeSendReqPdu() {
  653. if (mMessage == null) {
  654. mMessage = new ByteArrayOutputStream();
  655. mPosition = 0;
  656. }
  657. // X-Mms-Message-Type
  658. appendOctet(PduHeaders.MESSAGE_TYPE);
  659. appendOctet(PduHeaders.MESSAGE_TYPE_SEND_REQ);
  660. // X-Mms-Transaction-ID
  661. appendOctet(PduHeaders.TRANSACTION_ID);
  662. byte[] trid = mPduHeader.getTextString(PduHeaders.TRANSACTION_ID);
  663. if (trid == null) {
  664. // Transaction-ID should be set(by Transaction) before make().
  665. throw new IllegalArgumentException("Transaction-ID is null.");
  666. }
  667. appendTextString(trid);
  668. // X-Mms-MMS-Version
  669. if (appendHeader(PduHeaders.MMS_VERSION) != PDU_COMPOSE_SUCCESS) {
  670. return PDU_COMPOSE_CONTENT_ERROR;
  671. }
  672. // Date Date-value Optional.
  673. appendHeader(PduHeaders.DATE);
  674. // From
  675. if (appendHeader(PduHeaders.FROM) != PDU_COMPOSE_SUCCESS) {
  676. return PDU_COMPOSE_CONTENT_ERROR;
  677. }
  678. boolean recipient = false;
  679. // To
  680. if (appendHeader(PduHeaders.TO) != PDU_COMPOSE_CONTENT_ERROR) {
  681. recipient = true;
  682. }
  683. // Cc
  684. if (appendHeader(PduHeaders.CC) != PDU_COMPOSE_CONTENT_ERROR) {
  685. recipient = true;
  686. }
  687. // Bcc
  688. if (appendHeader(PduHeaders.BCC) != PDU_COMPOSE_CONTENT_ERROR) {
  689. recipient = true;
  690. }
  691. // Need at least one of "cc", "bcc" and "to".
  692. if (false == recipient) {
  693. return PDU_COMPOSE_CONTENT_ERROR;
  694. }
  695. // Subject Optional
  696. appendHeader(PduHeaders.SUBJECT);
  697. // X-Mms-Message-Class Optional
  698. // Message-class-value = Class-identifier | Token-text
  699. appendHeader(PduHeaders.MESSAGE_CLASS);
  700. // X-Mms-Expiry Optional
  701. appendHeader(PduHeaders.EXPIRY);
  702. // X-Mms-Priority Optional
  703. appendHeader(PduHeaders.PRIORITY);
  704. // X-Mms-Delivery-Report Optional
  705. appendHeader(PduHeaders.DELIVERY_REPORT);
  706. // X-Mms-Read-Report Optional
  707. appendHeader(PduHeaders.READ_REPORT);
  708. // Content-Type
  709. appendOctet(PduHeaders.CONTENT_TYPE);
  710. // Message body
  711. makeMessageBody();
  712. return PDU_COMPOSE_SUCCESS; // Composing the message is OK
  713. }
  714. /**
  715. * Make message body.
  716. */
  717. private int makeMessageBody() {
  718. // 1. add body informations
  719. mStack.newbuf(); // Switching buffer because we need to
  720. PositionMarker ctStart = mStack.mark();
  721. // This contentTypeIdentifier should be used for type of attachment...
  722. String contentType = new String(mPduHeader.getTextString(PduHeaders.CONTENT_TYPE));
  723. Integer contentTypeIdentifier = mContentTypeMap.get(contentType);
  724. if (contentTypeIdentifier == null) {
  725. // content type is mandatory
  726. return PDU_COMPOSE_CONTENT_ERROR;
  727. }
  728. appendShortInteger(contentTypeIdentifier.intValue());
  729. // content-type parameter: start
  730. PduBody body = ((SendReq) mPdu).getBody();
  731. if (null == body || body.getPartsNum() == 0) {
  732. // empty message
  733. appendUintvarInteger(0);
  734. mStack.pop();
  735. mStack.copy();
  736. return PDU_COMPOSE_SUCCESS;
  737. }
  738. PduPart part;
  739. try {
  740. part = body.getPart(0);
  741. byte[] start = part.getContentId();
  742. if (start != null) {
  743. appendOctet(PduPart.P_DEP_START);
  744. if (('<' == start[0]) && ('>' == start[start.length - 1])) {
  745. appendTextString(start);
  746. } else {
  747. appendTextString("<" + new String(start) + ">");
  748. }
  749. }
  750. // content-type parameter: type
  751. appendOctet(PduPart.P_CT_MR_TYPE);
  752. appendTextString(part.getContentType());
  753. }
  754. catch (ArrayIndexOutOfBoundsException e){
  755. e.printStackTrace();
  756. }
  757. int ctLength = ctStart.getLength();
  758. mStack.pop();
  759. appendValueLength(ctLength);
  760. mStack.copy();
  761. // 3. add content
  762. int partNum = body.getPartsNum();
  763. appendUintvarInteger(partNum);
  764. for (int i = 0; i < partNum; i++) {
  765. part = body.getPart(i);
  766. mStack.newbuf(); // Leaving space for header lengh and data length
  767. PositionMarker attachment = mStack.mark();
  768. mStack.newbuf(); // Leaving space for Content-Type length
  769. PositionMarker contentTypeBegin = mStack.mark();
  770. byte[] partContentType = part.getContentType();
  771. if (partContentType == null) {
  772. // content type is mandatory
  773. return PDU_COMPOSE_CONTENT_ERROR;
  774. }
  775. // content-type value
  776. Integer partContentTypeIdentifier =
  777. mContentTypeMap.get(new String(partContentType));
  778. if (partContentTypeIdentifier == null) {
  779. appendTextString(partContentType);
  780. } else {
  781. appendShortInteger(partContentTypeIdentifier.intValue());
  782. }
  783. /* Content-type parameter : name.
  784. * The value of name, filename, content-location is the same.
  785. * Just one of them is enough for this PDU.
  786. */
  787. byte[] name = part.getName();
  788. if (null == name) {
  789. name = part.getFilename();
  790. if (null == name) {
  791. name = part.getContentLocation();
  792. if (null == name) {
  793. /* at lease one of name, filename, Content-location
  794. * should be available.
  795. */
  796. return PDU_COMPOSE_CONTENT_ERROR;
  797. }
  798. }
  799. }
  800. appendOctet(PduPart.P_DEP_NAME);
  801. appendTextString(name);
  802. // content-type parameter : charset
  803. int charset = part.getCharset();
  804. if (charset != 0) {
  805. appendOctet(PduPart.P_CHARSET);
  806. appendShortInteger(charset);
  807. }
  808. int contentTypeLength = contentTypeBegin.getLength();
  809. mStack.pop();
  810. appendValueLength(contentTypeLength);
  811. mStack.copy();
  812. // content id
  813. byte[] contentId = part.getContentId();
  814. if (null != contentId) {
  815. appendOctet(PduPart.P_CONTENT_ID);
  816. if (('<' == contentId[0]) && ('>' == contentId[contentId.length - 1])) {
  817. appendQuotedString(contentId);
  818. } else {
  819. appendQuotedString("<" + new String(contentId) + ">");
  820. }
  821. }
  822. // content-location
  823. byte[] contentLocation = part.getContentLocation();
  824. if (null != contentLocation) {
  825. appendOctet(PduPart.P_CONTENT_LOCATION);
  826. appendTextString(contentLocation);
  827. }
  828. // content
  829. int headerLength = attachment.getLength();
  830. int dataLength = 0; // Just for safety...
  831. byte[] partData = part.getData();
  832. if (partData != null) {
  833. arraycopy(partData, 0, partData.length);
  834. dataLength = partData.length;
  835. } else {
  836. InputStream cr;
  837. try {
  838. byte[] buffer = new byte[PDU_COMPOSER_BLOCK_SIZE];
  839. cr = mResolver.openInputStream(part.getDataUri());
  840. int len = 0;
  841. while ((len = cr.read(buffer)) != -1) {
  842. mMessage.write(buffer, 0, len);
  843. mPosition += len;
  844. dataLength += len;
  845. }
  846. } catch (FileNotFoundException e) {
  847. return PDU_COMPOSE_CONTENT_ERROR;
  848. } catch (IOException e) {
  849. return PDU_COMPOSE_CONTENT_ERROR;
  850. } catch (RuntimeException e) {
  851. return PDU_COMPOSE_CONTENT_ERROR;
  852. }
  853. }
  854. if (dataLength != (attachment.getLength() - headerLength)) {
  855. throw new RuntimeException("BUG: Length sanity check failed");
  856. }
  857. mStack.pop();
  858. appendUintvarInteger(headerLength);
  859. appendUintvarInteger(dataLength);
  860. mStack.copy();
  861. }
  862. return PDU_COMPOSE_SUCCESS;
  863. }
  864. /**
  865. * Record current message informations.
  866. */
  867. static private class LengthRecordNode {
  868. ByteArrayOutputStream currentMessage = null;
  869. public int currentPosition = 0;
  870. public LengthRecordNode next = null;
  871. }
  872. /**
  873. * Mark current message position and stact size.
  874. */
  875. private class PositionMarker {
  876. private int c_pos; // Current position
  877. private int currentStackSize; // Current stack size
  878. int getLength() {
  879. // If these assert fails, likely that you are finding the
  880. // size of buffer that is deep in BufferStack you can only
  881. // find the length of the buffer that is on top
  882. if (currentStackSize != mStack.stackSize) {
  883. throw new RuntimeException("BUG: Invalid call to getLength()");
  884. }
  885. return mPosition - c_pos;
  886. }
  887. }
  888. /**
  889. * This implementation can be OPTIMIZED to use only
  890. * 2 buffers. This optimization involves changing BufferStack
  891. * only... Its usage (interface) will not change.
  892. */
  893. private class BufferStack {
  894. private LengthRecordNode stack = null;
  895. private LengthRecordNode toCopy = null;
  896. int stackSize = 0;
  897. /**
  898. * Create a new message buffer and push it into the stack.
  899. */
  900. void newbuf() {
  901. // You can't create a new buff when toCopy != null
  902. // That is after calling pop() and before calling copy()
  903. // If you do, it is a bug
  904. if (toCopy != null) {
  905. throw new RuntimeException("BUG: Invalid newbuf() before copy()");
  906. }
  907. LengthRecordNode temp = new LengthRecordNode();
  908. temp.currentMessage = mMessage;
  909. temp.currentPosition = mPosition;
  910. temp.next = stack;
  911. stack = temp;
  912. stackSize = stackSize + 1;
  913. mMessage = new ByteArrayOutputStream();
  914. mPosition = 0;
  915. }
  916. /**
  917. * Pop the message before and record current message in the stack.
  918. */
  919. void pop() {
  920. ByteArrayOutputStream currentMessage = mMessage;
  921. int currentPosition = mPosition;
  922. mMessage = stack.currentMessage;
  923. mPosition = stack.currentPosition;
  924. toCopy = stack;
  925. // Re using the top element of the stack to avoid memory allocation
  926. stack = stack.next;
  927. stackSize = stackSize - 1;
  928. toCopy.currentMessage = currentMessage;
  929. toCopy.currentPosition = currentPosition;
  930. }
  931. /**
  932. * Append current message to the message before.
  933. */
  934. void copy() {
  935. arraycopy(toCopy.currentMessage.toByteArray(), 0,
  936. toCopy.currentPosition);
  937. toCopy = null;
  938. }
  939. /**
  940. * Mark current message position
  941. */
  942. PositionMarker mark() {
  943. PositionMarker m = new PositionMarker();
  944. m.c_pos = mPosition;
  945. m.currentStackSize = stackSize;
  946. return m;
  947. }
  948. }
  949. /**
  950. * Check address type.
  951. *
  952. * @param address address string without the postfix stinng type,
  953. * such as "/TYPE=PLMN", "/TYPE=IPv6" and "/TYPE=IPv4"
  954. * @return PDU_PHONE_NUMBER_ADDRESS_TYPE if it is phone number,
  955. * PDU_EMAIL_ADDRESS_TYPE if it is email address,
  956. * PDU_IPV4_ADDRESS_TYPE if it is ipv4 address,
  957. * PDU_IPV6_ADDRESS_TYPE if it is ipv6 address,
  958. * PDU_UNKNOWN_ADDRESS_TYPE if it is unknown.
  959. */
  960. protected static int checkAddressType(String address) {
  961. /**
  962. * From OMA-TS-MMS-ENC-V1_3-20050927-C.pdf, section 8.
  963. * address = ( e-mail / device-address / alphanum-shortcode / num-shortcode)
  964. * e-mail = mailbox; to the definition of mailbox as described in
  965. * section 3.4 of [RFC2822], but excluding the
  966. * obsolete definitions as indicated by the "obs-" prefix.
  967. * device-address = ( global-phone-number "/TYPE=PLMN" )
  968. * / ( ipv4 "/TYPE=IPv4" ) / ( ipv6 "/TYPE=IPv6" )
  969. * / ( escaped-value "/TYPE=" address-type )
  970. *
  971. * global-phone-number = ["+"] 1*( DIGIT / written-sep )
  972. * written-sep =("-"/".")
  973. *
  974. * ipv4 = 1*3DIGIT 3( "." 1*3DIGIT ) ; IPv4 address value
  975. *
  976. * ipv6 = 4HEXDIG 7( ":" 4HEXDIG ) ; IPv6 address per RFC 2373
  977. */
  978. if (null == address) {
  979. return PDU_UNKNOWN_ADDRESS_TYPE;
  980. }
  981. if (address.matches(REGEXP_IPV4_ADDRESS_TYPE)) {
  982. // Ipv4 address.
  983. return PDU_IPV4_ADDRESS_TYPE;
  984. }else if (address.matches(REGEXP_PHONE_NUMBER_ADDRESS_TYPE)) {
  985. // Phone number.
  986. return PDU_PHONE_NUMBER_ADDRESS_TYPE;
  987. } else if (address.matches(REGEXP_EMAIL_ADDRESS_TYPE)) {
  988. // Email address.
  989. return PDU_EMAIL_ADDRESS_TYPE;
  990. } else if (address.matches(REGEXP_IPV6_ADDRESS_TYPE)) {
  991. // Ipv6 address.
  992. return PDU_IPV6_ADDRESS_TYPE;
  993. } else {
  994. // Unknown address.
  995. return PDU_UNKNOWN_ADDRESS_TYPE;
  996. }
  997. }
  998. }