/src/com/agh/is/android/logdroid/telephony/data/MMS.java

https://github.com/snuffix/Android_LogdroidProject · Java · 248 lines · 202 code · 46 blank · 0 comment · 5 complexity · b67d2be0fef5b8852ce47e34c49dac6b MD5 · raw file

  1. package com.agh.is.android.logdroid.telephony.data;
  2. import java.util.Date;
  3. import com.agh.is.android.logdroid.R;
  4. import com.agh.is.android.logdroid.telephony.data.MMS.MMSBuilder;
  5. import com.agh.is.android.logdroid.telephony.views.DisplayableData;
  6. import com.agh.is.android.logdroid.telephony.views.DisplayableDataType;
  7. import com.agh.is.android.logdroid.utilities.ExceptionUtilities;
  8. public class MMS implements Comparable<MMS>, DisplayableData, TelephoneData{
  9. private Date date;
  10. private String address="";
  11. private String body="";
  12. private MessageType messageType;
  13. private String contactName = "";
  14. private String subject = "";
  15. private MMSAddressType addressType = null;
  16. private String imagePath = "";
  17. private String imageTextUri = "";
  18. private String audioTextUri = "";
  19. private String audioName = "";
  20. public static class MMSBuilder {
  21. private Date date;
  22. private String address="";
  23. private String body="";
  24. private MessageType messageType;
  25. private String contactName = "";
  26. private String subject = "";
  27. private MMSAddressType addressType = null;
  28. private String imagePath = "";
  29. private String imageTextUri = "";
  30. private String audioTextUri = "";
  31. private String audioName = "";
  32. public MMSBuilder date(Date date) {
  33. this.date = date;
  34. return this;
  35. }
  36. public MMSBuilder address(String address) {
  37. this.address = address;
  38. return this;
  39. }
  40. public MMSBuilder body(String body) {
  41. this.body = body;
  42. return this;
  43. }
  44. public MMSBuilder messageType(MessageType messageType) {
  45. this.messageType = messageType;
  46. return this;
  47. }
  48. public MMSBuilder contactName(String contactName) {
  49. this.contactName = contactName;
  50. return this;
  51. }
  52. public MMSBuilder subject(String subject) {
  53. this.subject = subject;
  54. return this;
  55. }
  56. public MMSBuilder addressType(MMSAddressType addressType) {
  57. this.addressType = addressType;
  58. return this;
  59. }
  60. public MMSBuilder imagePath(String imagePath) {
  61. this.imagePath = imagePath;
  62. return this;
  63. }
  64. public MMSBuilder imageTextUri(String imageTextUri) {
  65. this.imageTextUri = imageTextUri;
  66. return this;
  67. }
  68. public MMSBuilder audioTextUri(String audioTextUri) {
  69. this.audioTextUri = audioTextUri;
  70. return this;
  71. }
  72. public MMSBuilder audioName(String audioName) {
  73. this.audioName = audioName;
  74. return this;
  75. }
  76. public MMS build() {
  77. return new MMS(this);
  78. }
  79. }
  80. public static enum MMSAddressType {
  81. EMAIL_ADDRESS, PHONE_NUMBER;
  82. public static MMSAddressType fromString(String address) {
  83. if (address.matches(EMAIL_ADDRESS_REGEXP)) {
  84. return MMSAddressType.EMAIL_ADDRESS;
  85. }
  86. else {
  87. return MMSAddressType.PHONE_NUMBER;
  88. }
  89. }
  90. private static final String EMAIL_ADDRESS_REGEXP =
  91. "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-])+(\\.[a-z,A-Z]{2,})$";
  92. }
  93. private MMS(MMSBuilder builder) {
  94. this.date = builder.date;
  95. this.address = builder.address;
  96. this.contactName = builder.contactName;
  97. this.body = builder.body;
  98. this.messageType = builder.messageType;
  99. this.subject = builder.subject;
  100. this.addressType = builder.addressType;
  101. this.imagePath = builder.imagePath;
  102. this.imageTextUri = builder.imageTextUri;
  103. this.audioTextUri = builder.audioTextUri;
  104. this.audioName = builder.audioName;
  105. throwIllegalStateExceptionIfStateIsNotValid();
  106. }
  107. public String getContactName() {
  108. return contactName;
  109. }
  110. public String getAddress(){
  111. return address;
  112. }
  113. public String getBody(){
  114. return body;
  115. }
  116. public MessageType getMessageType(){
  117. return messageType;
  118. }
  119. public MMSAddressType getAddressType() {
  120. return addressType;
  121. }
  122. public String getImagePath() {
  123. return imagePath;
  124. }
  125. public String getAudioTextUri() {
  126. return audioTextUri;
  127. }
  128. public String getAudioName() {
  129. return audioName;
  130. }
  131. public String getImageTextUri() {
  132. return imageTextUri;
  133. }
  134. public String getSubject() {
  135. return subject;
  136. }
  137. public boolean containsAudio() {
  138. return !getAudioTextUri().equals("") && !getAudioName().equals("");
  139. }
  140. @Override
  141. public int getMainLabelLength() {
  142. return subject.length();
  143. }
  144. @Override
  145. public String getMainLabelText() {
  146. return subject;
  147. }
  148. @Override
  149. public int getDisplayPriority() {
  150. return 3;
  151. }
  152. @Override
  153. public int getDataTypeThumbnail() {
  154. return R.drawable.mms;
  155. }
  156. @Override
  157. public Date getDate() {
  158. return date;
  159. }
  160. @Override
  161. public String getDisplayedAddress() {
  162. return address;
  163. }
  164. @Override
  165. public DisplayableDataType getDisplayableDataType() {
  166. return messageType == MessageType.RECEIVED ? DisplayableDataType.RECEIVED : DisplayableDataType.SENT;
  167. }
  168. @Override
  169. public boolean equals(Object o) {
  170. if (!(o instanceof MMS)) {
  171. return false;
  172. }
  173. MMS anotherMMS = (MMS)o;
  174. return date.equals(anotherMMS.getDate());
  175. }
  176. @Override
  177. public int compareTo(MMS another) {
  178. return (int)(date.getTime() - another.getDate().getTime());
  179. }
  180. private void throwIllegalStateExceptionIfStateIsNotValid() {
  181. ExceptionUtilities.throwRuntimeExceptionIfObjectIsNull(
  182. new IllegalStateException("contactName can't be null"), contactName);
  183. ExceptionUtilities.throwRuntimeExceptionIfObjectIsNull(
  184. new IllegalStateException("body can't be null"), body);
  185. ExceptionUtilities.throwRuntimeExceptionIfObjectIsNull(
  186. new IllegalStateException("date can't be null"), date);
  187. ExceptionUtilities.throwRuntimeExceptionIfObjectIsNull(
  188. new IllegalStateException("messageType can't be null"), messageType);
  189. ExceptionUtilities.throwRuntimeExceptionIfObjectIsNull(
  190. new IllegalStateException("subject can't be null"), subject);
  191. ExceptionUtilities.throwRuntimeExceptionIfObjectIsNull(
  192. new IllegalStateException("addressType can't be null"), addressType);
  193. ExceptionUtilities.throwRuntimeExceptionIfObjectIsNull(
  194. new IllegalStateException("imagePath can't be null"), imagePath);
  195. ExceptionUtilities.throwRuntimeExceptionIfObjectIsNull(
  196. new IllegalStateException("audioTextUri can't be null"), audioTextUri);
  197. ExceptionUtilities.throwRuntimeExceptionIfObjectIsNull(
  198. new IllegalStateException("imageTextUri can't be null"), imageTextUri);
  199. ExceptionUtilities.throwRuntimeExceptionIfObjectIsNull(
  200. new IllegalStateException("audioName can't be null"), audioName);
  201. }
  202. }