Merge "Attach listener to allow clicks to be handled on contact details"
diff --git a/src/com/android/contacts/activities/ContactDetailActivity.java b/src/com/android/contacts/activities/ContactDetailActivity.java
index 681cf0b..3cd07ca 100644
--- a/src/com/android/contacts/activities/ContactDetailActivity.java
+++ b/src/com/android/contacts/activities/ContactDetailActivity.java
@@ -28,6 +28,7 @@
 import com.android.contacts.detail.ContactDetailViewPagerAdapter;
 import com.android.contacts.detail.ContactLoaderFragment;
 import com.android.contacts.detail.ContactLoaderFragment.ContactLoaderFragmentListener;
+import com.android.contacts.detail.TabCarouselScrollManager;
 import com.android.contacts.interactions.ContactDeletionInteraction;
 import com.android.contacts.model.AccountWithDataSet;
 import com.android.contacts.util.PhoneCapabilityTester;
@@ -55,8 +56,6 @@
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.ViewGroup;
-import android.widget.AbsListView;
-import android.widget.AbsListView.OnScrollListener;
 import android.widget.CheckBox;
 import android.widget.Toast;
 
@@ -153,8 +152,7 @@
         }
 
         mDetailFragment.setListener(mFragmentListener);
-        mDetailFragment.setVerticalScrollListener(mVerticalScrollListener);
-        mUpdatesFragment.setVerticalScrollListener(mVerticalScrollListener);
+        TabCarouselScrollManager.bind(mTabCarousel, mDetailFragment, mUpdatesFragment);
         mDetailFragment.setData(mLookupUri, mContactData);
         mUpdatesFragment.setData(mLookupUri, mContactData);
 
@@ -556,34 +554,6 @@
         }
     };
 
-    private OnScrollListener mVerticalScrollListener = new OnScrollListener() {
-
-        @Override
-        public void onScroll(
-                AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
-            if (mTabCarousel == null) {
-                return;
-            }
-            // If the FIRST item is not visible on the screen, then the carousel must be pinned
-            // at the top of the screen.
-            if (firstVisibleItem != 0) {
-                mTabCarousel.setY(-mTabCarousel.getAllowedVerticalScrollLength());
-                return;
-            }
-            View topView = view.getChildAt(firstVisibleItem);
-            if (topView == null) {
-                return;
-            }
-            int amtToScroll = Math.max((int) view.getChildAt(firstVisibleItem).getY(),
-                    - mTabCarousel.getAllowedVerticalScrollLength());
-            mTabCarousel.setY(amtToScroll);
-        }
-
-        @Override
-        public void onScrollStateChanged(AbsListView view, int scrollState) {}
-
-    };
-
     /**
      * This interface should be implemented by {@link Fragment}s within this
      * activity so that the activity can determine whether the currently
diff --git a/src/com/android/contacts/detail/ContactDetailLayoutController.java b/src/com/android/contacts/detail/ContactDetailLayoutController.java
index 7abe76e..f3cc016 100644
--- a/src/com/android/contacts/detail/ContactDetailLayoutController.java
+++ b/src/com/android/contacts/detail/ContactDetailLayoutController.java
@@ -29,8 +29,6 @@
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
-import android.widget.AbsListView;
-import android.widget.AbsListView.OnScrollListener;
 
 /**
  * Determines the layout of the contact card.
@@ -115,8 +113,6 @@
         }
 
         mDetailFragment.setListener(mContactDetailFragmentListener);
-        mDetailFragment.setVerticalScrollListener(mVerticalScrollListener);
-        mUpdatesFragment.setVerticalScrollListener(mVerticalScrollListener);
 
         switch (mLayoutMode) {
             case VIEW_PAGER_AND_CAROUSEL: {
@@ -151,6 +147,7 @@
                 }
                 transaction.commit();
                 mFragmentManager.executePendingTransactions();
+                TabCarouselScrollManager.bind(mTabCarousel, mDetailFragment, mUpdatesFragment);
                 break;
             }
             case TWO_COLUMN: {
@@ -306,32 +303,4 @@
             mViewPager.setCurrentItem(position);
         }
     };
-
-    private OnScrollListener mVerticalScrollListener = new OnScrollListener() {
-
-        @Override
-        public void onScroll(
-                AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
-            if (mTabCarousel == null) {
-                return;
-            }
-            // If the FIRST item is not visible on the screen, then the carousel must be pinned
-            // at the top of the screen.
-            if (firstVisibleItem != 0) {
-                mTabCarousel.setY(-mTabCarousel.getAllowedVerticalScrollLength());
-                return;
-            }
-            View topView = view.getChildAt(firstVisibleItem);
-            if (topView == null) {
-                return;
-            }
-            int amtToScroll = Math.max((int) view.getChildAt(firstVisibleItem).getY(),
-                    -mTabCarousel.getAllowedVerticalScrollLength());
-            mTabCarousel.setY(amtToScroll);
-        }
-
-        @Override
-        public void onScrollStateChanged(AbsListView view, int scrollState) {}
-
-    };
 }
diff --git a/src/com/android/contacts/detail/TabCarouselScrollManager.java b/src/com/android/contacts/detail/TabCarouselScrollManager.java
new file mode 100644
index 0000000..2415b50
--- /dev/null
+++ b/src/com/android/contacts/detail/TabCarouselScrollManager.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2011 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.detail;
+
+import android.view.View;
+import android.widget.AbsListView;
+import android.widget.AbsListView.OnScrollListener;
+
+/**
+ * Takes care of managing scrolling for the side-by-side views using the tab carousel.
+ */
+public class TabCarouselScrollManager {
+    private final ContactDetailTabCarousel mTabCarousel;
+    private final ContactDetailFragment mAboutFragment;
+    private final ContactDetailUpdatesFragment mUpdatesFragment;
+    private final OnScrollListener mVerticalScrollListener = new OnScrollListener() {
+        @Override
+        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
+                int totalItemCount) {
+            if (mTabCarousel == null) {
+                return;
+            }
+            // If the FIRST item is not visible on the screen, then the carousel must be pinned
+            // at the top of the screen.
+            if (firstVisibleItem != 0) {
+                mTabCarousel.setY(-mTabCarousel.getAllowedVerticalScrollLength());
+                return;
+            }
+            View topView = view.getChildAt(firstVisibleItem);
+            if (topView == null) {
+                return;
+            }
+            int amtToScroll = Math.max((int) view.getChildAt(firstVisibleItem).getY(),
+                    -mTabCarousel.getAllowedVerticalScrollLength());
+            mTabCarousel.setY(amtToScroll);
+        }
+
+        @Override
+        public void onScrollStateChanged(AbsListView view, int scrollState) {}
+    };
+
+    private TabCarouselScrollManager(ContactDetailTabCarousel tabCarousel,
+            ContactDetailFragment aboutFragment, ContactDetailUpdatesFragment updatesFragment) {
+        mTabCarousel = tabCarousel;
+        mAboutFragment = aboutFragment;
+        mUpdatesFragment = updatesFragment;
+    }
+
+    public void bind() {
+        mAboutFragment.setVerticalScrollListener(mVerticalScrollListener);
+        mUpdatesFragment.setVerticalScrollListener(mVerticalScrollListener);
+    }
+
+    public static void bind(ContactDetailTabCarousel tabCarousel,
+            ContactDetailFragment aboutFragment, ContactDetailUpdatesFragment updatesFragment) {
+        TabCarouselScrollManager scrollManager =
+                new TabCarouselScrollManager(tabCarousel, aboutFragment, updatesFragment);
+        scrollManager.bind();
+    }
+}