RecordDetailFragment.java 7.6 KB

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