StatisticsFragment.java 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830
  1. package de.tudarmstadt.informatik.hostage.ui.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.ui.activity.MainActivity;
  42. import de.tudarmstadt.informatik.hostage.ui.adapter.StatisticListAdapter;
  43. import de.tudarmstadt.informatik.hostage.ui.dialog.ChecklistDialog;
  44. import de.tudarmstadt.informatik.hostage.ui.dialog.DateTimeDialogFragment;
  45. import de.tudarmstadt.informatik.hostage.ui.helper.ColorSequenceGenerator;
  46. import de.tudarmstadt.informatik.hostage.ui.model.PlotComparisonItem;
  47. import de.tudarmstadt.informatik.hostage.ui.popup.AbstractPopup;
  48. import de.tudarmstadt.informatik.hostage.ui.popup.AbstractPopupItem;
  49. import de.tudarmstadt.informatik.hostage.ui.popup.SimplePopupItem;
  50. import de.tudarmstadt.informatik.hostage.ui.popup.SimplePopupTable;
  51. import de.tudarmstadt.informatik.hostage.ui.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 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.ui.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.ui.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. return titles;
  1376. }
  1377. /**
  1378. * Returns a boolean array containing a bool value for each protocol. If the value is true, the filter object contains the protocol.
  1379. * The array sequence equates to the protocolTitles() list.
  1380. * @return boolean[] selection array
  1381. * */
  1382. public boolean[] selectedProtocols(){
  1383. ArrayList<String> protocols = this.protocolTitles();
  1384. boolean[] selected = new boolean[protocols.size()];
  1385. int i = 0;
  1386. for(String protocol : protocols){
  1387. selected[i] =(this.filter.protocols.contains(protocol));
  1388. i++;
  1389. }
  1390. return selected;
  1391. }
  1392. public ArrayList<String> getSelectedProtocolTitles(){
  1393. ArrayList<String> knownProtocols = this.protocolTitles();
  1394. if (this.filter.hasProtocols()){
  1395. ArrayList<String> titles = new ArrayList<String>();
  1396. int i =0;
  1397. for (boolean b : this.selectedProtocols()){
  1398. if (b){
  1399. String title = knownProtocols.get(i);
  1400. titles.add(title);
  1401. }
  1402. i++;
  1403. }
  1404. return titles;
  1405. }
  1406. return this.protocolTitles();
  1407. }
  1408. /*
  1409. *
  1410. * COLOR STUFF
  1411. *
  1412. * */
  1413. /** Returns the color for the other group
  1414. * @return int color*/
  1415. public int getOtherColor(){
  1416. return Color.argb(255, 80, 80, 80); // grey
  1417. }
  1418. /** Returns the color for the given index
  1419. * @return int color*/
  1420. public Integer getColor(int index) {
  1421. return ColorSequenceGenerator.getColorForIndex(index);
  1422. }
  1423. /** Returns the Plot layout.
  1424. *
  1425. * @return LinearLayout plot layout
  1426. */
  1427. public LinearLayout getPlotLayout(){
  1428. if (this.rootView != null){
  1429. return (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  1430. } else {
  1431. return null;
  1432. }
  1433. }
  1434. /*
  1435. *
  1436. * FILTER STUFF
  1437. *
  1438. * */
  1439. /**
  1440. * Returns true if the current filter is set for a given filter menu title.
  1441. * @param title of the filter menu item {@link String String}
  1442. * @return boolean b
  1443. */
  1444. private boolean isFilterSetForTitle(String title){
  1445. if (title.equals(FILTER_MENU_TITLE_BSSID)){
  1446. return this.filter.hasBSSIDs();
  1447. }
  1448. if (title.equals(FILTER_MENU_TITLE_ESSID)){
  1449. return this.filter.hasESSIDs();
  1450. }
  1451. if (title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
  1452. return (this.filter.getProtocols() != null && this.filter.hasProtocols() && this.filter.getProtocols().size() != this.protocolTitles().size());
  1453. }
  1454. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)){
  1455. return this.filter.hasBelowTimestamp();
  1456. }
  1457. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)){
  1458. return this.filter.hasAboveTimestamp();
  1459. }
  1460. return false;
  1461. }
  1462. /**
  1463. * Clears the current filter.
  1464. */
  1465. private void clearFilter(){
  1466. if(filter == null) this.filter = new LogFilter();
  1467. this.filter.clear();
  1468. }
  1469. /*
  1470. *
  1471. * DATE TRANSFORMATION
  1472. *
  1473. */
  1474. /**
  1475. * Returns the current hour from a date.
  1476. * @param timeInMillis long
  1477. * @return milliseconds in long
  1478. */
  1479. public long getDayHourFromDate(long timeInMillis){
  1480. Calendar calendar = Calendar.getInstance();
  1481. calendar.setTimeInMillis (timeInMillis);
  1482. int hour = calendar.get(Calendar.HOUR_OF_DAY);
  1483. //int min = calendar.get(Calendar.MINUTE);
  1484. return hour;
  1485. }
  1486. /**
  1487. * Returns the current date without the seconds, minutes, hours.
  1488. * @param timeInMillis long
  1489. * @return long date with time 00:00:00
  1490. * */
  1491. public long getDateFromMilliseconds(long timeInMillis){
  1492. long millisInDay = 60 * 60 * 24 * 1000;
  1493. return (timeInMillis / millisInDay) * millisInDay;
  1494. }
  1495. /**
  1496. * Returns the given hour as a formated string.
  1497. * Format: "HH:00"
  1498. * */
  1499. private String getHourAsTimeString(long hour) {
  1500. return "" + hour + ":00";
  1501. }
  1502. static final DateFormat dateFormat = new SimpleDateFormat("d.M.yy");
  1503. /**Returns a date as a formated string
  1504. * @param timeStamp date
  1505. * @return String date format (d.M.yy)*/
  1506. @SuppressLint("SimpleDateFormat")
  1507. private String getDateAsDayString(long timeStamp) {
  1508. try {
  1509. Date netDate = (new Date(timeStamp));
  1510. DateFormat dateFormat = android.text.format.DateFormat.getMediumDateFormat(this.getActivity());
  1511. return dateFormat.format(netDate);
  1512. //return dateFormat.format(netDate);
  1513. } catch (Exception ex) {
  1514. return "xx";
  1515. }
  1516. }
  1517. @SuppressLint("SimpleDateFormat")
  1518. /**Returns a date as a formated string
  1519. * @param long date
  1520. * @return String date format (H:mm dd/MM/yyyy)*/
  1521. private String getDateAsString(long timeStamp) {
  1522. try {
  1523. DateFormat sdf = new SimpleDateFormat("H:mm dd/MM/yyyy");
  1524. Date netDate = (new Date(timeStamp));
  1525. return sdf.format(netDate);
  1526. } catch (Exception ex) {
  1527. return "xx";
  1528. }
  1529. }
  1530. /**
  1531. * USERINTERACTION
  1532. */
  1533. /**
  1534. * Will be called if the users taps on a list row.
  1535. * @param index int
  1536. */
  1537. private void userTappedOnLegendItem(int index){
  1538. if (index < this.currentData.size()){
  1539. PlotComparisonItem item = this.currentData.get(index);
  1540. ArrayList<String> selectedData;
  1541. String sortKey = null;
  1542. selectedData = new ArrayList<String>();
  1543. if (item.getChildItems() == null){
  1544. selectedData.add(item.getTitle());
  1545. } else {
  1546. for (PlotComparisonItem other : item.getChildItems()){
  1547. selectedData.add(other.getTitle());
  1548. }
  1549. }
  1550. LogFilter filter = new LogFilter();
  1551. if (this.currentPlotView instanceof PieGraph){
  1552. filter.setProtocols(selectedData);
  1553. }
  1554. if (this.currentPlotView instanceof BarGraph){
  1555. if (this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerESSID)){
  1556. filter.setESSIDs(selectedData);
  1557. sortKey = "ESSID";
  1558. } else {
  1559. filter.setBSSIDs(selectedData);
  1560. sortKey = "BSSID";
  1561. }
  1562. ArrayList<String> currentSelectedProtocol = new ArrayList<String>();
  1563. currentSelectedProtocol.add(this.getCurrentSelectedProtocol());
  1564. filter.setProtocols(currentSelectedProtocol);
  1565. }
  1566. if (this.currentPlotView instanceof LineGraph){
  1567. selectedData = new ArrayList<String>();
  1568. selectedData.add(item.getTitle());
  1569. filter.setESSIDs(selectedData);
  1570. filter.setProtocols(this.filter.getProtocols());
  1571. sortKey = "ESSID";
  1572. }
  1573. if (this.filter.hasATimestamp()){
  1574. filter.setAboveTimestamp(this.filter.getAboveTimestamp());
  1575. filter.setBelowTimestamp(this.filter.getBelowTimestamp());
  1576. }
  1577. this.pushRecordOverviewForFilter(filter, sortKey);
  1578. }
  1579. }
  1580. /**Will be called if the user clicked on a slice
  1581. * @param index of the slice (int)*/
  1582. public void onSliceClick(int index){
  1583. }
  1584. /**Will be called if the user clicked on a bar
  1585. * @param index of the bar (int)*/
  1586. public void onBarClick(int index){
  1587. this.userTappedOnLegendItem(index);
  1588. }
  1589. /**
  1590. * Displays a record over view fragment.
  1591. * @param filter {@link de.tudarmstadt.informatik.hostage.ui.LogFilter LogFilter}
  1592. * @param sortKey String
  1593. */
  1594. private void pushRecordOverviewForFilter(LogFilter filter, String sortKey){
  1595. FragmentManager fm = this.getActivity().getFragmentManager();
  1596. if (fm != null){
  1597. RecordOverviewFragment newFragment = new RecordOverviewFragment();
  1598. newFragment.setUpNavigatible(true);
  1599. newFragment.setFilter(filter);
  1600. if (sortKey != null && sortKey.length() != 0) newFragment.setGroupKey(sortKey);
  1601. MainActivity.getInstance().injectFragment(newFragment);
  1602. }
  1603. }
  1604. }