Allow LauncherOverlay to access and manage insets

Change-Id: Ib9faf37eb22ad2a0b18c076978ec9f2fd8864c0c
diff --git a/res/layout-land/launcher.xml b/res/layout-land/launcher.xml
index fa5fe73..0858d6a 100644
--- a/res/layout-land/launcher.xml
+++ b/res/layout-land/launcher.xml
@@ -15,20 +15,20 @@
 -->
 
 <!-- Full screen view projects under the status bar and contains the background -->
-<FrameLayout
+<com.android.launcher3.LauncherRootView
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:launcher="http://schemas.android.com/apk/res-auto/com.android.launcher3"
 
     android:id="@+id/launcher"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
-    android:background="@drawable/workspace_bg">
+    android:background="@drawable/workspace_bg"
+    android:fitsSystemWindows="true">
 
     <com.android.launcher3.DragLayer
         android:id="@+id/drag_layer"
         android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:fitsSystemWindows="true">
+        android:layout_height="match_parent">
 
         <com.android.launcher3.FocusIndicatorView
             android:id="@+id/focus_indicator"
@@ -70,4 +70,4 @@
         android:layout_height="match_parent"
         android:inflatedId="@+id/launcher_overlay"
         android:layout="@layout/launcher_overlay" />
-</FrameLayout>
+</com.android.launcher3.LauncherRootView>
diff --git a/res/layout-port/launcher.xml b/res/layout-port/launcher.xml
index f2d2dd8..7ba7a89 100644
--- a/res/layout-port/launcher.xml
+++ b/res/layout-port/launcher.xml
@@ -15,14 +15,15 @@
 -->
 
 <!-- Full screen view projects under the status bar and contains the background -->
-<FrameLayout
+<com.android.launcher3.LauncherRootView
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:launcher="http://schemas.android.com/apk/res-auto/com.android.launcher3"
 
     android:id="@+id/launcher"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
-    android:background="@drawable/workspace_bg">
+    android:background="@drawable/workspace_bg"
+    android:fitsSystemWindows="true">
 
     <com.android.launcher3.DragLayer
         android:id="@+id/drag_layer"
@@ -90,4 +91,4 @@
         android:layout_height="match_parent"
         android:inflatedId="@+id/launcher_overlay"
         android:layout="@layout/launcher_overlay" />
-</FrameLayout>
+</com.android.launcher3.LauncherRootView>
diff --git a/res/layout-sw720dp/launcher.xml b/res/layout-sw720dp/launcher.xml
index 87fa2ed..d4fa2fa 100644
--- a/res/layout-sw720dp/launcher.xml
+++ b/res/layout-sw720dp/launcher.xml
@@ -15,20 +15,20 @@
 -->
 
 <!-- Full screen view projects under the status bar and contains the background -->
-<FrameLayout
+<com.android.launcher3.LauncherRootView
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:launcher="http://schemas.android.com/apk/res-auto/com.android.launcher3"
 
     android:id="@+id/launcher"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
-    android:background="@drawable/workspace_bg">
+    android:background="@drawable/workspace_bg"
+    android:fitsSystemWindows="true">
 
     <com.android.launcher3.DragLayer
         android:id="@+id/drag_layer"
         android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:fitsSystemWindows="true">
+        android:layout_height="match_parent">
 
         <com.android.launcher3.FocusIndicatorView
             android:id="@+id/focus_indicator"
@@ -90,4 +90,4 @@
         android:inflatedId="@+id/launcher_overlay"
         android:layout="@layout/launcher_overlay" />
 
-</FrameLayout>
+</com.android.launcher3.LauncherRootView>
diff --git a/res/layout/launcher_overlay.xml b/res/layout/launcher_overlay.xml
index 9ef0c9a..b35a2d8 100644
--- a/res/layout/launcher_overlay.xml
+++ b/res/layout/launcher_overlay.xml
@@ -14,7 +14,7 @@
      limitations under the License.
 -->
 
-<FrameLayout
+<com.android.launcher3.InsettableFrameLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
-    android:layout_height="match_parent" />
\ No newline at end of file
+    android:layout_height="match_parent" />
diff --git a/res/layout/launcher_overlay_example.xml b/res/layout/launcher_overlay_example.xml
index 422f281..8735d14 100644
--- a/res/layout/launcher_overlay_example.xml
+++ b/res/layout/launcher_overlay_example.xml
@@ -14,19 +14,19 @@
      limitations under the License.
 -->
 
-<FrameLayout
+<com.android.launcher3.InsettableFrameLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
 
-    <FrameLayout
+    <com.android.launcher3.InsettableFrameLayout
         android:id="@+id/search_overlay"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:background="#ff00ff00"
         android:visibility="invisible" />
 
-    <FrameLayout
+    <com.android.launcher3.InsettableFrameLayout
         android:id="@+id/search_box"
         android:layout_width="match_parent"
         android:layout_height="48dp"
@@ -34,4 +34,4 @@
         android:layout_marginRight="8dp"
         android:layout_marginTop="36dp"
         android:background="#ffff0000" />
-</FrameLayout>
+</com.android.launcher3.InsettableFrameLayout>
diff --git a/src/com/android/launcher3/DragLayer.java b/src/com/android/launcher3/DragLayer.java
index ff9072a..79d4278 100644
--- a/src/com/android/launcher3/DragLayer.java
+++ b/src/com/android/launcher3/DragLayer.java
@@ -43,7 +43,7 @@
 /**
  * A ViewGroup that coordinates dragging across its descendants
  */
-public class DragLayer extends FrameLayout implements ViewGroup.OnHierarchyChangeListener {
+public class DragLayer extends InsettableFrameLayout {
     private DragController mDragController;
     private int[] mTmpXY = new int[2];
 
@@ -71,8 +71,6 @@
 
     private TouchCompleteListener mTouchCompleteListener;
 
-    private final Rect mInsets = new Rect();
-
     private View mOverlayView;
     private int mTopViewIndex;
     private int mChildCountOnLastUpdate = -1;
@@ -103,7 +101,6 @@
         // Disable multitouch across the workspace/all apps/customize tray
         setMotionEventSplittingEnabled(false);
         setChildrenDrawingOrderEnabled(true);
-        setOnHierarchyChangeListener(this);
 
         final Resources res = getResources();
         mLeftHoverDrawable = res.getDrawable(R.drawable.page_hover_left);
@@ -123,27 +120,6 @@
         return mDragController.dispatchKeyEvent(event) || super.dispatchKeyEvent(event);
     }
 
-    @Override
-    protected boolean fitSystemWindows(Rect insets) {
-        final int n = getChildCount();
-        for (int i = 0; i < n; i++) {
-            final View child = getChildAt(i);
-            setInsets(child, insets, mInsets);
-        }
-        mInsets.set(insets);
-        return true; // I'll take it from here
-    }
-
-    Rect getInsets() {
-        return mInsets;
-    }
-
-    @Override
-    public void addView(View child, int index, android.view.ViewGroup.LayoutParams params) {
-        super.addView(child, index, params);
-        setInsets(child, mInsets, new Rect());
-    }
-
     public void showOverlayView(View overlayView) {
         LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
         mOverlayView = overlayView;
@@ -158,19 +134,6 @@
         removeView(mOverlayView);
     }
 
-    private void setInsets(View child, Rect newInsets, Rect oldInsets) {
-        final FrameLayout.LayoutParams flp = (FrameLayout.LayoutParams) child.getLayoutParams();
-        if (child instanceof Insettable) {
-            ((Insettable) child).setInsets(newInsets);
-        } else {
-            flp.topMargin += (newInsets.top - oldInsets.top);
-            flp.leftMargin += (newInsets.left - oldInsets.left);
-            flp.rightMargin += (newInsets.right - oldInsets.right);
-            flp.bottomMargin += (newInsets.bottom - oldInsets.bottom);
-        }
-        child.setLayoutParams(flp);
-    }
-
     private boolean isEventOverFolderTextRegion(Folder folder, MotionEvent ev) {
         getDescendantRectRelativeToSelf(folder.getEditTextRegion(), mHitRect);
         if (mHitRect.contains((int) ev.getX(), (int) ev.getY())) {
@@ -815,6 +778,7 @@
 
     @Override
     public void onChildViewAdded(View parent, View child) {
+        super.onChildViewAdded(parent, child);
         if (mOverlayView != null) {
             // ensure that the overlay view stays on top. we can't use drawing order for this
             // because in API level 16 touch dispatch doesn't respect drawing order.
diff --git a/src/com/android/launcher3/InsettableFrameLayout.java b/src/com/android/launcher3/InsettableFrameLayout.java
new file mode 100644
index 0000000..4ba9c88
--- /dev/null
+++ b/src/com/android/launcher3/InsettableFrameLayout.java
@@ -0,0 +1,52 @@
+package com.android.launcher3;
+
+import android.content.Context;
+import android.graphics.Rect;
+import android.util.AttributeSet;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.FrameLayout;
+
+public class InsettableFrameLayout extends FrameLayout implements
+    ViewGroup.OnHierarchyChangeListener, Insettable {
+
+    protected Rect mInsets = new Rect();
+
+    public InsettableFrameLayout(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        setOnHierarchyChangeListener(this);
+    }
+
+    public void setFrameLayoutChildInsets(View child, Rect newInsets, Rect oldInsets) {
+        final FrameLayout.LayoutParams flp = (FrameLayout.LayoutParams) child.getLayoutParams();
+        if (child instanceof Insettable) {
+            ((Insettable) child).setInsets(newInsets);
+        } else {
+            flp.topMargin += (newInsets.top - oldInsets.top);
+            flp.leftMargin += (newInsets.left - oldInsets.left);
+            flp.rightMargin += (newInsets.right - oldInsets.right);
+            flp.bottomMargin += (newInsets.bottom - oldInsets.bottom);
+        }
+        child.setLayoutParams(flp);
+    }
+
+    @Override
+    public void setInsets(Rect insets) {
+        final int n = getChildCount();
+        for (int i = 0; i < n; i++) {
+            final View child = getChildAt(i);
+            setFrameLayoutChildInsets(child, insets, mInsets);
+        }
+        mInsets.set(insets);
+    }
+
+    @Override
+    public void onChildViewAdded(View parent, View child) {
+        setFrameLayoutChildInsets(child, mInsets, new Rect());
+    }
+
+    @Override
+    public void onChildViewRemoved(View parent, View child) {
+    }
+
+}
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index 309837f..da83aa0 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -231,7 +231,7 @@
 
     LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
     LauncherOverlay mLauncherOverlay;
-    ViewGroup mLauncherOverlayView;
+    InsettableFrameLayout mLauncherOverlayContainer;
 
     static final int APPWIDGET_HOST_ID = 1024;
     public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
@@ -514,9 +514,9 @@
             mLauncherCallbacks.onCreate(savedInstanceState);
             if (mLauncherCallbacks.hasLauncherOverlay()) {
                 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
-                mLauncherOverlayView = (ViewGroup) stub.inflate();
-                mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(mLauncherOverlayView,
-                        mLauncherOverlayCallbacks);
+                mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
+                mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
+                        mLauncherOverlayContainer, mLauncherOverlayCallbacks);
                 mWorkspace.setLauncherOverlay(mLauncherOverlay);
             }
         }
diff --git a/src/com/android/launcher3/LauncherCallbacks.java b/src/com/android/launcher3/LauncherCallbacks.java
index e0cfa27..a1f4e0b 100644
--- a/src/com/android/launcher3/LauncherCallbacks.java
+++ b/src/com/android/launcher3/LauncherCallbacks.java
@@ -98,11 +98,11 @@
     /**
      * Handshake to establish an overlay relationship
      *
-     * @param overlayView Full screen overlay ViewGroup into which custom views can be placed.
+     * @param container Full screen overlay ViewGroup into which custom views can be placed.
      * @param callbacks A set of callbacks provided by Launcher in relation to the overlay
      * @return an interface used to make requests and notify the Launcher in relation to the overlay
      */
-    public Launcher.LauncherOverlay setLauncherOverlayView(ViewGroup overlayView,
+    public Launcher.LauncherOverlay setLauncherOverlayView(InsettableFrameLayout container,
             Launcher.LauncherOverlayCallbacks callbacks);
 
 }
diff --git a/src/com/android/launcher3/LauncherExtension.java b/src/com/android/launcher3/LauncherExtension.java
index 10bbd35..b264042 100644
--- a/src/com/android/launcher3/LauncherExtension.java
+++ b/src/com/android/launcher3/LauncherExtension.java
@@ -252,11 +252,11 @@
         }
 
         @Override
-        public LauncherOverlay setLauncherOverlayView(ViewGroup overlayView,
+        public LauncherOverlay setLauncherOverlayView(InsettableFrameLayout container,
                 LauncherOverlayCallbacks callbacks) {
 
             mLauncherOverlay.setOverlayCallbacks(callbacks);
-            mLauncherOverlay.setOverlayView(overlayView);
+            mLauncherOverlay.setOverlayContainer(container);
 
             return mLauncherOverlay;
         }
@@ -335,9 +335,9 @@
                 hideOverlayPanel();
             }
 
-            public void setOverlayView(ViewGroup overlayView) {
+            public void setOverlayContainer(InsettableFrameLayout container) {
                 mOverlayView = (ViewGroup) getLayoutInflater().inflate(
-                        R.layout.launcher_overlay_example, overlayView);
+                        R.layout.launcher_overlay_example, container);
                 mSearchOverlay = mOverlayView.findViewById(R.id.search_overlay);
                 mSearchBox = mOverlayView.findViewById(R.id.search_box);
             }
diff --git a/src/com/android/launcher3/LauncherRootView.java b/src/com/android/launcher3/LauncherRootView.java
new file mode 100644
index 0000000..e8c11c4
--- /dev/null
+++ b/src/com/android/launcher3/LauncherRootView.java
@@ -0,0 +1,17 @@
+package com.android.launcher3;
+
+import android.content.Context;
+import android.graphics.Rect;
+import android.util.AttributeSet;
+
+public class LauncherRootView extends InsettableFrameLayout {
+    public LauncherRootView(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    @Override
+    protected boolean fitSystemWindows(Rect insets) {
+        setInsets(insets);
+        return true; // I'll take it from here
+    }
+}
\ No newline at end of file