RecordOverviewFragment.java 28 KB

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