diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2018-01-19 21:48:41 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2018-01-19 21:48:41 +0000 |
commit | f101f6da201042951341939875be285348ae2425 (patch) | |
tree | eee4f125136bdb155919f8f17f9c9cc6d92fa00f /java/com/android/dialer/calllogutils/CallLogDates.java | |
parent | 79a26290ab9c04fbc007f4013a9b22945b6263ad (diff) | |
parent | fdaa46618ce61344bc83a66590863d126c47b05f (diff) |
Merge "Add the "Yesterday" header in the new call log"
Diffstat (limited to 'java/com/android/dialer/calllogutils/CallLogDates.java')
-rw-r--r-- | java/com/android/dialer/calllogutils/CallLogDates.java | 53 |
1 files changed, 40 insertions, 13 deletions
diff --git a/java/com/android/dialer/calllogutils/CallLogDates.java b/java/com/android/dialer/calllogutils/CallLogDates.java index 82e8e404e..84e52df14 100644 --- a/java/com/android/dialer/calllogutils/CallLogDates.java +++ b/java/com/android/dialer/calllogutils/CallLogDates.java @@ -50,7 +50,7 @@ public final class CallLogDates { return DateUtils.formatDateTime( context, timestampMillis, DateUtils.FORMAT_SHOW_TIME); // e.g. 12:15 PM } - if (isWithin3Days(nowMillis, timestampMillis)) { + if (getDayDifference(nowMillis, timestampMillis) < 3) { return formatDayOfWeek(context, timestampMillis); // e.g. "Wednesday" } return formatAbbreviatedMonthAndDay(context, timestampMillis); // e.g. "Jan 15" @@ -129,26 +129,53 @@ public final class CallLogDates { UCharacter.TITLECASE_NO_LOWERCASE); } - private static boolean isWithin3Days(long nowMillis, long timestampMillis) { - Calendar threeDaysAgoStartOfDay = Calendar.getInstance(); - threeDaysAgoStartOfDay.setTimeInMillis(nowMillis); + /** + * Returns the absolute difference in days between two timestamps. It is the caller's + * responsibility to ensure both timestamps are in milliseconds. Failure to do so will result in + * undefined behavior. + * + * <p>Note that the difference is based on day boundaries, not 24-hour periods. + * + * <p>Examples: + * + * <ul> + * <li>The difference between 01/19/2018 00:00 and 01/19/2018 23:59 is 0. + * <li>The difference between 01/18/2018 23:59 and 01/19/2018 23:59 is 1. + * <li>The difference between 01/18/2018 00:00 and 01/19/2018 23:59 is 1. + * <li>The difference between 01/17/2018 23:59 and 01/19/2018 00:00 is 2. + * </ul> + */ + public static int getDayDifference(long firstTimestamp, long secondTimestamp) { + // Ensure secondMillis is no less than firstMillis + if (secondTimestamp < firstTimestamp) { + long t = firstTimestamp; + firstTimestamp = secondTimestamp; + secondTimestamp = t; + } - // This is attempting to find the start of the current day, but it's not quite right due to + // Use secondTimestamp as reference + Calendar startOfReferenceDay = Calendar.getInstance(); + startOfReferenceDay.setTimeInMillis(secondTimestamp); + + // This is attempting to find the start of the reference day, but it's not quite right due to // daylight savings. Unfortunately there doesn't seem to be a way to get the correct start of // the day without using Joda or Java8, both of which are disallowed. This means that the wrong // formatting may be applied on days with time changes (though the displayed values will be // correct). - threeDaysAgoStartOfDay.add( - Calendar.HOUR_OF_DAY, -threeDaysAgoStartOfDay.get(Calendar.HOUR_OF_DAY)); - threeDaysAgoStartOfDay.add(Calendar.MINUTE, -threeDaysAgoStartOfDay.get(Calendar.MINUTE)); - threeDaysAgoStartOfDay.add(Calendar.SECOND, -threeDaysAgoStartOfDay.get(Calendar.SECOND)); + startOfReferenceDay.add(Calendar.HOUR_OF_DAY, -startOfReferenceDay.get(Calendar.HOUR_OF_DAY)); + startOfReferenceDay.add(Calendar.MINUTE, -startOfReferenceDay.get(Calendar.MINUTE)); + startOfReferenceDay.add(Calendar.SECOND, -startOfReferenceDay.get(Calendar.SECOND)); - threeDaysAgoStartOfDay.add(Calendar.DATE, -2); + Calendar other = Calendar.getInstance(); + other.setTimeInMillis(firstTimestamp); - Calendar then = Calendar.getInstance(); - then.setTimeInMillis(timestampMillis); + int dayDifference = 0; + while (other.before(startOfReferenceDay)) { + startOfReferenceDay.add(Calendar.DATE, -1); + dayDifference++; + } - return then.equals(threeDaysAgoStartOfDay) || then.after(threeDaysAgoStartOfDay); + return dayDifference; } /** Returns true if the provided timestamps are from the same day in the default time zone. */ |