summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/calllogutils
diff options
context:
space:
mode:
authorlinyuh <linyuh@google.com>2018-01-19 11:55:16 -0800
committerCopybara-Service <copybara-piper@google.com>2018-01-19 13:17:07 -0800
commitfdaa46618ce61344bc83a66590863d126c47b05f (patch)
treeeee4f125136bdb155919f8f17f9c9cc6d92fa00f /java/com/android/dialer/calllogutils
parent7e421825670b090d2fe035575769af751e908015 (diff)
Add the "Yesterday" header in the new call log
Bug: 70989598 Test: NewCallLogAdapterTest, CallLogDatesTest PiperOrigin-RevId: 182567571 Change-Id: Ieabbe709668d843334bc3bf4a128834fddb57cb8
Diffstat (limited to 'java/com/android/dialer/calllogutils')
-rw-r--r--java/com/android/dialer/calllogutils/CallLogDates.java53
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. */