Browse Source

changed timestamp grouping behaviour

Julien Clauter 10 years ago
parent
commit
b2edec3122

+ 188 - 50
src/de/tudarmstadt/informatik/hostage/ui2/fragment/RecordOverviewFragment.java

@@ -100,20 +100,32 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
 
 	private SharedPreferences pref;
 
-	public void setFilter(LogFilter filter){
-        this.filter = filter;
-    }
-
     Thread loader;
 
-    public RecordOverviewFragment(){}
 
-    public void setGroupKey(String key){
-        this.groupingKey = key;
-    }
+    /* DATE CONVERSION STUFF*/
+    static final DateFormat localisedDateFormatter = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
+    // DATE WHICH PATTERN
+    static final String localDatePattern  = ((SimpleDateFormat)localisedDateFormatter).toLocalizedPattern();
+    static final String groupingDatePattern  = "MMMM yyyy";
+
+    // INSERT HERE YOUR DATE PATERN
+    static final SimpleDateFormat groupingDateFormatter = new SimpleDateFormat(groupingDatePattern);
+    static final Calendar calendar = Calendar.getInstance();
+
+    // DATE STRINGS
+    static final String TODAY = MainActivity.getInstance().getResources().getString( R.string.TODAY);
+    static final String YESTERDAY = MainActivity.getInstance().getResources().getString( R.string.YESTERDAY);
 
 
 
+
+    /**
+     * Constructor
+     */
+    public RecordOverviewFragment(){}
+
+
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -251,7 +263,7 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
             @Override
             public void run()
             {
-                //RecordOverviewFragment.this.addRecordToDB(10, 5, 4);
+                //RecordOverviewFragment.this.addRecordToDB(40, 10, 4);
                 updateUI(doInBackground());
             }
 
@@ -494,7 +506,7 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
         }
 
         if (this.groupingKey.equals(this.groupingTitles().get(DEFAULT_GROUPING_KEY_INDEX))){
-            Collections.sort(groupTitle,new StringDateComparator());
+            Collections.sort(groupTitle,new DateStringComparator());
         } else {
             Collections.sort(groupTitle, new Comparator<String>() {
                 @Override
@@ -507,23 +519,16 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
         return sectionData;
     }
 
-    class StringDateComparator implements Comparator<String>
+    /**
+     * The DateStringComparator compares formatted date strings by converting the into date.
+     * This class  is mainly used for grouping the records by their timestamp.
+     */
+    class DateStringComparator 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;
-            }
+            Date date1 = RecordOverviewFragment.this.convertStringToDate(lhs);
+            Date date2 = RecordOverviewFragment.this.convertStringToDate(rhs);
 
             return date2.compareTo(date1);
         }
@@ -661,61 +666,179 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
 
 	/*****************************
 	 *
-	 *          Date Transform
+	 *          Date Transformation / Conversion
 	 *
 	 * ***************************/
 
 
-    /**Returns the date format "H:mm d.M.yy" for the given timestamp (long)
+    /**Returns the localised date format for the given timestamp
     * @param timeStamp long */
 	@SuppressLint("SimpleDateFormat")
 	private String getDateAsString(long timeStamp) {
-
-		try {
-			DateFormat sdf = new SimpleDateFormat("H:mm d.M.yy");
-			Date netDate = (new Date(timeStamp));
-			return sdf.format(netDate);
+        Date date = (new Date(timeStamp));
+        try {
+            DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault());
+			return formatter.format(date);
 		} catch (Exception ex) {
-			return "xx";
+			return "---";
 		}
 	}
 
     /**
-     * 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.
+     * Returns the timestamp in a own format.
+     * Depending on the returned format the grouping by timestamp will change.
+     *
+     * e.g.
+     * If you return a DateAsMonth formatted Date the records will be mapped by their month and year.
+     * If you return the a DateAsDay formatted Date the records will be mapped by their day, month and year.
+     * and so on...
+     *
      * @param timestamp long
      * @return formatted date String
      */
-    public String getFormattedDate(long timestamp) {
-        Calendar smsTime = Calendar.getInstance();
-        smsTime.setTimeInMillis(timestamp);
+    public String getFormattedDateForGrouping(long timestamp) {
+
+        // DECIDE WHICH KIND OF FORMAT SHOULD BE USED
+        // MONTH FORMAT
+        String date = this.getDateAsMonthString(timestamp);
+        // DAY FORMAT
+        //String date = this.getDateAsDayString(timestamp);
+
+        return date;
+    }
+
+    /**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));
+            String dateString;
+
+            long date = this.dayMilliseconds(timestamp);
 
-        Calendar now = Calendar.getInstance();
+            if(this.todayMilliseconds() == date ){
+                dateString = TODAY;
+            }else if(this.yesterdayMilliseconds() == date ){
+                dateString = YESTERDAY;
+            } else {
+                dateString = localisedDateFormatter.format(netDate);
+            }
+            return dateString;
 
-        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);
+        } catch (Exception ex) {
+            return "---";
         }
-        return this.getDateAsDayString(timestamp);
+    }
+
+    /**
+     * Converts a formatted DateString into a date.
+     * @param dateString String
+     * @return Date
+     */
+    private Date convertStringToDate(String dateString){
+        if (dateString != null && dateString.length() != 0){
+            SimpleDateFormat dateFormat = groupingDateFormatter; //new SimpleDateFormat(localDatePattern);
+            Date date;
+            try {
+                if (dateString.equals(TODAY)){
+                    long millisec = RecordOverviewFragment.this.todayMilliseconds();
+                    date = new Date(millisec);
+                } else if (dateString.equals(YESTERDAY)){
+                    long millisec = RecordOverviewFragment.this.yesterdayMilliseconds();
+                    date = new Date(millisec);
+                } else {
+                    date = dateFormat.parse(dateString);
+                }
+                return date;
+
+            } catch (java.text.ParseException e ) {
+                date = new Date(0);
+                return date;
+            }
+        } else {
+            return new Date(0);
+        }
+    }
+
+    /**
+     * Returns the milliseconds for the day today (not the time).
+     * @return long
+     */
+    private long todayMilliseconds(){
+        Date current = new Date();
+        calendar.setTimeInMillis(current.getTime());
+        int day = calendar.get(Calendar.DATE);
+        int month = calendar.get(Calendar.MONTH);
+        int year = calendar.get(Calendar.YEAR);
+
+        calendar.set(year, month, day, 0,0,0);
+
+        long milli = calendar.getTimeInMillis();
+
+        Date today = new Date(milli);
+
+        return (milli / (long) 1000) * (long) 1000;
+    }
+
+    /**
+     * Returns the milliseconds for the day yesterday (not the time).
+     * @return long
+     */
+    private long yesterdayMilliseconds(){
+        Date current = new Date();
+        calendar.setTimeInMillis(current.getTime());
+        int day = calendar.get(Calendar.DATE);
+        int month = calendar.get(Calendar.MONTH);
+        int year = calendar.get(Calendar.YEAR);
+
+        calendar.set(year, month, day, 0,0,0);
+
+        calendar.add(Calendar.DATE, -1);
+
+        long milli = calendar.getTimeInMillis();
+
+        Date today = new Date(milli);
+
+        return (milli / (long) 1000) * (long) 1000;
+    }
+
+    /**
+     * returns just the date not the time of a date.
+     * @param date Date
+     * @return long
+     */
+    private long dayMilliseconds(long date){
+        //Date current = new Date();
+        calendar.setTimeInMillis(date);
+        int day = calendar.get(Calendar.DATE);
+        int month = calendar.get(Calendar.MONTH);
+        int year = calendar.get(Calendar.YEAR);
+
+        calendar.set(year, month, day, 0,0,0);
+
+        long milli = calendar.getTimeInMillis();
+
+        Date test = new Date(milli);
+
+        return (milli / (long) 1000) * (long) 1000;
     }
 
     /**Returns a date as a formated string
      * @param timeStamp date
      * @return String date format is localised*/
     @SuppressLint("SimpleDateFormat")
-    private String getDateAsDayString(long timeStamp) {
+    private String getDateAsMonthString(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);
+            return groupingDateFormatter.format(netDate);
         } catch (Exception ex) {
             return "xx";
         }
     }
 
+
 	/*****************************
 	 *
 	 *          Getter / Setter
@@ -730,7 +853,22 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
 		this.showFilterButton = showFilterButton;
 	}
 
+    /**
+     * Set the group key for grouping the records.
+     * All possible grouping keys are:
+     * R.string.date,
+     * R.string.rec_protocol,
+     * R.string.ESSID,
+     * R.string.BSSID
+     * @param key String
+     */
+    public void setGroupKey(String key){
+        this.groupingKey = key;
+    }
 
+    public void setFilter(LogFilter filter){
+        this.filter = filter;
+    }
 
 
 	/*****************************
@@ -804,9 +942,9 @@ public class RecordOverviewFragment extends UpNavigatibleFragment implements Che
             case 3:
                 return rec.getBssid();
             case 0:
-                return this.getFormattedDate(rec.getTimestamp());
+                return this.getFormattedDateForGrouping(rec.getTimestamp());
             default:
-                return this.getFormattedDate(rec.getTimestamp());
+                return this.getFormattedDateForGrouping(rec.getTimestamp());
         }
     }