Merge "Switch the status bar color earlier in the all apps transition" into ub-launcher3-dorval-polish
diff --git a/res/layout-land/launcher.xml b/res/layout-land/launcher.xml
index dab0743..ac440fc 100644
--- a/res/layout-land/launcher.xml
+++ b/res/layout-land/launcher.xml
@@ -42,12 +42,7 @@
             android:layout_gravity="center"
             launcher:pageIndicator="@id/page_indicator" />
 
-        <com.android.launcher3.graphics.GradientView
-            android:id="@+id/gradient_bg"
-            android:layout_width="match_parent"
-            android:layout_height="match_parent"
-            android:visibility="gone"
-            launcher:layout_ignoreInsets="true"/>
+        <include layout="@layout/gradient_bg" />
 
         <!-- DO NOT CHANGE THE ID -->
         <include layout="@layout/hotseat"
diff --git a/res/layout-port/launcher.xml b/res/layout-port/launcher.xml
index bace51a..c41a6e3 100644
--- a/res/layout-port/launcher.xml
+++ b/res/layout-port/launcher.xml
@@ -44,12 +44,7 @@
             launcher:pageIndicator="@+id/page_indicator">
         </com.android.launcher3.Workspace>
 
-        <com.android.launcher3.graphics.GradientView
-            android:id="@+id/gradient_bg"
-            android:layout_width="match_parent"
-            android:layout_height="match_parent"
-            android:visibility="gone"
-            launcher:layout_ignoreInsets="true"/>
+        <include layout="@layout/gradient_bg" />
 
         <!-- DO NOT CHANGE THE ID -->
         <include layout="@layout/hotseat"
diff --git a/res/layout-sw720dp/launcher.xml b/res/layout-sw720dp/launcher.xml
index 18d235c..03e42bc 100644
--- a/res/layout-sw720dp/launcher.xml
+++ b/res/layout-sw720dp/launcher.xml
@@ -43,12 +43,7 @@
             launcher:pageIndicator="@id/page_indicator">
         </com.android.launcher3.Workspace>
 
-        <com.android.launcher3.graphics.GradientView
-            android:id="@+id/gradient_bg"
-            android:layout_width="match_parent"
-            android:layout_height="match_parent"
-            android:visibility="gone"
-            launcher:layout_ignoreInsets="true"/>
+        <include layout="@layout/gradient_bg" />
 
         <!-- DO NOT CHANGE THE ID -->
         <include layout="@layout/hotseat"
diff --git a/res/layout/gradient_bg.xml b/res/layout/gradient_bg.xml
new file mode 100644
index 0000000..db448d7
--- /dev/null
+++ b/res/layout/gradient_bg.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2017 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.
+-->
+
+<com.android.launcher3.graphics.GradientView
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:launcher="http://schemas.android.com/apk/res-auto"
+    android:id="@+id/gradient_bg"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:visibility="gone"
+    launcher:layout_ignoreInsets="true" />
\ No newline at end of file
diff --git a/src/com/android/launcher3/allapps/AllAppsRecyclerView.java b/src/com/android/launcher3/allapps/AllAppsRecyclerView.java
index 701402b..657af15 100644
--- a/src/com/android/launcher3/allapps/AllAppsRecyclerView.java
+++ b/src/com/android/launcher3/allapps/AllAppsRecyclerView.java
@@ -110,12 +110,6 @@
     }
 
     @Override
-    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent ev) {
-        mPullDetector.onTouchEvent(ev);
-        return super.onInterceptTouchEvent(rv, ev) || mOverScrollHelper.isInOverScroll();
-    }
-
-    @Override
     public boolean onTouchEvent(MotionEvent e) {
         mPullDetector.onTouchEvent(e);
         if (FeatureFlags.LAUNCHER3_PHYSICS && mSpringAnimationHandler != null) {
@@ -281,7 +275,8 @@
 
     @Override
     public boolean onInterceptTouchEvent(MotionEvent e) {
-        boolean result = super.onInterceptTouchEvent(e);
+        mPullDetector.onTouchEvent(e);
+        boolean result = super.onInterceptTouchEvent(e) || mOverScrollHelper.isInOverScroll();
         if (!result && e.getAction() == MotionEvent.ACTION_DOWN
                 && mEmptySearchBackground != null && mEmptySearchBackground.getAlpha() > 0) {
             mEmptySearchBackground.setHotspot(e.getX(), e.getY());
@@ -540,11 +535,16 @@
             // and if one of the following criteria are met:
             // - User scrolls down when they're already at the bottom.
             // - User starts scrolling up, hits the top, and continues scrolling up.
+            boolean wasInOverScroll = mIsInOverScroll;
             mIsInOverScroll = !mScrollbar.isDraggingThumb() &&
                     ((!canScrollVertically(1) && displacement < 0) ||
                     (!canScrollVertically(-1) && isScrollingUp && mFirstScrollYOnScrollUp != 0));
 
-            if (mIsInOverScroll) {
+            if (wasInOverScroll && !mIsInOverScroll) {
+                // Exit overscroll. This can happen when the user is in overscroll and then
+                // scrolls the opposite way.
+                reset(false /* shouldSpring */);
+            } else if (mIsInOverScroll) {
                 if (Float.compare(mFirstDisplacement, 0) == 0) {
                     // Because users can scroll before entering overscroll, we need to
                     // subtract the amount where the user was not in overscroll.
@@ -559,11 +559,15 @@
 
         @Override
         public void onDragEnd(float velocity, boolean fling) {
+           reset(mIsInOverScroll  /* shouldSpring */);
+        }
+
+        private void reset(boolean shouldSpring) {
             float y = getContentTranslationY();
             if (Float.compare(y, 0) != 0) {
-                if (FeatureFlags.LAUNCHER3_PHYSICS) {
+                if (FeatureFlags.LAUNCHER3_PHYSICS && shouldSpring) {
                     // We calculate our own velocity to give the springs the desired effect.
-                    velocity = y / getDampedOverScroll(getHeight()) * MAX_RELEASE_VELOCITY;
+                    float velocity = y / getDampedOverScroll(getHeight()) * MAX_RELEASE_VELOCITY;
                     // We want to negate the velocity because we are moving to 0 from -1 due to the
                     // downward motion. (y-axis -1 is above 0).
                     mSpringAnimationHandler.animateToPositionWithVelocity(0, -1, -velocity);
diff --git a/src/com/android/launcher3/widget/WidgetsBottomSheet.java b/src/com/android/launcher3/widget/WidgetsBottomSheet.java
index b2fb091..717a61c 100644
--- a/src/com/android/launcher3/widget/WidgetsBottomSheet.java
+++ b/src/com/android/launcher3/widget/WidgetsBottomSheet.java
@@ -89,7 +89,8 @@
         mInsets = new Rect();
         mVerticalPullDetector = new VerticalPullDetector(context);
         mVerticalPullDetector.setListener(this);
-        mGradientBackground = (GradientView) mLauncher.findViewById(R.id.gradient_bg);
+        mGradientBackground = (GradientView) mLauncher.getLayoutInflater().inflate(
+                R.layout.gradient_bg, mLauncher.getDragLayer(), false);
     }
 
     @Override
@@ -107,6 +108,8 @@
 
         onWidgetsBound();
 
+        mLauncher.getDragLayer().addView(mGradientBackground);
+        mGradientBackground.setVisibility(VISIBLE);
         mLauncher.getDragLayer().addView(this);
         measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
         setTranslationY(mTranslationYClosed);
@@ -213,11 +216,8 @@
             mOpenCloseAnimator.addListener(new AnimatorListenerAdapter() {
                 @Override
                 public void onAnimationEnd(Animator animation) {
-                    mIsOpen = false;
                     mVerticalPullDetector.finishedScrolling();
-                    ((ViewGroup) getParent()).removeView(WidgetsBottomSheet.this);
-                    mLauncher.getSystemUiController().updateUiState(
-                            SystemUiController.UI_STATE_WIDGET_BOTTOM_SHEET, 0);
+                    onCloseComplete();
                 }
             });
             mOpenCloseAnimator.setInterpolator(mVerticalPullDetector.isIdleState()
@@ -225,12 +225,18 @@
             mOpenCloseAnimator.start();
         } else {
             setTranslationY(mTranslationYClosed);
-            mLauncher.getSystemUiController().updateUiState(
-                    SystemUiController.UI_STATE_WIDGET_BOTTOM_SHEET, 0);
-            mIsOpen = false;
+            onCloseComplete();
         }
     }
 
+    private void onCloseComplete() {
+        mIsOpen = false;
+        mLauncher.getDragLayer().removeView(mGradientBackground);
+        mLauncher.getDragLayer().removeView(WidgetsBottomSheet.this);
+        mLauncher.getSystemUiController().updateUiState(
+                SystemUiController.UI_STATE_WIDGET_BOTTOM_SHEET, 0);
+    }
+
     @Override
     protected boolean isOfType(@FloatingViewType int type) {
         return (type & TYPE_WIDGETS_BOTTOM_SHEET) != 0;