RecordDetailFragment.java 7.4 KB

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