RecordOverviewFragment.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. package de.tudarmstadt.informatik.hostage.ui2.fragment;
  2. import android.annotation.SuppressLint;
  3. import android.app.Fragment;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.graphics.Color;
  7. import android.os.Bundle;
  8. import android.text.SpannableString;
  9. import android.text.style.ForegroundColorSpan;
  10. import android.view.LayoutInflater;
  11. import android.view.Menu;
  12. import android.view.MenuInflater;
  13. import android.view.MenuItem;
  14. import android.view.View;
  15. import android.view.ViewGroup;
  16. import android.widget.AdapterView;
  17. import android.widget.ExpandableListView;
  18. import android.widget.ImageButton;
  19. import android.widget.PopupMenu;
  20. import android.widget.PopupMenu.OnMenuItemClickListener;
  21. import android.widget.Toast;
  22. import java.text.DateFormat;
  23. import java.text.SimpleDateFormat;
  24. import java.util.ArrayList;
  25. import java.util.Calendar;
  26. import java.util.Date;
  27. import java.util.HashMap;
  28. import de.tudarmstadt.informatik.hostage.R;
  29. import de.tudarmstadt.informatik.hostage.logging.Record;
  30. import de.tudarmstadt.informatik.hostage.logging.Record.TYPE;
  31. import de.tudarmstadt.informatik.hostage.logging.UglyDbHelper;
  32. import de.tudarmstadt.informatik.hostage.ui.LogFilter;
  33. import de.tudarmstadt.informatik.hostage.ui.LogFilter.SortType;
  34. import de.tudarmstadt.informatik.hostage.ui2.adapter.RecordListAdapter;
  35. import de.tudarmstadt.informatik.hostage.ui2.dialog.ChecklistDialog;
  36. import de.tudarmstadt.informatik.hostage.ui2.dialog.DateTimeDialogFragment;
  37. import de.tudarmstadt.informatik.hostage.ui2.model.ExpandableListItem;
  38. public class RecordOverviewFragment extends Fragment implements ChecklistDialog.ChecklistDialogListener, DateTimeDialogFragment.DateTimeDialogFragmentListener {
  39. static final String SELECTED_KEY = "Selected";
  40. static final String OTHERS_KEY = "Other";
  41. static final String FILTER_MENU_TITLE_BSSID = "BSSID";
  42. static final String FILTER_MENU_TITLE_ESSID = "ESSID";
  43. static final String FILTER_MENU_TITLE_PROTOCOLS = "Protocols";
  44. static final String FILTER_MENU_TITLE_TIMESTAMP_BELOW = "To Date";
  45. static final String FILTER_MENU_TITLE_TIMESTAMP_ABOVE = "From Date";
  46. static final String FILTER_MENU_TITLE_SORTING = "Sort by";
  47. static final String FILTER_MENU_TITLE_REMOVE = "Reset Filter";
  48. private boolean wasBelowTimePicker;
  49. private LogFilter filter;
  50. private boolean showFilterButton;
  51. public String groupingKey;
  52. private ExpandableListView expListView;
  53. UglyDbHelper dbh;
  54. public RecordOverviewFragment(){}
  55. @Override
  56. public void onCreate(Bundle savedInstanceState) {
  57. super.onCreate(savedInstanceState);
  58. setHasOptionsMenu(true);
  59. }
  60. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  61. Bundle savedInstanceState) {
  62. dbh = new UglyDbHelper(this.getActivity().getBaseContext());
  63. // Get the message from the intent
  64. if (this.filter == null){
  65. Intent intent = this.getActivity().getIntent();
  66. LogFilter filter = intent.getParcelableExtra(LogFilter.LOG_FILTER_INTENT_KEY);
  67. if(filter == null){
  68. this.clearFilter();
  69. } else {
  70. this.filter = filter;
  71. }
  72. }
  73. if (this.groupingKey == null) this.groupingKey = this.groupingTitles().get(0);
  74. this.setShowFilterButton(!this.filter.isNotEditable());
  75. this.addRecordToDB();
  76. View rootView = inflater.inflate(R.layout.fragment_record_list, container, false);
  77. ExpandableListView mylist = (ExpandableListView) rootView.findViewById(R.id.loglistview);
  78. this.expListView = mylist;
  79. populateListViewFromDB(mylist);
  80. registerListClickCallback(mylist);
  81. ImageButton filterButton = (ImageButton) rootView.findViewById(R.id.FilterButton);
  82. filterButton.setOnClickListener(new View.OnClickListener() {
  83. public void onClick(View v) {
  84. RecordOverviewFragment.this.openFilterPopupMenuOnView(v);
  85. }
  86. });
  87. filterButton.setVisibility(this.showFilterButton? View.VISIBLE : View.INVISIBLE);
  88. ImageButton sortButton = (ImageButton) rootView.findViewById(R.id.SortButton);
  89. sortButton.setOnClickListener(new View.OnClickListener() {
  90. public void onClick(View v) {
  91. // Open SortMenu
  92. RecordOverviewFragment.this.openSortingDialog();
  93. }
  94. });
  95. return rootView;
  96. }
  97. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  98. super.onCreateOptionsMenu(menu, inflater);
  99. }
  100. public boolean onFilterMenuItemSelected(MenuItem item) {
  101. String title = item.getTitle().toString();
  102. if(title.equals(FILTER_MENU_TITLE_BSSID)){
  103. this.openBSSIDFilterDialog();
  104. }
  105. if(title.equals(FILTER_MENU_TITLE_ESSID)){
  106. this.openESSIDFilterDialog();
  107. }
  108. if(title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
  109. this.openProtocolsFilterDialog();
  110. }
  111. if(title.equals(FILTER_MENU_TITLE_SORTING)){
  112. this.openSortingDialog();
  113. }
  114. if(title.equals(FILTER_MENU_TITLE_REMOVE)){
  115. this.clearFilter();
  116. }
  117. if(title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)){
  118. this.openTimestampToFilterDialog();
  119. }
  120. if(title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)){
  121. this.openTimestampFromFilterDialog();
  122. }
  123. return super.onOptionsItemSelected(item);
  124. }
  125. /*****************************
  126. *
  127. * ListView Stuff
  128. *
  129. * ***************************/
  130. private void populateListViewFromDB(ExpandableListView mylist) {
  131. HashMap<String, ArrayList<ExpandableListItem>> sectionData = new HashMap<String, ArrayList<ExpandableListItem>>();
  132. ArrayList<Record> data = dbh.getRecordsForFilter(this.filter);
  133. // Adding Items to ListView
  134. String keys[] = new String[] { this.getString(R.string.RecordBSSID), this.getString(R.string.RecordSSID), this.getString(R.string.RecordProtocol), this.getString(R.string.RecordTimestamp)};
  135. int ids[] = new int[] {R.id.RecordTextFieldBSSID, R.id.RecordTextFieldSSID, R.id.RecordTextFieldProtocol, R.id.RecordTextFieldTimestamp };
  136. HashMap<String, Integer> mapping = new HashMap<String, Integer>();
  137. int i = 0;
  138. for(String key : keys){
  139. mapping.put(key, ids[i]);
  140. i++;
  141. }
  142. ArrayList<String>groupTitle = new ArrayList<String>();
  143. for (Record val : data) {
  144. // DO GROUPING IN HERE
  145. HashMap<String, String> map = new HashMap<String, String>();
  146. map.put(this.getString(R.string.RecordBSSID), val.getBssid());
  147. map.put(this.getString(R.string.RecordSSID), val.getSsid());
  148. map.put(this.getString(R.string.RecordProtocol), val.getProtocol());
  149. map.put(this.getString(R.string.RecordTimestamp),
  150. this.getDateAsString(val.getTimestamp()));
  151. ExpandableListItem item = new ExpandableListItem();
  152. item.setData(map);
  153. item.setId_Mapping(mapping);
  154. String groupID = this.getGroupValue(val);
  155. ArrayList<ExpandableListItem> items = sectionData.get(groupID);
  156. if (items == null) {
  157. items = new ArrayList<ExpandableListItem>();
  158. sectionData.put(groupID, items);
  159. groupTitle.add(groupID);
  160. }
  161. items.add(item);
  162. }
  163. RecordListAdapter adapter = new RecordListAdapter(this.getApplicationContext(), groupTitle, sectionData);
  164. mylist.setAdapter(adapter);
  165. }
  166. private Context getBaseContext(){
  167. return this.getActivity().getBaseContext();
  168. }
  169. private Context getApplicationContext(){
  170. return this.getActivity().getApplicationContext();
  171. }
  172. private void registerListClickCallback(ExpandableListView mylist) {
  173. mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  174. public void onItemClick(AdapterView<?> parent, View viewClicked,
  175. int position, long idInDB) {
  176. UglyDbHelper dbh = new UglyDbHelper(getBaseContext());
  177. Record rec = dbh.getRecord((int) idInDB);
  178. String message = createInformationStringFromRecord(rec);
  179. Toast.makeText(getApplicationContext(), message,
  180. Toast.LENGTH_LONG).show();
  181. }
  182. private String createInformationStringFromRecord(Record rec) {
  183. String message = "id: " + rec.getId() + "\n" + "attack_id: "
  184. + rec.getAttack_id() + "\n" + "protocol: "
  185. + rec.getProtocol() + "\n" + "type: " + rec.getType()
  186. + "\n" + "externalIP: " + rec.getExternalIP() + "\n"
  187. + "localIP: " + rec.getLocalIP() + "\n"
  188. + "local port: " + rec.getLocalPort() + "\n"
  189. + "remoteIP: " + rec.getRemoteIP() + "\n" + "BSSID: "
  190. + rec.getBssid() + "\n" + "SSID: " + rec.getSsid()
  191. + "\n" + "latitude: " + rec.getLatitude() + "\n"
  192. + "longitude: " + rec.getLongitude() + "\n"
  193. + "accuracy: " + rec.getAccuracy() + "\n" + "packet: "
  194. + rec.getPacket() + "\n"
  195. + getDateAsString(rec.getTimestamp()) + "";
  196. return message;
  197. }
  198. });
  199. }
  200. /*****************************
  201. *
  202. * Date Transform
  203. *
  204. * ***************************/
  205. @SuppressLint("SimpleDateFormat")
  206. private String getDateAsString(long timeStamp) {
  207. try {
  208. DateFormat sdf = new SimpleDateFormat("H:mm dd/MM/yyyy");
  209. Date netDate = (new Date(timeStamp));
  210. return sdf.format(netDate);
  211. } catch (Exception ex) {
  212. return "xx";
  213. }
  214. }
  215. /*****************************
  216. *
  217. * Getter / Setter
  218. *
  219. * ***************************/
  220. public boolean isShowFilterButton() {
  221. return showFilterButton;
  222. }
  223. public void setShowFilterButton(boolean showFilterButton) {
  224. this.showFilterButton = showFilterButton;
  225. }
  226. /*****************************
  227. *
  228. * Open Dialog Methods
  229. *
  230. * ***************************/
  231. private void openBSSIDFilterDialog(){
  232. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_BSSID,this.bssids(), this.selectedBSSIDs(), true , this);
  233. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_BSSID);
  234. }
  235. private void openESSIDFilterDialog(){
  236. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_ESSID,this.essids(), this.selectedESSIDs(), true , this);
  237. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_ESSID);
  238. }
  239. private void openProtocolsFilterDialog(){
  240. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_PROTOCOLS,this.protocolTitles(), this.selectedProtocols(), true , this);
  241. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_PROTOCOLS);
  242. }
  243. private void openTimestampFromFilterDialog(){
  244. this.wasBelowTimePicker = false;
  245. DateTimeDialogFragment newFragment = new DateTimeDialogFragment(this.getActivity());
  246. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_SORTING);
  247. if (this.filter.aboveTimestamp != Long.MIN_VALUE)newFragment.setDate(this.filter.aboveTimestamp);
  248. }
  249. private void openTimestampToFilterDialog(){
  250. this.wasBelowTimePicker = true;
  251. DateTimeDialogFragment newFragment = new DateTimeDialogFragment(this.getActivity());
  252. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_SORTING);
  253. if (this.filter.belowTimestamp != Long.MAX_VALUE) newFragment.setDate(this.filter.belowTimestamp);
  254. }
  255. private void openSortingDialog(){
  256. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_SORTING,this.sortTypeTtiles(), this.selectedSorttype(), false , this);
  257. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_SORTING);
  258. }
  259. /*****************************
  260. *
  261. * Grouping Stuff
  262. *
  263. * ***************************/
  264. public String getGroupValue(Record rec){
  265. int index = this.groupingTitles().indexOf(this.groupingKey);
  266. switch (index){
  267. case 0:
  268. return rec.getProtocol();
  269. case 1:
  270. return rec.getBssid();
  271. case 2:
  272. return rec.getSsid();
  273. default:
  274. return rec.getProtocol();
  275. }
  276. }
  277. public ArrayList<String> getGroupTitle(){
  278. int index = this.groupingTitles().indexOf(this.groupingKey);
  279. switch (index){
  280. case 0:
  281. return this.protocolTitles();
  282. case 1:
  283. return this.bssids();
  284. case 2:
  285. return this.essids();
  286. default:
  287. return this.protocolTitles();
  288. }
  289. }
  290. /*****************************
  291. *
  292. * Filter Stuff
  293. *
  294. * ***************************/
  295. private void openFilterPopupMenuOnView(View v){
  296. // Open FilterMenu
  297. PopupMenu filterMenu = new PopupMenu(getBaseContext(), v);
  298. for(String title : RecordOverviewFragment.this.filterMenuTitles()){
  299. // Set a white Title
  300. SpannableString styledMenuTitle = new SpannableString(title);
  301. styledMenuTitle.setSpan(new ForegroundColorSpan(Color.parseColor("#FFFFFF")), 0, title.length(), 0);
  302. filterMenu.getMenu().add(styledMenuTitle);
  303. }
  304. filterMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
  305. public boolean onMenuItemClick(MenuItem item) {
  306. RecordOverviewFragment.this.onFilterMenuItemSelected(item);
  307. return true;
  308. }
  309. });
  310. filterMenu.show();
  311. }
  312. private void clearFilter(){
  313. if(filter == null) this.filter = new LogFilter();
  314. this.filter.clear();
  315. }
  316. public ArrayList<String> groupingTitles(){
  317. ArrayList<String> titles = new ArrayList<String>();
  318. for (String groupTitle : this.getResources().getStringArray(
  319. R.array.Grouping)) {
  320. titles.add(groupTitle);
  321. }
  322. return titles;
  323. }
  324. public ArrayList<String> protocolTitles(){
  325. ArrayList<String> titles = new ArrayList<String>();
  326. for (String protocol : this.getResources().getStringArray(
  327. R.array.protocols)) {
  328. titles.add(protocol);
  329. }
  330. return titles;
  331. }
  332. public boolean[] selectedProtocols(){
  333. ArrayList<String> protocols = this.protocolTitles();
  334. boolean[] selected = new boolean[protocols.size()];
  335. int i = 0;
  336. for(String protocol : protocols){
  337. selected[i] =(this.filter.protocols.contains(protocol));
  338. i++;
  339. }
  340. return selected;
  341. }
  342. public ArrayList<String> sortTypeTtiles(){
  343. ArrayList<String> titles = new ArrayList<String>();
  344. titles.add("Time");
  345. titles.add("Protocol");
  346. titles.add("BSSID");
  347. titles.add("ESSID");
  348. //titles.add("Remote Host Name");
  349. //titles.add("Local Host Name");
  350. //titles.add("Attack ID");
  351. //titles.add("ID");
  352. return titles;
  353. }
  354. public boolean[] selectedSorttype(){
  355. ArrayList<String> types = this.sortTypeTtiles();
  356. boolean[] selected = new boolean[types.size()];
  357. int i = 0;
  358. for(String sorttype : types){
  359. selected[i] =(this.filter.sorttype.toString() == sorttype);
  360. i++;
  361. }
  362. return selected;
  363. }
  364. public ArrayList<String> bssids(){
  365. ArrayList<String> records = dbh.getUniqueBSSIDRecords();
  366. return records;
  367. }
  368. public boolean[] selectedBSSIDs(){
  369. ArrayList<String> bssids = this.bssids();
  370. boolean[] selected = new boolean[bssids.size()];
  371. int i = 0;
  372. for(String bssid : bssids){
  373. selected[i] =(this.filter.BSSIDs.contains(bssid));
  374. i++;
  375. }
  376. return selected;
  377. }
  378. public ArrayList<String> essids(){
  379. ArrayList<String> records = dbh.getUniqueESSIDRecords();
  380. return records;
  381. }
  382. public boolean[] selectedESSIDs(){
  383. ArrayList<String> essids = this.essids();
  384. boolean[] selected = new boolean[essids.size()];
  385. int i = 0;
  386. for(String essid : essids){
  387. selected[i] =(this.filter.ESSIDs.contains(essid));
  388. i++;
  389. }
  390. return selected;
  391. }
  392. private ArrayList<String> filterMenuTitles(){
  393. ArrayList<String> titles = new ArrayList<String>();
  394. titles.add(FILTER_MENU_TITLE_BSSID);
  395. titles.add(FILTER_MENU_TITLE_ESSID);
  396. titles.add(FILTER_MENU_TITLE_PROTOCOLS);
  397. titles.add(FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  398. titles.add(FILTER_MENU_TITLE_TIMESTAMP_BELOW);
  399. if (this.filter.isSet())titles.add(FILTER_MENU_TITLE_REMOVE);
  400. return titles;
  401. }
  402. /*****************************
  403. *
  404. * Listener Actions
  405. *
  406. * ***************************/
  407. public void onDateTimePickerPositiveClick(DateTimeDialogFragment dialog) {
  408. if(this.wasBelowTimePicker){
  409. this.filter.setBelowTimestamp(dialog.getDate());
  410. } else {
  411. this.filter.setAboveTimestamp(dialog.getDate());
  412. }
  413. this.populateListViewFromDB(this.expListView);
  414. }
  415. public void onDateTimePickerNegativeClick(DateTimeDialogFragment dialog) {
  416. if(this.wasBelowTimePicker){
  417. this.filter.setBelowTimestamp(Long.MAX_VALUE);
  418. } else {
  419. this.filter.setAboveTimestamp(Long.MIN_VALUE);
  420. }
  421. }
  422. public void onDialogPositiveClick(ChecklistDialog dialog) {
  423. String title = dialog.getTitle();
  424. if(title.equals(FILTER_MENU_TITLE_BSSID)){
  425. ArrayList<String> titles =dialog.getSelectedItemTitles();
  426. if (titles.size() == this.bssids().size()){
  427. this.filter.setBSSIDs(new ArrayList<String>());
  428. } else {
  429. this.filter.setBSSIDs(titles);
  430. }
  431. }
  432. if(title.equals(FILTER_MENU_TITLE_ESSID)){
  433. ArrayList<String> titles =dialog.getSelectedItemTitles();
  434. if (titles.size() == this.essids().size()){
  435. this.filter.setESSIDs(new ArrayList<String>());
  436. } else {
  437. this.filter.setESSIDs(titles);
  438. }
  439. }
  440. if(title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
  441. ArrayList<String> protocols = dialog.getSelectedItemTitles();
  442. if (protocols.size() == this.protocolTitles().size()){
  443. this.filter.setProtocols(new ArrayList<String>());
  444. } else {
  445. this.filter.setProtocols(dialog.getSelectedItemTitles());
  446. }
  447. }
  448. if(title.equals(FILTER_MENU_TITLE_SORTING)){
  449. ArrayList<String> titles = dialog.getSelectedItemTitles();
  450. String t = titles.get(0);
  451. int sortType = this.sortTypeTtiles().indexOf(t);
  452. this.filter.setSorttype(SortType.values()[sortType]);
  453. }
  454. this.populateListViewFromDB(this.expListView);
  455. }
  456. public void onDialogNegativeClick(ChecklistDialog dialog) {
  457. }
  458. /*****************************
  459. *
  460. * TEST
  461. *
  462. * ***************************/
  463. private void addRecordToDB() {
  464. if ((dbh.getRecordCount() > 0)) return;
  465. Calendar cal = Calendar.getInstance();
  466. int maxProtocolsIndex = this.getResources().getStringArray(
  467. R.array.protocols).length;
  468. int numberofRecords = (int) (Math.random() * (50 - 10));
  469. for (int i = 0; i < numberofRecords; i++) {
  470. Record record = new Record();
  471. record.setId(i);
  472. record.setAttack_id(i);
  473. record.setBssid("BSSID: " + i);
  474. record.setSsid("SSID: w" + i);
  475. record.setTimestamp(cal.getTimeInMillis()
  476. + ((i * 60 * 60 * 60 * 24) * 1000));
  477. int index = i % maxProtocolsIndex;
  478. String protocolName = this.getResources().getStringArray(
  479. R.array.protocols)[index];
  480. record.setProtocol(protocolName);
  481. record.setLocalIP("127.0.0.1");
  482. record.setType(TYPE.SEND);
  483. dbh.addRecord(record);
  484. }
  485. int countAllLogs = dbh.getAllRecords().size();
  486. int countRecords = dbh.getRecordCount();
  487. int countAttacks = dbh.getAttackCount();
  488. if ((countRecords == 0)) {
  489. Record rec = dbh.getRecordOfAttackId(0);
  490. Record rec2 = dbh.getRecord(0);
  491. System.out.println(""+"Could not create logs!");
  492. System.exit(0);
  493. }
  494. }
  495. }