StatisticsFragment.java 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  1. package de.tudarmstadt.informatik.hostage.ui2.fragment;
  2. import android.annotation.SuppressLint;
  3. import android.app.Fragment;
  4. import android.app.FragmentManager;
  5. import android.app.FragmentTransaction;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.graphics.Color;
  9. import android.os.Bundle;
  10. import android.view.LayoutInflater;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.widget.AdapterView;
  14. import android.widget.ImageButton;
  15. import android.widget.LinearLayout;
  16. import android.widget.ListView;
  17. import android.widget.TextView;
  18. import com.echo.holographlibrary.Bar;
  19. import com.echo.holographlibrary.BarGraph;
  20. import com.echo.holographlibrary.Line;
  21. import com.echo.holographlibrary.LineGraph;
  22. import com.echo.holographlibrary.LinePoint;
  23. import com.echo.holographlibrary.PieGraph;
  24. import com.echo.holographlibrary.PieSlice;
  25. import java.text.DateFormat;
  26. import java.text.SimpleDateFormat;
  27. import java.util.ArrayList;
  28. import java.util.Calendar;
  29. import java.util.Collections;
  30. import java.util.Comparator;
  31. import java.util.Date;
  32. import java.util.HashMap;
  33. import de.tudarmstadt.informatik.hostage.R;
  34. import de.tudarmstadt.informatik.hostage.logging.Record;
  35. import de.tudarmstadt.informatik.hostage.logging.UglyDbHelper;
  36. import de.tudarmstadt.informatik.hostage.ui.LogFilter;
  37. import de.tudarmstadt.informatik.hostage.ui2.adapter.StatisticListAdapter;
  38. import de.tudarmstadt.informatik.hostage.ui2.dialog.ChecklistDialog;
  39. import de.tudarmstadt.informatik.hostage.ui2.dialog.DateTimeDialogFragment;
  40. import de.tudarmstadt.informatik.hostage.ui2.helper.ColorSequenceGenerator;
  41. import de.tudarmstadt.informatik.hostage.ui2.model.PlotComparisonItem;
  42. import de.tudarmstadt.informatik.hostage.ui2.popup.AbstractPopup;
  43. import de.tudarmstadt.informatik.hostage.ui2.popup.AbstractPopupItem;
  44. import de.tudarmstadt.informatik.hostage.ui2.popup.SimplePopupItem;
  45. import de.tudarmstadt.informatik.hostage.ui2.popup.SimplePopupTable;
  46. import de.tudarmstadt.informatik.hostage.ui2.popup.SplitPopupItem;
  47. /**
  48. * Created by Julien on 16.02.14.
  49. */
  50. public class StatisticsFragment extends Fragment implements ChecklistDialog.ChecklistDialogListener, DateTimeDialogFragment.DateTimeDialogFragmentListener {
  51. static final String FILTER_MENU_TITLE_BSSID = "BSSID";
  52. static final String FILTER_MENU_TITLE_ESSID = "ESSID";
  53. static final String FILTER_MENU_TITLE_PROTOCOLS = "Protocols";
  54. static final String FILTER_MENU_TITLE_PROTOCOL = "Protocol";
  55. static final String FILTER_MENU_TITLE_TIMESTAMP_BELOW = "Latest";
  56. static final String FILTER_MENU_TITLE_TIMESTAMP_ABOVE = "Earliest";
  57. static final String FILTER_MENU_TITLE_REMOVE = "Reset Filter";
  58. static final String FILTER_MENU_POPUP_TITLE = "Filter by";
  59. static final String MENU_TITLE_PROTOCOLS = "Protocols";
  60. static final String MENU_TITLE_NETWORK = "Networks";
  61. static final String MENU_TITLE_ATTACKS = "Attacks";
  62. static final String MENU_POPUP_TITLE = "Visualize";
  63. static final String CHART_TYPE_TITLE_BAR = "Bar Plot";
  64. static final String CHART_TYPE_TITLE_PIE = "Pie Plot";
  65. static final String CHART_TYPE_TITLE_LINE = "Line Plot";
  66. static final String DIALOG_PROTOCOLS_TITLE = "Select protocol data to compare";
  67. static final String DIALOG_NETWORK_TITLE = "Select network data to compare";
  68. static final String DIALOG_ATTACK_TITLE = "Select attack data to compare";
  69. static final String COMPARE_TITLE_AttacksPerProtocol = "Attacks per protocol";
  70. static final String COMPARE_TITLE_UsesPerProtocol = "Uses per protocol";
  71. static final String COMPARE_TITLE_AttacksPerDate = "Attacks per date";
  72. static final String COMPARE_TITLE_AttacksPerTime = "Attacks per time";
  73. static final String COMPARE_TITLE_AttacksPerBSSID = "Attacks per BSSID";
  74. static final String COMPARE_TITLE_AttacksPerESSID = "Attacks per ESSID";
  75. static final String FILTER_MENU_PROTOCOL_SINGLE_CHOICE_TITLE = "Select Protocol";
  76. static final String OTHER_CHART_TITLE = "Other";
  77. // MINIMAL 2
  78. static int MAX_NUMBER_OF_CHART_OBJECTS = 6;
  79. private boolean wasBelowTimePicker;
  80. private LogFilter filter;
  81. private boolean showFilterButton;
  82. private PieGraph pieGraph;
  83. private LineGraph lineGraph;
  84. private BarGraph barGraph;
  85. private View rootView;
  86. private View currentPlotView;
  87. private ArrayList<Integer> colorList;
  88. private ArrayList<PlotComparisonItem> currentData;
  89. private UglyDbHelper dbh;
  90. private ListView legendListView;
  91. private String selectedCompareData = COMPARE_TITLE_AttacksPerProtocol;
  92. public enum ChartType {
  93. PIE_CHART(0),
  94. BAR_CHART(1),
  95. LINE_CHART(2);
  96. private int value;
  97. private ChartType(int value) {
  98. this.value = value;
  99. }
  100. static public ChartType create(int value){
  101. if (value < 0 || value >= ChartType.values().length) return ChartType.PIE_CHART;
  102. return ChartType.values()[value];
  103. }
  104. public String toString(){
  105. if (this.equals(ChartType.create(0))){
  106. return CHART_TYPE_TITLE_PIE;
  107. }
  108. if (this.equals(ChartType.create(1))){
  109. return CHART_TYPE_TITLE_BAR;
  110. }
  111. return CHART_TYPE_TITLE_LINE;
  112. }
  113. }
  114. @Override
  115. public void onCreate(Bundle savedInstanceState) {
  116. super.onCreate(savedInstanceState);
  117. setHasOptionsMenu(true);
  118. }
  119. private ImageButton getFilterButton(){
  120. return (ImageButton) this.rootView.findViewById(R.id.FilterButton);
  121. }
  122. public int getLayoutID(){
  123. return R.layout.fragment_statistics;
  124. }
  125. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  126. Bundle savedInstanceState) {
  127. super.onCreateView(inflater, container, savedInstanceState);
  128. getActivity().setTitle(getResources().getString(R.string.drawer_statistics));
  129. dbh = new UglyDbHelper(this.getBaseContext());
  130. // Get the message from the intent
  131. if (this.filter == null){
  132. Intent intent = this.getActivity().getIntent();
  133. LogFilter filter = intent.getParcelableExtra(LogFilter.LOG_FILTER_INTENT_KEY);
  134. if(filter == null){
  135. this.clearFilter();
  136. } else {
  137. this.filter = filter;
  138. }
  139. }
  140. View rootView = inflater.inflate(this.getLayoutID(), container, false);
  141. LinearLayout plotLayout = (LinearLayout) rootView.findViewById(R.id.plot_layout);
  142. plotLayout.setWillNotDraw(false);
  143. container.setWillNotDraw(false);
  144. this.legendListView = (ListView) rootView.findViewById(R.id.legend_list_view);
  145. this.legendListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  146. @Override
  147. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  148. StatisticsFragment.this.userTappedOnLegendItem(i);
  149. }
  150. });
  151. this.rootView = rootView;
  152. rootView.setWillNotDraw(false);
  153. this.configureRootView(rootView);
  154. return rootView;
  155. }
  156. private Context getBaseContext(){
  157. return this.getActivity().getBaseContext();
  158. }
  159. private Context getApplicationContext(){
  160. return this.getActivity().getApplicationContext();
  161. }
  162. public void configureRootView(View rootView){
  163. LinearLayout plotLayout = (LinearLayout) rootView.findViewById(R.id.plot_layout);
  164. plotLayout.setWillNotDraw(false);
  165. plotLayout.removeAllViews();
  166. this.actualiseCurrentPlot();
  167. ImageButton visualButton = (ImageButton) rootView.findViewById(R.id.plot_data_button);
  168. visualButton.setOnClickListener(new View.OnClickListener() {
  169. public void onClick(View v) {
  170. StatisticsFragment.this.openBarSelectionMenuOnView(v);
  171. }
  172. });
  173. ImageButton filterButton = this.getFilterButton();
  174. filterButton.setOnClickListener(new View.OnClickListener() {
  175. public void onClick(View v) {
  176. StatisticsFragment.this.openFilterMenuOnView(v);
  177. }
  178. });
  179. if (this.currentPlotView instanceof BarGraph){
  180. this.setTitle("" + this.getCurrentSelectedProtocol() + ": " +this.selectedCompareData);
  181. } else {
  182. this.setTitle(this.selectedCompareData);
  183. }
  184. }
  185. public void setTitle(String title){
  186. TextView titleView = (TextView) this.rootView.findViewById(R.id.title_text_view);
  187. if (title != null && titleView != null){
  188. titleView.setText(title);
  189. titleView.invalidate();
  190. }
  191. }
  192. public String getTitle(){
  193. TextView titleView = (TextView) this.rootView.findViewById(R.id.title_text_view);
  194. if (titleView != null){
  195. return "" + titleView.getText();
  196. }
  197. return "";
  198. }
  199. public void onStart() {
  200. super.onStart();
  201. this.actualiseCurrentPlot();
  202. this.currentPlotView.invalidate();
  203. if (this.currentPlotView instanceof BarGraph){
  204. this.setTitle("" + this.getCurrentSelectedProtocol() + ": " +this.selectedCompareData);
  205. } else {
  206. this.setTitle(this.selectedCompareData);
  207. }
  208. }
  209. public void setChartType(ChartType type){
  210. boolean shouldChange = true;
  211. this.clearFilter();
  212. if (this.currentPlotView != null){
  213. if (type == ChartType.PIE_CHART){
  214. shouldChange = ! (this.currentPlotView instanceof PieGraph);
  215. // SET FILTER BUTTON HIDDEN
  216. ImageButton filterButton = this.getFilterButton();
  217. if (filterButton != null) filterButton.setVisibility(View.GONE);
  218. } else {
  219. if (this.pieGraph != null)
  220. this.pieGraph.setVisibility(View.GONE);
  221. // SHOW FILTER BUTTON
  222. ImageButton filterButton = this.getFilterButton();
  223. if (filterButton != null) filterButton.setVisibility(View.VISIBLE);
  224. }
  225. if (type == ChartType.LINE_CHART){
  226. shouldChange = ! (this.currentPlotView instanceof LineGraph);
  227. } else {
  228. if (this.lineGraph != null)
  229. this.lineGraph.setVisibility(View.GONE);
  230. }
  231. if (type == ChartType.BAR_CHART){
  232. shouldChange = ! (this.currentPlotView instanceof BarGraph);
  233. } else {
  234. if (this.barGraph != null)
  235. this.barGraph.setVisibility(View.GONE);
  236. }
  237. }
  238. if (shouldChange){
  239. this.currentPlotView = this.getPlotViewForType(type);
  240. this.currentPlotView.setVisibility(View.VISIBLE);
  241. }
  242. this.actualiseCurrentPlot();
  243. }
  244. public View getPlotViewForType(ChartType type){
  245. switch (type){
  246. case PIE_CHART:
  247. return this.getPieGraphView();
  248. case LINE_CHART:
  249. return this.getLineGraphView();
  250. default:
  251. return this.getBarGraphView();
  252. }
  253. }
  254. public void actualiseLegendList(){
  255. StatisticListAdapter adapter = new StatisticListAdapter(this.getApplicationContext(), this.currentData);
  256. this.legendListView.setAdapter(adapter);
  257. }
  258. /*
  259. * MENU
  260. * */
  261. private void openBarSelectionMenuOnView(View anchorView){
  262. SimplePopupTable visualiseMenu = new SimplePopupTable(this.getActivity(), new AbstractPopup.OnPopupItemClickListener() {
  263. public void onItemClick(Object ob) {
  264. if (ob instanceof AbstractPopupItem){
  265. AbstractPopupItem item = (AbstractPopupItem) ob;
  266. StatisticsFragment.this.userSelectMenuItem(item);
  267. }
  268. }
  269. });
  270. visualiseMenu.setTitle(MENU_POPUP_TITLE);
  271. int id = 0;
  272. for(String title : StatisticsFragment.this.getMenuTitles()){
  273. SimplePopupItem item = new SimplePopupItem(this.getActivity());
  274. item.setTitle(title);
  275. item.setItemId(id);
  276. item.setSelected(false);
  277. visualiseMenu.addItem(item);
  278. id++;
  279. }
  280. visualiseMenu.showOnView(anchorView);
  281. }
  282. private void userSelectMenuItem(AbstractPopupItem item){
  283. // OPEN A DIALOG TO SPECIFY THE VISUALISE DATA
  284. if (item.getTitle().equals(MENU_TITLE_PROTOCOLS)){
  285. ChartType chartType = ChartType.PIE_CHART;
  286. this.selectedCompareData = COMPARE_TITLE_AttacksPerProtocol;
  287. this.setChartType(chartType);
  288. this.setTitle(COMPARE_TITLE_AttacksPerProtocol);
  289. }
  290. if (item.getTitle().equals(MENU_TITLE_NETWORK)){
  291. this.openNetworkDataDialog();
  292. }
  293. if (item.getTitle().equals(MENU_TITLE_ATTACKS)){
  294. this.openAttackDataDialog();
  295. }
  296. }
  297. private ArrayList<String> getMenuTitles(){
  298. ArrayList<String> titles = new ArrayList<String>();
  299. titles.add(MENU_TITLE_PROTOCOLS);
  300. titles.add(MENU_TITLE_NETWORK);
  301. titles.add(MENU_TITLE_ATTACKS);
  302. return titles;
  303. }
  304. /*
  305. * PLOT DATA DIALOGS
  306. * */
  307. private void openProtocolDataDialog(){
  308. ArrayList<String> titles = this.getDialogProtocolDataTitle();
  309. ChecklistDialog newFragment = new ChecklistDialog(DIALOG_PROTOCOLS_TITLE, titles, this.selectedData(titles), false , this);
  310. newFragment.show(this.getActivity().getFragmentManager(), DIALOG_PROTOCOLS_TITLE);
  311. }
  312. private void openNetworkDataDialog(){
  313. ArrayList<String> titles = this.getDialogNetworkDataTitle();
  314. ChecklistDialog newFragment = new ChecklistDialog(DIALOG_NETWORK_TITLE, titles, this.selectedData(titles), false , this);
  315. newFragment.show(this.getActivity().getFragmentManager(), DIALOG_NETWORK_TITLE);
  316. }
  317. private void openAttackDataDialog(){
  318. ArrayList<String> titles = this.getDialogAttackDataTitle();
  319. ChecklistDialog newFragment = new ChecklistDialog(DIALOG_ATTACK_TITLE, titles, this.selectedData(titles), false , this);
  320. newFragment.show(this.getActivity().getFragmentManager(), DIALOG_ATTACK_TITLE);
  321. }
  322. /*
  323. *
  324. * DIALOG ACTION METHODS
  325. *
  326. * */
  327. public void onDialogPositiveClick(ChecklistDialog dialog) {
  328. String title = dialog.getTitle();
  329. ArrayList<String> titles =dialog.getSelectedItemTitles();
  330. if (title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
  331. this.filter.setProtocols(titles.size() == 0 ? this.protocolTitles() : titles);
  332. this.actualiseCurrentPlot();
  333. return;
  334. }
  335. if (title.equals(FILTER_MENU_PROTOCOL_SINGLE_CHOICE_TITLE)){
  336. this.filter.setProtocols(titles);
  337. this.actualiseCurrentPlot();
  338. String fragTitle = "" + this.getCurrentSelectedProtocol() + ": " + this.selectedCompareData;
  339. this.setTitle(fragTitle);
  340. return;
  341. }
  342. if (title.equals(FILTER_MENU_TITLE_ESSID)){
  343. this.filter.setESSIDs(titles);
  344. this.actualiseCurrentPlot();
  345. return;
  346. }
  347. if (title.equals(FILTER_MENU_TITLE_BSSID)){
  348. this.filter.setBSSIDs(titles);
  349. this.actualiseCurrentPlot();
  350. return;
  351. }
  352. if (titles.size() != 0){
  353. String data = titles.get(0);
  354. this.setTitle(data);
  355. if (title.equals(DIALOG_PROTOCOLS_TITLE)){
  356. ChartType chartType = ChartType.PIE_CHART;
  357. this.selectedCompareData = data;
  358. this.setChartType(chartType);
  359. }
  360. if (title.equals(DIALOG_ATTACK_TITLE)){
  361. ChartType chartType = ChartType.LINE_CHART;
  362. this.selectedCompareData = data;
  363. this.setChartType(chartType);
  364. }
  365. if (title.equals(DIALOG_NETWORK_TITLE)){
  366. ChartType chartType = ChartType.BAR_CHART;
  367. this.selectedCompareData = data;
  368. this.setChartType(chartType);
  369. String fragTitle = "" + this.getCurrentSelectedProtocol() + ": " + this.selectedCompareData;
  370. this.setTitle(fragTitle);
  371. }
  372. }
  373. }
  374. public void onDialogNegativeClick(ChecklistDialog dialog) {
  375. }
  376. /*
  377. *
  378. * DIALOG DATA
  379. *
  380. * */
  381. private ArrayList<String> getDialogProtocolDataTitle(){
  382. ArrayList<String> data = new ArrayList<String>();
  383. data.add(COMPARE_TITLE_AttacksPerProtocol);
  384. data.add(COMPARE_TITLE_UsesPerProtocol);
  385. return data;
  386. }
  387. private ArrayList<String> getDialogAttackDataTitle(){
  388. ArrayList<String> data = new ArrayList<String>();
  389. data.add(COMPARE_TITLE_AttacksPerDate);
  390. data.add(COMPARE_TITLE_AttacksPerTime);
  391. return data;
  392. }
  393. private ArrayList<String> getDialogNetworkDataTitle(){
  394. ArrayList<String> data = new ArrayList<String>();
  395. data.add(COMPARE_TITLE_AttacksPerESSID);
  396. data.add(COMPARE_TITLE_AttacksPerBSSID);
  397. return data;
  398. }
  399. private boolean[] selectedData(ArrayList<String> data){
  400. boolean[] selected = new boolean[data.size()];
  401. // SET DEFAULT
  402. selected[0] = true;
  403. return selected;
  404. }
  405. /*
  406. *
  407. * FILTER BUTTON
  408. *
  409. * */
  410. private void openFilterMenuOnView(View anchor){
  411. SimplePopupTable filterMenu = new SimplePopupTable(this.getActivity(), new AbstractPopup.OnPopupItemClickListener() {
  412. public void onItemClick(Object ob) {
  413. if (ob instanceof AbstractPopupItem){
  414. AbstractPopupItem item = (AbstractPopupItem) ob;
  415. StatisticsFragment.this.onFilterMenuItemSelected(item);
  416. }
  417. }
  418. });
  419. filterMenu.setTitle(FILTER_MENU_POPUP_TITLE);
  420. for(String title : StatisticsFragment.this.filterMenuTitles()){
  421. AbstractPopupItem item = null;
  422. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)) continue;
  423. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)){
  424. item = new SplitPopupItem(this.getActivity());
  425. item.setValue(SplitPopupItem.RIGHT_TITLE, FILTER_MENU_TITLE_TIMESTAMP_BELOW);
  426. item.setValue(SplitPopupItem.LEFT_TITLE, FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  427. if (this.filter.hasBelowTimestamp()){
  428. item.setValue(SplitPopupItem.RIGHT_SUBTITLE, this.getDateAsString(this.filter.belowTimestamp));
  429. }
  430. if (this.filter.hasAboveTimestamp()){
  431. item.setValue(SplitPopupItem.LEFT_SUBTITLE, this.getDateAsString(this.filter.aboveTimestamp));
  432. }
  433. } else {
  434. item = new SimplePopupItem(this.getActivity());
  435. item.setTitle(title);
  436. ((SimplePopupItem)item).setSelected(this.isFilterSetForTitle(title));
  437. }
  438. filterMenu.addItem(item);
  439. }
  440. if (this.filter.isSet()){
  441. AbstractPopupItem item = new SimplePopupItem(this.getActivity());
  442. item.setTitle(FILTER_MENU_TITLE_REMOVE);
  443. filterMenu.addItem(item);
  444. }
  445. filterMenu.showOnView(anchor);
  446. }
  447. private void onFilterMenuItemSelected(AbstractPopupItem item){
  448. if (item instanceof SplitPopupItem){
  449. SplitPopupItem sItem = (SplitPopupItem) item;
  450. this.wasBelowTimePicker = sItem.wasRightTouch;
  451. if (this.wasBelowTimePicker){
  452. this.openTimestampToFilterDialog();
  453. } else {
  454. this.openTimestampFromFilterDialog();
  455. }
  456. return;
  457. }
  458. String title = item.getTitle();
  459. if (title.equals(FILTER_MENU_TITLE_ESSID)){
  460. this.openESSIDFilterDialog();
  461. }
  462. if (title.equals(FILTER_MENU_TITLE_BSSID)){
  463. this.openBSSIDFilterDialog();
  464. }
  465. if (title.equals(FILTER_MENU_TITLE_PROTOCOL)){
  466. this.openFilterDialogSelectProtocol();
  467. }
  468. if (title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
  469. this.openProtocolsFilterDialog();
  470. }
  471. if (title.equals(FILTER_MENU_TITLE_REMOVE)){
  472. this.clearFilter();
  473. this.actualiseCurrentPlot();
  474. }
  475. }
  476. private ArrayList<String> filterMenuTitles(){
  477. ArrayList<String> titles = new ArrayList<String>();
  478. if (this.currentPlotView instanceof LineGraph){
  479. titles.add(FILTER_MENU_TITLE_ESSID);
  480. titles.add(FILTER_MENU_TITLE_PROTOCOLS);
  481. titles.add(FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  482. if (this.filter.hasESSIDs() || this.filter.hasATimestamp()){
  483. titles.add(FILTER_MENU_TITLE_REMOVE);
  484. }
  485. } else {
  486. titles.add(FILTER_MENU_TITLE_PROTOCOL);
  487. String protocol = this.getCurrentSelectedProtocol();
  488. if (protocol.length() > 0){
  489. if (this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerBSSID)){
  490. titles.add(FILTER_MENU_TITLE_BSSID);
  491. }
  492. // DEFAULT
  493. titles.add(FILTER_MENU_TITLE_ESSID);
  494. }
  495. titles.add(FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  496. if (this.filter.hasProtocols() || this.filter.hasATimestamp() || this.filter.hasESSIDs() || this.filter.hasBSSIDs()){
  497. titles.add(FILTER_MENU_TITLE_REMOVE);
  498. }
  499. }
  500. return titles;
  501. }
  502. private void openProtocolsFilterDialog(){
  503. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_PROTOCOLS,this.protocolTitles(), this.selectedProtocols(), true , this);
  504. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_PROTOCOLS);
  505. }
  506. private void openFilterDialogSelectProtocol(){
  507. ArrayList<String> titles = this.protocolTitles();
  508. boolean[] selected = new boolean[titles.size()];
  509. int i = 0;
  510. for (String title : titles){
  511. selected[i] = title.equals(this.getCurrentSelectedProtocol());
  512. i++;
  513. }
  514. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_PROTOCOL_SINGLE_CHOICE_TITLE, titles, selected, false , this);
  515. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_PROTOCOL_SINGLE_CHOICE_TITLE);
  516. }
  517. private void openESSIDFilterDialog(){
  518. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_ESSID, this.essids(), this.selectedESSIDs(), true , this);
  519. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_ESSID);
  520. }
  521. private void openBSSIDFilterDialog(){
  522. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_BSSID, this.bssids(), this.selectedBSSIDs(), true , this);
  523. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_BSSID);
  524. }
  525. private void openTimestampFromFilterDialog(){
  526. this.wasBelowTimePicker = false;
  527. DateTimeDialogFragment newFragment = new DateTimeDialogFragment(this.getActivity());
  528. newFragment.setDateChangeListener(this);
  529. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  530. if (this.filter.aboveTimestamp != Long.MIN_VALUE)newFragment.setDate(this.filter.aboveTimestamp);
  531. }
  532. private void openTimestampToFilterDialog(){
  533. this.wasBelowTimePicker = true;
  534. DateTimeDialogFragment newFragment = new DateTimeDialogFragment(this.getActivity());
  535. newFragment.setDateChangeListener(this);
  536. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_TIMESTAMP_BELOW);
  537. if (this.filter.belowTimestamp != Long.MAX_VALUE) newFragment.setDate(this.filter.belowTimestamp);
  538. }
  539. public ArrayList<String> essids(){
  540. ArrayList<String> records;
  541. if (this.currentPlotView instanceof BarGraph){
  542. records = dbh.getUniqueESSIDRecordsForProtocol(this.getCurrentSelectedProtocol());
  543. } else {
  544. records = dbh.getUniqueESSIDRecords();
  545. }
  546. return records;
  547. }
  548. public boolean[] selectedESSIDs(){
  549. ArrayList<String> essids = this.essids();
  550. boolean[] selected = new boolean[essids.size()];
  551. int i = 0;
  552. for(String essid : essids){
  553. selected[i] =(this.filter.getESSIDs().contains(essid));
  554. i++;
  555. }
  556. return selected;
  557. }
  558. public ArrayList<String> bssids(){
  559. ArrayList<String> records ;
  560. if (this.currentPlotView instanceof BarGraph){
  561. records = dbh.getUniqueBSSIDRecordsForProtocol(this.getCurrentSelectedProtocol());
  562. } else {
  563. records = dbh.getUniqueBSSIDRecords();
  564. }
  565. return records;
  566. }
  567. public boolean[] selectedBSSIDs(){
  568. ArrayList<String> bssids = this.bssids();
  569. boolean[] selected = new boolean[bssids.size()];
  570. int i = 0;
  571. for(String bssid : bssids){
  572. selected[i] =(this.filter.getBSSIDs().contains(bssid));
  573. i++;
  574. }
  575. return selected;
  576. }
  577. public void onDateTimePickerPositiveClick(DateTimeDialogFragment dialog) {
  578. if(this.wasBelowTimePicker){
  579. this.filter.setBelowTimestamp(dialog.getDate());
  580. } else {
  581. this.filter.setAboveTimestamp(dialog.getDate());
  582. }
  583. this.actualiseCurrentPlot();
  584. }
  585. public void onDateTimePickerNegativeClick(DateTimeDialogFragment dialog) {
  586. if(this.wasBelowTimePicker){
  587. this.filter.setBelowTimestamp(Long.MAX_VALUE);
  588. } else {
  589. this.filter.setAboveTimestamp(Long.MIN_VALUE);
  590. }
  591. this.actualiseCurrentPlot();
  592. }
  593. /*
  594. *
  595. * PLOT TYPES
  596. *
  597. * **/
  598. public PieGraph getPieGraphView(){
  599. if (this.pieGraph == null) {
  600. this.pieGraph = new PieGraph(this.getApplicationContext());
  601. LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  602. plotLayout.addView(this.pieGraph);
  603. this.pieGraph.setOnSliceClickedListener(new PieGraph.OnSliceClickedListener() {
  604. @Override
  605. public void onClick(int index) {
  606. StatisticsFragment.this.onSliceClick(index);
  607. }
  608. });
  609. }
  610. return this.pieGraph;
  611. }
  612. public LineGraph getLineGraphView(){
  613. if (this.lineGraph == null) {
  614. this.lineGraph = new LineGraph(this.getActivity());
  615. LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  616. plotLayout.addView(this.lineGraph);
  617. }
  618. return this.lineGraph;
  619. }
  620. public BarGraph getBarGraphView(){
  621. if (this.barGraph == null) {
  622. this.barGraph = new BarGraph(this.getActivity());
  623. LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  624. this.barGraph.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT ));
  625. plotLayout.addView(this.barGraph);
  626. this.barGraph.setShowBarText(false);
  627. this.barGraph.setPopupImageID(R.drawable.popup_black);
  628. this.barGraph.setOnBarClickedListener(new BarGraph.OnBarClickedListener() {
  629. @Override
  630. public void onClick(int i) {
  631. StatisticsFragment.this.onBarClick(i);
  632. }
  633. });
  634. }
  635. return this.barGraph;
  636. }
  637. /*
  638. * FEED PLOTS WITH DATA
  639. * */
  640. public void setPieGraphData(PieGraph piegraph){
  641. this.currentData = this.getPieData();
  642. if (this.currentData == null){
  643. this.currentData = new ArrayList<PlotComparisonItem>();
  644. }
  645. this.pieGraph.removeSlices();
  646. for (PlotComparisonItem item : this.currentData){
  647. PieSlice slice = new PieSlice();
  648. slice.setColor(item.getColor());
  649. Double value2 = (Double) item.getValue2();
  650. float v = value2.floatValue();
  651. slice.setValue(v);
  652. slice.setTitle(item.getTitle());
  653. this.pieGraph.addSlice(slice);
  654. }
  655. this.pieGraph.invalidate();
  656. }
  657. public void setLineGraphData(LineGraph linegraph){
  658. this.currentData = this.getLineData();
  659. if (this.currentData == null){
  660. this.currentData = new ArrayList<PlotComparisonItem>();
  661. }
  662. this.lineGraph.removeAllLines();
  663. double rangeMax_Y = 0;
  664. double rangeMin_Y = 0;
  665. double rangeMax_X = 0;
  666. double rangeMin_X = 0;
  667. int count = 0;
  668. for (PlotComparisonItem lineItem : this.currentData){
  669. ArrayList<PlotComparisonItem> data = lineItem.getOtherData();
  670. int index = 0;
  671. Line l = new Line();
  672. int lineColor = lineItem.getColor();
  673. l.setColor(lineColor);
  674. for (PlotComparisonItem pointItem : data){
  675. LinePoint p = new LinePoint();
  676. p.setX(pointItem.getValue1());
  677. Double value2 = pointItem.getValue2();
  678. p.setY(value2);
  679. p.setColor(lineColor);
  680. l.addPoint(p);
  681. rangeMax_Y = Math.max(pointItem.getValue2(), rangeMax_Y);
  682. rangeMax_X = Math.max(pointItem.getValue1(), rangeMax_X);
  683. if (count != 0){
  684. rangeMin_Y = Math.min(pointItem.getValue2(), rangeMin_Y);
  685. rangeMin_X = Math.min(pointItem.getValue1(), rangeMin_X);
  686. } else {
  687. rangeMin_Y = pointItem.getValue2();
  688. rangeMin_X = pointItem.getValue1();
  689. }
  690. index++;
  691. count++;
  692. }
  693. this.lineGraph.addLine(l);
  694. }
  695. // add a bit more space
  696. rangeMax_Y++;
  697. rangeMin_Y--;
  698. boolean shouldUseDate = this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerDate);
  699. if (shouldUseDate){
  700. this.lineGraph.resetXLimits();
  701. if (this.filter.hasBelowTimestamp()){
  702. rangeMax_X = Math.max(this.filter.belowTimestamp, rangeMax_X);
  703. }
  704. if (this.filter.hasAboveTimestamp()){
  705. rangeMin_X = Math.min(this.filter.aboveTimestamp, rangeMin_X);
  706. }
  707. double stepRange = (rangeMax_X - rangeMin_X)/(60*60*24*1000);
  708. this.lineGraph.setxAxisStep(Math.max(1, (float) Math.min(stepRange, 4)));
  709. this.lineGraph.setRangeX(rangeMin_X , rangeMax_X);
  710. this.lineGraph.setConverter(new LineGraph.AxisDataConverter() {
  711. @Override
  712. public String convertDataForX_Position(double x) {
  713. return StatisticsFragment.this.getDateAsDayString((long)x);
  714. }
  715. @Override
  716. public String convertDataForY_Position(double y){
  717. return "" + (long)y;
  718. }
  719. });
  720. } else {
  721. this.lineGraph.setxAxisStep(12.f);
  722. this.lineGraph.setRangeX(0, 24);
  723. this.lineGraph.setConverter(null);
  724. }
  725. this.lineGraph.setRangeY(rangeMin_Y, rangeMax_Y);
  726. this.lineGraph.setLineToFill(0);
  727. this.lineGraph.invalidate();
  728. }
  729. public void setBarGraphData(BarGraph bargraph){
  730. this.currentData = this.getBarData();
  731. if (this.currentData == null){
  732. this.currentData = new ArrayList<PlotComparisonItem>();
  733. }
  734. ArrayList<Bar> bars = new ArrayList<Bar>();
  735. for (PlotComparisonItem item : this.currentData){
  736. Bar d = new Bar();
  737. d.setColor(item.getColor());
  738. Long value2 = item.getValue2().longValue();
  739. d.setName("" + value2);
  740. d.setValue(value2.floatValue());
  741. bars.add(d);
  742. }
  743. this.barGraph.setBars(bars);
  744. this.barGraph.invalidate();
  745. }
  746. /*
  747. *
  748. * FETCH & ACTUALISE RECORD DATA
  749. *
  750. * */
  751. public ArrayList<Record> getFetchedRecords(){
  752. if (this.filter == null) this.clearFilter();
  753. return this.dbh.getRecordsForFilter(this.filter);
  754. }
  755. public void actualiseCurrentPlot(){
  756. LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  757. View plot = this.currentPlotView;
  758. if (plot == null){
  759. this.currentPlotView = this.getPieGraphView();
  760. plot = this.currentPlotView;
  761. }
  762. if (plot.getParent() != null && !plot.getParent().equals(plotLayout)){
  763. LinearLayout linLayout = (LinearLayout)plot.getParent();
  764. linLayout.removeView(plot);
  765. plot.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT ));
  766. plotLayout.addView(plot);
  767. }
  768. if (plot.getVisibility() == View.GONE) plot.setVisibility(View.VISIBLE);
  769. if (plot instanceof PieGraph){
  770. PieGraph pie = (PieGraph) plot;
  771. this.setPieGraphData(pie);
  772. // HIDE FILTER BUTTON
  773. ImageButton filterButton = this.getFilterButton();
  774. if (filterButton != null) filterButton.setVisibility(View.GONE);
  775. } else {
  776. if (this.pieGraph != null){
  777. this.pieGraph.setVisibility(View.GONE);
  778. if (this.pieGraph.getParent() != null){
  779. plotLayout.removeView(this.pieGraph);
  780. }
  781. }
  782. // SHOW FILTER BUTTON
  783. ImageButton filterButton = this.getFilterButton();
  784. if (filterButton != null) filterButton.setVisibility(View.VISIBLE);
  785. }
  786. if (plot instanceof BarGraph){
  787. BarGraph bar = (BarGraph) plot;
  788. this.setBarGraphData(bar);
  789. } else {
  790. if (this.barGraph != null){
  791. this.barGraph.setVisibility(View.GONE);
  792. if (this.barGraph.getParent() != null){
  793. plotLayout.removeView(this.barGraph);
  794. }
  795. }
  796. }
  797. if (plot instanceof LineGraph){
  798. LineGraph line = (LineGraph)plot;
  799. this.setLineGraphData(line);
  800. }else {
  801. if (this.lineGraph != null){
  802. this.lineGraph.setVisibility(View.GONE);
  803. if (this.lineGraph.getParent() != null){
  804. plotLayout.removeView(this.lineGraph);
  805. }
  806. }
  807. }
  808. plot.setVisibility(View.VISIBLE);
  809. if (plot.getParent() == null){
  810. plotLayout.addView(plot);
  811. }
  812. this.actualiseLegendList();
  813. this.currentPlotView.bringToFront();
  814. this.currentPlotView.invalidate();
  815. }
  816. public ArrayList<PlotComparisonItem> getPieData(){
  817. // DEFAULT
  818. return this.attacksPerProtocols();
  819. }
  820. public ArrayList<PlotComparisonItem> getBarData(){
  821. String protocol = this.getCurrentSelectedProtocol();
  822. if (protocol.length() > 0){
  823. if (this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerESSID)){
  824. return this.attacksPerESSID(protocol);
  825. }
  826. // DEFAULT
  827. return this.attacksPerBSSID(protocol);
  828. }
  829. // Nothing available
  830. return new ArrayList<PlotComparisonItem>();
  831. }
  832. public ArrayList<PlotComparisonItem> getLineData(){
  833. return this.attacksPerTime();
  834. }
  835. /*
  836. * DATA SOURCE
  837. * */
  838. /*PROTOCOLS OVERVIEW*/
  839. public ArrayList<PlotComparisonItem> attacksPerProtocols(){
  840. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  841. int index = 0;
  842. for (String title : this.getSelectedProtocolTitles()){
  843. int attacksCount = this.dbh.getAttackPerProtocolCount(title);
  844. if (attacksCount == 0) continue;
  845. PlotComparisonItem item = new PlotComparisonItem(title,this.getColor(index), 0., (double) attacksCount);
  846. plotItems.add(item);
  847. index++;
  848. }
  849. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  850. @Override
  851. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  852. return s1.getValue2().compareTo(s2.getValue2());
  853. }
  854. });
  855. return this.resizeData(plotItems);
  856. }
  857. /*LINE PLOT DATA*/
  858. public ArrayList<PlotComparisonItem> attacksPerTime(){
  859. HashMap<String,HashMap<Long, ArrayList<Record> > > lineMap = new HashMap<String, HashMap<Long, ArrayList<Record>>>();
  860. boolean shouldUseDate = this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerDate);
  861. for (Record record : this.getFetchedRecords()){
  862. long timestamp = record.getTimestamp();
  863. long time = 0;
  864. if (shouldUseDate){
  865. time = this.getDateFromMilliseconds(timestamp);
  866. } else {
  867. time = this.getDayHourFromDate(timestamp);
  868. }
  869. // GET CORRECT MAP
  870. HashMap<Long, ArrayList<Record> > recordMap;
  871. String groupKey = record.getSsid();
  872. if (lineMap.containsKey(groupKey)){
  873. recordMap = lineMap.get(record.getSsid());
  874. } else {
  875. recordMap = new HashMap<Long, ArrayList<Record> >();
  876. lineMap.put(groupKey, recordMap);
  877. }
  878. // GET LIST OF RECORDS
  879. ArrayList<Record> list;
  880. if (recordMap.containsKey(time)){
  881. list = recordMap.get(time);
  882. } else {
  883. list = new ArrayList<Record>();
  884. recordMap.put(time, list);
  885. }
  886. list.add(record);
  887. }
  888. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  889. int index = 0;
  890. for (String groupKey : lineMap.keySet()){
  891. HashMap<Long, ArrayList<Record> > recordMap = lineMap.get(groupKey);
  892. ArrayList<PlotComparisonItem> singleLineItems = new ArrayList<PlotComparisonItem>();
  893. for (long time : recordMap.keySet()){
  894. ArrayList<Record>list = recordMap.get(time);
  895. if (list.size() == 0) continue;
  896. PlotComparisonItem item = new PlotComparisonItem(this.getHourAsTimeString(time), 0 , (double)time, (double) list.size());
  897. singleLineItems.add(item);
  898. }
  899. Collections.sort(singleLineItems, new Comparator<PlotComparisonItem>() {
  900. @Override
  901. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  902. return s1.getValue1().compareTo(s2.getValue1());
  903. }
  904. });
  905. PlotComparisonItem item = new PlotComparisonItem(groupKey, this.getColor(index), 0., (double) singleLineItems.size());
  906. item.setOtherData(singleLineItems);
  907. plotItems.add(item);
  908. index++;
  909. }
  910. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  911. @Override
  912. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  913. return s2.getValue2().compareTo(s1.getValue2());
  914. }
  915. });
  916. return plotItems;
  917. }
  918. // BAR PLOT DATA
  919. public ArrayList<PlotComparisonItem> attacksPerBSSID(String protocol){
  920. LogFilter filter = new LogFilter();
  921. ArrayList<String> protocollist = new ArrayList<String>();
  922. filter.setAboveTimestamp(this.filter.getAboveTimestamp());
  923. filter.setBelowTimestamp(this.filter.getBelowTimestamp());
  924. filter.setBSSIDs(this.filter.getBSSIDs());
  925. protocollist.add(protocol);
  926. filter.setProtocols(protocollist);
  927. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  928. HashMap<String, Integer> recordMap = new HashMap<String, Integer>();
  929. ArrayList<Record> records = this.dbh.getRecordsForFilter(filter);
  930. for (Record record : records){
  931. int count = 0;
  932. if (recordMap.containsKey(record.getBssid())){
  933. count = recordMap.get(record.getBssid());
  934. }
  935. count++;
  936. recordMap.put(record.getBssid(), count);
  937. }
  938. int index = 0;
  939. for (String key : recordMap.keySet()){
  940. double value = (double)recordMap.get(key);
  941. if (value == 0.) continue;
  942. PlotComparisonItem item = new PlotComparisonItem(key, this.getColor(index), 0., value);
  943. plotItems.add(item);
  944. index++;
  945. }
  946. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  947. @Override
  948. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  949. return s1.getValue2().compareTo(s2.getValue2());
  950. }
  951. });
  952. return this.resizeData(plotItems);
  953. }
  954. public ArrayList<PlotComparisonItem> attacksPerESSID(String protocol){
  955. LogFilter filter = new LogFilter();
  956. filter.setAboveTimestamp(this.filter.getAboveTimestamp());
  957. filter.setBelowTimestamp(this.filter.getBelowTimestamp());
  958. filter.setESSIDs(this.filter.getESSIDs());
  959. ArrayList<String> protocollist = new ArrayList<String>();
  960. protocollist.add(protocol);
  961. filter.setProtocols(protocollist);
  962. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  963. HashMap<String, Integer> recordMap = new HashMap<String, Integer>();
  964. ArrayList<Record> records = this.dbh.getRecordsForFilter(filter);
  965. for (Record record : records){
  966. int count = 0;
  967. if (recordMap.containsKey(record.getSsid())){
  968. count = recordMap.get(record.getSsid());
  969. }
  970. count++;
  971. recordMap.put(record.getSsid(), count);
  972. }
  973. int index = 0;
  974. for (String key : recordMap.keySet()){
  975. double value = (double)recordMap.get(key);
  976. if (value == 0.) continue;
  977. PlotComparisonItem item = new PlotComparisonItem(key,this.getColor(index), 0. ,value);
  978. plotItems.add(item);
  979. index++;
  980. }
  981. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  982. @Override
  983. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  984. return s1.getValue2().compareTo(s2.getValue2());
  985. }
  986. });
  987. return this.resizeData(plotItems);
  988. }
  989. private ArrayList<PlotComparisonItem> resizeData(ArrayList<PlotComparisonItem> plotItems){
  990. if (plotItems != null){
  991. if (plotItems.size() > MAX_NUMBER_OF_CHART_OBJECTS && MAX_NUMBER_OF_CHART_OBJECTS > 1){
  992. ArrayList<PlotComparisonItem> copy = new ArrayList<PlotComparisonItem>();
  993. ArrayList<PlotComparisonItem> others = new ArrayList<PlotComparisonItem>();
  994. double valueOfOthers = 0;
  995. for (int i = 0; i < plotItems.size(); i++){
  996. if (i < MAX_NUMBER_OF_CHART_OBJECTS - 1){
  997. PlotComparisonItem item = plotItems.get(i);
  998. item.setColor(this.getColor(i));
  999. copy.add(plotItems.get(i));
  1000. } else {
  1001. PlotComparisonItem item = plotItems.get(i);
  1002. others.add(item);
  1003. valueOfOthers+=item.getValue2();
  1004. }
  1005. }
  1006. PlotComparisonItem otherItem = new PlotComparisonItem(OTHER_CHART_TITLE, this.getOtherColor(), 0., valueOfOthers);
  1007. otherItem.setOtherData(others);
  1008. copy.add(otherItem);
  1009. return copy;
  1010. }
  1011. }
  1012. return plotItems;
  1013. }
  1014. /*
  1015. * FILTER STUFF
  1016. * */
  1017. private String getCurrentSelectedProtocol(){
  1018. ArrayList<String> protocolTitles = this.getSelectedProtocolTitles();
  1019. if (protocolTitles != null && protocolTitles.size() != 0){
  1020. return this.getSelectedProtocolTitles().get(0);
  1021. }
  1022. return "";
  1023. }
  1024. public ArrayList<String> protocolTitles(){
  1025. ArrayList<String> titles = new ArrayList<String>();
  1026. for (String protocol : this.getResources().getStringArray(
  1027. R.array.protocols)) {
  1028. titles.add(protocol);
  1029. }
  1030. return titles;
  1031. }
  1032. public boolean[] selectedProtocols(){
  1033. ArrayList<String> protocols = this.protocolTitles();
  1034. boolean[] selected = new boolean[protocols.size()];
  1035. int i = 0;
  1036. for(String protocol : protocols){
  1037. selected[i] =(this.filter.protocols.contains(protocol));
  1038. i++;
  1039. }
  1040. return selected;
  1041. }
  1042. public ArrayList<String> getSelectedProtocolTitles(){
  1043. ArrayList<String> knownProtocols = this.protocolTitles();
  1044. if (this.filter.hasProtocols()){
  1045. ArrayList<String> titles = new ArrayList<String>();
  1046. int i =0;
  1047. for (boolean b : this.selectedProtocols()){
  1048. if (b){
  1049. String title = knownProtocols.get(i);
  1050. titles.add(title);
  1051. }
  1052. i++;
  1053. }
  1054. return titles;
  1055. }
  1056. return this.protocolTitles();
  1057. }
  1058. /*
  1059. *
  1060. * COLOR STUFF
  1061. *
  1062. * */
  1063. public int getOtherColor(){
  1064. return Color.argb(255, 80, 80, 80); // grey
  1065. }
  1066. public Integer getColor(int index) {
  1067. return ColorSequenceGenerator.getColorForIndex(index);
  1068. }
  1069. public LinearLayout getPlotLayout(){
  1070. if (this.rootView != null){
  1071. return (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  1072. } else {
  1073. return null;
  1074. }
  1075. }
  1076. /**
  1077. *
  1078. * FILTER STUFF
  1079. *
  1080. * */
  1081. private boolean isFilterSetForTitle(String title){
  1082. if (title.equals(FILTER_MENU_TITLE_BSSID)){
  1083. return this.filter.hasBSSIDs();
  1084. }
  1085. if (title.equals(FILTER_MENU_TITLE_ESSID)){
  1086. return this.filter.hasESSIDs();
  1087. }
  1088. if (title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
  1089. return this.filter.hasProtocols();
  1090. }
  1091. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)){
  1092. return this.filter.hasBelowTimestamp();
  1093. }
  1094. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)){
  1095. return this.filter.hasAboveTimestamp();
  1096. }
  1097. return false;
  1098. }
  1099. private void clearFilter(){
  1100. if(filter == null) this.filter = new LogFilter();
  1101. this.filter.clear();
  1102. }
  1103. /*
  1104. *
  1105. * DATE TRANSFORMATION
  1106. *
  1107. */
  1108. public long getDayHourFromDate(long timeInMillis){
  1109. Calendar calendar = Calendar.getInstance();
  1110. calendar.setTimeInMillis (timeInMillis);
  1111. int hour = calendar.get(Calendar.HOUR_OF_DAY);
  1112. int min = calendar.get(Calendar.MINUTE);
  1113. return hour;
  1114. }
  1115. public long getDateFromMilliseconds(long timeInMillis){
  1116. long millisInDay = 60 * 60 * 24 * 1000;
  1117. return (timeInMillis / millisInDay) * millisInDay;
  1118. }
  1119. /*
  1120. *
  1121. * */
  1122. private String getHourAsTimeString(long hour) {
  1123. return "" + hour + ":00";
  1124. }
  1125. static final DateFormat dateFormat = new SimpleDateFormat("d.M.yyyy");
  1126. @SuppressLint("SimpleDateFormat")
  1127. private String getDateAsDayString(long timeStamp) {
  1128. try {
  1129. Date netDate = (new Date(timeStamp));
  1130. return dateFormat.format(netDate);
  1131. } catch (Exception ex) {
  1132. return "xx";
  1133. }
  1134. }
  1135. @SuppressLint("SimpleDateFormat")
  1136. private String getDateAsString(long timeStamp) {
  1137. try {
  1138. DateFormat sdf = new SimpleDateFormat("H:mm dd/MM/yyyy");
  1139. Date netDate = (new Date(timeStamp));
  1140. return sdf.format(netDate);
  1141. } catch (Exception ex) {
  1142. return "xx";
  1143. }
  1144. }
  1145. /**
  1146. * USERINTERACTION
  1147. */
  1148. private void userTappedOnLegendItem(int index){
  1149. if (index < this.currentData.size()){
  1150. PlotComparisonItem item = this.currentData.get(index);
  1151. ArrayList<String> selectedData;
  1152. String sortKey = null;
  1153. selectedData = new ArrayList<String>();
  1154. if (item.getOtherData() == null){
  1155. selectedData.add(item.getTitle());
  1156. } else {
  1157. for (PlotComparisonItem other : item.getOtherData()){
  1158. selectedData.add(other.getTitle());
  1159. }
  1160. }
  1161. LogFilter filter = new LogFilter();
  1162. if (this.currentPlotView instanceof PieGraph){
  1163. filter.setProtocols(selectedData);
  1164. }
  1165. if (this.currentPlotView instanceof BarGraph){
  1166. if (this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerESSID)){
  1167. filter.setESSIDs(selectedData);
  1168. sortKey = "ESSID";
  1169. } else {
  1170. filter.setBSSIDs(selectedData);
  1171. sortKey = "BSSID";
  1172. }
  1173. ArrayList<String> currentSelectedProtocol = new ArrayList<String>();
  1174. currentSelectedProtocol.add(this.getCurrentSelectedProtocol());
  1175. filter.setProtocols(currentSelectedProtocol);
  1176. }
  1177. if (this.currentPlotView instanceof LineGraph){
  1178. selectedData = new ArrayList<String>();
  1179. selectedData.add(item.getTitle());
  1180. filter.setESSIDs(selectedData);
  1181. filter.setProtocols(this.filter.getProtocols());
  1182. sortKey = "ESSID";
  1183. }
  1184. if (this.filter.hasATimestamp()){
  1185. filter.setAboveTimestamp(this.filter.getAboveTimestamp());
  1186. filter.setBelowTimestamp(this.filter.getBelowTimestamp());
  1187. }
  1188. this.pushRecordOverviewForFilter(filter, sortKey);
  1189. }
  1190. }
  1191. public void onSliceClick(int index){
  1192. }
  1193. public void onBarClick(int index){
  1194. this.userTappedOnLegendItem(index);
  1195. }
  1196. private void pushRecordOverviewForFilter(LogFilter filter, String sortKey){
  1197. FragmentManager fm = this.getActivity().getFragmentManager();
  1198. if (fm != null){
  1199. RecordOverviewFragment newFragment = new RecordOverviewFragment();
  1200. newFragment.setFilter(filter);
  1201. if (sortKey != null && sortKey.length() != 0) newFragment.setGroupKey(sortKey);
  1202. FragmentTransaction transaction = fm.beginTransaction();
  1203. transaction.replace(R.id.content_frame, newFragment, newFragment.getTag());
  1204. transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
  1205. transaction.addToBackStack(null);
  1206. transaction.commit();
  1207. }
  1208. }
  1209. }