1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030 |
- package de.tudarmstadt.informatik.hostage.ui2.fragment;
- import android.annotation.SuppressLint;
- import android.app.Fragment;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AdapterView;
- import android.widget.ImageButton;
- import android.widget.LinearLayout;
- import android.widget.ListView;
- import android.widget.TextView;
- import com.echo.holographlibrary.Bar;
- import com.echo.holographlibrary.BarGraph;
- import com.echo.holographlibrary.Line;
- import com.echo.holographlibrary.LineGraph;
- import com.echo.holographlibrary.LinePoint;
- import com.echo.holographlibrary.PieGraph;
- import com.echo.holographlibrary.PieSlice;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.Date;
- import java.util.HashMap;
- import de.tudarmstadt.informatik.hostage.R;
- import de.tudarmstadt.informatik.hostage.logging.Record;
- import de.tudarmstadt.informatik.hostage.logging.UglyDbHelper;
- import de.tudarmstadt.informatik.hostage.ui.LogFilter;
- import de.tudarmstadt.informatik.hostage.ui2.adapter.StatisticListAdapter;
- import de.tudarmstadt.informatik.hostage.ui2.dialog.ChecklistDialog;
- import de.tudarmstadt.informatik.hostage.ui2.model.PlotComparisonItem;
- import de.tudarmstadt.informatik.hostage.ui2.popup.AbstractPopup;
- import de.tudarmstadt.informatik.hostage.ui2.popup.AbstractPopupItem;
- import de.tudarmstadt.informatik.hostage.ui2.popup.SimplePopupItem;
- import de.tudarmstadt.informatik.hostage.ui2.popup.SimplePopupTable;
- /**
- * Created by Julien on 16.02.14.
- */
- public class StatisticsFragment extends Fragment implements ChecklistDialog.ChecklistDialogListener {
- static final String FILTER_MENU_TITLE_BSSID = "BSSID";
- static final String FILTER_MENU_TITLE_ESSID = "ESSID";
- static final String FILTER_MENU_TITLE_PROTOCOLS = "Protocol";
- static final String FILTER_MENU_TITLE_TIMESTAMP_BELOW = "Latest";
- static final String FILTER_MENU_TITLE_TIMESTAMP_ABOVE = "Earliest";
- static final String MENU_TITLE_PROTOCOLS = "Protocols";
- static final String MENU_TITLE_NETWORK = "Networks";
- static final String MENU_TITLE_ATTACKS = "Attacks";
- static final String MENU_POPUP_TITLE = "Visualise:";
- static final String CHART_TYPE_TITLE_BAR = "Bar Plot";
- static final String CHART_TYPE_TITLE_PIE = "Pie Plot";
- static final String CHART_TYPE_TITLE_LINE = "Line Plot";
- static final String DIALOG_PROTOCOLS_TITLE = "Select protocol data to compare";
- static final String DIALOG_NETWORK_TITLE = "Select network data to compare";
- static final String DIALOG_ATTACK_TITLE = "Select attack data to compare";
- static final String COMPARE_TITLE_AttacksPerProtocol = "Attacks per protocol";
- static final String COMPARE_TITLE_UsesPerProtocol = "Uses per protocol";
- static final String COMPARE_TITLE_AttacksPerDate = "Attacks per date";
- static final String COMPARE_TITLE_AttacksPerTime = "Attacks per time";
- static final String COMPARE_TITLE_AttacksPerBSSID = "Attacks per BSSID";
- static final String COMPARE_TITLE_AttacksPerESSID = "Attacks per ESSID";
- static final String OTHER_CHART_TITLE = "Other";
- // MINIMAL 2
- static int MAX_NUMBER_OF_CHART_OBJECTS = 5;
- private boolean wasBelowTimePicker;
- private LogFilter filter;
- private boolean showFilterButton;
- private PieGraph pieGraph;
- private LineGraph lineGraph;
- private BarGraph barGraph;
- private View rootView;
- private View currentPlotView;
- private ArrayList<Integer> colorList;
- private ArrayList<PlotComparisonItem> currentData;
- private UglyDbHelper dbh;
- private ListView legendListView;
- private String selectedCompareData = COMPARE_TITLE_AttacksPerProtocol;
- public enum ChartType {
- PIE_CHART(0),
- BAR_CHART(1),
- LINE_CHART(2);
- private int value;
- private ChartType(int value) {
- this.value = value;
- }
- static public ChartType create(int value){
- if (value < 0 || value >= ChartType.values().length) return ChartType.PIE_CHART;
- return ChartType.values()[value];
- }
- public String toString(){
- if (this.equals(ChartType.create(0))){
- return CHART_TYPE_TITLE_PIE;
- }
- if (this.equals(ChartType.create(1))){
- return CHART_TYPE_TITLE_BAR;
- }
- return CHART_TYPE_TITLE_LINE;
- }
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setHasOptionsMenu(true);
- }
- public int getLayoutID(){
- return R.layout.fragment_statistics;
- }
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- dbh = new UglyDbHelper(this.getBaseContext());
- // Get the message from the intent
- if (this.filter == null){
- Intent intent = this.getActivity().getIntent();
- LogFilter filter = intent.getParcelableExtra(LogFilter.LOG_FILTER_INTENT_KEY);
- if(filter == null){
- this.clearFilter();
- } else {
- this.filter = filter;
- }
- }
- View rootView = inflater.inflate(this.getLayoutID(), container, false);
- LinearLayout plotLayout = (LinearLayout) rootView.findViewById(R.id.plot_layout);
- plotLayout.setWillNotDraw(false);
- container.setWillNotDraw(false);
- this.legendListView = (ListView) rootView.findViewById(R.id.legend_list_view);
- this.legendListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
- StatisticsFragment.this.userTappedOnItem(i);
- }
- });
- this.rootView = rootView;
- rootView.setWillNotDraw(false);
- this.configureRootView(rootView);
- return rootView;
- }
- private Context getBaseContext(){
- return this.getActivity().getBaseContext();
- }
- private Context getApplicationContext(){
- return this.getActivity().getApplicationContext();
- }
- public void configureRootView(View rootView){
- LinearLayout plotLayout = (LinearLayout) rootView.findViewById(R.id.plot_layout);
- plotLayout.setWillNotDraw(false);
- plotLayout.removeAllViews();
- this.actualiseCurrentPlot();
- ImageButton filterButton = (ImageButton) rootView.findViewById(R.id.plot_data_button);
- filterButton.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- StatisticsFragment.this.openPopupMenuOnView(v);
- }
- });
- }
- public void setTitle(String title){
- TextView titleView = (TextView) this.rootView.findViewById(R.id.title_text_view);
- if (title != null && titleView != null){
- titleView.setText(title);
- }
- }
- public String getTitle(){
- TextView titleView = (TextView) this.rootView.findViewById(R.id.title_text_view);
- if (titleView != null){
- return "" + titleView.getText();
- }
- return "";
- }
- public void onStart() {
- super.onStart();
- this.actualiseCurrentPlot();
- this.currentPlotView.invalidate();
- }
- public void setChartType(ChartType type){
- boolean shouldChange = true;
- if (this.currentPlotView != null){
- if (type == ChartType.PIE_CHART){
- shouldChange = ! (this.currentPlotView instanceof PieGraph);
- } else {
- if (this.pieGraph != null)
- this.pieGraph.setVisibility(View.INVISIBLE);
- }
- if (type == ChartType.LINE_CHART){
- shouldChange = ! (this.currentPlotView instanceof LineGraph);
- } else {
- if (this.lineGraph != null)
- this.lineGraph.setVisibility(View.INVISIBLE);
- }
- if (type == ChartType.BAR_CHART){
- shouldChange = ! (this.currentPlotView instanceof BarGraph);
- // SHOW FILTER BUTTON
- } else {
- if (this.barGraph != null)
- this.barGraph.setVisibility(View.INVISIBLE);
- // SET FILTER BUTTON HIDDEN
- }
- }
- if (shouldChange){
- this.currentPlotView = this.getPlotViewForType(type);
- this.currentPlotView.setVisibility(View.VISIBLE);
- this.actualiseCurrentPlot();
- }
- }
- public View getPlotViewForType(ChartType type){
- switch (type){
- case PIE_CHART:
- return this.getPieGraphView();
- case LINE_CHART:
- return this.getLineGraphView();
- default:
- return this.getBarGraphView();
- }
- }
- public void actualiseLegendList(){
- StatisticListAdapter adapter = new StatisticListAdapter(this.getApplicationContext(), this.currentData);
- this.legendListView.setAdapter(adapter);
- }
- /*
- * MENU
- * */
- private void openPopupMenuOnView(View anchorView){
- SimplePopupTable visualiseMenu = new SimplePopupTable(this.getActivity(), new AbstractPopup.OnPopupItemClickListener() {
- public void onItemClick(Object ob) {
- if (ob instanceof AbstractPopupItem){
- AbstractPopupItem item = (AbstractPopupItem) ob;
- StatisticsFragment.this.userSelectMenuItem(item);
- }
- }
- });
- visualiseMenu.setTitle(MENU_POPUP_TITLE);
- int id = 0;
- for(String title : StatisticsFragment.this.getMenuTitles()){
- SimplePopupItem item = new SimplePopupItem(this.getActivity());
- item.setTitle(title);
- item.setItemId(id);
- item.setSelected(this.isFilterSetForTitle(title));
- visualiseMenu.addItem(item);
- id++;
- }
- visualiseMenu.showOnView(anchorView);
- }
- private void userSelectMenuItem(AbstractPopupItem item){
- //int id = item.getItemId();
- //this.setChartType(ChartType.create(id));
- // OPEN A DIALOG TO SPECIFY THE VISUALISE DATA
- if (item.getTitle().equals(MENU_TITLE_PROTOCOLS)){
- this.openProtocolDataDialog();
- }
- if (item.getTitle().equals(MENU_TITLE_NETWORK)){
- this.openNetworkDataDialog();
- }
- if (item.getTitle().equals(MENU_TITLE_ATTACKS)){
- this.openAttackDataDialog();
- }
- }
- private ArrayList<String> getMenuTitles(){
- ArrayList<String> titles = new ArrayList<String>();
- titles.add(MENU_TITLE_PROTOCOLS);
- titles.add(MENU_TITLE_NETWORK);
- titles.add(MENU_TITLE_ATTACKS);
- return titles;
- }
- /*
- * PLOT DATA DIALOGS
- * */
- private void openProtocolDataDialog(){
- ArrayList<String> titles = this.getDialogProtocolDataTitle();
- ChecklistDialog newFragment = new ChecklistDialog(DIALOG_PROTOCOLS_TITLE, titles, this.selectedData(titles), false , this);
- newFragment.show(this.getActivity().getFragmentManager(), DIALOG_PROTOCOLS_TITLE);
- }
- private void openNetworkDataDialog(){
- ArrayList<String> titles = this.getDialogNetworkDataTitle();
- ChecklistDialog newFragment = new ChecklistDialog(DIALOG_NETWORK_TITLE, titles, this.selectedData(titles), false , this);
- newFragment.show(this.getActivity().getFragmentManager(), DIALOG_NETWORK_TITLE);
- }
- private void openAttackDataDialog(){
- ArrayList<String> titles = this.getDialogAttackDataTitle();
- ChecklistDialog newFragment = new ChecklistDialog(DIALOG_ATTACK_TITLE, titles, this.selectedData(titles), false , this);
- newFragment.show(this.getActivity().getFragmentManager(), DIALOG_ATTACK_TITLE);
- }
- /*
- *
- * DIALOG ACTION METHODS
- *
- * */
- public void onDialogPositiveClick(ChecklistDialog dialog) {
- String title = dialog.getTitle();
- ArrayList<String> titles =dialog.getSelectedItemTitles();
- if (titles.size() != 0){
- String data = titles.get(0);
- if (title.equals(DIALOG_PROTOCOLS_TITLE)){
- ChartType chartType = ChartType.PIE_CHART;
- this.selectedCompareData = data;
- this.setChartType(chartType);
- }
- if (title.equals(DIALOG_ATTACK_TITLE)){
- ChartType chartType = ChartType.LINE_CHART;
- this.selectedCompareData = data;
- this.setChartType(chartType);
- }
- if (title.equals(DIALOG_NETWORK_TITLE)){
- ChartType chartType = ChartType.BAR_CHART;
- this.selectedCompareData = data;
- this.setChartType(chartType);
- }
- }
- }
- public void onDialogNegativeClick(ChecklistDialog dialog) {
- }
- /*
- *
- * DIALOG DATA
- *
- * */
- private ArrayList<String> getDialogProtocolDataTitle(){
- ArrayList<String> data = new ArrayList<String>();
- data.add(COMPARE_TITLE_AttacksPerProtocol);
- data.add(COMPARE_TITLE_UsesPerProtocol);
- return data;
- }
- private ArrayList<String> getDialogAttackDataTitle(){
- ArrayList<String> data = new ArrayList<String>();
- data.add(COMPARE_TITLE_AttacksPerDate);
- data.add(COMPARE_TITLE_AttacksPerTime);
- return data;
- }
- private ArrayList<String> getDialogNetworkDataTitle(){
- ArrayList<String> data = new ArrayList<String>();
- data.add(COMPARE_TITLE_AttacksPerBSSID);
- data.add(COMPARE_TITLE_AttacksPerESSID);
- return data;
- }
- private boolean[] selectedData(ArrayList<String> data){
- boolean[] selected = new boolean[data.size()];
- // SET DEFAULT
- selected[0] = true;
- return selected;
- }
- /**
- *
- * PLOT TOUCH HANDLING
- *
- * */
- public void onSliceClick(int index){
- this.userTappedOnItem(index);
- }
- public void onBarClick(int index){
- this.userTappedOnItem(index);
- }
- /*
- *
- * PLOT TYPES
- *
- * **/
- public PieGraph getPieGraphView(){
- if (this.pieGraph == null) {
- this.pieGraph = new PieGraph(this.getApplicationContext());
- LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
- plotLayout.addView(this.pieGraph);
- this.pieGraph.setOnSliceClickedListener(new PieGraph.OnSliceClickedListener() {
- @Override
- public void onClick(int index) {
- StatisticsFragment.this.onSliceClick(index);
- }
- });
- }
- return this.pieGraph;
- }
- public LineGraph getLineGraphView(){
- if (this.lineGraph == null) {
- this.lineGraph = new LineGraph(this.getActivity());
- LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
- plotLayout.addView(this.lineGraph);
- }
- return this.lineGraph;
- }
- public BarGraph getBarGraphView(){
- if (this.barGraph == null) {
- this.barGraph = new BarGraph(this.getActivity());
- LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
- this.barGraph.setLayoutParams(new ViewGroup.LayoutParams(plotLayout.getWidth(), plotLayout.getHeight()));
- plotLayout.addView(this.barGraph);
- this.barGraph.setPopupImageID(R.drawable.popup_black);
- this.barGraph.setOnBarClickedListener(new BarGraph.OnBarClickedListener() {
- @Override
- public void onClick(int i) {
- StatisticsFragment.this.onBarClick(i);
- }
- });
- }
- return this.barGraph;
- }
- /*
- * FEED PLOTS WITH DATA
- * */
- public void setPieGraphData(PieGraph piegraph){
- this.currentData = this.getPieData();
- if (this.currentData == null){
- this.currentData = new ArrayList<PlotComparisonItem>();
- }
- this.pieGraph.removeSlices();
- for (PlotComparisonItem item : this.currentData){
- PieSlice slice = new PieSlice();
- slice.setColor(item.getColor());
- Double value2 = (Double) item.getValue2();
- float v = value2.floatValue();
- slice.setValue(v);
- slice.setTitle(item.getTitle());
- this.pieGraph.addSlice(slice);
- }
- this.pieGraph.invalidate();
- }
- public void setLineGraphData(LineGraph linegraph){
- this.currentData = this.getLineData();
- if (this.currentData == null){
- this.currentData = new ArrayList<PlotComparisonItem>();
- }
- int index = 0;
- this.lineGraph.removeAllLines();
- Line l = new Line();
- for (PlotComparisonItem item : this.currentData){
- LinePoint p = new LinePoint();
- p.setX(index);
- Double value2 = (Double) item.getValue2();
- p.setY(value2.floatValue());
- index++;
- }
- this.lineGraph.addLine(l);
- this.lineGraph.setRangeY(0, index);
- this.lineGraph.setLineToFill(0);
- this.lineGraph.invalidate();
- }
- public void setBarGraphData(BarGraph bargraph){
- this.currentData = this.getBarData();
- if (this.currentData == null){
- this.currentData = new ArrayList<PlotComparisonItem>();
- }
- ArrayList<Bar> bars = new ArrayList<Bar>();
- for (PlotComparisonItem item : this.currentData){
- Bar d = new Bar();
- d.setColor(item.getColor());
- d.setName(item.getTitle());
- Double value2 = (Double) item.getValue2();
- d.setValue(value2.floatValue());
- bars.add(d);
- }
- this.barGraph.setBars(bars);
- this.barGraph.invalidate();
- }
- /*
- *
- * FETCH & ACTUALISE RECORD DATA
- *
- * */
- public ArrayList<Record> getFetchedRecords(){
- if (this.filter == null) this.clearFilter();
- return this.dbh.getRecordsForFilter(this.filter);
- }
- public void actualiseCurrentPlot(){
- LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
- View plot = this.currentPlotView;
- if (plot == null){
- this.currentPlotView = this.getPieGraphView();
- plot = this.currentPlotView;
- }
- if (plot.getVisibility() == View.INVISIBLE) plot.setVisibility(View.VISIBLE);
- if (plot instanceof PieGraph){
- PieGraph pie = (PieGraph) plot;
- this.setPieGraphData(pie);
- } else {
- if (this.pieGraph != null){
- this.pieGraph.setVisibility(View.INVISIBLE);
- if (this.pieGraph.getParent() != null){
- plotLayout.removeView(this.pieGraph);
- }
- }
- }
- if (plot instanceof BarGraph){
- BarGraph bar = (BarGraph) plot;
- this.setBarGraphData(bar);
- } else {
- if (this.barGraph != null){
- this.barGraph.setVisibility(View.INVISIBLE);
- if (this.barGraph.getParent() != null){
- plotLayout.removeView(this.barGraph);
- }
- }
- }
- if (plot instanceof LineGraph){
- LineGraph line = (LineGraph)plot;
- this.setLineGraphData(line);
- }else {
- if (this.lineGraph != null){
- this.lineGraph.setVisibility(View.INVISIBLE);
- if (this.lineGraph.getParent() != null){
- plotLayout.removeView(this.lineGraph);
- }
- }
- }
- plot.setVisibility(View.VISIBLE);
- if (plot.getParent() == null){
- plotLayout.addView(plot);
- }
- this.actualiseLegendList();
- this.currentPlotView.bringToFront();
- this.currentPlotView.invalidate();
- }
- public ArrayList<PlotComparisonItem> getPieData(){
- if (this.selectedCompareData.equals(COMPARE_TITLE_UsesPerProtocol)){
- return this.usesPerProtocol();
- }
- // DEFAULT
- return this.attacksPerProtocols();
- }
- public ArrayList<PlotComparisonItem> getBarData(){
- ArrayList<String> protocolTitles = this.getSelectedProtocolTitles();
- if (protocolTitles != null && protocolTitles.size() != 0){
- String protocol = this.getSelectedProtocolTitles().get(0);
- return this.attacksPerBSSID(protocol);
- }
- // Nothing available
- return new ArrayList<PlotComparisonItem>();
- }
- public ArrayList<PlotComparisonItem> getLineData(){
- if (this.selectedCompareData.equals(COMPARE_TITLE_AttacksPerTime)){
- return this.attacksPerTime();
- }
- // DEFAULT
- return this.attacksPerDate();
- }
- /*
- * DATA SOURCE
- * */
- /*PROTOCOLS OVERVIEW*/
- public ArrayList<PlotComparisonItem> attacksPerProtocols(){
- ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
- int index = 0;
- for (String title : this.getSelectedProtocolTitles()){
- int attacksCount = this.dbh.getAttackPerProtocolCount(title);
- if (attacksCount == 0) continue;
- PlotComparisonItem item = new PlotComparisonItem(title,this.getColor(index), 0., (double) attacksCount);
- plotItems.add(item);
- index++;
- }
- Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
- @Override
- public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
- return s1.getValue2().compareTo(s2.getValue2());
- }
- });
- return this.resizeData(plotItems);
- }
- public ArrayList<PlotComparisonItem> usesPerProtocol(){
- ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
- for (String title : this.getSelectedProtocolTitles()){
- /*At the moment there is no possibillity to get the number of uses for each protocol*/
- // TODO GET USES PER PROTOCOL
- int uses = 0;
- if (uses == 0) continue;
- // ADD ITEMS
- }
- Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
- @Override
- public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
- return s1.getTitle().compareToIgnoreCase(s2.getTitle());
- }
- });
- return plotItems;
- }
- /*LINE PLOT DATA*/
- public ArrayList<PlotComparisonItem> attacksPerDate(){
- ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
- HashMap<Long, ArrayList<Record> > recordMap = new HashMap<Long, ArrayList<Record> >();
- for (Record record : this.getFetchedRecords()){
- long date = this.getDateFromMilliseconds(record.getTimestamp());
- ArrayList<Record> list = recordMap.get(date);
- if (list == null){
- list = new ArrayList<Record>();
- recordMap.put(date, list);
- }
- list.add(record);
- }
- int index = 0;
- for (long date : recordMap.keySet()){
- ArrayList<Record> list = recordMap.get(date);
- if (list.size() == 0) continue;
- PlotComparisonItem item = new PlotComparisonItem(this.getDateAsDayString(date), this.getColor(index), (double) date, (double)list.size());
- plotItems.add(item);
- index++;
- }
- Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
- @Override
- public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
- return s1.getValue1().compareTo(s2.getValue1());
- }
- });
- return plotItems;
- }
- public ArrayList<PlotComparisonItem> attacksPerTime(){
- ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
- HashMap<Long, ArrayList<Record> > recordMap = new HashMap<Long, ArrayList<Record> >();
- for (Record record : this.getFetchedRecords()){
- long date = this.getDayHourFromDate(record.getTimestamp());
- ArrayList<Record> list = recordMap.get(date);
- if (list == null){
- list = new ArrayList<Record>();
- recordMap.put(date, list);
- }
- list.add(record);
- }
- int index = 0;
- for (long time : recordMap.keySet()){
- ArrayList<Record> list = recordMap.get(time);
- if (list.size() == 0) continue;
- PlotComparisonItem item = new PlotComparisonItem(this.getDateAsTimeString(time), this.getColor(index) , (double)time, (double) list.size());
- plotItems.add(item);
- index++;
- }
- Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
- @Override
- public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
- return s1.getValue1().compareTo(s2.getValue1());
- }
- });
- return plotItems;
- }
- // BAR PLOT DATA
- public ArrayList<PlotComparisonItem> attacksPerBSSID(String protocol){
- LogFilter filter = new LogFilter();
- ArrayList<String> protocollist = new ArrayList<String>();
- protocollist.add(protocol);
- filter.setProtocols(protocollist);
- ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
- HashMap<String, Integer> recordMap = new HashMap<String, Integer>();
- ArrayList<Record> records = this.dbh.getRecordsForFilter(filter);
- for (Record record : records){
- int count = 0;
- if (recordMap.containsKey(record.getBssid())){
- count = recordMap.get(record.getBssid());
- }
- count++;
- recordMap.put(record.getBssid(), count);
- }
- int index = 0;
- for (String key : recordMap.keySet()){
- double value = (double)recordMap.get(key);
- if (value == 0.) continue;
- PlotComparisonItem item = new PlotComparisonItem(key, this.getColor(index), 0., value);
- plotItems.add(item);
- index++;
- }
- Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
- @Override
- public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
- return s1.getValue2().compareTo(s2.getValue2());
- }
- });
- return this.resizeData(plotItems);
- }
- public ArrayList<PlotComparisonItem> attacksPerESSID(String protocol){
- LogFilter filter = new LogFilter();
- ArrayList<String> protocollist = new ArrayList<String>();
- protocollist.add(protocol);
- filter.setProtocols(protocollist);
- ArrayList<PlotComparisonItem> plotItems = new ArrayList<PlotComparisonItem>();
- HashMap<String, Integer> recordMap = new HashMap<String, Integer>();
- ArrayList<Record> records = this.dbh.getRecordsForFilter(filter);
- for (Record record : records){
- int count = 0;
- if (recordMap.containsKey(record.getSsid())){
- count = recordMap.get(record.getSsid());
- }
- count++;
- recordMap.put(record.getSsid(), count);
- }
- int index = 0;
- for (String key : recordMap.keySet()){
- double value = (double)recordMap.get(key);
- if (value == 0.) continue;
- PlotComparisonItem item = new PlotComparisonItem(key,this.getColor(index), 0. ,value);
- plotItems.add(item);
- index++;
- }
- Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
- @Override
- public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
- return s1.getValue2().compareTo(s2.getValue2());
- }
- });
- return this.resizeData(plotItems);
- }
- private ArrayList<PlotComparisonItem> resizeData(ArrayList<PlotComparisonItem> plotItems){
- if (plotItems != null){
- if (plotItems.size() > MAX_NUMBER_OF_CHART_OBJECTS && MAX_NUMBER_OF_CHART_OBJECTS > 1){
- ArrayList<PlotComparisonItem> copy = new ArrayList<PlotComparisonItem>();
- ArrayList<PlotComparisonItem> others = new ArrayList<PlotComparisonItem>();
- double valueOfOthers = 0;
- for (int i = 0; i < plotItems.size(); i++){
- if (i < MAX_NUMBER_OF_CHART_OBJECTS - 1){
- PlotComparisonItem item = plotItems.get(i);
- item.setColor(this.getColor(i));
- copy.add(plotItems.get(i));
- } else {
- PlotComparisonItem item = plotItems.get(i);
- others.add(item);
- valueOfOthers+=item.getValue2();
- }
- }
- PlotComparisonItem otherItem = new PlotComparisonItem(OTHER_CHART_TITLE, this.getOtherColor(), 0., valueOfOthers);
- otherItem.setOtherData(others);
- copy.add(otherItem);
- return copy;
- }
- }
- return plotItems;
- }
- /*
- * FILTER STUFF
- * */
- public ArrayList<String> protocolTitles(){
- ArrayList<String> titles = new ArrayList<String>();
- for (String protocol : this.getResources().getStringArray(
- R.array.protocols)) {
- titles.add(protocol);
- }
- return titles;
- }
- public boolean[] selectedProtocols(){
- ArrayList<String> protocols = this.protocolTitles();
- boolean[] selected = new boolean[protocols.size()];
- int i = 0;
- for(String protocol : protocols){
- selected[i] =(this.filter.protocols.contains(protocol));
- i++;
- }
- return selected;
- }
- public ArrayList<String> getSelectedProtocolTitles(){
- ArrayList<String> knownProtocols = this.protocolTitles();
- if (this.filter.hasProtocols()){
- ArrayList<String> titles = new ArrayList<String>();
- int i =0;
- for (boolean b : this.selectedProtocols()){
- if (b){
- String title = knownProtocols.get(i);
- titles.add(title);
- }
- i++;
- }
- return titles;
- }
- return this.protocolTitles();
- }
- /*
- *
- * COLOR STUFF
- *
- * */
- public int getOtherColor(){
- return Color.argb(255,0,0,0);
- }
- public ArrayList<Integer> getColorList(){
- if (this.colorList == null){
- this.colorList = new ArrayList<Integer>();
- for (int i =0; i < 255; i++){
- Integer color = this.generateColorForIndex(i);
- this.colorList.add(color);
- }
- }
- return this.colorList;
- }
- public Integer getColor(int index){
- if (this.colorList == null) this.colorList = new ArrayList<Integer>();
- if (index >= this.colorList.size()) {
- for (int i = this.colorList.size(); i<= index; i++){
- Integer color = this.generateColorForIndex(i);
- this.colorList.add(color);
- }
- return this.generateColorForIndex(index);
- }
- return this.colorList.get(index);
- }
- public Integer generateColorForIndex(int index){
- int r[] = new int[]{0,0,0,1,1,1,1};
- int g[] = new int[]{0,1,1,1,1,0,0};
- int b[] = new int[]{1,1,0,0,1,1,0};
- int a = index % 16;
- int n = index % 7;
- int R = Math.max( (r[n] * 255) - ((Math.max(0,(a ))) * 16), 0);
- int G = Math.max( (g[n] * 255) - ((Math.max(0,(a ))) * 16), 0);
- int B = Math.max( (b[n] * 255) - ((Math.max(0,(a ))) * 16), 0);
- return Color.argb(255,R,G,B);
- }
- public LinearLayout getPlotLayout(){
- if (this.rootView != null){
- return (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
- } else {
- return null;
- }
- }
- /**
- *
- * FILTER STUFF
- *
- * */
- private boolean isFilterSetForTitle(String title){
- if (title.equals(FILTER_MENU_TITLE_BSSID)){
- return this.filter.hasBSSIDs();
- }
- if (title.equals(FILTER_MENU_TITLE_ESSID)){
- return this.filter.hasESSIDs();
- }
- if (title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
- return this.filter.hasProtocols();
- }
- if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)){
- return this.filter.hasBelowTimestamp();
- }
- if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)){
- return this.filter.hasAboveTimestamp();
- }
- return false;
- }
- private void clearFilter(){
- if(filter == null) this.filter = new LogFilter();
- this.filter.clear();
- }
- /*
- *
- * DATE TRANSFORMATION
- *
- */
- public long getDayHourFromDate(long timeInMillis){
- Calendar calendar = Calendar.getInstance();
- calendar.setTimeInMillis (timeInMillis);
- int hour = calendar.get(Calendar.HOUR);
- int min = calendar.get(Calendar.MINUTE);
- return (hour*60*60)*1000;
- }
- public long getDateFromMilliseconds(long timeInMillis){
- Calendar calendar = Calendar.getInstance();
- calendar.setTimeInMillis (timeInMillis);
- int year = calendar.get(Calendar.YEAR) ;
- int month = calendar.get(Calendar.MONTH);
- int day = calendar.get(Calendar.DATE);
- calendar.set(year, month, day, 0,0);
- return calendar.getTimeInMillis();
- }
- static final DateFormat timeFormat = new SimpleDateFormat("H:mm");
- @SuppressLint("SimpleDateFormat")
- private String getDateAsTimeString(long timeStamp) {
- try {
- Date netDate = (new Date(timeStamp));
- return timeFormat.format(netDate);
- } catch (Exception ex) {
- return "xx";
- }
- }
- static final DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
- @SuppressLint("SimpleDateFormat")
- private String getDateAsDayString(long timeStamp) {
- try {
- Date netDate = (new Date(timeStamp));
- return dateFormat.format(netDate);
- } catch (Exception ex) {
- return "xx";
- }
- }
- /**
- * USERINTERACTION
- */
- private void userTappedOnItem(int index){
- if (index < this.currentData.size()){
- PlotComparisonItem item = this.currentData.get(index);
- ArrayList<String> selectedData;
- selectedData = new ArrayList<String>();
- if (item.getOtherData() == null){
- selectedData.add(item.getTitle());
- } else {
- for (PlotComparisonItem other : item.getOtherData()){
- selectedData.add(other.getTitle());
- }
- }
- LogFilter filter = new LogFilter();
- if (this.currentPlotView instanceof PieGraph){
- filter.setProtocols(selectedData);
- }
- if (this.currentPlotView instanceof BarGraph){
- // TODO set data for BSSID / ESSID
- }
- if (this.currentPlotView instanceof LineGraph){
- // TODO set data for BSSID / ESSID
- }
- this.pushRecordOverviewForFilter(filter);
- }
- }
- private void pushRecordOverviewForFilter(LogFilter filter){
- }
- }
|