Browse Source

bug fixes; statistics now load data in background thread

Julien Clauter 10 years ago
parent
commit
44f9891f43

+ 2 - 1
res/layout/fragment_record_list.xml

@@ -11,7 +11,8 @@
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerHorizontal="true"
-        android:layout_centerVertical="true"/>
+        android:layout_centerVertical="true"
+        android:visibility="gone"/>
 
             <ExpandableListView
                 xmlns:android="http://schemas.android.com/apk/res/android"

+ 9 - 0
res/layout/fragment_statistics.xml

@@ -21,6 +21,15 @@
                 android:id="@+id/plot_parent_layout"
                 android:layout_weight="0.9">
 
+                <ProgressBar
+                    android:id="@+id/progressBar1"
+                    style="?android:attr/progressBarStyleLarge"
+                    android:layout_width="wrap_content"
+                    android:layout_height="wrap_content"
+                    android:layout_centerHorizontal="true"
+                    android:layout_centerVertical="true"
+                    android:visibility="gone"/>
+
                 <!-- TITLE LAYOUT -->
                 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                     android:orientation="vertical"

+ 1 - 1
src/com/echo/holographlibrary/BarGraph.java

@@ -82,7 +82,7 @@ public class BarGraph extends View {
     public void setBars(ArrayList<Bar> points){
         this.mBars = points;
         mShouldUpdate = true;
-        postInvalidate();
+        //postInvalidate();
     }
     
     public ArrayList<Bar> getBars(){

+ 8 - 8
src/com/echo/holographlibrary/LineGraph.java

@@ -108,13 +108,13 @@ public class LineGraph extends View {
 			lines.remove(0);
 		}
 		shouldUpdate = true;
-		postInvalidate();
+		//postInvalidate();
 	}
 	
 	public void addLine(Line line) {
 		lines.add(line);
 		shouldUpdate = true;
-		postInvalidate();
+		//postInvalidate();
 	}
 	public void addPointToLine(int lineIndex, double x, double y){
 		addPointToLine(lineIndex, (float) x, (float) y);
@@ -145,7 +145,7 @@ public class LineGraph extends View {
 		lines.set(lineIndex, line);
 		resetLimits();
 		shouldUpdate = true;
-		postInvalidate();
+		//postInvalidate();
 	}
 	
 	public void addPointsToLine(int lineIndex, LinePoint[] points){
@@ -156,7 +156,7 @@ public class LineGraph extends View {
 		lines.set(lineIndex, line);
 		resetLimits();
 		shouldUpdate = true;
-		postInvalidate();
+		//postInvalidate();
 	}
 	
 	public void removeAllPointsAfter(int lineIndex, double x){
@@ -177,7 +177,7 @@ public class LineGraph extends View {
 		lines.set(lineIndex, line);
 		resetLimits();
 		shouldUpdate = true;
-		postInvalidate();
+		//postInvalidate();
 	}
 	public void removePointsFromLine(int lineIndex, LinePoint[] points){
 		Line line = getLine(lineIndex);
@@ -187,7 +187,7 @@ public class LineGraph extends View {
 		lines.set(lineIndex, line);
 		resetLimits();
 		shouldUpdate = true;
-		postInvalidate();
+		//postInvalidate();
 	}
 	public void removePointFromLine(int lineIndex, float x, float y){
 		LinePoint p = null;
@@ -201,7 +201,7 @@ public class LineGraph extends View {
 		lines.set(lineIndex, line);
 		resetLimits();
 		shouldUpdate = true;
-		postInvalidate();
+		//postInvalidate();
 	}
 	
 	public void resetYLimits(){
@@ -224,7 +224,7 @@ public class LineGraph extends View {
 	public void setLineToFill(int indexOfLine) {
 		this.lineToFill = indexOfLine;
 		shouldUpdate = true;
-		postInvalidate();
+		//postInvalidate();
 	}
 	public int getLineToFill(){
 		return lineToFill;

+ 17 - 8
src/com/echo/holographlibrary/PieGraph.java

@@ -39,6 +39,7 @@ import android.view.View;
 import android.widget.TextView;
 
 import java.util.ArrayList;
+import java.util.Iterator;
 
 import de.tudarmstadt.informatik.hostage.R;
 import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
@@ -253,7 +254,7 @@ public class PieGraph extends View {
 	}
 	public void setSlices(ArrayList<PieSlice> slices) {
 		this.slices = slices;
-		postInvalidate();
+		//postInvalidate();
 	}
 	public PieSlice getSlice(int index) {
 		return slices.get(index);
@@ -263,12 +264,17 @@ public class PieGraph extends View {
 		    this.slices.add(slice);
 
         long countedValue = 0;
-        for (PieSlice s : this.slices){
-            countedValue+=s.getValue();
+
+        Iterator<PieSlice> iter = this.slices.iterator();
+
+        while (iter.hasNext()){
+            PieSlice s = iter.next();
+            countedValue+= s.getValue();
         }
+
         this.title = ALL_TITLE;
         this.subtitle = "" + countedValue;
-		postInvalidate();
+		//postInvalidate();
 	}
 	public void setOnSliceClickedListener(OnSliceClickedListener listener) {
 		this.listener = listener;
@@ -279,17 +285,20 @@ public class PieGraph extends View {
 	}
 	public void setThickness(int thickness) {
 		this.thickness = thickness;
-		postInvalidate();
+		//postInvalidate();
 	}
 	
 	public void removeSlices(){
-		for (int i = slices.size()-1; i >= 0; i--){
-			slices.remove(i);
+        Iterator<PieSlice> iter = slices.iterator();
+
+        while (iter.hasNext()) {
+            iter.next();
+            iter.remove();
 		}
         this.title = "";
         this.subtitle = "";
 
-		postInvalidate();
+		//postInvalidate();
 	}
 
 	public static interface OnSliceClickedListener {

+ 11 - 5
src/de/tudarmstadt/informatik/hostage/deprecated/UglyDbHelper.java

@@ -514,7 +514,10 @@ public class UglyDbHelper extends SQLiteOpenHelper {
 		String countQuery = "SELECT  * FROM " + TABLE_ATTACK_INFO;
 		SQLiteDatabase db = this.getReadableDatabase();
 		Cursor cursor = db.rawQuery(countQuery, null);
-		int result = cursor.getCount();
+
+        if (!cursor.moveToFirst()) return 0;
+
+        int result = cursor.getCount();
 		cursor.close();
 
 		// return count
@@ -535,12 +538,15 @@ public class UglyDbHelper extends SQLiteOpenHelper {
 	public int getAttackPerProtocolCount(String protocol) {
 		String countQuery = "SELECT  * FROM " + TABLE_ATTACK_INFO + " WHERE " + KEY_PROTOCOL + " = " + "'" + protocol + "'";
 		SQLiteDatabase db = this.getReadableDatabase();
-		Cursor cursor = db.rawQuery(countQuery, null);
+
+        Cursor cursor = db.rawQuery(countQuery, null);
+
+        if (!cursor.moveToFirst()) return 0;
 		int result = cursor.getCount();
-		cursor.close();
 
-		// return count
-		db.close();
+        cursor.close();
+		//db.close();
+
 		return result;
 	}
 

+ 108 - 48
src/de/tudarmstadt/informatik/hostage/ui2/fragment/StatisticsFragment.java

@@ -1,6 +1,7 @@
 package de.tudarmstadt.informatik.hostage.ui2.fragment;
 
 import android.annotation.SuppressLint;
+import android.app.Activity;
 import android.app.Fragment;
 import android.app.FragmentManager;
 import android.content.Context;
@@ -15,6 +16,7 @@ import android.widget.AdapterView;
 import android.widget.ImageButton;
 import android.widget.LinearLayout;
 import android.widget.ListView;
+import android.widget.ProgressBar;
 import android.widget.TextView;
 
 import com.echo.holographlibrary.Bar;
@@ -35,8 +37,8 @@ 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.deprecated.UglyDbHelper;
+import de.tudarmstadt.informatik.hostage.logging.Record;
 import de.tudarmstadt.informatik.hostage.ui.LogFilter;
 import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
 import de.tudarmstadt.informatik.hostage.ui2.adapter.StatisticListAdapter;
@@ -114,6 +116,8 @@ public class StatisticsFragment extends Fragment implements ChecklistDialog.Chec
     private View rootView;
     private View currentPlotView;
 
+    private Thread loader;
+    private ProgressBar spinner;
 
     private ArrayList<Integer> colorList;
     private ArrayList<PlotComparisonItem> currentData;
@@ -224,6 +228,9 @@ public class StatisticsFragment extends Fragment implements ChecklistDialog.Chec
 
         plotLayout.setWillNotDraw(false);
 
+        this.spinner = (ProgressBar) rootView.findViewById(R.id.progressBar1);
+        this.spinner.setVisibility(View.GONE);
+
         this.legendListView = (ListView) rootView.findViewById(R.id.legend_list_view);
         this.legendListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             @Override
@@ -954,7 +961,17 @@ public class StatisticsFragment extends Fragment implements ChecklistDialog.Chec
 
     /*Actualises the current plot.*/
      public void actualiseCurrentPlot(){
+         this.spinner.setVisibility(View.VISIBLE);
+
          LinearLayout plotLayout = (LinearLayout) this.rootView.findViewById(R.id.plot_layout);
+
+         if (this.barGraph != null)
+            this.barGraph.setVisibility(View.GONE);
+         if (this.lineGraph != null)
+            this.lineGraph.setVisibility(View.GONE);
+         if (this.pieGraph != null)
+            this.pieGraph.setVisibility(View.GONE);
+
         View plot = this.currentPlotView;
          if (plot == null){
              this.currentPlotView = this.getPieGraphView();
@@ -967,54 +984,97 @@ public class StatisticsFragment extends Fragment implements ChecklistDialog.Chec
              plotLayout.addView(plot);
          }
 
-         if (plot.getVisibility() == View.GONE) plot.setVisibility(View.VISIBLE);
+         this.currentPlotView = plot;
 
-        if (plot instanceof PieGraph){
-            PieGraph pie = (PieGraph) plot;
-            this.setPieGraphData(pie);
-            // HIDE FILTER BUTTON
-            ImageButton filterButton = this.getFilterButton();
-            if (filterButton != null) filterButton.setVisibility(View.GONE);
-        } else {
-            if (this.pieGraph != null){
-                this.pieGraph.setVisibility(View.GONE);
-                if (this.pieGraph.getParent() != null){
-                    plotLayout.removeView(this.pieGraph);
-                }
-            }
-            // SHOW FILTER BUTTON
-            ImageButton filterButton = this.getFilterButton();
-            if (filterButton != null) filterButton.setVisibility(View.VISIBLE);
-        }
-        if (plot instanceof BarGraph){
-            BarGraph bar = (BarGraph) plot;
-            this.setBarGraphData(bar);
-        } else {
-            if (this.barGraph != null){
-                this.barGraph.setVisibility(View.GONE);
-                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.GONE);
-                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();
+         final LinearLayout thePlotlayout = plotLayout;
+
+         if (this.loader != null && this.loader.isAlive()) this.loader.interrupt();
+
+         this.loader = null;
+
+         this.loader = new Thread(new Runnable() {
+             @Override
+             public void run() {
+                 this.loadDataInBackground();
+                 this.actualiseUI();
+             }
+
+             private void loadDataInBackground(){
+                 View plot = StatisticsFragment.this.currentPlotView;
+                 if (plot instanceof PieGraph){
+                     PieGraph pie = (PieGraph) plot;
+                     StatisticsFragment.this.setPieGraphData(pie);
+                 }
+                 if (plot instanceof BarGraph){
+                     BarGraph bar = (BarGraph) plot;
+                     StatisticsFragment.this.setBarGraphData(bar);
+                 }
+                 if (plot instanceof LineGraph){
+                     LineGraph line = (LineGraph)plot;
+                     StatisticsFragment.this.setLineGraphData(line);
+                 }
+             }
+
+             private void actualiseUI(){
+
+                 Activity actv = StatisticsFragment.this.getActivity();
+
+                 if (actv != null){
+                     actv.runOnUiThread(new Runnable() {
+                         @Override
+                         public void run() {
+                             // SET VISIBILITY
+                             View plot = StatisticsFragment.this.currentPlotView;
+
+                             if (plot instanceof PieGraph){
+                                 // HIDE FILTER BUTTON
+                                 ImageButton filterButton = StatisticsFragment.this.getFilterButton();
+                                 if (filterButton != null) filterButton.setVisibility(View.GONE);
+                             } else {
+                                 if (StatisticsFragment.this.pieGraph != null){
+                                     StatisticsFragment.this.pieGraph.setVisibility(View.GONE);
+                                     if (StatisticsFragment.this.pieGraph.getParent() != null){
+                                         thePlotlayout.removeView(StatisticsFragment.this.pieGraph);
+                                     }
+                                 }
+                                 // SHOW FILTER BUTTON
+                                 ImageButton filterButton = StatisticsFragment.this.getFilterButton();
+                                 if (filterButton != null) filterButton.setVisibility(View.VISIBLE);
+                             }
+                             if (! (plot instanceof BarGraph)){
+                                 if (StatisticsFragment.this.barGraph != null){
+                                     StatisticsFragment.this.barGraph.setVisibility(View.GONE);
+                                     if (StatisticsFragment.this.barGraph.getParent() != null){
+                                         thePlotlayout.removeView(StatisticsFragment.this.barGraph);
+                                     }
+                                 }
+                             }
+                             if (!(plot instanceof LineGraph)){
+                                 if (StatisticsFragment.this.lineGraph != null){
+                                     StatisticsFragment.this.lineGraph.setVisibility(View.GONE);
+                                     if (StatisticsFragment.this.lineGraph.getParent() != null){
+                                         thePlotlayout.removeView(StatisticsFragment.this.lineGraph);
+                                     }
+                                 }
+                             }
+
+                             plot.setVisibility(View.VISIBLE);
+
+                             if (plot.getParent() == null){
+                                 thePlotlayout.addView(plot);
+                             }
+                             StatisticsFragment.this.actualiseLegendList();
+                             StatisticsFragment.this.currentPlotView.bringToFront();
+                             StatisticsFragment.this.currentPlotView.invalidate();
+
+                             StatisticsFragment.this.spinner.setVisibility(View.GONE);
+                         }
+                     });
+                 }
+             }
+         });
+
+        this.loader.start();
      }
 
     public ArrayList<PlotComparisonItem> getPieData(){