Преглед на файлове

fixed many bugs in statistic view

Julien Clauter преди 10 години
родител
ревизия
90a8f16c94

+ 43 - 2
res/layout/fragment_statistics.xml

@@ -6,14 +6,52 @@
     android:background="@android:color/transparent"
     >
 
+
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:orientation="vertical"
-        android:layout_height="match_parent"
+        android:layout_height="40dp"
         android:layout_width="match_parent"
-        android:id="@+id/plot_layout">
+        android:id="@+id/title_layout"
+        android:padding="5dp"
+        android:layout_alignParentTop="true"
+        android:layout_alignParentLeft="true"
+        android:layout_alignParentStart="true">
 
+        <TextView
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:textAppearance="?android:attr/textAppearanceMedium"
+            android:text="Title"
+            android:textAlignment="center"
+            android:id="@+id/title_textView" />
     </LinearLayout>
 
+    <LinearLayout
+        android:orientation="vertical"
+        android:layout_width="fill_parent"
+        android:layout_height="wrap_content"
+        android:layout_above="@+id/linearLayout"
+        android:layout_below="@+id/title_layout"
+        android:weightSum="2">
+
+        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+            android:orientation="vertical"
+            android:layout_height="fill_parent"
+            android:layout_width="fill_parent"
+            android:id="@+id/plot_layout"
+            android:padding="2dp"
+            android:layout_weight="0.9">
+
+        </LinearLayout>
+
+        <ListView
+            android:layout_width="fill_parent"
+            android:layout_height="fill_parent"
+            android:id="@+id/listView"
+            android:layout_weight="1.1"
+            />
+
+        </LinearLayout>
 
 
     <RelativeLayout
@@ -57,4 +95,7 @@
             android:layout_toLeftOf="@+id/PiePlotButton"/>
     </RelativeLayout>
 
+
+
+
 </RelativeLayout>

+ 115 - 31
src/de/tudarmstadt/informatik/hostage/ui2/fragment/StatisticsFragment.java

@@ -55,6 +55,11 @@ public class StatisticsFragment extends Fragment {
     static final String CHART_TYPE_TITLE_PIE = "Pie Plot";
     static final String CHART_TYPE_TITLE_LINE = "Line Plot";
 
+    static final String OTHER_CHART_TITLE = "Other";
+
+    // MINIMAL 2
+    static int MAX_NUMBER_OF_CHART_OBJECTS = 5;
+
     private boolean wasBelowTimePicker;
 
     private LogFilter filter;
@@ -145,9 +150,39 @@ public class StatisticsFragment extends Fragment {
     }
 
     public void configureRootView(View rootView){
-        LinearLayout plotLayout = (LinearLayout) rootView.findViewById(R.id.plot_layout);
-        // DEFAULT
-        this.actualiseCurrentPlot();
+        this.setChartType(ChartType.PIE_CHART);
+    }
+
+    public void setChartType(ChartType type){
+        boolean shouldChange = true;
+        if (this.currentPlotView != null){
+            if (type == ChartType.PIE_CHART){
+                shouldChange = ! (this.currentPlotView instanceof PieGraph);
+            }
+            if (type == ChartType.LINE_CHART){
+                shouldChange = ! (this.currentPlotView instanceof LineGraph);
+            }
+            if (type == ChartType.BAR_CHART){
+                shouldChange = ! (this.currentPlotView instanceof BarGraph);
+            }
+        }
+        if (shouldChange){
+            LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
+            this.currentPlotView = this.getPlotViewForType(type);
+            plotLayout.addView(this.currentPlotView);
+            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();
+        }
     }
 
     /**
@@ -174,7 +209,6 @@ public class StatisticsFragment extends Fragment {
                 public void onClick(int index) {
                     StatisticsFragment.this.onSliceClick(index);
                 }
-
             });
         }
         /*
@@ -212,23 +246,28 @@ public class StatisticsFragment extends Fragment {
     * */
     public void setPieGraphData(PieGraph piegraph){
         this.currentData = this.getPieData();
-        int index = 0;
+        if (this.currentData == null){
+            this.currentData = new ArrayList<PlotComparisonItem>();
+        }
 
         this.pieGraph.removeSlices();
 
         for (PlotComparisonItem item : this.currentData){
             PieSlice slice = new PieSlice();
-            slice.setColor(this.getColor(index));
-            slice.setValue((Float) item.getValue2());
+            slice.setColor(item.getColor());
+            Double value2 = (Double) item.getValue2();
+            slice.setValue(value2.floatValue());
             slice.setTitle(item.getTitle());
             this.pieGraph.addSlice(slice);
-            index++;
         }
         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();
@@ -237,7 +276,8 @@ public class StatisticsFragment extends Fragment {
         for (PlotComparisonItem item : this.currentData){
             LinePoint p = new LinePoint();
             p.setX(index);
-            p.setY((Float) item.getValue2());
+            Double value2 = (Double) item.getValue2();
+            p.setY(value2.floatValue());
             index++;
         }
         this.lineGraph.addLine(l);
@@ -248,16 +288,18 @@ public class StatisticsFragment extends Fragment {
 
     public void setBarGraphData(BarGraph bargraph){
         this.currentData = this.getLinedata();
-        int index = 0;
+        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(this.getColor(index));
+            d.setColor(item.getColor());
             d.setName(item.getTitle());
-            d.setValue((Float)item.getValue2());
-            index++;
+            Double value2 = (Double) item.getValue2();
+            d.setValue(value2.floatValue());
         }
 
         this.barGraph.setBars(bars);
@@ -325,20 +367,22 @@ public class StatisticsFragment extends Fragment {
     /*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);
-            PlotComparisonItem item = new PlotComparisonItem(title, title, attacksCount);
+            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.getTitle().compareToIgnoreCase(s2.getTitle());
+                 return s1.getValue2().compareTo(s2.getValue2());
              }
          });
-        return plotItems;
+         return this.resizeData(plotItems);
     }
-    public ArrayList<PlotComparisonItem> usesPerProtocols(){
+    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*/
@@ -366,15 +410,17 @@ public class StatisticsFragment extends Fragment {
             }
             list.add(record);
         }
+        int index = 0;
         for (long date : recordMap.keySet()){
             ArrayList<Record> list = recordMap.get(date);
-            PlotComparisonItem item = new PlotComparisonItem(this.getDateAsDayString(date), date, list.size());
+            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.getTitle().compareToIgnoreCase(s2.getTitle());
+                return s1.getValue1().compareTo(s2.getValue1());
             }
         });
         return  plotItems;
@@ -392,15 +438,17 @@ public class StatisticsFragment extends Fragment {
             }
             list.add(record);
         }
+        int index = 0;
         for (long time : recordMap.keySet()){
             ArrayList<Record> list = recordMap.get(time);
-            PlotComparisonItem item = new PlotComparisonItem(this.getDateAsTimeString(time), time, list.size());
+            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.getTitle().compareToIgnoreCase(s2.getTitle());
+                return s1.getValue1().compareTo(s2.getValue1());
             }
         });
         return  plotItems;
@@ -421,17 +469,19 @@ public class StatisticsFragment extends Fragment {
             count++;
             recordMap.put(record.getBssid(), count);
         }
+        int index = 0;
         for (String key : recordMap.keySet()){
-            PlotComparisonItem item = new PlotComparisonItem(key, key, recordMap.get(key));
+            PlotComparisonItem item = new PlotComparisonItem(key, this.getColor(index), 0., (double)recordMap.get(key));
             plotItems.add(item);
+            index++;
         }
         Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
             @Override
             public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
-                return s1.getTitle().compareToIgnoreCase(s2.getTitle());
+                return s1.getValue2().compareTo(s2.getValue2());
             }
         });
-        return plotItems;
+        return this.resizeData(plotItems);
     }
     public ArrayList<PlotComparisonItem> attacksPerESSID(String protocol){
         LogFilter filter = new LogFilter();
@@ -448,20 +498,48 @@ public class StatisticsFragment extends Fragment {
             count++;
             recordMap.put(record.getSsid(), count);
         }
+        int index = 0;
         for (String key : recordMap.keySet()){
-            PlotComparisonItem item = new PlotComparisonItem(key, key, recordMap.get(key));
+            PlotComparisonItem item = new PlotComparisonItem(key,this.getColor(index), 0. , (double)recordMap.get(key));
             plotItems.add(item);
+            index++;
         }
         Collections.sort(plotItems, new Comparator<PlotComparisonItem>() {
             @Override
             public int compare(PlotComparisonItem s1, PlotComparisonItem s2) {
-                return s1.getTitle().compareToIgnoreCase(s2.getTitle());
+                return s1.getValue2().compareTo(s2.getValue2());
             }
         });
-        return plotItems;
+
+        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){
+                        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);
+                copy.add(otherItem);
+
+                return copy;
+            }
+        }
+        return plotItems;
+    }
+
     /*
     * FILTER STUFF
     * */
@@ -506,6 +584,11 @@ public class StatisticsFragment extends Fragment {
     *
     * */
 
+
+    public int getOtherColor(){
+        return Color.rgb(0,0,0);
+    }
+
      public ArrayList<Integer> getColorList(){
         if (this.colorList == null){
             this.colorList = new ArrayList<Integer>();
@@ -518,7 +601,8 @@ public class StatisticsFragment extends Fragment {
     }
 
     public Integer getColor(int index){
-        if (index > this.colorList.size()) {
+        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);
@@ -536,9 +620,9 @@ public class StatisticsFragment extends Fragment {
         int a = index % 16;
         int n = index % 7;
 
-        int R = Math.max( (r[n] * 255) - (a * 16) , 0);
-        int G = Math.max( (g[n] * 255) - ((Math.max(0,(a -1))) * 16), 0);
-        int B = Math.max( (b[n] * 255) - ((Math.max(0,(a -2))) * 16), 0);
+        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.rgb(R,G,B);
     }

+ 21 - 6
src/de/tudarmstadt/informatik/hostage/ui2/model/PlotComparisonItem.java

@@ -1,30 +1,45 @@
 package de.tudarmstadt.informatik.hostage.ui2.model;
 
+import java.util.ArrayList;
+
 /**
  * Created by Julien on 16.02.14.
  */
 public class PlotComparisonItem {
 
-    private Object value1;
-    private Object value2;
+    private Double value1;
+    private Double value2;
 
     private String title;
 
-    public PlotComparisonItem(String title, Object value1, Object value2){
+    private int color;
+
+    private ArrayList<Object> otherData;
+
+    public PlotComparisonItem(String title, int color , Double value1, Double value2){
         super();
+        this.color = color;
         this.title = title;
         this.value1 = value1;
         this.value2 = value2;
     }
 
-
+    public ArrayList<Object> getOtherData(){
+        return this.otherData;
+    }
+    public void setOtherData(ArrayList<Object> other){
+        this.otherData = other;
+    }
     public String getTitle(){
         return this.title;
     }
-    public Object getValue1(){
+    public Double getValue1(){
         return this.value1;
     }
-    public Object getValue2(){
+    public Double getValue2(){
         return this.value2;
     }
+    public int getColor(){
+        return this.color;
+    }
 }