RecordDetailFragment.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package de.tudarmstadt.informatik.hostage.ui2.fragment;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import android.app.Activity;
  5. import android.app.AlertDialog;
  6. import android.content.DialogInterface;
  7. import android.os.Bundle;
  8. import android.text.format.DateFormat;
  9. import android.view.LayoutInflater;
  10. import android.view.Menu;
  11. import android.view.MenuInflater;
  12. import android.view.MotionEvent;
  13. import android.view.View;
  14. import android.view.ViewGroup;
  15. import android.widget.Button;
  16. import android.widget.LinearLayout;
  17. import android.widget.ScrollView;
  18. import android.widget.TextView;
  19. import de.tudarmstadt.informatik.hostage.R;
  20. import de.tudarmstadt.informatik.hostage.logging.Record;
  21. import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
  22. import de.tudarmstadt.informatik.hostage.logging.MessageRecord;
  23. import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
  24. /**
  25. * Displays detailed informations about an record.
  26. *
  27. * @author Fabio Arnold
  28. * @author Alexander Brakowski
  29. * @author Julien Clauter
  30. */
  31. public class RecordDetailFragment extends UpNavigatibleFragment {
  32. /**
  33. * Hold the record of which the detail informations should be shown
  34. */
  35. private Record mRecord;
  36. /**
  37. * The database helper to retrieve data from the database
  38. */
  39. private HostageDBOpenHelper mDBOpenHelper;
  40. /**
  41. * The layout inflater
  42. */
  43. private LayoutInflater mInflater;
  44. /*
  45. * References to the views in the layout
  46. */
  47. private View mRootView;
  48. private LinearLayout mRecordOverviewConversation;
  49. private TextView mRecordDetailsTextSsid;
  50. private TextView mRecordDetailsTextBssid;
  51. private TextView mRecordDetailsTextRemoteip;
  52. private TextView mRecordDetailsTextProtocol;
  53. private Button mRecordDeleteButton;
  54. /**
  55. * Sets the record of which the details should be displayed
  56. * @param rec the record to be used
  57. */
  58. public void setRecord(Record rec) {
  59. this.mRecord = rec;
  60. }
  61. /**
  62. * Retriebes the record which is used for the display of the detail informations
  63. * @return the record
  64. */
  65. public Record getRecord() {
  66. return this.mRecord;
  67. }
  68. /**
  69. * Retrieves the id of the layout
  70. * @return the id of the layout
  71. */
  72. public int getLayoutId() {
  73. return R.layout.fragment_record_detail;
  74. }
  75. /**
  76. * {@inheritDoc}
  77. */
  78. @Override
  79. public void onCreate(Bundle savedInstanceState) {
  80. super.onCreate(savedInstanceState);
  81. setHasOptionsMenu(true);
  82. }
  83. /**
  84. * {@inheritDoc}
  85. */
  86. @Override
  87. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  88. super.onCreateView(inflater, container, savedInstanceState);
  89. mInflater = inflater;
  90. getActivity().setTitle(mRecord.getSsid());
  91. this.mDBOpenHelper = new HostageDBOpenHelper(this.getActivity().getBaseContext());
  92. this.mRootView = inflater.inflate(this.getLayoutId(), container, false);
  93. this.assignViews(mRootView);
  94. this.configurateRootView(mRootView);
  95. return mRootView;
  96. }
  97. /**
  98. * {@inheritDoc}
  99. */
  100. @Override
  101. public void onStart() {
  102. super.onStart();
  103. }
  104. /**
  105. * Retrieves all the views from the given view
  106. *
  107. * @param view the layout view
  108. */
  109. private void assignViews(View view) {
  110. mRecordOverviewConversation = (LinearLayout) view.findViewById(R.id.record_overview_conversation);
  111. mRecordDetailsTextSsid = (TextView) view.findViewById(R.id.record_details_text_ssid);
  112. mRecordDetailsTextBssid = (TextView) view.findViewById(R.id.record_details_text_bssid);
  113. mRecordDetailsTextRemoteip = (TextView) view.findViewById(R.id.record_details_text_remoteip);
  114. mRecordDetailsTextProtocol = (TextView) view.findViewById(R.id.record_details_text_protocol);
  115. mRecordDeleteButton = (Button) view.findViewById(R.id.record_delete_button);
  116. }
  117. /**
  118. * Configures the given view and fills it with the detail information
  119. *
  120. * @param rootView the view to use to display the informations
  121. */
  122. private void configurateRootView(View rootView) {
  123. mRecordDetailsTextBssid.setText(mRecord.getBssid());
  124. mRecordDetailsTextSsid.setText(mRecord.getSsid());
  125. if (mRecord.getRemoteIP() != null)
  126. mRecordDetailsTextRemoteip.setText(mRecord.getRemoteIP() + ":" + mRecord.getRemotePort());
  127. mRecordDetailsTextProtocol.setText(mRecord.getProtocol());
  128. ArrayList<Record> conversation = this.mDBOpenHelper.getConversationForAttackID(mRecord.getAttack_id());
  129. // display the conversation of the attack
  130. for (Record r : conversation) {
  131. View row;
  132. String from = r.getLocalIP() == null ? "-" : r.getLocalIP() + ":" + r.getLocalPort();
  133. String to = r.getRemoteIP() == null ? "-" : r.getRemoteIP() + ":" + r.getRemotePort();
  134. if (r.getType() == MessageRecord.TYPE.SEND) {
  135. row = mInflater.inflate(R.layout.fragment_record_conversation_sent, null);
  136. } else {
  137. row = mInflater.inflate(R.layout.fragment_record_conversation_received, null);
  138. String tmp = from;
  139. from = to;
  140. to = tmp;
  141. }
  142. TextView conversationInfo = (TextView) row.findViewById(R.id.record_conversation_info);
  143. TextView conversationContent = (TextView) row.findViewById(R.id.record_conversation_content);
  144. conversationContent.setOnTouchListener(new View.OnTouchListener() {
  145. @Override
  146. public boolean onTouch(final View v, final MotionEvent motionEvent) {
  147. if (v.getId() == R.id.record_conversation_content) {
  148. v.getParent().requestDisallowInterceptTouchEvent(true);
  149. switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
  150. case MotionEvent.ACTION_UP:
  151. v.getParent().requestDisallowInterceptTouchEvent(false);
  152. break;
  153. }
  154. }
  155. return false;
  156. }
  157. });
  158. Date date = new Date(r.getTimestamp());
  159. conversationInfo.setText(String.format(getString(R.string.record_details_info), from, to, getDateAsString(date), getTimeAsString(date)));
  160. if (r.getPacket() != null)
  161. conversationContent.setText(r.getPacket());
  162. mRecordOverviewConversation.addView(row);
  163. }
  164. mRecordDeleteButton.setOnClickListener(new View.OnClickListener() {
  165. @Override
  166. public void onClick(View v) {
  167. Activity activity = getActivity();
  168. if (activity == null) {
  169. return;
  170. }
  171. new AlertDialog.Builder(getActivity())
  172. .setTitle(android.R.string.dialog_alert_title)
  173. .setMessage(R.string.record_details_confirm_delete)
  174. .setPositiveButton(R.string.yes,
  175. new DialogInterface.OnClickListener() {
  176. public void onClick(DialogInterface dialog,
  177. int which) {
  178. mDBOpenHelper.deleteByAttackID(mRecord.getAttack_id());
  179. MainActivity.getInstance().navigateBack();
  180. }
  181. }
  182. ).setNegativeButton(R.string.no, null)
  183. .setIcon(android.R.drawable.ic_dialog_alert).show();
  184. }
  185. });
  186. }
  187. /*****************************
  188. *
  189. * Date Transform
  190. *
  191. * ***************************/
  192. /**
  193. * Converts the given data to an localized string
  194. *
  195. * @param date the date to convert
  196. * @return the converted date as an string
  197. */
  198. private String getDateAsString(Date date) {
  199. return DateFormat.getDateFormat(getActivity()).format(date);
  200. }
  201. /**
  202. * Converts the given date to an localized time
  203. *
  204. * @param date the date to convert
  205. * @return the converted time as an string
  206. */
  207. private String getTimeAsString(Date date) {
  208. return DateFormat.getTimeFormat(getActivity()).format(date);
  209. }
  210. }