StatisticsFragment.java 59 KB

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