Use Calendar.add() instead of Calendar.roll()
Calendar.roll() breaks the year at the beginning of the year, so use add() instead, which changes larger fields. Also, only use 4 bins, since the last bin's boundary is not relevant. Lastly, no need to return Calendar in beginningOfDay. Fixes http://b/issue?id=2361382
This commit is contained in:
@ -38,7 +38,7 @@ public class DateSorter {
|
||||
/** must be >= 3 */
|
||||
public static final int DAY_COUNT = 5;
|
||||
|
||||
private long [] mBins = new long[DAY_COUNT];
|
||||
private long [] mBins = new long[DAY_COUNT-1];
|
||||
private String [] mLabels = new String[DAY_COUNT];
|
||||
|
||||
private static final int NUM_DAYS_AGO = 5;
|
||||
@ -54,15 +54,13 @@ public class DateSorter {
|
||||
|
||||
// Create the bins
|
||||
mBins[0] = c.getTimeInMillis(); // Today
|
||||
c.roll(Calendar.DAY_OF_YEAR, -1);
|
||||
c.add(Calendar.DAY_OF_YEAR, -1);
|
||||
mBins[1] = c.getTimeInMillis(); // Yesterday
|
||||
c.roll(Calendar.DAY_OF_YEAR, -(NUM_DAYS_AGO - 1));
|
||||
c.add(Calendar.DAY_OF_YEAR, -(NUM_DAYS_AGO - 1));
|
||||
mBins[2] = c.getTimeInMillis(); // Five days ago
|
||||
c.roll(Calendar.DAY_OF_YEAR, NUM_DAYS_AGO); // move back to today
|
||||
c.roll(Calendar.MONTH, -1);
|
||||
c.add(Calendar.DAY_OF_YEAR, NUM_DAYS_AGO); // move back to today
|
||||
c.add(Calendar.MONTH, -1);
|
||||
mBins[3] = c.getTimeInMillis(); // One month ago
|
||||
c.roll(Calendar.MONTH, -1);
|
||||
mBins[4] = c.getTimeInMillis(); // Over one month ago
|
||||
|
||||
// build labels
|
||||
mLabels[0] = context.getText(com.android.internal.R.string.today).toString();
|
||||
@ -84,11 +82,11 @@ public class DateSorter {
|
||||
* date bin this date belongs to
|
||||
*/
|
||||
public int getIndex(long time) {
|
||||
// Lame linear search
|
||||
for (int i = 0; i < DAY_COUNT; i++) {
|
||||
int lastDay = DAY_COUNT - 1;
|
||||
for (int i = 0; i < lastDay; i++) {
|
||||
if (time > mBins[i]) return i;
|
||||
}
|
||||
return DAY_COUNT - 1;
|
||||
return lastDay;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -96,6 +94,7 @@ public class DateSorter {
|
||||
* @return string label suitable for display to user
|
||||
*/
|
||||
public String getLabel(int index) {
|
||||
if (index < 0 || index >= DAY_COUNT) return "";
|
||||
return mLabels[index];
|
||||
}
|
||||
|
||||
@ -105,17 +104,22 @@ public class DateSorter {
|
||||
* @return date boundary at given index
|
||||
*/
|
||||
public long getBoundary(int index) {
|
||||
int lastDay = DAY_COUNT - 1;
|
||||
// Error case
|
||||
if (index < 0 || index > lastDay) index = 0;
|
||||
// Since this provides a lower boundary on dates that will be included
|
||||
// in the given bin, provide the smallest value
|
||||
if (index == lastDay) return Long.MIN_VALUE;
|
||||
return mBins[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Calcuate 12:00am by zeroing out hour, minute, second, millisecond
|
||||
*/
|
||||
private Calendar beginningOfDay(Calendar c) {
|
||||
private void beginningOfDay(Calendar c) {
|
||||
c.set(Calendar.HOUR_OF_DAY, 0);
|
||||
c.set(Calendar.MINUTE, 0);
|
||||
c.set(Calendar.SECOND, 0);
|
||||
c.set(Calendar.MILLISECOND, 0);
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user