Back to project page Android-Counter-Application.
The source code is released under:
GNU General Public License
If you think the Android project Android-Counter-Application listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package ca.ualberta.cs.artem_counter; // w w w. ja v a 2s .co m import java.util.ArrayList; import java.util.Date; //This class computes the usage statistics for a given counter object public class CounterHistory { private Counter counter; private String[] monthNames = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; public CounterHistory(Counter counter) { super(); this.counter = counter; } public Counter getCounter() { return counter; } public void setCounter(Counter counter) { this.counter = counter; } // The following methods iterate over all the hours/days/weeks/months that have passed since // the creation of the counter, and compile a list of strings in the required format. @SuppressWarnings("deprecation") public ArrayList<String> hourlyStats(){ long hour = 3600000; long minute = 60000; ArrayList<String> statsList = new ArrayList<String>(); Date created = counter.getTimestamp(); //get the exact hour of counter creation instead of minute of the hour when counter was created Date hourOf = new Date(created.getTime()-((created.getMinutes()-1)*minute)); Date now = new Date(); Date from = new Date(hourOf.getTime()); Date to = new Date(hourOf.getTime()+hour); do { int clicks = counter.getClicksInRange(from, to); if(clicks>0){ statsList.add(monthNames[from.getMonth()] + " " + Integer.toString(from.getDate()) + ", " + Integer.toString(from.getHours())+":00" + " -- " + Integer.toString(clicks)); } from = new Date(from.getTime()+hour); to = new Date(to.getTime()+hour); } while(from.before(now)); return statsList; } @SuppressWarnings("deprecation") public ArrayList<String> dailyStats(){ long day = 86400000; long hour = 3600000; ArrayList<String> statsList = new ArrayList<String>(); Date created = counter.getTimestamp(); //get the day of counter instead of moment of the day when counter was created Date dayOf= new Date(created.getTime()-((created.getHours()-1)*hour)); Date now = new Date(); Date from = new Date(dayOf.getTime()); Date to = new Date(dayOf.getTime()+day); do { int clicks = counter.getClicksInRange(from, to); if(clicks>0){ statsList.add(monthNames[from.getMonth()] + " " + Integer.toString(from.getDate()) + " -- " + Integer.toString(clicks)); } from = new Date(from.getTime()+day); to = new Date(to.getTime()+day); } while(from.before(now)); return statsList; } @SuppressWarnings("deprecation") public ArrayList<String> weeklyStats(){ long week = 604800000; long day = 86400000; ArrayList<String> statsList = new ArrayList<String>(); Date created = counter.getTimestamp(); //get the exact week of counter creation instead of weekday Date weekOf = new Date(created.getTime()-((created.getDay()-1)*day)); Date now = new Date(); Date from = new Date(weekOf.getTime()); Date to = new Date(weekOf.getTime()+week); do { int clicks = counter.getClicksInRange(from, to); if(clicks>0){ statsList.add("Week of " + monthNames[from.getMonth()] + " " + Integer.toString(from.getDate()) + " -- " + Integer.toString(clicks)); } from = new Date(from.getTime()+week); to = new Date(to.getTime()+week); } while(from.before(now)); return statsList; } @SuppressWarnings("deprecation") public ArrayList<String> monthlyStats(){ long month = 2628000000L; long day = 86400000L; ArrayList<String> statsList = new ArrayList<String>(); Date created = counter.getTimestamp(); Date monthOf = new Date(created.getTime()-((created.getDate()-1)*day)); Date now = new Date(); Date from = new Date(monthOf.getTime()); Date to = new Date((long) (monthOf.getTime()+ month)); do { int clicks = counter.getClicksInRange(from, to); if(clicks>0){ statsList.add("Month of " + monthNames[from.getMonth()] + " -- " + Integer.toString(clicks)); } from = new Date((long) (from.getTime()+month)); to = new Date((long) (to.getTime()+month)); } while(from.before(now)); return statsList; } }