StatisticsFragment.java 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801
  1. package de.tudarmstadt.informatik.hostage.ui2.fragment;
  2. import android.annotation.SuppressLint;
  3. import android.app.Activity;
  4. import android.app.Fragment;
  5. import android.app.FragmentManager;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.content.res.Configuration;
  9. import android.graphics.Color;
  10. import android.os.Bundle;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.widget.AdapterView;
  15. import android.widget.ImageButton;
  16. import android.widget.LinearLayout;
  17. import android.widget.ListView;
  18. import android.widget.ProgressBar;
  19. import android.widget.RelativeLayout;
  20. import android.widget.TextView;
  21. import com.echo.holographlibrary.Bar;
  22. import com.echo.holographlibrary.BarGraph;
  23. import com.echo.holographlibrary.Line;
  24. import com.echo.holographlibrary.LineGraph;
  25. import com.echo.holographlibrary.LinePoint;
  26. import com.echo.holographlibrary.PieGraph;
  27. import com.echo.holographlibrary.PieSlice;
  28. import java.text.DateFormat;
  29. import java.text.SimpleDateFormat;
  30. import java.util.ArrayList;
  31. import java.util.Calendar;
  32. import java.util.Collections;
  33. import java.util.Comparator;
  34. import java.util.Date;
  35. import java.util.HashMap;
  36. import de.tudarmstadt.informatik.hostage.R;
  37. import de.tudarmstadt.informatik.hostage.logging.Record;
  38. import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
  39. import de.tudarmstadt.informatik.hostage.ui.LogFilter;
  40. import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
  41. import de.tudarmstadt.informatik.hostage.ui2.adapter.StatisticListAdapter;
  42. import de.tudarmstadt.informatik.hostage.ui2.dialog.ChecklistDialog;
  43. import de.tudarmstadt.informatik.hostage.ui2.dialog.DateTimeDialogFragment;
  44. import de.tudarmstadt.informatik.hostage.ui2.helper.ColorSequenceGenerator;
  45. import de.tudarmstadt.informatik.hostage.ui2.model.PlotComparisonItem;
  46. import de.tudarmstadt.informatik.hostage.ui2.popup.AbstractPopup;
  47. import de.tudarmstadt.informatik.hostage.ui2.popup.AbstractPopupItem;
  48. import de.tudarmstadt.informatik.hostage.ui2.popup.SimplePopupItem;
  49. import de.tudarmstadt.informatik.hostage.ui2.popup.SimplePopupTable;
  50. import de.tudarmstadt.informatik.hostage.ui2.popup.SplitPopupItem;
  51. /**
  52. * Created by Julien on 16.02.14.
  53. */
  54. public class StatisticsFragment extends Fragment implements ChecklistDialog.ChecklistDialogListener, DateTimeDialogFragment.DateTimeDialogFragmentListener {
  55. static final String FILTER_MENU_TITLE_BSSID = "BSSID";
  56. static final String FILTER_MENU_TITLE_ESSID = "ESSID";
  57. static final String FILTER_MENU_TITLE_PROTOCOLS = MainActivity.getContext().getString(R.string.stats_protocols);
  58. static final String FILTER_MENU_TITLE_PROTOCOL = MainActivity.getContext().getString(R.string.rec_protocol);
  59. static final String FILTER_MENU_TITLE_TIMESTAMP_BELOW = MainActivity.getContext().getString(R.string.rec_latest);
  60. static final String FILTER_MENU_TITLE_TIMESTAMP_ABOVE = MainActivity.getContext().getString(R.string.rec_earliest);
  61. static final String FILTER_MENU_TITLE_REMOVE = MainActivity.getContext().getString(R.string.rec_reset_filter);
  62. static final String FILTER_MENU_POPUP_TITLE = MainActivity.getContext().getString(R.string.rec_filter_by);
  63. static final String MENU_TITLE_PROTOCOLS = MainActivity.getContext().getString(
  64. R.string.stats_protocols);
  65. static final String MENU_TITLE_NETWORK = MainActivity.getContext().getString(
  66. R.string.stats_networks);
  67. static final String MENU_TITLE_ATTACKS = MainActivity.getContext().getString(
  68. R.string.stats_attacks);
  69. static final String MENU_POPUP_TITLE = MainActivity.getContext().getString(
  70. R.string.stats_visualize);
  71. static final String CHART_TYPE_TITLE_BAR = MainActivity.getContext().getString(
  72. R.string.stats_bar_plot);
  73. static final String CHART_TYPE_TITLE_PIE = MainActivity.getContext().getString(R.string.stats_pie_plot);
  74. static final String CHART_TYPE_TITLE_LINE = MainActivity.getContext().getString(R.string.stats_line_plot);
  75. //static final String DIALOG_PROTOCOLS_TITLE = MainActivity.getContext().getString(R.string.stats_select_protocol_data);
  76. static final String DIALOG_NETWORK_TITLE = MainActivity.getContext().getString(R.string.stats_select_network_data);
  77. static final String DIALOG_ATTACK_TITLE = MainActivity.getContext().getString(R.string.stats_select_attack_data);
  78. static final String COMPARE_TITLE_AttacksPerProtocol = MainActivity.getContext().getString(R.string.stats_attacks_protocol);
  79. //static final String COMPARE_TITLE_UsesPerProtocol = MainActivity.getContext().getString(R.string.stats_uses_protocol);
  80. static final String COMPARE_TITLE_AttacksPerDate = MainActivity.getContext().getString(R.string.stats_attacks_date);
  81. static final String COMPARE_TITLE_AttacksPerTime = MainActivity.getContext().getString(R.string.stats_attacks_time);
  82. static final String COMPARE_TITLE_AttacksPerBSSID = MainActivity.getContext().getString(R.string.stats_attacks_bssid);
  83. static final String COMPARE_TITLE_AttacksPerESSID = MainActivity.getContext().getString(R.string.stats_attacks_essid);
  84. static final String FILTER_MENU_PROTOCOL_SINGLE_CHOICE_TITLE = MainActivity.getContext().getString(R.string.stats_select_protocol);
  85. static final String TABLE_HEADER_VALUE_TITLE_ATTACKS_COUNT = MainActivity.getContext().getString(R.string.stats_attacks_count);
  86. static final String TABLE_HEADER_VALUE_TITLE_ATTACKS_PERCENTAGE = MainActivity.getContext().getString(R.string.stats_per_cent_all);
  87. static final String OTHER_CHART_TITLE = MainActivity.getContext().getString(R.string.stats_other);
  88. // MINIMAL 2
  89. static int MAX_NUMBER_OF_CHART_OBJECTS = 6;
  90. private boolean wasBelowTimePicker;
  91. private LogFilter filter;
  92. /*Maybe used in the future if the users doesn't need a filterbutton in every situation*/
  93. private boolean showFilterButton;
  94. private PieGraph pieGraph;
  95. private LineGraph lineGraph;
  96. private BarGraph barGraph;
  97. private View rootView;
  98. private View currentPlotView;
  99. private Thread loader;
  100. private ProgressBar spinner;
  101. private ArrayList<PlotComparisonItem> currentData;
  102. private HostageDBOpenHelper dbh;
  103. private ListView legendListView;
  104. private String selectedCompareData = COMPARE_TITLE_AttacksPerProtocol;
  105. /**The Charttype.
  106. * PIE_CHART = 0
  107. * BAR_CHART = 1
  108. * LINE_CHART = 2
  109. */
  110. public enum ChartType {
  111. PIE_CHART(0),
  112. BAR_CHART(1),
  113. LINE_CHART(2);
  114. private int value;
  115. private ChartType(int value) {
  116. this.value = value;
  117. }
  118. static public ChartType create(int value){
  119. if (value < 0 || value >= ChartType.values().length) return ChartType.PIE_CHART;
  120. return ChartType.values()[value];
  121. }
  122. public String toString(){
  123. if (this.equals(ChartType.create(0))){
  124. return CHART_TYPE_TITLE_PIE;
  125. }
  126. if (this.equals(ChartType.create(1))){
  127. return CHART_TYPE_TITLE_BAR;
  128. }
  129. return CHART_TYPE_TITLE_LINE;
  130. }
  131. }
  132. /**Returns the FilterButton.
  133. * @return ImageButton filterButton*/
  134. private ImageButton getFilterButton(){
  135. return (ImageButton) this.rootView.findViewById(R.id.FilterButton);
  136. }
  137. /**
  138. * Returns the layout ID
  139. * @Return int layoutID
  140. * */
  141. public int getLayoutID(){
  142. return R.layout.fragment_statistics;
  143. }
  144. @Override
  145. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  146. Bundle savedInstanceState) {
  147. super.onCreateView(inflater, container, savedInstanceState);
  148. getActivity().setTitle(getResources().getString(R.string.drawer_statistics));
  149. dbh = new HostageDBOpenHelper(this.getBaseContext());
  150. // Get the message from the intent
  151. if (this.filter == null){
  152. Intent intent = this.getActivity().getIntent();
  153. LogFilter filter = intent.getParcelableExtra(LogFilter.LOG_FILTER_INTENT_KEY);
  154. if(filter == null){
  155. this.clearFilter();
  156. } else {
  157. this.filter = filter;
  158. }
  159. }
  160. this.rootView = inflater.inflate(this.getLayoutID(), container, false);
  161. this.configureRootView(this.rootView);
  162. return this.rootView;
  163. }
  164. @Override
  165. public void onCreate(Bundle savedInstanceState) {
  166. super.onCreate(savedInstanceState);
  167. setHasOptionsMenu(true);
  168. setRetainInstance(true);
  169. }
  170. @Override
  171. public void onConfigurationChanged(Configuration newConfig){
  172. super.onConfigurationChanged(newConfig);
  173. LayoutInflater inflater = LayoutInflater.from(getActivity());
  174. ViewGroup container = (ViewGroup) this.getView();
  175. container.removeAllViewsInLayout();
  176. this.rootView = inflater.inflate(this.getLayoutID(), container, false);
  177. container.addView(this.rootView);
  178. this.configureRootView(this.rootView);
  179. }
  180. /**Returns the base context.
  181. * @return Context the base context
  182. * */
  183. private Context getBaseContext(){
  184. return this.getActivity().getBaseContext();
  185. }
  186. /**Returns the application context.
  187. * @return Context the application context*/
  188. private Context getApplicationContext(){
  189. return this.getActivity().getApplicationContext();
  190. }
  191. /**Configures the given rootview.
  192. * Sets the Spinner, the list and all requiered buttons.
  193. * It also actualises the current plot type.
  194. * @param View rootView
  195. * */
  196. public void configureRootView(View rootView){
  197. LinearLayout plotLayout = (LinearLayout) rootView.findViewById(R.id.plot_layout);
  198. plotLayout.removeAllViews();
  199. plotLayout.setWillNotDraw(false);
  200. ProgressBar spinner = (ProgressBar) rootView.findViewById(R.id.progressBar1);
  201. if(spinner != null){
  202. this.spinner = spinner;
  203. this.spinner.setVisibility(View.GONE);
  204. } else {
  205. RelativeLayout parent = (RelativeLayout) this.spinner.getParent();
  206. parent.removeView(this.spinner);
  207. RelativeLayout newParent = (RelativeLayout) rootView.findViewById(R.id.plot_parent_layout);
  208. if (newParent != null){
  209. newParent.addView(this.spinner);
  210. }
  211. }
  212. this.legendListView = (ListView) rootView.findViewById(R.id.legend_list_view);
  213. this.legendListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  214. @Override
  215. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  216. StatisticsFragment.this.userTappedOnLegendItem(i);
  217. }
  218. });
  219. rootView.setWillNotDraw(false);
  220. ImageButton visualButton = (ImageButton) rootView.findViewById(R.id.plot_data_button);
  221. visualButton.setOnClickListener(new View.OnClickListener() {
  222. public void onClick(View v) {
  223. StatisticsFragment.this.openBarSelectionMenuOnView(v);
  224. }
  225. });
  226. ImageButton filterButton = this.getFilterButton();
  227. filterButton.setOnClickListener(new View.OnClickListener() {
  228. public void onClick(View v) {
  229. StatisticsFragment.this.openFilterMenuOnView(v);
  230. }
  231. });
  232. this.actualiseCurrentPlot();
  233. if (this.currentPlotView instanceof BarGraph){
  234. this.setTitle("" + this.getCurrentSelectedProtocol() + ": " +this.selectedCompareData);
  235. } else {
  236. this.setTitle(this.selectedCompareData);
  237. }
  238. }
  239. /**Sets the title over the plot view.
  240. * @param String title
  241. * */
  242. public void setTitle(String title){
  243. TextView titleView = (TextView) this.rootView.findViewById(R.id.title_text_view);
  244. if (title != null && titleView != null){
  245. titleView.setText(title);
  246. titleView.invalidate();
  247. }
  248. }
  249. /**Returns the title over the plot view.
  250. * @return String title
  251. * */
  252. public String getTitle(){
  253. TextView titleView = (TextView) this.rootView.findViewById(R.id.title_text_view);
  254. if (titleView != null){
  255. return "" + titleView.getText();
  256. }
  257. return "";
  258. }
  259. @Override
  260. public void onStart() {
  261. super.onStart();
  262. this.actualiseCurrentPlot();
  263. this.currentPlotView.invalidate();
  264. if (this.currentPlotView instanceof BarGraph){
  265. this.setTitle("" + this.getCurrentSelectedProtocol() + ": " +this.selectedCompareData);
  266. } else {
  267. this.setTitle(this.selectedCompareData);
  268. }
  269. }
  270. /**Sets the current chart to the given type and acualises it.
  271. * @param ChartType type
  272. * */
  273. public void setChartType(ChartType type){
  274. boolean shouldChange = true;
  275. this.clearFilter();
  276. if (this.currentPlotView != null){
  277. if (type == ChartType.PIE_CHART){
  278. shouldChange = ! (this.currentPlotView instanceof PieGraph);
  279. // SET FILTER BUTTON HIDDEN
  280. ImageButton filterButton = this.getFilterButton();
  281. if (filterButton != null) filterButton.setVisibility(View.GONE);
  282. } else {
  283. if (this.pieGraph != null)
  284. this.pieGraph.setVisibility(View.GONE);
  285. // SHOW FILTER BUTTON
  286. ImageButton filterButton = this.getFilterButton();
  287. if (filterButton != null) filterButton.setVisibility(View.VISIBLE);
  288. }
  289. if (type == ChartType.LINE_CHART){
  290. shouldChange = ! (this.currentPlotView instanceof LineGraph);
  291. } else {
  292. if (this.lineGraph != null)
  293. this.lineGraph.setVisibility(View.GONE);
  294. }
  295. if (type == ChartType.BAR_CHART){
  296. shouldChange = ! (this.currentPlotView instanceof BarGraph);
  297. } else {
  298. if (this.barGraph != null)
  299. this.barGraph.setVisibility(View.GONE);
  300. }
  301. }
  302. if (shouldChange){
  303. this.currentPlotView = this.getPlotViewForType(type);
  304. this.currentPlotView.setVisibility(View.VISIBLE);
  305. }
  306. this.actualiseCurrentPlot();
  307. }
  308. /**Returns the plot view for a given type.
  309. * @param ChartType type.
  310. * */
  311. public View getPlotViewForType(ChartType type){
  312. switch (type){
  313. case PIE_CHART:
  314. return this.getPieGraphView();
  315. case LINE_CHART:
  316. return this.getLineGraphView();
  317. default:
  318. return this.getBarGraphView();
  319. }
  320. }
  321. /**Actualises the list view. Therefore it requiers the "currentData".*/
  322. public void actualiseLegendList(){
  323. StatisticListAdapter adapter = new StatisticListAdapter(this.getApplicationContext(), this.currentData);
  324. if (this.currentPlotView instanceof LineGraph){
  325. adapter.setValueFormatter(new StatisticListAdapter.ValueFormatter() {
  326. @Override
  327. public String convertValueForItemToString(PlotComparisonItem item) {
  328. return String.format("%.02f", item.getValue2()) + " %" + " " + "("+ (item.getValue1().intValue()) +")";
  329. }
  330. });
  331. } else {
  332. adapter.setValueFormatter(new StatisticListAdapter.ValueFormatter() {
  333. @Override
  334. public String convertValueForItemToString(PlotComparisonItem item) {
  335. int v = (int) item.getValue2().intValue();
  336. return "" + v;
  337. }
  338. });
  339. }
  340. this.legendListView.setAdapter(adapter);
  341. TextView tableHeaderTitleView = (TextView) this.rootView.findViewById(R.id.table_header_title_textview);
  342. TextView tableHeaderValueView = (TextView) this.rootView.findViewById(R.id.table_header_value_textview);
  343. if (this.currentPlotView instanceof LineGraph){
  344. tableHeaderTitleView.setText(FILTER_MENU_TITLE_ESSID);
  345. tableHeaderValueView.setText(TABLE_HEADER_VALUE_TITLE_ATTACKS_PERCENTAGE);
  346. }
  347. if (this.currentPlotView instanceof PieGraph){
  348. tableHeaderTitleView.setText(FILTER_MENU_TITLE_PROTOCOL);
  349. tableHeaderValueView.setText(TABLE_HEADER_VALUE_TITLE_ATTACKS_COUNT);
  350. }
  351. if (this.currentPlotView instanceof BarGraph){
  352. tableHeaderValueView.setText(TABLE_HEADER_VALUE_TITLE_ATTACKS_COUNT);
  353. if (this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerBSSID)){
  354. tableHeaderTitleView.setText(FILTER_MENU_TITLE_BSSID);
  355. } else {
  356. tableHeaderTitleView.setText(FILTER_MENU_TITLE_ESSID);
  357. }
  358. }
  359. }
  360. /*
  361. * MENU
  362. * */
  363. /**Opens the Bar Option Menu above the given anchor view.
  364. * @param View the anchor view*/
  365. private void openBarSelectionMenuOnView(View anchorView){
  366. SimplePopupTable visualiseMenu = new SimplePopupTable(this.getActivity(), new AbstractPopup.OnPopupItemClickListener() {
  367. public void onItemClick(Object ob) {
  368. if (ob instanceof AbstractPopupItem){
  369. AbstractPopupItem item = (AbstractPopupItem) ob;
  370. StatisticsFragment.this.userSelectMenuItem(item);
  371. }
  372. }
  373. });
  374. visualiseMenu.setTitle(MENU_POPUP_TITLE);
  375. int id = 0;
  376. for(String title : StatisticsFragment.this.getMenuTitles()){
  377. SimplePopupItem item = new SimplePopupItem(this.getActivity());
  378. item.setTitle(title);
  379. item.setItemId(id);
  380. item.setSelected(false);
  381. visualiseMenu.addItem(item);
  382. id++;
  383. }
  384. visualiseMenu.showOnView(anchorView);
  385. }
  386. /**Will be called when the users selected an menu item (visualise menu / plot menu).
  387. * If the user selected "Protocols" this method sets the current plot type to piegraph.
  388. * Otherwise it will open a new dialog to select the comparison type.
  389. * */
  390. private void userSelectMenuItem(AbstractPopupItem item){
  391. // OPEN A DIALOG TO SPECIFY THE VISUALISE DATA
  392. if (item.getTitle().equals(MENU_TITLE_PROTOCOLS)){
  393. ChartType chartType = ChartType.PIE_CHART;
  394. this.selectedCompareData = COMPARE_TITLE_AttacksPerProtocol;
  395. this.setChartType(chartType);
  396. this.setTitle(COMPARE_TITLE_AttacksPerProtocol);
  397. }
  398. if (item.getTitle().equals(MENU_TITLE_NETWORK)){
  399. this.openNetworkDataDialog();
  400. }
  401. if (item.getTitle().equals(MENU_TITLE_ATTACKS)){
  402. this.openAttackDataDialog();
  403. }
  404. }
  405. /**Returns the menu titles (visualise menu / plot menu)*/
  406. private ArrayList<String> getMenuTitles(){
  407. ArrayList<String> titles = new ArrayList<String>();
  408. titles.add(MENU_TITLE_PROTOCOLS);
  409. titles.add(MENU_TITLE_NETWORK);
  410. titles.add(MENU_TITLE_ATTACKS);
  411. return titles;
  412. }
  413. /*
  414. * PLOT DATA DIALOGS
  415. * */
  416. // private void openProtocolDataDialog(){
  417. // ArrayList<String> titles = this.getDialogProtocolDataTitle();
  418. // ChecklistDialog newFragment = new ChecklistDialog(DIALOG_PROTOCOLS_TITLE, titles, this.selectedData(titles), false , this);
  419. // newFragment.show(this.getActivity().getFragmentManager(), DIALOG_PROTOCOLS_TITLE);
  420. // }
  421. /**Opens the network comparison dialog*/
  422. private void openNetworkDataDialog(){
  423. ArrayList<String> titles = this.getDialogNetworkDataTitle();
  424. ChecklistDialog newFragment = new ChecklistDialog(DIALOG_NETWORK_TITLE, titles, this.selectedData(titles), false , this);
  425. newFragment.show(this.getActivity().getFragmentManager(), DIALOG_NETWORK_TITLE);
  426. }
  427. /**Opens the attack comparison dialog*/
  428. private void openAttackDataDialog(){
  429. ArrayList<String> titles = this.getDialogAttackDataTitle();
  430. ChecklistDialog newFragment = new ChecklistDialog(DIALOG_ATTACK_TITLE, titles, this.selectedData(titles), false , this);
  431. newFragment.show(this.getActivity().getFragmentManager(), DIALOG_ATTACK_TITLE);
  432. }
  433. /*
  434. *
  435. * DIALOG ACTION METHODS
  436. *
  437. * */
  438. /**
  439. * Will be called if the user selects the positiv button on an checklist dialog.
  440. * @param ChecklistDialog dialog
  441. * */
  442. public void onDialogPositiveClick(ChecklistDialog dialog) {
  443. String title = dialog.getTitle();
  444. ArrayList<String> titles =dialog.getSelectedItemTitles();
  445. if (title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
  446. //titles = titles.size() == 0 ? this.protocolTitles() : titles;
  447. this.filter.setProtocols(titles);
  448. this.actualiseCurrentPlot();
  449. return;
  450. }
  451. if (title.equals(FILTER_MENU_PROTOCOL_SINGLE_CHOICE_TITLE)){
  452. if (titles.size() == 0){
  453. titles = new ArrayList<String>();
  454. titles.add(this.protocolTitles().get(0));
  455. }
  456. this.filter.setProtocols(titles);
  457. this.actualiseCurrentPlot();
  458. String fragTitle = "" + this.getCurrentSelectedProtocol() + ": " + this.selectedCompareData;
  459. this.setTitle(fragTitle);
  460. return;
  461. }
  462. if (title.equals(FILTER_MENU_TITLE_ESSID)){
  463. this.filter.setESSIDs(titles);
  464. this.actualiseCurrentPlot();
  465. return;
  466. }
  467. if (title.equals(FILTER_MENU_TITLE_BSSID)){
  468. this.filter.setBSSIDs(titles);
  469. this.actualiseCurrentPlot();
  470. return;
  471. }
  472. if (titles.size() != 0){
  473. String data = titles.get(0);
  474. this.setTitle(data);
  475. this.actualiseFilterButton();
  476. if (data.equals(COMPARE_TITLE_AttacksPerTime) || data.equals(COMPARE_TITLE_AttacksPerDate)){
  477. ChartType chartType = ChartType.LINE_CHART;
  478. this.selectedCompareData = data;
  479. this.setChartType(chartType);
  480. return;
  481. }
  482. if (data.equals(COMPARE_TITLE_AttacksPerBSSID) || data.equals(COMPARE_TITLE_AttacksPerESSID)){
  483. ChartType chartType = ChartType.BAR_CHART;
  484. this.selectedCompareData = data;
  485. this.setChartType(chartType);
  486. String fragTitle = "" + this.getCurrentSelectedProtocol() + ": " + this.selectedCompareData;
  487. this.setTitle(fragTitle);
  488. return;
  489. }
  490. }
  491. }
  492. /**
  493. * Will be called if the user selects the negativ button on an checklist dialog.
  494. * @param ChecklistDialog dialog
  495. * */
  496. public void onDialogNegativeClick(ChecklistDialog dialog) {
  497. }
  498. /*
  499. *
  500. * DIALOG DATA
  501. *
  502. * */
  503. // private ArrayList<String> getDialogProtocolDataTitle(){
  504. // ArrayList<String> data = new ArrayList<String>();
  505. // data.add(COMPARE_TITLE_AttacksPerProtocol);
  506. // data.add(COMPARE_TITLE_UsesPerProtocol);
  507. // return data;
  508. // }
  509. /**
  510. * Returns the Attacks comparison titles.
  511. * @return ArrayList<String> the titles
  512. */
  513. private ArrayList<String> getDialogAttackDataTitle(){
  514. ArrayList<String> data = new ArrayList<String>();
  515. data.add(COMPARE_TITLE_AttacksPerDate);
  516. data.add(COMPARE_TITLE_AttacksPerTime);
  517. return data;
  518. }
  519. /**
  520. * Returns the network comparison titles.
  521. * @return ArrayList<String> the titles
  522. */
  523. private ArrayList<String> getDialogNetworkDataTitle(){
  524. ArrayList<String> data = new ArrayList<String>();
  525. data.add(COMPARE_TITLE_AttacksPerESSID);
  526. data.add(COMPARE_TITLE_AttacksPerBSSID);
  527. return data;
  528. }
  529. /**
  530. * DEFAULT
  531. * Returns an boolean array with a default selection. Just the first object is true.
  532. * @return boolean[] selected array
  533. */
  534. private boolean[] selectedData(ArrayList<String> data){
  535. boolean[] selected = new boolean[data.size()];
  536. // SET DEFAULT
  537. selected[0] = true;
  538. return selected;
  539. }
  540. /*
  541. *
  542. * FILTER BUTTON
  543. *
  544. * */
  545. /**Paints the filter button if the current filter object is set.*/
  546. private void actualiseFilterButton(){
  547. if ((this.filter.isSet() && (!(this.currentPlotView instanceof BarGraph))|| (this.filter.hasATimestamp() || this.filter.hasBSSIDs() || this.filter.hasESSIDs()))){
  548. ImageButton filterButton = this.getFilterButton();
  549. if (filterButton != null){
  550. filterButton.setImageResource(R.drawable.ic_filter_pressed);
  551. filterButton.invalidate();
  552. }
  553. } else {
  554. ImageButton filterButton = this.getFilterButton();
  555. if (filterButton != null){
  556. filterButton.setImageResource(R.drawable.ic_filter);
  557. filterButton.invalidate();
  558. }
  559. }
  560. }
  561. /**
  562. * Opens the filter menu above an given anchor view.
  563. * @param View anchor view
  564. */
  565. private void openFilterMenuOnView(View anchor){
  566. SimplePopupTable filterMenu = new SimplePopupTable(this.getActivity(), new AbstractPopup.OnPopupItemClickListener() {
  567. public void onItemClick(Object ob) {
  568. if (ob instanceof AbstractPopupItem){
  569. AbstractPopupItem item = (AbstractPopupItem) ob;
  570. StatisticsFragment.this.onFilterMenuItemSelected(item);
  571. }
  572. }
  573. });
  574. filterMenu.setTitle(FILTER_MENU_POPUP_TITLE);
  575. for(String title : StatisticsFragment.this.filterMenuTitles()){
  576. AbstractPopupItem item = null;
  577. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)) continue;
  578. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)){
  579. item = new SplitPopupItem(this.getActivity());
  580. item.setValue(SplitPopupItem.RIGHT_TITLE, FILTER_MENU_TITLE_TIMESTAMP_BELOW);
  581. item.setValue(SplitPopupItem.LEFT_TITLE, FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  582. if (this.filter.hasBelowTimestamp()){
  583. item.setValue(SplitPopupItem.RIGHT_SUBTITLE, this.getDateAsString(this.filter.belowTimestamp));
  584. }
  585. if (this.filter.hasAboveTimestamp()){
  586. item.setValue(SplitPopupItem.LEFT_SUBTITLE, this.getDateAsString(this.filter.aboveTimestamp));
  587. }
  588. } else {
  589. item = new SimplePopupItem(this.getActivity());
  590. item.setTitle(title);
  591. ((SimplePopupItem)item).setSelected(this.isFilterSetForTitle(title));
  592. }
  593. filterMenu.addItem(item);
  594. }
  595. filterMenu.showOnView(anchor);
  596. }
  597. /**
  598. * Will be called if the user selected an filter item.
  599. * @param AbstractPopupItem item
  600. */
  601. private void onFilterMenuItemSelected(AbstractPopupItem item){
  602. if (item instanceof SplitPopupItem){
  603. SplitPopupItem sItem = (SplitPopupItem) item;
  604. this.wasBelowTimePicker = sItem.wasRightTouch;
  605. if (this.wasBelowTimePicker){
  606. this.openTimestampToFilterDialog();
  607. } else {
  608. this.openTimestampFromFilterDialog();
  609. }
  610. return;
  611. }
  612. String title = item.getTitle();
  613. if (title.equals(FILTER_MENU_TITLE_ESSID)){
  614. this.openESSIDFilterDialog();
  615. }
  616. if (title.equals(FILTER_MENU_TITLE_BSSID)){
  617. this.openBSSIDFilterDialog();
  618. }
  619. if (title.equals(FILTER_MENU_TITLE_PROTOCOL)){
  620. this.openFilterDialogSelectProtocol();
  621. }
  622. if (title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
  623. this.openProtocolsFilterDialog();
  624. }
  625. if (title.equals(FILTER_MENU_TITLE_REMOVE)){
  626. this.clearFilter();
  627. this.actualiseCurrentPlot();
  628. }
  629. }
  630. /**
  631. * Return the menu titles of the filter menu.
  632. * @return ArrayList<String> filter menu title
  633. * */
  634. private ArrayList<String> filterMenuTitles(){
  635. ArrayList<String> titles = new ArrayList<String>();
  636. if (this.currentPlotView instanceof LineGraph){
  637. titles.add(FILTER_MENU_TITLE_ESSID);
  638. titles.add(FILTER_MENU_TITLE_PROTOCOLS);
  639. titles.add(FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  640. if (this.filter.hasESSIDs() || this.filter.hasATimestamp() || (this.filter.getProtocols() != null && this.filter.hasProtocols() && this.filter.getProtocols().size() != this.protocolTitles().size())){
  641. titles.add(FILTER_MENU_TITLE_REMOVE);
  642. }
  643. } else {
  644. titles.add(FILTER_MENU_TITLE_PROTOCOL);
  645. String protocol = this.getCurrentSelectedProtocol();
  646. if (protocol.length() > 0){
  647. if (this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerBSSID)){
  648. titles.add(FILTER_MENU_TITLE_BSSID);
  649. } else {
  650. // DEFAULT
  651. titles.add(FILTER_MENU_TITLE_ESSID);
  652. }
  653. }
  654. titles.add(FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  655. if (this.filter.hasATimestamp() || this.filter.hasESSIDs() || this.filter.hasBSSIDs()
  656. || (this.currentPlotView instanceof LineGraph && this.filter.hasProtocols())){
  657. titles.add(FILTER_MENU_TITLE_REMOVE);
  658. }
  659. }
  660. return titles;
  661. }
  662. /**
  663. * Opens a multiple protocol checklist dialog
  664. */
  665. private void openProtocolsFilterDialog(){
  666. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_PROTOCOLS,
  667. this.protocolTitles(),
  668. this.selectedProtocols(),
  669. true ,
  670. this);
  671. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_PROTOCOLS);
  672. }
  673. /**
  674. * Opens a single protocol checklist dialog
  675. */
  676. private void openFilterDialogSelectProtocol(){
  677. ArrayList<String> titles = this.protocolTitles();
  678. boolean[] selected = new boolean[titles.size()];
  679. int i = 0;
  680. for (String title : titles){
  681. selected[i] = title.equals(this.getCurrentSelectedProtocol());
  682. i++;
  683. }
  684. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_PROTOCOL_SINGLE_CHOICE_TITLE, titles, selected, false , this);
  685. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_PROTOCOL_SINGLE_CHOICE_TITLE);
  686. }
  687. /**
  688. * Opens a multiple essid checklist dialog
  689. */
  690. private void openESSIDFilterDialog(){
  691. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_ESSID, this.essids(), this.selectedESSIDs(), true , this);
  692. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_ESSID);
  693. }
  694. /**Opens a multiple bssid checlist dialog.*/
  695. private void openBSSIDFilterDialog(){
  696. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_BSSID, this.bssids(), this.selectedBSSIDs(), true , this);
  697. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_BSSID);
  698. }
  699. /** Opens a minimal timestamp dialog.**/
  700. private void openTimestampFromFilterDialog(){
  701. this.wasBelowTimePicker = false;
  702. DateTimeDialogFragment newFragment = new DateTimeDialogFragment(this.getActivity());
  703. newFragment.setDateChangeListener(this);
  704. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  705. if (this.filter.aboveTimestamp != Long.MIN_VALUE)newFragment.setDate(this.filter.aboveTimestamp);
  706. }
  707. /** Opens the maximal timestamp dialog.*/
  708. private void openTimestampToFilterDialog(){
  709. this.wasBelowTimePicker = true;
  710. DateTimeDialogFragment newFragment = new DateTimeDialogFragment(this.getActivity());
  711. newFragment.setDateChangeListener(this);
  712. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_TIMESTAMP_BELOW);
  713. if (this.filter.belowTimestamp != Long.MAX_VALUE) newFragment.setDate(this.filter.belowTimestamp);
  714. }
  715. /** Returns all essids
  716. * If the current plot is a bar graph, it just return all possible essids for the selected protocol
  717. * @return ArrayList<String> essids
  718. * */
  719. public ArrayList<String> essids(){
  720. ArrayList<String> records;
  721. if (this.currentPlotView instanceof BarGraph){
  722. records = dbh.getUniqueESSIDRecordsForProtocol(this.getCurrentSelectedProtocol());
  723. } else {
  724. records = dbh.getUniqueESSIDRecords();
  725. }
  726. return records;
  727. }
  728. /** Returns a boolean array. The position in the array will be true, if the essid is selected in the filter.
  729. * @return boolean[] selected essids*/
  730. public boolean[] selectedESSIDs(){
  731. ArrayList<String> essids = this.essids();
  732. boolean[] selected = new boolean[essids.size()];
  733. int i = 0;
  734. for(String essid : essids){
  735. selected[i] =(this.filter.getESSIDs().contains(essid));
  736. i++;
  737. }
  738. return selected;
  739. }
  740. /** Returns all bssids
  741. * If the current plot is a bar graph, it just return all possible bssids for the selected protocol
  742. * @return ArrayList<String> bssids
  743. * */
  744. public ArrayList<String> bssids(){
  745. ArrayList<String> records ;
  746. if (this.currentPlotView instanceof BarGraph){
  747. records = dbh.getUniqueBSSIDRecordsForProtocol(this.getCurrentSelectedProtocol());
  748. } else {
  749. records = dbh.getUniqueBSSIDRecords();
  750. }
  751. return records;
  752. }
  753. /** Returns a boolean array. The position in the array will be true, if the bssid is selected in the filter.
  754. * @return boolean[] selected bssids*/
  755. public boolean[] selectedBSSIDs(){
  756. ArrayList<String> bssids = this.bssids();
  757. boolean[] selected = new boolean[bssids.size()];
  758. int i = 0;
  759. for(String bssid : bssids){
  760. selected[i] =(this.filter.getBSSIDs().contains(bssid));
  761. i++;
  762. }
  763. return selected;
  764. }
  765. /**Will be called if the user selects an date on the timestamp dialog*/
  766. public void onDateTimePickerPositiveClick(DateTimeDialogFragment dialog) {
  767. if(this.wasBelowTimePicker){
  768. this.filter.setBelowTimestamp(dialog.getDate());
  769. } else {
  770. this.filter.setAboveTimestamp(dialog.getDate());
  771. }
  772. this.actualiseCurrentPlot();
  773. }
  774. /**Will be called if the user cancels an date selection on the timestamp dialog*/
  775. public void onDateTimePickerNegativeClick(DateTimeDialogFragment dialog) {
  776. if(this.wasBelowTimePicker){
  777. this.filter.setBelowTimestamp(Long.MAX_VALUE);
  778. } else {
  779. this.filter.setAboveTimestamp(Long.MIN_VALUE);
  780. }
  781. this.actualiseCurrentPlot();
  782. }
  783. /*
  784. *
  785. * PLOT TYPES
  786. *
  787. * **/
  788. /**Returns the current pie graph.
  789. * @return PieGraph current piegraph*/
  790. public PieGraph getPieGraphView(){
  791. if (this.pieGraph == null) {
  792. this.pieGraph = new PieGraph(this.getApplicationContext());
  793. LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  794. plotLayout.addView(this.pieGraph);
  795. this.pieGraph.setOnSliceClickedListener(new PieGraph.OnSliceClickedListener() {
  796. @Override
  797. public void onClick(int index) {
  798. StatisticsFragment.this.onSliceClick(index);
  799. }
  800. });
  801. }
  802. return this.pieGraph;
  803. }
  804. /**
  805. * Returns the current line graph.
  806. * @return LineGraph current line graph
  807. */
  808. public LineGraph getLineGraphView(){
  809. if (this.lineGraph == null) {
  810. this.lineGraph = new LineGraph(this.getActivity());
  811. LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  812. plotLayout.addView(this.lineGraph);
  813. this.lineGraph.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
  814. }
  815. return this.lineGraph;
  816. }
  817. /**
  818. * Returns the current BarGraph.
  819. * @return BarGraph the current bar graph
  820. */
  821. public BarGraph getBarGraphView(){
  822. if (this.barGraph == null) {
  823. this.barGraph = new BarGraph(this.getActivity());
  824. LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  825. this.barGraph.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT ));
  826. plotLayout.addView(this.barGraph);
  827. this.barGraph.setShowBarText(false);
  828. this.barGraph.setPopupImageID(R.drawable.popup_black);
  829. this.barGraph.setOnBarClickedListener(new BarGraph.OnBarClickedListener() {
  830. @Override
  831. public void onClick(int i) {
  832. StatisticsFragment.this.onBarClick(i);
  833. }
  834. });
  835. }
  836. return this.barGraph;
  837. }
  838. /*
  839. * FEED PLOTS WITH DATA
  840. * */
  841. /**
  842. * Sets the data for the given PieGraph
  843. * @param PieGraph piegraph
  844. */
  845. public void setPieGraphData(PieGraph piegraph){
  846. this.currentData = this.getPieData();
  847. if (this.currentData == null){
  848. this.currentData = new ArrayList<PlotComparisonItem>();
  849. }
  850. piegraph.removeSlices();
  851. for (PlotComparisonItem item : this.currentData){
  852. PieSlice slice = new PieSlice();
  853. slice.setColor(item.getColor());
  854. Double value2 = (Double) item.getValue2();
  855. float v = value2.floatValue();
  856. slice.setValue(v);
  857. slice.setTitle(item.getTitle());
  858. piegraph.addSlice(slice);
  859. }
  860. //piegraph.invalidate();
  861. }
  862. /**
  863. * Sets the data for the given LineGraph
  864. * @param LineGraph linegraph
  865. */
  866. public void setLineGraphData(LineGraph linegraph){
  867. this.currentData = this.getLineData();
  868. if (this.currentData == null){
  869. this.currentData = new ArrayList<PlotComparisonItem>();
  870. }
  871. linegraph.removeAllLines();
  872. double rangeMax_Y = 0;
  873. double rangeMin_Y = 0;
  874. double rangeMax_X = 0;
  875. double rangeMin_X = 0;
  876. int count = 0;
  877. for (PlotComparisonItem lineItem : this.currentData){
  878. ArrayList<PlotComparisonItem> data = lineItem.getChildItems();
  879. //int index = 0;
  880. Line l = new Line();
  881. int lineColor = lineItem.getColor();
  882. l.setColor(lineColor);
  883. for (PlotComparisonItem pointItem : data){
  884. LinePoint p = new LinePoint();
  885. p.setX(pointItem.getValue1());
  886. Double value2 = pointItem.getValue2();
  887. p.setY(value2);
  888. p.setColor(lineColor);
  889. l.addPoint(p);
  890. rangeMax_Y = Math.max(pointItem.getValue2(), rangeMax_Y);
  891. rangeMax_X = Math.max(pointItem.getValue1(), rangeMax_X);
  892. if (count != 0){
  893. rangeMin_Y = Math.min(pointItem.getValue2(), rangeMin_Y);
  894. rangeMin_X = Math.min(pointItem.getValue1(), rangeMin_X);
  895. } else {
  896. rangeMin_Y = pointItem.getValue2();
  897. rangeMin_X = pointItem.getValue1();
  898. }
  899. //index++;
  900. count++;
  901. }
  902. linegraph.addLine(l);
  903. }
  904. // add a bit more space
  905. rangeMax_Y++;
  906. rangeMin_Y--;
  907. boolean shouldUseDate = this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerDate);
  908. if (shouldUseDate){
  909. linegraph.resetXLimits();
  910. if (this.filter.hasBelowTimestamp()){
  911. rangeMax_X = Math.max(this.filter.belowTimestamp, rangeMax_X);
  912. }
  913. if (this.filter.hasAboveTimestamp()){
  914. rangeMin_X = Math.min(this.filter.aboveTimestamp, rangeMin_X);
  915. }
  916. if (rangeMax_X == rangeMin_X){
  917. double aDay = 60*60*24*1000;
  918. rangeMax_X+= aDay;
  919. rangeMin_X-= aDay;
  920. }
  921. double stepRange = (rangeMax_X - rangeMin_X)/(60*60*24*1000);
  922. linegraph.setxAxisStep(Math.max(1, (float) Math.min(stepRange, 4)));
  923. linegraph.setRangeX(rangeMin_X , rangeMax_X);
  924. linegraph.setConverter(new LineGraph.AxisDataConverter() {
  925. @Override
  926. public String convertDataForX_Position(double x) {
  927. return StatisticsFragment.this.getDateAsDayString((long)x);
  928. }
  929. @Override
  930. public String convertDataForY_Position(double y){
  931. return "" + (long)y;
  932. }
  933. });
  934. } else {
  935. linegraph.setxAxisStep(12.f);
  936. linegraph.setRangeX(0, 24);
  937. linegraph.setConverter(null);
  938. }
  939. int maxY = (int)(rangeMax_Y - rangeMin_Y);
  940. linegraph.setYAxisStep(Math.min(maxY, 5));
  941. int yStep = (int)linegraph.getyAxisStep();
  942. if ((maxY % yStep) != 0) {
  943. maxY = maxY + (yStep - (maxY % yStep));
  944. }
  945. rangeMax_Y = rangeMin_Y + maxY;
  946. linegraph.setRangeY(rangeMin_Y, rangeMax_Y);
  947. linegraph.setLineToFill(0);
  948. }
  949. /**
  950. * Set the graph data to the given graph
  951. * @param BarGraph bargraph
  952. * */
  953. public void setBarGraphData(BarGraph bargraph){
  954. this.currentData = this.getBarData();
  955. if (this.currentData == null){
  956. this.currentData = new ArrayList<PlotComparisonItem>();
  957. }
  958. ArrayList<Bar> bars = new ArrayList<Bar>();
  959. for (PlotComparisonItem item : this.currentData){
  960. Bar d = new Bar();
  961. d.setColor(item.getColor());
  962. Long value2 = item.getValue2().longValue();
  963. d.setName("");
  964. d.setValue(value2.floatValue());
  965. bars.add(d);
  966. }
  967. barGraph.setBars(bars);
  968. }
  969. /*
  970. *
  971. * FETCH & ACTUALISE RECORD DATA
  972. *
  973. * */
  974. /**
  975. * Returns the DataBaseHandler result for the current filter.
  976. * @param ArrayList<Record> records
  977. */
  978. public ArrayList<Record> getFetchedRecords(){
  979. if (this.filter == null) this.clearFilter();
  980. return this.dbh.getRecordsForFilter(this.filter);
  981. }
  982. /**Actualises the current plot in a background thread.*/
  983. public void actualiseCurrentPlot(){
  984. this.spinner.setVisibility(View.VISIBLE);
  985. this.actualiseFilterButton();
  986. LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  987. if (this.barGraph != null)
  988. this.barGraph.setVisibility(View.GONE);
  989. if (this.lineGraph != null)
  990. this.lineGraph.setVisibility(View.GONE);
  991. if (this.pieGraph != null)
  992. this.pieGraph.setVisibility(View.GONE);
  993. View plot = this.currentPlotView;
  994. if (plot == null){
  995. this.currentPlotView = this.getPieGraphView();
  996. plot = this.currentPlotView;
  997. }
  998. if (plot.getParent() != null && !plot.getParent().equals(plotLayout)){
  999. LinearLayout linLayout = (LinearLayout)plot.getParent();
  1000. linLayout.removeView(plot);
  1001. plot.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT ));
  1002. plotLayout.addView(plot);
  1003. }
  1004. this.currentPlotView = plot;
  1005. final LinearLayout thePlotlayout = plotLayout;
  1006. if (this.loader != null && this.loader.isAlive()) this.loader.interrupt();
  1007. this.loader = null;
  1008. this.loader = new Thread(new Runnable() {
  1009. @Override
  1010. public void run() {
  1011. this.loadDataInBackground();
  1012. this.actualiseUI();
  1013. }
  1014. private void loadDataInBackground(){
  1015. View plot = StatisticsFragment.this.currentPlotView;
  1016. if (plot instanceof PieGraph){
  1017. PieGraph pie = (PieGraph) plot;
  1018. StatisticsFragment.this.setPieGraphData(pie);
  1019. }
  1020. if (plot instanceof BarGraph){
  1021. BarGraph bar = (BarGraph) plot;
  1022. StatisticsFragment.this.setBarGraphData(bar);
  1023. }
  1024. if (plot instanceof LineGraph){
  1025. LineGraph line = (LineGraph)plot;
  1026. StatisticsFragment.this.setLineGraphData(line);
  1027. }
  1028. }
  1029. private void actualiseUI(){
  1030. Activity actv = StatisticsFragment.this.getActivity();
  1031. if (actv != null){
  1032. actv.runOnUiThread(new Runnable() {
  1033. @Override
  1034. public void run() {
  1035. // SET VISIBILITY
  1036. View plot = StatisticsFragment.this.currentPlotView;
  1037. if (plot instanceof PieGraph){
  1038. // HIDE FILTER BUTTON
  1039. ImageButton filterButton = StatisticsFragment.this.getFilterButton();
  1040. if (filterButton != null) filterButton.setVisibility(View.GONE);
  1041. } else {
  1042. if (StatisticsFragment.this.pieGraph != null){
  1043. StatisticsFragment.this.pieGraph.setVisibility(View.GONE);
  1044. if (StatisticsFragment.this.pieGraph.getParent() != null){
  1045. thePlotlayout.removeView(StatisticsFragment.this.pieGraph);
  1046. }
  1047. }
  1048. // SHOW FILTER BUTTON
  1049. ImageButton filterButton = StatisticsFragment.this.getFilterButton();
  1050. if (filterButton != null) filterButton.setVisibility(View.VISIBLE);
  1051. }
  1052. if (! (plot instanceof BarGraph)){
  1053. if (StatisticsFragment.this.barGraph != null){
  1054. StatisticsFragment.this.barGraph.setVisibility(View.GONE);
  1055. if (StatisticsFragment.this.barGraph.getParent() != null){
  1056. thePlotlayout.removeView(StatisticsFragment.this.barGraph);
  1057. }
  1058. }
  1059. }
  1060. if (!(plot instanceof LineGraph)){
  1061. if (StatisticsFragment.this.lineGraph != null){
  1062. StatisticsFragment.this.lineGraph.setVisibility(View.GONE);
  1063. if (StatisticsFragment.this.lineGraph.getParent() != null){
  1064. thePlotlayout.removeView(StatisticsFragment.this.lineGraph);
  1065. }
  1066. }
  1067. }
  1068. plot.setVisibility(View.VISIBLE);
  1069. if (plot.getParent() == null){
  1070. thePlotlayout.addView(plot);
  1071. }
  1072. StatisticsFragment.this.actualiseLegendList();
  1073. StatisticsFragment.this.currentPlotView.bringToFront();
  1074. StatisticsFragment.this.currentPlotView.invalidate();
  1075. StatisticsFragment.this.spinner.setVisibility(View.GONE);
  1076. }
  1077. });
  1078. }
  1079. }
  1080. });
  1081. this.loader.start();
  1082. }
  1083. /** Calculates and returns the data for the piegraph
  1084. * @return ArrayList<PlotComparisonItem> data */
  1085. public ArrayList<PlotComparisonItem> getPieData(){
  1086. // DEFAULT
  1087. return this.attacksPerProtocols();
  1088. }
  1089. /** Calculates and returns the data for the bargraph.
  1090. * @return ArrayList<PlotComparisonItem> data */
  1091. public ArrayList<PlotComparisonItem> getBarData(){
  1092. String protocol = this.getCurrentSelectedProtocol();
  1093. if (protocol.length() > 0){
  1094. if (this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerESSID)){
  1095. return this.attacksPerESSID(protocol);
  1096. }
  1097. // DEFAULT
  1098. return this.attacksPerBSSID(protocol);
  1099. }
  1100. // Nothing available
  1101. return new ArrayList<PlotComparisonItem>();
  1102. }
  1103. /** Calculates and returns the data for the linegraph
  1104. * @return ArrayList<PlotComparisonItem> data */
  1105. public ArrayList<PlotComparisonItem> getLineData(){
  1106. return this.attacksPerTime();
  1107. }
  1108. /*
  1109. * DATA SOURCE
  1110. * */
  1111. /*PROTOCOLS OVERVIEW*/
  1112. /**
  1113. * Returns the attacks per protocols comparison result.
  1114. * The returned data is resized to the specified limit.
  1115. * @return ArrayList<PlotComparisonItem>
  1116. */
  1117. public synchronized ArrayList<PlotComparisonItem> attacksPerProtocols(){
  1118. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  1119. int index = 0;
  1120. for (String title : this.getSelectedProtocolTitles()){
  1121. int attacksCount = this.dbh.getAttackPerProtocolCount(title);
  1122. if (attacksCount == 0) continue;
  1123. PlotComparisonItem item = new PlotComparisonItem(title,this.getColor(index), 0., (double) attacksCount);
  1124. plotItems.add(item);
  1125. index++;
  1126. }
  1127. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  1128. @Override
  1129. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  1130. return s2.getValue2().compareTo(s1.getValue2());
  1131. }
  1132. });
  1133. return this.resizeData(plotItems);
  1134. }
  1135. /*
  1136. * LINE PLOT DATA
  1137. */
  1138. /**Returns the line graph data responding to the selectedCompareData key.
  1139. * The returned data is resized to the specified limit.
  1140. * @return ArrayList<PlotComparisonItem attacksPerTime or attacksPerDate*/
  1141. public ArrayList<PlotComparisonItem> attacksPerTime(){
  1142. HashMap<String,HashMap<Long, ArrayList<Record> > > lineMap = new HashMap<String, HashMap<Long, ArrayList<Record>>>();
  1143. boolean shouldUseDate = this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerDate);
  1144. ArrayList<Record> records = this.getFetchedRecords();
  1145. for (Record record : records){
  1146. long timestamp = record.getTimestamp();
  1147. long time = 0;
  1148. if (shouldUseDate){
  1149. time = this.getDateFromMilliseconds(timestamp);
  1150. } else {
  1151. time = this.getDayHourFromDate(timestamp);
  1152. }
  1153. // GET CORRECT MAP
  1154. HashMap<Long, ArrayList<Record> > recordMap;
  1155. String groupKey = record.getSsid();
  1156. if (lineMap.containsKey(groupKey)){
  1157. recordMap = lineMap.get(record.getSsid());
  1158. } else {
  1159. recordMap = new HashMap<Long, ArrayList<Record> >();
  1160. lineMap.put(groupKey, recordMap);
  1161. }
  1162. // GET LIST OF RECORDS
  1163. ArrayList<Record> list;
  1164. if (recordMap.containsKey(time)){
  1165. list = recordMap.get(time);
  1166. } else {
  1167. list = new ArrayList<Record>();
  1168. recordMap.put(time, list);
  1169. }
  1170. list.add(record);
  1171. }
  1172. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  1173. int index = 0;
  1174. for (String groupKey : lineMap.keySet()){
  1175. HashMap<Long, ArrayList<Record> > recordMap = lineMap.get(groupKey);
  1176. ArrayList<PlotComparisonItem> singleLineItems = new ArrayList<PlotComparisonItem>();
  1177. int numbOfAttacks = 0;
  1178. for (long time : recordMap.keySet()){
  1179. ArrayList<Record>list = recordMap.get(time);
  1180. if (list.size() == 0) continue;
  1181. PlotComparisonItem item = new PlotComparisonItem(this.getHourAsTimeString(time), 0 , (double)time, (double) list.size());
  1182. singleLineItems.add(item);
  1183. numbOfAttacks +=list.size();
  1184. }
  1185. Collections.sort(singleLineItems, new Comparator<PlotComparisonItem>() {
  1186. @Override
  1187. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  1188. return s1.getValue1().compareTo(s2.getValue1());
  1189. }
  1190. });
  1191. double itemValue = (((double)numbOfAttacks / (double)records.size())*100.);
  1192. PlotComparisonItem item = new PlotComparisonItem(groupKey, this.getColor(index), (double) numbOfAttacks, itemValue);
  1193. item.setChildItems(singleLineItems);
  1194. plotItems.add(item);
  1195. index++;
  1196. }
  1197. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  1198. @Override
  1199. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  1200. return s2.getValue2().compareTo(s1.getValue2());
  1201. }
  1202. });
  1203. return plotItems;
  1204. }
  1205. /*
  1206. * BAR PLOT DATA
  1207. */
  1208. /**Returns plotitems for the comparison "attacks per bssid"
  1209. * The returned data is resized to the specified limit.
  1210. * @return ArrayList<PlotComparisonItem> the data
  1211. */
  1212. public ArrayList<PlotComparisonItem> attacksPerBSSID(String protocol){
  1213. LogFilter filter = new LogFilter();
  1214. ArrayList<String> protocollist = new ArrayList<String>();
  1215. filter.setAboveTimestamp(this.filter.getAboveTimestamp());
  1216. filter.setBelowTimestamp(this.filter.getBelowTimestamp());
  1217. filter.setBSSIDs(this.filter.getBSSIDs());
  1218. protocollist.add(protocol);
  1219. filter.setProtocols(protocollist);
  1220. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  1221. HashMap<String, Integer> recordMap = new HashMap<String, Integer>();
  1222. ArrayList<Record> records = this.dbh.getRecordsForFilter(filter);
  1223. for (Record record : records){
  1224. int count = 0;
  1225. if (recordMap.containsKey(record.getBssid())){
  1226. count = recordMap.get(record.getBssid());
  1227. }
  1228. count++;
  1229. recordMap.put(record.getBssid(), count);
  1230. }
  1231. int index = 0;
  1232. for (String key : recordMap.keySet()){
  1233. double value = (double)recordMap.get(key);
  1234. if (value == 0.) continue;
  1235. PlotComparisonItem item = new PlotComparisonItem(key, this.getColor(index), 0., value);
  1236. plotItems.add(item);
  1237. index++;
  1238. }
  1239. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  1240. @Override
  1241. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  1242. return s2.getValue2().compareTo(s1.getValue2());
  1243. }
  1244. });
  1245. return this.resizeData(plotItems);
  1246. }
  1247. /**Returns plotitems for the comparison "attacks per essid"
  1248. * @return ArrayList<PlotComparisonItem> the data
  1249. */
  1250. public ArrayList<PlotComparisonItem> attacksPerESSID(String protocol){
  1251. LogFilter filter = new LogFilter();
  1252. filter.setAboveTimestamp(this.filter.getAboveTimestamp());
  1253. filter.setBelowTimestamp(this.filter.getBelowTimestamp());
  1254. filter.setESSIDs(this.filter.getESSIDs());
  1255. ArrayList<String> protocollist = new ArrayList<String>();
  1256. protocollist.add(protocol);
  1257. filter.setProtocols(protocollist);
  1258. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  1259. HashMap<String, Integer> recordMap = new HashMap<String, Integer>();
  1260. ArrayList<Record> records = this.dbh.getRecordsForFilter(filter);
  1261. for (Record record : records){
  1262. int count = 0;
  1263. if (recordMap.containsKey(record.getSsid())){
  1264. count = recordMap.get(record.getSsid());
  1265. }
  1266. count++;
  1267. recordMap.put(record.getSsid(), count);
  1268. }
  1269. int index = 0;
  1270. for (String key : recordMap.keySet()){
  1271. double value = (double)recordMap.get(key);
  1272. if (value == 0.) continue;
  1273. PlotComparisonItem item = new PlotComparisonItem(key,this.getColor(index), 0. ,value);
  1274. plotItems.add(item);
  1275. index++;
  1276. }
  1277. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  1278. @Override
  1279. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  1280. return s2.getValue2().compareTo(s1.getValue2());
  1281. }
  1282. });
  1283. return this.resizeData(plotItems);
  1284. }
  1285. /**
  1286. * This will normalize the given list of plot items to the specified length of MAX_NUMBER_OF_CHART_OBJECTS.
  1287. * Creates an "others" group, containing all objects after the (MAX_NUMBER_OF_CHART_OBJECTS - 1)th object from the given list.
  1288. * If the given list does contain MAX_NUMBER_OF_CHART_OBJECTS+1 or more objects, nothing will change.
  1289. *
  1290. * @param AtrrayList<PlotComparisonItem> the list to normalize
  1291. * @return ArrayList<PlotComparisonItem> the normalized data
  1292. */
  1293. private ArrayList<PlotComparisonItem> resizeData(ArrayList<PlotComparisonItem> plotItems){
  1294. if (plotItems != null){
  1295. if (plotItems.size() > MAX_NUMBER_OF_CHART_OBJECTS && MAX_NUMBER_OF_CHART_OBJECTS > 1){
  1296. ArrayList<PlotComparisonItem> copy = new ArrayList<PlotComparisonItem>();
  1297. ArrayList<PlotComparisonItem> others = new ArrayList<PlotComparisonItem>();
  1298. double valueOfOthers = 0;
  1299. for (int i = 0; i < plotItems.size(); i++){
  1300. if (i < MAX_NUMBER_OF_CHART_OBJECTS - 1){
  1301. PlotComparisonItem item = plotItems.get(i);
  1302. item.setColor(this.getColor(i));
  1303. copy.add(plotItems.get(i));
  1304. } else {
  1305. PlotComparisonItem item = plotItems.get(i);
  1306. others.add(item);
  1307. valueOfOthers+=item.getValue2();
  1308. }
  1309. }
  1310. PlotComparisonItem otherItem = new PlotComparisonItem(OTHER_CHART_TITLE, this.getOtherColor(), 0., valueOfOthers);
  1311. otherItem.setChildItems(others);
  1312. copy.add(otherItem);
  1313. Collections.sort(copy, new Comparator<PlotComparisonItem>() {
  1314. @Override
  1315. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  1316. return s2.getValue2().compareTo(s1.getValue2());
  1317. }
  1318. });
  1319. return copy;
  1320. }
  1321. }
  1322. return plotItems;
  1323. }
  1324. /*
  1325. * FILTER STUFF
  1326. * */
  1327. /**
  1328. * Returns the first selected protocol from all selected protocols.
  1329. * If no protocol is selected, it return the first protocol in the protocolTitles() list.
  1330. * @return String protocolName
  1331. * */
  1332. private String getCurrentSelectedProtocol(){
  1333. ArrayList<String> protocolTitles = this.getSelectedProtocolTitles();
  1334. if (protocolTitles != null && protocolTitles.size() != 0){
  1335. return protocolTitles.get(0);
  1336. }
  1337. return this.protocolTitles().get(0);
  1338. }
  1339. /**
  1340. * Return all Protocols
  1341. *
  1342. * @return ArrayList<String> protocolNames
  1343. * */
  1344. public ArrayList<String> protocolTitles(){
  1345. ArrayList<String> titles = new ArrayList<String>();
  1346. for (String protocol : this.getResources().getStringArray(
  1347. R.array.protocols)) {
  1348. titles.add(protocol);
  1349. }
  1350. return titles;
  1351. }
  1352. /**
  1353. * Returns a boolean array containing a bool value for each protocol. If the value is true, the filter object contains the protocol.
  1354. * The array sequence equates to the protocolTitles() list.
  1355. * @return boolean[] selection array
  1356. * */
  1357. public boolean[] selectedProtocols(){
  1358. ArrayList<String> protocols = this.protocolTitles();
  1359. boolean[] selected = new boolean[protocols.size()];
  1360. int i = 0;
  1361. for(String protocol : protocols){
  1362. selected[i] =(this.filter.protocols.contains(protocol));
  1363. i++;
  1364. }
  1365. return selected;
  1366. }
  1367. public ArrayList<String> getSelectedProtocolTitles(){
  1368. ArrayList<String> knownProtocols = this.protocolTitles();
  1369. if (this.filter.hasProtocols()){
  1370. ArrayList<String> titles = new ArrayList<String>();
  1371. int i =0;
  1372. for (boolean b : this.selectedProtocols()){
  1373. if (b){
  1374. String title = knownProtocols.get(i);
  1375. titles.add(title);
  1376. }
  1377. i++;
  1378. }
  1379. return titles;
  1380. }
  1381. return this.protocolTitles();
  1382. }
  1383. /*
  1384. *
  1385. * COLOR STUFF
  1386. *
  1387. * */
  1388. /** Returns the color for the other group
  1389. * @return int color*/
  1390. public int getOtherColor(){
  1391. return Color.argb(255, 80, 80, 80); // grey
  1392. }
  1393. /** Returns the color for the given index
  1394. * @return int color*/
  1395. public Integer getColor(int index) {
  1396. return ColorSequenceGenerator.getColorForIndex(index);
  1397. }
  1398. /** Returns the Plot layout.
  1399. *
  1400. * @return LinearLayout plot layout
  1401. */
  1402. public LinearLayout getPlotLayout(){
  1403. if (this.rootView != null){
  1404. return (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  1405. } else {
  1406. return null;
  1407. }
  1408. }
  1409. /*
  1410. *
  1411. * FILTER STUFF
  1412. *
  1413. * */
  1414. /**
  1415. * Returns true if the current filter is set for a given filter menu title.
  1416. * @param String filter menu title
  1417. * @return boolean b
  1418. */
  1419. private boolean isFilterSetForTitle(String title){
  1420. if (title.equals(FILTER_MENU_TITLE_BSSID)){
  1421. return this.filter.hasBSSIDs();
  1422. }
  1423. if (title.equals(FILTER_MENU_TITLE_ESSID)){
  1424. return this.filter.hasESSIDs();
  1425. }
  1426. if (title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
  1427. return (this.filter.getProtocols() != null && this.filter.hasProtocols() && this.filter.getProtocols().size() != this.protocolTitles().size());
  1428. }
  1429. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)){
  1430. return this.filter.hasBelowTimestamp();
  1431. }
  1432. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)){
  1433. return this.filter.hasAboveTimestamp();
  1434. }
  1435. return false;
  1436. }
  1437. /**
  1438. * Clears the current filter.
  1439. */
  1440. private void clearFilter(){
  1441. if(filter == null) this.filter = new LogFilter();
  1442. this.filter.clear();
  1443. }
  1444. /*
  1445. *
  1446. * DATE TRANSFORMATION
  1447. *
  1448. */
  1449. /**
  1450. * Returns the current hour from a date.
  1451. * @param timeInMillis
  1452. * @return
  1453. */
  1454. public long getDayHourFromDate(long timeInMillis){
  1455. Calendar calendar = Calendar.getInstance();
  1456. calendar.setTimeInMillis (timeInMillis);
  1457. int hour = calendar.get(Calendar.HOUR_OF_DAY);
  1458. //int min = calendar.get(Calendar.MINUTE);
  1459. return hour;
  1460. }
  1461. /**
  1462. * Returns the current date without the seconds, minutes, hours.
  1463. * @param long date
  1464. * @return long date with time 00:00:00
  1465. * */
  1466. public long getDateFromMilliseconds(long timeInMillis){
  1467. long millisInDay = 60 * 60 * 24 * 1000;
  1468. return (timeInMillis / millisInDay) * millisInDay;
  1469. }
  1470. /**
  1471. * Returns the given hour as a formated string.
  1472. * Format: "HH:00"
  1473. * */
  1474. private String getHourAsTimeString(long hour) {
  1475. return "" + hour + ":00";
  1476. }
  1477. static final DateFormat dateFormat = new SimpleDateFormat("d.M.yy");
  1478. /**Returns a date as a formated string
  1479. * @param long date
  1480. * @return String date format (d.M.yy)*/
  1481. @SuppressLint("SimpleDateFormat")
  1482. private String getDateAsDayString(long timeStamp) {
  1483. try {
  1484. Date netDate = (new Date(timeStamp));
  1485. DateFormat dateFormat = android.text.format.DateFormat.getMediumDateFormat(this.getActivity());
  1486. return dateFormat.format(netDate);
  1487. //return dateFormat.format(netDate);
  1488. } catch (Exception ex) {
  1489. return "xx";
  1490. }
  1491. }
  1492. @SuppressLint("SimpleDateFormat")
  1493. /**Returns a date as a formated string
  1494. * @param long date
  1495. * @return String date format (H:mm dd/MM/yyyy)*/
  1496. private String getDateAsString(long timeStamp) {
  1497. try {
  1498. DateFormat sdf = new SimpleDateFormat("H:mm dd/MM/yyyy");
  1499. Date netDate = (new Date(timeStamp));
  1500. return sdf.format(netDate);
  1501. } catch (Exception ex) {
  1502. return "xx";
  1503. }
  1504. }
  1505. /**
  1506. * USERINTERACTION
  1507. */
  1508. /**
  1509. * Will be called if the users taps on a list row.
  1510. * @param int index
  1511. */
  1512. private void userTappedOnLegendItem(int index){
  1513. if (index < this.currentData.size()){
  1514. PlotComparisonItem item = this.currentData.get(index);
  1515. ArrayList<String> selectedData;
  1516. String sortKey = null;
  1517. selectedData = new ArrayList<String>();
  1518. if (item.getChildItems() == null){
  1519. selectedData.add(item.getTitle());
  1520. } else {
  1521. for (PlotComparisonItem other : item.getChildItems()){
  1522. selectedData.add(other.getTitle());
  1523. }
  1524. }
  1525. LogFilter filter = new LogFilter();
  1526. if (this.currentPlotView instanceof PieGraph){
  1527. filter.setProtocols(selectedData);
  1528. }
  1529. if (this.currentPlotView instanceof BarGraph){
  1530. if (this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerESSID)){
  1531. filter.setESSIDs(selectedData);
  1532. sortKey = "ESSID";
  1533. } else {
  1534. filter.setBSSIDs(selectedData);
  1535. sortKey = "BSSID";
  1536. }
  1537. ArrayList<String> currentSelectedProtocol = new ArrayList<String>();
  1538. currentSelectedProtocol.add(this.getCurrentSelectedProtocol());
  1539. filter.setProtocols(currentSelectedProtocol);
  1540. }
  1541. if (this.currentPlotView instanceof LineGraph){
  1542. selectedData = new ArrayList<String>();
  1543. selectedData.add(item.getTitle());
  1544. filter.setESSIDs(selectedData);
  1545. filter.setProtocols(this.filter.getProtocols());
  1546. sortKey = "ESSID";
  1547. }
  1548. if (this.filter.hasATimestamp()){
  1549. filter.setAboveTimestamp(this.filter.getAboveTimestamp());
  1550. filter.setBelowTimestamp(this.filter.getBelowTimestamp());
  1551. }
  1552. this.pushRecordOverviewForFilter(filter, sortKey);
  1553. }
  1554. }
  1555. /**Will be called if the user clicked on a slice
  1556. * @param int slice index*/
  1557. public void onSliceClick(int index){
  1558. }
  1559. /**Will be called if the user clicked on a bar
  1560. * @param int bar index*/
  1561. public void onBarClick(int index){
  1562. this.userTappedOnLegendItem(index);
  1563. }
  1564. /**
  1565. * Displays a record over view fragment.
  1566. * @param LogFilter filter
  1567. * @param String sortKey
  1568. */
  1569. private void pushRecordOverviewForFilter(LogFilter filter, String sortKey){
  1570. FragmentManager fm = this.getActivity().getFragmentManager();
  1571. if (fm != null){
  1572. RecordOverviewFragment newFragment = new RecordOverviewFragment();
  1573. newFragment.setUpNavigatible(true);
  1574. newFragment.setFilter(filter);
  1575. if (sortKey != null && sortKey.length() != 0) newFragment.setGroupKey(sortKey);
  1576. MainActivity.getInstance().injectFragment(newFragment);
  1577. }
  1578. }
  1579. }