Show the duration of the call in QuickContact "Recent card"

bug 28806182

Change-Id: I3bc21dbe0096221264d7ffb955281a16a40c54f6
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 4fe3edc..359e536 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1793,4 +1793,13 @@
 
     <!-- Content description for (...) in no name header [CHAR LIMIT=30]-->
     <string name="description_no_name_header">Ellipsis</string>
+
+    <!-- Formatted call duration displayed in recent card in QuickContact, for duration less than 1 minute -->
+    <string name="callDurationSecondFormat"><xliff:g id="seconds">%s</xliff:g> sec</string>
+
+    <!-- Formatted call duration displayed in recent card in QuickContact, for duration less than 1 hour -->
+    <string name="callDurationMinuteFormat"><xliff:g id="minutes">%s</xliff:g> min <xliff:g id="seconds">%s</xliff:g> sec</string>
+
+    <!-- Formatted call duration displayed in recent card in QuickContact, for duration more than 1 hour -->
+    <string name="callDurationHourFormat"><xliff:g id="minutes">%s</xliff:g> hr <xliff:g id="minutes">%s</xliff:g> min <xliff:g id="seconds">%s</xliff:g> sec</string>
 </resources>
diff --git a/src/com/android/contacts/interactions/CallLogInteraction.java b/src/com/android/contacts/interactions/CallLogInteraction.java
index 3464c0f..06fd273 100644
--- a/src/com/android/contacts/interactions/CallLogInteraction.java
+++ b/src/com/android/contacts/interactions/CallLogInteraction.java
@@ -87,9 +87,18 @@
 
     @Override
     public String getViewFooter(Context context) {
-        Long date = getDate();
-        return date == null ? null : ContactInteractionUtil.formatDateStringFromTimestamp(
-                date, context);
+        final Long date = getDate();
+        if (date != null) {
+            final StringBuilder callDetail = new StringBuilder();
+            callDetail.append(ContactInteractionUtil.formatDateStringFromTimestamp(date, context));
+            final Long duration = getDuration();
+            if (duration != null) {
+                callDetail.append("\n");
+                callDetail.append(ContactInteractionUtil.formatDuration(duration, context));
+            }
+            return callDetail.toString();
+        }
+        return null;
     }
 
     @Override
diff --git a/src/com/android/contacts/interactions/ContactInteractionUtil.java b/src/com/android/contacts/interactions/ContactInteractionUtil.java
index 8ec0547..b2bda5d 100644
--- a/src/com/android/contacts/interactions/ContactInteractionUtil.java
+++ b/src/com/android/contacts/interactions/ContactInteractionUtil.java
@@ -26,6 +26,8 @@
 
 import java.util.Calendar;
 
+import com.android.contacts.R;
+
 /**
  * Utility methods for interactions and their loaders
  */
@@ -84,4 +86,23 @@
         return c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) &&
                 c1.get(Calendar.DAY_OF_YEAR) == c2.get(Calendar.DAY_OF_YEAR);
     }
+
+    /**
+     * Takes duration of the call in seconds.
+     * Return the formatted duration in hr, min, sec order if they exist.
+     */
+    @NeededForTesting
+    public static String formatDuration(long callDuration, Context context) {
+        final int hours = (int) callDuration / 3600;
+        final int minutes = (int) (callDuration % 3600) / 60;
+        final int seconds = (int) (callDuration % 60);
+
+        if (hours > 0) {
+            return context.getString(R.string.callDurationHourFormat, hours, minutes, seconds);
+        } else if (minutes > 0) {
+            return context.getString(R.string.callDurationMinuteFormat, minutes, seconds);
+        } else {
+            return context.getString(R.string.callDurationSecondFormat, seconds);
+        }
+    }
 }
diff --git a/tests/src/com/android/contacts/interactions/ContactInteractionUtilTest.java b/tests/src/com/android/contacts/interactions/ContactInteractionUtilTest.java
index 86167c1..07ad722 100644
--- a/tests/src/com/android/contacts/interactions/ContactInteractionUtilTest.java
+++ b/tests/src/com/android/contacts/interactions/ContactInteractionUtilTest.java
@@ -90,6 +90,31 @@
                         getContext()));
     }
 
+    public void testFormatDuration_zero() {
+        assertEquals("0 sec",
+                ContactInteractionUtil.formatDuration(0, getContext()));
+    }
+
+    public void testFormatDuration_minZeroSec() {
+        assertEquals("1 min 0 sec",
+                ContactInteractionUtil.formatDuration(60, getContext()));
+    }
+
+    public void testFormatDuration_minSec() {
+        assertEquals("30 min 9 sec",
+                ContactInteractionUtil.formatDuration(1809, getContext()));
+    }
+
+    public void testFormatDuration_hrZeroMinZeroSec() {
+        assertEquals("1 hr 0 min 0 sec",
+                ContactInteractionUtil.formatDuration(3600, getContext()));
+    }
+
+    public void testFormatDuration_hrMinSec() {
+        assertEquals("2 hr 44 min 36 sec",
+                ContactInteractionUtil.formatDuration(9876, getContext()));
+    }
+
     private void setLocale(Locale locale) {
         Locale.setDefault(locale);
         Resources res = getContext().getResources();