Adding contract for custom content screen

Change-Id: Ia4dae3c641730ab24a0a124fd94b871d248b7dda
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index 7d9ebc0..3592045 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -838,9 +838,44 @@
     protected void onFinishBindingItems() {
     }
 
+    QSBScroller mQsbScroller = new QSBScroller() {
+        int scrollY = 0;
+
+        @Override
+        public void setScrollY(int scroll) {
+            scrollY = scroll;
+
+            if (mWorkspace.isOnOrMovingToCustomContent()) {
+                mSearchDropTargetBar.setTranslationY(- scrollY);
+            }
+        }
+    };
+
+    public void resetQSBScroll() {
+        mSearchDropTargetBar.animate().translationY(0).start();
+    }
+
+    public interface CustomContentCallbacks {
+        // Custom content is completely shown
+        public void onShow();
+
+        // Custom content is completely hidden
+        public void onHide();
+    }
+
+    public interface QSBScroller {
+        public void setScrollY(int scrollY);
+    }
+
     // Add a fullscreen unpadded view to the workspace to the left all other screens.
-    public void addCustomContentToLeft(View customContent) {
-        mWorkspace.addCustomContentToLeft(customContent);
+    public QSBScroller addCustomContentToLeft(View customContent) {
+        return addCustomContentToLeft(customContent, null);
+    }
+
+    public QSBScroller addCustomContentToLeft(View customContent,
+            CustomContentCallbacks callbacks) {
+        mWorkspace.addCustomContentToLeft(customContent, callbacks);
+        return mQsbScroller;
     }
 
     // The custom content needs to offset its content to account for the QSB
diff --git a/src/com/android/launcher3/Workspace.java b/src/com/android/launcher3/Workspace.java
index b8ef6b1..edf3721 100644
--- a/src/com/android/launcher3/Workspace.java
+++ b/src/com/android/launcher3/Workspace.java
@@ -56,6 +56,7 @@
 import android.widget.TextView;
 
 import com.android.launcher3.FolderIcon.FolderRingAnimator;
+import com.android.launcher3.Launcher.CustomContentCallbacks;
 import com.android.launcher3.LauncherSettings.Favorites;
 
 import java.net.URISyntaxException;
@@ -98,8 +99,6 @@
     boolean mDrawBackground = true;
     private float mBackgroundAlpha = 0;
 
-    private float mWallpaperScrollRatio = 1.0f;
-
     private LayoutTransition mLayoutTransition;
     private final WallpaperManager mWallpaperManager;
     private IBinder mWindowToken;
@@ -129,6 +128,9 @@
     static Rect mLandscapeCellLayoutMetrics = null;
     static Rect mPortraitCellLayoutMetrics = null;
 
+    CustomContentCallbacks mCustomContentCallbacks;
+    boolean mCustomContentShowing;
+
     /**
      * The CellLayout that is currently being dragged over
      */
@@ -526,7 +528,7 @@
         return screenId;
     }
 
-    public void addCustomContentToLeft(View customContent) {
+    public void addCustomContentToLeft(View customContent, CustomContentCallbacks callbacks) {
         CellLayout customScreen = (CellLayout)
                 mLauncher.getLayoutInflater().inflate(R.layout.workspace_screen, null);
 
@@ -541,17 +543,13 @@
         Rect p = new Rect();
         AppWidgetHostView.getDefaultPaddingForWidget(mLauncher, mLauncher.getComponentName(), p);
 
-        // For now we force top padding on the entire custom content screen. The intention
-        // is for the hosted content to get this offset and account for it so that upon scrolling
-        // it can use the entire space.
-        customContent.setPadding(p.left, mLauncher.getTopOffsetForCustomContent(),
-                p.right, p.bottom);
-
         mWorkspaceScreens.put(CUSTOM_CONTENT_SCREEN_ID, customScreen);
         mScreenOrder.add(0, CUSTOM_CONTENT_SCREEN_ID);
 
         addFullScreenPage(customScreen);
 
+        mCustomContentCallbacks = callbacks;
+
         // Ensure that the current page and default page are maintained.
         mDefaultPage++;
         setCurrentPage(getCurrentPage() + 1);
@@ -937,6 +935,19 @@
     protected void notifyPageSwitchListener() {
         super.notifyPageSwitchListener();
         Launcher.setScreen(mCurrentPage);
+
+        if (hasCustomContent() && mCurrentPage == 0) {
+            mCustomContentShowing = true;
+            if (mCustomContentCallbacks != null) {
+                mCustomContentCallbacks.onShow();
+            }
+        } else if (hasCustomContent() && mCustomContentShowing) {
+            mCustomContentShowing = false;
+            if (mCustomContentCallbacks != null) {
+                mCustomContentCallbacks.onHide();
+                mLauncher.resetQSBScroll();
+            }
+        }
     };
 
     // As a ratio of screen height, the total distance we want the parallax effect to span
@@ -966,11 +977,6 @@
         return x * aspectRatio + y;
     }
 
-    // The range of scroll values for Workspace
-    private int getScrollRange() {
-        return getChildOffset(getChildCount() - 1) - getChildOffset(0);
-    }
-
     protected void setWallpaperDimension() {
         Point minDims = new Point();
         Point maxDims = new Point();
@@ -995,33 +1001,6 @@
         }.start();
     }
 
-    private float wallpaperOffsetForCurrentScroll() {
-        // Set wallpaper offset steps (1 / (number of screens - 1))
-        mWallpaperManager.setWallpaperOffsetSteps(1.0f / (getChildCount() - 1), 1.0f);
-
-        int scrollRange = getScrollRange();
-
-        float adjustedScrollX = Math.max(0, Math.min(getScrollX(), mMaxScrollX));
-        adjustedScrollX *= mWallpaperScrollRatio;
-
-        float scrollProgress =
-            adjustedScrollX / (float) scrollRange;
-
-        if (LauncherAppState.getInstance().isScreenLarge() && mIsStaticWallpaper) {
-            // The wallpaper travel width is how far, from left to right, the wallpaper will move
-            // at this orientation. On tablets in portrait mode we don't move all the way to the
-            // edges of the wallpaper, or otherwise the parallax effect would be too strong.
-            int wallpaperTravelWidth = Math.min(mWallpaperTravelWidth, mWallpaperWidth);
-
-            float offsetInDips = wallpaperTravelWidth * scrollProgress +
-                (mWallpaperWidth - wallpaperTravelWidth) / 2; // center it
-            float offset = offsetInDips / (float) mWallpaperWidth;
-            return offset;
-        } else {
-            return scrollProgress;
-        }
-    }
-
     private void syncWallpaperOffsetWithScroll() {
         final boolean enableWallpaperEffects = isHardwareAccelerated();
         if (enableWallpaperEffects) {
@@ -1317,6 +1296,10 @@
         return (mScreenOrder.size() > 0 && mScreenOrder.get(0) == CUSTOM_CONTENT_SCREEN_ID);
     }
 
+    public boolean isOnOrMovingToCustomContent() {
+        return hasCustomContent() && getNextPage() == 0;
+    }
+
     private void updateStateForCustomContent(int screenCenter) {
         if (hasCustomContent()) {
             CellLayout customContent = getScreenWithId(CUSTOM_CONTENT_SCREEN_ID);