StatisticsFragment.java 38 KB

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