diff options
author | Leon Scroggins <scroggo@google.com> | 2010-01-07 18:47:06 -0500 |
---|---|---|
committer | Leon Scroggins <scroggo@google.com> | 2010-01-08 09:18:37 -0500 |
commit | c89b13b962c6634db536e563a42d058702ce045b (patch) | |
tree | fc37c9e36c7a03c9d68bebe39c5b77b188e1a1c8 /core | |
parent | affa5d2c0f7c2ade43ded5ff9a7952b269183117 (diff) | |
download | frameworks_base-c89b13b962c6634db536e563a42d058702ce045b.zip frameworks_base-c89b13b962c6634db536e563a42d058702ce045b.tar.gz frameworks_base-c89b13b962c6634db536e563a42d058702ce045b.tar.bz2 |
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
Diffstat (limited to 'core')
-rw-r--r-- | core/java/android/webkit/DateSorter.java | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/core/java/android/webkit/DateSorter.java b/core/java/android/webkit/DateSorter.java index c46702e..16feaa9 100644 --- a/core/java/android/webkit/DateSorter.java +++ b/core/java/android/webkit/DateSorter.java @@ -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; } } |