StatisticsFragment.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. package de.tudarmstadt.informatik.hostage.ui2.fragment;
  2. import android.annotation.SuppressLint;
  3. import android.app.Fragment;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.graphics.Color;
  7. import android.os.Bundle;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.AdapterView;
  12. import android.widget.LinearLayout;
  13. import android.widget.ListView;
  14. import android.widget.TextView;
  15. import com.echo.holographlibrary.Bar;
  16. import com.echo.holographlibrary.BarGraph;
  17. import com.echo.holographlibrary.Line;
  18. import com.echo.holographlibrary.LineGraph;
  19. import com.echo.holographlibrary.LinePoint;
  20. import com.echo.holographlibrary.PieGraph;
  21. import com.echo.holographlibrary.PieSlice;
  22. import java.text.DateFormat;
  23. import java.text.SimpleDateFormat;
  24. import java.util.ArrayList;
  25. import java.util.Calendar;
  26. import java.util.Collections;
  27. import java.util.Comparator;
  28. import java.util.Date;
  29. import java.util.HashMap;
  30. import de.tudarmstadt.informatik.hostage.R;
  31. import de.tudarmstadt.informatik.hostage.logging.Record;
  32. import de.tudarmstadt.informatik.hostage.logging.UglyDbHelper;
  33. import de.tudarmstadt.informatik.hostage.ui.LogFilter;
  34. import de.tudarmstadt.informatik.hostage.ui2.adapter.StatisticListAdapter;
  35. import de.tudarmstadt.informatik.hostage.ui2.model.PlotComparisonItem;
  36. /**
  37. * Created by Julien on 16.02.14.
  38. */
  39. public class StatisticsFragment extends Fragment {
  40. static final String SELECTED_KEY = "Selected";
  41. static final String OTHERS_KEY = "Other";
  42. static final String FILTER_MENU_TITLE_BSSID = "BSSID";
  43. static final String FILTER_MENU_TITLE_ESSID = "ESSID";
  44. static final String FILTER_MENU_TITLE_PROTOCOLS = "Protocol";
  45. static final String FILTER_MENU_TITLE_TIMESTAMP_BELOW = "Latest";
  46. static final String FILTER_MENU_TITLE_TIMESTAMP_ABOVE = "Earliest";
  47. static final String FILTER_MENU_TITLE_SORTING = "Sort by";
  48. static final String FILTER_MENU_TITLE_REMOVE = "Reset Filter";
  49. static final String FILTER_MENU_TITLE_GROUP = "Group by";
  50. static final String FILTER_MENU_POPUP_TITLE = "Filter by";
  51. static final String CHART_TYPE_TITLE_BAR = "Bar Plot";
  52. static final String CHART_TYPE_TITLE_PIE = "Pie Plot";
  53. static final String CHART_TYPE_TITLE_LINE = "Line Plot";
  54. static final String OTHER_CHART_TITLE = "Other";
  55. // MINIMAL 2
  56. static int MAX_NUMBER_OF_CHART_OBJECTS = 5;
  57. private boolean wasBelowTimePicker;
  58. private LogFilter filter;
  59. private boolean showFilterButton;
  60. private PieGraph pieGraph;
  61. private LineGraph lineGraph;
  62. private BarGraph barGraph;
  63. private View rootView;
  64. private View currentPlotView;
  65. private ArrayList<Integer> colorList;
  66. private ArrayList<PlotComparisonItem> currentData;
  67. private UglyDbHelper dbh;
  68. private ListView legendListView;
  69. public enum ChartType {
  70. PIE_CHART(0),
  71. BAR_CHART(1),
  72. LINE_CHART(2);
  73. private int value;
  74. private ChartType(int value) {
  75. this.value = value;
  76. }
  77. static public ChartType create(int value){
  78. if (value < 0 || value >= ChartType.values().length) return ChartType.PIE_CHART;
  79. return ChartType.values()[value];
  80. }
  81. public String toString(){
  82. if (this.equals(ChartType.create(0))){
  83. return CHART_TYPE_TITLE_PIE;
  84. }
  85. if (this.equals(ChartType.create(1))){
  86. return CHART_TYPE_TITLE_BAR;
  87. }
  88. return CHART_TYPE_TITLE_LINE;
  89. }
  90. }
  91. @Override
  92. public void onCreate(Bundle savedInstanceState) {
  93. super.onCreate(savedInstanceState);
  94. setHasOptionsMenu(true);
  95. }
  96. public int getLayoutID(){
  97. return R.layout.fragment_statistics;
  98. }
  99. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  100. Bundle savedInstanceState) {
  101. dbh = new UglyDbHelper(this.getBaseContext());
  102. // Get the message from the intent
  103. if (this.filter == null){
  104. Intent intent = this.getActivity().getIntent();
  105. LogFilter filter = intent.getParcelableExtra(LogFilter.LOG_FILTER_INTENT_KEY);
  106. if(filter == null){
  107. this.clearFilter();
  108. } else {
  109. this.filter = filter;
  110. }
  111. }
  112. View rootView = inflater.inflate(this.getLayoutID(), container, false);
  113. LinearLayout plotLayout = (LinearLayout) rootView.findViewById(R.id.plot_layout);
  114. plotLayout.setWillNotDraw(false);
  115. container.setWillNotDraw(false);
  116. this.legendListView = (ListView) rootView.findViewById(R.id.legend_list_view);
  117. this.legendListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  118. @Override
  119. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  120. StatisticsFragment.this.userTappedOnItem(i);
  121. }
  122. });
  123. this.rootView = rootView;
  124. rootView.setWillNotDraw(false);
  125. this.configureRootView(rootView);
  126. return rootView;
  127. }
  128. private Context getBaseContext(){
  129. return this.getActivity().getBaseContext();
  130. }
  131. private Context getApplicationContext(){
  132. return this.getActivity().getApplicationContext();
  133. }
  134. public void configureRootView(View rootView){
  135. LinearLayout plotLayout = (LinearLayout) rootView.findViewById(R.id.plot_layout);
  136. plotLayout.setWillNotDraw(false);
  137. plotLayout.removeAllViews();
  138. this.actualiseCurrentPlot();
  139. }
  140. public void setTitle(String title){
  141. TextView titleView = (TextView) this.rootView.findViewById(R.id.title_text_view);
  142. if (title != null && titleView != null){
  143. titleView.setText(title);
  144. }
  145. }
  146. public String getTitle(){
  147. TextView titleView = (TextView) this.rootView.findViewById(R.id.title_text_view);
  148. if (titleView != null){
  149. return "" + titleView.getText();
  150. }
  151. return "";
  152. }
  153. public void onStart() {
  154. super.onStart();
  155. this.actualiseCurrentPlot();
  156. this.currentPlotView.invalidate();
  157. }
  158. public void setChartType(ChartType type){
  159. boolean shouldChange = true;
  160. if (this.currentPlotView != null){
  161. if (type == ChartType.PIE_CHART){
  162. shouldChange = ! (this.currentPlotView instanceof PieGraph);
  163. } else {
  164. this.pieGraph.setVisibility(View.INVISIBLE);
  165. }
  166. if (type == ChartType.LINE_CHART){
  167. shouldChange = ! (this.currentPlotView instanceof LineGraph);
  168. } else {
  169. this.lineGraph.setVisibility(View.INVISIBLE);
  170. }
  171. if (type == ChartType.BAR_CHART){
  172. shouldChange = ! (this.currentPlotView instanceof BarGraph);
  173. } else {
  174. this.barGraph.setVisibility(View.INVISIBLE);
  175. }
  176. }
  177. if (shouldChange){
  178. this.currentPlotView = this.getPlotViewForType(type);
  179. this.currentPlotView.setVisibility(View.VISIBLE);
  180. this.actualiseCurrentPlot();
  181. }
  182. }
  183. public View getPlotViewForType(ChartType type){
  184. switch (type){
  185. case PIE_CHART:
  186. return this.getPieGraphView();
  187. case LINE_CHART:
  188. return this.getLineGraphView();
  189. default:
  190. return this.getBarGraphView();
  191. }
  192. }
  193. public void actualiseLegendList(){
  194. StatisticListAdapter adapter = new StatisticListAdapter(this.getApplicationContext(), this.currentData);
  195. this.legendListView.setAdapter(adapter);
  196. }
  197. /**
  198. *
  199. * PLOT TOUCH HANDLING
  200. *
  201. * */
  202. public void onSliceClick(int index){
  203. this.userTappedOnItem(index);
  204. }
  205. public void onBarClick(int index){
  206. this.userTappedOnItem(index);
  207. }
  208. /*
  209. *
  210. * PLOT TYPES
  211. *
  212. * **/
  213. public PieGraph getPieGraphView(){
  214. if (this.pieGraph == null) {
  215. this.pieGraph = new PieGraph(this.getApplicationContext());
  216. LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  217. plotLayout.addView(this.pieGraph);
  218. this.pieGraph.setOnSliceClickedListener(new PieGraph.OnSliceClickedListener() {
  219. @Override
  220. public void onClick(int index) {
  221. StatisticsFragment.this.onSliceClick(index);
  222. }
  223. });
  224. }
  225. return this.pieGraph;
  226. }
  227. public LineGraph getLineGraphView(){
  228. if (this.lineGraph == null) {
  229. this.lineGraph = new LineGraph(this.getActivity());
  230. LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  231. plotLayout.addView(this.lineGraph);
  232. }
  233. return this.lineGraph;
  234. }
  235. public BarGraph getBarGraphView(){
  236. if (this.barGraph == null) {
  237. this.barGraph = new BarGraph(this.getActivity());
  238. LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  239. plotLayout.addView(this.barGraph);
  240. this.barGraph.setPopupImageID(R.drawable.popup_black);
  241. this.barGraph.setOnBarClickedListener(new BarGraph.OnBarClickedListener() {
  242. @Override
  243. public void onClick(int i) {
  244. StatisticsFragment.this.onBarClick(i);
  245. }
  246. });
  247. }
  248. return this.barGraph;
  249. }
  250. /*
  251. * FEED PLOTS WITH DATA
  252. * */
  253. public void setPieGraphData(PieGraph piegraph){
  254. this.currentData = this.getPieData();
  255. if (this.currentData == null){
  256. this.currentData = new ArrayList<PlotComparisonItem>();
  257. }
  258. this.pieGraph.removeSlices();
  259. for (PlotComparisonItem item : this.currentData){
  260. PieSlice slice = new PieSlice();
  261. slice.setColor(item.getColor());
  262. Double value2 = (Double) item.getValue2();
  263. float v = value2.floatValue();
  264. slice.setValue(v);
  265. slice.setTitle(item.getTitle());
  266. this.pieGraph.addSlice(slice);
  267. }
  268. this.pieGraph.invalidate();
  269. }
  270. public void setLineGraphData(LineGraph linegraph){
  271. this.currentData = this.getLinedata();
  272. if (this.currentData == null){
  273. this.currentData = new ArrayList<PlotComparisonItem>();
  274. }
  275. int index = 0;
  276. this.lineGraph.removeAllLines();
  277. Line l = new Line();
  278. for (PlotComparisonItem item : this.currentData){
  279. LinePoint p = new LinePoint();
  280. p.setX(index);
  281. Double value2 = (Double) item.getValue2();
  282. p.setY(value2.floatValue());
  283. index++;
  284. }
  285. this.lineGraph.addLine(l);
  286. this.lineGraph.setRangeY(0, index);
  287. this.lineGraph.setLineToFill(0);
  288. this.lineGraph.invalidate();
  289. }
  290. public void setBarGraphData(BarGraph bargraph){
  291. this.currentData = this.getLinedata();
  292. if (this.currentData == null){
  293. this.currentData = new ArrayList<PlotComparisonItem>();
  294. }
  295. ArrayList<Bar> bars = new ArrayList<Bar>();
  296. for (PlotComparisonItem item : this.currentData){
  297. Bar d = new Bar();
  298. d.setColor(item.getColor());
  299. d.setName(item.getTitle());
  300. Double value2 = (Double) item.getValue2();
  301. d.setValue(value2.floatValue());
  302. }
  303. this.barGraph.setBars(bars);
  304. this.barGraph.invalidate();
  305. }
  306. /*
  307. *
  308. * FETCH & ACTUALISE RECORD DATA
  309. *
  310. * */
  311. public ArrayList<Record> getFetchedRecords(){
  312. if (this.filter == null) this.clearFilter();
  313. return this.dbh.getRecordsForFilter(this.filter);
  314. }
  315. public void actualiseCurrentPlot(){
  316. View plot = this.currentPlotView;
  317. if (plot == null){
  318. this.currentPlotView = this.getPieGraphView();
  319. plot = this.currentPlotView;
  320. }
  321. if (plot.getVisibility() == View.INVISIBLE) plot.setVisibility(View.VISIBLE);
  322. if (plot instanceof PieGraph){
  323. PieGraph pie = (PieGraph) plot;
  324. this.setPieGraphData(pie);
  325. } else {
  326. if (this.pieGraph != null)
  327. this.pieGraph.setVisibility(View.INVISIBLE);
  328. }
  329. if (plot instanceof BarGraph){
  330. BarGraph bar = (BarGraph) plot;
  331. this.setBarGraphData(bar);
  332. } else {
  333. if (this.barGraph != null)
  334. this.barGraph.setVisibility(View.INVISIBLE);
  335. }
  336. if (plot instanceof LineGraph){
  337. LineGraph line = (LineGraph)plot;
  338. this.setLineGraphData(line);
  339. }else {
  340. if (this.lineGraph != null)
  341. this.lineGraph.setVisibility(View.INVISIBLE);
  342. }
  343. plot.setVisibility(View.VISIBLE);
  344. this.actualiseLegendList();
  345. this.currentPlotView.invalidate();
  346. }
  347. public ArrayList<PlotComparisonItem> getPieData(){
  348. // DEFAULT
  349. return this.attacksPerProtocols();
  350. }
  351. public ArrayList<PlotComparisonItem> getBarData(){
  352. ArrayList<String> protocolTitles = this.getSelectedProtocolTitles();
  353. // DEFAULT
  354. if (protocolTitles != null && protocolTitles.size() != 0){
  355. String protocol = this.getSelectedProtocolTitles().get(0);
  356. return this.attacksPerBSSID(protocol);
  357. }
  358. // Nothing available
  359. return new ArrayList<PlotComparisonItem>();
  360. }
  361. public ArrayList<PlotComparisonItem> getLinedata(){
  362. // DEFAULT
  363. return this.attacksPerDate();
  364. }
  365. /*
  366. * SPECIFIC DATA STUFF
  367. * */
  368. /*PROTOCOLS OVERVIEW*/
  369. public ArrayList<PlotComparisonItem> attacksPerProtocols(){
  370. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  371. int index = 0;
  372. for (String title : this.getSelectedProtocolTitles()){
  373. int attacksCount = this.dbh.getAttackPerProtocolCount(title);
  374. PlotComparisonItem item = new PlotComparisonItem(title,this.getColor(index), 0., (double) attacksCount);
  375. plotItems.add(item);
  376. index++;
  377. }
  378. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  379. @Override
  380. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  381. return s1.getValue2().compareTo(s2.getValue2());
  382. }
  383. });
  384. return this.resizeData(plotItems);
  385. }
  386. public ArrayList<PlotComparisonItem> usesPerProtocol(){
  387. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  388. for (String title : this.getSelectedProtocolTitles()){
  389. /*At the moment there is no possibillity to get the number of uses for each protocol*/
  390. }
  391. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  392. @Override
  393. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  394. return s1.getTitle().compareToIgnoreCase(s2.getTitle());
  395. }
  396. });
  397. return plotItems;
  398. }
  399. /*LINE PLOT DATA*/
  400. public ArrayList<PlotComparisonItem> attacksPerDate(){
  401. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  402. HashMap<Long, ArrayList<Record> > recordMap = new HashMap<Long, ArrayList<Record> >();
  403. for (Record record : this.getFetchedRecords()){
  404. long date = this.getdateFromMilliseconds(record.getTimestamp());
  405. ArrayList<Record> list = recordMap.get(date);
  406. if (list == null){
  407. list = new ArrayList<Record>();
  408. recordMap.put(date, list);
  409. }
  410. list.add(record);
  411. }
  412. int index = 0;
  413. for (long date : recordMap.keySet()){
  414. ArrayList<Record> list = recordMap.get(date);
  415. PlotComparisonItem item = new PlotComparisonItem(this.getDateAsDayString(date), this.getColor(index), (double) date, (double)list.size());
  416. plotItems.add(item);
  417. index++;
  418. }
  419. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  420. @Override
  421. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  422. return s1.getValue1().compareTo(s2.getValue1());
  423. }
  424. });
  425. return plotItems;
  426. }
  427. public ArrayList<PlotComparisonItem> attacksPerTime(){
  428. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  429. HashMap<Long, ArrayList<Record> > recordMap = new HashMap<Long, ArrayList<Record> >();
  430. for (Record record : this.getFetchedRecords()){
  431. long date = this.getDayHourFromDate(record.getTimestamp());
  432. ArrayList<Record> list = recordMap.get(date);
  433. if (list == null){
  434. list = new ArrayList<Record>();
  435. recordMap.put(date, list);
  436. }
  437. list.add(record);
  438. }
  439. int index = 0;
  440. for (long time : recordMap.keySet()){
  441. ArrayList<Record> list = recordMap.get(time);
  442. PlotComparisonItem item = new PlotComparisonItem(this.getDateAsTimeString(time), this.getColor(index) , (double)time, (double) list.size());
  443. plotItems.add(item);
  444. index++;
  445. }
  446. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  447. @Override
  448. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  449. return s1.getValue1().compareTo(s2.getValue1());
  450. }
  451. });
  452. return plotItems;
  453. }
  454. // BAR PLOT DATA
  455. public ArrayList<PlotComparisonItem> attacksPerBSSID(String protocol){
  456. LogFilter filter = new LogFilter();
  457. ArrayList<String> protocollist = new ArrayList<String>();
  458. protocollist.add(protocol);
  459. filter.setProtocols(protocollist);
  460. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  461. HashMap<String, Integer> recordMap = new HashMap<String, Integer>();
  462. ArrayList<Record> records = this.dbh.getRecordsForFilter(filter);
  463. for (Record record : records){
  464. int count = recordMap.get(record.getBssid());
  465. count++;
  466. recordMap.put(record.getBssid(), count);
  467. }
  468. int index = 0;
  469. for (String key : recordMap.keySet()){
  470. PlotComparisonItem item = new PlotComparisonItem(key, this.getColor(index), 0., (double)recordMap.get(key));
  471. plotItems.add(item);
  472. index++;
  473. }
  474. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  475. @Override
  476. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  477. return s1.getValue2().compareTo(s2.getValue2());
  478. }
  479. });
  480. return this.resizeData(plotItems);
  481. }
  482. public ArrayList<PlotComparisonItem> attacksPerESSID(String protocol){
  483. LogFilter filter = new LogFilter();
  484. ArrayList<String> protocollist = new ArrayList<String>();
  485. protocollist.add(protocol);
  486. filter.setProtocols(protocollist);
  487. ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
  488. HashMap<String, Integer> recordMap = new HashMap<String, Integer>();
  489. ArrayList<Record> records = this.dbh.getRecordsForFilter(filter);
  490. for (Record record : records){
  491. int count = recordMap.get(record.getSsid());
  492. count++;
  493. recordMap.put(record.getSsid(), count);
  494. }
  495. int index = 0;
  496. for (String key : recordMap.keySet()){
  497. PlotComparisonItem item = new PlotComparisonItem(key,this.getColor(index), 0. , (double)recordMap.get(key));
  498. plotItems.add(item);
  499. index++;
  500. }
  501. Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
  502. @Override
  503. public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
  504. return s1.getValue2().compareTo(s2.getValue2());
  505. }
  506. });
  507. return this.resizeData(plotItems);
  508. }
  509. private ArrayList<PlotComparisonItem> resizeData(ArrayList<PlotComparisonItem> plotItems){
  510. if (plotItems != null){
  511. if (plotItems.size() > MAX_NUMBER_OF_CHART_OBJECTS && MAX_NUMBER_OF_CHART_OBJECTS > 1){
  512. ArrayList<PlotComparisonItem> copy = new ArrayList<PlotComparisonItem>();
  513. ArrayList<PlotComparisonItem> others = new ArrayList<PlotComparisonItem>();
  514. double valueOfOthers = 0;
  515. for (int i = 0; i < plotItems.size(); i++){
  516. if (i < MAX_NUMBER_OF_CHART_OBJECTS - 1){
  517. PlotComparisonItem item = plotItems.get(i);
  518. item.setColor(this.getColor(i));
  519. copy.add(plotItems.get(i));
  520. } else {
  521. PlotComparisonItem item = plotItems.get(i);
  522. others.add(item);
  523. valueOfOthers+=item.getValue2();
  524. }
  525. }
  526. PlotComparisonItem otherItem = new PlotComparisonItem(OTHER_CHART_TITLE, this.getOtherColor(), 0., valueOfOthers);
  527. otherItem.setOtherData(others);
  528. copy.add(otherItem);
  529. return copy;
  530. }
  531. }
  532. return plotItems;
  533. }
  534. /*
  535. * FILTER STUFF
  536. * */
  537. public ArrayList<String> protocolTitles(){
  538. ArrayList<String> titles = new ArrayList<String>();
  539. for (String protocol : this.getResources().getStringArray(
  540. R.array.protocols)) {
  541. titles.add(protocol);
  542. }
  543. return titles;
  544. }
  545. public boolean[] selectedProtocols(){
  546. ArrayList<String> protocols = this.protocolTitles();
  547. boolean[] selected = new boolean[protocols.size()];
  548. int i = 0;
  549. for(String protocol : protocols){
  550. selected[i] =(this.filter.protocols.contains(protocol));
  551. i++;
  552. }
  553. return selected;
  554. }
  555. public ArrayList<String> getSelectedProtocolTitles(){
  556. ArrayList<String> knownProtocols = this.protocolTitles();
  557. if (this.filter.hasProtocols()){
  558. ArrayList<String> titles = new ArrayList<String>();
  559. int i =0;
  560. for (boolean b : this.selectedProtocols()){
  561. if (b){
  562. String title = knownProtocols.get(i);
  563. titles.add(title);
  564. }
  565. i++;
  566. }
  567. return titles;
  568. }
  569. return this.protocolTitles();
  570. }
  571. /*
  572. *
  573. * COLOR STUFF
  574. *
  575. * */
  576. public int getOtherColor(){
  577. return Color.argb(255,0,0,0);
  578. }
  579. public ArrayList<Integer> getColorList(){
  580. if (this.colorList == null){
  581. this.colorList = new ArrayList<Integer>();
  582. for (int i =0; i < 255; i++){
  583. Integer color = this.generateColorForIndex(i);
  584. this.colorList.add(color);
  585. }
  586. }
  587. return this.colorList;
  588. }
  589. public Integer getColor(int index){
  590. if (this.colorList == null) this.colorList = new ArrayList<Integer>();
  591. if (index >= this.colorList.size()) {
  592. for (int i = this.colorList.size(); i<= index; i++){
  593. Integer color = this.generateColorForIndex(i);
  594. this.colorList.add(color);
  595. }
  596. return this.generateColorForIndex(index);
  597. }
  598. return this.colorList.get(index);
  599. }
  600. public Integer generateColorForIndex(int index){
  601. int r[] = new int[]{0,0,0,1,1,1,1};
  602. int g[] = new int[]{0,1,1,1,1,0,0};
  603. int b[] = new int[]{1,1,0,0,1,1,0};
  604. int a = index % 16;
  605. int n = index % 7;
  606. int R = Math.max( (r[n] * 255) - ((Math.max(0,(a ))) * 16), 0);
  607. int G = Math.max( (g[n] * 255) - ((Math.max(0,(a ))) * 16), 0);
  608. int B = Math.max( (b[n] * 255) - ((Math.max(0,(a ))) * 16), 0);
  609. return Color.argb(255,R,G,B);
  610. }
  611. public LinearLayout getPlotLayout(){
  612. if (this.rootView != null){
  613. return (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
  614. } else {
  615. return null;
  616. }
  617. }
  618. /**
  619. *
  620. * FILTER STUFF
  621. *
  622. * */
  623. private boolean isFilterSetForTitle(String title){
  624. if (title.equals(FILTER_MENU_TITLE_BSSID)){
  625. return this.filter.hasBSSIDs();
  626. }
  627. if (title.equals(FILTER_MENU_TITLE_ESSID)){
  628. return this.filter.hasESSIDs();
  629. }
  630. if (title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
  631. return this.filter.hasProtocols();
  632. }
  633. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)){
  634. return this.filter.hasBelowTimestamp();
  635. }
  636. if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)){
  637. return this.filter.hasAboveTimestamp();
  638. }
  639. return false;
  640. }
  641. private void clearFilter(){
  642. if(filter == null) this.filter = new LogFilter();
  643. this.filter.clear();
  644. }
  645. /*
  646. *
  647. * DATE TRANSFORMATION
  648. *
  649. */
  650. public long getDayHourFromDate(long timeInMillis){
  651. Calendar calendar = Calendar.getInstance();
  652. calendar.setTimeInMillis (timeInMillis);
  653. int hour = calendar.get(Calendar.HOUR);
  654. int min = calendar.get(Calendar.MINUTE);
  655. return (hour*60*60)*1000;
  656. }
  657. public long getdateFromMilliseconds(long timeInMillis){
  658. Calendar calendar = Calendar.getInstance();
  659. calendar.setTimeInMillis (timeInMillis);
  660. int year = calendar.get(Calendar.YEAR) ;
  661. int month = calendar.get(Calendar.MONTH);
  662. int day = calendar.get(Calendar.DATE);
  663. calendar.set(year, month, day, 0,0);
  664. return calendar.getTimeInMillis();
  665. }
  666. static final DateFormat timeFormat = new SimpleDateFormat("H:mm");
  667. @SuppressLint("SimpleDateFormat")
  668. private String getDateAsTimeString(long timeStamp) {
  669. try {
  670. Date netDate = (new Date(timeStamp));
  671. return timeFormat.format(netDate);
  672. } catch (Exception ex) {
  673. return "xx";
  674. }
  675. }
  676. static final DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
  677. @SuppressLint("SimpleDateFormat")
  678. private String getDateAsDayString(long timeStamp) {
  679. try {
  680. Date netDate = (new Date(timeStamp));
  681. return dateFormat.format(netDate);
  682. } catch (Exception ex) {
  683. return "xx";
  684. }
  685. }
  686. /**
  687. * USERINTERACTION
  688. */
  689. private void userTappedOnItem(int index){
  690. if (index < this.currentData.size()){
  691. PlotComparisonItem item = this.currentData.get(index);
  692. ArrayList<String> selectedData;
  693. selectedData = new ArrayList<String>();
  694. if (item.getOtherData() == null){
  695. selectedData.add(item.getTitle());
  696. } else {
  697. for (PlotComparisonItem other : item.getOtherData()){
  698. selectedData.add(other.getTitle());
  699. }
  700. }
  701. LogFilter filter = new LogFilter();
  702. if (this.currentPlotView instanceof PieGraph){
  703. filter.setProtocols(selectedData);
  704. }
  705. if (this.currentPlotView instanceof BarGraph){
  706. // TODO set data for BSSID / ESSID
  707. }
  708. if (this.currentPlotView instanceof LineGraph){
  709. // TODO set data for BSSID / ESSID
  710. }
  711. }
  712. }
  713. private void pushRecordOverviewForFilter(LogFilter filter){
  714. }
  715. }