RecordOverviewFragment.java 25 KB

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