StatisticsFragment.java 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  1. package de.tudarmstadt.informatik.hostage.ui2.fragment;
  2. import java.text.DateFormat;
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.Calendar;
  6. import java.util.Collections;
  7. import java.util.Comparator;
  8. import java.util.Date;
  9. import java.util.HashMap;
  10. import android.annotation.SuppressLint;
  11. import android.app.Fragment;
  12. import android.app.FragmentManager;
  13. import android.content.Context;
  14. import android.content.Intent;
  15. import android.content.res.Configuration;
  16. import android.graphics.Color;
  17. import android.os.Bundle;
  18. import android.view.LayoutInflater;
  19. import android.view.View;
  20. import android.view.ViewGroup;
  21. import android.widget.AdapterView;
  22. import android.widget.ImageButton;
  23. import android.widget.LinearLayout;
  24. import android.widget.ListView;
  25. import android.widget.TextView;
  26. import com.echo.holographlibrary.Bar;
  27. import com.echo.holographlibrary.BarGraph;
  28. import com.echo.holographlibrary.Line;
  29. import com.echo.holographlibrary.LineGraph;
  30. import com.echo.holographlibrary.LinePoint;
  31. import com.echo.holographlibrary.PieGraph;
  32. import com.echo.holographlibrary.PieSlice;
  33. import de.tudarmstadt.informatik.hostage.R;
  34. import de.tudarmstadt.informatik.hostage.logging.Record;
  35. import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
  36. import de.tudarmstadt.informatik.hostage.ui.LogFilter;
  37. import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
  38. import de.tudarmstadt.informatik.hostage.ui2.adapter.StatisticListAdapter;
  39. import de.tudarmstadt.informatik.hostage.ui2.dialog.ChecklistDialog;
  40. import de.tudarmstadt.informatik.hostage.ui2.dialog.DateTimeDialogFragment;
  41. import de.tudarmstadt.informatik.hostage.ui2.helper.ColorSequenceGenerator;
  42. import de.tudarmstadt.informatik.hostage.ui2.model.PlotComparisonItem;
  43. import de.tudarmstadt.informatik.hostage.ui2.popup.AbstractPopup;
  44. import de.tudarmstadt.informatik.hostage.ui2.popup.AbstractPopupItem;
  45. import de.tudarmstadt.informatik.hostage.ui2.popup.SimplePopupItem;
  46. import de.tudarmstadt.informatik.hostage.ui2.popup.SimplePopupTable;
  47. import de.tudarmstadt.informatik.hostage.ui2.popup.SplitPopupItem;
  48. /**
  49. * Created by Julien on 16.02.14.
  50. */
  51. public class StatisticsFragment extends Fragment implements ChecklistDialog.ChecklistDialogListener, DateTimeDialogFragment.DateTimeDialogFragmentListener {
  52. static final String FILTER_MENU_TITLE_BSSID = "BSSID";
  53. static final String FILTER_MENU_TITLE_ESSID = "ESSID";
  54. static final String FILTER_MENU_TITLE_PROTOCOLS = MainActivity.getContext().getString(R.string.stats_protocols);
  55. static final String FILTER_MENU_TITLE_PROTOCOL = MainActivity.getContext().getString(R.string.rec_protocol);
  56. static final String FILTER_MENU_TITLE_TIMESTAMP_BELOW = MainActivity.getContext().getString(R.string.rec_latest);
  57. static final String FILTER_MENU_TITLE_TIMESTAMP_ABOVE = MainActivity.getContext().getString(R.string.rec_earliest);
  58. static final String FILTER_MENU_TITLE_REMOVE = MainActivity.getContext().getString(R.string.rec_reset_filter);
  59. static final String FILTER_MENU_POPUP_TITLE = MainActivity.getContext().getString(R.string.rec_filter_by);
  60. static final String MENU_TITLE_PROTOCOLS = MainActivity.getContext().getString(R.string.stats_protocols);
  61. static final String MENU_TITLE_NETWORK = MainActivity.getContext().getString(R.string.stats_networks);
  62. static final String MENU_TITLE_ATTACKS = MainActivity.getContext().getString(R.string.stats_attacks);
  63. static final String MENU_POPUP_TITLE = MainActivity.getContext().getString(R.string.stats_visualize);
  64. static final String CHART_TYPE_TITLE_BAR = MainActivity.getContext().getString(R.string.stats_bar_plot);
  65. static final String CHART_TYPE_TITLE_PIE = MainActivity.getContext().getString(R.string.stats_pie_plot);
  66. static final String CHART_TYPE_TITLE_LINE = MainActivity.getContext().getString(R.string.stats_line_plot);
  67. static final String DIALOG_PROTOCOLS_TITLE = MainActivity.getContext().getString(R.string.stats_select_protocol_data);
  68. static final String DIALOG_NETWORK_TITLE = MainActivity.getContext().getString(R.string.stats_select_network_data);
  69. static final String DIALOG_ATTACK_TITLE = MainActivity.getContext().getString(R.string.stats_select_attack_data);
  70. static final String COMPARE_TITLE_AttacksPerProtocol = MainActivity.getContext().getString(R.string.stats_attacks_protocol);
  71. static final String COMPARE_TITLE_UsesPerProtocol = MainActivity.getContext().getString(R.string.stats_uses_protocol);
  72. static final String COMPARE_TITLE_AttacksPerDate = MainActivity.getContext().getString(R.string.stats_attacks_date);
  73. static final String COMPARE_TITLE_AttacksPerTime = MainActivity.getContext().getString(R.string.stats_attacks_time);
  74. static final String COMPARE_TITLE_AttacksPerBSSID = MainActivity.getContext().getString(R.string.stats_attacks_bssid);
  75. static final String COMPARE_TITLE_AttacksPerESSID = MainActivity.getContext().getString(R.string.stats_attacks_essid);
  76. static final String FILTER_MENU_PROTOCOL_SINGLE_CHOICE_TITLE = MainActivity.getContext().getString(R.string.stats_select_protocol);
  77. static final String TABLE_HEADER_VALUE_TITLE_ATTACKS_COUNT = MainActivity.getContext().getString(R.string.stats_attacks_count);
  78. static final String TABLE_HEADER_VALUE_TITLE_ATTACKS_PERCENTAGE = MainActivity.getContext().getString(R.string.stats_per_cent_all);
  79. static final String OTHER_CHART_TITLE = MainActivity.getContext().getString(R.string.stats_other);
  80. // MINIMAL 2
  81. static int MAX_NUMBER_OF_CHART_OBJECTS = 6;
  82. private boolean wasBelowTimePicker;
  83. private LogFilter filter;
  84. private boolean showFilterButton;
  85. private PieGraph pieGraph;
  86. private LineGraph lineGraph;
  87. private BarGraph barGraph;
  88. private View rootView;
  89. private View currentPlotView;
  90. private ArrayList<Integer> colorList;
  91. private ArrayList<PlotComparisonItem> currentData;
  92. private HostageDBOpenHelper dbh;
  93. private ListView legendListView;
  94. private String selectedCompareData = COMPARE_TITLE_AttacksPerProtocol;
  95. public enum ChartType {
  96. PIE_CHART(0), BAR_CHART(1), LINE_CHART(2);
  97. private int value;
  98. private ChartType(int value) {
  99. this.value = value;
  100. }
  101. static public ChartType create(int value) {
  102. if (value < 0 || value >= ChartType.values().length)
  103. return ChartType.PIE_CHART;
  104. return ChartType.values()[value];
  105. }
  106. public String toString() {
  107. if (this.equals(ChartType.create(0))) {
  108. return CHART_TYPE_TITLE_PIE;
  109. }
  110. if (this.equals(ChartType.create(1))) {
  111. return CHART_TYPE_TITLE_BAR;
  112. }
  113. return CHART_TYPE_TITLE_LINE;
  114. }
  115. }
  116. private ImageButton getFilterButton() {
  117. return (ImageButton) this.rootView.findViewById(R.id.FilterButton);
  118. }
  119. public int getLayoutID() {
  120. return R.layout.fragment_statistics;
  121. }
  122. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  123. super.onCreateView(inflater, container, savedInstanceState);
  124. getActivity().setTitle(getResources().getString(R.string.drawer_statistics));
  125. dbh = new HostageDBOpenHelper(this.getBaseContext());
  126. // Get the message from the intent
  127. if (this.filter == null) {
  128. Intent intent = this.getActivity().getIntent();
  129. LogFilter filter = intent.getParcelableExtra(LogFilter.LOG_FILTER_INTENT_KEY);
  130. if (filter == null) {
  131. this.clearFilter();
  132. } else {
  133. this.filter = filter;
  134. }
  135. }
  136. this.rootView = inflater.inflate(this.getLayoutID(), container, false);
  137. this.configureRootView(this.rootView);
  138. return this.rootView;
  139. }
  140. @Override
  141. public void onCreate(Bundle savedInstanceState) {
  142. super.onCreate(savedInstanceState);
  143. setHasOptionsMenu(true);
  144. setRetainInstance(true);
  145. }
  146. @Override
  147. public void onConfigurationChanged(Configuration newConfig) {
  148. super.onConfigurationChanged(newConfig);
  149. LayoutInflater inflater = LayoutInflater.from(getActivity());
  150. ViewGroup container = (ViewGroup) this.getView();
  151. container.removeAllViewsInLayout();
  152. this.rootView = inflater.inflate(this.getLayoutID(), container, false);
  153. container.addView(this.rootView);
  154. this.configureRootView(this.rootView);
  155. }
  156. private Context getBaseContext() {
  157. return this.getActivity().getBaseContext();
  158. }
  159. private Context getApplicationContext() {
  160. return this.getActivity().getApplicationContext();
  161. }
  162. public void configureRootView(View rootView) {
  163. LinearLayout plotLayout = (LinearLayout) rootView.findViewById(R.id.plot_layout);
  164. plotLayout.setWillNotDraw(false);
  165. plotLayout.removeAllViews();
  166. plotLayout.setWillNotDraw(false);
  167. this.legendListView = (ListView) rootView.findViewById(R.id.legend_list_view);
  168. this.legendListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  169. @Override
  170. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  171. StatisticsFragment.this.userTappedOnLegendItem(i);
  172. }
  173. });
  174. rootView.setWillNotDraw(false);
  175. this.actualiseCurrentPlot();
  176. ImageButton visualButton = (ImageButton) rootView.findViewById(R.id.plot_data_button);
  177. visualButton.setOnClickListener(new View.OnClickListener() {
  178. public void onClick(View v) {
  179. StatisticsFragment.this.openBarSelectionMenuOnView(v);
  180. }
  181. });
  182. ImageButton filterButton = this.getFilterButton();
  183. filterButton.setOnClickListener(new View.OnClickListener() {
  184. public void onClick(View v) {
  185. StatisticsFragment.this.openFilterMenuOnView(v);
  186. }
  187. });
  188. if (this.currentPlotView instanceof BarGraph) {
  189. this.setTitle("" + this.getCurrentSelectedProtocol() + ": " + this.selectedCompareData);
  190. } else {
  191. this.setTitle(this.selectedCompareData);
  192. }
  193. }
  194. public void setTitle(String title) {
  195. TextView titleView = (TextView) this.rootView.findViewById(R.id.title_text_view);
  196. if (title != null && titleView != null) {
  197. titleView.setText(title);
  198. titleView.invalidate();
  199. }
  200. }
  201. public String getTitle() {
  202. TextView titleView = (TextView) this.rootView.findViewById(R.id.title_text_view);
  203. if (titleView != null) {
  204. return "" + titleView.getText();
  205. }
  206. return "";
  207. }
  208. public void onStart() {
  209. super.onStart();
  210. this.actualiseCurrentPlot();
  211. this.currentPlotView.invalidate();
  212. if (this.currentPlotView instanceof BarGraph) {
  213. this.setTitle("" + this.getCurrentSelectedProtocol() + ": " + this.selectedCompareData);
  214. } else {
  215. this.setTitle(this.selectedCompareData);
  216. }
  217. }
  218. public void setChartType(ChartType type) {
  219. boolean shouldChange = true;
  220. this.clearFilter();
  221. if (this.currentPlotView != null) {
  222. if (type == ChartType.PIE_CHART) {
  223. shouldChange = !(this.currentPlotView instanceof PieGraph);
  224. // SET FILTER BUTTON HIDDEN
  225. ImageButton filterButton = this.getFilterButton();
  226. if (filterButton != null)
  227. filterButton.setVisibility(View.GONE);
  228. } else {
  229. if (this.pieGraph != null)
  230. this.pieGraph.setVisibility(View.GONE);
  231. // SHOW FILTER BUTTON
  232. ImageButton filterButton = this.getFilterButton();
  233. if (filterButton != null)
  234. filterButton.setVisibility(View.VISIBLE);
  235. }
  236. if (type == ChartType.LINE_CHART) {
  237. shouldChange = !(this.currentPlotView instanceof LineGraph);
  238. } else {
  239. if (this.lineGraph != null)
  240. this.lineGraph.setVisibility(View.GONE);
  241. }
  242. if (type == ChartType.BAR_CHART) {
  243. shouldChange = !(this.currentPlotView instanceof BarGraph);
  244. } else {
  245. if (this.barGraph != null)
  246. this.barGraph.setVisibility(View.GONE);
  247. }
  248. }
  249. if (shouldChange) {
  250. this.currentPlotView = this.getPlotViewForType(type);
  251. this.currentPlotView.setVisibility(View.VISIBLE);
  252. }
  253. this.actualiseCurrentPlot();
  254. }
  255. public View getPlotViewForType(ChartType type) {
  256. switch (type) {
  257. case PIE_CHART:
  258. return this.getPieGraphView();
  259. case LINE_CHART:
  260. return this.getLineGraphView();
  261. default:
  262. return this.getBarGraphView();
  263. }
  264. }
  265. public void actualiseLegendList() {
  266. StatisticListAdapter adapter = new StatisticListAdapter(this.getApplicationContext(), this.currentData);
  267. if (this.currentPlotView instanceof LineGraph) {
  268. adapter.setValueFormatter(new StatisticListAdapter.ValueFormatter() {
  269. @Override
  270. public String convertValueForItemToString(PlotComparisonItem item) {
  271. return String.format("%.02f", item.getValue2()) + " %" + " " + "(" + (item.getValue1().intValue()) + ")";
  272. }
  273. });
  274. } else {
  275. adapter.setValueFormatter(new StatisticListAdapter.ValueFormatter() {
  276. @Override
  277. public String convertValueForItemToString(PlotComparisonItem item) {
  278. int v = (int) item.getValue2().intValue();
  279. return "" + v;
  280. }
  281. });
  282. }
  283. this.legendListView.setAdapter(adapter);
  284. TextView tableHeaderTitleView = (TextView) this.rootView.findViewById(R.id.table_header_title_textview);
  285. TextView tableHeaderValueView = (TextView) this.rootView.findViewById(R.id.table_header_value_textview);
  286. if (this.currentPlotView instanceof LineGraph) {
  287. tableHeaderTitleView.setText(FILTER_MENU_TITLE_ESSID);
  288. tableHeaderValueView.setText(TABLE_HEADER_VALUE_TITLE_ATTACKS_PERCENTAGE);
  289. }
  290. if (this.currentPlotView instanceof PieGraph) {
  291. tableHeaderTitleView.setText(FILTER_MENU_TITLE_PROTOCOL);
  292. tableHeaderValueView.setText(TABLE_HEADER_VALUE_TITLE_ATTACKS_COUNT);
  293. }
  294. if (this.currentPlotView instanceof BarGraph) {
  295. tableHeaderValueView.setText(TABLE_HEADER_VALUE_TITLE_ATTACKS_COUNT);
  296. if (this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerBSSID)) {
  297. tableHeaderTitleView.setText(FILTER_MENU_TITLE_BSSID);
  298. } else {
  299. tableHeaderTitleView.setText(FILTER_MENU_TITLE_ESSID);
  300. }
  301. }
  302. }
  303. /*
  304. * MENU
  305. */
  306. private void openBarSelectionMenuOnView(View anchorView) {
  307. SimplePopupTable visualiseMenu = new SimplePopupTable(this.getActivity(), new AbstractPopup.OnPopupItemClickListener() {
  308. public void onItemClick(Object ob) {
  309. if (ob instanceof AbstractPopupItem) {
  310. AbstractPopupItem item = (AbstractPopupItem) ob;
  311. StatisticsFragment.this.userSelectMenuItem(item);
  312. }
  313. }
  314. });
  315. visualiseMenu.setTitle(MENU_POPUP_TITLE);
  316. int id = 0;
  317. for (String title : StatisticsFragment.this.getMenuTitles()) {
  318. SimplePopupItem item = new SimplePopupItem(this.getActivity());
  319. item.setTitle(title);
  320. item.setItemId(id);
  321. item.setSelected(false);
  322. visualiseMenu.addItem(item);
  323. id++;
  324. }
  325. visualiseMenu.showOnView(anchorView);
  326. }
  327. private void userSelectMenuItem(AbstractPopupItem item) {
  328. // OPEN A DIALOG TO SPECIFY THE VISUALISE DATA
  329. if (item.getTitle().equals(MENU_TITLE_PROTOCOLS)) {
  330. ChartType chartType = ChartType.PIE_CHART;
  331. this.selectedCompareData = COMPARE_TITLE_AttacksPerProtocol;
  332. this.setChartType(chartType);
  333. this.setTitle(COMPARE_TITLE_AttacksPerProtocol);
  334. }
  335. if (item.getTitle().equals(MENU_TITLE_NETWORK)) {
  336. this.openNetworkDataDialog();
  337. }
  338. if (item.getTitle().equals(MENU_TITLE_ATTACKS)) {
  339. this.openAttackDataDialog();
  340. }
  341. }
  342. private ArrayList<String> getMenuTitles() {
  343. ArrayList<String> titles = new ArrayList<String>();
  344. titles.add(MENU_TITLE_PROTOCOLS);
  345. titles.add(MENU_TITLE_NETWORK);
  346. titles.add(MENU_TITLE_ATTACKS);
  347. return titles;
  348. }
  349. /*
  350. * PLOT DATA DIALOGS
  351. */
  352. private void openProtocolDataDialog() {
  353. ArrayList<String> titles = this.getDialogProtocolDataTitle();
  354. ChecklistDialog newFragment = new ChecklistDialog(DIALOG_PROTOCOLS_TITLE, titles, this.selectedData(titles), false, this);
  355. newFragment.show(this.getActivity().getFragmentManager(), DIALOG_PROTOCOLS_TITLE);
  356. }
  357. private void openNetworkDataDialog() {
  358. ArrayList<String> titles = this.getDialogNetworkDataTitle();
  359. ChecklistDialog newFragment = new ChecklistDialog(DIALOG_NETWORK_TITLE, titles, this.selectedData(titles), false, this);
  360. newFragment.show(this.getActivity().getFragmentManager(), DIALOG_NETWORK_TITLE);
  361. }
  362. private void openAttackDataDialog() {
  363. ArrayList<String> titles = this.getDialogAttackDataTitle();
  364. ChecklistDialog newFragment = new ChecklistDialog(DIALOG_ATTACK_TITLE, titles, this.selectedData(titles), false, this);
  365. newFragment.show(this.getActivity().getFragmentManager(), DIALOG_ATTACK_TITLE);
  366. }
  367. /*
  368. *
  369. * DIALOG ACTION METHODS
  370. */
  371. public void onDialogPositiveClick(ChecklistDialog dialog) {
  372. String title = dialog.getTitle();
  373. ArrayList<String> titles = dialog.getSelectedItemTitles();
  374. if (title.equals(FILTER_MENU_TITLE_PROTOCOLS)) {
  375. // titles = titles.size() == 0 ? this.protocolTitles() : titles;
  376. this.filter.setProtocols(titles);
  377. this.actualiseCurrentPlot();
  378. return;
  379. }
  380. if (title.equals(FILTER_MENU_PROTOCOL_SINGLE_CHOICE_TITLE)) {
  381. if (titles.size() == 0) {
  382. titles = new ArrayList<String>();
  383. titles.add(this.protocolTitles().get(0));
  384. }
  385. this.filter.setProtocols(titles);
  386. this.actualiseCurrentPlot();
  387. String fragTitle = "" + this.getCurrentSelectedProtocol() + ": " + this.selectedCompareData;
  388. this.setTitle(fragTitle);
  389. return;
  390. }
  391. if (title.equals(FILTER_MENU_TITLE_ESSID)) {
  392. this.filter.setESSIDs(titles);
  393. this.actualiseCurrentPlot();
  394. return;
  395. }
  396. if (title.equals(FILTER_MENU_TITLE_BSSID)) {
  397. this.filter.setBSSIDs(titles);
  398. this.actualiseCurrentPlot();
  399. return;
  400. }
  401. if (titles.size() != 0) {
  402. String data = titles.get(0);
  403. this.setTitle(data);
  404. if (title.equals(DIALOG_PROTOCOLS_TITLE)) {
  405. ChartType chartType = ChartType.PIE_CHART;
  406. this.selectedCompareData = data;
  407. this.setChartType(chartType);
  408. }
  409. if (title.equals(DIALOG_ATTACK_TITLE)) {
  410. ChartType chartType = ChartType.LINE_CHART;
  411. this.selectedCompareData = data;
  412. this.setChartType(chartType);
  413. }
  414. if (title.equals(DIALOG_NETWORK_TITLE)) {
  415. ChartType chartType = ChartType.BAR_CHART;
  416. this.selectedCompareData = data;
  417. this.setChartType(chartType);
  418. String fragTitle = "" + this.getCurrentSelectedProtocol() + ": " + this.selectedCompareData;
  419. this.setTitle(fragTitle);
  420. }
  421. }
  422. }
  423. public void onDialogNegativeClick(ChecklistDialog dialog) {
  424. }
  425. /*
  426. *
  427. * DIALOG DATA
  428. */
  429. private ArrayList<String> getDialogProtocolDataTitle() {
  430. ArrayList<String> data = new ArrayList<String>();
  431. data.add(COMPARE_TITLE_AttacksPerProtocol);
  432. data.add(COMPARE_TITLE_UsesPerProtocol);
  433. return data;
  434. }
  435. private ArrayList<String> getDialogAttackDataTitle() {
  436. ArrayList<String> data = new ArrayList<String>();
  437. data.add(COMPARE_TITLE_AttacksPerDate);
  438. data.add(COMPARE_TITLE_AttacksPerTime);
  439. return data;
  440. }
  441. private ArrayList<String> getDialogNetworkDataTitle() {
  442. ArrayList<String> data = new ArrayList<String>();
  443. data.add(COMPARE_TITLE_AttacksPerESSID);
  444. data.add(COMPARE_TITLE_AttacksPerBSSID);
  445. return data;
  446. }
  447. private boolean[] selectedData(ArrayList<String> data) {
  448. boolean[] selected = new boolean[data.size()];
  449. // SET DEFAULT
  450. selected[0] = true;
  451. return selected;
  452. }
  453. /*
  454. *
  455. * FILTER BUTTON
  456. */
  457. private void openFilterMenuOnView(View anchor) {
  458. SimplePopupTable filterMenu = new SimplePopupTable(this.getActivity(), new AbstractPopup.OnPopupItemClickListener() {
  459. public void onItemClick(Object ob) {
  460. if (ob instanceof AbstractPopupItem) {
  461. AbstractPopupItem item = (AbstractPopupItem) ob;
  462. StatisticsFragment.this.onFilterMenuItemSelected(item);
  463. }
  464. }
  465. });
  466. filterMenu.setTitle(FILTER_MENU_POPUP_TITLE);
  467. for (String title : StatisticsFragment.this.filterMenuTitles()) {
  468. AbstractPopupItem item = null;
  469. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW))
  470. continue;
  471. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)) {
  472. item = new SplitPopupItem(this.getActivity());
  473. item.setValue(SplitPopupItem.RIGHT_TITLE, FILTER_MENU_TITLE_TIMESTAMP_BELOW);
  474. item.setValue(SplitPopupItem.LEFT_TITLE, FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  475. if (this.filter.hasBelowTimestamp()) {
  476. item.setValue(SplitPopupItem.RIGHT_SUBTITLE, this.getDateAsString(this.filter.belowTimestamp));
  477. }
  478. if (this.filter.hasAboveTimestamp()) {
  479. item.setValue(SplitPopupItem.LEFT_SUBTITLE, this.getDateAsString(this.filter.aboveTimestamp));
  480. }
  481. } else {
  482. item = new SimplePopupItem(this.getActivity());
  483. item.setTitle(title);
  484. ((SimplePopupItem) item).setSelected(this.isFilterSetForTitle(title));
  485. }
  486. filterMenu.addItem(item);
  487. }
  488. filterMenu.showOnView(anchor);
  489. }
  490. private void onFilterMenuItemSelected(AbstractPopupItem item) {
  491. if (item instanceof SplitPopupItem) {
  492. SplitPopupItem sItem = (SplitPopupItem) item;
  493. this.wasBelowTimePicker = sItem.wasRightTouch;
  494. if (this.wasBelowTimePicker) {
  495. this.openTimestampToFilterDialog();
  496. } else {
  497. this.openTimestampFromFilterDialog();
  498. }
  499. return;
  500. }
  501. String title = item.getTitle();
  502. if (title.equals(FILTER_MENU_TITLE_ESSID)) {
  503. this.openESSIDFilterDialog();
  504. }
  505. if (title.equals(FILTER_MENU_TITLE_BSSID)) {
  506. this.openBSSIDFilterDialog();
  507. }
  508. if (title.equals(FILTER_MENU_TITLE_PROTOCOL)) {
  509. this.openFilterDialogSelectProtocol();
  510. }
  511. if (title.equals(FILTER_MENU_TITLE_PROTOCOLS)) {
  512. this.openProtocolsFilterDialog();
  513. }
  514. if (title.equals(FILTER_MENU_TITLE_REMOVE)) {
  515. this.clearFilter();
  516. this.actualiseCurrentPlot();
  517. }
  518. }
  519. private ArrayList<String> filterMenuTitles() {
  520. ArrayList<String> titles = new ArrayList<String>();
  521. if (this.currentPlotView instanceof LineGraph) {
  522. titles.add(FILTER_MENU_TITLE_ESSID);
  523. titles.add(FILTER_MENU_TITLE_PROTOCOLS);
  524. titles.add(FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  525. if (this.filter.hasESSIDs() || this.filter.hasATimestamp()
  526. || (this.filter.getProtocols() != null && this.filter.hasProtocols() && this.filter.getProtocols().size() != this.protocolTitles().size())) {
  527. titles.add(FILTER_MENU_TITLE_REMOVE);
  528. }
  529. } else {
  530. titles.add(FILTER_MENU_TITLE_PROTOCOL);
  531. String protocol = this.getCurrentSelectedProtocol();
  532. if (protocol.length() > 0) {
  533. if (this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerBSSID)) {
  534. titles.add(FILTER_MENU_TITLE_BSSID);
  535. } else {
  536. // DEFAULT
  537. titles.add(FILTER_MENU_TITLE_ESSID);
  538. }
  539. }
  540. titles.add(FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  541. if (this.filter.hasATimestamp() || this.filter.hasESSIDs() || this.filter.hasBSSIDs()
  542. || (this.currentPlotView instanceof LineGraph && this.filter.hasProtocols())) {
  543. titles.add(FILTER_MENU_TITLE_REMOVE);
  544. }
  545. }
  546. return titles;
  547. }
  548. private void openProtocolsFilterDialog() {
  549. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_PROTOCOLS, this.protocolTitles(), this.selectedProtocols(), true, this);
  550. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_PROTOCOLS);
  551. }
  552. private void openFilterDialogSelectProtocol() {
  553. ArrayList<String> titles = this.protocolTitles();
  554. boolean[] selected = new boolean[titles.size()];
  555. int i = 0;
  556. for (String title : titles) {
  557. selected[i] = title.equals(this.getCurrentSelectedProtocol());
  558. i++;
  559. }
  560. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_PROTOCOL_SINGLE_CHOICE_TITLE, titles, selected, false, this);
  561. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_PROTOCOL_SINGLE_CHOICE_TITLE);
  562. }
  563. private void openESSIDFilterDialog() {
  564. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_ESSID, this.essids(), this.selectedESSIDs(), true, this);
  565. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_ESSID);
  566. }
  567. private void openBSSIDFilterDialog() {
  568. ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_BSSID, this.bssids(), this.selectedBSSIDs(), true, this);
  569. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_BSSID);
  570. }
  571. private void openTimestampFromFilterDialog() {
  572. this.wasBelowTimePicker = false;
  573. DateTimeDialogFragment newFragment = new DateTimeDialogFragment(this.getActivity());
  574. newFragment.setDateChangeListener(this);
  575. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
  576. if (this.filter.aboveTimestamp != Long.MIN_VALUE)
  577. newFragment.setDate(this.filter.aboveTimestamp);
  578. }
  579. private void openTimestampToFilterDialog() {
  580. this.wasBelowTimePicker = true;
  581. DateTimeDialogFragment newFragment = new DateTimeDialogFragment(this.getActivity());
  582. newFragment.setDateChangeListener(this);
  583. newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_TIMESTAMP_BELOW);
  584. if (this.filter.belowTimestamp != Long.MAX_VALUE)
  585. newFragment.setDate(this.filter.belowTimestamp);
  586. }
  587. public ArrayList<String> essids() {
  588. ArrayList<String> records;
  589. if (this.currentPlotView instanceof BarGraph) {
  590. records = dbh.getUniqueESSIDRecordsForProtocol(this.getCurrentSelectedProtocol());
  591. } else {
  592. records = dbh.getUniqueESSIDRecords();
  593. }
  594. return records;
  595. }
  596. public boolean[] selectedESSIDs() {
  597. ArrayList<String> essids = this.essids();
  598. boolean[] selected = new boolean[essids.size()];
  599. int i = 0;
  600. for (String essid : essids) {
  601. selected[i] = (this.filter.getESSIDs().contains(essid));
  602. i++;
  603. }
  604. return selected;
  605. }
  606. public ArrayList<String> bssids() {
  607. ArrayList<String> records;
  608. if (this.currentPlotView instanceof BarGraph) {
  609. records = dbh.getUniqueBSSIDRecordsForProtocol(this.getCurrentSelectedProtocol());
  610. } else {
  611. records = dbh.getUniqueBSSIDRecords();
  612. }
  613. return records;
  614. }
  615. public boolean[] selectedBSSIDs() {
  616. ArrayList<String> bssids = this.bssids();
  617. boolean[] selected = new boolean[bssids.size()];
  618. int i = 0;
  619. for (String bssid : bssids) {
  620. selected[i] = (this.filter.getBSSIDs().contains(bssid));
  621. i++;
  622. }
  623. return selected;
  624. }
  625. public void onDateTimePickerPositiveClick(DateTimeDialogFragment dialog) {
  626. if (this.wasBelowTimePicker) {
  627. this.filter.setBelowTimestamp(dialog.getDate());
  628. } else {
  629. this.filter.setAboveTimestamp(dialog.getDate());
  630. }
  631. this.actualiseCurrentPlot();
  632. }
  633. public void onDateTimePickerNegativeClick(DateTimeDialogFragment dialog) {
  634. if (this.wasBelowTimePicker) {
  635. this.filter.setBelowTimestamp(Long.MAX_VALUE);
  636. } else {
  637. this.filter.setAboveTimestamp(Long.MIN_VALUE);
  638. }
  639. this.actualiseCurrentPlot();
  640. }
  641. /*
  642. *
  643. * PLOT TYPES
  644. *
  645. * *
  646. */
  647. public PieGraph getPieGraphView() {
  648. if (this.pieGraph == null) {
  649. this.pieGraph = new PieGraph(this.getApplicationContext());
  650. LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  651. plotLayout.addView(this.pieGraph);
  652. this.pieGraph.setOnSliceClickedListener(new PieGraph.OnSliceClickedListener() {
  653. @Override
  654. public void onClick(int index) {
  655. StatisticsFragment.this.onSliceClick(index);
  656. }
  657. });
  658. }
  659. return this.pieGraph;
  660. }
  661. public LineGraph getLineGraphView() {
  662. if (this.lineGraph == null) {
  663. this.lineGraph = new LineGraph(this.getActivity());
  664. LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  665. plotLayout.addView(this.lineGraph);
  666. this.lineGraph.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
  667. }
  668. return this.lineGraph;
  669. }
  670. public BarGraph getBarGraphView() {
  671. if (this.barGraph == null) {
  672. this.barGraph = new BarGraph(this.getActivity());
  673. LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  674. this.barGraph.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
  675. plotLayout.addView(this.barGraph);
  676. this.barGraph.setShowBarText(false);
  677. this.barGraph.setPopupImageID(R.drawable.popup_black);
  678. this.barGraph.setOnBarClickedListener(new BarGraph.OnBarClickedListener() {
  679. @Override
  680. public void onClick(int i) {
  681. StatisticsFragment.this.onBarClick(i);
  682. }
  683. });
  684. }
  685. return this.barGraph;
  686. }
  687. /*
  688. * FEED PLOTS WITH DATA
  689. */
  690. public void setPieGraphData(PieGraph piegraph) {
  691. this.currentData = this.getPieData();
  692. if (this.currentData == null) {
  693. this.currentData = new ArrayList<PlotComparisonItem>();
  694. }
  695. this.pieGraph.removeSlices();
  696. for (PlotComparisonItem item : this.currentData) {
  697. PieSlice slice = new PieSlice();
  698. slice.setColor(item.getColor());
  699. Double value2 = (Double) item.getValue2();
  700. float v = value2.floatValue();
  701. slice.setValue(v);
  702. slice.setTitle(item.getTitle());
  703. this.pieGraph.addSlice(slice);
  704. }
  705. this.pieGraph.invalidate();
  706. }
  707. public void setLineGraphData(LineGraph linegraph) {
  708. this.currentData = this.getLineData();
  709. if (this.currentData == null) {
  710. this.currentData = new ArrayList<PlotComparisonItem>();
  711. }
  712. this.lineGraph.removeAllLines();
  713. double rangeMax_Y = 0;
  714. double rangeMin_Y = 0;
  715. double rangeMax_X = 0;
  716. double rangeMin_X = 0;
  717. int count = 0;
  718. for (PlotComparisonItem lineItem : this.currentData) {
  719. ArrayList<PlotComparisonItem> data = lineItem.getOtherData();
  720. int index = 0;
  721. Line l = new Line();
  722. int lineColor = lineItem.getColor();
  723. l.setColor(lineColor);
  724. for (PlotComparisonItem pointItem : data) {
  725. LinePoint p = new LinePoint();
  726. p.setX(pointItem.getValue1());
  727. Double value2 = pointItem.getValue2();
  728. p.setY(value2);
  729. p.setColor(lineColor);
  730. l.addPoint(p);
  731. rangeMax_Y = Math.max(pointItem.getValue2(), rangeMax_Y);
  732. rangeMax_X = Math.max(pointItem.getValue1(), rangeMax_X);
  733. if (count != 0) {
  734. rangeMin_Y = Math.min(pointItem.getValue2(), rangeMin_Y);
  735. rangeMin_X = Math.min(pointItem.getValue1(), rangeMin_X);
  736. } else {
  737. rangeMin_Y = pointItem.getValue2();
  738. rangeMin_X = pointItem.getValue1();
  739. }
  740. index++;
  741. count++;
  742. }
  743. this.lineGraph.addLine(l);
  744. }
  745. // add a bit more space
  746. rangeMax_Y++;
  747. rangeMin_Y--;
  748. boolean shouldUseDate = this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerDate);
  749. if (shouldUseDate) {
  750. this.lineGraph.resetXLimits();
  751. if (this.filter.hasBelowTimestamp()) {
  752. rangeMax_X = Math.max(this.filter.belowTimestamp, rangeMax_X);
  753. }
  754. if (this.filter.hasAboveTimestamp()) {
  755. rangeMin_X = Math.min(this.filter.aboveTimestamp, rangeMin_X);
  756. }
  757. double stepRange = (rangeMax_X - rangeMin_X) / (60 * 60 * 24 * 1000);
  758. this.lineGraph.setxAxisStep(Math.max(1, (float) Math.min(stepRange, 4)));
  759. this.lineGraph.setRangeX(rangeMin_X, rangeMax_X);
  760. this.lineGraph.setConverter(new LineGraph.AxisDataConverter() {
  761. @Override
  762. public String convertDataForX_Position(double x) {
  763. return StatisticsFragment.this.getDateAsDayString((long) x);
  764. }
  765. @Override
  766. public String convertDataForY_Position(double y) {
  767. return "" + (long) y;
  768. }
  769. });
  770. } else {
  771. this.lineGraph.setxAxisStep(12.f);
  772. this.lineGraph.setRangeX(0, 24);
  773. this.lineGraph.setConverter(null);
  774. }
  775. int maxY = (int) (rangeMax_Y - rangeMin_Y);
  776. this.lineGraph.setYAxisStep(Math.min(maxY, 5));
  777. int yStep = (int) this.lineGraph.getyAxisStep();
  778. if ((maxY % yStep) != 0) {
  779. maxY = maxY + (yStep - (maxY % yStep));
  780. }
  781. this.lineGraph.setRangeY(rangeMin_Y, rangeMin_Y + maxY);
  782. this.lineGraph.setLineToFill(0);
  783. this.lineGraph.invalidate();
  784. }
  785. public void setBarGraphData(BarGraph bargraph) {
  786. this.currentData = this.getBarData();
  787. if (this.currentData == null) {
  788. this.currentData = new ArrayList<PlotComparisonItem>();
  789. }
  790. ArrayList<Bar> bars = new ArrayList<Bar>();
  791. for (PlotComparisonItem item : this.currentData) {
  792. Bar d = new Bar();
  793. d.setColor(item.getColor());
  794. Long value2 = item.getValue2().longValue();
  795. d.setName("" + value2);
  796. d.setValue(value2.floatValue());
  797. bars.add(d);
  798. }
  799. this.barGraph.setBars(bars);
  800. this.barGraph.invalidate();
  801. }
  802. /*
  803. *
  804. * FETCH & ACTUALISE RECORD DATA
  805. */
  806. public ArrayList<Record> getFetchedRecords() {
  807. if (this.filter == null)
  808. this.clearFilter();
  809. return this.dbh.getRecordsForFilter(this.filter);
  810. }
  811. public void actualiseCurrentPlot() {
  812. LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  813. View plot = this.currentPlotView;
  814. if (plot == null) {
  815. this.currentPlotView = this.getPieGraphView();
  816. plot = this.currentPlotView;
  817. }
  818. if (plot.getParent() != null && !plot.getParent().equals(plotLayout)) {
  819. LinearLayout linLayout = (LinearLayout) plot.getParent();
  820. linLayout.removeView(plot);
  821. plot.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
  822. plotLayout.addView(plot);
  823. }
  824. if (plot.getVisibility() == View.GONE)
  825. plot.setVisibility(View.VISIBLE);
  826. if (plot instanceof PieGraph) {
  827. PieGraph pie = (PieGraph) plot;
  828. this.setPieGraphData(pie);
  829. // HIDE FILTER BUTTON
  830. ImageButton filterButton = this.getFilterButton();
  831. if (filterButton != null)
  832. filterButton.setVisibility(View.GONE);
  833. } else {
  834. if (this.pieGraph != null) {
  835. this.pieGraph.setVisibility(View.GONE);
  836. if (this.pieGraph.getParent() != null) {
  837. plotLayout.removeView(this.pieGraph);
  838. }
  839. }
  840. // SHOW FILTER BUTTON
  841. ImageButton filterButton = this.getFilterButton();
  842. if (filterButton != null)
  843. filterButton.setVisibility(View.VISIBLE);
  844. }
  845. if (plot instanceof BarGraph) {
  846. BarGraph bar = (BarGraph) plot;
  847. this.setBarGraphData(bar);
  848. } else {
  849. if (this.barGraph != null) {
  850. this.barGraph.setVisibility(View.GONE);
  851. if (this.barGraph.getParent() != null) {
  852. plotLayout.removeView(this.barGraph);
  853. }
  854. }
  855. }
  856. if (plot instanceof LineGraph) {
  857. LineGraph line = (LineGraph) plot;
  858. this.setLineGraphData(line);
  859. } else {
  860. if (this.lineGraph != null) {
  861. this.lineGraph.setVisibility(View.GONE);
  862. if (this.lineGraph.getParent() != null) {
  863. plotLayout.removeView(this.lineGraph);
  864. }
  865. }
  866. }
  867. plot.setVisibility(View.VISIBLE);
  868. if (plot.getParent() == null) {
  869. plotLayout.addView(plot);
  870. }
  871. this.actualiseLegendList();
  872. this.currentPlotView.bringToFront();
  873. this.currentPlotView.invalidate();
  874. }
  875. public ArrayList<PlotComparisonItem> getPieData() {
  876. // DEFAULT
  877. return this.attacksPerProtocols();
  878. }
  879. public ArrayList<PlotComparisonItem> getBarData() {
  880. String protocol = this.getCurrentSelectedProtocol();
  881. if (protocol.length() > 0) {
  882. if (this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerESSID)) {
  883. return this.attacksPerESSID(protocol);
  884. }
  885. // DEFAULT
  886. return this.attacksPerBSSID(protocol);
  887. }
  888. // Nothing available
  889. return new ArrayList<PlotComparisonItem>();
  890. }
  891. public ArrayList<PlotComparisonItem> getLineData() {
  892. return this.attacksPerTime();
  893. }
  894. /*
  895. * DATA SOURCE
  896. */
  897. /* PROTOCOLS OVERVIEW */
  898. public ArrayList<PlotComparisonItem> attacksPerProtocols() {
  899. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  900. int index = 0;
  901. for (String title : this.getSelectedProtocolTitles()) {
  902. int attacksCount = this.dbh.getAttackPerProtocolCount(title);
  903. if (attacksCount == 0)
  904. continue;
  905. PlotComparisonItem item = new PlotComparisonItem(title, this.getColor(index), 0., (double) attacksCount);
  906. plotItems.add(item);
  907. index++;
  908. }
  909. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  910. @Override
  911. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  912. return s2.getValue2().compareTo(s1.getValue2());
  913. }
  914. });
  915. return this.resizeData(plotItems);
  916. }
  917. /* LINE PLOT DATA */
  918. public ArrayList<PlotComparisonItem> attacksPerTime() {
  919. HashMap<String, HashMap<Long, ArrayList<Record>>> lineMap = new HashMap<String, HashMap<Long, ArrayList<Record>>>();
  920. boolean shouldUseDate = this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerDate);
  921. ArrayList<Record> records = this.getFetchedRecords();
  922. for (Record record : records) {
  923. long timestamp = record.getTimestamp();
  924. long time = 0;
  925. if (shouldUseDate) {
  926. time = this.getDateFromMilliseconds(timestamp);
  927. } else {
  928. time = this.getDayHourFromDate(timestamp);
  929. }
  930. // GET CORRECT MAP
  931. HashMap<Long, ArrayList<Record>> recordMap;
  932. String groupKey = record.getSsid();
  933. if (lineMap.containsKey(groupKey)) {
  934. recordMap = lineMap.get(record.getSsid());
  935. } else {
  936. recordMap = new HashMap<Long, ArrayList<Record>>();
  937. lineMap.put(groupKey, recordMap);
  938. }
  939. // GET LIST OF RECORDS
  940. ArrayList<Record> list;
  941. if (recordMap.containsKey(time)) {
  942. list = recordMap.get(time);
  943. } else {
  944. list = new ArrayList<Record>();
  945. recordMap.put(time, list);
  946. }
  947. list.add(record);
  948. }
  949. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  950. int index = 0;
  951. for (String groupKey : lineMap.keySet()) {
  952. HashMap<Long, ArrayList<Record>> recordMap = lineMap.get(groupKey);
  953. ArrayList<PlotComparisonItem> singleLineItems = new ArrayList<PlotComparisonItem>();
  954. int numbOfAttacks = 0;
  955. for (long time : recordMap.keySet()) {
  956. ArrayList<Record> list = recordMap.get(time);
  957. if (list.size() == 0)
  958. continue;
  959. PlotComparisonItem item = new PlotComparisonItem(this.getHourAsTimeString(time), 0, (double) time, (double) list.size());
  960. singleLineItems.add(item);
  961. numbOfAttacks += list.size();
  962. }
  963. Collections.sort(singleLineItems, new Comparator<PlotComparisonItem>() {
  964. @Override
  965. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  966. return s1.getValue1().compareTo(s2.getValue1());
  967. }
  968. });
  969. double itemValue = (((double) numbOfAttacks / (double) records.size()) * 100.);
  970. PlotComparisonItem item = new PlotComparisonItem(groupKey, this.getColor(index), (double) numbOfAttacks, itemValue);
  971. item.setOtherData(singleLineItems);
  972. plotItems.add(item);
  973. index++;
  974. }
  975. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  976. @Override
  977. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  978. return s2.getValue2().compareTo(s1.getValue2());
  979. }
  980. });
  981. return plotItems;
  982. }
  983. // BAR PLOT DATA
  984. public ArrayList<PlotComparisonItem> attacksPerBSSID(String protocol) {
  985. LogFilter filter = new LogFilter();
  986. ArrayList<String> protocollist = new ArrayList<String>();
  987. filter.setAboveTimestamp(this.filter.getAboveTimestamp());
  988. filter.setBelowTimestamp(this.filter.getBelowTimestamp());
  989. filter.setBSSIDs(this.filter.getBSSIDs());
  990. protocollist.add(protocol);
  991. filter.setProtocols(protocollist);
  992. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  993. HashMap<String, Integer> recordMap = new HashMap<String, Integer>();
  994. ArrayList<Record> records = this.dbh.getRecordsForFilter(filter);
  995. for (Record record : records) {
  996. int count = 0;
  997. if (recordMap.containsKey(record.getBssid())) {
  998. count = recordMap.get(record.getBssid());
  999. }
  1000. count++;
  1001. recordMap.put(record.getBssid(), count);
  1002. }
  1003. int index = 0;
  1004. for (String key : recordMap.keySet()) {
  1005. double value = (double) recordMap.get(key);
  1006. if (value == 0.)
  1007. continue;
  1008. PlotComparisonItem item = new PlotComparisonItem(key, this.getColor(index), 0., value);
  1009. plotItems.add(item);
  1010. index++;
  1011. }
  1012. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  1013. @Override
  1014. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  1015. return s2.getValue2().compareTo(s1.getValue2());
  1016. }
  1017. });
  1018. return this.resizeData(plotItems);
  1019. }
  1020. public ArrayList<PlotComparisonItem> attacksPerESSID(String protocol) {
  1021. LogFilter filter = new LogFilter();
  1022. filter.setAboveTimestamp(this.filter.getAboveTimestamp());
  1023. filter.setBelowTimestamp(this.filter.getBelowTimestamp());
  1024. filter.setESSIDs(this.filter.getESSIDs());
  1025. ArrayList<String> protocollist = new ArrayList<String>();
  1026. protocollist.add(protocol);
  1027. filter.setProtocols(protocollist);
  1028. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  1029. HashMap<String, Integer> recordMap = new HashMap<String, Integer>();
  1030. ArrayList<Record> records = this.dbh.getRecordsForFilter(filter);
  1031. for (Record record : records) {
  1032. int count = 0;
  1033. if (recordMap.containsKey(record.getSsid())) {
  1034. count = recordMap.get(record.getSsid());
  1035. }
  1036. count++;
  1037. recordMap.put(record.getSsid(), count);
  1038. }
  1039. int index = 0;
  1040. for (String key : recordMap.keySet()) {
  1041. double value = (double) recordMap.get(key);
  1042. if (value == 0.)
  1043. continue;
  1044. PlotComparisonItem item = new PlotComparisonItem(key, this.getColor(index), 0., value);
  1045. plotItems.add(item);
  1046. index++;
  1047. }
  1048. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  1049. @Override
  1050. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  1051. return s2.getValue2().compareTo(s1.getValue2());
  1052. }
  1053. });
  1054. return this.resizeData(plotItems);
  1055. }
  1056. private ArrayList<PlotComparisonItem> resizeData(ArrayList<PlotComparisonItem> plotItems) {
  1057. if (plotItems != null) {
  1058. if (plotItems.size() > MAX_NUMBER_OF_CHART_OBJECTS && MAX_NUMBER_OF_CHART_OBJECTS > 1) {
  1059. ArrayList<PlotComparisonItem> copy = new ArrayList<PlotComparisonItem>();
  1060. ArrayList<PlotComparisonItem> others = new ArrayList<PlotComparisonItem>();
  1061. double valueOfOthers = 0;
  1062. for (int i = 0; i < plotItems.size(); i++) {
  1063. if (i < MAX_NUMBER_OF_CHART_OBJECTS - 1) {
  1064. PlotComparisonItem item = plotItems.get(i);
  1065. item.setColor(this.getColor(i));
  1066. copy.add(plotItems.get(i));
  1067. } else {
  1068. PlotComparisonItem item = plotItems.get(i);
  1069. others.add(item);
  1070. valueOfOthers += item.getValue2();
  1071. }
  1072. }
  1073. PlotComparisonItem otherItem = new PlotComparisonItem(OTHER_CHART_TITLE, this.getOtherColor(), 0., valueOfOthers);
  1074. otherItem.setOtherData(others);
  1075. copy.add(otherItem);
  1076. Collections.sort(copy, new Comparator<PlotComparisonItem>() {
  1077. @Override
  1078. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  1079. return s2.getValue2().compareTo(s1.getValue2());
  1080. }
  1081. });
  1082. return copy;
  1083. }
  1084. }
  1085. return plotItems;
  1086. }
  1087. /*
  1088. * FILTER STUFF
  1089. */
  1090. private String getCurrentSelectedProtocol() {
  1091. ArrayList<String> protocolTitles = this.getSelectedProtocolTitles();
  1092. if (protocolTitles != null && protocolTitles.size() != 0) {
  1093. return protocolTitles.get(0);
  1094. }
  1095. return this.protocolTitles().get(0);
  1096. }
  1097. public ArrayList<String> protocolTitles() {
  1098. ArrayList<String> titles = new ArrayList<String>();
  1099. for (String protocol : this.getResources().getStringArray(R.array.protocols)) {
  1100. titles.add(protocol);
  1101. }
  1102. return titles;
  1103. }
  1104. public boolean[] selectedProtocols() {
  1105. ArrayList<String> protocols = this.protocolTitles();
  1106. boolean[] selected = new boolean[protocols.size()];
  1107. int i = 0;
  1108. for (String protocol : protocols) {
  1109. selected[i] = (this.filter.protocols.contains(protocol));
  1110. i++;
  1111. }
  1112. return selected;
  1113. }
  1114. public ArrayList<String> getSelectedProtocolTitles() {
  1115. ArrayList<String> knownProtocols = this.protocolTitles();
  1116. if (this.filter.hasProtocols()) {
  1117. ArrayList<String> titles = new ArrayList<String>();
  1118. int i = 0;
  1119. for (boolean b : this.selectedProtocols()) {
  1120. if (b) {
  1121. String title = knownProtocols.get(i);
  1122. titles.add(title);
  1123. }
  1124. i++;
  1125. }
  1126. return titles;
  1127. }
  1128. return this.protocolTitles();
  1129. }
  1130. /*
  1131. *
  1132. * COLOR STUFF
  1133. */
  1134. public int getOtherColor() {
  1135. return Color.argb(255, 80, 80, 80); // grey
  1136. }
  1137. public Integer getColor(int index) {
  1138. return ColorSequenceGenerator.getColorForIndex(index);
  1139. }
  1140. public LinearLayout getPlotLayout() {
  1141. if (this.rootView != null) {
  1142. return (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  1143. } else {
  1144. return null;
  1145. }
  1146. }
  1147. /**
  1148. *
  1149. * FILTER STUFF
  1150. *
  1151. * */
  1152. private boolean isFilterSetForTitle(String title) {
  1153. if (title.equals(FILTER_MENU_TITLE_BSSID)) {
  1154. return this.filter.hasBSSIDs();
  1155. }
  1156. if (title.equals(FILTER_MENU_TITLE_ESSID)) {
  1157. return this.filter.hasESSIDs();
  1158. }
  1159. if (title.equals(FILTER_MENU_TITLE_PROTOCOLS)) {
  1160. return (this.filter.getProtocols() != null && this.filter.hasProtocols() && this.filter.getProtocols().size() != this.protocolTitles().size());
  1161. }
  1162. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)) {
  1163. return this.filter.hasBelowTimestamp();
  1164. }
  1165. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)) {
  1166. return this.filter.hasAboveTimestamp();
  1167. }
  1168. return false;
  1169. }
  1170. private void clearFilter() {
  1171. if (filter == null)
  1172. this.filter = new LogFilter();
  1173. this.filter.clear();
  1174. }
  1175. /*
  1176. *
  1177. * DATE TRANSFORMATION
  1178. */
  1179. public long getDayHourFromDate(long timeInMillis) {
  1180. Calendar calendar = Calendar.getInstance();
  1181. calendar.setTimeInMillis(timeInMillis);
  1182. int hour = calendar.get(Calendar.HOUR_OF_DAY);
  1183. int min = calendar.get(Calendar.MINUTE);
  1184. return hour;
  1185. }
  1186. public long getDateFromMilliseconds(long timeInMillis) {
  1187. long millisInDay = 60 * 60 * 24 * 1000;
  1188. return (timeInMillis / millisInDay) * millisInDay;
  1189. }
  1190. /*
  1191. *
  1192. * */
  1193. private String getHourAsTimeString(long hour) {
  1194. return "" + hour + ":00";
  1195. }
  1196. static final DateFormat dateFormat = new SimpleDateFormat("d.M.yyyy");
  1197. @SuppressLint("SimpleDateFormat")
  1198. private String getDateAsDayString(long timeStamp) {
  1199. try {
  1200. Date netDate = (new Date(timeStamp));
  1201. return dateFormat.format(netDate);
  1202. } catch (Exception ex) {
  1203. return "xx";
  1204. }
  1205. }
  1206. @SuppressLint("SimpleDateFormat")
  1207. private String getDateAsString(long timeStamp) {
  1208. try {
  1209. DateFormat sdf = new SimpleDateFormat("H:mm dd/MM/yyyy");
  1210. Date netDate = (new Date(timeStamp));
  1211. return sdf.format(netDate);
  1212. } catch (Exception ex) {
  1213. return "xx";
  1214. }
  1215. }
  1216. /**
  1217. * USERINTERACTION
  1218. */
  1219. private void userTappedOnLegendItem(int index) {
  1220. if (index < this.currentData.size()) {
  1221. PlotComparisonItem item = this.currentData.get(index);
  1222. ArrayList<String> selectedData;
  1223. String sortKey = null;
  1224. selectedData = new ArrayList<String>();
  1225. if (item.getOtherData() == null) {
  1226. selectedData.add(item.getTitle());
  1227. } else {
  1228. for (PlotComparisonItem other : item.getOtherData()) {
  1229. selectedData.add(other.getTitle());
  1230. }
  1231. }
  1232. LogFilter filter = new LogFilter();
  1233. if (this.currentPlotView instanceof PieGraph) {
  1234. filter.setProtocols(selectedData);
  1235. }
  1236. if (this.currentPlotView instanceof BarGraph) {
  1237. if (this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerESSID)) {
  1238. filter.setESSIDs(selectedData);
  1239. sortKey = "ESSID";
  1240. } else {
  1241. filter.setBSSIDs(selectedData);
  1242. sortKey = "BSSID";
  1243. }
  1244. ArrayList<String> currentSelectedProtocol = new ArrayList<String>();
  1245. currentSelectedProtocol.add(this.getCurrentSelectedProtocol());
  1246. filter.setProtocols(currentSelectedProtocol);
  1247. }
  1248. if (this.currentPlotView instanceof LineGraph) {
  1249. selectedData = new ArrayList<String>();
  1250. selectedData.add(item.getTitle());
  1251. filter.setESSIDs(selectedData);
  1252. filter.setProtocols(this.filter.getProtocols());
  1253. sortKey = "ESSID";
  1254. }
  1255. if (this.filter.hasATimestamp()) {
  1256. filter.setAboveTimestamp(this.filter.getAboveTimestamp());
  1257. filter.setBelowTimestamp(this.filter.getBelowTimestamp());
  1258. }
  1259. this.pushRecordOverviewForFilter(filter, sortKey);
  1260. }
  1261. }
  1262. public void onSliceClick(int index) {
  1263. }
  1264. public void onBarClick(int index) {
  1265. this.userTappedOnLegendItem(index);
  1266. }
  1267. private void pushRecordOverviewForFilter(LogFilter filter, String sortKey) {
  1268. FragmentManager fm = this.getActivity().getFragmentManager();
  1269. if (fm != null) {
  1270. RecordOverviewFragment newFragment = new RecordOverviewFragment();
  1271. newFragment.setUpNavigatible(true);
  1272. newFragment.setFilter(filter);
  1273. if (sortKey != null && sortKey.length() != 0)
  1274. newFragment.setGroupKey(sortKey);
  1275. MainActivity.getInstance().injectFragment(newFragment);
  1276. }
  1277. }
  1278. }