Merge "Fix missed calls not updating properly" into klp-dev
diff --git a/src/com/android/dialer/calllog/CallLogFragment.java b/src/com/android/dialer/calllog/CallLogFragment.java
index ce27ab7..c8e2613 100644
--- a/src/com/android/dialer/calllog/CallLogFragment.java
+++ b/src/com/android/dialer/calllog/CallLogFragment.java
@@ -432,8 +432,7 @@
         updateOnTransition(true);
     }
 
-    // TODO krelease: Figure out if we still need this. If so, it should be probably be moved to
-    // the call log activity instead, or done only in a single call log fragment.
+    // TODO: Move to CallLogActivity
     private void updateOnTransition(boolean onEntry) {
         // We don't want to update any call data when keyguard is on because the user has likely not
         // seen the new calls yet.
@@ -441,6 +440,7 @@
         if (mKeyguardManager != null && !mKeyguardManager.inKeyguardRestrictedInputMode()) {
             // On either of the transitions we update the missed call and voicemail notifications.
             // While exiting we additionally consume all missed calls (by marking them as read).
+            mCallLogQueryHandler.markNewCallsAsOld();
             if (!onEntry) {
                 mCallLogQueryHandler.markMissedCallsAsRead();
             }
diff --git a/src/com/android/dialer/calllog/CallLogQueryHandler.java b/src/com/android/dialer/calllog/CallLogQueryHandler.java
index 987dedf..def3c97 100644
--- a/src/com/android/dialer/calllog/CallLogQueryHandler.java
+++ b/src/com/android/dialer/calllog/CallLogQueryHandler.java
@@ -138,22 +138,14 @@
      * <p>
      * It will asynchronously update the content of the list view when the fetch completes.
      */
-    public void fetchCalls(int callType) {
+    public void fetchCalls(int callType, long newerThan) {
         cancelFetch();
         int requestId = newCallsRequest();
-        fetchCalls(QUERY_CALLLOG_TOKEN, requestId, callType , false /* newOnly */);
+        fetchCalls(QUERY_CALLLOG_TOKEN, requestId, callType, false /* newOnly */, newerThan);
     }
 
-    /**
-     * Fetches the list of calls from the call log for a given type.
-     * This call fetches only the new (i.e. NEW = 1) ones.
-     * <p>
-     * It will asynchronously update the content of the list view when the fetch completes.
-     */
-    public void fetchNewCalls(int callType) {
-        cancelFetch();
-        int requestId = newCallsRequest();
-        fetchCalls(QUERY_CALLLOG_TOKEN, requestId, callType , true /* newOnly */);
+    public void fetchCalls(int callType) {
+        fetchCalls(callType, 0);
     }
 
     public void fetchVoicemailStatus() {
@@ -162,7 +154,8 @@
     }
 
     /** Fetches the list of calls in the call log. */
-    private void fetchCalls(int token, int requestId, int callType, boolean newOnly) {
+    private void fetchCalls(int token, int requestId, int callType, boolean newOnly,
+            long newerThan) {
         // We need to check for NULL explicitly otherwise entries with where READ is NULL
         // may not match either the query or its negation.
         // We consider the calls that are not yet consumed (i.e. IS_READ = 0) as "new".
@@ -180,8 +173,18 @@
             }
             // Add a clause to fetch only items of type voicemail.
             where.append(String.format("(%s = ?)", Calls.TYPE));
+            // Add a clause to fetch only items newer than the requested date
             selectionArgs.add(Integer.toString(callType));
         }
+
+        if (newerThan > 0) {
+            if (where.length() > 0) {
+                where.append(" AND ");
+            }
+            where.append(String.format("(%s > ?)", Calls.DATE));
+            selectionArgs.add(Long.toString(newerThan));
+        }
+
         final int limit = (mLogLimit == -1) ? NUM_LOGS_TO_DISPLAY : mLogLimit;
         final String selection = where.length() > 0 ? where.toString() : null;
         Uri uri = Calls.CONTENT_URI_WITH_VOICEMAIL.buildUpon()
diff --git a/src/com/android/dialer/list/PhoneFavoriteFragment.java b/src/com/android/dialer/list/PhoneFavoriteFragment.java
index a961347..beeb320 100644
--- a/src/com/android/dialer/list/PhoneFavoriteFragment.java
+++ b/src/com/android/dialer/list/PhoneFavoriteFragment.java
@@ -18,9 +18,12 @@
 import android.app.Activity;
 import android.app.Fragment;
 import android.app.LoaderManager;
+import android.content.Context;
 import android.content.CursorLoader;
 import android.content.Loader;
+import android.content.SharedPreferences;
 import android.database.Cursor;
+import android.database.MatrixCursor;
 import android.graphics.Rect;
 import android.net.Uri;
 import android.os.Bundle;
@@ -42,7 +45,9 @@
 import com.android.contacts.common.GeoUtil;
 import com.android.contacts.common.list.ContactEntry;
 import com.android.contacts.common.list.ContactTileView;
+import com.android.dialer.DialtactsActivity;
 import com.android.dialer.R;
+import com.android.dialer.calllog.CallLogQuery;
 import com.android.dialer.calllog.ContactInfoHelper;
 import com.android.dialer.calllog.CallLogAdapter;
 import com.android.dialer.calllog.CallLogQueryHandler;
@@ -74,6 +79,9 @@
      */
     private static int LOADER_ID_CONTACT_TILE = 1;
 
+    private static final String KEY_LAST_DISMISSED_CALL_SHORTCUT_DATE =
+            "key_last_dismissed_call_shortcut_date";
+
     public interface OnShowAllContactsListener {
         public void onShowAllContacts();
     }
@@ -157,6 +165,17 @@
      */
     private View mEmptyView;
 
+    /**
+     * Call shortcuts older than this date (persisted in shared preferences) will not show up in
+     * at the top of the screen
+     */
+    private long mLastCallShortcutDate = 0;
+
+    /**
+     * The date of the current call shortcut that is showing on screen.
+     */
+    private long mCurrentCallShortcutDate = 0;
+
     private final ContactTileView.Listener mContactTileAdapterListener =
             new ContactTileAdapterListener();
     private final LoaderManager.LoaderCallbacks<Cursor> mContactTileLoaderListener =
@@ -195,6 +214,11 @@
     @Override
     public void onResume() {
         super.onResume();
+        final SharedPreferences prefs = getActivity().getSharedPreferences(
+                DialtactsActivity.SHARED_PREFS_NAME, Context.MODE_PRIVATE);
+
+        mLastCallShortcutDate = prefs.getLong(KEY_LAST_DISMISSED_CALL_SHORTCUT_DATE, 0);
+
         fetchCalls();
         mCallLogAdapter.setLoading(true);
         getLoaderManager().getLoader(LOADER_ID_CONTACT_TILE).forceLoad();
@@ -231,7 +255,7 @@
         });
 
         mContactTileAdapter.setEmptyView(mEmptyView);
-        mAdapter = new PhoneFavoriteMergedAdapter(getActivity(), mContactTileAdapter,
+        mAdapter = new PhoneFavoriteMergedAdapter(getActivity(), this, mContactTileAdapter,
                 mCallLogAdapter, mShowAllContactsButton);
 
         mListView.setAdapter(mAdapter);
@@ -308,13 +332,20 @@
     @Override
     public void onCallsFetched(Cursor cursor) {
         mCallLogAdapter.setLoading(false);
+
+        // Save the date of the most recent call log item
+        if (cursor != null && cursor.moveToFirst()) {
+            mCurrentCallShortcutDate = cursor.getLong(CallLogQuery.DATE);
+        }
+
         mCallLogAdapter.changeCursor(cursor);
+
         mAdapter.notifyDataSetChanged();
     }
 
     @Override
     public void fetchCalls() {
-        mCallLogQueryHandler.fetchNewCalls(CallLogQueryHandler.CALL_TYPE_ALL);
+        mCallLogQueryHandler.fetchCalls(CallLogQueryHandler.CALL_TYPE_ALL, mLastCallShortcutDate);
     }
 
     @Override
@@ -452,4 +483,13 @@
     public void cacheOffsetsForDatasetChange() {
         saveOffsets();
     }
+
+    public void dismissShortcut() {
+        mLastCallShortcutDate = mCurrentCallShortcutDate;
+        final SharedPreferences prefs = getActivity().getSharedPreferences(
+                DialtactsActivity.SHARED_PREFS_NAME, Context.MODE_PRIVATE);
+        prefs.edit().putLong(KEY_LAST_DISMISSED_CALL_SHORTCUT_DATE, mLastCallShortcutDate)
+                .apply();
+        fetchCalls();
+    }
 }
diff --git a/src/com/android/dialer/list/PhoneFavoriteMergedAdapter.java b/src/com/android/dialer/list/PhoneFavoriteMergedAdapter.java
index c7554e2..cf2aeee 100644
--- a/src/com/android/dialer/list/PhoneFavoriteMergedAdapter.java
+++ b/src/com/android/dialer/list/PhoneFavoriteMergedAdapter.java
@@ -53,6 +53,7 @@
     private final PhoneFavoritesTileAdapter mContactTileAdapter;
     private final CallLogAdapter mCallLogAdapter;
     private final View mShowAllContactsButton;
+    private final PhoneFavoriteFragment mFragment;
 
     private final int mCallLogPadding;
 
@@ -70,7 +71,7 @@
             mCallLogQueryHandler.markNewVoicemailsAsOld();
             CallLogNotificationsHelper.removeMissedCallNotifications();
             CallLogNotificationsHelper.updateVoicemailNotifications(mContext);
-            mCallLogQueryHandler.fetchNewCalls(CallLogQueryHandler.CALL_TYPE_ALL);
+            mFragment.dismissShortcut();
         }
 
         @Override
@@ -96,11 +97,13 @@
     };
 
     public PhoneFavoriteMergedAdapter(Context context,
+            PhoneFavoriteFragment fragment,
             PhoneFavoritesTileAdapter contactTileAdapter,
             CallLogAdapter callLogAdapter,
             View showAllContactsButton) {
         final Resources resources = context.getResources();
         mContext = context;
+        mFragment = fragment;
         mCallLogPadding = resources.getDimensionPixelSize(R.dimen.recent_call_log_item_padding);
         mContactTileAdapter = contactTileAdapter;
         mCallLogAdapter = callLogAdapter;