Merge "Add AnimatorListenerAdapter stub"
diff --git a/src/com/android/contacts/common/list/ContactEntryListFragment.java b/src/com/android/contacts/common/list/ContactEntryListFragment.java
index b1edc59..8543da3 100644
--- a/src/com/android/contacts/common/list/ContactEntryListFragment.java
+++ b/src/com/android/contacts/common/list/ContactEntryListFragment.java
@@ -95,6 +95,7 @@
     private boolean mIncludeProfile;
     private boolean mSearchMode;
     private boolean mVisibleScrollbarEnabled;
+    private boolean mShowEmptyListForEmptyQuery;
     private int mVerticalScrollbarPosition = getDefaultVerticalScrollbarPosition();
     private String mQueryString;
     private int mDirectorySearchMode = DirectoryListLoader.SEARCH_MODE_NONE;
@@ -560,9 +561,8 @@
     }
 
     /**
-     * Enter/exit search mode.  By design, a fragment enters search mode only when it has a
-     * non-empty query text, so the mode must be tightly related to the current query.
-     * For this reason this method must only be called by {@link #setQueryString}.
+     * Enter/exit search mode. This is method is tightly related to the current query, and should
+     * only be called by {@link #setQueryString}.
      *
      * Also note this method doesn't call {@link #reloadData()}; {@link #setQueryString} does it.
      */
@@ -605,12 +605,19 @@
     }
 
     public void setQueryString(String queryString, boolean delaySelection) {
-        // Normalize the empty query.
-        if (TextUtils.isEmpty(queryString)) queryString = null;
-
         if (!TextUtils.equals(mQueryString, queryString)) {
+            if (mShowEmptyListForEmptyQuery && mAdapter != null && mListView != null) {
+                if (TextUtils.isEmpty(mQueryString)) {
+                    // Restore the adapter if the query used to be empty.
+                    mListView.setAdapter(mAdapter);
+                } else if (TextUtils.isEmpty(queryString)) {
+                    // Instantly clear the list view if the new query is empty.
+                    mListView.setAdapter(null);
+                }
+            }
+
             mQueryString = queryString;
-            setSearchMode(!TextUtils.isEmpty(mQueryString));
+            setSearchMode(!TextUtils.isEmpty(mQueryString) || mShowEmptyListForEmptyQuery);
 
             if (mAdapter != null) {
                 mAdapter.setQueryString(queryString);
@@ -619,6 +626,10 @@
         }
     }
 
+    public void setShowEmptyListForNullQuery(boolean show) {
+        mShowEmptyListForEmptyQuery = show;
+    }
+
     public int getDirectoryLoaderId() {
         return DIRECTORY_LOADER_ID;
     }
diff --git a/src/com/android/contacts/common/util/DateUtils.java b/src/com/android/contacts/common/util/DateUtils.java
index f527eb9..5d8ba70 100644
--- a/src/com/android/contacts/common/util/DateUtils.java
+++ b/src/com/android/contacts/common/util/DateUtils.java
@@ -18,6 +18,7 @@
 
 import android.content.Context;
 import android.text.format.DateFormat;
+import android.text.format.Time;
 
 
 import java.text.ParsePosition;
@@ -268,4 +269,51 @@
         }
         return anniversary.getTime();
     }
+
+    /**
+     * Retrieves a locale-specific string for "Today".
+     *
+     * @return Locale-specific string for "Today".
+     */
+    public static CharSequence getTodayString() {
+       return android.text.format.DateUtils.getRelativeTimeSpanString(
+               -android.text.format.DateUtils.HOUR_IN_MILLIS,
+               0,
+               android.text.format.DateUtils.DAY_IN_MILLIS,
+               android.text.format.DateUtils.FORMAT_ABBREV_RELATIVE);
+
+    }
+
+    /**
+     * Retrieves a locale-specific string for "Yesterday".
+     *
+     * @return Locale-specific string for "Yesterday".
+     */
+    public static CharSequence getYesterdayString() {
+        return android.text.format.DateUtils.getRelativeTimeSpanString(
+                -android.text.format.DateUtils.DAY_IN_MILLIS,
+                0,
+                android.text.format.DateUtils.DAY_IN_MILLIS,
+                android.text.format.DateUtils.FORMAT_ABBREV_RELATIVE);
+
+    }
+
+    /**
+     * Determine the difference, in days between two dates.  Uses similar logic as the
+     * {@link android.text.format.DateUtils.getRelativeTimeSpanString} method.
+     *
+     * @param time Instance of time object to use for calculations.
+     * @param date1 First date to check.
+     * @param date2 Second date to check.
+     * @return The absolute difference in days between the two dates.
+     */
+    public static int getDayDifference(Time time, long date1, long date2) {
+        time.set(date1);
+        int startDay = Time.getJulianDay(date1, time.gmtoff);
+
+        time.set(date2);
+        int currentDay = Time.getJulianDay(date2, time.gmtoff);
+
+        return Math.abs(currentDay - startDay);
+    }
 }
diff --git a/tests/src/com/android/contacts/common/util/DateUtilTests.java b/tests/src/com/android/contacts/common/util/DateUtilTests.java
new file mode 100644
index 0000000..c225780
--- /dev/null
+++ b/tests/src/com/android/contacts/common/util/DateUtilTests.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.contacts.common.util;
+
+import junit.framework.TestCase;
+
+import android.test.suitebuilder.annotation.SmallTest;
+import android.text.format.Time;
+
+/**
+ * Unit tests for {@link com.android.contacts.common.util.DateUtils}.
+ */
+@SmallTest
+public class DateUtilTests extends TestCase {
+
+    /**
+     * Test ability to get the word "Yesterday".
+     */
+    public void testGetYesterday() {
+        assertEquals("Yesterday", DateUtils.getYesterdayString());
+    }
+
+    /**
+     * Test ability to get the word "Today".
+     */
+    public void testGetToday() {
+        assertEquals("Yesterday", DateUtils.getYesterdayString());
+    }
+
+    /**
+     * Test date differences which are in the same day.
+     */
+    public void testDayDiffNone() {
+        Time time = new Time();
+        long date1 = System.currentTimeMillis();
+        long date2 = System.currentTimeMillis() + android.text.format.DateUtils.HOUR_IN_MILLIS;
+        assertEquals(0, DateUtils.getDayDifference(time, date1, date2));
+        assertEquals(0, DateUtils.getDayDifference(time, date2, date1));
+    }
+
+    /**
+     * Test date differences which are a day apart.
+     */
+    public void testDayDiffOne() {
+        Time time = new Time();
+        long date1 = System.currentTimeMillis();
+        long date2 = date1 + android.text.format.DateUtils.DAY_IN_MILLIS;
+        assertEquals(1, DateUtils.getDayDifference(time, date1, date2));
+        assertEquals(1, DateUtils.getDayDifference(time, date2, date1));
+    }
+
+    /**
+     * Test date differences which are two days apart.
+     */
+    public void testDayDiffTwo() {
+        Time time = new Time();
+        long date1 = System.currentTimeMillis();
+        long date2 = date1 + 2*android.text.format.DateUtils.DAY_IN_MILLIS;
+        assertEquals(2, DateUtils.getDayDifference(time, date1, date2));
+        assertEquals(2, DateUtils.getDayDifference(time, date2, date1));
+    }
+}