Implement new window cropping.

The window manager now performs the crop internally, evaluating
it every animation from, to be able to update it along with
the surface position.

Change-Id: I960a2161b9defb6fba4840fa35aee4e411c39b32
diff --git a/core/java/android/service/wallpaper/WallpaperService.java b/core/java/android/service/wallpaper/WallpaperService.java
index 9d36677..3e0942c 100644
--- a/core/java/android/service/wallpaper/WallpaperService.java
+++ b/core/java/android/service/wallpaper/WallpaperService.java
@@ -154,7 +154,6 @@
         int mCurWindowPrivateFlags = mWindowPrivateFlags;
         final Rect mVisibleInsets = new Rect();
         final Rect mWinFrame = new Rect();
-        final Rect mSystemInsets = new Rect();
         final Rect mContentInsets = new Rect();
         final Configuration mConfiguration = new Configuration();
         
@@ -254,7 +253,7 @@
 
         final BaseIWindow mWindow = new BaseIWindow() {
             @Override
-            public void resized(int w, int h, Rect systemInsets, Rect contentInsets,
+            public void resized(int w, int h, Rect contentInsets,
                     Rect visibleInsets, boolean reportDraw, Configuration newConfig) {
                 Message msg = mCaller.obtainMessageI(MSG_WINDOW_RESIZED,
                         reportDraw ? 1 : 0);
@@ -621,7 +620,7 @@
 
                     final int relayoutResult = mSession.relayout(
                         mWindow, mWindow.mSeq, mLayout, mWidth, mHeight,
-                            View.VISIBLE, 0, mWinFrame, mSystemInsets, mContentInsets,
+                            View.VISIBLE, 0, mWinFrame, mContentInsets,
                             mVisibleInsets, mConfiguration, mSurfaceHolder.mSurface);
 
                     if (DEBUG) Log.v(TAG, "New surface: " + mSurfaceHolder.mSurface
diff --git a/core/java/android/view/IWindow.aidl b/core/java/android/view/IWindow.aidl
index 555f306..b4caad3 100644
--- a/core/java/android/view/IWindow.aidl
+++ b/core/java/android/view/IWindow.aidl
@@ -45,7 +45,7 @@
      */
     void executeCommand(String command, String parameters, in ParcelFileDescriptor descriptor);
 
-    void resized(int w, int h, in Rect systemInsets, in Rect contentInsets,
+    void resized(int w, int h, in Rect contentInsets,
             in Rect visibleInsets, boolean reportDraw, in Configuration newConfig);
     void dispatchAppVisibility(boolean visible);
     void dispatchGetNewSurface();
diff --git a/core/java/android/view/IWindowSession.aidl b/core/java/android/view/IWindowSession.aidl
index f26d5e1..d4a03ce 100644
--- a/core/java/android/view/IWindowSession.aidl
+++ b/core/java/android/view/IWindowSession.aidl
@@ -58,10 +58,6 @@
      * {@link WindowManagerImpl#RELAYOUT_DEFER_SURFACE_DESTROY}.
      * @param outFrame Rect in which is placed the new position/size on
      * screen.
-     * @param outSystemInsets Rect in which is placed the offsets from
-     * <var>outFrame</var> over which any core system UI elements are
-     * currently covering the window.  This is not generally used for
-     * layout, but just to know where the window is obscured.
      * @param outContentInsets Rect in which is placed the offsets from
      * <var>outFrame</var> in which the content of the window should be
      * placed.  This can be used to modify the window layout to ensure its
@@ -83,7 +79,7 @@
      */
     int relayout(IWindow window, int seq, in WindowManager.LayoutParams attrs,
             int requestedWidth, int requestedHeight, int viewVisibility,
-            int flags, out Rect outFrame, out Rect outSystemInsets,
+            int flags, out Rect outFrame,
             out Rect outContentInsets, out Rect outVisibleInsets,
             out Configuration outConfig, out Surface outSurface);
 
diff --git a/core/java/android/view/SurfaceView.java b/core/java/android/view/SurfaceView.java
index ee322f8..f4ca9d2 100644
--- a/core/java/android/view/SurfaceView.java
+++ b/core/java/android/view/SurfaceView.java
@@ -98,7 +98,6 @@
     MyWindow mWindow;
     final Rect mVisibleInsets = new Rect();
     final Rect mWinFrame = new Rect();
-    final Rect mSystemInsets = new Rect();
     final Rect mContentInsets = new Rect();
     final Configuration mConfiguration = new Configuration();
     
@@ -472,7 +471,7 @@
                         mWindow, mWindow.mSeq, mLayout, mWidth, mHeight,
                             visible ? VISIBLE : GONE,
                             WindowManagerImpl.RELAYOUT_DEFER_SURFACE_DESTROY,
-                            mWinFrame, mSystemInsets, mContentInsets,
+                            mWinFrame, mContentInsets,
                             mVisibleInsets, mConfiguration, mNewSurface);
                     if ((relayoutResult&WindowManagerImpl.RELAYOUT_RES_FIRST_TIME) != 0) {
                         mReportDrawNeeded = true;
@@ -606,7 +605,7 @@
             mSurfaceView = new WeakReference<SurfaceView>(surfaceView);
         }
 
-        public void resized(int w, int h, Rect systemInsets, Rect contentInsets,
+        public void resized(int w, int h, Rect contentInsets,
                 Rect visibleInsets, boolean reportDraw, Configuration newConfig) {
             SurfaceView surfaceView = mSurfaceView.get();
             if (surfaceView != null) {
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
index 4d13e8a..cec27fc 100644
--- a/core/java/android/view/View.java
+++ b/core/java/android/view/View.java
@@ -17161,12 +17161,6 @@
         boolean mUse32BitDrawingCache;
 
         /**
-         * Describes the parts of the window that are currently completely
-         * obscured by system UI elements.
-         */
-        final Rect mSystemInsets = new Rect();
-
-        /**
          * For windows that are full-screen but using insets to layout inside
          * of the screen decorations, these are the current insets for the
          * content of the window.
diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java
index 1310719..90e6034 100644
--- a/core/java/android/view/ViewRootImpl.java
+++ b/core/java/android/view/ViewRootImpl.java
@@ -262,7 +262,6 @@
 
     final Rect mPendingVisibleInsets = new Rect();
     final Rect mPendingContentInsets = new Rect();
-    final Rect mPendingSystemInsets = new Rect();
     final ViewTreeObserver.InternalInsetsInfo mLastGivenInsets
             = new ViewTreeObserver.InternalInsetsInfo();
 
@@ -272,7 +271,6 @@
     final Configuration mPendingConfiguration = new Configuration();
 
     class ResizedInfo {
-        Rect systemInsets;
         Rect contentInsets;
         Rect visibleInsets;
         Configuration newConfig;
@@ -568,7 +566,6 @@
                 if (mTranslator != null) {
                     mTranslator.translateRectInScreenToAppWindow(mAttachInfo.mContentInsets);
                 }
-                mPendingSystemInsets.set(0, 0, 0, 0);
                 mPendingContentInsets.set(mAttachInfo.mContentInsets);
                 mPendingVisibleInsets.set(0, 0, 0, 0);
                 if (DEBUG_LAYOUT) Log.v(TAG, "Added window " + mWindow);
@@ -1235,7 +1232,6 @@
         getRunQueue().executeActions(attachInfo.mHandler);
 
         boolean insetsChanged = false;
-        boolean activeRectChanged = false;
 
         boolean layoutRequested = mLayoutRequested && !mStopped;
         if (layoutRequested) {
@@ -1247,12 +1243,7 @@
                 // to opposite of the added touch mode.
                 mAttachInfo.mInTouchMode = !mAddedTouchMode;
                 ensureTouchModeLocally(mAddedTouchMode);
-                activeRectChanged = true;
             } else {
-                if (!mPendingSystemInsets.equals(mAttachInfo.mSystemInsets)) {
-                    mAttachInfo.mSystemInsets.set(mPendingSystemInsets);
-                    activeRectChanged = true;
-                }
                 if (!mPendingContentInsets.equals(mAttachInfo.mContentInsets)) {
                     insetsChanged = true;
                 }
@@ -1406,10 +1397,6 @@
                     mPendingConfiguration.seq = 0;
                 }
 
-                if (!mPendingSystemInsets.equals(mAttachInfo.mSystemInsets)) {
-                    activeRectChanged = true;
-                    mAttachInfo.mSystemInsets.set(mPendingSystemInsets);
-                }
                 contentInsetsChanged = !mPendingContentInsets.equals(
                         mAttachInfo.mContentInsets);
                 visibleInsetsChanged = !mPendingVisibleInsets.equals(
@@ -1512,7 +1499,6 @@
                         // before actually drawing them, so it can display then
                         // all at once.
                         newSurface = true;
-                        activeRectChanged = true;
                         mFullRedrawNeeded = true;
                         mPreviousTransparentRegion.setEmpty();
 
@@ -1578,7 +1564,6 @@
             // window size we asked for. We should avoid this by getting a maximum size from
             // the window session beforehand.
             if (mWidth != frame.width() || mHeight != frame.height()) {
-                activeRectChanged = true;
                 mWidth = frame.width();
                 mHeight = frame.height();
             }
@@ -2814,7 +2799,6 @@
                 ResizedInfo ri = (ResizedInfo)msg.obj;
 
                 if (mWinFrame.width() == msg.arg1 && mWinFrame.height() == msg.arg2
-                        && mPendingSystemInsets.equals(ri.systemInsets)
                         && mPendingContentInsets.equals(ri.contentInsets)
                         && mPendingVisibleInsets.equals(ri.visibleInsets)
                         && ((ResizedInfo)msg.obj).newConfig == null) {
@@ -2831,7 +2815,6 @@
                     mWinFrame.right = msg.arg1;
                     mWinFrame.top = 0;
                     mWinFrame.bottom = msg.arg2;
-                    mPendingSystemInsets.set(((ResizedInfo)msg.obj).systemInsets);
                     mPendingContentInsets.set(((ResizedInfo)msg.obj).contentInsets);
                     mPendingVisibleInsets.set(((ResizedInfo)msg.obj).visibleInsets);
                     if (msg.what == MSG_RESIZED_REPORT) {
@@ -3866,7 +3849,7 @@
                 (int) (mView.getMeasuredWidth() * appScale + 0.5f),
                 (int) (mView.getMeasuredHeight() * appScale + 0.5f),
                 viewVisibility, insetsPending ? WindowManagerImpl.RELAYOUT_INSETS_PENDING : 0,
-                mWinFrame, mPendingSystemInsets, mPendingContentInsets, mPendingVisibleInsets,
+                mWinFrame, mPendingContentInsets, mPendingVisibleInsets,
                 mPendingConfiguration, mSurface);
         //Log.d(TAG, "<<<<<< BACK FROM relayout");
         if (restore) {
@@ -4062,11 +4045,10 @@
         mHandler.sendMessage(msg);
     }
 
-    public void dispatchResized(int w, int h, Rect systemInsets, Rect contentInsets,
+    public void dispatchResized(int w, int h, Rect contentInsets,
             Rect visibleInsets, boolean reportDraw, Configuration newConfig) {
         if (DEBUG_LAYOUT) Log.v(TAG, "Resizing " + this + ": w=" + w
-                + " h=" + h + " systemInsets=" + systemInsets.toShortString()
-                + " contentInsets=" + contentInsets.toShortString()
+                + " h=" + h + " contentInsets=" + contentInsets.toShortString()
                 + " visibleInsets=" + visibleInsets.toShortString()
                 + " reportDraw=" + reportDraw);
         Message msg = mHandler.obtainMessage(reportDraw ? MSG_RESIZED_REPORT :MSG_RESIZED);
@@ -4079,7 +4061,6 @@
         msg.arg1 = w;
         msg.arg2 = h;
         ResizedInfo ri = new ResizedInfo();
-        ri.systemInsets = new Rect(systemInsets);
         ri.contentInsets = new Rect(contentInsets);
         ri.visibleInsets = new Rect(visibleInsets);
         ri.newConfig = newConfig;
@@ -4735,11 +4716,11 @@
             mViewAncestor = new WeakReference<ViewRootImpl>(viewAncestor);
         }
 
-        public void resized(int w, int h, Rect systemInsets, Rect contentInsets,
+        public void resized(int w, int h, Rect contentInsets,
                 Rect visibleInsets, boolean reportDraw, Configuration newConfig) {
             final ViewRootImpl viewAncestor = mViewAncestor.get();
             if (viewAncestor != null) {
-                viewAncestor.dispatchResized(w, h, systemInsets, contentInsets,
+                viewAncestor.dispatchResized(w, h, contentInsets,
                         visibleInsets, reportDraw, newConfig);
             }
         }
diff --git a/core/java/android/view/WindowManagerPolicy.java b/core/java/android/view/WindowManagerPolicy.java
index 388cfb3..ddef59b 100644
--- a/core/java/android/view/WindowManagerPolicy.java
+++ b/core/java/android/view/WindowManagerPolicy.java
@@ -145,10 +145,6 @@
          * @param displayFrame The frame of the overall display in which this
          * window can appear, used for constraining the overall dimensions
          * of the window.
-         * @param systemFrame The frame within the display that any system
-         * elements are currently covering.  These indicate which parts of
-         * the window should be considered completely obscured by the screen
-         * decorations.
          * @param contentFrame The frame within the display in which we would
          * like active content to appear.  This will cause windows behind to
          * be resized to match the given content frame.
@@ -160,7 +156,7 @@
          * are visible.
          */
         public void computeFrameLw(Rect parentFrame, Rect displayFrame,
-                Rect systemFrame, Rect contentFrame, Rect visibleFrame);
+                Rect contentFrame, Rect visibleFrame);
 
         /**
          * Retrieve the current frame of the window that has been assigned by
@@ -188,14 +184,6 @@
         public Rect getDisplayFrameLw();
 
         /**
-         * Retrieve the frame of the system elements that last covered the window.
-         * Must be called with the window manager lock held.
-         *
-         * @return Rect The rectangle holding the system frame.
-         */
-        public Rect getSystemFrameLw();
-
-        /**
          * Retrieve the frame of the content area that this window was last
          * laid out in.  This is the area in which the content of the window
          * should be placed.  It will be smaller than the display frame to
@@ -770,6 +758,21 @@
     public void beginLayoutLw(int displayWidth, int displayHeight, int displayRotation);
 
     /**
+     * Return the rectangle of the screen currently covered by system decorations.
+     * This will be called immediately after {@link #layoutWindowLw}.  It can
+     * fill in the rectangle to indicate any part of the screen that it knows
+     * for sure is covered by system decor such as the status bar.  The rectangle
+     * is initially set to the actual size of the screen, indicating nothing is
+     * covered.
+     *
+     * @param systemRect The rectangle of the screen that is not covered by
+     * system decoration.
+     * @return Returns the layer above which the system rectangle should
+     * not be applied.
+     */
+    public int getSystemDecorRectLw(Rect systemRect);
+
+    /**
      * Called for each window attached to the window manager as layout is
      * proceeding.  The implementation of this function must take care of
      * setting the window's frame, either here or in finishLayout().
@@ -794,7 +797,7 @@
      * 
      */
     public void getContentInsetHintLw(WindowManager.LayoutParams attrs, Rect contentInset);
-    
+
     /**
      * Called when layout of the windows is finished.  After this function has
      * returned, all windows given to layoutWindow() <em>must</em> have had a
diff --git a/core/java/com/android/internal/view/BaseIWindow.java b/core/java/com/android/internal/view/BaseIWindow.java
index fbed4859..4c34d73 100644
--- a/core/java/com/android/internal/view/BaseIWindow.java
+++ b/core/java/com/android/internal/view/BaseIWindow.java
@@ -33,7 +33,7 @@
         mSession = session;
     }
     
-    public void resized(int w, int h, Rect systemInsets, Rect contentInsets,
+    public void resized(int w, int h, Rect contentInsets,
             Rect visibleInsets, boolean reportDraw, Configuration newConfig) {
         if (reportDraw) {
             try {
diff --git a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
index 794ed15..5916bd1 100755
--- a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
+++ b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
@@ -15,7 +15,6 @@
 
 package com.android.internal.policy.impl;
 
-import android.app.Activity;
 import android.app.ActivityManager;
 import android.app.ActivityManagerNative;
 import android.app.IUiModeManager;
@@ -37,8 +36,6 @@
 import android.database.ContentObserver;
 import android.graphics.PixelFormat;
 import android.graphics.Rect;
-import android.graphics.RectF;
-import android.hardware.input.InputManager;
 import android.media.AudioManager;
 import android.media.IAudioService;
 import android.os.BatteryManager;
@@ -149,7 +146,6 @@
 import java.io.FileReader;
 import java.io.IOException;
 import java.io.PrintWriter;
-import java.util.ArrayList;
 
 /**
  * WindowManagerPolicy implementation for the Android phone UI.  This
@@ -248,8 +244,6 @@
     static final int SYSTEM_UI_CHANGING_LAYOUT =
             View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN;
 
-    private static final int BTN_MOUSE = 0x110;
-
     /* Table of Application Launch keys.  Maps from key codes to intent categories.
      *
      * These are special keys that are used to launch particular kinds of applications,
@@ -448,7 +442,6 @@
 
     static final Rect mTmpParentFrame = new Rect();
     static final Rect mTmpDisplayFrame = new Rect();
-    static final Rect mTmpSystemFrame = new Rect();
     static final Rect mTmpContentFrame = new Rect();
     static final Rect mTmpVisibleFrame = new Rect();
     static final Rect mTmpNavigationFrame = new Rect();
@@ -2302,7 +2295,7 @@
             mStatusBarLayer = mNavigationBar.getSurfaceLayer();
             // And compute the final frame.
             mNavigationBar.computeFrameLw(mTmpNavigationFrame, mTmpNavigationFrame,
-                    mTmpNavigationFrame, mTmpNavigationFrame, mTmpNavigationFrame);
+                    mTmpNavigationFrame, mTmpNavigationFrame);
             if (DEBUG_LAYOUT) Log.i(TAG, "mNavigationBar frame: " + mTmpNavigationFrame);
         }
         if (DEBUG_LAYOUT) Log.i(TAG, String.format("mDock rect: (%d,%d - %d,%d)",
@@ -2323,7 +2316,7 @@
             mStatusBarLayer = mStatusBar.getSurfaceLayer();
 
             // Let the status bar determine its size.
-            mStatusBar.computeFrameLw(pf, df, df, vf, vf);
+            mStatusBar.computeFrameLw(pf, df, vf, vf);
 
             // For layout, the status bar is always at the top with our fixed height.
             mStableTop = mUnrestrictedScreenTop + mStatusBarHeight;
@@ -2357,6 +2350,17 @@
         }
     }
 
+    /** {@inheritDoc} */
+    public int getSystemDecorRectLw(Rect systemRect) {
+        systemRect.left = mSystemLeft;
+        systemRect.top = mSystemTop;
+        systemRect.right = mSystemRight;
+        systemRect.bottom = mSystemBottom;
+        if (mStatusBar != null) return mStatusBar.getSurfaceLayer();
+        if (mNavigationBar != null) return mNavigationBar.getSurfaceLayer();
+        return 0;
+    }
+
     void setAttachedWindowFrames(WindowState win, int fl, int adjust,
             WindowState attached, boolean insetDecors, Rect pf, Rect df, Rect cf, Rect vf) {
         if (win.getSurfaceLayer() > mDockLayer && attached.getSurfaceLayer() < mDockLayer) {
@@ -2427,7 +2431,6 @@
 
         final Rect pf = mTmpParentFrame;
         final Rect df = mTmpDisplayFrame;
-        final Rect sf = mTmpSystemFrame;
         final Rect cf = mTmpContentFrame;
         final Rect vf = mTmpVisibleFrame;
         
@@ -2671,20 +2674,6 @@
             df.right = df.bottom = cf.right = cf.bottom = vf.right = vf.bottom = 10000;
         }
 
-        // Compute the system frame.  This is easy: for things behind the
-        // status bar, it is any application windows; otherwise it is not set.
-        int parentType = attached != null ? attached.getAttrs().type : attrs.type;
-        if (win.getSurfaceLayer() < mStatusBarLayer
-                && parentType < WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW) {
-            sf.left = mSystemLeft;
-            sf.top = mSystemTop;
-            sf.right = mSystemRight;
-            sf.bottom = mSystemBottom;
-        } else {
-            sf.left = sf.top = -10000;
-            sf.right = sf.bottom = 10000;
-        }
-
         if (DEBUG_LAYOUT) Log.v(TAG, "Compute frame " + attrs.getTitle()
                 + ": sim=#" + Integer.toHexString(sim)
                 + " attach=" + attached + " type=" + attrs.type 
@@ -2692,7 +2681,7 @@
                 + " pf=" + pf.toShortString() + " df=" + df.toShortString()
                 + " cf=" + cf.toShortString() + " vf=" + vf.toShortString());
         
-        win.computeFrameLw(pf, df, sf, cf, vf);
+        win.computeFrameLw(pf, df, cf, vf);
         
         // Dock windows carve out the bottom of the screen, so normal windows
         // can't appear underneath them.
diff --git a/services/java/com/android/server/wm/Session.java b/services/java/com/android/server/wm/Session.java
index 53c0e07..61c0e9c 100644
--- a/services/java/com/android/server/wm/Session.java
+++ b/services/java/com/android/server/wm/Session.java
@@ -151,13 +151,13 @@
 
     public int relayout(IWindow window, int seq, WindowManager.LayoutParams attrs,
             int requestedWidth, int requestedHeight, int viewFlags,
-            int flags, Rect outFrame, Rect outSystemInsets, Rect outContentInsets,
+            int flags, Rect outFrame, Rect outContentInsets,
             Rect outVisibleInsets, Configuration outConfig, Surface outSurface) {
         if (false) Slog.d(WindowManagerService.TAG, ">>>>>> ENTERED relayout from "
                 + Binder.getCallingPid());
         int res = mService.relayoutWindow(this, window, seq, attrs,
                 requestedWidth, requestedHeight, viewFlags, flags,
-                outFrame, outSystemInsets, outContentInsets, outVisibleInsets,
+                outFrame, outContentInsets, outVisibleInsets,
                 outConfig, outSurface);
         if (false) Slog.d(WindowManagerService.TAG, "<<<<<< EXITING relayout to "
                 + Binder.getCallingPid());
diff --git a/services/java/com/android/server/wm/WindowManagerService.java b/services/java/com/android/server/wm/WindowManagerService.java
index 2efcb8e..c7023c0 100755
--- a/services/java/com/android/server/wm/WindowManagerService.java
+++ b/services/java/com/android/server/wm/WindowManagerService.java
@@ -480,6 +480,9 @@
             = new ArrayList<IRotationWatcher>();
     int mDeferredRotationPauseCount;
 
+    final Rect mSystemDecorRect = new Rect();
+    int mSystemDecorLayer = 0;
+
     int mPendingLayoutChanges = 0;
     boolean mLayoutNeeded = true;
     boolean mTraversalScheduled = false;
@@ -2647,7 +2650,7 @@
     public int relayoutWindow(Session session, IWindow client, int seq,
             WindowManager.LayoutParams attrs, int requestedWidth,
             int requestedHeight, int viewVisibility, int flags,
-            Rect outFrame, Rect outSystemInsets, Rect outContentInsets,
+            Rect outFrame, Rect outContentInsets,
             Rect outVisibleInsets, Configuration outConfig, Surface outSurface) {
         boolean displayed = false;
         boolean inTouchMode;
@@ -2939,7 +2942,6 @@
                 win.mAppToken.updateReportedVisibilityLocked();
             }
             outFrame.set(win.mCompatFrame);
-            outSystemInsets.set(win.mSystemInsets);
             outContentInsets.set(win.mContentInsets);
             outVisibleInsets.set(win.mVisibleInsets);
             if (localLOGV) Slog.v(
@@ -7710,6 +7712,7 @@
         }
         
         mPolicy.beginLayoutLw(dw, dh, mRotation);
+        mSystemDecorLayer = mPolicy.getSystemDecorRectLw(mSystemDecorRect);
 
         int seq = mLayoutSeq+1;
         if (seq < 0) seq = 0;
@@ -8172,8 +8175,6 @@
     private void updateResizingWindows(final WindowState w) {
         final WindowStateAnimator winAnimator = w.mWinAnimator;
         if (w.mHasSurface && !w.mAppFreezing && w.mLayoutSeq == mLayoutSeq) {
-            w.mSystemInsetsChanged |=
-                    !w.mLastSystemInsets.equals(w.mSystemInsets);
             w.mContentInsetsChanged |=
                     !w.mLastContentInsets.equals(w.mContentInsets);
             w.mVisibleInsetsChanged |=
@@ -8190,8 +8191,7 @@
                     + ": configChanged=" + configChanged
                     + " last=" + w.mLastFrame + " frame=" + w.mFrame);
             w.mLastFrame.set(w.mFrame);
-            if (w.mSystemInsetsChanged
-                    || w.mContentInsetsChanged
+            if (w.mContentInsetsChanged
                     || w.mVisibleInsetsChanged
                     || winAnimator.mSurfaceResized
                     || configChanged) {
@@ -8203,7 +8203,6 @@
                             + " configChanged=" + configChanged);
                 }
 
-                w.mLastSystemInsets.set(w.mSystemInsets);
                 w.mLastContentInsets.set(w.mContentInsets);
                 w.mLastVisibleInsets.set(w.mVisibleInsets);
                 makeWindowFreezingScreenIfNeededLocked(w);
@@ -8587,11 +8586,10 @@
                             winAnimator.mDrawState == WindowStateAnimator.DRAW_PENDING) Slog.i(
                             TAG, "Resizing " + win + " WITH DRAW PENDING");
                     win.mClient.resized((int)winAnimator.mSurfaceW,
-                            (int)winAnimator.mSurfaceH, win.mLastSystemInsets,
+                            (int)winAnimator.mSurfaceH,
                             win.mLastContentInsets, win.mLastVisibleInsets,
                             winAnimator.mDrawState == WindowStateAnimator.DRAW_PENDING,
                             configChanged ? win.mConfiguration : null);
-                    win.mSystemInsetsChanged = false;
                     win.mContentInsetsChanged = false;
                     win.mVisibleInsetsChanged = false;
                     winAnimator.mSurfaceResized = false;
@@ -9582,6 +9580,8 @@
         pw.print("  mInTouchMode="); pw.print(mInTouchMode);
                 pw.print(" mLayoutSeq="); pw.println(mLayoutSeq);
         if (dumpAll) {
+            pw.print("  mSystemDecorRect="); pw.print(mSystemDecorRect.toShortString());
+                    pw.print(" mSystemDecorLayer="); pw.println(mSystemDecorLayer);
             if (mLastStatusBarVisibility != 0) {
                 pw.print("  mLastStatusBarVisibility=0x");
                         pw.println(Integer.toHexString(mLastStatusBarVisibility));
diff --git a/services/java/com/android/server/wm/WindowState.java b/services/java/com/android/server/wm/WindowState.java
index 05e7d3a..1fd80c2 100644
--- a/services/java/com/android/server/wm/WindowState.java
+++ b/services/java/com/android/server/wm/WindowState.java
@@ -133,14 +133,6 @@
     boolean mContentInsetsChanged;
 
     /**
-     * Insets that are covered by system windows such as the status bar.  These
-     * are in the application's coordinate space (without compatibility scale applied).
-     */
-    final Rect mSystemInsets = new Rect();
-    final Rect mLastSystemInsets = new Rect();
-    boolean mSystemInsetsChanged;
-
-    /**
      * Set to true if we are waiting for this window to receive its
      * given internal insets before laying out other windows based on it.
      */
@@ -171,6 +163,13 @@
      */
     int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
 
+    /**
+     * This is rectangle of the window's surface that is not covered by
+     * system decorations.
+     */
+    final Rect mSystemDecorRect = new Rect();
+    final Rect mLastSystemDecorRect = new Rect();
+
     // Current transformation being applied.
     float mGlobalScale=1;
     float mInvGlobalScale=1;
@@ -187,7 +186,6 @@
 
     final Rect mContainingFrame = new Rect();
     final Rect mDisplayFrame = new Rect();
-    final Rect mSystemFrame = new Rect();
     final Rect mContentFrame = new Rect();
     final Rect mParentFrame = new Rect();
     final Rect mVisibleFrame = new Rect();
@@ -356,7 +354,7 @@
     }
 
     @Override
-    public void computeFrameLw(Rect pf, Rect df, Rect sf, Rect cf, Rect vf) {
+    public void computeFrameLw(Rect pf, Rect df, Rect cf, Rect vf) {
         mHaveFrame = true;
 
         final Rect container = mContainingFrame;
@@ -413,9 +411,6 @@
             mContentChanged = true;
         }
 
-        final Rect system = mSystemFrame;
-        system.set(sf);
-
         final Rect content = mContentFrame;
         content.set(cf);
 
@@ -449,10 +444,6 @@
 
         // Make sure the system, content and visible frames are inside of the
         // final window frame.
-        if (system.left < frame.left) system.left = frame.left;
-        if (system.top < frame.top) system.top = frame.top;
-        if (system.right > frame.right) system.right = frame.right;
-        if (system.bottom > frame.bottom) system.bottom = frame.bottom;
         if (content.left < frame.left) content.left = frame.left;
         if (content.top < frame.top) content.top = frame.top;
         if (content.right > frame.right) content.right = frame.right;
@@ -462,12 +453,6 @@
         if (visible.right > frame.right) visible.right = frame.right;
         if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
 
-        final Rect systemInsets = mSystemInsets;
-        systemInsets.left = system.left-frame.left;
-        systemInsets.top = system.top-frame.top;
-        systemInsets.right = frame.right-system.right;
-        systemInsets.bottom = frame.bottom-system.bottom;
-
         final Rect contentInsets = mContentInsets;
         contentInsets.left = content.left-frame.left;
         contentInsets.top = content.top-frame.top;
@@ -485,7 +470,6 @@
             // If there is a size compatibility scale being applied to the
             // window, we need to apply this to its insets so that they are
             // reported to the app in its coordinate space.
-            systemInsets.scale(mInvGlobalScale);
             contentInsets.scale(mInvGlobalScale);
             visibleInsets.scale(mInvGlobalScale);
 
@@ -506,7 +490,6 @@
                         + mRequestedWidth + ", mRequestedheight="
                         + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
                         + "): frame=" + mFrame.toShortString()
-                        + " si=" + systemInsets.toShortString()
                         + " ci=" + contentInsets.toShortString()
                         + " vi=" + visibleInsets.toShortString());
             //}
@@ -529,11 +512,6 @@
     }
 
     @Override
-    public Rect getSystemFrameLw() {
-        return mSystemFrame;
-    }
-
-    @Override
     public Rect getContentFrameLw() {
         return mContentFrame;
     }
@@ -1067,6 +1045,9 @@
             pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
                     pw.print(" last="); mLastFrame.printShortString(pw);
                     pw.println();
+            pw.print(prefix); pw.print("mSystemDecorRect="); mSystemDecorRect.printShortString(pw);
+                    pw.print(" last="); mLastSystemDecorRect.printShortString(pw);
+                    pw.println();
         }
         if (mEnforceSizeCompat) {
             pw.print(prefix); pw.print("mCompatFrame="); mCompatFrame.printShortString(pw);
@@ -1078,16 +1059,15 @@
                     pw.print(" parent="); mParentFrame.printShortString(pw);
                     pw.print(" display="); mDisplayFrame.printShortString(pw);
                     pw.println();
-            pw.print(prefix); pw.print("    system="); mSystemFrame.printShortString(pw);
-                    pw.print(" content="); mContentFrame.printShortString(pw);
+            pw.print(prefix); pw.print("    content="); mContentFrame.printShortString(pw);
                     pw.print(" visible="); mVisibleFrame.printShortString(pw);
                     pw.println();
-            pw.print(prefix); pw.print("Cur insets: system="); mSystemInsets.printShortString(pw);
-                    pw.print(" content="); mContentInsets.printShortString(pw);;
+            pw.print(prefix); pw.print("Cur insets: content=");
+                    mContentInsets.printShortString(pw);
                     pw.print(" visible="); mVisibleInsets.printShortString(pw);
                     pw.println();
-            pw.print(prefix); pw.print("Lst insets: system="); mLastSystemInsets.printShortString(pw);
-                    pw.print(" content="); mLastContentInsets.printShortString(pw);;
+            pw.print(prefix); pw.print("Lst insets: content=");
+                    mLastContentInsets.printShortString(pw);
                     pw.print(" visible="); mLastVisibleInsets.printShortString(pw);
                     pw.println();
         }
diff --git a/services/java/com/android/server/wm/WindowStateAnimator.java b/services/java/com/android/server/wm/WindowStateAnimator.java
index 293d3e8..0aa1b45 100644
--- a/services/java/com/android/server/wm/WindowStateAnimator.java
+++ b/services/java/com/android/server/wm/WindowStateAnimator.java
@@ -436,8 +436,9 @@
 
         private float mSurfaceTraceAlpha = 0;
         private int mLayer;
-        private PointF mPosition = new PointF();
-        private Point mSize;
+        private final PointF mPosition = new PointF();
+        private final Point mSize = new Point();
+        private final Rect mWindowCrop = new Rect();
         private boolean mShown = false;
         private String mName = "Not named";
 
@@ -445,7 +446,7 @@
                        int pid, int display, int w, int h, int format, int flags) throws
                        OutOfResourcesException {
             super(s, pid, display, w, h, format, flags);
-            mSize = new Point(w, h);
+            mSize.set(w, h);
             Slog.v(SURFACE_TAG, "ctor: " + this + ". Called by "
                     + Debug.getCallers(3));
         }
@@ -455,7 +456,7 @@
                    throws OutOfResourcesException {
             super(s, pid, name, display, w, h, format, flags);
             mName = name;
-            mSize = new Point(w, h);
+            mSize.set(w, h);
             Slog.v(SURFACE_TAG, "ctor: " + this + ". Called by "
                     + Debug.getCallers(3));
         }
@@ -489,7 +490,7 @@
         @Override
         public void setPosition(float x, float y) {
             super.setPosition(x, y);
-            mPosition = new PointF(x, y);
+            mPosition.set(x, y);
             Slog.v(SURFACE_TAG, "setPosition: " + this + ". Called by "
                     + Debug.getCallers(3));
         }
@@ -497,12 +498,20 @@
         @Override
         public void setSize(int w, int h) {
             super.setSize(w, h);
-            mSize = new Point(w, h);
+            mSize.set(w, h);
             Slog.v(SURFACE_TAG, "setSize: " + this + ". Called by "
                     + Debug.getCallers(3));
         }
 
         @Override
+        public void setWindowCrop(Rect crop) {
+            super.setWindowCrop(crop);
+            mWindowCrop.set(crop);
+            Slog.v(SURFACE_TAG, "setWindowCrop: " + this + ". Called by "
+                    + Debug.getCallers(3));
+        }
+
+        @Override
         public void hide() {
             super.hide();
             mShown = false;
@@ -545,7 +554,8 @@
             return "Surface " + Integer.toHexString(System.identityHashCode(this)) + " "
                     + mName + ": shown=" + mShown + " layer=" + mLayer
                     + " alpha=" + mSurfaceTraceAlpha + " " + mPosition.x + "," + mPosition.y
-                    + " " + mSize.x + "x" + mSize.y;
+                    + " " + mSize.x + "x" + mSize.y
+                    + " crop=" + mWindowCrop.toShortString();
         }
     }
 
@@ -596,6 +606,7 @@
             mSurfaceY = 0;
             mSurfaceW = w;
             mSurfaceH = h;
+            mWin.mLastSystemDecorRect.set(0, 0, 0, 0);
             try {
                 final boolean isHwAccelerated = (attrs.flags &
                         WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0;
@@ -991,6 +1002,55 @@
                 }
             }
         }
+
+        // Need to recompute a new system decor rect each time.
+        if ((w.mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
+            // Currently can't do this cropping for scaled windows.  We'll
+            // just keep the crop rect the same as the source surface.
+            w.mSystemDecorRect.set(0, 0, w.mRequestedWidth, w.mRequestedHeight);
+        } else if (w.mLayer >= mService.mSystemDecorLayer) {
+            // Above the decor layer is easy, just use the entire window.
+            w.mSystemDecorRect.set(0, 0, w.mCompatFrame.width(),
+                    w.mCompatFrame.height());
+        } else {
+            final Rect decorRect = mService.mSystemDecorRect;
+            // Compute the offset of the window in relation to the decor rect.
+            final int offX = w.mXOffset + w.mFrame.left;
+            final int offY = w.mYOffset + w.mFrame.top;
+            // Initialize the decor rect to the entire frame.
+            w.mSystemDecorRect.set(0, 0, w.mFrame.width(), w.mFrame.height());
+            // Intersect with the decor rect, offsetted by window position.
+            w.mSystemDecorRect.intersect(decorRect.left-offX, decorRect.top-offY,
+                    decorRect.right-offX, decorRect.bottom-offY);
+            // If size compatibility is being applied to the window, the
+            // surface is scaled relative to the screen.  Also apply this
+            // scaling to the crop rect.  We aren't using the standard rect
+            // scale function because we want to round things to make the crop
+            // always round to a larger rect to ensure we don't crop too
+            // much and hide part of the window that should be seen.
+            if (w.mEnforceSizeCompat && w.mInvGlobalScale != 1.0f) {
+                final float scale = w.mInvGlobalScale;
+                w.mSystemDecorRect.left = (int) (w.mSystemDecorRect.left * scale - 0.5f);
+                w.mSystemDecorRect.top = (int) (w.mSystemDecorRect.top * scale - 0.5f);
+                w.mSystemDecorRect.right = (int) ((w.mSystemDecorRect.right+1) * scale - 0.5f);
+                w.mSystemDecorRect.bottom = (int) ((w.mSystemDecorRect.bottom+1) * scale - 0.5f);
+            }
+        }
+
+        if (!w.mSystemDecorRect.equals(w.mLastSystemDecorRect)) {
+            w.mLastSystemDecorRect.set(w.mSystemDecorRect);
+            try {
+                if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
+                        "CROP " + w.mSystemDecorRect.toShortString(), null);
+                mSurface.setWindowCrop(w.mSystemDecorRect);
+            } catch (RuntimeException e) {
+                Slog.w(TAG, "Error setting crop surface of " + w
+                        + " crop=" + w.mSystemDecorRect.toShortString(), e);
+                if (!recoveringMemory) {
+                    mService.reclaimSomeSurfaceMemoryLocked(this, "crop", true);
+                }
+            }
+        }
     }
 
     public void prepareSurfaceLocked(final boolean recoveringMemory) {
diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeWindow.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeWindow.java
index c44ddc6..379fb81 100644
--- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeWindow.java
+++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeWindow.java
@@ -47,7 +47,7 @@
     }
 
     @Override
-    public void resized(int arg0, int arg1, Rect argBlah, Rect arg2, Rect arg3,
+    public void resized(int arg0, int arg1, Rect arg2, Rect arg3,
             boolean arg4, Configuration arg5) throws RemoteException {
         // pass for now.
     }