StatisticsFragment.java 67 KB

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