RecordOverviewFragment.java 20 KB

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