MessageRecord.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package de.tudarmstadt.informatik.hostage.logging;
  2. import java.io.Serializable;
  3. import android.content.SharedPreferences;
  4. import android.os.Parcel;
  5. import android.os.Parcelable;
  6. import android.preference.PreferenceManager;
  7. import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;
  8. /**
  9. * Holds all necessary information about a single message exchanged during an attack.
  10. */
  11. public class MessageRecord implements Parcelable, Serializable{
  12. private static final long serialVersionUID = -5936572995202342935L;
  13. public static enum TYPE {
  14. SEND, RECEIVE
  15. };
  16. // attack
  17. private int id;
  18. private long attack_id;
  19. private long timestamp;
  20. private TYPE type;
  21. private String packet;
  22. public static final Parcelable.Creator<MessageRecord> CREATOR = new Parcelable.Creator<MessageRecord>() {
  23. @Override
  24. public MessageRecord createFromParcel(Parcel source) {
  25. return new MessageRecord(source);
  26. }
  27. @Override
  28. public MessageRecord[] newArray(int size) {
  29. return new MessageRecord[size];
  30. }
  31. };
  32. public MessageRecord() {
  33. }
  34. public MessageRecord(boolean autoincrement){
  35. if (autoincrement){
  36. SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.getContext());
  37. SharedPreferences.Editor editor = pref.edit();
  38. int message_id = pref.getInt("MESSAGE_ID_COUNTER", 0);
  39. editor.putInt("MESSAGE_ID_COUNTER", message_id + 1);
  40. editor.commit();
  41. this.id = message_id;
  42. }
  43. }
  44. public MessageRecord(Parcel source) {
  45. this.id = source.readInt();
  46. this.attack_id = source.readLong();
  47. this.timestamp = source.readLong();
  48. this.type = TYPE.valueOf(source.readString());
  49. this.packet = source.readString();
  50. }
  51. @Override
  52. public int describeContents() {
  53. return 0;
  54. }
  55. @Override
  56. public void writeToParcel(Parcel dest, int flags) {
  57. dest.writeInt(id);
  58. dest.writeLong(attack_id);
  59. dest.writeLong(timestamp);
  60. dest.writeString(type.name());
  61. dest.writeString(packet);
  62. }
  63. /**
  64. * @return the id
  65. */
  66. public int getId() {
  67. return id;
  68. }
  69. /**
  70. * @param id the id to set
  71. */
  72. public void setId(int id) {
  73. this.id = id;
  74. }
  75. /**
  76. * @return the attack_id
  77. */
  78. public long getAttack_id() {
  79. return attack_id;
  80. }
  81. /**
  82. * @param attack_id the attack_id to set
  83. */
  84. public void setAttack_id(long attack_id) {
  85. this.attack_id = attack_id;
  86. }
  87. /**
  88. * @return the timestamp
  89. */
  90. public long getTimestamp() {
  91. return timestamp;
  92. }
  93. /**
  94. * @param timestamp the timestamp to set
  95. */
  96. public void setTimestamp(long timestamp) {
  97. this.timestamp = timestamp;
  98. }
  99. /**
  100. * @return the type
  101. */
  102. public TYPE getType() {
  103. return type;
  104. }
  105. /**
  106. * @param type the type to set
  107. */
  108. public void setType(TYPE type) {
  109. this.type = type;
  110. }
  111. /**
  112. * @return the packet
  113. */
  114. public String getPacket() {
  115. return packet;
  116. }
  117. @Override
  118. public String toString() {
  119. return "MessageRecord{" +
  120. "id=" + id +
  121. ", attack_id=" + attack_id +
  122. ", timestamp=" + timestamp +
  123. ", type=" + type +
  124. ", packet='" + packet + '\'' +
  125. '}';
  126. }
  127. /**
  128. * @param packet the packet to set
  129. */
  130. public void setPacket(String packet) {
  131. this.packet = packet;
  132. }
  133. }