Quellcode durchsuchen

many fixes in ugly db;
record over view list will perform reload stuff in a background thread

Julien Clauter vor 10 Jahren
Ursprung
Commit
0ec5ef24f5

+ 10 - 9
src/de/tudarmstadt/informatik/hostage/deprecated/UglyDbHelper.java

@@ -1,16 +1,17 @@
 package de.tudarmstadt.informatik.hostage.deprecated;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.List;
-
 import android.content.ContentValues;
 import android.content.Context;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
 import android.util.Log;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+
 import de.tudarmstadt.informatik.hostage.logging.Record;
 import de.tudarmstadt.informatik.hostage.logging.Record.TYPE;
 import de.tudarmstadt.informatik.hostage.model.Profile;
@@ -286,7 +287,7 @@ public class UglyDbHelper extends SQLiteOpenHelper {
 		bssidValues.put(KEY_LATITUDE, record.getLatitude());
 		bssidValues.put(KEY_LONGITUDE, record.getLongitude());
 		bssidValues.put(KEY_ACCURACY, record.getAccuracy());
-		bssidValues.put(KEY_TIME, record.getTimestampLocation());
+		bssidValues.put(KEY_GEO_TIMESTAMP, record.getTimestampLocation());
 
 		ContentValues attackValues = new ContentValues();
 		attackValues.put(KEY_ATTACK_ID, record.getAttack_id()); // Log Attack ID
@@ -580,7 +581,7 @@ public class UglyDbHelper extends SQLiteOpenHelper {
 				values.put(KEY_LATITUDE, Double.parseDouble(cursor.getString(2)));
 				values.put(KEY_LONGITUDE, Double.parseDouble(cursor.getString(3)));
 				values.put(KEY_ACCURACY, Float.parseFloat(cursor.getString(4)));
-				values.put(KEY_TIME, cursor.getLong(5));
+				values.put(KEY_GEO_TIMESTAMP, cursor.getLong(5));
 				networkInformation.add(values);
 			} while (cursor.moveToNext());
 		}
@@ -919,7 +920,7 @@ public class UglyDbHelper extends SQLiteOpenHelper {
 		String bssidQuery = "SELECT  * FROM " + TABLE_BSSIDS + " WHERE " + KEY_BSSID + " = " + "'" + bssid + "'";
 		Cursor cursor = db.rawQuery(bssidQuery, null);
 		int result = cursor.getCount();
-		if (cursor != null && cursor.moveToFirst() && (result <= 0 || cursor.getLong(5) < (Long) networkInformation.get(KEY_TIME)))
+		if (cursor != null && cursor.moveToFirst() && (result <= 0 || cursor.getLong(5) < (Long) networkInformation.get(KEY_GEO_TIMESTAMP)))
 			;
 		{
 			ContentValues bssidValues = new ContentValues();
@@ -928,7 +929,7 @@ public class UglyDbHelper extends SQLiteOpenHelper {
 			bssidValues.put(KEY_LATITUDE, (double) (Double) networkInformation.get(KEY_LATITUDE));
 			bssidValues.put(KEY_LONGITUDE, (double) (Double) networkInformation.get(KEY_LONGITUDE));
 			bssidValues.put(KEY_ACCURACY, (float) (Float) networkInformation.get(KEY_ACCURACY));
-			bssidValues.put(KEY_TIME, (Long) networkInformation.get(KEY_TIME));
+			bssidValues.put(KEY_GEO_TIMESTAMP, (Long) networkInformation.get(KEY_GEO_TIMESTAMP));
 			db.insertWithOnConflict(TABLE_BSSIDS, null, bssidValues, SQLiteDatabase.CONFLICT_REPLACE);
 		}
 		cursor.close();

+ 18 - 6
src/de/tudarmstadt/informatik/hostage/ui2/adapter/ExpandableListAdapter.java

@@ -1,15 +1,14 @@
 package de.tudarmstadt.informatik.hostage.ui2.adapter;
 
+import android.content.Context;
+import android.view.LayoutInflater;
 import android.view.View;
+import android.view.ViewGroup;
+import android.widget.BaseExpandableListAdapter;
 
 import java.util.ArrayList;
 import java.util.HashMap;
-        import java.util.List;
-
-        import android.content.Context;
-        import android.view.LayoutInflater;
-        import android.view.ViewGroup;
-        import android.widget.BaseExpandableListAdapter;
+import java.util.List;
 
 import de.tudarmstadt.informatik.hostage.ui2.model.ExpandableListItem;
 
@@ -33,6 +32,19 @@ public abstract class ExpandableListAdapter extends BaseExpandableListAdapter {
         this._sectionTitleToChildData = dataMapping;
     }
 
+    public void setData(HashMap<String, ArrayList<ExpandableListItem>> dataMapping){
+        this._sectionTitleToChildData = dataMapping;
+    }
+    public HashMap<String, ArrayList<ExpandableListItem>> getData(){
+        return this._sectionTitleToChildData;
+    }
+    public void setSectionHeader(List<String> listSectionHeaders){
+        this._sectionHeader = listSectionHeaders;
+    }
+    public List<String> getSectionHeaders(){
+        return this._sectionHeader;
+    }
+
     @Override
     public Object getChild(int section, int row) {
         return this._sectionTitleToChildData.get(this._sectionHeader.get(section))

+ 144 - 43
src/de/tudarmstadt/informatik/hostage/ui2/fragment/RecordOverviewFragment.java

@@ -1,6 +1,7 @@
 package de.tudarmstadt.informatik.hostage.ui2.fragment;
 
 import android.annotation.SuppressLint;
+import android.app.Activity;
 import android.app.FragmentManager;
 import android.content.Context;
 import android.content.Intent;
@@ -25,8 +26,8 @@ import java.util.HashMap;
 import java.util.Random;
 
 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.ui.LogFilter.SortType;
 import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
@@ -77,6 +78,7 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
         this.filter = filter;
     }
 
+    Thread loader;
 
     public RecordOverviewFragment(){}
 
@@ -116,30 +118,13 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
         if (this.groupingKey == null) this.groupingKey = this.groupingTitles().get(0);
 
 	    this.setShowFilterButton(!this.filter.isNotEditable());
-	    //this.addRecordToDB();
 
 		View rootView = inflater.inflate(this.getLayoutId(), container, false);
 		ExpandableListView mylist = (ExpandableListView) rootView.findViewById(R.id.loglistview);
 
 		this.expListView = mylist;
-		populateListViewFromDB(mylist);
-
-        if (this.openSections != null && this.openSections.size() != 0){
-            for (int i = 0; i < this.openSections.size(); i++){
-                int index = this.openSections.get(i);
-                this.expListView.expandGroup(index);
-            }
-        } else {
-            this.openSections = new ArrayList<Integer>();
-        }
 
-        if (mListPosition != -1 && mItemPosition != -1)
-            this.expListView.setSelectedChild(mListPosition, mItemPosition, true);
-
-        mListPosition = -1;
-        mItemPosition = -1;
-
-		registerListClickCallback(mylist);
+        this.initialiseListView();
 
         ImageButton filterButton = (ImageButton) rootView.findViewById(R.id.FilterButton);
         filterButton.setOnClickListener(new View.OnClickListener() {
@@ -168,6 +153,75 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
 		return rootView;
 	 }
 
+
+    /*Initialises the expandable list view in a backgorund thread*/
+    private void initialiseListView(){
+        if (loader != null) loader.interrupt();
+
+        loader = new Thread(new Runnable(){
+
+            private void updateUI(final RecordListAdapter currentAdapter)
+            {
+                if(loader.isInterrupted()){
+                    return;
+                }
+                Activity activity = RecordOverviewFragment.this.getActivity();
+
+                if (activity != null){
+                    activity.runOnUiThread(new Runnable() {
+                        @Override
+                        public void run() {
+                            RecordOverviewFragment.this.expListView.setAdapter(currentAdapter);
+                            // Update view and remove loading spinner etc...
+                            RecordListAdapter adapter = (RecordListAdapter) RecordOverviewFragment.this.expListView.getExpandableListAdapter();
+
+                            if (adapter != null){
+                                adapter.notifyDataSetChanged();
+
+                                if (adapter.getGroupCount() == 1){
+                                    RecordOverviewFragment.this.expListView.expandGroup(0);
+                                } else {
+                                    RecordOverviewFragment.this.setSectionToOpen(RecordOverviewFragment.this.sectionToOpen);
+                                }
+                            }
+
+                            if (RecordOverviewFragment.this.openSections != null && RecordOverviewFragment.this.openSections.size() != 0){
+                                for (int i = 0; i < RecordOverviewFragment.this.openSections.size(); i++){
+                                    int index = RecordOverviewFragment.this.openSections.get(i);
+                                    RecordOverviewFragment.this.expListView.expandGroup(index);
+                                }
+                            } else {
+                                RecordOverviewFragment.this.openSections = new ArrayList<Integer>();
+                            }
+
+                            if (mListPosition != -1 && mItemPosition != -1)
+                                RecordOverviewFragment.this.expListView.setSelectedChild(mListPosition, mItemPosition, true);
+
+                            mListPosition = -1;
+                            mItemPosition = -1;
+                            registerListClickCallback(RecordOverviewFragment.this.expListView);
+                        }
+                    });
+                }
+            }
+
+            private RecordListAdapter doInBackground()
+            {
+                return populateListViewFromDB(RecordOverviewFragment.this.expListView);
+            }
+
+            @Override
+            public void run()
+            {
+                updateUI(doInBackground());
+            }
+
+        });
+
+        loader.start();
+    }
+
+
     /*
     *  Returns the Fragment layout ID
     *  @return int The fragment layout ID
@@ -209,7 +263,7 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
             }
             if(title.equals(FILTER_MENU_TITLE_REMOVE)){
                 this.clearFilter();
-                this.populateListViewFromDB(this.expListView);
+                this.actualiseListViewInBackground();
             }
             if(title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)){
                 this.openTimestampToFilterDialog();
@@ -226,12 +280,14 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
 
     public void onStart() {
         super.onStart();
-        //this.populateListViewFromDB(this.expListView);
-        if (this.expListView.getExpandableListAdapter().getGroupCount() == 1){
-            this.expListView.expandGroup(0);
-        } else {
-            this.setSectionToOpen(this.sectionToOpen);
+        if (this.expListView.getExpandableListAdapter() != null){
+            if (this.expListView.getExpandableListAdapter().getGroupCount() == 1){
+                this.expListView.expandGroup(0);
+            } else {
+                this.setSectionToOpen(this.sectionToOpen);
+            }
         }
+
     }
 
 	/*****************************
@@ -265,14 +321,14 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
     *  Reloads the data in the ExpandableListView for the given filter object.
     *  @param ExpandableListView  listview
     * */
-	private void populateListViewFromDB(ExpandableListView mylist) {
+	private RecordListAdapter populateListViewFromDB(ExpandableListView mylist) {
 
-		HashMap<String, ArrayList<ExpandableListItem>> sectionData = new HashMap<String, ArrayList<ExpandableListItem>>();
+        HashMap<String, ArrayList<ExpandableListItem>> sectionData = new HashMap<String, ArrayList<ExpandableListItem>>();
 
-		ArrayList<Record> data = dbh.getRecordsForFilter(this.filter);
+        ArrayList<Record> data = dbh.getRecordsForFilter(RecordOverviewFragment.this.filter);
 
         // Adding Items to ListView
-        String keys[] = new String[] { this.getString(R.string.RecordBSSID), this.getString(R.string.RecordSSID), this.getString(R.string.RecordProtocol), this.getString(R.string.RecordTimestamp)};
+        String keys[] = new String[] { RecordOverviewFragment.this.getString(R.string.RecordBSSID), RecordOverviewFragment.this.getString(R.string.RecordSSID), RecordOverviewFragment.this.getString(R.string.RecordProtocol), RecordOverviewFragment.this.getString(R.string.RecordTimestamp)};
         int ids[] = new int[] {R.id.RecordTextFieldBSSID, R.id.RecordTextFieldSSID, R.id.RecordTextFieldProtocol, R.id.RecordTextFieldTimestamp };
 
         HashMap<String, Integer> mapping = new HashMap<String, Integer>();
@@ -284,14 +340,14 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
 
         ArrayList<String>groupTitle = new ArrayList<String>();
 
-		for (Record val : data) {
+        for (Record val : data) {
             // DO GROUPING IN HERE
-			HashMap<String, String> map = new HashMap<String, String>();
-			map.put(this.getString(R.string.RecordBSSID), val.getBssid());
-			map.put(this.getString(R.string.RecordSSID), val.getSsid());
-			map.put(this.getString(R.string.RecordProtocol), val.getProtocol());
-			map.put(this.getString(R.string.RecordTimestamp),
-					this.getDateAsString(val.getTimestamp()));
+            HashMap<String, String> map = new HashMap<String, String>();
+            map.put(RecordOverviewFragment.this.getString(R.string.RecordBSSID), val.getBssid());
+            map.put(RecordOverviewFragment.this.getString(R.string.RecordSSID), val.getSsid());
+            map.put(RecordOverviewFragment.this.getString(R.string.RecordProtocol), val.getProtocol());
+            map.put(RecordOverviewFragment.this.getString(R.string.RecordTimestamp),
+                    RecordOverviewFragment.this.getDateAsString(val.getTimestamp()));
 
             ExpandableListItem item = new ExpandableListItem();
             item.setData(map);
@@ -300,7 +356,7 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
 
             item.setTag(val.getAttack_id());
 
-            String groupID = this.getGroupValue(val);
+            String groupID = RecordOverviewFragment.this.getGroupValue(val);
 
             ArrayList<ExpandableListItem> items = sectionData.get(groupID);
             if (items == null) {
@@ -310,8 +366,8 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
             }
 
 
-			items.add(item);
-		}
+            items.add(item);
+        }
 
         Collections.sort(groupTitle, new Comparator<String>() {
             @Override
@@ -320,11 +376,56 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
             }
         });
 
-        RecordListAdapter adapter = new RecordListAdapter(this.getApplicationContext(), groupTitle, sectionData);
 
-        mylist.setAdapter(adapter);
+        RecordListAdapter adapter = null;
+        if (mylist.getAdapter() != null && mylist.getAdapter() instanceof RecordListAdapter){
+            adapter = (RecordListAdapter) mylist.getAdapter();
+            adapter.setData(sectionData);
+            adapter.setSectionHeader(groupTitle);
+        } else {
+            adapter = new RecordListAdapter( RecordOverviewFragment.this.getApplicationContext(), groupTitle, sectionData);
+        }
+
+        return adapter;
 	}
 
+
+    private void actualiseListViewInBackground(){
+        if (loader != null && loader.isAlive()) loader.interrupt();
+
+        loader = null;
+
+        loader = new Thread(new Runnable() {
+            @Override
+            public void run() {
+                this.runOnUiThread(this.doInBackground());
+            }
+
+            private RecordListAdapter doInBackground(){
+                return RecordOverviewFragment.this.populateListViewFromDB(RecordOverviewFragment.this.expListView);
+            }
+
+            private void runOnUiThread(final RecordListAdapter adapter){
+                Activity actv = RecordOverviewFragment.this.getActivity();
+                if (actv != null){
+                    actv.runOnUiThread(new Runnable() {
+                        @Override
+                        public void run() {
+                            this.actualiseUI();
+                        }
+                        private void actualiseUI(){
+                            if (adapter != null){
+                                RecordOverviewFragment.this.expListView.setAdapter(adapter);
+                                adapter.notifyDataSetChanged();
+                            }
+                        }
+                    });
+                }
+            }
+        });
+        loader.start();
+    }
+
     /*Thsi will open a section in the ExpandableListView with the same title as the parameter s.
     *
     * @param String s (the section title to open)
@@ -764,7 +865,7 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
 		} else {
 			this.filter.setAboveTimestamp(dialog.getDate());
 		}
-		this.populateListViewFromDB(this.expListView);
+        this.actualiseListViewInBackground();
 	}
 
 	public void onDateTimePickerNegativeClick(DateTimeDialogFragment dialog) {
@@ -813,7 +914,7 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
             if (titles.size() == 0) return;
             this.groupingKey =  titles.get(0);
         }
-		this.populateListViewFromDB(this.expListView);
+        this.actualiseListViewInBackground();
 	}
 
 	public void onDialogNegativeClick(ChecklistDialog dialog) {}

+ 38 - 0
src/de/tudarmstadt/informatik/hostage/ui2/task/LoaderTask.java

@@ -0,0 +1,38 @@
+package de.tudarmstadt.informatik.hostage.ui2.task;
+
+import android.annotation.SuppressLint;
+import android.os.AsyncTask;
+
+/**
+ * Created by Julien on 23.03.14.
+ */
+public class LoaderTask extends AsyncTask<Void, Void, Void> {
+
+    private TaskListener listener;
+
+    @SuppressLint("ValidFragment")
+    public interface TaskListener {
+        public void doInBackgorund();
+        public void onFinish();
+    }
+
+    public LoaderTask(TaskListener listener){
+        super();
+        this.listener = listener;
+    }
+
+    @Override
+    protected Void doInBackground(Void... unused) {
+        if (this.listener != null){
+            this.listener.doInBackgorund();
+        }
+        return(null);
+    }
+
+    @Override
+    protected void onPostExecute(Void unused) {
+        if (this.listener != null){
+            this.listener.onFinish();
+        }
+    }
+}