Browse Source

added timestamp grouping to record overview; timestamp grouping is now the default in record overview.

Julien Clauter 10 years ago
parent
commit
a613eba942

+ 1 - 0
res/values-de/arrays.xml

@@ -13,6 +13,7 @@
     </string-array>
 
     <string-array name="Grouping">
+        <item>Datum</item>
         <item>Protokol</item>
         <item>BSSID</item>
         <item>ESSID</item>

+ 2 - 0
res/values-de/strings.xml

@@ -92,6 +92,8 @@
     <string name="no_data_notification">Es sind keine Attacken vorhanden. Überprüfen Sie gegebenenfalls Ihre Filtereinstellungen.</string>
     <string name="no_data_notification_no_filter">Es sind keine Attacken vorhanden.</string>
 
+    <string name="TODAY">Heute</string>
+    <string name="YESTERDAY">Gestern</string>
 
     <string name="ui_dev_by">UI Entwicklung</string>
     <string name="core_dev_by">Core Entwicklung</string>

+ 1 - 0
res/values/arrays.xml

@@ -13,6 +13,7 @@
     </string-array>
 
     <string-array name="Grouping">
+        <item>Date</item>
         <item>Protocol</item>
         <item>BSSID</item>
         <item>ESSID</item>

+ 2 - 0
res/values/strings.xml

@@ -92,6 +92,8 @@
     <string name="no_data_notification">No Attacks are recorded. Please check your filter option.</string>
     <string name="no_data_notification_no_filter">No Attacks are recorded.</string>
 
+    <string name="TODAY">Today</string>
+    <string name="YESTERDAY">Yesterday</string>
 
     <string name="hostage_with_url"><a href="http://www.tk.informatik.tu-darmstadt.de/de/research/secure-smart-infrastructures/hostage">HosTaGe</a></string>
     <string name="ui_dev_by">UI developed by</string>

+ 119 - 31
src/de/tudarmstadt/informatik/hostage/ui2/fragment/RecordOverviewFragment.java

@@ -35,6 +35,8 @@ import java.util.Collections;
 import java.util.Comparator;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
 import java.util.Random;
 
 import de.tudarmstadt.informatik.hostage.R;
@@ -48,13 +50,13 @@ import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
 import de.tudarmstadt.informatik.hostage.sync.bluetooth.BluetoothSync;
 import de.tudarmstadt.informatik.hostage.sync.nfc.NFCSync;
 import de.tudarmstadt.informatik.hostage.sync.tracing.TracingSyncActivity;
-import de.tudarmstadt.informatik.hostage.ui2.model.LogFilter;
-import de.tudarmstadt.informatik.hostage.ui2.model.LogFilter.SortType;
 import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
 import de.tudarmstadt.informatik.hostage.ui2.adapter.RecordListAdapter;
 import de.tudarmstadt.informatik.hostage.ui2.dialog.ChecklistDialog;
 import de.tudarmstadt.informatik.hostage.ui2.dialog.DateTimeDialogFragment;
 import de.tudarmstadt.informatik.hostage.ui2.model.ExpandableListItem;
+import de.tudarmstadt.informatik.hostage.ui2.model.LogFilter;
+import de.tudarmstadt.informatik.hostage.ui2.model.LogFilter.SortType;
 import de.tudarmstadt.informatik.hostage.ui2.popup.AbstractPopup;
 import de.tudarmstadt.informatik.hostage.ui2.popup.AbstractPopupItem;
 import de.tudarmstadt.informatik.hostage.ui2.popup.SimplePopupItem;
@@ -251,7 +253,7 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
             @Override
             public void run()
             {
-                //RecordOverviewFragment.this.addRecordToDB(5, 10, 1);
+                RecordOverviewFragment.this.addRecordToDB(5, 10, 2);
                 updateUI(doInBackground());
             }
 
@@ -460,10 +462,26 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
     *  @param  mylist {@link ExpandableListView ExpandableListView}
     * */
 	private RecordListAdapter populateListViewFromDB(ExpandableListView mylist) {
+        ArrayList<String> groupTitle = new ArrayList<String>();
+
+        HashMap<String, ArrayList<ExpandableListItem>> sectionData = this.fetchDataForFilter(this.filter, groupTitle);
+
+        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 HashMap<String, ArrayList<ExpandableListItem>> fetchDataForFilter(LogFilter filter, ArrayList<String> groupTitle){
         HashMap<String, ArrayList<ExpandableListItem>> sectionData = new HashMap<String, ArrayList<ExpandableListItem>>();
 
-        ArrayList<Record> data = dbh.getRecordsForFilter(RecordOverviewFragment.this.filter);
+        ArrayList<Record> data = dbh.getRecordsForFilter(filter == null ? this.filter : filter);
 
         // Adding Items to ListView
         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)};
@@ -476,7 +494,12 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
             i++;
         }
 
-        ArrayList<String>groupTitle = new ArrayList<String>();
+        if (groupTitle == null){
+            groupTitle = new ArrayList<String>();
+        } else {
+            groupTitle.clear();
+        }
+
 
         for (Record val : data) {
             // DO GROUPING IN HERE
@@ -507,25 +530,41 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
             items.add(item);
         }
 
-        Collections.sort(groupTitle, new Comparator<String>() {
-            @Override
-            public int compare(String s1, String s2) {
-                return s1.compareToIgnoreCase(s2);
-            }
-        });
-
-
-        RecordListAdapter adapter = null;
-        if (mylist.getAdapter() != null && mylist.getAdapter() instanceof RecordListAdapter){
-            adapter = (RecordListAdapter) mylist.getAdapter();
-            adapter.setData(sectionData);
-            adapter.setSectionHeader(groupTitle);
+        if (this.groupingKey.equals(this.groupingTitles().get(0))){
+            Collections.sort(groupTitle,new StringDateComparator());
         } else {
-            adapter = new RecordListAdapter( RecordOverviewFragment.this.getApplicationContext(), groupTitle, sectionData);
+            Collections.sort(groupTitle, new Comparator<String>() {
+                @Override
+                public int compare(String s1, String s2) {
+                    return s1.compareToIgnoreCase(s2);
+                }
+            });
         }
 
-        return adapter;
-	}
+        return sectionData;
+    }
+
+    class StringDateComparator implements Comparator<String>
+    {
+        DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
+        //String pattern       = ((SimpleDateFormat)formatter).toPattern();
+        String localPattern  = ((SimpleDateFormat)formatter).toLocalizedPattern();
+        SimpleDateFormat dateFormat = new SimpleDateFormat(localPattern);
+
+        public int compare(String lhs, String rhs)
+        {
+            Date date1;
+            Date date2;
+            try {
+                date1 = dateFormat.parse(lhs);
+                date2 = dateFormat.parse(rhs);
+            } catch (java.text.ParseException e ) {
+                return 0;
+            }
+
+            return date2.compareTo(date1);
+        }
+    }
 
     /**
      * Actualises the list in a background thread
@@ -678,6 +717,41 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
 		}
 	}
 
+    /**
+     * Returns the timestamp in a localised format.
+     * if the date is today, it returns today,
+     * if the date was yesterday it returns yesterday, otherwise the format is localised.
+     * @param timestamp long
+     * @return formatted date String
+     */
+    public String getFormattedDate(long timestamp) {
+        Calendar smsTime = Calendar.getInstance();
+        smsTime.setTimeInMillis(timestamp);
+
+        Calendar now = Calendar.getInstance();
+
+        if(now.get(Calendar.DATE) == smsTime.get(Calendar.DATE) ){
+            return this.getDateAsDayString(timestamp) + " - " + this.getResources().getString( R.string.TODAY);
+        }else if(now.get(Calendar.DATE) - smsTime.get(Calendar.DATE) == 1 ){
+            return this.getDateAsDayString(timestamp) + " - " + this.getResources().getString( R.string.YESTERDAY);
+        }
+        return this.getDateAsDayString(timestamp);
+    }
+
+    /**Returns a date as a formated string
+     * @param timeStamp date
+     * @return String date format is localised*/
+    @SuppressLint("SimpleDateFormat")
+    private String getDateAsDayString(long timeStamp) {
+        try {
+            Date netDate = (new Date(timeStamp));
+            DateFormat dateFormat = android.text.format.DateFormat.getMediumDateFormat(this.getActivity());
+            return dateFormat.format(netDate);
+            //return dateFormat.format(netDate);
+        } catch (Exception ex) {
+            return "xx";
+        }
+    }
 
 	/*****************************
 	 *
@@ -760,12 +834,14 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
     public String getGroupValue(Record rec){
         int index = this.groupingTitles().indexOf(this.groupingKey);
         switch (index){
-            case 0:
-                return rec.getProtocol();
             case 1:
-                return rec.getBssid();
+                return rec.getProtocol();
             case 2:
+                return rec.getBssid();
+            case 3:
                 return rec.getSsid();
+            case 0:
+                return this.getFormattedDate(rec.getTimestamp());
             default:
                 return rec.getProtocol();
         }
@@ -773,18 +849,30 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
 
     /**Returns the Group titles for the specified grouping key. e.g. groupingKey is "ESSID" it returns all available essids.
     * @return ArrayList<String> grouptitles*/
-    public ArrayList<String> getGroupTitles(){
+    public List<String> getGroupTitles(){
         int index = this.groupingTitles().indexOf(this.groupingKey);
         switch (index){
-            case 0:
-                return this.protocolTitles();
             case 1:
-                return this.bssids();
+                return this.protocolTitles();
             case 2:
+                return this.bssids();
+            case 3:
                 return this.essids();
-            default:
-                return this.protocolTitles();
-        }
+            case 0:{
+                RecordListAdapter adapter = (RecordListAdapter) this.expListView.getExpandableListAdapter();
+                if (adapter != null){
+                    return adapter.getSectionHeaders();
+                }
+                return new ArrayList<String>();
+            }
+            default:{
+                RecordListAdapter adapter = (RecordListAdapter) this.expListView.getExpandableListAdapter();
+                if (adapter != null){
+                    return adapter.getSectionHeaders();
+                }
+                return new ArrayList<String>();
+            }
+            }
     }
 
 

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

@@ -1707,11 +1707,11 @@ public class StatisticsFragment extends Fragment implements ChecklistDialog.Chec
         return "" + hour + ":00";
     }
 
-    static final DateFormat dateFormat = new SimpleDateFormat("d.M.yy");
+    //static final DateFormat dateFormat = new SimpleDateFormat("d.M.yy");
 
     /**Returns a date as a formated string
      * @param timeStamp date
-     * @return String date format (d.M.yy)*/
+     * @return String date format is localised*/
     @SuppressLint("SimpleDateFormat")
     private String getDateAsDayString(long timeStamp) {
         try {
@@ -1730,7 +1730,7 @@ public class StatisticsFragment extends Fragment implements ChecklistDialog.Chec
      * @return String date format (H:mm  dd/MM/yyyy)*/
     private String getDateAsString(long timeStamp) {
         try {
-            DateFormat sdf = new SimpleDateFormat("H:mm  dd/MM/yyyy");
+            DateFormat sdf = new SimpleDateFormat("H:mm  dd.MM.yyyy");
             Date netDate = (new Date(timeStamp));
             return sdf.format(netDate);
         } catch (Exception ex) {