Merge "Adding common contact list view and card layout" into lmp-dev
diff --git a/src/com/android/contacts/common/list/ContactEntryListFragment.java b/src/com/android/contacts/common/list/ContactEntryListFragment.java
index 78e7ce4..21660d4 100644
--- a/src/com/android/contacts/common/list/ContactEntryListFragment.java
+++ b/src/com/android/contacts/common/list/ContactEntryListFragment.java
@@ -17,13 +17,13 @@
 package com.android.contacts.common.list;
 
 import android.app.Activity;
-import android.app.Fragment;
 import android.app.LoaderManager;
 import android.app.LoaderManager.LoaderCallbacks;
 import android.content.Context;
 import android.content.CursorLoader;
 import android.content.Intent;
 import android.content.Loader;
+import android.content.res.Resources;
 import android.database.Cursor;
 import android.os.Bundle;
 import android.os.Handler;
@@ -45,9 +45,11 @@
 import android.widget.ListView;
 
 import com.android.common.widget.CompositeCursorAdapter.Partition;
-import com.android.contacts.common.ContactPhotoManager;
 import com.android.contacts.common.R;
+import com.android.contacts.common.ContactPhotoManager;
 import com.android.contacts.common.preference.ContactsPreferences;
+import com.android.contacts.common.util.ContactListViewUtils;
+import com.android.contacts.common.util.SchedulingUtils;
 import com.android.dialerbind.analytics.AnalyticsFragment;
 
 import java.util.Locale;
@@ -705,6 +707,32 @@
             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;
     }
 
diff --git a/src/com/android/contacts/common/util/ContactListViewUtils.java b/src/com/android/contacts/common/util/ContactListViewUtils.java
new file mode 100644
index 0000000..7748607
--- /dev/null
+++ b/src/com/android/contacts/common/util/ContactListViewUtils.java
@@ -0,0 +1,35 @@
+package com.android.contacts.common.util;
+
+
+import android.content.res.Resources;
+import android.widget.ListView;
+import com.android.contacts.common.R;
+
+/**
+ * Utilities for loading contact list view.
+ */
+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)
+    {
+        if (listSpaceWeight > 0 && listViewWeight > 0) {
+            double paddingPercent = (double) listSpaceWeight / (double)
+                    (listSpaceWeight * 2 + listViewWeight);
+            int width = listView.getWidth();
+            listView.setPadding(
+                    (int) (width * paddingPercent * TEXT_LEFT_PADDING_TO_CARD_PADDING_RATIO),
+                    listView.getPaddingTop(),
+                    (int) (width * paddingPercent * TEXT_LEFT_PADDING_TO_CARD_PADDING_RATIO),
+                    listView.getPaddingBottom());
+        }
+    }
+}
diff --git a/src/com/android/contacts/common/util/SchedulingUtils.java b/src/com/android/contacts/common/util/SchedulingUtils.java
new file mode 100644
index 0000000..1dfa153
--- /dev/null
+++ b/src/com/android/contacts/common/util/SchedulingUtils.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2012 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 android.view.View;
+import android.view.ViewTreeObserver.OnGlobalLayoutListener;
+import android.view.ViewTreeObserver.OnPreDrawListener;
+
+/** Static methods that are useful for scheduling actions to occur at a later time. */
+public class SchedulingUtils {
+
+
+    /** Runs a piece of code after the next layout run */
+    public static void doAfterLayout(final View view, final Runnable runnable) {
+        final OnGlobalLayoutListener listener = new OnGlobalLayoutListener() {
+            @Override
+            public void onGlobalLayout() {
+                // Layout pass done, unregister for further events
+                view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
+                runnable.run();
+            }
+        };
+        view.getViewTreeObserver().addOnGlobalLayoutListener(listener);
+    }
+
+    /** Runs a piece of code just before the next draw, after layout and measurement */
+    public static void doOnPreDraw(final View view, final boolean drawNextFrame,
+            final Runnable runnable) {
+        final OnPreDrawListener listener = new OnPreDrawListener() {
+            @Override
+            public boolean onPreDraw() {
+                view.getViewTreeObserver().removeOnPreDrawListener(this);
+                runnable.run();
+                return drawNextFrame;
+            }
+        };
+        view.getViewTreeObserver().addOnPreDrawListener(listener);
+    }
+}