Remove shortcut cards list view

Remove ListView containing shortcut cards and replace it with a single
ShortcutCard card.

Rename ShortcutCardAdapter to ShortcutCardManager with slight refactor.

Some corresponding logic changes inside ListsFragment.

Reduces the number of getView calls at startup from 4 to 1.

Change-Id: I11787bdb1d04cc19634c21bfd398b7533c75d339
diff --git a/res/layout/lists_fragment.xml b/res/layout/lists_fragment.xml
index face20e..a11c89f 100644
--- a/res/layout/lists_fragment.xml
+++ b/res/layout/lists_fragment.xml
@@ -21,15 +21,12 @@
     android:layout_height="match_parent"
     android:animateLayoutChanges="true" >
 
-    <ListView
-        android:id="@+id/shortcut_card_list"
+    <com.android.dialer.list.SwipeableShortcutCard
+        android:id="@+id/shortcut_card"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:paddingTop="@dimen/action_bar_height_large"
-        android:background="@color/actionbar_background_color"
-        android:clipToPadding="false"
-        android:fadingEdge="none"
-        android:divider="@null" />
+        android:background="@color/actionbar_background_color" />
 
     <FrameLayout
         android:layout_width="match_parent"
diff --git a/src/com/android/dialer/list/ListsFragment.java b/src/com/android/dialer/list/ListsFragment.java
index e9ce57b..b303d00 100644
--- a/src/com/android/dialer/list/ListsFragment.java
+++ b/src/com/android/dialer/list/ListsFragment.java
@@ -76,7 +76,7 @@
     private ViewPager mViewPager;
     private ViewPagerTabs mViewPagerTabs;
     private ViewPagerAdapter mViewPagerAdapter;
-    private ListView mShortcutCardsListView;
+    private SwipeableShortcutCard mShortcutCard;
     private RemoveView mRemoveView;
     private View mRemoveViewContent;
     private SpeedDialFragment mSpeedDialFragment;
@@ -87,7 +87,6 @@
 
     private String[] mTabTitles;
 
-    private ShortcutCardsAdapter mMergedAdapter;
     private CallLogAdapter mCallLogAdapter;
     private CallLogQueryHandler mCallLogQueryHandler;
     private OverlappingPaneLayout mOverlappingPaneLayout;
@@ -112,16 +111,16 @@
             // edge of the shortcut card, to achieve the animated effect of the shortcut card
             // being pushed out of view when the panel is slid upwards. slideOffset is 1 when
             // the shortcut card is fully exposed, and 0 when completely hidden.
-            float ratioCardHidden = (1 - slideOffset);
-            if (mShortcutCardsListView.getChildCount() > 0) {
-                final SwipeableShortcutCard v =
-                        (SwipeableShortcutCard) mShortcutCardsListView.getChildAt(0);
-                v.clipCard(ratioCardHidden);
+            if (mShortcutCard.isShown()) {
+                float ratioCardHidden = (1 - slideOffset);
+                mShortcutCard.clipCard(ratioCardHidden);
             }
 
             if (mActionBar != null) {
                 // Amount of available space that is not being hidden by the bottom pane
-                final int topPaneHeight = (int) (slideOffset * mShortcutCardsListView.getHeight());
+                final int shortcutCardHeight =
+                        mShortcutCard.isShown() ? mShortcutCard.getHeight() : 0;
+                final int topPaneHeight = (int) (slideOffset * shortcutCardHeight);
 
                 final int availableActionBarHeight =
                         Math.min(mActionBar.getHeight(), topPaneHeight);
@@ -250,7 +249,6 @@
         mCallLogAdapter = ObjectFactory.newCallLogAdapter(getActivity(), this,
                 new ContactInfoHelper(getActivity(), currentCountryIso), null, null, false);
 
-        mMergedAdapter = new ShortcutCardsAdapter(getActivity(), this, mCallLogAdapter);
         Trace.endSection();
         Trace.endSection();
     }
@@ -309,8 +307,8 @@
         mViewPagerTabs.setViewPager(mViewPager);
         addOnPageChangeListener(mViewPagerTabs);
 
-        mShortcutCardsListView = (ListView) parentView.findViewById(R.id.shortcut_card_list);
-        mShortcutCardsListView.setAdapter(mMergedAdapter);
+        mShortcutCard = (SwipeableShortcutCard) parentView.findViewById(R.id.shortcut_card);
+        new ShortcutCardsManager(getActivity(), this, mCallLogAdapter, mShortcutCard);
 
         mRemoveView = (RemoveView) parentView.findViewById(R.id.remove_view);
         mRemoveViewContent = parentView.findViewById(R.id.remove_view_content);
@@ -337,7 +335,6 @@
         }
 
         mCallLogAdapter.changeCursor(cursor);
-        mMergedAdapter.notifyDataSetChanged();
 
         // Refresh the overlapping pane to ensure that any changes in the shortcut card height
         // are appropriately reflected in the overlap position.
@@ -398,11 +395,12 @@
         mRemoveView.setAlpha(show ? 0 : 1);
         mRemoveView.animate().alpha(show ? 1 : 0).start();
 
-        if (mShortcutCardsListView.getChildCount() > 0) {
-            View v = mShortcutCardsListView.getChildAt(0);
-            v.animate().withLayer()
-                    .alpha(show ? REMOVE_VIEW_SHOWN_ALPHA : REMOVE_VIEW_HIDDEN_ALPHA)
-                    .start();
+        if (mShortcutCard.isShown()) {
+            final View child = mShortcutCard.getChildAt(0);
+            if (child != null) {
+                child.animate().withLayer()
+                        .alpha(show ? REMOVE_VIEW_SHOWN_ALPHA : REMOVE_VIEW_HIDDEN_ALPHA).start();
+            }
         }
     }
 
diff --git a/src/com/android/dialer/list/ShortcutCardsAdapter.java b/src/com/android/dialer/list/ShortcutCardsAdapter.java
deleted file mode 100644
index ad6a61c..0000000
--- a/src/com/android/dialer/list/ShortcutCardsAdapter.java
+++ /dev/null
@@ -1,180 +0,0 @@
-/*
- * Copyright (C) 2011 Google Inc.
- * Licensed to 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.dialer.list;
-
-import android.content.Context;
-import android.content.res.Resources;
-import android.database.Cursor;
-import android.database.DataSetObserver;
-import android.view.View;
-import android.view.ViewGroup;
-import android.widget.BaseAdapter;
-
-import com.android.dialer.calllog.CallLogAdapter;
-import com.android.dialer.calllog.CallLogNotificationsHelper;
-import com.android.dialer.calllog.CallLogQueryHandler;
-import com.android.dialer.list.SwipeHelper.OnItemGestureListener;
-
-/**
- * An adapter that displays call shortcuts from {@link com.android.dialer.calllog.CallLogAdapter}
- * in the form of cards.
- */
-public class ShortcutCardsAdapter extends BaseAdapter {
-
-    private class CustomDataSetObserver extends DataSetObserver {
-        @Override
-        public void onChanged() {
-            notifyDataSetChanged();
-        }
-    }
-
-    private static final String TAG = ShortcutCardsAdapter.class.getSimpleName();
-
-    private final CallLogAdapter mCallLogAdapter;
-
-    private final ListsFragment mFragment;
-
-    private final Context mContext;
-
-    private final DataSetObserver mObserver;
-
-    private final CallLogQueryHandler mCallLogQueryHandler;
-
-    private final OnItemGestureListener mCallLogOnItemSwipeListener =
-            new OnItemGestureListener() {
-        @Override
-        public void onSwipe(View view) {
-            mCallLogQueryHandler.markNewCallsAsOld();
-            mCallLogQueryHandler.markNewVoicemailsAsOld();
-            CallLogNotificationsHelper.removeMissedCallNotifications(mContext);
-            CallLogNotificationsHelper.updateVoicemailNotifications(mContext);
-            mFragment.dismissShortcut(view);
-        }
-
-        @Override
-        public void onTouch() {}
-
-        @Override
-        public boolean isSwipeEnabled() {
-            return true;
-        }
-    };
-
-    private final CallLogQueryHandler.Listener mCallLogQueryHandlerListener =
-            new CallLogQueryHandler.Listener() {
-        @Override
-        public void onVoicemailStatusFetched(Cursor statusCursor) {}
-
-        @Override
-        public boolean onCallsFetched(Cursor combinedCursor) {
-            mCallLogAdapter.invalidateCache();
-            mCallLogAdapter.changeCursor(combinedCursor);
-            mCallLogAdapter.notifyDataSetChanged();
-            // Return true; took ownership of cursor
-            return true;
-        }
-    };
-
-    public ShortcutCardsAdapter(Context context,
-            ListsFragment fragment,
-            CallLogAdapter callLogAdapter) {
-        final Resources resources = context.getResources();
-        mContext = context;
-        mFragment = fragment;
-
-        mCallLogAdapter = callLogAdapter;
-        mObserver = new CustomDataSetObserver();
-        mCallLogAdapter.registerDataSetObserver(mObserver);
-        mCallLogQueryHandler = new CallLogQueryHandler(mContext.getContentResolver(),
-                mCallLogQueryHandlerListener);
-    }
-
-    /**
-     * Determines the number of items in the adapter.
-     * mCallLogAdapter contains the item for the most recent caller.
-     * mContactTileAdapter contains the starred contacts.
-     * The +1 is to account for the presence of the favorites menu.
-     *
-     * @return Number of items in the adapter.
-     */
-    @Override
-    public int getCount() {
-        return mCallLogAdapter.getCount();
-    }
-
-    @Override
-    public Object getItem(int position) {
-        return mCallLogAdapter.getItem(position);
-    }
-
-    @Override
-    public long getItemId(int position) {
-        return position;
-    }
-
-    @Override
-    public boolean hasStableIds() {
-        return true;
-    }
-
-    /**
-     * Determine the number of view types present.
-     */
-    @Override
-    public int getViewTypeCount() {
-        return mCallLogAdapter.getViewTypeCount();
-    }
-
-    @Override
-    public int getItemViewType(int position) {
-        return mCallLogAdapter.getItemViewType(position);
-    }
-
-    @Override
-    public View getView(int position, View convertView, ViewGroup parent) {
-        final SwipeableShortcutCard wrapper;
-        if (convertView == null) {
-            wrapper = new SwipeableShortcutCard(mContext);
-            wrapper.setOnItemSwipeListener(mCallLogOnItemSwipeListener);
-        } else {
-            wrapper = (SwipeableShortcutCard) convertView;
-        }
-
-        // Special case wrapper view for the most recent call log item. This allows
-        // us to create a card-like effect for the more recent call log item in
-        // the PhoneFavoriteMergedAdapter, but keep the original look of the item in
-        // the CallLogAdapter.
-        final View view = mCallLogAdapter.getView(position, convertView == null ?
-                null : wrapper.getChildAt(0), parent
-        );
-        wrapper.removeAllViews();
-        wrapper.prepareChildView(view);
-        wrapper.addView(view);
-        wrapper.setVisibility(View.VISIBLE);
-        return wrapper;
-    }
-
-    @Override
-    public boolean areAllItemsEnabled() {
-        return mCallLogAdapter.areAllItemsEnabled();
-    }
-
-    @Override
-    public boolean isEnabled(int position) {
-        return mCallLogAdapter.isEnabled(position);
-    }
-}
diff --git a/src/com/android/dialer/list/ShortcutCardsManager.java b/src/com/android/dialer/list/ShortcutCardsManager.java
new file mode 100644
index 0000000..8b8a7ba
--- /dev/null
+++ b/src/com/android/dialer/list/ShortcutCardsManager.java
@@ -0,0 +1,129 @@
+/*
+ * Copyright (C) 2011 Google Inc.
+ * Licensed to 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.dialer.list;
+
+import android.content.Context;
+import android.database.Cursor;
+import android.database.DataSetObserver;
+import android.view.View;
+
+import com.android.dialer.calllog.CallLogAdapter;
+import com.android.dialer.calllog.CallLogNotificationsHelper;
+import com.android.dialer.calllog.CallLogQueryHandler;
+import com.android.dialer.list.SwipeHelper.OnItemGestureListener;
+
+/**
+ * Handles the logic that displays call shortcuts from
+ * {@link com.android.dialer.calllog.CallLogAdapter} in the form of cards.
+ */
+public class ShortcutCardsManager {
+
+    private class CustomDataSetObserver extends DataSetObserver {
+        @Override
+        public void onChanged() {
+            updateShortcutCard();
+        }
+    }
+
+    private static final String TAG = ShortcutCardsManager.class.getSimpleName();
+
+    // The position of the shortcut card within the CallLogAdapter
+    private static final int SHORTCUT_CARD_INDEX = 0;
+
+    private final CallLogAdapter mCallLogAdapter;
+    private final ListsFragment mFragment;
+    private final SwipeableShortcutCard mShortcutCard;
+
+    private final Context mContext;
+
+    private final DataSetObserver mObserver;
+
+    private final CallLogQueryHandler mCallLogQueryHandler;
+
+    private final OnItemGestureListener mCallLogOnItemSwipeListener =
+            new OnItemGestureListener() {
+        @Override
+        public void onSwipe(View view) {
+            mCallLogQueryHandler.markNewCallsAsOld();
+            mCallLogQueryHandler.markNewVoicemailsAsOld();
+            CallLogNotificationsHelper.removeMissedCallNotifications(mContext);
+            CallLogNotificationsHelper.updateVoicemailNotifications(mContext);
+            mFragment.dismissShortcut(view);
+        }
+
+        @Override
+        public void onTouch() {}
+
+        @Override
+        public boolean isSwipeEnabled() {
+            return true;
+        }
+    };
+
+    private final CallLogQueryHandler.Listener mCallLogQueryHandlerListener =
+            new CallLogQueryHandler.Listener() {
+        @Override
+        public void onVoicemailStatusFetched(Cursor statusCursor) {}
+
+        @Override
+        public boolean onCallsFetched(Cursor combinedCursor) {
+            mCallLogAdapter.invalidateCache();
+            mCallLogAdapter.changeCursor(combinedCursor);
+            mCallLogAdapter.notifyDataSetChanged();
+            // Return true; took ownership of cursor
+            return true;
+        }
+    };
+
+    public ShortcutCardsManager(Context context,
+            ListsFragment fragment,
+            CallLogAdapter callLogAdapter,
+            SwipeableShortcutCard shortcutCard) {
+        mContext = context;
+        mFragment = fragment;
+        mShortcutCard = shortcutCard;
+
+        mCallLogAdapter = callLogAdapter;
+        mObserver = new CustomDataSetObserver();
+        mCallLogAdapter.registerDataSetObserver(mObserver);
+        mCallLogQueryHandler = new CallLogQueryHandler(mContext.getContentResolver(),
+                mCallLogQueryHandlerListener);
+        mShortcutCard.setOnItemSwipeListener(mCallLogOnItemSwipeListener);
+    }
+
+    /**
+     * Updates the contents of the shortcut card with the view provided by the
+     * {@link CallLogAdapter}.
+     */
+    private void updateShortcutCard() {
+        final int count = mCallLogAdapter.getCount();
+        final View convertView = mShortcutCard.getChildAt(SHORTCUT_CARD_INDEX);
+        if (count <= SHORTCUT_CARD_INDEX) {
+            if (convertView != null) {
+                convertView.setVisibility(View.GONE);
+            }
+        } else {
+            mShortcutCard.setVisibility(View.VISIBLE);
+            final View view = mCallLogAdapter.getView(SHORTCUT_CARD_INDEX, convertView,
+                    mShortcutCard);
+            mShortcutCard.removeAllViews();
+            mShortcutCard.prepareChildView(view);
+            mShortcutCard.addView(view);
+            view.setVisibility(View.VISIBLE);
+        }
+    }
+}
diff --git a/src/com/android/dialer/list/SwipeableShortcutCard.java b/src/com/android/dialer/list/SwipeableShortcutCard.java
index 93e9fff..db2210a 100644
--- a/src/com/android/dialer/list/SwipeableShortcutCard.java
+++ b/src/com/android/dialer/list/SwipeableShortcutCard.java
@@ -19,6 +19,7 @@
 import android.content.res.Resources;
 import android.graphics.Rect;
 import android.text.TextUtils;
+import android.util.AttributeSet;
 import android.view.MotionEvent;
 import android.view.View;
 import android.view.ViewConfiguration;
@@ -56,7 +57,15 @@
     private Rect mClipRect = new Rect();
 
     public SwipeableShortcutCard(Context context) {
-        super(context);
+        this(context, null);
+    }
+
+    public SwipeableShortcutCard(Context context, AttributeSet attrs) {
+        this(context, attrs, 0);
+    }
+
+    public SwipeableShortcutCard(Context context, AttributeSet attrs, int defStyle) {
+        super(context, attrs, defStyle);
         final Resources resources = getResources();
         final float densityScale =resources.getDisplayMetrics().density;
         final float pagingTouchSlop = ViewConfiguration.get(context)
@@ -204,9 +213,7 @@
 
         if (ratioHidden > CLIP_CARD_MOSTLY_HIDDEN_RATIO) {
             mClipRect.set(0, 0 , 0, 0);
-            setVisibility(View.INVISIBLE);
         } else {
-            setVisibility(View.VISIBLE);
             int newTop = (int) (ratioHidden * height);
             mClipRect.set(0, newTop, width, height);