StatisticsFragment.java 55 KB

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