StatisticsFragment.java 68 KB

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