RecordOverviewFragment.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. package de.tudarmstadt.informatik.hostage.ui2.fragment;
  2. import java.text.DateFormat;
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.Calendar;
  6. import java.util.Collections;
  7. import java.util.Comparator;
  8. import java.util.Date;
  9. import java.util.HashMap;
  10. import java.util.Random;
  11. import android.annotation.SuppressLint;
  12. import android.app.FragmentManager;
  13. import android.content.Context;
  14. import android.content.Intent;
  15. import android.os.Bundle;
  16. import android.util.Log;
  17. import android.view.LayoutInflater;
  18. import android.view.Menu;
  19. import android.view.MenuInflater;
  20. import android.view.View;
  21. import android.view.ViewGroup;
  22. import android.widget.ExpandableListView;
  23. import android.widget.ImageButton;
  24. import com.google.android.gms.maps.model.LatLng;
  25. import de.tudarmstadt.informatik.hostage.R;
  26. import de.tudarmstadt.informatik.hostage.logging.AttackRecord;
  27. import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
  28. import de.tudarmstadt.informatik.hostage.logging.Record;
  29. import de.tudarmstadt.informatik.hostage.logging.MessageRecord;
  30. import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
  31. import de.tudarmstadt.informatik.hostage.ui.LogFilter;
  32. import de.tudarmstadt.informatik.hostage.ui.LogFilter.SortType;
  33. import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
  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. import de.tudarmstadt.informatik.hostage.ui2.popup.AbstractPopup;
  39. import de.tudarmstadt.informatik.hostage.ui2.popup.AbstractPopupItem;
  40. import de.tudarmstadt.informatik.hostage.ui2.popup.SimplePopupItem;
  41. import de.tudarmstadt.informatik.hostage.ui2.popup.SimplePopupTable;
  42. import de.tudarmstadt.informatik.hostage.ui2.popup.SplitPopupItem;
  43. public class RecordOverviewFragment extends UpNavigatibleFragment implements ChecklistDialog.ChecklistDialogListener,
  44. DateTimeDialogFragment.DateTimeDialogFragmentListener {
  45. static final String FILTER_MENU_TITLE_BSSID = "BSSID";
  46. static final String FILTER_MENU_TITLE_ESSID = "ESSID";
  47. static final String FILTER_MENU_TITLE_PROTOCOLS = MainActivity.getContext().getString(R.string.rec_protocol);
  48. static final String FILTER_MENU_TITLE_TIMESTAMP_BELOW = MainActivity.getContext().getString(R.string.rec_latest);
  49. static final String FILTER_MENU_TITLE_TIMESTAMP_ABOVE = MainActivity.getContext().getString(R.string.rec_earliest);
  50. static final String FILTER_MENU_TITLE_SORTING = MainActivity.getContext().getString(R.string.rec_sortby);
  51. static final String FILTER_MENU_TITLE_REMOVE = MainActivity.getContext().getString(R.string.rec_reset_filter);
  52. static final String FILTER_MENU_TITLE_GROUP = MainActivity.getContext().getString(R.string.rec_group_by);
  53. static final String FILTER_MENU_POPUP_TITLE = MainActivity.getContext().getString(R.string.rec_filter_by);
  54. private boolean wasBelowTimePicker;
  55. private LogFilter filter;
  56. private boolean showFilterButton;
  57. private int mListPosition = -1;
  58. private int mItemPosition = -1;
  59. public String groupingKey;
  60. private ExpandableListView expListView;
  61. HostageDBOpenHelper dbh;
  62. private String sectionToOpen = "";
  63. private ArrayList<Integer> openSections;
  64. public void setFilter(LogFilter filter) {
  65. this.filter = filter;
  66. }
  67. public RecordOverviewFragment() {
  68. }
  69. public void setGroupKey(String key) {
  70. this.groupingKey = key;
  71. }
  72. @Override
  73. public void onCreate(Bundle savedInstanceState) {
  74. super.onCreate(savedInstanceState);
  75. setHasOptionsMenu(true);
  76. }
  77. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  78. getActivity().setTitle(getResources().getString(R.string.drawer_records));
  79. dbh = new HostageDBOpenHelper(this.getActivity().getBaseContext());
  80. // Get the message from the intent
  81. if (this.filter == null) {
  82. Intent intent = this.getActivity().getIntent();
  83. LogFilter filter = intent.getParcelableExtra(LogFilter.LOG_FILTER_INTENT_KEY);
  84. if (filter == null) {
  85. this.clearFilter();
  86. } else {
  87. this.filter = filter;
  88. }
  89. }
  90. if (this.groupingKey == null)
  91. this.groupingKey = this.groupingTitles().get(0);
  92. this.setShowFilterButton(!this.filter.isNotEditable());
  93. // this.addRecordToDB();
  94. View rootView = inflater.inflate(this.getLayoutId(), container, false);
  95. ExpandableListView mylist = (ExpandableListView) rootView.findViewById(R.id.loglistview);
  96. this.expListView = mylist;
  97. populateListViewFromDB(mylist);
  98. if (this.openSections != null && this.openSections.size() != 0) {
  99. for (int i = 0; i < this.openSections.size(); i++) {
  100. int index = this.openSections.get(i);
  101. this.expListView.expandGroup(index);
  102. }
  103. } else {
  104. this.openSections = new ArrayList<Integer>();
  105. }
  106. if (mListPosition != -1 && mItemPosition != -1)
  107. this.expListView.setSelectedChild(mListPosition, mItemPosition, true);
  108. mListPosition = -1;
  109. mItemPosition = -1;
  110. registerListClickCallback(mylist);
  111. ImageButton filterButton = (ImageButton) rootView.findViewById(R.id.FilterButton);
  112. filterButton.setOnClickListener(new View.OnClickListener() {
  113. public void onClick(View v) {
  114. RecordOverviewFragment.this.openFilterPopupMenuOnView(v);
  115. }
  116. });
  117. filterButton.setVisibility(this.showFilterButton ? View.VISIBLE : View.INVISIBLE);
  118. ImageButton sortButton = (ImageButton) rootView.findViewById(R.id.SortButton);
  119. sortButton.setOnClickListener(new View.OnClickListener() {
  120. public void onClick(View v) {
  121. // Open SortMenu
  122. RecordOverviewFragment.this.openSortingDialog();
  123. }
  124. });
  125. ImageButton groupButton = (ImageButton) rootView.findViewById(R.id.GroupButton);
  126. groupButton.setOnClickListener(new View.OnClickListener() {
  127. public void onClick(View v) {
  128. // Open SortMenu
  129. RecordOverviewFragment.this.openGroupingDialog();
  130. }
  131. });
  132. return rootView;
  133. }
  134. public int getLayoutId() {
  135. return R.layout.fragment_record_list;
  136. }
  137. public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
  138. super.onCreateOptionsMenu(menu, inflater);
  139. }
  140. public void onFilterMenuItemSelected(AbstractPopupItem item) {
  141. String title = item.getTitle();
  142. if (item instanceof SplitPopupItem) {
  143. SplitPopupItem splitItem = (SplitPopupItem) item;
  144. if (splitItem.wasRightTouch) {
  145. this.openTimestampToFilterDialog();
  146. } else {
  147. this.openTimestampFromFilterDialog();
  148. }
  149. return;
  150. }
  151. if (title != null) {
  152. if (title.equals(FILTER_MENU_TITLE_BSSID)) {
  153. this.openBSSIDFilterDialog();
  154. }
  155. if (title.equals(FILTER_MENU_TITLE_ESSID)) {
  156. this.openESSIDFilterDialog();
  157. }
  158. if (title.equals(FILTER_MENU_TITLE_PROTOCOLS)) {
  159. this.openProtocolsFilterDialog();
  160. }
  161. if (title.equals(FILTER_MENU_TITLE_SORTING)) {
  162. this.openSortingDialog();
  163. }
  164. if (title.equals(FILTER_MENU_TITLE_REMOVE)) {
  165. this.clearFilter();
  166. this.populateListViewFromDB(this.expListView);
  167. }
  168. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)) {
  169. this.openTimestampToFilterDialog();
  170. }
  171. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)) {
  172. this.openTimestampFromFilterDialog();
  173. }
  174. }
  175. // return super.onOptionsItemSelected(item);
  176. }
  177. public void onStart() {
  178. super.onStart();
  179. // this.populateListViewFromDB(this.expListView);
  180. if (this.expListView.getExpandableListAdapter().getGroupCount() == 1) {
  181. this.expListView.expandGroup(0);
  182. } else {
  183. this.setSectionToOpen(this.sectionToOpen);
  184. }
  185. }
  186. /*****************************
  187. *
  188. * Public API
  189. *
  190. * ***************************/
  191. /**
  192. * Group records by SSID and expand given SSID
  193. *
  194. * @param SSID
  195. * the SSID
  196. */
  197. public void showDetailsForSSID(Context context, String SSID) {
  198. Log.e("RecordOverviewFragment", "Implement showDetailsForSSID!!");
  199. this.clearFilter();
  200. int ESSID_INDEX = 2;
  201. ArrayList<String> ssids = new ArrayList<String>();
  202. this.sectionToOpen = SSID;
  203. this.groupingKey = this.groupingTitles(context).get(ESSID_INDEX);
  204. }
  205. /*****************************
  206. * UglyDbHelper
  207. *
  208. * ListView Stuff
  209. *
  210. * ***************************/
  211. private void populateListViewFromDB(ExpandableListView mylist) {
  212. HashMap<String, ArrayList<ExpandableListItem>> sectionData = new HashMap<String, ArrayList<ExpandableListItem>>();
  213. ArrayList<Record> data = dbh.getRecordsForFilter(this.filter);
  214. // Adding Items to ListView
  215. String keys[] = new String[] { this.getString(R.string.RecordBSSID), this.getString(R.string.RecordSSID), this.getString(R.string.RecordProtocol),
  216. this.getString(R.string.RecordTimestamp) };
  217. int ids[] = new int[] { R.id.RecordTextFieldBSSID, R.id.RecordTextFieldSSID, R.id.RecordTextFieldProtocol, R.id.RecordTextFieldTimestamp };
  218. HashMap<String, Integer> mapping = new HashMap<String, Integer>();
  219. int i = 0;
  220. for (String key : keys) {
  221. mapping.put(key, ids[i]);
  222. i++;
  223. }
  224. ArrayList<String> groupTitle = new ArrayList<String>();
  225. for (Record val : data) {
  226. // DO GROUPING IN HERE
  227. HashMap<String, String> map = new HashMap<String, String>();
  228. map.put(this.getString(R.string.RecordBSSID), val.getBssid());
  229. map.put(this.getString(R.string.RecordSSID), val.getSsid());
  230. map.put(this.getString(R.string.RecordProtocol), val.getProtocol());
  231. map.put(this.getString(R.string.RecordTimestamp), this.getDateAsString(val.getTimestamp()));
  232. ExpandableListItem item = new ExpandableListItem();
  233. item.setData(map);
  234. item.setId_Mapping(mapping);
  235. item.setTag(val.getAttack_id());
  236. String groupID = this.getGroupValue(val);
  237. ArrayList<ExpandableListItem> items = sectionData.get(groupID);
  238. if (items == null) {
  239. items = new ArrayList<ExpandableListItem>();
  240. sectionData.put(groupID, items);
  241. groupTitle.add(groupID);
  242. }
  243. items.add(item);
  244. }
  245. Collections.sort(groupTitle, new Comparator<String>() {
  246. @Override
  247. public int compare(String s1, String s2) {
  248. return s1.compareToIgnoreCase(s2);
  249. }
  250. });
  251. RecordListAdapter adapter = new RecordListAdapter(this.getApplicationContext(), groupTitle, sectionData);
  252. mylist.setAdapter(adapter);
  253. }
  254. private void setSectionToOpen(String s) {
  255. this.sectionToOpen = s;
  256. if (this.sectionToOpen != null && this.sectionToOpen.length() != 0) {
  257. if (this.getGroupTitle().contains(this.sectionToOpen)) {
  258. int section = this.getGroupTitle().indexOf(this.sectionToOpen);
  259. this.expListView.expandGroup(section);
  260. this.sectionToOpen = "";
  261. }
  262. }
  263. }
  264. private Context getBaseContext() {
  265. return this.getActivity().getBaseContext();
  266. }
  267. private Context getApplicationContext() {
  268. return this.getActivity().getApplicationContext();
  269. }
  270. private void registerListClickCallback(ExpandableListView mylist) {
  271. mylist.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
  272. @Override
  273. public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i2, long l) {
  274. RecordListAdapter adapter = (RecordListAdapter) expandableListView.getExpandableListAdapter();
  275. ExpandableListItem item = (ExpandableListItem) adapter.getChild(i, i2);
  276. mListPosition = i;
  277. mItemPosition = i2;
  278. HostageDBOpenHelper dbh = new HostageDBOpenHelper(getBaseContext());
  279. Record rec = dbh.getRecordOfAttackId((int) item.getTag());
  280. RecordOverviewFragment.this.pushRecordDetailViewForRecord(rec);
  281. return true;
  282. }
  283. });
  284. mylist.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
  285. @Override
  286. public void onGroupExpand(int i) {
  287. RecordOverviewFragment.this.openSections.add(new Integer(i));
  288. }
  289. });
  290. mylist.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
  291. @Override
  292. public void onGroupCollapse(int i) {
  293. RecordOverviewFragment.this.openSections.remove(new Integer(i));
  294. }
  295. });
  296. }
  297. /*****************************
  298. *
  299. * Date Transform
  300. *
  301. * ***************************/
  302. @SuppressLint("SimpleDateFormat")
  303. private String getDateAsString(long timeStamp) {
  304. try {
  305. DateFormat sdf = new SimpleDateFormat("H:mm d.M.yy");
  306. Date netDate = (new Date(timeStamp));
  307. return sdf.format(netDate);
  308. } catch (Exception ex) {
  309. return "xx";
  310. }
  311. }
  312. /*****************************
  313. *
  314. * Getter / Setter
  315. *
  316. * ***************************/
  317. public boolean isShowFilterButton() {
  318. return showFilterButton;
  319. }
  320. public void setShowFilterButton(boolean showFilterButton) {
  321. this.showFilterButton = showFilterButton;
  322. }
  323. /*****************************
  324. *
  325. * Open Dialog Methods
  326. *
  327. * ***************************/
  328. private void openGroupingDialog() {
  329. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_GROUP, this.groupingTitles(), this.selectedGroup(), false, this);
  330. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_GROUP);
  331. }
  332. private void openBSSIDFilterDialog() {
  333. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_BSSID, this.bssids(), this.selectedBSSIDs(), true, this);
  334. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_BSSID);
  335. }
  336. private void openESSIDFilterDialog() {
  337. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_ESSID, this.essids(), this.selectedESSIDs(), true, this);
  338. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_ESSID);
  339. }
  340. private void openProtocolsFilterDialog() {
  341. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_PROTOCOLS, this.protocolTitles(), this.selectedProtocols(), true, this);
  342. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_PROTOCOLS);
  343. }
  344. private void openTimestampFromFilterDialog() {
  345. this.wasBelowTimePicker = false;
  346. DateTimeDialogFragment newFragment = new DateTimeDialogFragment(this.getActivity());
  347. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_SORTING);
  348. if (this.filter.aboveTimestamp != Long.MIN_VALUE)
  349. newFragment.setDate(this.filter.aboveTimestamp);
  350. }
  351. private void openTimestampToFilterDialog() {
  352. this.wasBelowTimePicker = true;
  353. DateTimeDialogFragment newFragment = new DateTimeDialogFragment(this.getActivity());
  354. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_SORTING);
  355. if (this.filter.belowTimestamp != Long.MAX_VALUE)
  356. newFragment.setDate(this.filter.belowTimestamp);
  357. }
  358. private void openSortingDialog() {
  359. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_SORTING, this.sortTypeTiles(), this.selectedSorttype(), false, this);
  360. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_SORTING);
  361. }
  362. /*****************************
  363. *
  364. * Grouping Stuff
  365. *
  366. * ***************************/
  367. public String getGroupValue(Record rec) {
  368. int index = this.groupingTitles().indexOf(this.groupingKey);
  369. switch (index) {
  370. case 0:
  371. return rec.getProtocol();
  372. case 1:
  373. return rec.getBssid();
  374. case 2:
  375. return rec.getSsid();
  376. default:
  377. return rec.getProtocol();
  378. }
  379. }
  380. public ArrayList<String> getGroupTitle() {
  381. int index = this.groupingTitles().indexOf(this.groupingKey);
  382. switch (index) {
  383. case 0:
  384. return this.protocolTitles();
  385. case 1:
  386. return this.bssids();
  387. case 2:
  388. return this.essids();
  389. default:
  390. return this.protocolTitles();
  391. }
  392. }
  393. /*****************************
  394. *
  395. * Filter Stuff
  396. *
  397. * ***************************/
  398. private void openFilterPopupMenuOnView(View v) {
  399. SimplePopupTable filterMenu = new SimplePopupTable(this.getActivity(), new AbstractPopup.OnPopupItemClickListener() {
  400. public void onItemClick(Object ob) {
  401. if (ob instanceof AbstractPopupItem) {
  402. AbstractPopupItem item = (AbstractPopupItem) ob;
  403. RecordOverviewFragment.this.onFilterMenuItemSelected(item);
  404. }
  405. }
  406. });
  407. filterMenu.setTitle(FILTER_MENU_POPUP_TITLE);
  408. for (String title : RecordOverviewFragment.this.filterMenuTitles()) {
  409. AbstractPopupItem item = null;
  410. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW))
  411. continue;
  412. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)) {
  413. item = new SplitPopupItem(this.getActivity());
  414. item.setValue(SplitPopupItem.RIGHT_TITLE, FILTER_MENU_TITLE_TIMESTAMP_BELOW);
  415. item.setValue(SplitPopupItem.LEFT_TITLE, FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  416. if (this.filter.hasBelowTimestamp()) {
  417. item.setValue(SplitPopupItem.RIGHT_SUBTITLE, this.getDateAsString(this.filter.belowTimestamp));
  418. }
  419. if (this.filter.hasAboveTimestamp()) {
  420. item.setValue(SplitPopupItem.LEFT_SUBTITLE, this.getDateAsString(this.filter.aboveTimestamp));
  421. }
  422. } else {
  423. item = new SimplePopupItem(this.getActivity());
  424. item.setTitle(title);
  425. ((SimplePopupItem) item).setSelected(this.isFilterSetForTitle(title));
  426. }
  427. filterMenu.addItem(item);
  428. }
  429. filterMenu.showOnView(v);
  430. }
  431. private boolean isFilterSetForTitle(String title) {
  432. if (title.equals(FILTER_MENU_TITLE_BSSID)) {
  433. return this.filter.hasBSSIDs();
  434. }
  435. if (title.equals(FILTER_MENU_TITLE_ESSID)) {
  436. return this.filter.hasESSIDs();
  437. }
  438. if (title.equals(FILTER_MENU_TITLE_PROTOCOLS)) {
  439. return this.filter.hasProtocols();
  440. }
  441. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)) {
  442. return this.filter.hasBelowTimestamp();
  443. }
  444. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)) {
  445. return this.filter.hasAboveTimestamp();
  446. }
  447. return false;
  448. }
  449. private void clearFilter() {
  450. if (filter == null)
  451. this.filter = new LogFilter();
  452. this.filter.clear();
  453. }
  454. public ArrayList<String> groupingTitles(Context context) {
  455. ArrayList<String> titles = new ArrayList<String>();
  456. for (String groupTitle : context.getResources().getStringArray(R.array.Grouping)) {
  457. titles.add(groupTitle);
  458. }
  459. return titles;
  460. }
  461. public ArrayList<String> groupingTitles() {
  462. ArrayList<String> titles = new ArrayList<String>();
  463. for (String groupTitle : this.getResources().getStringArray(R.array.Grouping)) {
  464. titles.add(groupTitle);
  465. }
  466. return titles;
  467. }
  468. public boolean[] selectedGroup() {
  469. ArrayList<String> groups = this.groupingTitles();
  470. boolean[] selected = new boolean[groups.size()];
  471. int i = 0;
  472. for (String group : groups) {
  473. selected[i] = (group.equals(this.groupingKey));
  474. i++;
  475. }
  476. return selected;
  477. }
  478. public ArrayList<String> protocolTitles() {
  479. ArrayList<String> titles = new ArrayList<String>();
  480. for (String protocol : this.getResources().getStringArray(R.array.protocols)) {
  481. titles.add(protocol);
  482. }
  483. return titles;
  484. }
  485. public boolean[] selectedProtocols() {
  486. ArrayList<String> protocols = this.protocolTitles();
  487. boolean[] selected = new boolean[protocols.size()];
  488. int i = 0;
  489. for (String protocol : protocols) {
  490. selected[i] = (this.filter.protocols.contains(protocol));
  491. i++;
  492. }
  493. return selected;
  494. }
  495. public ArrayList<String> sortTypeTiles() {
  496. ArrayList<String> titles = new ArrayList<String>();
  497. titles.add(MainActivity.getContext().getString(R.string.rec_time));
  498. titles.add(MainActivity.getContext().getString(R.string.rec_protocol));
  499. titles.add("BSSID");
  500. titles.add("ESSID");
  501. // titles.add("Remote Host Name");
  502. // titles.add("Local Host Name");
  503. // titles.add("Attack ID");
  504. // titles.add("ID");
  505. return titles;
  506. }
  507. public boolean[] selectedSorttype() {
  508. ArrayList<String> types = this.sortTypeTiles();
  509. boolean[] selected = new boolean[types.size()];
  510. int i = 0;
  511. for (String sorttype : types) {
  512. selected[i] = (this.filter.sorttype.toString().equals(sorttype));
  513. i++;
  514. }
  515. return selected;
  516. }
  517. public ArrayList<String> bssids() {
  518. ArrayList<String> records = dbh.getUniqueBSSIDRecords();
  519. return records;
  520. }
  521. public boolean[] selectedBSSIDs() {
  522. ArrayList<String> bssids = this.bssids();
  523. boolean[] selected = new boolean[bssids.size()];
  524. int i = 0;
  525. for (String bssid : bssids) {
  526. selected[i] = (this.filter.BSSIDs.contains(bssid));
  527. i++;
  528. }
  529. return selected;
  530. }
  531. public ArrayList<String> essids() {
  532. ArrayList<String> records = dbh.getUniqueESSIDRecords();
  533. return records;
  534. }
  535. public boolean[] selectedESSIDs() {
  536. ArrayList<String> essids = this.essids();
  537. boolean[] selected = new boolean[essids.size()];
  538. int i = 0;
  539. for (String essid : essids) {
  540. selected[i] = (this.filter.ESSIDs.contains(essid));
  541. i++;
  542. }
  543. return selected;
  544. }
  545. private ArrayList<String> filterMenuTitles() {
  546. ArrayList<String> titles = new ArrayList<String>();
  547. titles.add(FILTER_MENU_TITLE_BSSID);
  548. titles.add(FILTER_MENU_TITLE_ESSID);
  549. titles.add(FILTER_MENU_TITLE_PROTOCOLS);
  550. titles.add(FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  551. titles.add(FILTER_MENU_TITLE_TIMESTAMP_BELOW);
  552. if (this.filter.isSet())
  553. titles.add(FILTER_MENU_TITLE_REMOVE);
  554. return titles;
  555. }
  556. /*****************************
  557. *
  558. * Listener Actions
  559. *
  560. * ***************************/
  561. public void onDateTimePickerPositiveClick(DateTimeDialogFragment dialog) {
  562. if (this.wasBelowTimePicker) {
  563. this.filter.setBelowTimestamp(dialog.getDate());
  564. } else {
  565. this.filter.setAboveTimestamp(dialog.getDate());
  566. }
  567. this.populateListViewFromDB(this.expListView);
  568. }
  569. public void onDateTimePickerNegativeClick(DateTimeDialogFragment dialog) {
  570. if (this.wasBelowTimePicker) {
  571. this.filter.setBelowTimestamp(Long.MAX_VALUE);
  572. } else {
  573. this.filter.setAboveTimestamp(Long.MIN_VALUE);
  574. }
  575. }
  576. public void onDialogPositiveClick(ChecklistDialog dialog) {
  577. String title = dialog.getTitle();
  578. if (title.equals(FILTER_MENU_TITLE_BSSID)) {
  579. ArrayList<String> titles = dialog.getSelectedItemTitles();
  580. if (titles.size() == this.bssids().size()) {
  581. this.filter.setBSSIDs(new ArrayList<String>());
  582. } else {
  583. this.filter.setBSSIDs(titles);
  584. }
  585. }
  586. if (title.equals(FILTER_MENU_TITLE_ESSID)) {
  587. ArrayList<String> titles = dialog.getSelectedItemTitles();
  588. if (titles.size() == this.essids().size()) {
  589. this.filter.setESSIDs(new ArrayList<String>());
  590. } else {
  591. this.filter.setESSIDs(titles);
  592. }
  593. }
  594. if (title.equals(FILTER_MENU_TITLE_PROTOCOLS)) {
  595. ArrayList<String> protocols = dialog.getSelectedItemTitles();
  596. if (protocols.size() == this.protocolTitles().size()) {
  597. this.filter.setProtocols(new ArrayList<String>());
  598. } else {
  599. this.filter.setProtocols(dialog.getSelectedItemTitles());
  600. }
  601. }
  602. if (title.equals(FILTER_MENU_TITLE_SORTING)) {
  603. ArrayList<String> titles = dialog.getSelectedItemTitles();
  604. if (titles.size() == 0)
  605. return;
  606. String t = titles.get(0);
  607. int sortType = this.sortTypeTiles().indexOf(t);
  608. this.filter.setSorttype(SortType.values()[sortType]);
  609. }
  610. if (title.equals(FILTER_MENU_TITLE_GROUP)) {
  611. ArrayList<String> titles = dialog.getSelectedItemTitles();
  612. if (titles.size() == 0)
  613. return;
  614. this.groupingKey = titles.get(0);
  615. }
  616. this.populateListViewFromDB(this.expListView);
  617. }
  618. public void onDialogNegativeClick(ChecklistDialog dialog) {
  619. }
  620. /*****************************
  621. *
  622. * TEST
  623. *
  624. * ***************************/
  625. private void addRecordToDB() {
  626. if ((dbh.getRecordCount() > 0))
  627. return;
  628. Calendar cal = Calendar.getInstance();
  629. int maxProtocolsIndex = this.getResources().getStringArray(R.array.protocols).length;
  630. Random random = new Random();
  631. LatLng tudarmstadtLoc = new LatLng(49.86923, 8.6632768);
  632. final int numSSIDs = 10;
  633. final int numUniqueSSIDs = 6;
  634. final double ssidRadius = 0.1;
  635. final double bssidRadius = 0.004;
  636. int id = 0; // global id
  637. for (int ssid = 0; ssid < numSSIDs; ssid++) {
  638. LatLng ssidLocation = new LatLng(tudarmstadtLoc.latitude - ssidRadius + 2.0 * ssidRadius * Math.random(), tudarmstadtLoc.longitude - ssidRadius
  639. + 2.0 * ssidRadius * Math.random());
  640. String ssidName = "WiFi" + ((ssid % numUniqueSSIDs) + 1);
  641. int numBSSIDs = (Math.abs(random.nextInt()) % 20) + 1;
  642. for (int bssid = 0; bssid < numBSSIDs; bssid++) {
  643. MessageRecord message = new MessageRecord();
  644. AttackRecord attack = new AttackRecord();
  645. NetworkRecord network = new NetworkRecord();
  646. message.setId(id);
  647. message.setAttack_id(id);
  648. message.setType(MessageRecord.TYPE.SEND);
  649. message.setTimestamp(cal.getTimeInMillis() + ((id * 60 * 60 * 60 * 24) * 1000));
  650. int index = id % maxProtocolsIndex;
  651. String protocolName = this.getResources().getStringArray(R.array.protocols)[index];
  652. attack.setAttack_id(id);
  653. attack.setProtocol(protocolName);
  654. attack.setLocalIP("127.0.0.1");
  655. attack.setBssid("BSSID" + id);
  656. network.setBssid("BSSID" + id);
  657. network.setSsid(ssidName);
  658. network.setLatitude(ssidLocation.latitude - bssidRadius + 2.0 * bssidRadius * Math.random());
  659. network.setLongitude(ssidLocation.longitude - bssidRadius + 2.0 * bssidRadius * Math.random());
  660. id++;
  661. dbh.updateNetworkInformation(network);
  662. dbh.addAttackRecord(attack);
  663. dbh.addMessageRecord(message);
  664. }
  665. }
  666. int countAllLogs = dbh.getAllRecords().size();
  667. int countRecords = dbh.getRecordCount();
  668. int countAttacks = dbh.getAttackCount();
  669. if ((countRecords == 0)) {
  670. Record rec = dbh.getRecordOfAttackId(0);
  671. Record rec2 = dbh.getRecord(0);
  672. System.out.println("" + "Could not create logs!");
  673. }
  674. }
  675. /* Navigation */
  676. private void pushRecordDetailViewForRecord(Record record) {
  677. FragmentManager fm = this.getActivity().getFragmentManager();
  678. if (fm != null) {
  679. RecordDetailFragment newFragment = new RecordDetailFragment();
  680. newFragment.setRecord(record);
  681. newFragment.setUpNavigatible(true);
  682. MainActivity.getInstance().injectFragment(newFragment);
  683. }
  684. }
  685. }