Removing dependency on LauncherCallbacks for getting the search bounds

> Instead of handling insets in BaseContainerView, directly applying
  them to margins, as that scpace can't be used for scroll handling
> Appliying the top and bottom padding in xml
> The left & right padding is defied in xml which is reused for scroll
  handling

Bug: 27108154
Change-Id: Ia32e6d5e8fd1bfafb1d77d1244ce2268e4da9df9
diff --git a/res/layout/all_apps.xml b/res/layout/all_apps.xml
index faa88f7..1b843ed 100644
--- a/res/layout/all_apps.xml
+++ b/res/layout/all_apps.xml
@@ -22,6 +22,8 @@
     android:id="@+id/apps_view"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
+    android:paddingTop="@dimen/container_bounds_inset"
+    android:paddingBottom="@dimen/container_bounds_inset"
     android:orientation="vertical"
     launcher:revealBackground="@drawable/quantum_panel_shape">
 
diff --git a/res/layout/widgets_view.xml b/res/layout/widgets_view.xml
index 410d1be..e55f6f0 100644
--- a/res/layout/widgets_view.xml
+++ b/res/layout/widgets_view.xml
@@ -22,6 +22,8 @@
     android:id="@+id/widgets_view"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
+    android:paddingTop="@dimen/container_bounds_inset"
+    android:paddingBottom="@dimen/container_bounds_inset"
     android:descendantFocusability="afterDescendants"
     launcher:revealBackground="@drawable/quantum_panel_shape_dark">
 
diff --git a/res/values-land/dimens.xml b/res/values-land/dimens.xml
index 06a9984..2be9391 100644
--- a/res/values-land/dimens.xml
+++ b/res/values-land/dimens.xml
@@ -18,4 +18,7 @@
 <!-- QSB -->
     <dimen name="toolbar_button_vertical_padding">8dip</dimen>
     <dimen name="toolbar_button_horizontal_padding">0dip</dimen>
+
+<!-- Container -->
+     <item name="container_margin" format="fraction" type="fraction">12%</item>
 </resources>
diff --git a/res/values-sw600dp/dimens.xml b/res/values-sw600dp/dimens.xml
index 8e1e4f1..e5f2d82 100644
--- a/res/values-sw600dp/dimens.xml
+++ b/res/values-sw600dp/dimens.xml
@@ -15,6 +15,9 @@
 -->
 
 <resources>
+<!-- Container -->
+    <dimen name="container_min_margin">16dp</dimen>
+
 <!-- All Apps -->
     <dimen name="all_apps_grid_view_start_margin">0dp</dimen>
     <dimen name="all_apps_grid_section_text_size">26sp</dimen>
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
index b06afc4..a70d3b8 100644
--- a/res/values/dimens.xml
+++ b/res/values/dimens.xml
@@ -67,6 +67,9 @@
     <dimen name="container_fastscroll_popup_size">72dp</dimen>
     <dimen name="container_fastscroll_popup_text_size">48dp</dimen>
 
+    <item name="container_margin" format="fraction" type="fraction">0%</item>
+    <dimen name="container_min_margin">8dp</dimen>
+
 <!-- All Apps -->
     <dimen name="all_apps_button_scale_down">0dp</dimen>
     <dimen name="all_apps_grid_view_start_margin">0dp</dimen>
diff --git a/src/com/android/launcher3/BaseContainerView.java b/src/com/android/launcher3/BaseContainerView.java
index bd41992..51a97b9 100644
--- a/src/com/android/launcher3/BaseContainerView.java
+++ b/src/com/android/launcher3/BaseContainerView.java
@@ -31,19 +31,9 @@
 /**
  * A base container view, which supports resizing.
  */
-public abstract class BaseContainerView extends FrameLayout implements Insettable {
+public abstract class BaseContainerView extends FrameLayout {
 
-    private final static String TAG = "BaseContainerView";
-
-    // The window insets
-    private final Rect mInsets = new Rect();
-    // The bounds of the search bar.  Only the left, top, right are used to inset the
-    // search bar and the height is determined by the measurement of the layout
-    private final Rect mFixedSearchBarBounds = new Rect();
-    // The computed padding to apply to the container to achieve the container bounds
-    protected final Rect mContentPadding = new Rect();
-    // The inset to apply to the edges and between the search bar and the container
-    private final int mContainerBoundsInset;
+    protected final int mHorizontalPadding;
 
     private final Drawable mRevealDrawable;
 
@@ -60,11 +50,17 @@
 
     public BaseContainerView(Context context, AttributeSet attrs, int defStyleAttr) {
         super(context, attrs, defStyleAttr);
-        mContainerBoundsInset = getResources().getDimensionPixelSize(R.dimen.container_bounds_inset);
+
+        int width = ((Launcher) context).getDeviceProfile().availableWidthPx;
+        mHorizontalPadding = Math.max(
+                getResources().getDimensionPixelSize(R.dimen.container_min_margin),
+                (int) getResources().getFraction(R.fraction.container_margin, width, 1));
 
         TypedArray a = context.obtainStyledAttributes(attrs,
                 R.styleable.BaseContainerView, defStyleAttr, 0);
-        mRevealDrawable = a.getDrawable(R.styleable.BaseContainerView_revealBackground);
+        mRevealDrawable = new InsetDrawable(
+                a.getDrawable(R.styleable.BaseContainerView_revealBackground),
+                mHorizontalPadding, 0, mHorizontalPadding, 0);
         a.recycle();
     }
 
@@ -74,90 +70,13 @@
 
         mContent = findViewById(R.id.main_content);
         mRevealView = findViewById(R.id.reveal_view);
-    }
 
-    @Override
-    final public void setInsets(Rect insets) {
-        mInsets.set(insets);
-        updateBackgroundAndPaddings();
-    }
-
-    /**
-     * Sets the search bar bounds for this container view to match.
-     */
-    final public void setSearchBarBounds(Rect bounds) {
-        if (ProviderConfig.IS_DOGFOOD_BUILD && !isValidSearchBarBounds(bounds)) {
-            Log.e(TAG, "Invalid search bar bounds: " + bounds);
-        }
-
-        mFixedSearchBarBounds.set(bounds);
-
-        // Post the updates since they can trigger a relayout, and this call can be triggered from
-        // a layout pass itself.
-        post(new Runnable() {
-            @Override
-            public void run() {
-                updateBackgroundAndPaddings();
-            }
-        });
-    }
-
-    /**
-     * Update the backgrounds and padding in response to a change in the bounds or insets.
-     */
-    protected void updateBackgroundAndPaddings() {
-        Rect padding;
-        if (isValidSearchBarBounds(mFixedSearchBarBounds)) {
-            padding = new Rect(
-                    mFixedSearchBarBounds.left,
-                    mInsets.top + mContainerBoundsInset,
-                    getMeasuredWidth() - mFixedSearchBarBounds.right,
-                    mInsets.bottom + mContainerBoundsInset
-            );
-        } else {
-            padding = new Rect(
-                    mInsets.left + mContainerBoundsInset,
-                    mInsets.top + mContainerBoundsInset,
-                    mInsets.right + mContainerBoundsInset,
-                    mInsets.bottom + mContainerBoundsInset
-            );
-        }
-
-        // The container padding changed, notify the container.
-        if (!padding.equals(mContentPadding)) {
-            mContentPadding.set(padding);
-            onUpdateBackgroundAndPaddings(padding);
-        }
-    }
-
-    private void onUpdateBackgroundAndPaddings(Rect padding) {
-        // Apply the top-bottom padding to itself so that the launcher transition is
-        // clipped correctly
-        setPadding(0, padding.top, 0, padding.bottom);
-
-        InsetDrawable background = new InsetDrawable(mRevealDrawable,
-                padding.left, 0, padding.right, 0);
-        mRevealView.setBackground(background.getConstantState().newDrawable());
-        mContent.setBackground(background);
+        mRevealView.setBackground(mRevealDrawable.getConstantState().newDrawable());
+        mContent.setBackground(mRevealDrawable);
 
         // We let the content have a intent background, but still have full width.
         // This allows the scroll bar to be used responsive outside the background bounds as well.
         mContent.setPadding(0, 0, 0, 0);
-
-        Rect bgPadding = new Rect();
-        background.getPadding(bgPadding);
-        onUpdateBgPadding(padding, bgPadding);
-    }
-
-    protected abstract void onUpdateBgPadding(Rect padding, Rect bgPadding);
-
-    /**
-     * Returns whether the search bar bounds we got are considered valid.
-     */
-    private boolean isValidSearchBarBounds(Rect searchBarBounds) {
-        return !searchBarBounds.isEmpty() &&
-                searchBarBounds.right <= getMeasuredWidth() &&
-                searchBarBounds.bottom <= getMeasuredHeight();
     }
 
     public final View getContentView() {
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index 245b399..5a1f99a 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -583,14 +583,6 @@
         }
     }
 
-    /**
-     * Updates the bounds of all the overlays to match the new fixed bounds.
-     */
-    public void updateOverlayBounds(Rect newBounds) {
-        mAppsView.setSearchBarBounds(newBounds);
-        mWidgetsView.setSearchBarBounds(newBounds);
-    }
-
     /** To be overridden by subclasses to hint to Launcher that we have custom content */
     protected boolean hasCustomContentToLeft() {
         if (mLauncherCallbacks != null) {
diff --git a/src/com/android/launcher3/allapps/AllAppsContainerView.java b/src/com/android/launcher3/allapps/AllAppsContainerView.java
index 9d5afb4..fa34d75 100644
--- a/src/com/android/launcher3/allapps/AllAppsContainerView.java
+++ b/src/com/android/launcher3/allapps/AllAppsContainerView.java
@@ -230,8 +230,6 @@
         mSearchBarController = searchController;
         mSearchBarController.initialize(mApps, mSearchInput, mLauncher, this);
         mAdapter.setSearchController(mSearchBarController);
-
-        updateBackgroundAndPaddings();
     }
 
     /**
@@ -311,19 +309,17 @@
         mAppsRecyclerView.setPremeasuredIconHeights(predIcon.getMeasuredHeight(),
                 icon.getMeasuredHeight());
 
-        updateBackgroundAndPaddings();
+        updatePaddingsAndMargins();
     }
 
     @Override
-    public void onBoundsChanged(Rect newBounds) {
-        mLauncher.updateOverlayBounds(newBounds);
-    }
+    public void onBoundsChanged(Rect newBounds) { }
 
     @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
-        mContentBounds.set(mContentPadding.left, mContentPadding.top,
-                MeasureSpec.getSize(widthMeasureSpec) - mContentPadding.right,
-                MeasureSpec.getSize(heightMeasureSpec) - mContentPadding.bottom);
+        mContentBounds.set(mHorizontalPadding, 0,
+                MeasureSpec.getSize(widthMeasureSpec) - mHorizontalPadding,
+                MeasureSpec.getSize(heightMeasureSpec));
 
         // Update the number of items in the grid before we measure the view
         // TODO: mSectionNamesMargin is currently 0, but also account for it,
@@ -365,8 +361,10 @@
      * container view, we inset the background and padding of the recycler view to allow for the
      * recycler view to handle touch events (for fast scrolling) all the way to the edge.
      */
-    @Override
-    protected void onUpdateBgPadding(Rect padding, Rect bgPadding) {
+    private void updatePaddingsAndMargins() {
+        Rect bgPadding = new Rect();
+        getRevealView().getBackground().getPadding(bgPadding);
+
         mAppsRecyclerView.updateBackgroundPadding(bgPadding);
         mAdapter.updateBackgroundPadding(bgPadding);
         mElevationController.updateBackgroundPadding(bgPadding);
@@ -377,16 +375,16 @@
         int startInset = Math.max(mSectionNamesMargin, maxScrollBarWidth);
         int topBottomPadding = mRecyclerViewTopBottomPadding;
         if (Utilities.isRtl(getResources())) {
-            mAppsRecyclerView.setPadding(padding.left + maxScrollBarWidth,
-                    topBottomPadding, padding.right + startInset, topBottomPadding);
+            mAppsRecyclerView.setPadding(bgPadding.left + maxScrollBarWidth,
+                    topBottomPadding, bgPadding.right + startInset, topBottomPadding);
         } else {
-            mAppsRecyclerView.setPadding(padding.left + startInset, topBottomPadding,
-                    padding.right + maxScrollBarWidth, topBottomPadding);
+            mAppsRecyclerView.setPadding(bgPadding.left + startInset, topBottomPadding,
+                    bgPadding.right + maxScrollBarWidth, topBottomPadding);
         }
 
         MarginLayoutParams lp = (MarginLayoutParams) mSearchContainer.getLayoutParams();
-        lp.leftMargin = padding.left;
-        lp.rightMargin = padding.right;
+        lp.leftMargin = bgPadding.left;
+        lp.rightMargin = bgPadding.right;
         mSearchContainer.setLayoutParams(lp);
     }
 
diff --git a/src/com/android/launcher3/allapps/AllAppsSearchBarController.java b/src/com/android/launcher3/allapps/AllAppsSearchBarController.java
index 39d6dd5..a4bea8d 100644
--- a/src/com/android/launcher3/allapps/AllAppsSearchBarController.java
+++ b/src/com/android/launcher3/allapps/AllAppsSearchBarController.java
@@ -175,6 +175,7 @@
         /**
          * Called when the bounds of the search bar has changed.
          */
+        @Deprecated
         void onBoundsChanged(Rect newBounds);
 
         /**
diff --git a/src/com/android/launcher3/widget/WidgetsContainerView.java b/src/com/android/launcher3/widget/WidgetsContainerView.java
index 5d6f475..23d0433 100644
--- a/src/com/android/launcher3/widget/WidgetsContainerView.java
+++ b/src/com/android/launcher3/widget/WidgetsContainerView.java
@@ -96,6 +96,18 @@
         mRecyclerView = (WidgetsRecyclerView) getContentView().findViewById(R.id.widgets_list_view);
         mRecyclerView.setAdapter(mAdapter);
         mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
+
+        Rect bgPadding = new Rect();
+        getRevealView().getBackground().getPadding(bgPadding);
+        if (Utilities.isRtl(getResources())) {
+            getContentView().setPadding(0, bgPadding.top,
+                    bgPadding.right, bgPadding.bottom);
+            mRecyclerView.updateBackgroundPadding(new Rect(bgPadding.left, 0, 0, 0));
+        } else {
+            getContentView().setPadding(bgPadding.left, bgPadding.top,
+                    0, bgPadding.bottom);
+            mRecyclerView.updateBackgroundPadding(new Rect(0, 0, bgPadding.right, 0));
+        }
     }
 
     //
@@ -310,22 +322,6 @@
         }
     }
 
-    //
-    // Container rendering related.
-    //
-    @Override
-    protected void onUpdateBgPadding(Rect padding, Rect bgPadding) {
-        if (Utilities.isRtl(getResources())) {
-            getContentView().setPadding(0, bgPadding.top,
-                    bgPadding.right, bgPadding.bottom);
-            mRecyclerView.updateBackgroundPadding(new Rect(bgPadding.left, 0, 0, 0));
-        } else {
-            getContentView().setPadding(bgPadding.left, bgPadding.top,
-                    0, bgPadding.bottom);
-            mRecyclerView.updateBackgroundPadding(new Rect(0, 0, bgPadding.right, 0));
-        }
-    }
-
     /**
      * Initialize the widget data model.
      */