StatisticsFragment.java 53 KB

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