Performance improvements for contact card with social updates

- Use background color with alpha hex color instead of
alpha attribute of View
- Instead of posting a runnable in onMeasure, move it
to setCurrentPage / setFragments methods

Bug: 5161205

Change-Id: Ib1ad3b11c359d8fdc1e9143ab9d64c39aa914b91
diff --git a/res/layout-w470dp/contact_detail_fragment.xml b/res/layout-w470dp/contact_detail_fragment.xml
index d63236d..982abbb 100644
--- a/res/layout-w470dp/contact_detail_fragment.xml
+++ b/res/layout-w470dp/contact_detail_fragment.xml
@@ -76,7 +76,6 @@
         android:layout_height="match_parent"
         android:layout_alignParentLeft="true"
         android:layout_alignParentTop="true"
-        android:background="@android:color/black"
         android:visibility="gone"/>
 
     <View
diff --git a/res/layout-w470dp/contact_detail_updates_fragment.xml b/res/layout-w470dp/contact_detail_updates_fragment.xml
index 60d728d..ccb7123 100644
--- a/res/layout-w470dp/contact_detail_updates_fragment.xml
+++ b/res/layout-w470dp/contact_detail_updates_fragment.xml
@@ -32,7 +32,6 @@
         android:layout_height="match_parent"
         android:layout_alignParentLeft="true"
         android:layout_alignParentTop="true"
-        android:background="@android:color/black"
         android:visibility="gone"/>
 
     <View
diff --git a/res/layout/carousel_about_tab.xml b/res/layout/carousel_about_tab.xml
index ad763be..c7c4394 100644
--- a/res/layout/carousel_about_tab.xml
+++ b/res/layout/carousel_about_tab.xml
@@ -19,8 +19,7 @@
     class="com.android.contacts.detail.CarouselTab"
     android:layout_width="0dip"
     android:layout_height="match_parent"
-    android:layout_weight="1"
-    android:background="@color/detail_tab_background">
+    android:layout_weight="1">
 
     <ImageView android:id="@+id/photo"
         android:scaleType="centerCrop"
@@ -45,8 +44,6 @@
         android:layout_alignParentLeft="true"
         android:layout_alignParentTop="true"
         android:layout_marginBottom="@dimen/detail_tab_carousel_tab_label_height"
-        android:background="@android:color/black"
-        android:alpha="0"
         android:visibility="gone"/>
 
     <TextView
diff --git a/res/layout/carousel_updates_tab.xml b/res/layout/carousel_updates_tab.xml
index b41829f..9637023 100644
--- a/res/layout/carousel_updates_tab.xml
+++ b/res/layout/carousel_updates_tab.xml
@@ -33,7 +33,6 @@
         android:layout_alignParentLeft="true"
         android:visibility="gone" />
 
-
     <!-- Transparent view to overlay on the update photo
     (to allow white text to appear over a white photo). -->
     <View
@@ -65,8 +64,6 @@
         android:layout_alignParentLeft="true"
         android:layout_alignParentTop="true"
         android:layout_marginBottom="@dimen/detail_tab_carousel_tab_label_height"
-        android:background="@android:color/black"
-        android:alpha="0"
         android:visibility="gone"/>
 
     <TextView
diff --git a/src/com/android/contacts/detail/CarouselTab.java b/src/com/android/contacts/detail/CarouselTab.java
index 9b8efd5..26397ff 100644
--- a/src/com/android/contacts/detail/CarouselTab.java
+++ b/src/com/android/contacts/detail/CarouselTab.java
@@ -94,8 +94,6 @@
 
     @Override
     public void setAlphaLayerValue(float alpha) {
-        if (mAlphaLayer != null) {
-            mAlphaLayer.setAlpha(alpha);
-        }
+        ContactDetailDisplayUtils.setAlphaOnViewBackground(mAlphaLayer, alpha);
     }
 }
diff --git a/src/com/android/contacts/detail/ContactDetailDisplayUtils.java b/src/com/android/contacts/detail/ContactDetailDisplayUtils.java
index 131f052..cbdf148 100644
--- a/src/com/android/contacts/detail/ContactDetailDisplayUtils.java
+++ b/src/com/android/contacts/detail/ContactDetailDisplayUtils.java
@@ -452,4 +452,15 @@
             }
         }
     }
+
+    /**
+     * Sets an alpha value on the view.
+     */
+    public static void setAlphaOnViewBackground(View view, float alpha) {
+        if (view != null) {
+            // Convert alpha layer to a black background HEX color with an alpha value for better
+            // performance (i.e. use setBackgroundColor() instead of setAlpha())
+            view.setBackgroundColor((int) (alpha * 255) << 24);
+        }
+    }
 }
diff --git a/src/com/android/contacts/detail/ContactDetailFragment.java b/src/com/android/contacts/detail/ContactDetailFragment.java
index 88ae7da..bbf9d5b 100644
--- a/src/com/android/contacts/detail/ContactDetailFragment.java
+++ b/src/com/android/contacts/detail/ContactDetailFragment.java
@@ -315,9 +315,7 @@
 
     @Override
     public void setAlphaLayerValue(float alpha) {
-        if (mAlphaLayer != null) {
-            mAlphaLayer.setAlpha(alpha);
-        }
+        ContactDetailDisplayUtils.setAlphaOnViewBackground(mAlphaLayer, alpha);
     }
 
     @Override
diff --git a/src/com/android/contacts/detail/ContactDetailFragmentCarousel.java b/src/com/android/contacts/detail/ContactDetailFragmentCarousel.java
index 561d44e..7efcc51 100644
--- a/src/com/android/contacts/detail/ContactDetailFragmentCarousel.java
+++ b/src/com/android/contacts/detail/ContactDetailFragmentCarousel.java
@@ -98,27 +98,16 @@
             mAllowedHorizontalScrollLength = (2 * fragmentWidth) - screenWidth;
             mLowerThreshold = (screenWidth - fragmentWidth) / 2;
             mUpperThreshold = mAllowedHorizontalScrollLength - mLowerThreshold;
-
-            // Snap to the current page now that the allowed horizontal scroll length has been
-            // computed.
-            mHandler.post(new Runnable() {
-                @Override
-                public void run() {
-                    if (isAttachedToWindow() && mAboutFragment != null &&
-                            mUpdatesFragment != null) {
-                        snapToEdge();
-                    }
-                }
-            });
         }
     }
 
     public void setCurrentPage(int pageIndex) {
         if (mCurrentPage != pageIndex) {
             mCurrentPage = pageIndex;
-            if (isAttachedToWindow() && mAboutFragment != null && mUpdatesFragment != null) {
-                snapToEdge();
-            }
+
+            // This method could have been called before the view has been measured, so snap to edge
+            // only after the view is ready.
+            postRunnableToSnapToEdge();
         }
     }
 
@@ -131,7 +120,26 @@
         mUpdatesFragment.enableAlphaLayer();
         mUpdatesFragment.setAlphaLayerValue(mCurrentPage == UPDATES_PAGE ? 0 : MAX_ALPHA);
 
-        snapToEdge();
+        // This method could have been called before the view has been measured, so snap to edge
+        // only after the view is ready.
+        postRunnableToSnapToEdge();
+    }
+
+    /**
+     * Snap to the currently selected page only once all the view setup and measurement has
+     * completed (i.e. we need to know the allowed horizontal scroll width in order to
+     * snap to the correct page).
+     */
+    private void postRunnableToSnapToEdge() {
+        mHandler.post(new Runnable() {
+            @Override
+            public void run() {
+                if (isAttachedToWindow() && mAboutFragment != null &&
+                        mUpdatesFragment != null) {
+                    snapToEdge();
+                }
+            }
+        });
     }
 
     public int getCurrentPage() {
diff --git a/src/com/android/contacts/detail/ContactDetailTabCarousel.java b/src/com/android/contacts/detail/ContactDetailTabCarousel.java
index cc4f0ff..5daca05 100644
--- a/src/com/android/contacts/detail/ContactDetailTabCarousel.java
+++ b/src/com/android/contacts/detail/ContactDetailTabCarousel.java
@@ -162,7 +162,7 @@
         mAboutTab.setAlphaLayerValue(mLastScrollPosition * MAX_ALPHA /
                 mAllowedHorizontalScrollLength);
         mUpdatesTab.setAlphaLayerValue(MAX_ALPHA - mLastScrollPosition * MAX_ALPHA /
-                mAllowedVerticalScrollLength);
+                mAllowedHorizontalScrollLength);
     }
 
     @Override
diff --git a/src/com/android/contacts/detail/ContactDetailUpdatesFragment.java b/src/com/android/contacts/detail/ContactDetailUpdatesFragment.java
index 02bd314..b2a203b 100644
--- a/src/com/android/contacts/detail/ContactDetailUpdatesFragment.java
+++ b/src/com/android/contacts/detail/ContactDetailUpdatesFragment.java
@@ -142,9 +142,7 @@
 
     @Override
     public void setAlphaLayerValue(float alpha) {
-        if (mAlphaLayer != null) {
-            mAlphaLayer.setAlpha(alpha);
-        }
+        ContactDetailDisplayUtils.setAlphaOnViewBackground(mAlphaLayer, alpha);
     }
 
     @Override