StatisticsFragment.java 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. package de.tudarmstadt.informatik.hostage.ui2.fragment;
  2. import android.annotation.SuppressLint;
  3. import android.app.Fragment;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.graphics.Color;
  7. import android.os.Bundle;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.AdapterView;
  12. import android.widget.ImageButton;
  13. import android.widget.LinearLayout;
  14. import android.widget.ListView;
  15. import android.widget.TextView;
  16. import com.echo.holographlibrary.Bar;
  17. import com.echo.holographlibrary.BarGraph;
  18. import com.echo.holographlibrary.Line;
  19. import com.echo.holographlibrary.LineGraph;
  20. import com.echo.holographlibrary.LinePoint;
  21. import com.echo.holographlibrary.PieGraph;
  22. import com.echo.holographlibrary.PieSlice;
  23. import java.text.DateFormat;
  24. import java.text.SimpleDateFormat;
  25. import java.util.ArrayList;
  26. import java.util.Calendar;
  27. import java.util.Collections;
  28. import java.util.Comparator;
  29. import java.util.Date;
  30. import java.util.HashMap;
  31. import de.tudarmstadt.informatik.hostage.R;
  32. import de.tudarmstadt.informatik.hostage.logging.Record;
  33. import de.tudarmstadt.informatik.hostage.logging.UglyDbHelper;
  34. import de.tudarmstadt.informatik.hostage.ui.LogFilter;
  35. import de.tudarmstadt.informatik.hostage.ui2.adapter.StatisticListAdapter;
  36. import de.tudarmstadt.informatik.hostage.ui2.dialog.ChecklistDialog;
  37. import de.tudarmstadt.informatik.hostage.ui2.helper.ColorSequenceGenerator;
  38. import de.tudarmstadt.informatik.hostage.ui2.model.PlotComparisonItem;
  39. import de.tudarmstadt.informatik.hostage.ui2.popup.AbstractPopup;
  40. import de.tudarmstadt.informatik.hostage.ui2.popup.AbstractPopupItem;
  41. import de.tudarmstadt.informatik.hostage.ui2.popup.SimplePopupItem;
  42. import de.tudarmstadt.informatik.hostage.ui2.popup.SimplePopupTable;
  43. /**
  44. * Created by Julien on 16.02.14.
  45. */
  46. public class StatisticsFragment extends Fragment implements ChecklistDialog.ChecklistDialogListener {
  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 MENU_TITLE_PROTOCOLS = "Protocols";
  53. static final String MENU_TITLE_NETWORK = "Networks";
  54. static final String MENU_TITLE_ATTACKS = "Attacks";
  55. static final String MENU_POPUP_TITLE = "Visualize";
  56. static final String CHART_TYPE_TITLE_BAR = "Bar Plot";
  57. static final String CHART_TYPE_TITLE_PIE = "Pie Plot";
  58. static final String CHART_TYPE_TITLE_LINE = "Line Plot";
  59. static final String DIALOG_PROTOCOLS_TITLE = "Select protocol data to compare";
  60. static final String DIALOG_NETWORK_TITLE = "Select network data to compare";
  61. static final String DIALOG_ATTACK_TITLE = "Select attack data to compare";
  62. static final String COMPARE_TITLE_AttacksPerProtocol = "Attacks per protocol";
  63. static final String COMPARE_TITLE_UsesPerProtocol = "Uses per protocol";
  64. static final String COMPARE_TITLE_AttacksPerDate = "Attacks per date";
  65. static final String COMPARE_TITLE_AttacksPerTime = "Attacks per time";
  66. static final String COMPARE_TITLE_AttacksPerBSSID = "Attacks per BSSID";
  67. static final String COMPARE_TITLE_AttacksPerESSID = "Attacks per ESSID";
  68. static final String FILTER_MENU_PROTOCOL_SINGLE_CHOICE_TITLE = "Select Protocol";
  69. static final String OTHER_CHART_TITLE = "Other";
  70. // MINIMAL 2
  71. static int MAX_NUMBER_OF_CHART_OBJECTS = 6;
  72. private boolean wasBelowTimePicker;
  73. private LogFilter filter;
  74. private boolean showFilterButton;
  75. private PieGraph pieGraph;
  76. private LineGraph lineGraph;
  77. private BarGraph barGraph;
  78. private View rootView;
  79. private View currentPlotView;
  80. private ArrayList<Integer> colorList;
  81. private ArrayList<PlotComparisonItem> currentData;
  82. private UglyDbHelper dbh;
  83. private ListView legendListView;
  84. private String selectedCompareData = COMPARE_TITLE_AttacksPerProtocol;
  85. public enum ChartType {
  86. PIE_CHART(0),
  87. BAR_CHART(1),
  88. LINE_CHART(2);
  89. private int value;
  90. private ChartType(int value) {
  91. this.value = value;
  92. }
  93. static public ChartType create(int value){
  94. if (value < 0 || value >= ChartType.values().length) return ChartType.PIE_CHART;
  95. return ChartType.values()[value];
  96. }
  97. public String toString(){
  98. if (this.equals(ChartType.create(0))){
  99. return CHART_TYPE_TITLE_PIE;
  100. }
  101. if (this.equals(ChartType.create(1))){
  102. return CHART_TYPE_TITLE_BAR;
  103. }
  104. return CHART_TYPE_TITLE_LINE;
  105. }
  106. }
  107. @Override
  108. public void onCreate(Bundle savedInstanceState) {
  109. super.onCreate(savedInstanceState);
  110. setHasOptionsMenu(true);
  111. }
  112. private ImageButton getFilterButton(){
  113. return (ImageButton) this.rootView.findViewById(R.id.FilterButton);
  114. }
  115. public int getLayoutID(){
  116. return R.layout.fragment_statistics;
  117. }
  118. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  119. Bundle savedInstanceState) {
  120. dbh = new UglyDbHelper(this.getBaseContext());
  121. // Get the message from the intent
  122. if (this.filter == null){
  123. Intent intent = this.getActivity().getIntent();
  124. LogFilter filter = intent.getParcelableExtra(LogFilter.LOG_FILTER_INTENT_KEY);
  125. if(filter == null){
  126. this.clearFilter();
  127. } else {
  128. this.filter = filter;
  129. }
  130. }
  131. View rootView = inflater.inflate(this.getLayoutID(), container, false);
  132. LinearLayout plotLayout = (LinearLayout) rootView.findViewById(R.id.plot_layout);
  133. plotLayout.setWillNotDraw(false);
  134. container.setWillNotDraw(false);
  135. this.legendListView = (ListView) rootView.findViewById(R.id.legend_list_view);
  136. this.legendListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  137. @Override
  138. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  139. StatisticsFragment.this.userTappedOnItem(i);
  140. }
  141. });
  142. this.rootView = rootView;
  143. rootView.setWillNotDraw(false);
  144. this.configureRootView(rootView);
  145. return rootView;
  146. }
  147. private Context getBaseContext(){
  148. return this.getActivity().getBaseContext();
  149. }
  150. private Context getApplicationContext(){
  151. return this.getActivity().getApplicationContext();
  152. }
  153. public void configureRootView(View rootView){
  154. LinearLayout plotLayout = (LinearLayout) rootView.findViewById(R.id.plot_layout);
  155. plotLayout.setWillNotDraw(false);
  156. plotLayout.removeAllViews();
  157. this.actualiseCurrentPlot();
  158. ImageButton visualButton = (ImageButton) rootView.findViewById(R.id.plot_data_button);
  159. visualButton.setOnClickListener(new View.OnClickListener() {
  160. public void onClick(View v) {
  161. StatisticsFragment.this.openPopupMenuOnView(v);
  162. }
  163. });
  164. this.setTitle(this.selectedCompareData);
  165. ImageButton filterButton = this.getFilterButton();
  166. filterButton.setOnClickListener(new View.OnClickListener() {
  167. public void onClick(View v) {
  168. StatisticsFragment.this.openFilterMenuOnView(v);
  169. }
  170. });
  171. }
  172. public void setTitle(String title){
  173. TextView titleView = (TextView) this.rootView.findViewById(R.id.title_text_view);
  174. if (title != null && titleView != null){
  175. titleView.setText(title);
  176. titleView.invalidate();
  177. }
  178. }
  179. public String getTitle(){
  180. TextView titleView = (TextView) this.rootView.findViewById(R.id.title_text_view);
  181. if (titleView != null){
  182. return "" + titleView.getText();
  183. }
  184. return "";
  185. }
  186. public void onStart() {
  187. super.onStart();
  188. this.actualiseCurrentPlot();
  189. this.currentPlotView.invalidate();
  190. }
  191. public void setChartType(ChartType type){
  192. boolean shouldChange = true;
  193. this.clearFilter();
  194. if (this.currentPlotView != null){
  195. if (type == ChartType.PIE_CHART){
  196. shouldChange = ! (this.currentPlotView instanceof PieGraph);
  197. } else {
  198. if (this.pieGraph != null)
  199. this.pieGraph.setVisibility(View.INVISIBLE);
  200. }
  201. if (type == ChartType.LINE_CHART){
  202. shouldChange = ! (this.currentPlotView instanceof LineGraph);
  203. } else {
  204. if (this.lineGraph != null)
  205. this.lineGraph.setVisibility(View.INVISIBLE);
  206. }
  207. if (type == ChartType.BAR_CHART){
  208. shouldChange = ! (this.currentPlotView instanceof BarGraph);
  209. // SHOW FILTER BUTTON
  210. ImageButton filterButton = this.getFilterButton();
  211. if (filterButton != null) filterButton.setVisibility(View.VISIBLE);
  212. } else {
  213. if (this.barGraph != null)
  214. this.barGraph.setVisibility(View.INVISIBLE);
  215. // SET FILTER BUTTON HIDDEN
  216. ImageButton filterButton = this.getFilterButton();
  217. if (filterButton != null) filterButton.setVisibility(View.INVISIBLE);
  218. }
  219. }
  220. if (shouldChange){
  221. this.currentPlotView = this.getPlotViewForType(type);
  222. this.currentPlotView.setVisibility(View.VISIBLE);
  223. }
  224. this.actualiseCurrentPlot();
  225. }
  226. public View getPlotViewForType(ChartType type){
  227. switch (type){
  228. case PIE_CHART:
  229. return this.getPieGraphView();
  230. case LINE_CHART:
  231. return this.getLineGraphView();
  232. default:
  233. return this.getBarGraphView();
  234. }
  235. }
  236. public void actualiseLegendList(){
  237. StatisticListAdapter adapter = new StatisticListAdapter(this.getApplicationContext(), this.currentData);
  238. this.legendListView.setAdapter(adapter);
  239. }
  240. /*
  241. * MENU
  242. * */
  243. private void openPopupMenuOnView(View anchorView){
  244. SimplePopupTable visualiseMenu = new SimplePopupTable(this.getActivity(), new AbstractPopup.OnPopupItemClickListener() {
  245. public void onItemClick(Object ob) {
  246. if (ob instanceof AbstractPopupItem){
  247. AbstractPopupItem item = (AbstractPopupItem) ob;
  248. StatisticsFragment.this.userSelectMenuItem(item);
  249. }
  250. }
  251. });
  252. visualiseMenu.setTitle(MENU_POPUP_TITLE);
  253. int id = 0;
  254. for(String title : StatisticsFragment.this.getMenuTitles()){
  255. SimplePopupItem item = new SimplePopupItem(this.getActivity());
  256. item.setTitle(title);
  257. item.setItemId(id);
  258. item.setSelected(this.isFilterSetForTitle(title));
  259. visualiseMenu.addItem(item);
  260. id++;
  261. }
  262. visualiseMenu.showOnView(anchorView);
  263. }
  264. private void userSelectMenuItem(AbstractPopupItem item){
  265. // OPEN A DIALOG TO SPECIFY THE VISUALISE DATA
  266. if (item.getTitle().equals(MENU_TITLE_PROTOCOLS)){
  267. this.openProtocolDataDialog();
  268. }
  269. if (item.getTitle().equals(MENU_TITLE_NETWORK)){
  270. this.openNetworkDataDialog();
  271. }
  272. if (item.getTitle().equals(MENU_TITLE_ATTACKS)){
  273. this.openAttackDataDialog();
  274. }
  275. }
  276. private ArrayList<String> getMenuTitles(){
  277. ArrayList<String> titles = new ArrayList<String>();
  278. titles.add(MENU_TITLE_PROTOCOLS);
  279. titles.add(MENU_TITLE_NETWORK);
  280. titles.add(MENU_TITLE_ATTACKS);
  281. return titles;
  282. }
  283. /*
  284. * PLOT DATA DIALOGS
  285. * */
  286. private void openProtocolDataDialog(){
  287. ArrayList<String> titles = this.getDialogProtocolDataTitle();
  288. ChecklistDialog newFragment = new ChecklistDialog(DIALOG_PROTOCOLS_TITLE, titles, this.selectedData(titles), false , this);
  289. newFragment.show(this.getActivity().getFragmentManager(), DIALOG_PROTOCOLS_TITLE);
  290. }
  291. private void openNetworkDataDialog(){
  292. ArrayList<String> titles = this.getDialogNetworkDataTitle();
  293. ChecklistDialog newFragment = new ChecklistDialog(DIALOG_NETWORK_TITLE, titles, this.selectedData(titles), false , this);
  294. newFragment.show(this.getActivity().getFragmentManager(), DIALOG_NETWORK_TITLE);
  295. }
  296. private void openAttackDataDialog(){
  297. ArrayList<String> titles = this.getDialogAttackDataTitle();
  298. ChecklistDialog newFragment = new ChecklistDialog(DIALOG_ATTACK_TITLE, titles, this.selectedData(titles), false , this);
  299. newFragment.show(this.getActivity().getFragmentManager(), DIALOG_ATTACK_TITLE);
  300. }
  301. /*
  302. *
  303. * DIALOG ACTION METHODS
  304. *
  305. * */
  306. public void onDialogPositiveClick(ChecklistDialog dialog) {
  307. String title = dialog.getTitle();
  308. ArrayList<String> titles =dialog.getSelectedItemTitles();
  309. if (title.equals(FILTER_MENU_PROTOCOL_SINGLE_CHOICE_TITLE)){
  310. this.filter.setProtocols(titles);
  311. this.actualiseCurrentPlot();
  312. String fragTitle = "" + this.getCurrentSelectedProtocol() + ": " + this.selectedCompareData;
  313. this.setTitle(fragTitle);
  314. return;
  315. }
  316. if (titles.size() != 0){
  317. String data = titles.get(0);
  318. this.setTitle(data);
  319. if (title.equals(DIALOG_PROTOCOLS_TITLE)){
  320. ChartType chartType = ChartType.PIE_CHART;
  321. this.selectedCompareData = data;
  322. this.setChartType(chartType);
  323. }
  324. if (title.equals(DIALOG_ATTACK_TITLE)){
  325. ChartType chartType = ChartType.LINE_CHART;
  326. this.selectedCompareData = data;
  327. this.setChartType(chartType);
  328. }
  329. if (title.equals(DIALOG_NETWORK_TITLE)){
  330. ChartType chartType = ChartType.BAR_CHART;
  331. this.selectedCompareData = data;
  332. this.setChartType(chartType);
  333. String fragTitle = "" + this.getCurrentSelectedProtocol() + ": " + this.selectedCompareData;
  334. this.setTitle(fragTitle);
  335. }
  336. }
  337. }
  338. public void onDialogNegativeClick(ChecklistDialog dialog) {
  339. }
  340. /*
  341. *
  342. * DIALOG DATA
  343. *
  344. * */
  345. private ArrayList<String> getDialogProtocolDataTitle(){
  346. ArrayList<String> data = new ArrayList<String>();
  347. data.add(COMPARE_TITLE_AttacksPerProtocol);
  348. data.add(COMPARE_TITLE_UsesPerProtocol);
  349. return data;
  350. }
  351. private ArrayList<String> getDialogAttackDataTitle(){
  352. ArrayList<String> data = new ArrayList<String>();
  353. data.add(COMPARE_TITLE_AttacksPerDate);
  354. data.add(COMPARE_TITLE_AttacksPerTime);
  355. return data;
  356. }
  357. private ArrayList<String> getDialogNetworkDataTitle(){
  358. ArrayList<String> data = new ArrayList<String>();
  359. data.add(COMPARE_TITLE_AttacksPerBSSID);
  360. data.add(COMPARE_TITLE_AttacksPerESSID);
  361. return data;
  362. }
  363. private boolean[] selectedData(ArrayList<String> data){
  364. boolean[] selected = new boolean[data.size()];
  365. // SET DEFAULT
  366. selected[0] = true;
  367. return selected;
  368. }
  369. /*
  370. *
  371. * FILTER BUTTON
  372. *
  373. * */
  374. private void openFilterMenuOnView(View anchor){
  375. ArrayList<String> titles = this.protocolTitles();
  376. boolean[] selected = new boolean[titles.size()];
  377. int i = 0;
  378. for (String title : titles){
  379. selected[i] = title.equals(this.getCurrentSelectedProtocol());
  380. i++;
  381. }
  382. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_PROTOCOL_SINGLE_CHOICE_TITLE, titles, selected, false , this);
  383. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_PROTOCOL_SINGLE_CHOICE_TITLE);
  384. }
  385. /**
  386. *
  387. * PLOT TOUCH HANDLING
  388. *
  389. * */
  390. public void onSliceClick(int index){
  391. this.userTappedOnItem(index);
  392. }
  393. public void onBarClick(int index){
  394. this.userTappedOnItem(index);
  395. }
  396. /*
  397. *
  398. * PLOT TYPES
  399. *
  400. * **/
  401. public PieGraph getPieGraphView(){
  402. if (this.pieGraph == null) {
  403. this.pieGraph = new PieGraph(this.getApplicationContext());
  404. LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  405. plotLayout.addView(this.pieGraph);
  406. this.pieGraph.setOnSliceClickedListener(new PieGraph.OnSliceClickedListener() {
  407. @Override
  408. public void onClick(int index) {
  409. StatisticsFragment.this.onSliceClick(index);
  410. }
  411. });
  412. }
  413. return this.pieGraph;
  414. }
  415. public LineGraph getLineGraphView(){
  416. if (this.lineGraph == null) {
  417. this.lineGraph = new LineGraph(this.getActivity());
  418. LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  419. plotLayout.addView(this.lineGraph);
  420. }
  421. return this.lineGraph;
  422. }
  423. public BarGraph getBarGraphView(){
  424. if (this.barGraph == null) {
  425. this.barGraph = new BarGraph(this.getActivity());
  426. LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  427. this.barGraph.setLayoutParams(new ViewGroup.LayoutParams(plotLayout.getWidth() - plotLayout.getPaddingLeft() - plotLayout.getPaddingRight(), plotLayout.getHeight() - - plotLayout.getPaddingTop() - plotLayout.getPaddingBottom()));
  428. plotLayout.addView(this.barGraph);
  429. this.barGraph.setShowBarText(false);
  430. this.barGraph.setPopupImageID(R.drawable.popup_black);
  431. this.barGraph.setOnBarClickedListener(new BarGraph.OnBarClickedListener() {
  432. @Override
  433. public void onClick(int i) {
  434. StatisticsFragment.this.onBarClick(i);
  435. }
  436. });
  437. }
  438. return this.barGraph;
  439. }
  440. /*
  441. * FEED PLOTS WITH DATA
  442. * */
  443. public void setPieGraphData(PieGraph piegraph){
  444. this.currentData = this.getPieData();
  445. if (this.currentData == null){
  446. this.currentData = new ArrayList<PlotComparisonItem>();
  447. }
  448. this.pieGraph.removeSlices();
  449. for (PlotComparisonItem item : this.currentData){
  450. PieSlice slice = new PieSlice();
  451. slice.setColor(item.getColor());
  452. Double value2 = (Double) item.getValue2();
  453. float v = value2.floatValue();
  454. slice.setValue(v);
  455. slice.setTitle(item.getTitle());
  456. this.pieGraph.addSlice(slice);
  457. }
  458. this.pieGraph.invalidate();
  459. }
  460. public void setLineGraphData(LineGraph linegraph){
  461. this.currentData = this.getLineData();
  462. if (this.currentData == null){
  463. this.currentData = new ArrayList<PlotComparisonItem>();
  464. }
  465. this.lineGraph.removeAllLines();
  466. double rangeMax = 0;
  467. double rangeMin = 0;
  468. boolean shouldUseDate = this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerDate);
  469. if (shouldUseDate){
  470. this.lineGraph.resetXLimits();
  471. this.lineGraph.setxAxisStep(4);
  472. this.lineGraph.setConverter(new LineGraph.AxisDataConverter() {
  473. @Override
  474. public String convertDataForX_Position(double x) {
  475. return StatisticsFragment.this.getDateAsDayString((long)x);
  476. }
  477. @Override
  478. public String convertDataForY_Position(double y){
  479. return "" + (long)y;
  480. }
  481. });
  482. } else {
  483. this.lineGraph.setxAxisStep(12.f);
  484. this.lineGraph.setRangeX(0 , 24);
  485. this.lineGraph.setConverter(null);
  486. }
  487. int count = 0;
  488. for (PlotComparisonItem lineItem : this.currentData){
  489. ArrayList<PlotComparisonItem> data = lineItem.getOtherData();
  490. int index = 0;
  491. Line l = new Line();
  492. int lineColor = lineItem.getColor();
  493. l.setColor(lineColor);
  494. for (PlotComparisonItem pointItem : data){
  495. LinePoint p = new LinePoint();
  496. p.setX(pointItem.getValue1());
  497. Double value2 = pointItem.getValue2();
  498. p.setY(value2);
  499. p.setColor(lineColor);
  500. l.addPoint(p);
  501. rangeMax = Math.max(pointItem.getValue2(), rangeMax);
  502. if (count != 0){
  503. rangeMin = Math.min(pointItem.getValue2(), rangeMin);
  504. } else {
  505. rangeMin = pointItem.getValue2();
  506. }
  507. index++;
  508. count++;
  509. }
  510. this.lineGraph.addLine(l);
  511. }
  512. // add a bit more space
  513. rangeMax++;
  514. rangeMin--;
  515. this.lineGraph.setRangeY(rangeMin, rangeMax);
  516. this.lineGraph.setLineToFill(0);
  517. this.lineGraph.invalidate();
  518. }
  519. public void setBarGraphData(BarGraph bargraph){
  520. this.currentData = this.getBarData();
  521. if (this.currentData == null){
  522. this.currentData = new ArrayList<PlotComparisonItem>();
  523. }
  524. ArrayList<Bar> bars = new ArrayList<Bar>();
  525. for (PlotComparisonItem item : this.currentData){
  526. Bar d = new Bar();
  527. d.setColor(item.getColor());
  528. Long value2 = item.getValue2().longValue();
  529. d.setName("" + value2);
  530. d.setValue(value2.floatValue());
  531. bars.add(d);
  532. }
  533. this.barGraph.setBars(bars);
  534. this.barGraph.invalidate();
  535. }
  536. /*
  537. *
  538. * FETCH & ACTUALISE RECORD DATA
  539. *
  540. * */
  541. public ArrayList<Record> getFetchedRecords(){
  542. if (this.filter == null) this.clearFilter();
  543. return this.dbh.getRecordsForFilter(this.filter);
  544. }
  545. public void actualiseCurrentPlot(){
  546. LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  547. View plot = this.currentPlotView;
  548. if (plot == null){
  549. this.currentPlotView = this.getPieGraphView();
  550. plot = this.currentPlotView;
  551. }
  552. if (plot.getVisibility() == View.INVISIBLE) plot.setVisibility(View.VISIBLE);
  553. if (plot instanceof PieGraph){
  554. PieGraph pie = (PieGraph) plot;
  555. this.setPieGraphData(pie);
  556. } else {
  557. if (this.pieGraph != null){
  558. this.pieGraph.setVisibility(View.INVISIBLE);
  559. if (this.pieGraph.getParent() != null){
  560. plotLayout.removeView(this.pieGraph);
  561. }
  562. }
  563. }
  564. if (plot instanceof BarGraph){
  565. BarGraph bar = (BarGraph) plot;
  566. this.setBarGraphData(bar);
  567. } else {
  568. if (this.barGraph != null){
  569. this.barGraph.setVisibility(View.INVISIBLE);
  570. if (this.barGraph.getParent() != null){
  571. plotLayout.removeView(this.barGraph);
  572. }
  573. }
  574. }
  575. if (plot instanceof LineGraph){
  576. LineGraph line = (LineGraph)plot;
  577. this.setLineGraphData(line);
  578. }else {
  579. if (this.lineGraph != null){
  580. this.lineGraph.setVisibility(View.INVISIBLE);
  581. if (this.lineGraph.getParent() != null){
  582. plotLayout.removeView(this.lineGraph);
  583. }
  584. }
  585. }
  586. plot.setVisibility(View.VISIBLE);
  587. if (plot.getParent() == null){
  588. plotLayout.addView(plot);
  589. }
  590. this.actualiseLegendList();
  591. this.currentPlotView.bringToFront();
  592. this.currentPlotView.invalidate();
  593. }
  594. public ArrayList<PlotComparisonItem> getPieData(){
  595. if (this.selectedCompareData.equals(COMPARE_TITLE_UsesPerProtocol)){
  596. return this.usesPerProtocol();
  597. }
  598. // DEFAULT
  599. return this.attacksPerProtocols();
  600. }
  601. public ArrayList<PlotComparisonItem> getBarData(){
  602. String protocol = this.getCurrentSelectedProtocol();
  603. if (protocol.length() > 0){
  604. if (this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerBSSID)){
  605. return this.attacksPerBSSID(protocol);
  606. }
  607. // DEFAULT
  608. return this.attacksPerESSID(protocol);
  609. }
  610. // Nothing available
  611. return new ArrayList<PlotComparisonItem>();
  612. }
  613. public ArrayList<PlotComparisonItem> getLineData(){
  614. return this.attacksPerTime();
  615. }
  616. /*
  617. * DATA SOURCE
  618. * */
  619. /*PROTOCOLS OVERVIEW*/
  620. public ArrayList<PlotComparisonItem> attacksPerProtocols(){
  621. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  622. int index = 0;
  623. for (String title : this.getSelectedProtocolTitles()){
  624. int attacksCount = this.dbh.getAttackPerProtocolCount(title);
  625. if (attacksCount == 0) continue;
  626. PlotComparisonItem item = new PlotComparisonItem(title,this.getColor(index), 0., (double) attacksCount);
  627. plotItems.add(item);
  628. index++;
  629. }
  630. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  631. @Override
  632. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  633. return s1.getValue2().compareTo(s2.getValue2());
  634. }
  635. });
  636. return this.resizeData(plotItems);
  637. }
  638. public ArrayList<PlotComparisonItem> usesPerProtocol(){
  639. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  640. for (String title : this.getSelectedProtocolTitles()){
  641. /*At the moment there is no possibillity to get the number of uses for each protocol*/
  642. // TODO GET USES PER PROTOCOL
  643. int uses = 0;
  644. if (uses == 0) continue;
  645. // ADD ITEMS
  646. }
  647. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  648. @Override
  649. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  650. return s1.getTitle().compareToIgnoreCase(s2.getTitle());
  651. }
  652. });
  653. return plotItems;
  654. }
  655. /*LINE PLOT DATA*/
  656. public ArrayList<PlotComparisonItem> attacksPerTime(){
  657. HashMap<String,HashMap<Long, ArrayList<Record> > > lineMap = new HashMap<String, HashMap<Long, ArrayList<Record>>>();
  658. boolean shouldUseDate = this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerDate);
  659. for (Record record : this.getFetchedRecords()){
  660. long timestamp = record.getTimestamp();
  661. long time = 0;
  662. if (shouldUseDate){
  663. time = this.getDateFromMilliseconds(timestamp);
  664. } else {
  665. time = this.getDayHourFromDate(timestamp);
  666. }
  667. // GET CORRECT MAP
  668. HashMap<Long, ArrayList<Record> > recordMap;
  669. String groupKey = record.getSsid();
  670. if (lineMap.containsKey(groupKey)){
  671. recordMap = lineMap.get(record.getSsid());
  672. } else {
  673. recordMap = new HashMap<Long, ArrayList<Record> >();
  674. lineMap.put(groupKey, recordMap);
  675. }
  676. // GET LIST OF RECORDS
  677. ArrayList<Record> list;
  678. if (recordMap.containsKey(time)){
  679. list = recordMap.get(time);
  680. } else {
  681. list = new ArrayList<Record>();
  682. recordMap.put(time, list);
  683. }
  684. list.add(record);
  685. }
  686. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  687. int index = 0;
  688. for (String groupKey : lineMap.keySet()){
  689. HashMap<Long, ArrayList<Record> > recordMap = lineMap.get(groupKey);
  690. ArrayList<PlotComparisonItem> singleLineItems = new ArrayList<PlotComparisonItem>();
  691. for (long time : recordMap.keySet()){
  692. ArrayList<Record>list = recordMap.get(time);
  693. if (list.size() == 0) continue;
  694. PlotComparisonItem item = new PlotComparisonItem(this.getHourAsTimeString(time), 0 , (double)time, (double) list.size());
  695. singleLineItems.add(item);
  696. }
  697. Collections.sort(singleLineItems, new Comparator<PlotComparisonItem>() {
  698. @Override
  699. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  700. return s1.getValue1().compareTo(s2.getValue1());
  701. }
  702. });
  703. PlotComparisonItem item = new PlotComparisonItem(groupKey, this.getColor(index), 0., (double) singleLineItems.size());
  704. item.setOtherData(singleLineItems);
  705. plotItems.add(item);
  706. index++;
  707. }
  708. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  709. @Override
  710. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  711. return s2.getValue2().compareTo(s1.getValue2());
  712. }
  713. });
  714. return plotItems;
  715. }
  716. // BAR PLOT DATA
  717. public ArrayList<PlotComparisonItem> attacksPerBSSID(String protocol){
  718. LogFilter filter = new LogFilter();
  719. ArrayList<String> protocollist = new ArrayList<String>();
  720. protocollist.add(protocol);
  721. filter.setProtocols(protocollist);
  722. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  723. HashMap<String, Integer> recordMap = new HashMap<String, Integer>();
  724. ArrayList<Record> records = this.dbh.getRecordsForFilter(filter);
  725. for (Record record : records){
  726. int count = 0;
  727. if (recordMap.containsKey(record.getBssid())){
  728. count = recordMap.get(record.getBssid());
  729. }
  730. count++;
  731. recordMap.put(record.getBssid(), count);
  732. }
  733. int index = 0;
  734. for (String key : recordMap.keySet()){
  735. double value = (double)recordMap.get(key);
  736. if (value == 0.) continue;
  737. PlotComparisonItem item = new PlotComparisonItem(key, this.getColor(index), 0., value);
  738. plotItems.add(item);
  739. index++;
  740. }
  741. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  742. @Override
  743. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  744. return s1.getValue2().compareTo(s2.getValue2());
  745. }
  746. });
  747. return this.resizeData(plotItems);
  748. }
  749. public ArrayList<PlotComparisonItem> attacksPerESSID(String protocol){
  750. LogFilter filter = new LogFilter();
  751. ArrayList<String> protocollist = new ArrayList<String>();
  752. protocollist.add(protocol);
  753. filter.setProtocols(protocollist);
  754. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  755. HashMap<String, Integer> recordMap = new HashMap<String, Integer>();
  756. ArrayList<Record> records = this.dbh.getRecordsForFilter(filter);
  757. for (Record record : records){
  758. int count = 0;
  759. if (recordMap.containsKey(record.getSsid())){
  760. count = recordMap.get(record.getSsid());
  761. }
  762. count++;
  763. recordMap.put(record.getSsid(), count);
  764. }
  765. int index = 0;
  766. for (String key : recordMap.keySet()){
  767. double value = (double)recordMap.get(key);
  768. if (value == 0.) continue;
  769. PlotComparisonItem item = new PlotComparisonItem(key,this.getColor(index), 0. ,value);
  770. plotItems.add(item);
  771. index++;
  772. }
  773. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  774. @Override
  775. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  776. return s1.getValue2().compareTo(s2.getValue2());
  777. }
  778. });
  779. return plotItems;// this.resizeData(plotItems);
  780. }
  781. private ArrayList<PlotComparisonItem> resizeData(ArrayList<PlotComparisonItem> plotItems){
  782. if (plotItems != null){
  783. if (plotItems.size() > MAX_NUMBER_OF_CHART_OBJECTS && MAX_NUMBER_OF_CHART_OBJECTS > 1){
  784. ArrayList<PlotComparisonItem> copy = new ArrayList<PlotComparisonItem>();
  785. ArrayList<PlotComparisonItem> others = new ArrayList<PlotComparisonItem>();
  786. double valueOfOthers = 0;
  787. for (int i = 0; i < plotItems.size(); i++){
  788. if (i < MAX_NUMBER_OF_CHART_OBJECTS - 1){
  789. PlotComparisonItem item = plotItems.get(i);
  790. item.setColor(this.getColor(i));
  791. copy.add(plotItems.get(i));
  792. } else {
  793. PlotComparisonItem item = plotItems.get(i);
  794. others.add(item);
  795. valueOfOthers+=item.getValue2();
  796. }
  797. }
  798. PlotComparisonItem otherItem = new PlotComparisonItem(OTHER_CHART_TITLE, this.getOtherColor(), 0., valueOfOthers);
  799. otherItem.setOtherData(others);
  800. copy.add(otherItem);
  801. return copy;
  802. }
  803. }
  804. return plotItems;
  805. }
  806. /*
  807. * FILTER STUFF
  808. * */
  809. private String getCurrentSelectedProtocol(){
  810. ArrayList<String> protocolTitles = this.getSelectedProtocolTitles();
  811. if (protocolTitles != null && protocolTitles.size() != 0){
  812. return this.getSelectedProtocolTitles().get(0);
  813. }
  814. return "";
  815. }
  816. public ArrayList<String> protocolTitles(){
  817. ArrayList<String> titles = new ArrayList<String>();
  818. for (String protocol : this.getResources().getStringArray(
  819. R.array.protocols)) {
  820. titles.add(protocol);
  821. }
  822. return titles;
  823. }
  824. public boolean[] selectedProtocols(){
  825. ArrayList<String> protocols = this.protocolTitles();
  826. boolean[] selected = new boolean[protocols.size()];
  827. int i = 0;
  828. for(String protocol : protocols){
  829. selected[i] =(this.filter.protocols.contains(protocol));
  830. i++;
  831. }
  832. return selected;
  833. }
  834. public ArrayList<String> getSelectedProtocolTitles(){
  835. ArrayList<String> knownProtocols = this.protocolTitles();
  836. if (this.filter.hasProtocols()){
  837. ArrayList<String> titles = new ArrayList<String>();
  838. int i =0;
  839. for (boolean b : this.selectedProtocols()){
  840. if (b){
  841. String title = knownProtocols.get(i);
  842. titles.add(title);
  843. }
  844. i++;
  845. }
  846. return titles;
  847. }
  848. return this.protocolTitles();
  849. }
  850. /*
  851. *
  852. * COLOR STUFF
  853. *
  854. * */
  855. public int getOtherColor(){
  856. return Color.argb(255, 80, 80, 80); // grey
  857. }
  858. public Integer getColor(int index) {
  859. return ColorSequenceGenerator.getColorForIndex(index);
  860. }
  861. public LinearLayout getPlotLayout(){
  862. if (this.rootView != null){
  863. return (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  864. } else {
  865. return null;
  866. }
  867. }
  868. /**
  869. *
  870. * FILTER STUFF
  871. *
  872. * */
  873. private boolean isFilterSetForTitle(String title){
  874. if (title.equals(FILTER_MENU_TITLE_BSSID)){
  875. return this.filter.hasBSSIDs();
  876. }
  877. if (title.equals(FILTER_MENU_TITLE_ESSID)){
  878. return this.filter.hasESSIDs();
  879. }
  880. if (title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
  881. return this.filter.hasProtocols();
  882. }
  883. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)){
  884. return this.filter.hasBelowTimestamp();
  885. }
  886. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)){
  887. return this.filter.hasAboveTimestamp();
  888. }
  889. return false;
  890. }
  891. private void clearFilter(){
  892. if(filter == null) this.filter = new LogFilter();
  893. this.filter.clear();
  894. }
  895. /*
  896. *
  897. * DATE TRANSFORMATION
  898. *
  899. */
  900. public long getDayHourFromDate(long timeInMillis){
  901. Calendar calendar = Calendar.getInstance();
  902. calendar.setTimeInMillis (timeInMillis);
  903. int hour = calendar.get(Calendar.HOUR_OF_DAY);
  904. int min = calendar.get(Calendar.MINUTE);
  905. return hour;
  906. }
  907. public long getDateFromMilliseconds(long timeInMillis){
  908. long millisInDay = 60 * 60 * 24 * 1000;
  909. return (timeInMillis / millisInDay) * millisInDay;
  910. }
  911. /*
  912. *
  913. * */
  914. private String getHourAsTimeString(long hour) {
  915. return "" + hour + ":00";
  916. }
  917. static final DateFormat dateFormat = new SimpleDateFormat("d.M.yyyy");
  918. @SuppressLint("SimpleDateFormat")
  919. private String getDateAsDayString(long timeStamp) {
  920. try {
  921. Date netDate = (new Date(timeStamp));
  922. return dateFormat.format(netDate);
  923. } catch (Exception ex) {
  924. return "xx";
  925. }
  926. }
  927. /**
  928. * USERINTERACTION
  929. */
  930. private void userTappedOnItem(int index){
  931. if (index < this.currentData.size()){
  932. PlotComparisonItem item = this.currentData.get(index);
  933. ArrayList<String> selectedData;
  934. selectedData = new ArrayList<String>();
  935. if (item.getOtherData() == null){
  936. selectedData.add(item.getTitle());
  937. } else {
  938. for (PlotComparisonItem other : item.getOtherData()){
  939. selectedData.add(other.getTitle());
  940. }
  941. }
  942. LogFilter filter = new LogFilter();
  943. if (this.currentPlotView instanceof PieGraph){
  944. filter.setProtocols(selectedData);
  945. }
  946. if (this.currentPlotView instanceof BarGraph){
  947. // TODO set data for BSSID / ESSID
  948. }
  949. if (this.currentPlotView instanceof LineGraph){
  950. // TODO set data for BSSID / ESSID
  951. }
  952. this.pushRecordOverviewForFilter(filter);
  953. }
  954. }
  955. private void pushRecordOverviewForFilter(LogFilter filter){
  956. }
  957. }