StatisticsFragment.java 50 KB

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