StatisticsFragment.java 54 KB

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