StatisticsFragment.java 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846
  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.ui2.model.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 TrackerFragment 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 rootView View
  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 title String
  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 type {@link de.tudarmstadt.informatik.hostage.ui2.fragment.StatisticsFragment.ChartType ChartType}
  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 type {@link de.tudarmstadt.informatik.hostage.ui2.fragment.StatisticsFragment.ChartType ChartType}
  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. if (this.currentData == null || this.currentData.isEmpty()){
  362. tableHeaderTitleView.setText("");
  363. tableHeaderValueView.setText("");
  364. }
  365. }
  366. /*
  367. * MENU
  368. * */
  369. /**Opens the Bar Option Menu above the given anchor view.
  370. * @param anchorView View*/
  371. private void openBarSelectionMenuOnView(View anchorView){
  372. SimplePopupTable visualiseMenu = new SimplePopupTable(this.getActivity(), new AbstractPopup.OnPopupItemClickListener() {
  373. public void onItemClick(Object ob) {
  374. if (ob instanceof AbstractPopupItem){
  375. AbstractPopupItem item = (AbstractPopupItem) ob;
  376. StatisticsFragment.this.userSelectMenuItem(item);
  377. }
  378. }
  379. });
  380. visualiseMenu.setTitle(MENU_POPUP_TITLE);
  381. int id = 0;
  382. for(String title : StatisticsFragment.this.getMenuTitles()){
  383. SimplePopupItem item = new SimplePopupItem(this.getActivity());
  384. item.setTitle(title);
  385. item.setItemId(id);
  386. item.setSelected(false);
  387. visualiseMenu.addItem(item);
  388. id++;
  389. }
  390. visualiseMenu.showOnView(anchorView);
  391. }
  392. /**Will be called when the users selected an menu item (visualise menu / plot menu).
  393. * If the user selected "Protocols" this method sets the current plot type to piegraph.
  394. * Otherwise it will open a new dialog to select the comparison type.
  395. * */
  396. private void userSelectMenuItem(AbstractPopupItem item){
  397. // OPEN A DIALOG TO SPECIFY THE VISUALISE DATA
  398. if (item.getTitle().equals(MENU_TITLE_PROTOCOLS)){
  399. ChartType chartType = ChartType.PIE_CHART;
  400. this.selectedCompareData = COMPARE_TITLE_AttacksPerProtocol;
  401. this.setChartType(chartType);
  402. this.setTitle(COMPARE_TITLE_AttacksPerProtocol);
  403. }
  404. if (item.getTitle().equals(MENU_TITLE_NETWORK)){
  405. this.openNetworkDataDialog();
  406. }
  407. if (item.getTitle().equals(MENU_TITLE_ATTACKS)){
  408. this.openAttackDataDialog();
  409. }
  410. }
  411. /**Returns the menu titles (visualise menu / plot menu)*/
  412. private ArrayList<String> getMenuTitles(){
  413. ArrayList<String> titles = new ArrayList<String>();
  414. titles.add(MENU_TITLE_PROTOCOLS);
  415. titles.add(MENU_TITLE_NETWORK);
  416. titles.add(MENU_TITLE_ATTACKS);
  417. return titles;
  418. }
  419. /*
  420. * PLOT DATA DIALOGS
  421. * */
  422. // private void openProtocolDataDialog(){
  423. // ArrayList<String> titles = this.getDialogProtocolDataTitle();
  424. // ChecklistDialog newFragment = new ChecklistDialog(DIALOG_PROTOCOLS_TITLE, titles, this.selectedData(titles), false , this);
  425. // newFragment.show(this.getActivity().getFragmentManager(), DIALOG_PROTOCOLS_TITLE);
  426. // }
  427. /**Opens the network comparison dialog*/
  428. private void openNetworkDataDialog(){
  429. ArrayList<String> titles = this.getDialogNetworkDataTitle();
  430. ChecklistDialog newFragment = new ChecklistDialog(DIALOG_NETWORK_TITLE, titles, this.selectedData(titles), false , this);
  431. newFragment.show(this.getActivity().getFragmentManager(), DIALOG_NETWORK_TITLE);
  432. }
  433. /**Opens the attack comparison dialog*/
  434. private void openAttackDataDialog(){
  435. ArrayList<String> titles = this.getDialogAttackDataTitle();
  436. ChecklistDialog newFragment = new ChecklistDialog(DIALOG_ATTACK_TITLE, titles, this.selectedData(titles), false , this);
  437. newFragment.show(this.getActivity().getFragmentManager(), DIALOG_ATTACK_TITLE);
  438. }
  439. /*
  440. *
  441. * DIALOG ACTION METHODS
  442. *
  443. * */
  444. /**
  445. * Will be called if the user selects the positiv button on an checklist dialog.
  446. * @param dialog {@link ChecklistDialog ChecklistDialog}
  447. * */
  448. public void onDialogPositiveClick(ChecklistDialog dialog) {
  449. String title = dialog.getTitle();
  450. ArrayList<String> titles =dialog.getSelectedItemTitles();
  451. if (title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
  452. //titles = titles.size() == 0 ? this.protocolTitles() : titles;
  453. this.filter.setProtocols(titles);
  454. this.actualiseCurrentPlot();
  455. return;
  456. }
  457. if (title.equals(FILTER_MENU_PROTOCOL_SINGLE_CHOICE_TITLE)){
  458. if (titles.size() == 0){
  459. titles = new ArrayList<String>();
  460. titles.add(this.protocolTitles().get(0));
  461. }
  462. this.filter.setProtocols(titles);
  463. this.actualiseCurrentPlot();
  464. String fragTitle = "" + this.getCurrentSelectedProtocol() + ": " + this.selectedCompareData;
  465. this.setTitle(fragTitle);
  466. return;
  467. }
  468. if (title.equals(FILTER_MENU_TITLE_ESSID)){
  469. this.filter.setESSIDs(titles);
  470. this.actualiseCurrentPlot();
  471. return;
  472. }
  473. if (title.equals(FILTER_MENU_TITLE_BSSID)){
  474. this.filter.setBSSIDs(titles);
  475. this.actualiseCurrentPlot();
  476. return;
  477. }
  478. if (titles.size() != 0){
  479. String data = titles.get(0);
  480. this.setTitle(data);
  481. this.actualiseFilterButton();
  482. if (data.equals(COMPARE_TITLE_AttacksPerTime) || data.equals(COMPARE_TITLE_AttacksPerDate)){
  483. ChartType chartType = ChartType.LINE_CHART;
  484. this.selectedCompareData = data;
  485. this.setChartType(chartType);
  486. return;
  487. }
  488. if (data.equals(COMPARE_TITLE_AttacksPerBSSID) || data.equals(COMPARE_TITLE_AttacksPerESSID)){
  489. ChartType chartType = ChartType.BAR_CHART;
  490. this.selectedCompareData = data;
  491. this.setChartType(chartType);
  492. String fragTitle = "" + this.getCurrentSelectedProtocol() + ": " + this.selectedCompareData;
  493. this.setTitle(fragTitle);
  494. return;
  495. }
  496. }
  497. }
  498. /**
  499. * Will be called if the user selects the negativ button on an checklist dialog.
  500. * @param dialog {@link ChecklistDialog ChecklistDialog}
  501. * */
  502. public void onDialogNegativeClick(ChecklistDialog dialog) {
  503. }
  504. /*
  505. *
  506. * DIALOG DATA
  507. *
  508. * */
  509. // private ArrayList<String> getDialogProtocolDataTitle(){
  510. // ArrayList<String> data = new ArrayList<String>();
  511. // data.add(COMPARE_TITLE_AttacksPerProtocol);
  512. // data.add(COMPARE_TITLE_UsesPerProtocol);
  513. // return data;
  514. // }
  515. /**
  516. * Returns the Attacks comparison titles.
  517. * @return ArrayList<String> the titles
  518. */
  519. private ArrayList<String> getDialogAttackDataTitle(){
  520. ArrayList<String> data = new ArrayList<String>();
  521. data.add(COMPARE_TITLE_AttacksPerDate);
  522. data.add(COMPARE_TITLE_AttacksPerTime);
  523. return data;
  524. }
  525. /**
  526. * Returns the network comparison titles.
  527. * @return ArrayList<String> the titles
  528. */
  529. private ArrayList<String> getDialogNetworkDataTitle(){
  530. ArrayList<String> data = new ArrayList<String>();
  531. data.add(COMPARE_TITLE_AttacksPerESSID);
  532. data.add(COMPARE_TITLE_AttacksPerBSSID);
  533. return data;
  534. }
  535. /**
  536. * DEFAULT
  537. * Returns an boolean array with a default selection. Just the first object is true.
  538. * @return boolean[] selected array
  539. */
  540. private boolean[] selectedData(ArrayList<String> data){
  541. boolean[] selected = new boolean[data.size()];
  542. // SET DEFAULT
  543. selected[0] = true;
  544. return selected;
  545. }
  546. /*
  547. *
  548. * FILTER BUTTON
  549. *
  550. * */
  551. /**Paints the filter button if the current filter object is set.*/
  552. private void actualiseFilterButton(){
  553. if ((this.filter.isSet() && (!(this.currentPlotView instanceof BarGraph))|| (this.filter.hasATimestamp() || this.filter.hasBSSIDs() || this.filter.hasESSIDs()))){
  554. ImageButton filterButton = this.getFilterButton();
  555. if (filterButton != null){
  556. filterButton.setImageResource(R.drawable.ic_filter_pressed);
  557. filterButton.invalidate();
  558. }
  559. } else {
  560. ImageButton filterButton = this.getFilterButton();
  561. if (filterButton != null){
  562. filterButton.setImageResource(R.drawable.ic_filter);
  563. filterButton.invalidate();
  564. }
  565. }
  566. }
  567. /**
  568. * Opens the filter menu above an given anchor view.
  569. * @param anchor View
  570. */
  571. private void openFilterMenuOnView(View anchor){
  572. SimplePopupTable filterMenu = new SimplePopupTable(this.getActivity(), new AbstractPopup.OnPopupItemClickListener() {
  573. public void onItemClick(Object ob) {
  574. if (ob instanceof AbstractPopupItem){
  575. AbstractPopupItem item = (AbstractPopupItem) ob;
  576. StatisticsFragment.this.onFilterMenuItemSelected(item);
  577. }
  578. }
  579. });
  580. filterMenu.setTitle(FILTER_MENU_POPUP_TITLE);
  581. for(String title : StatisticsFragment.this.filterMenuTitles()){
  582. AbstractPopupItem item = null;
  583. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)) continue;
  584. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)){
  585. item = new SplitPopupItem(this.getActivity());
  586. item.setValue(SplitPopupItem.RIGHT_TITLE, FILTER_MENU_TITLE_TIMESTAMP_BELOW);
  587. item.setValue(SplitPopupItem.LEFT_TITLE, FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  588. if (this.filter.hasBelowTimestamp()){
  589. item.setValue(SplitPopupItem.RIGHT_SUBTITLE, this.getDateAsString(this.filter.belowTimestamp));
  590. }
  591. if (this.filter.hasAboveTimestamp()){
  592. item.setValue(SplitPopupItem.LEFT_SUBTITLE, this.getDateAsString(this.filter.aboveTimestamp));
  593. }
  594. } else {
  595. item = new SimplePopupItem(this.getActivity());
  596. item.setTitle(title);
  597. ((SimplePopupItem)item).setSelected(this.isFilterSetForTitle(title));
  598. }
  599. filterMenu.addItem(item);
  600. }
  601. filterMenu.showOnView(anchor);
  602. }
  603. /**
  604. * Will be called if the user selected an filter item.
  605. * @param item {@link AbstractPopupItem AbstractPopupItem}
  606. */
  607. private void onFilterMenuItemSelected(AbstractPopupItem item){
  608. if (item instanceof SplitPopupItem){
  609. SplitPopupItem sItem = (SplitPopupItem) item;
  610. this.wasBelowTimePicker = sItem.wasRightTouch;
  611. if (this.wasBelowTimePicker){
  612. this.openTimestampToFilterDialog();
  613. } else {
  614. this.openTimestampFromFilterDialog();
  615. }
  616. return;
  617. }
  618. String title = item.getTitle();
  619. if (title.equals(FILTER_MENU_TITLE_ESSID)){
  620. this.openESSIDFilterDialog();
  621. }
  622. if (title.equals(FILTER_MENU_TITLE_BSSID)){
  623. this.openBSSIDFilterDialog();
  624. }
  625. if (title.equals(FILTER_MENU_TITLE_PROTOCOL)){
  626. this.openFilterDialogSelectProtocol();
  627. }
  628. if (title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
  629. this.openProtocolsFilterDialog();
  630. }
  631. if (title.equals(FILTER_MENU_TITLE_REMOVE)){
  632. this.clearFilter();
  633. this.actualiseCurrentPlot();
  634. }
  635. }
  636. /**
  637. * Return the menu titles of the filter menu.
  638. * @return ArrayList<String> filter menu title
  639. * */
  640. private ArrayList<String> filterMenuTitles(){
  641. ArrayList<String> titles = new ArrayList<String>();
  642. if (this.currentPlotView instanceof LineGraph){
  643. titles.add(FILTER_MENU_TITLE_ESSID);
  644. titles.add(FILTER_MENU_TITLE_PROTOCOLS);
  645. titles.add(FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  646. if (this.filter.hasESSIDs() || this.filter.hasATimestamp() || (this.filter.getProtocols() != null && this.filter.hasProtocols() && this.filter.getProtocols().size() != this.protocolTitles().size())){
  647. titles.add(FILTER_MENU_TITLE_REMOVE);
  648. }
  649. } else {
  650. titles.add(FILTER_MENU_TITLE_PROTOCOL);
  651. String protocol = this.getCurrentSelectedProtocol();
  652. if (protocol.length() > 0){
  653. if (this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerBSSID)){
  654. titles.add(FILTER_MENU_TITLE_BSSID);
  655. } else {
  656. // DEFAULT
  657. titles.add(FILTER_MENU_TITLE_ESSID);
  658. }
  659. }
  660. titles.add(FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  661. if (this.filter.hasATimestamp() || this.filter.hasESSIDs() || this.filter.hasBSSIDs()
  662. || (this.currentPlotView instanceof LineGraph && this.filter.hasProtocols())){
  663. titles.add(FILTER_MENU_TITLE_REMOVE);
  664. }
  665. }
  666. return titles;
  667. }
  668. /**
  669. * Opens a multiple protocol checklist dialog
  670. */
  671. private void openProtocolsFilterDialog(){
  672. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_PROTOCOLS,
  673. this.protocolTitles(),
  674. this.selectedProtocols(),
  675. true ,
  676. this);
  677. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_PROTOCOLS);
  678. }
  679. /**
  680. * Opens a single protocol checklist dialog
  681. */
  682. private void openFilterDialogSelectProtocol(){
  683. ArrayList<String> titles = this.protocolTitles();
  684. boolean[] selected = new boolean[titles.size()];
  685. int i = 0;
  686. for (String title : titles){
  687. selected[i] = title.equals(this.getCurrentSelectedProtocol());
  688. i++;
  689. }
  690. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_PROTOCOL_SINGLE_CHOICE_TITLE, titles, selected, false , this);
  691. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_PROTOCOL_SINGLE_CHOICE_TITLE);
  692. }
  693. /**
  694. * Opens a multiple essid checklist dialog
  695. */
  696. private void openESSIDFilterDialog(){
  697. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_ESSID, this.essids(), this.selectedESSIDs(), true , this);
  698. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_ESSID);
  699. }
  700. /**Opens a multiple bssid checlist dialog.*/
  701. private void openBSSIDFilterDialog(){
  702. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_BSSID, this.bssids(), this.selectedBSSIDs(), true , this);
  703. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_BSSID);
  704. }
  705. /** Opens a minimal timestamp dialog.**/
  706. private void openTimestampFromFilterDialog(){
  707. this.wasBelowTimePicker = false;
  708. DateTimeDialogFragment newFragment = new DateTimeDialogFragment(this.getActivity());
  709. newFragment.setDateChangeListener(this);
  710. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  711. if (this.filter.aboveTimestamp != Long.MIN_VALUE)newFragment.setDate(this.filter.aboveTimestamp);
  712. }
  713. /** Opens the maximal timestamp dialog.*/
  714. private void openTimestampToFilterDialog(){
  715. this.wasBelowTimePicker = true;
  716. DateTimeDialogFragment newFragment = new DateTimeDialogFragment(this.getActivity());
  717. newFragment.setDateChangeListener(this);
  718. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_TIMESTAMP_BELOW);
  719. if (this.filter.belowTimestamp != Long.MAX_VALUE) newFragment.setDate(this.filter.belowTimestamp);
  720. }
  721. /** Returns all essids
  722. * If the current plot is a bar graph, it just return all possible essids for the selected protocol
  723. * @return ArrayList<String> essids
  724. * */
  725. public ArrayList<String> essids(){
  726. ArrayList<String> records;
  727. if (this.currentPlotView instanceof BarGraph){
  728. records = dbh.getUniqueESSIDRecordsForProtocol(this.getCurrentSelectedProtocol());
  729. } else {
  730. records = dbh.getUniqueESSIDRecords();
  731. }
  732. return records;
  733. }
  734. /** Returns a boolean array. The position in the array will be true, if the essid is selected in the filter.
  735. * @return boolean[] selected essids*/
  736. public boolean[] selectedESSIDs(){
  737. ArrayList<String> essids = this.essids();
  738. boolean[] selected = new boolean[essids.size()];
  739. int i = 0;
  740. for(String essid : essids){
  741. selected[i] =(this.filter.getESSIDs().contains(essid));
  742. i++;
  743. }
  744. return selected;
  745. }
  746. /** Returns all bssids
  747. * If the current plot is a bar graph, it just return all possible bssids for the selected protocol
  748. * @return ArrayList<String> bssids
  749. * */
  750. public ArrayList<String> bssids(){
  751. ArrayList<String> records ;
  752. if (this.currentPlotView instanceof BarGraph){
  753. records = dbh.getUniqueBSSIDRecordsForProtocol(this.getCurrentSelectedProtocol());
  754. } else {
  755. records = dbh.getUniqueBSSIDRecords();
  756. }
  757. return records;
  758. }
  759. /** Returns a boolean array. The position in the array will be true, if the bssid is selected in the filter.
  760. * @return boolean[] selected bssids*/
  761. public boolean[] selectedBSSIDs(){
  762. ArrayList<String> bssids = this.bssids();
  763. boolean[] selected = new boolean[bssids.size()];
  764. int i = 0;
  765. for(String bssid : bssids){
  766. selected[i] =(this.filter.getBSSIDs().contains(bssid));
  767. i++;
  768. }
  769. return selected;
  770. }
  771. /**Will be called if the user selects an date on the timestamp dialog*/
  772. public void onDateTimePickerPositiveClick(DateTimeDialogFragment dialog) {
  773. if(this.wasBelowTimePicker){
  774. this.filter.setBelowTimestamp(dialog.getDate());
  775. } else {
  776. this.filter.setAboveTimestamp(dialog.getDate());
  777. }
  778. this.actualiseCurrentPlot();
  779. }
  780. /**Will be called if the user cancels an date selection on the timestamp dialog*/
  781. public void onDateTimePickerNegativeClick(DateTimeDialogFragment dialog) {
  782. if(this.wasBelowTimePicker){
  783. this.filter.setBelowTimestamp(Long.MAX_VALUE);
  784. } else {
  785. this.filter.setAboveTimestamp(Long.MIN_VALUE);
  786. }
  787. this.actualiseCurrentPlot();
  788. }
  789. /*
  790. *
  791. * PLOT TYPES
  792. *
  793. * **/
  794. /**Returns the current pie graph.
  795. * @return PieGraph current piegraph*/
  796. public PieGraph getPieGraphView(){
  797. if (this.pieGraph == null) {
  798. this.pieGraph = new PieGraph(this.getApplicationContext());
  799. LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  800. plotLayout.addView(this.pieGraph);
  801. this.pieGraph.setOnSliceClickedListener(new PieGraph.OnSliceClickedListener() {
  802. @Override
  803. public void onClick(int index) {
  804. StatisticsFragment.this.onSliceClick(index);
  805. }
  806. });
  807. }
  808. return this.pieGraph;
  809. }
  810. /**
  811. * Returns the current {@link com.echo.holographlibrary.LineGraph Linegraph} .
  812. * @return LineGraph current line graph
  813. */
  814. public LineGraph getLineGraphView(){
  815. if (this.lineGraph == null) {
  816. this.lineGraph = new LineGraph(this.getActivity());
  817. LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  818. plotLayout.addView(this.lineGraph);
  819. this.lineGraph.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
  820. }
  821. return this.lineGraph;
  822. }
  823. /**
  824. * Returns the current {@link com.echo.holographlibrary.BarGraph BarGraph} .
  825. * @return BarGraph the current bar graph
  826. */
  827. public BarGraph getBarGraphView(){
  828. if (this.barGraph == null) {
  829. this.barGraph = new BarGraph(this.getActivity());
  830. LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  831. this.barGraph.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT ));
  832. plotLayout.addView(this.barGraph);
  833. this.barGraph.setShowBarText(false);
  834. this.barGraph.setPopupImageID(R.drawable.popup_black);
  835. this.barGraph.setOnBarClickedListener(new BarGraph.OnBarClickedListener() {
  836. @Override
  837. public void onClick(int i) {
  838. StatisticsFragment.this.onBarClick(i);
  839. }
  840. });
  841. }
  842. return this.barGraph;
  843. }
  844. /*
  845. * FEED PLOTS WITH DATA
  846. * */
  847. /**
  848. * Sets the data for the given PieGraph
  849. * @param piegraph {@link com.echo.holographlibrary.PieGraph PieGraph}
  850. */
  851. public void setPieGraphData(PieGraph piegraph){
  852. this.currentData = this.getPieData();
  853. if (this.currentData == null){
  854. this.currentData = new ArrayList<PlotComparisonItem>();
  855. }
  856. piegraph.removeSlices();
  857. for (PlotComparisonItem item : this.currentData){
  858. PieSlice slice = new PieSlice();
  859. slice.setColor(item.getColor());
  860. Double value2 = (Double) item.getValue2();
  861. float v = value2.floatValue();
  862. slice.setValue(v);
  863. slice.setTitle(item.getTitle());
  864. piegraph.addSlice(slice);
  865. }
  866. //piegraph.invalidate();
  867. }
  868. /**
  869. * Sets the data for the given LineGraph
  870. * @param linegraph {@link LineGraph Linegraph}
  871. */
  872. public void setLineGraphData(LineGraph linegraph){
  873. this.currentData = this.getLineData();
  874. if (this.currentData == null){
  875. this.currentData = new ArrayList<PlotComparisonItem>();
  876. }
  877. linegraph.removeAllLines();
  878. double rangeMax_Y = 0;
  879. double rangeMin_Y = 0;
  880. double rangeMax_X = 0;
  881. double rangeMin_X = 0;
  882. int count = 0;
  883. for (PlotComparisonItem lineItem : this.currentData){
  884. ArrayList<PlotComparisonItem> data = lineItem.getChildItems();
  885. //int index = 0;
  886. Line l = new Line();
  887. int lineColor = lineItem.getColor();
  888. l.setColor(lineColor);
  889. for (PlotComparisonItem pointItem : data){
  890. LinePoint p = new LinePoint();
  891. p.setX(pointItem.getValue1());
  892. Double value2 = pointItem.getValue2();
  893. p.setY(value2);
  894. p.setColor(lineColor);
  895. l.addPoint(p);
  896. rangeMax_Y = Math.max(pointItem.getValue2(), rangeMax_Y);
  897. rangeMax_X = Math.max(pointItem.getValue1(), rangeMax_X);
  898. if (count != 0){
  899. rangeMin_Y = Math.min(pointItem.getValue2(), rangeMin_Y);
  900. rangeMin_X = Math.min(pointItem.getValue1(), rangeMin_X);
  901. } else {
  902. rangeMin_Y = pointItem.getValue2();
  903. rangeMin_X = pointItem.getValue1();
  904. }
  905. //index++;
  906. count++;
  907. }
  908. linegraph.addLine(l);
  909. }
  910. // add a bit more space
  911. rangeMax_Y++;
  912. rangeMin_Y--;
  913. boolean shouldUseDate = this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerDate);
  914. if (shouldUseDate){
  915. linegraph.resetXLimits();
  916. if (this.filter.hasBelowTimestamp()){
  917. rangeMax_X = Math.max(this.filter.belowTimestamp, rangeMax_X);
  918. }
  919. if (this.filter.hasAboveTimestamp()){
  920. rangeMin_X = Math.min(this.filter.aboveTimestamp, rangeMin_X);
  921. }
  922. if (rangeMax_X == rangeMin_X){
  923. double aDay = 60*60*24*1000;
  924. rangeMax_X+= aDay;
  925. rangeMin_X-= aDay;
  926. }
  927. double stepRange = (rangeMax_X - rangeMin_X)/(60*60*24*1000);
  928. linegraph.setxAxisStep(Math.max(1, (float) Math.min(stepRange, 4)));
  929. linegraph.setRangeX(rangeMin_X , rangeMax_X);
  930. linegraph.setConverter(new LineGraph.AxisDataConverter() {
  931. @Override
  932. public String convertDataForX_Position(double x) {
  933. return StatisticsFragment.this.getDateAsDayString((long)x);
  934. }
  935. @Override
  936. public String convertDataForY_Position(double y){
  937. return "" + (long)y;
  938. }
  939. });
  940. } else {
  941. linegraph.setxAxisStep(12.f);
  942. linegraph.setRangeX(0, 24);
  943. linegraph.setConverter(null);
  944. }
  945. int maxY = (int)(rangeMax_Y - rangeMin_Y);
  946. linegraph.setYAxisStep(Math.min(maxY, 5));
  947. int yStep = (int)linegraph.getyAxisStep();
  948. if ((maxY % yStep) != 0) {
  949. maxY = maxY + (yStep - (maxY % yStep));
  950. }
  951. rangeMax_Y = rangeMin_Y + maxY;
  952. linegraph.setRangeY(rangeMin_Y, rangeMax_Y);
  953. linegraph.setLineToFill(0);
  954. }
  955. /**
  956. * Set the graph data to the given graph
  957. * @param bargraph {@link BarGraph BarGraph}
  958. * */
  959. public void setBarGraphData(BarGraph bargraph){
  960. this.currentData = this.getBarData();
  961. if (this.currentData == null){
  962. this.currentData = new ArrayList<PlotComparisonItem>();
  963. }
  964. ArrayList<Bar> bars = new ArrayList<Bar>();
  965. for (PlotComparisonItem item : this.currentData){
  966. Bar d = new Bar();
  967. d.setColor(item.getColor());
  968. Long value2 = item.getValue2().longValue();
  969. d.setName("");
  970. d.setValue(value2.floatValue());
  971. bars.add(d);
  972. }
  973. barGraph.setBars(bars);
  974. }
  975. /*
  976. *
  977. * FETCH & ACTUALISE RECORD DATA
  978. *
  979. * */
  980. /**
  981. * Returns the DataBaseHandler result for the current filter.
  982. * @return records {@link java.util.ArrayList}, {@link Record Record}
  983. */
  984. public ArrayList<Record> getFetchedRecords(){
  985. if (this.filter == null) this.clearFilter();
  986. return this.dbh.getRecordsForFilter(this.filter);
  987. }
  988. /**Actualises the current plot in a background thread.*/
  989. public void actualiseCurrentPlot(){
  990. this.spinner.setVisibility(View.VISIBLE);
  991. this.actualiseFilterButton();
  992. LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  993. if (this.barGraph != null)
  994. this.barGraph.setVisibility(View.GONE);
  995. if (this.lineGraph != null)
  996. this.lineGraph.setVisibility(View.GONE);
  997. if (this.pieGraph != null)
  998. this.pieGraph.setVisibility(View.GONE);
  999. View plot = this.currentPlotView;
  1000. if (plot == null){
  1001. this.currentPlotView = this.getPieGraphView();
  1002. plot = this.currentPlotView;
  1003. }
  1004. if (plot.getParent() != null && !plot.getParent().equals(plotLayout)){
  1005. LinearLayout linLayout = (LinearLayout)plot.getParent();
  1006. linLayout.removeView(plot);
  1007. plot.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT ));
  1008. plotLayout.addView(plot);
  1009. }
  1010. this.currentPlotView = plot;
  1011. final LinearLayout thePlotlayout = plotLayout;
  1012. if (this.loader != null && this.loader.isAlive()) this.loader.interrupt();
  1013. this.loader = null;
  1014. this.loader = new Thread(new Runnable() {
  1015. @Override
  1016. public void run() {
  1017. this.loadDataInBackground();
  1018. this.actualiseUI();
  1019. }
  1020. private void loadDataInBackground(){
  1021. View plot = StatisticsFragment.this.currentPlotView;
  1022. if (plot instanceof PieGraph){
  1023. PieGraph pie = (PieGraph) plot;
  1024. StatisticsFragment.this.setPieGraphData(pie);
  1025. }
  1026. if (plot instanceof BarGraph){
  1027. BarGraph bar = (BarGraph) plot;
  1028. StatisticsFragment.this.setBarGraphData(bar);
  1029. }
  1030. if (plot instanceof LineGraph){
  1031. LineGraph line = (LineGraph)plot;
  1032. StatisticsFragment.this.setLineGraphData(line);
  1033. }
  1034. }
  1035. private void actualiseUI(){
  1036. Activity actv = StatisticsFragment.this.getActivity();
  1037. if (actv != null){
  1038. actv.runOnUiThread(new Runnable() {
  1039. @Override
  1040. public void run() {
  1041. // SET VISIBILITY
  1042. View plot = StatisticsFragment.this.currentPlotView;
  1043. if (plot instanceof PieGraph){
  1044. // HIDE FILTER BUTTON
  1045. ImageButton filterButton = StatisticsFragment.this.getFilterButton();
  1046. if (filterButton != null) filterButton.setVisibility(View.GONE);
  1047. } else {
  1048. if (StatisticsFragment.this.pieGraph != null){
  1049. StatisticsFragment.this.pieGraph.setVisibility(View.GONE);
  1050. if (StatisticsFragment.this.pieGraph.getParent() != null){
  1051. thePlotlayout.removeView(StatisticsFragment.this.pieGraph);
  1052. }
  1053. }
  1054. // SHOW FILTER BUTTON
  1055. ImageButton filterButton = StatisticsFragment.this.getFilterButton();
  1056. if (filterButton != null) filterButton.setVisibility(View.VISIBLE);
  1057. }
  1058. if (! (plot instanceof BarGraph)){
  1059. if (StatisticsFragment.this.barGraph != null){
  1060. StatisticsFragment.this.barGraph.setVisibility(View.GONE);
  1061. if (StatisticsFragment.this.barGraph.getParent() != null){
  1062. thePlotlayout.removeView(StatisticsFragment.this.barGraph);
  1063. }
  1064. }
  1065. }
  1066. if (!(plot instanceof LineGraph)){
  1067. if (StatisticsFragment.this.lineGraph != null){
  1068. StatisticsFragment.this.lineGraph.setVisibility(View.GONE);
  1069. if (StatisticsFragment.this.lineGraph.getParent() != null){
  1070. thePlotlayout.removeView(StatisticsFragment.this.lineGraph);
  1071. }
  1072. }
  1073. }
  1074. plot.setVisibility(View.VISIBLE);
  1075. if (plot.getParent() == null){
  1076. thePlotlayout.addView(plot);
  1077. }
  1078. StatisticsFragment.this.actualiseLegendList();
  1079. StatisticsFragment.this.currentPlotView.bringToFront();
  1080. StatisticsFragment.this.currentPlotView.invalidate();
  1081. StatisticsFragment.this.spinner.setVisibility(View.GONE);
  1082. StatisticsFragment.this.showEmptyDataNotification();
  1083. }
  1084. });
  1085. }
  1086. }
  1087. });
  1088. this.loader.start();
  1089. }
  1090. /**
  1091. * Shows a small toast if the data to show is empty (no records).
  1092. */
  1093. private void showEmptyDataNotification(){
  1094. if (this.noDataNotificationToast == null){
  1095. this.noDataNotificationToast = Toast.makeText(getApplicationContext(), R.string.no_data_notification, Toast.LENGTH_SHORT);
  1096. }
  1097. if (this.getFilterButton().getVisibility() == View.VISIBLE){
  1098. this.noDataNotificationToast.setText(R.string.no_data_notification);
  1099. } else {
  1100. this.noDataNotificationToast.setText(R.string.no_data_notification_no_filter);
  1101. }
  1102. if (this.currentData == null || this.currentData.isEmpty()){
  1103. this.noDataNotificationToast.show();
  1104. }
  1105. }
  1106. /** Calculates and returns the data for the piegraph
  1107. * @return ArrayList<PlotComparisonItem> data */
  1108. public ArrayList<PlotComparisonItem> getPieData(){
  1109. // DEFAULT
  1110. return this.attacksPerProtocols();
  1111. }
  1112. /** Calculates and returns the data for the bargraph.
  1113. * @return ArrayList<PlotComparisonItem> data */
  1114. public ArrayList<PlotComparisonItem> getBarData(){
  1115. String protocol = this.getCurrentSelectedProtocol();
  1116. if (protocol.length() > 0){
  1117. if (this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerESSID)){
  1118. return this.attacksPerESSID(protocol);
  1119. }
  1120. // DEFAULT
  1121. return this.attacksPerBSSID(protocol);
  1122. }
  1123. // Nothing available
  1124. return new ArrayList<PlotComparisonItem>();
  1125. }
  1126. /** Calculates and returns the data for the linegraph
  1127. * @return ArrayList<PlotComparisonItem> data */
  1128. public ArrayList<PlotComparisonItem> getLineData(){
  1129. return this.attacksPerTime();
  1130. }
  1131. /*
  1132. * DATA SOURCE
  1133. * */
  1134. /*PROTOCOLS OVERVIEW*/
  1135. /**
  1136. * Returns the attacks per protocols comparison result.
  1137. * The returned data is resized to the specified limit.
  1138. * @return ArrayList<PlotComparisonItem>
  1139. */
  1140. public synchronized ArrayList<PlotComparisonItem> attacksPerProtocols(){
  1141. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  1142. int index = 0;
  1143. for (String title : this.getSelectedProtocolTitles()){
  1144. int attacksCount = this.dbh.getAttackPerProtocolCount(title);
  1145. if (attacksCount == 0) continue;
  1146. PlotComparisonItem item = new PlotComparisonItem(title,this.getColor(index), 0., (double) attacksCount);
  1147. plotItems.add(item);
  1148. index++;
  1149. }
  1150. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  1151. @Override
  1152. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  1153. return s2.getValue2().compareTo(s1.getValue2());
  1154. }
  1155. });
  1156. return this.resizeData(plotItems);
  1157. }
  1158. /*
  1159. * LINE PLOT DATA
  1160. */
  1161. /**
  1162. * Returns the line graph data responding to the selectedCompareData key.
  1163. * The returned data is resized to the specified limit.
  1164. * @return plotItems {@link PlotComparisonItem PlotComparisonItems}
  1165. */
  1166. public ArrayList<PlotComparisonItem> attacksPerTime(){
  1167. HashMap<String,HashMap<Long, ArrayList<Record> > > lineMap = new HashMap<String, HashMap<Long, ArrayList<Record>>>();
  1168. boolean shouldUseDate = this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerDate);
  1169. ArrayList<Record> records = this.getFetchedRecords();
  1170. for (Record record : records){
  1171. long timestamp = record.getTimestamp();
  1172. long time = 0;
  1173. if (shouldUseDate){
  1174. time = this.getDateFromMilliseconds(timestamp);
  1175. } else {
  1176. time = this.getDayHourFromDate(timestamp);
  1177. }
  1178. // GET CORRECT MAP
  1179. HashMap<Long, ArrayList<Record> > recordMap;
  1180. String groupKey = record.getSsid();
  1181. if (lineMap.containsKey(groupKey)){
  1182. recordMap = lineMap.get(record.getSsid());
  1183. } else {
  1184. recordMap = new HashMap<Long, ArrayList<Record> >();
  1185. lineMap.put(groupKey, recordMap);
  1186. }
  1187. // GET LIST OF RECORDS
  1188. ArrayList<Record> list;
  1189. if (recordMap.containsKey(time)){
  1190. list = recordMap.get(time);
  1191. } else {
  1192. list = new ArrayList<Record>();
  1193. recordMap.put(time, list);
  1194. }
  1195. list.add(record);
  1196. }
  1197. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  1198. int index = 0;
  1199. for (String groupKey : lineMap.keySet()){
  1200. HashMap<Long, ArrayList<Record> > recordMap = lineMap.get(groupKey);
  1201. ArrayList<PlotComparisonItem> singleLineItems = new ArrayList<PlotComparisonItem>();
  1202. int numbOfAttacks = 0;
  1203. for (long time : recordMap.keySet()){
  1204. ArrayList<Record>list = recordMap.get(time);
  1205. if (list.size() == 0) continue;
  1206. PlotComparisonItem item = new PlotComparisonItem(this.getHourAsTimeString(time), 0 , (double)time, (double) list.size());
  1207. singleLineItems.add(item);
  1208. numbOfAttacks +=list.size();
  1209. }
  1210. Collections.sort(singleLineItems, new Comparator<PlotComparisonItem>() {
  1211. @Override
  1212. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  1213. return s1.getValue1().compareTo(s2.getValue1());
  1214. }
  1215. });
  1216. double itemValue = (((double)numbOfAttacks / (double)records.size())*100.);
  1217. PlotComparisonItem item = new PlotComparisonItem(groupKey, this.getColor(index), (double) numbOfAttacks, itemValue);
  1218. item.setChildItems(singleLineItems);
  1219. plotItems.add(item);
  1220. index++;
  1221. }
  1222. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  1223. @Override
  1224. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  1225. return s2.getValue2().compareTo(s1.getValue2());
  1226. }
  1227. });
  1228. return plotItems;
  1229. }
  1230. /*
  1231. * BAR PLOT DATA
  1232. */
  1233. /**Returns plotitems for the comparison "attacks per bssid"
  1234. * The returned data is resized to the specified limit.
  1235. * @return ArrayList of {@link PlotComparisonItem PlotComparisonItems}
  1236. */
  1237. public ArrayList<PlotComparisonItem> attacksPerBSSID(String protocol){
  1238. LogFilter filter = new LogFilter();
  1239. ArrayList<String> protocollist = new ArrayList<String>();
  1240. filter.setAboveTimestamp(this.filter.getAboveTimestamp());
  1241. filter.setBelowTimestamp(this.filter.getBelowTimestamp());
  1242. filter.setBSSIDs(this.filter.getBSSIDs());
  1243. protocollist.add(protocol);
  1244. filter.setProtocols(protocollist);
  1245. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  1246. HashMap<String, Integer> recordMap = new HashMap<String, Integer>();
  1247. ArrayList<Record> records = this.dbh.getRecordsForFilter(filter);
  1248. for (Record record : records){
  1249. int count = 0;
  1250. if (recordMap.containsKey(record.getBssid())){
  1251. count = recordMap.get(record.getBssid());
  1252. }
  1253. count++;
  1254. recordMap.put(record.getBssid(), count);
  1255. }
  1256. int index = 0;
  1257. for (String key : recordMap.keySet()){
  1258. double value = (double)recordMap.get(key);
  1259. if (value == 0.) continue;
  1260. PlotComparisonItem item = new PlotComparisonItem(key, this.getColor(index), 0., value);
  1261. plotItems.add(item);
  1262. index++;
  1263. }
  1264. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  1265. @Override
  1266. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  1267. return s2.getValue2().compareTo(s1.getValue2());
  1268. }
  1269. });
  1270. return this.resizeData(plotItems);
  1271. }
  1272. /**Returns plotitems for the comparison "attacks per essid"
  1273. * @return ArrayList of {@link PlotComparisonItem PlotComparisonItems}
  1274. */
  1275. public ArrayList<PlotComparisonItem> attacksPerESSID(String protocol){
  1276. LogFilter filter = new LogFilter();
  1277. filter.setAboveTimestamp(this.filter.getAboveTimestamp());
  1278. filter.setBelowTimestamp(this.filter.getBelowTimestamp());
  1279. filter.setESSIDs(this.filter.getESSIDs());
  1280. ArrayList<String> protocollist = new ArrayList<String>();
  1281. protocollist.add(protocol);
  1282. filter.setProtocols(protocollist);
  1283. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  1284. HashMap<String, Integer> recordMap = new HashMap<String, Integer>();
  1285. ArrayList<Record> records = this.dbh.getRecordsForFilter(filter);
  1286. for (Record record : records){
  1287. int count = 0;
  1288. if (recordMap.containsKey(record.getSsid())){
  1289. count = recordMap.get(record.getSsid());
  1290. }
  1291. count++;
  1292. recordMap.put(record.getSsid(), count);
  1293. }
  1294. int index = 0;
  1295. for (String key : recordMap.keySet()){
  1296. double value = (double)recordMap.get(key);
  1297. if (value == 0.) continue;
  1298. PlotComparisonItem item = new PlotComparisonItem(key,this.getColor(index), 0. ,value);
  1299. plotItems.add(item);
  1300. index++;
  1301. }
  1302. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  1303. @Override
  1304. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  1305. return s2.getValue2().compareTo(s1.getValue2());
  1306. }
  1307. });
  1308. return this.resizeData(plotItems);
  1309. }
  1310. /**
  1311. * This will normalize the given list of plot items to the specified length of MAX_NUMBER_OF_CHART_OBJECTS.
  1312. * Creates an "others" group, containing all objects after the (MAX_NUMBER_OF_CHART_OBJECTS - 1)th object from the given list.
  1313. * If the given list does contain MAX_NUMBER_OF_CHART_OBJECTS+1 or more objects, nothing will change.
  1314. *
  1315. * @param plotItems to normalize {@link PlotComparisonItem PlotComparisonItems}
  1316. * @return the normalized ArrayList of {@link PlotComparisonItem PlotComparisonItems}
  1317. */
  1318. private ArrayList<PlotComparisonItem> resizeData(ArrayList<PlotComparisonItem> plotItems){
  1319. if (plotItems != null){
  1320. if (plotItems.size() > MAX_NUMBER_OF_CHART_OBJECTS && MAX_NUMBER_OF_CHART_OBJECTS > 1){
  1321. ArrayList<PlotComparisonItem> copy = new ArrayList<PlotComparisonItem>();
  1322. ArrayList<PlotComparisonItem> others = new ArrayList<PlotComparisonItem>();
  1323. double valueOfOthers = 0;
  1324. for (int i = 0; i < plotItems.size(); i++){
  1325. if (i < MAX_NUMBER_OF_CHART_OBJECTS - 1){
  1326. PlotComparisonItem item = plotItems.get(i);
  1327. item.setColor(this.getColor(i));
  1328. copy.add(plotItems.get(i));
  1329. } else {
  1330. PlotComparisonItem item = plotItems.get(i);
  1331. others.add(item);
  1332. valueOfOthers+=item.getValue2();
  1333. }
  1334. }
  1335. PlotComparisonItem otherItem = new PlotComparisonItem(OTHER_CHART_TITLE, this.getOtherColor(), 0., valueOfOthers);
  1336. otherItem.setChildItems(others);
  1337. copy.add(otherItem);
  1338. Collections.sort(copy, new Comparator<PlotComparisonItem>() {
  1339. @Override
  1340. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  1341. return s2.getValue2().compareTo(s1.getValue2());
  1342. }
  1343. });
  1344. return copy;
  1345. }
  1346. }
  1347. return plotItems;
  1348. }
  1349. /*
  1350. * FILTER STUFF
  1351. * */
  1352. /**
  1353. * Returns the first selected protocol from all selected protocols.
  1354. * If no protocol is selected, it return the first protocol in the protocolTitles() list.
  1355. * @return String protocolName
  1356. * */
  1357. private String getCurrentSelectedProtocol(){
  1358. ArrayList<String> protocolTitles = this.getSelectedProtocolTitles();
  1359. if (protocolTitles != null && protocolTitles.size() != 0){
  1360. return protocolTitles.get(0);
  1361. }
  1362. return this.protocolTitles().get(0);
  1363. }
  1364. /**
  1365. * Return all Protocols
  1366. *
  1367. * @return ArrayList<String> protocolNames
  1368. * */
  1369. public ArrayList<String> protocolTitles(){
  1370. ArrayList<String> titles = new ArrayList<String>();
  1371. for (String protocol : this.getResources().getStringArray(
  1372. R.array.protocols)) {
  1373. titles.add(protocol);
  1374. }
  1375. titles.add("PORTSCAN");
  1376. return titles;
  1377. }
  1378. /**
  1379. * Returns a boolean array containing a bool value for each protocol. If the value is true, the filter object contains the protocol.
  1380. * The array sequence equates to the protocolTitles() list.
  1381. * @return boolean[] selection array
  1382. * */
  1383. public boolean[] selectedProtocols(){
  1384. ArrayList<String> protocols = this.protocolTitles();
  1385. boolean[] selected = new boolean[protocols.size()];
  1386. int i = 0;
  1387. for(String protocol : protocols){
  1388. selected[i] =(this.filter.protocols.contains(protocol));
  1389. i++;
  1390. }
  1391. return selected;
  1392. }
  1393. public ArrayList<String> getSelectedProtocolTitles(){
  1394. ArrayList<String> knownProtocols = this.protocolTitles();
  1395. if (this.filter.hasProtocols()){
  1396. ArrayList<String> titles = new ArrayList<String>();
  1397. int i =0;
  1398. for (boolean b : this.selectedProtocols()){
  1399. if (b){
  1400. String title = knownProtocols.get(i);
  1401. titles.add(title);
  1402. }
  1403. i++;
  1404. }
  1405. return titles;
  1406. }
  1407. return this.protocolTitles();
  1408. }
  1409. /*
  1410. *
  1411. * COLOR STUFF
  1412. *
  1413. * */
  1414. /** Returns the color for the other group
  1415. * @return int color*/
  1416. public int getOtherColor(){
  1417. return Color.argb(255, 80, 80, 80); // grey
  1418. }
  1419. /** Returns the color for the given index
  1420. * @return int color*/
  1421. public Integer getColor(int index) {
  1422. return ColorSequenceGenerator.getColorForIndex(index);
  1423. }
  1424. /** Returns the Plot layout.
  1425. *
  1426. * @return LinearLayout plot layout
  1427. */
  1428. public LinearLayout getPlotLayout(){
  1429. if (this.rootView != null){
  1430. return (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  1431. } else {
  1432. return null;
  1433. }
  1434. }
  1435. /*
  1436. *
  1437. * FILTER STUFF
  1438. *
  1439. * */
  1440. /**
  1441. * Returns true if the current filter is set for a given filter menu title.
  1442. * @param title of the filter menu item {@link String String}
  1443. * @return boolean b
  1444. */
  1445. private boolean isFilterSetForTitle(String title){
  1446. if (title.equals(FILTER_MENU_TITLE_BSSID)){
  1447. return this.filter.hasBSSIDs();
  1448. }
  1449. if (title.equals(FILTER_MENU_TITLE_ESSID)){
  1450. return this.filter.hasESSIDs();
  1451. }
  1452. if (title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
  1453. return (this.filter.getProtocols() != null && this.filter.hasProtocols() && this.filter.getProtocols().size() != this.protocolTitles().size());
  1454. }
  1455. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)){
  1456. return this.filter.hasBelowTimestamp();
  1457. }
  1458. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)){
  1459. return this.filter.hasAboveTimestamp();
  1460. }
  1461. return false;
  1462. }
  1463. /**
  1464. * Clears the current filter.
  1465. */
  1466. private void clearFilter(){
  1467. if(filter == null) this.filter = new LogFilter();
  1468. this.filter.clear();
  1469. }
  1470. /*
  1471. *
  1472. * DATE TRANSFORMATION
  1473. *
  1474. */
  1475. /**
  1476. * Returns the current hour from a date.
  1477. * @param timeInMillis long
  1478. * @return milliseconds in long
  1479. */
  1480. public long getDayHourFromDate(long timeInMillis){
  1481. Calendar calendar = Calendar.getInstance();
  1482. calendar.setTimeInMillis (timeInMillis);
  1483. int hour = calendar.get(Calendar.HOUR_OF_DAY);
  1484. //int min = calendar.get(Calendar.MINUTE);
  1485. return hour;
  1486. }
  1487. /**
  1488. * Returns the current date without the seconds, minutes, hours.
  1489. * @param timeInMillis long
  1490. * @return long date with time 00:00:00
  1491. * */
  1492. public long getDateFromMilliseconds(long timeInMillis){
  1493. long millisInDay = 60 * 60 * 24 * 1000;
  1494. return (timeInMillis / millisInDay) * millisInDay;
  1495. }
  1496. /**
  1497. * Returns the given hour as a formated string.
  1498. * Format: "HH:00"
  1499. * */
  1500. private String getHourAsTimeString(long hour) {
  1501. return "" + hour + ":00";
  1502. }
  1503. //static final DateFormat dateFormat = new SimpleDateFormat("d.M.yy");
  1504. /**Returns a date as a formated string
  1505. * @param timeStamp date
  1506. * @return String date format is localised*/
  1507. @SuppressLint("SimpleDateFormat")
  1508. private String getDateAsDayString(long timeStamp) {
  1509. try {
  1510. Date netDate = (new Date(timeStamp));
  1511. DateFormat dateFormat = android.text.format.DateFormat.getMediumDateFormat(this.getActivity());
  1512. return dateFormat.format(netDate);
  1513. //return dateFormat.format(netDate);
  1514. } catch (Exception ex) {
  1515. return "xx";
  1516. }
  1517. }
  1518. @SuppressLint("SimpleDateFormat")
  1519. /**Returns a date as a formated string
  1520. * @param long date
  1521. * @return String date format (H:mm dd/MM/yyyy)*/
  1522. private String getDateAsString(long timeStamp) {
  1523. try {
  1524. DateFormat sdf = new SimpleDateFormat("H:mm dd.MM.yyyy");
  1525. Date netDate = (new Date(timeStamp));
  1526. return sdf.format(netDate);
  1527. } catch (Exception ex) {
  1528. return "xx";
  1529. }
  1530. }
  1531. /**
  1532. * USERINTERACTION
  1533. */
  1534. /**
  1535. * Will be called if the users taps on a list row.
  1536. * @param index int
  1537. */
  1538. private void userTappedOnLegendItem(int index){
  1539. if (index < this.currentData.size()){
  1540. PlotComparisonItem item = this.currentData.get(index);
  1541. ArrayList<String> selectedData;
  1542. String groupingKey = null;
  1543. selectedData = new ArrayList<String>();
  1544. if (item.getChildItems() == null){
  1545. selectedData.add(item.getTitle());
  1546. } else {
  1547. for (PlotComparisonItem other : item.getChildItems()){
  1548. selectedData.add(other.getTitle());
  1549. }
  1550. }
  1551. LogFilter filter = new LogFilter();
  1552. if (this.currentPlotView instanceof PieGraph){
  1553. filter.setProtocols(selectedData);
  1554. if(selectedData != null && selectedData.size() > 1){
  1555. groupingKey = MainActivity.getInstance().getResources().getString(R.string.rec_protocol);
  1556. }
  1557. }
  1558. if (this.currentPlotView instanceof BarGraph){
  1559. if (this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerESSID)){
  1560. filter.setESSIDs(selectedData);
  1561. groupingKey = MainActivity.getInstance().getResources().getString(R.string.ESSID);
  1562. } else {
  1563. filter.setBSSIDs(selectedData);
  1564. groupingKey = MainActivity.getInstance().getResources().getString(R.string.BSSID);
  1565. }
  1566. ArrayList<String> currentSelectedProtocol = new ArrayList<String>();
  1567. currentSelectedProtocol.add(this.getCurrentSelectedProtocol());
  1568. filter.setProtocols(currentSelectedProtocol);
  1569. }
  1570. if (this.currentPlotView instanceof LineGraph){
  1571. selectedData = new ArrayList<String>();
  1572. selectedData.add(item.getTitle());
  1573. filter.setESSIDs(selectedData);
  1574. filter.setProtocols(this.filter.getProtocols());
  1575. groupingKey = MainActivity.getInstance().getResources().getString(R.string.ESSID);
  1576. }
  1577. if (this.filter.hasATimestamp()){
  1578. filter.setAboveTimestamp(this.filter.getAboveTimestamp());
  1579. filter.setBelowTimestamp(this.filter.getBelowTimestamp());
  1580. }
  1581. this.pushRecordOverviewForFilter(filter, groupingKey);
  1582. }
  1583. }
  1584. /**Will be called if the user clicked on a slice
  1585. * @param index of the slice (int)*/
  1586. public void onSliceClick(int index){
  1587. }
  1588. /**Will be called if the user clicked on a bar
  1589. * @param index of the bar (int)*/
  1590. public void onBarClick(int index){
  1591. this.userTappedOnLegendItem(index);
  1592. }
  1593. /**
  1594. * Displays a record over view fragment.
  1595. * @param filter {@link de.tudarmstadt.informatik.hostage.ui2.model.LogFilter LogFilter}
  1596. * @param groupingKey String, key to group the attack list in the RecordOverview
  1597. */
  1598. private void pushRecordOverviewForFilter(LogFilter filter, String groupingKey){
  1599. // Get the FragmentManager from the MainActivity.
  1600. // The FragmentManager handles every pushing / popping action of a Fragment.
  1601. FragmentManager fm = this.getActivity().getFragmentManager();
  1602. if (fm != null){
  1603. // Create a new instance of your Fragment.
  1604. RecordOverviewFragment newFragment = new RecordOverviewFragment();
  1605. // Set true, if the user can navigate backwards from the new pushed fragment.
  1606. newFragment.setUpNavigatible(true);
  1607. // Set a Filter Object if needed.
  1608. // Otherwise the RecordOverviewFragment will display all possible records / attacks.
  1609. // With the filter object you also can change the sorting, by changing the sorttype of it.
  1610. if (filter != null) newFragment.setFilter(filter);
  1611. // Set a grouping key, if you want a other grouping behaviour than the default (timestamp).
  1612. if (groupingKey != null && groupingKey.length() != 0) newFragment.setGroupKey(groupingKey);
  1613. // Push new Fragment
  1614. MainActivity.getInstance().injectFragment(newFragment);
  1615. }
  1616. }
  1617. }