RecordOverviewFragment.java 28 KB

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