RecordOverviewFragment.java 28 KB

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