StatisticsFragment.java 35 KB

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