Apply card padding when fragment becomes unhidden

Refactor ContactListViewUtils to reduce copy and pasting.

Apply padding inside ContactEntryListFragment#onHiddenChanged().

Bug: 17303217
Bug: 17135027
Change-Id: I8f972f1a503fdbf7561aea1c5fef9316c49a0bac
diff --git a/src/com/android/contacts/common/list/ContactEntryListFragment.java b/src/com/android/contacts/common/list/ContactEntryListFragment.java
index 21660d4..62515e2 100644
--- a/src/com/android/contacts/common/list/ContactEntryListFragment.java
+++ b/src/com/android/contacts/common/list/ContactEntryListFragment.java
@@ -707,32 +707,6 @@
             mListView.requestFocus();
         }
 
-        // Set a padding on the list view so it appears in the center of the card
-        // in the layout if required.
-        Resources resources = getResources();
-        final int listSpaceWeight = resources.getInteger(
-                R.integer.contact_list_space_layout_weight);
-        final int listViewWeight = resources.getInteger(
-                R.integer.contact_list_card_layout_weight);
-        if (listSpaceWeight > 0 && listViewWeight > 0) {
-            // Set the card view visible
-            mView.setBackgroundResource(0);
-            View mCardView = mView.findViewById(R.id.list_card);
-            if (mCardView == null) {
-                throw new RuntimeException(
-                        "Your content must have a list card view who can be turned visible " +
-                        "whenever it is necessary.");
-            }
-            mCardView.setVisibility(View.VISIBLE);
-            // Add extra padding to the list view to make them appear in the center of the card.
-            SchedulingUtils.doOnPreDraw(mListView, true, new Runnable() {
-                @Override
-                public void run() {
-                    ContactListViewUtils.addPaddingToView(
-                            mListView, listSpaceWeight, listViewWeight);
-                }
-            });
-        }
         return mView;
     }
 
@@ -767,6 +741,18 @@
         configurePhotoLoader();
 
         getAdapter().setFragmentRootView(getView());
+
+        ContactListViewUtils.applyCardPaddingToView(getResources(), mListView, mView);
+    }
+
+    @Override
+    public void onHiddenChanged(boolean hidden) {
+        super.onHiddenChanged(hidden);
+        if (getActivity() != null && getView() != null && !hidden) {
+            // If the padding was last applied when in a hidden state, it may have been applied
+            // incorrectly. Therefore we need to reapply it.
+            ContactListViewUtils.applyCardPaddingToView(getResources(), mListView, getView());
+        }
     }
 
     protected void configurePhotoLoader() {
diff --git a/src/com/android/contacts/common/util/ContactListViewUtils.java b/src/com/android/contacts/common/util/ContactListViewUtils.java
index 05c17ae..2bb5c73 100644
--- a/src/com/android/contacts/common/util/ContactListViewUtils.java
+++ b/src/com/android/contacts/common/util/ContactListViewUtils.java
@@ -1,24 +1,21 @@
 package com.android.contacts.common.util;
 
 
+import com.android.contacts.common.R;
+
+import android.content.res.Resources;
 import android.view.View;
 import android.widget.ListView;
 
 /**
- * Utilities for loading contact list view.
+ * Utilities for configuring ListViews with a card background.
  */
 public class ContactListViewUtils {
 
     // These two constants will help add more padding for the text inside the card.
     private static final double TEXT_LEFT_PADDING_TO_CARD_PADDING_RATIO = 1.1;
 
-    /**
-     * Add padding to the given list view if the given resources has set
-     * both space weight and view weight on the layout. Use this util method
-     * instead of defining in the layout file so that the list view padding
-     * can be set proportional to the card padding.
-     */
-    public static void addPaddingToView(ListView listView, int listSpaceWeight, int listViewWeight)
+    private static void addPaddingToView(ListView listView, int listSpaceWeight, int listViewWeight)
     {
         if (listSpaceWeight > 0 && listViewWeight > 0) {
             double paddingPercent = (double) listSpaceWeight / (double)
@@ -34,4 +31,44 @@
             listView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
         }
     }
+
+    /**
+     * Add padding to {@param listView} if this configuration has set both space weight and
+     * view weight on the layout. Use this util method instead of defining the padding in the
+     * layout file so that the {@param listView}'s padding can be set proportional to the card
+     * padding.
+     *
+     * @param resources
+     * @param listView ListView that we add padding to
+     * @param rootLayout layout that contains ListView and R.id.list_card
+     */
+    public static void applyCardPaddingToView(Resources resources,
+            final ListView listView, View rootLayout) {
+        // Set a padding on the list view so it appears in the center of the card
+        // in the layout if required.
+        final int listSpaceWeight = resources.getInteger(
+                R.integer.contact_list_space_layout_weight);
+        final int listViewWeight = resources.getInteger(
+                R.integer.contact_list_card_layout_weight);
+        if (listSpaceWeight > 0 && listViewWeight > 0) {
+            rootLayout.setBackgroundResource(0);
+            // Set the card view visible
+            View mCardView = rootLayout.findViewById(R.id.list_card);
+            if (mCardView == null) {
+                throw new RuntimeException(
+                        "Your content must have a list card view who can be turned visible " +
+                                "whenever it is necessary.");
+            }
+            mCardView.setVisibility(View.VISIBLE);
+            // Add extra padding to the list view to make them appear in the center of the card.
+            // In order to avoid jumping, we skip drawing the next frame of the ListView.
+            SchedulingUtils.doOnPreDraw(listView, /* drawNextFrame = */ false, new Runnable() {
+                @Override
+                public void run() {
+                    ContactListViewUtils.addPaddingToView(
+                            listView, listSpaceWeight, listViewWeight);
+                }
+            });
+        }
+    }
 }