StatisticsFragment.java 56 KB

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