Home | History | Annotate | Download | only in wm
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.server.wm;
     18 
     19 import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW;
     20 import static android.view.WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW;
     21 import static android.view.WindowManager.LayoutParams.LAST_SUB_WINDOW;
     22 import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_NO_MOVE_ANIMATION;
     23 import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
     24 import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
     25 import static android.view.WindowManager.LayoutParams.TYPE_KEYGUARD;
     26 import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
     27 
     28 import android.app.AppOpsManager;
     29 import android.os.RemoteCallbackList;
     30 import android.util.TimeUtils;
     31 import android.view.IWindowFocusObserver;
     32 import android.view.IWindowId;
     33 import com.android.server.input.InputWindowHandle;
     34 
     35 import android.content.Context;
     36 import android.content.res.Configuration;
     37 import android.graphics.Matrix;
     38 import android.graphics.PixelFormat;
     39 import android.graphics.Rect;
     40 import android.graphics.RectF;
     41 import android.graphics.Region;
     42 import android.os.IBinder;
     43 import android.os.RemoteException;
     44 import android.os.UserHandle;
     45 import android.util.Slog;
     46 import android.view.DisplayInfo;
     47 import android.view.Gravity;
     48 import android.view.IApplicationToken;
     49 import android.view.IWindow;
     50 import android.view.InputChannel;
     51 import android.view.View;
     52 import android.view.ViewTreeObserver;
     53 import android.view.WindowManager;
     54 import android.view.WindowManagerPolicy;
     55 
     56 import java.io.PrintWriter;
     57 import java.util.ArrayList;
     58 
     59 class WindowList extends ArrayList<WindowState> {
     60 }
     61 
     62 /**
     63  * A window in the window manager.
     64  */
     65 final class WindowState implements WindowManagerPolicy.WindowState {
     66     static final String TAG = "WindowState";
     67 
     68     static final boolean DEBUG_VISIBILITY = WindowManagerService.DEBUG_VISIBILITY;
     69     static final boolean SHOW_TRANSACTIONS = WindowManagerService.SHOW_TRANSACTIONS;
     70     static final boolean SHOW_LIGHT_TRANSACTIONS = WindowManagerService.SHOW_LIGHT_TRANSACTIONS;
     71     static final boolean SHOW_SURFACE_ALLOC = WindowManagerService.SHOW_SURFACE_ALLOC;
     72 
     73     final WindowManagerService mService;
     74     final WindowManagerPolicy mPolicy;
     75     final Context mContext;
     76     final Session mSession;
     77     final IWindow mClient;
     78     final int mAppOp;
     79     // UserId and appId of the owner. Don't display windows of non-current user.
     80     final int mOwnerUid;
     81     final IWindowId mWindowId;
     82     WindowToken mToken;
     83     WindowToken mRootToken;
     84     AppWindowToken mAppToken;
     85     AppWindowToken mTargetAppToken;
     86 
     87     // mAttrs.flags is tested in animation without being locked. If the bits tested are ever
     88     // modified they will need to be locked.
     89     final WindowManager.LayoutParams mAttrs = new WindowManager.LayoutParams();
     90     final DeathRecipient mDeathRecipient;
     91     final WindowState mAttachedWindow;
     92     final WindowList mChildWindows = new WindowList();
     93     final int mBaseLayer;
     94     final int mSubLayer;
     95     final boolean mLayoutAttached;
     96     final boolean mIsImWindow;
     97     final boolean mIsWallpaper;
     98     final boolean mIsFloatingLayer;
     99     int mSeq;
    100     boolean mEnforceSizeCompat;
    101     int mViewVisibility;
    102     int mSystemUiVisibility;
    103     boolean mPolicyVisibility = true;
    104     boolean mPolicyVisibilityAfterAnim = true;
    105     boolean mAppOpVisibility = true;
    106     boolean mAppFreezing;
    107     boolean mAttachedHidden;    // is our parent window hidden?
    108     boolean mWallpaperVisible;  // for wallpaper, what was last vis report?
    109 
    110     RemoteCallbackList<IWindowFocusObserver> mFocusCallbacks;
    111 
    112     /**
    113      * The window size that was requested by the application.  These are in
    114      * the application's coordinate space (without compatibility scale applied).
    115      */
    116     int mRequestedWidth;
    117     int mRequestedHeight;
    118     int mLastRequestedWidth;
    119     int mLastRequestedHeight;
    120 
    121     int mLayer;
    122     boolean mHaveFrame;
    123     boolean mObscured;
    124     boolean mTurnOnScreen;
    125 
    126     int mLayoutSeq = -1;
    127 
    128     Configuration mConfiguration = null;
    129     // Sticky answer to isConfigChanged(), remains true until new Configuration is assigned.
    130     // Used only on {@link #TYPE_KEYGUARD}.
    131     private boolean mConfigHasChanged;
    132 
    133     /**
    134      * Actual frame shown on-screen (may be modified by animation).  These
    135      * are in the screen's coordinate space (WITH the compatibility scale
    136      * applied).
    137      */
    138     final RectF mShownFrame = new RectF();
    139 
    140     /**
    141      * Insets that determine the actually visible area.  These are in the application's
    142      * coordinate space (without compatibility scale applied).
    143      */
    144     final Rect mVisibleInsets = new Rect();
    145     final Rect mLastVisibleInsets = new Rect();
    146     boolean mVisibleInsetsChanged;
    147 
    148     /**
    149      * Insets that are covered by system windows (such as the status bar) and
    150      * transient docking windows (such as the IME).  These are in the application's
    151      * coordinate space (without compatibility scale applied).
    152      */
    153     final Rect mContentInsets = new Rect();
    154     final Rect mLastContentInsets = new Rect();
    155     boolean mContentInsetsChanged;
    156 
    157     /**
    158      * Insets that determine the area covered by the display overscan region.  These are in the
    159      * application's coordinate space (without compatibility scale applied).
    160      */
    161     final Rect mOverscanInsets = new Rect();
    162     final Rect mLastOverscanInsets = new Rect();
    163     boolean mOverscanInsetsChanged;
    164 
    165     /**
    166      * Set to true if we are waiting for this window to receive its
    167      * given internal insets before laying out other windows based on it.
    168      */
    169     boolean mGivenInsetsPending;
    170 
    171     /**
    172      * These are the content insets that were given during layout for
    173      * this window, to be applied to windows behind it.
    174      */
    175     final Rect mGivenContentInsets = new Rect();
    176 
    177     /**
    178      * These are the visible insets that were given during layout for
    179      * this window, to be applied to windows behind it.
    180      */
    181     final Rect mGivenVisibleInsets = new Rect();
    182 
    183     /**
    184      * This is the given touchable area relative to the window frame, or null if none.
    185      */
    186     final Region mGivenTouchableRegion = new Region();
    187 
    188     /**
    189      * Flag indicating whether the touchable region should be adjusted by
    190      * the visible insets; if false the area outside the visible insets is
    191      * NOT touchable, so we must use those to adjust the frame during hit
    192      * tests.
    193      */
    194     int mTouchableInsets = ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
    195 
    196     /**
    197      * This is rectangle of the window's surface that is not covered by
    198      * system decorations.
    199      */
    200     final Rect mSystemDecorRect = new Rect();
    201     final Rect mLastSystemDecorRect = new Rect();
    202 
    203     // Current transformation being applied.
    204     float mGlobalScale=1;
    205     float mInvGlobalScale=1;
    206     float mHScale=1, mVScale=1;
    207     float mLastHScale=1, mLastVScale=1;
    208     final Matrix mTmpMatrix = new Matrix();
    209 
    210     // "Real" frame that the application sees, in display coordinate space.
    211     final Rect mFrame = new Rect();
    212     final Rect mLastFrame = new Rect();
    213     // Frame that is scaled to the application's coordinate space when in
    214     // screen size compatibility mode.
    215     final Rect mCompatFrame = new Rect();
    216 
    217     final Rect mContainingFrame = new Rect();
    218     final Rect mDisplayFrame = new Rect();
    219     final Rect mOverscanFrame = new Rect();
    220     final Rect mContentFrame = new Rect();
    221     final Rect mParentFrame = new Rect();
    222     final Rect mVisibleFrame = new Rect();
    223 
    224     boolean mContentChanged;
    225 
    226     // If a window showing a wallpaper: the requested offset for the
    227     // wallpaper; if a wallpaper window: the currently applied offset.
    228     float mWallpaperX = -1;
    229     float mWallpaperY = -1;
    230 
    231     // If a window showing a wallpaper: what fraction of the offset
    232     // range corresponds to a full virtual screen.
    233     float mWallpaperXStep = -1;
    234     float mWallpaperYStep = -1;
    235 
    236     // Wallpaper windows: pixels offset based on above variables.
    237     int mXOffset;
    238     int mYOffset;
    239 
    240     /**
    241      * This is set after IWindowSession.relayout() has been called at
    242      * least once for the window.  It allows us to detect the situation
    243      * where we don't yet have a surface, but should have one soon, so
    244      * we can give the window focus before waiting for the relayout.
    245      */
    246     boolean mRelayoutCalled;
    247 
    248     /**
    249      * If the application has called relayout() with changes that can
    250      * impact its window's size, we need to perform a layout pass on it
    251      * even if it is not currently visible for layout.  This is set
    252      * when in that case until the layout is done.
    253      */
    254     boolean mLayoutNeeded;
    255 
    256     /** Currently running an exit animation? */
    257     boolean mExiting;
    258 
    259     /** Currently on the mDestroySurface list? */
    260     boolean mDestroying;
    261 
    262     /** Completely remove from window manager after exit animation? */
    263     boolean mRemoveOnExit;
    264 
    265     /**
    266      * Set when the orientation is changing and this window has not yet
    267      * been updated for the new orientation.
    268      */
    269     boolean mOrientationChanging;
    270 
    271     /**
    272      * How long we last kept the screen frozen.
    273      */
    274     int mLastFreezeDuration;
    275 
    276     /** Is this window now (or just being) removed? */
    277     boolean mRemoved;
    278 
    279     /**
    280      * Temp for keeping track of windows that have been removed when
    281      * rebuilding window list.
    282      */
    283     boolean mRebuilding;
    284 
    285     // Input channel and input window handle used by the input dispatcher.
    286     final InputWindowHandle mInputWindowHandle;
    287     InputChannel mInputChannel;
    288 
    289     // Used to improve performance of toString()
    290     String mStringNameCache;
    291     CharSequence mLastTitle;
    292     boolean mWasExiting;
    293 
    294     final WindowStateAnimator mWinAnimator;
    295 
    296     boolean mHasSurface = false;
    297 
    298     DisplayContent  mDisplayContent;
    299 
    300     /** When true this window can be displayed on screens owther than mOwnerUid's */
    301     private boolean mShowToOwnerOnly;
    302 
    303     WindowState(WindowManagerService service, Session s, IWindow c, WindowToken token,
    304            WindowState attachedWindow, int appOp, int seq, WindowManager.LayoutParams a,
    305            int viewVisibility, final DisplayContent displayContent) {
    306         mService = service;
    307         mSession = s;
    308         mClient = c;
    309         mAppOp = appOp;
    310         mToken = token;
    311         mOwnerUid = s.mUid;
    312         mWindowId = new IWindowId.Stub() {
    313             @Override
    314             public void registerFocusObserver(IWindowFocusObserver observer) {
    315                 WindowState.this.registerFocusObserver(observer);
    316             }
    317             @Override
    318             public void unregisterFocusObserver(IWindowFocusObserver observer) {
    319                 WindowState.this.unregisterFocusObserver(observer);
    320             }
    321             @Override
    322             public boolean isFocused() {
    323                 return WindowState.this.isFocused();
    324             }
    325         };
    326         mAttrs.copyFrom(a);
    327         mViewVisibility = viewVisibility;
    328         mDisplayContent = displayContent;
    329         mPolicy = mService.mPolicy;
    330         mContext = mService.mContext;
    331         DeathRecipient deathRecipient = new DeathRecipient();
    332         mSeq = seq;
    333         mEnforceSizeCompat = (mAttrs.flags & FLAG_COMPATIBLE_WINDOW) != 0;
    334         if (WindowManagerService.localLOGV) Slog.v(
    335             TAG, "Window " + this + " client=" + c.asBinder()
    336             + " token=" + token + " (" + mAttrs.token + ")" + " params=" + a);
    337         try {
    338             c.asBinder().linkToDeath(deathRecipient, 0);
    339         } catch (RemoteException e) {
    340             mDeathRecipient = null;
    341             mAttachedWindow = null;
    342             mLayoutAttached = false;
    343             mIsImWindow = false;
    344             mIsWallpaper = false;
    345             mIsFloatingLayer = false;
    346             mBaseLayer = 0;
    347             mSubLayer = 0;
    348             mInputWindowHandle = null;
    349             mWinAnimator = null;
    350             return;
    351         }
    352         mDeathRecipient = deathRecipient;
    353 
    354         if ((mAttrs.type >= FIRST_SUB_WINDOW &&
    355                 mAttrs.type <= LAST_SUB_WINDOW)) {
    356             // The multiplier here is to reserve space for multiple
    357             // windows in the same type layer.
    358             mBaseLayer = mPolicy.windowTypeToLayerLw(
    359                     attachedWindow.mAttrs.type) * WindowManagerService.TYPE_LAYER_MULTIPLIER
    360                     + WindowManagerService.TYPE_LAYER_OFFSET;
    361             mSubLayer = mPolicy.subWindowTypeToLayerLw(a.type);
    362             mAttachedWindow = attachedWindow;
    363             if (WindowManagerService.DEBUG_ADD_REMOVE) Slog.v(TAG, "Adding " + this + " to " + mAttachedWindow);
    364             mAttachedWindow.mChildWindows.add(this);
    365             mLayoutAttached = mAttrs.type !=
    366                     WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
    367             mIsImWindow = attachedWindow.mAttrs.type == TYPE_INPUT_METHOD
    368                     || attachedWindow.mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
    369             mIsWallpaper = attachedWindow.mAttrs.type == TYPE_WALLPAPER;
    370             mIsFloatingLayer = mIsImWindow || mIsWallpaper;
    371         } else {
    372             // The multiplier here is to reserve space for multiple
    373             // windows in the same type layer.
    374             mBaseLayer = mPolicy.windowTypeToLayerLw(a.type)
    375                     * WindowManagerService.TYPE_LAYER_MULTIPLIER
    376                     + WindowManagerService.TYPE_LAYER_OFFSET;
    377             mSubLayer = 0;
    378             mAttachedWindow = null;
    379             mLayoutAttached = false;
    380             mIsImWindow = mAttrs.type == TYPE_INPUT_METHOD
    381                     || mAttrs.type == TYPE_INPUT_METHOD_DIALOG;
    382             mIsWallpaper = mAttrs.type == TYPE_WALLPAPER;
    383             mIsFloatingLayer = mIsImWindow || mIsWallpaper;
    384         }
    385 
    386         WindowState appWin = this;
    387         while (appWin.mAttachedWindow != null) {
    388             appWin = appWin.mAttachedWindow;
    389         }
    390         WindowToken appToken = appWin.mToken;
    391         while (appToken.appWindowToken == null) {
    392             WindowToken parent = mService.mTokenMap.get(appToken.token);
    393             if (parent == null || appToken == parent) {
    394                 break;
    395             }
    396             appToken = parent;
    397         }
    398         mRootToken = appToken;
    399         mAppToken = appToken.appWindowToken;
    400 
    401         mWinAnimator = new WindowStateAnimator(this);
    402         mWinAnimator.mAlpha = a.alpha;
    403 
    404         mRequestedWidth = 0;
    405         mRequestedHeight = 0;
    406         mLastRequestedWidth = 0;
    407         mLastRequestedHeight = 0;
    408         mXOffset = 0;
    409         mYOffset = 0;
    410         mLayer = 0;
    411         mInputWindowHandle = new InputWindowHandle(
    412                 mAppToken != null ? mAppToken.mInputApplicationHandle : null, this,
    413                 displayContent.getDisplayId());
    414     }
    415 
    416     void attach() {
    417         if (WindowManagerService.localLOGV) Slog.v(
    418             TAG, "Attaching " + this + " token=" + mToken
    419             + ", list=" + mToken.windows);
    420         mSession.windowAddedLocked();
    421     }
    422 
    423     @Override
    424     public int getOwningUid() {
    425         return mOwnerUid;
    426     }
    427 
    428     @Override
    429     public String getOwningPackage() {
    430         return mAttrs.packageName;
    431     }
    432 
    433     @Override
    434     public void computeFrameLw(Rect pf, Rect df, Rect of, Rect cf, Rect vf) {
    435         mHaveFrame = true;
    436 
    437         final Rect container = mContainingFrame;
    438         container.set(pf);
    439 
    440         final Rect display = mDisplayFrame;
    441         display.set(df);
    442 
    443         final int pw = container.right - container.left;
    444         final int ph = container.bottom - container.top;
    445 
    446         int w,h;
    447         if ((mAttrs.flags & WindowManager.LayoutParams.FLAG_SCALED) != 0) {
    448             if (mAttrs.width < 0) {
    449                 w = pw;
    450             } else if (mEnforceSizeCompat) {
    451                 w = (int)(mAttrs.width * mGlobalScale + .5f);
    452             } else {
    453                 w = mAttrs.width;
    454             }
    455             if (mAttrs.height < 0) {
    456                 h = ph;
    457             } else if (mEnforceSizeCompat) {
    458                 h = (int)(mAttrs.height * mGlobalScale + .5f);
    459             } else {
    460                 h = mAttrs.height;
    461             }
    462         } else {
    463             if (mAttrs.width == WindowManager.LayoutParams.MATCH_PARENT) {
    464                 w = pw;
    465             } else if (mEnforceSizeCompat) {
    466                 w = (int)(mRequestedWidth * mGlobalScale + .5f);
    467             } else {
    468                 w = mRequestedWidth;
    469             }
    470             if (mAttrs.height == WindowManager.LayoutParams.MATCH_PARENT) {
    471                 h = ph;
    472             } else if (mEnforceSizeCompat) {
    473                 h = (int)(mRequestedHeight * mGlobalScale + .5f);
    474             } else {
    475                 h = mRequestedHeight;
    476             }
    477         }
    478 
    479         if (!mParentFrame.equals(pf)) {
    480             //Slog.i(TAG, "Window " + this + " content frame from " + mParentFrame
    481             //        + " to " + pf);
    482             mParentFrame.set(pf);
    483             mContentChanged = true;
    484         }
    485         if (mRequestedWidth != mLastRequestedWidth || mRequestedHeight != mLastRequestedHeight) {
    486             mLastRequestedWidth = mRequestedWidth;
    487             mLastRequestedHeight = mRequestedHeight;
    488             mContentChanged = true;
    489         }
    490 
    491         final Rect overscan = mOverscanFrame;
    492         overscan.set(of);
    493 
    494         final Rect content = mContentFrame;
    495         content.set(cf);
    496 
    497         final Rect visible = mVisibleFrame;
    498         visible.set(vf);
    499 
    500         final Rect frame = mFrame;
    501         final int fw = frame.width();
    502         final int fh = frame.height();
    503 
    504         //System.out.println("In: w=" + w + " h=" + h + " container=" +
    505         //                   container + " x=" + mAttrs.x + " y=" + mAttrs.y);
    506 
    507         float x, y;
    508         if (mEnforceSizeCompat) {
    509             x = mAttrs.x * mGlobalScale;
    510             y = mAttrs.y * mGlobalScale;
    511         } else {
    512             x = mAttrs.x;
    513             y = mAttrs.y;
    514         }
    515 
    516         Gravity.apply(mAttrs.gravity, w, h, container,
    517                 (int) (x + mAttrs.horizontalMargin * pw),
    518                 (int) (y + mAttrs.verticalMargin * ph), frame);
    519 
    520         //System.out.println("Out: " + mFrame);
    521 
    522         // Now make sure the window fits in the overall display.
    523         Gravity.applyDisplay(mAttrs.gravity, df, frame);
    524 
    525         // Make sure the content and visible frames are inside of the
    526         // final window frame.
    527         if (content.left < frame.left) content.left = frame.left;
    528         if (content.top < frame.top) content.top = frame.top;
    529         if (content.right > frame.right) content.right = frame.right;
    530         if (content.bottom > frame.bottom) content.bottom = frame.bottom;
    531         if (visible.left < frame.left) visible.left = frame.left;
    532         if (visible.top < frame.top) visible.top = frame.top;
    533         if (visible.right > frame.right) visible.right = frame.right;
    534         if (visible.bottom > frame.bottom) visible.bottom = frame.bottom;
    535 
    536         final Rect overscanInsets = mOverscanInsets;
    537         overscanInsets.left = overscan.left > frame.left ? overscan.left-frame.left : 0;
    538         overscanInsets.top = overscan.top > frame.top ? overscan.top-frame.top : 0;
    539         overscanInsets.right = overscan.right < frame.right ? frame.right-overscan.right : 0;
    540         overscanInsets.bottom = overscan.bottom < frame.bottom ? frame.bottom-overscan.bottom : 0;
    541 
    542         final Rect contentInsets = mContentInsets;
    543         contentInsets.left = content.left-frame.left;
    544         contentInsets.top = content.top-frame.top;
    545         contentInsets.right = frame.right-content.right;
    546         contentInsets.bottom = frame.bottom-content.bottom;
    547 
    548         final Rect visibleInsets = mVisibleInsets;
    549         visibleInsets.left = visible.left-frame.left;
    550         visibleInsets.top = visible.top-frame.top;
    551         visibleInsets.right = frame.right-visible.right;
    552         visibleInsets.bottom = frame.bottom-visible.bottom;
    553 
    554         mCompatFrame.set(frame);
    555         if (mEnforceSizeCompat) {
    556             // If there is a size compatibility scale being applied to the
    557             // window, we need to apply this to its insets so that they are
    558             // reported to the app in its coordinate space.
    559             overscanInsets.scale(mInvGlobalScale);
    560             contentInsets.scale(mInvGlobalScale);
    561             visibleInsets.scale(mInvGlobalScale);
    562 
    563             // Also the scaled frame that we report to the app needs to be
    564             // adjusted to be in its coordinate space.
    565             mCompatFrame.scale(mInvGlobalScale);
    566         }
    567 
    568         if (mIsWallpaper && (fw != frame.width() || fh != frame.height())) {
    569             final DisplayInfo displayInfo = mDisplayContent.getDisplayInfo();
    570             mService.updateWallpaperOffsetLocked(this, displayInfo.appWidth, displayInfo.appHeight,
    571                     false);
    572         }
    573 
    574         if (WindowManagerService.localLOGV) {
    575             //if ("com.google.android.youtube".equals(mAttrs.packageName)
    576             //        && mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) {
    577                 Slog.v(TAG, "Resolving (mRequestedWidth="
    578                         + mRequestedWidth + ", mRequestedheight="
    579                         + mRequestedHeight + ") to" + " (pw=" + pw + ", ph=" + ph
    580                         + "): frame=" + mFrame.toShortString()
    581                         + " ci=" + contentInsets.toShortString()
    582                         + " vi=" + visibleInsets.toShortString());
    583             //}
    584         }
    585     }
    586 
    587     @Override
    588     public Rect getFrameLw() {
    589         return mFrame;
    590     }
    591 
    592     @Override
    593     public RectF getShownFrameLw() {
    594         return mShownFrame;
    595     }
    596 
    597     @Override
    598     public Rect getDisplayFrameLw() {
    599         return mDisplayFrame;
    600     }
    601 
    602     @Override
    603     public Rect getOverscanFrameLw() {
    604         return mOverscanFrame;
    605     }
    606 
    607     @Override
    608     public Rect getContentFrameLw() {
    609         return mContentFrame;
    610     }
    611 
    612     @Override
    613     public Rect getVisibleFrameLw() {
    614         return mVisibleFrame;
    615     }
    616 
    617     @Override
    618     public boolean getGivenInsetsPendingLw() {
    619         return mGivenInsetsPending;
    620     }
    621 
    622     @Override
    623     public Rect getGivenContentInsetsLw() {
    624         return mGivenContentInsets;
    625     }
    626 
    627     @Override
    628     public Rect getGivenVisibleInsetsLw() {
    629         return mGivenVisibleInsets;
    630     }
    631 
    632     @Override
    633     public WindowManager.LayoutParams getAttrs() {
    634         return mAttrs;
    635     }
    636 
    637     @Override
    638     public boolean getNeedsMenuLw(WindowManagerPolicy.WindowState bottom) {
    639         int index = -1;
    640         WindowState ws = this;
    641         WindowList windows = getWindowList();
    642         while (true) {
    643             if ((ws.mAttrs.privateFlags
    644                     & WindowManager.LayoutParams.PRIVATE_FLAG_SET_NEEDS_MENU_KEY) != 0) {
    645                 return (ws.mAttrs.flags & WindowManager.LayoutParams.FLAG_NEEDS_MENU_KEY) != 0;
    646             }
    647             // If we reached the bottom of the range of windows we are considering,
    648             // assume no menu is needed.
    649             if (ws == bottom) {
    650                 return false;
    651             }
    652             // The current window hasn't specified whether menu key is needed;
    653             // look behind it.
    654             // First, we may need to determine the starting position.
    655             if (index < 0) {
    656                 index = windows.indexOf(ws);
    657             }
    658             index--;
    659             if (index < 0) {
    660                 return false;
    661             }
    662             ws = windows.get(index);
    663         }
    664     }
    665 
    666     @Override
    667     public int getSystemUiVisibility() {
    668         return mSystemUiVisibility;
    669     }
    670 
    671     @Override
    672     public int getSurfaceLayer() {
    673         return mLayer;
    674     }
    675 
    676     @Override
    677     public IApplicationToken getAppToken() {
    678         return mAppToken != null ? mAppToken.appToken : null;
    679     }
    680 
    681     public int getDisplayId() {
    682         return mDisplayContent.getDisplayId();
    683     }
    684 
    685     public long getInputDispatchingTimeoutNanos() {
    686         return mAppToken != null
    687                 ? mAppToken.inputDispatchingTimeoutNanos
    688                 : WindowManagerService.DEFAULT_INPUT_DISPATCHING_TIMEOUT_NANOS;
    689     }
    690 
    691     @Override
    692     public boolean hasAppShownWindows() {
    693         return mAppToken != null && (mAppToken.firstWindowDrawn || mAppToken.startingDisplayed);
    694     }
    695 
    696     boolean isIdentityMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
    697         if (dsdx < .99999f || dsdx > 1.00001f) return false;
    698         if (dtdy < .99999f || dtdy > 1.00001f) return false;
    699         if (dtdx < -.000001f || dtdx > .000001f) return false;
    700         if (dsdy < -.000001f || dsdy > .000001f) return false;
    701         return true;
    702     }
    703 
    704     void prelayout() {
    705         if (mEnforceSizeCompat) {
    706             mGlobalScale = mService.mCompatibleScreenScale;
    707             mInvGlobalScale = 1/mGlobalScale;
    708         } else {
    709             mGlobalScale = mInvGlobalScale = 1;
    710         }
    711     }
    712 
    713     /**
    714      * Is this window visible?  It is not visible if there is no
    715      * surface, or we are in the process of running an exit animation
    716      * that will remove the surface, or its app token has been hidden.
    717      */
    718     @Override
    719     public boolean isVisibleLw() {
    720         final AppWindowToken atoken = mAppToken;
    721         return mHasSurface && mPolicyVisibility && !mAttachedHidden
    722                 && (atoken == null || !atoken.hiddenRequested)
    723                 && !mExiting && !mDestroying;
    724     }
    725 
    726     /**
    727      * Like {@link #isVisibleLw}, but also counts a window that is currently
    728      * "hidden" behind the keyguard as visible.  This allows us to apply
    729      * things like window flags that impact the keyguard.
    730      * XXX I am starting to think we need to have ANOTHER visibility flag
    731      * for this "hidden behind keyguard" state rather than overloading
    732      * mPolicyVisibility.  Ungh.
    733      */
    734     @Override
    735     public boolean isVisibleOrBehindKeyguardLw() {
    736         if (mRootToken.waitingToShow &&
    737                 mService.mAppTransition.isTransitionSet()) {
    738             return false;
    739         }
    740         final AppWindowToken atoken = mAppToken;
    741         final boolean animating = atoken != null
    742                 ? (atoken.mAppAnimator.animation != null) : false;
    743         return mHasSurface && !mDestroying && !mExiting
    744                 && (atoken == null ? mPolicyVisibility : !atoken.hiddenRequested)
    745                 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
    746                                 && !mRootToken.hidden)
    747                         || mWinAnimator.mAnimation != null || animating);
    748     }
    749 
    750     /**
    751      * Is this window visible, ignoring its app token?  It is not visible
    752      * if there is no surface, or we are in the process of running an exit animation
    753      * that will remove the surface.
    754      */
    755     public boolean isWinVisibleLw() {
    756         final AppWindowToken atoken = mAppToken;
    757         return mHasSurface && mPolicyVisibility && !mAttachedHidden
    758                 && (atoken == null || !atoken.hiddenRequested || atoken.mAppAnimator.animating)
    759                 && !mExiting && !mDestroying;
    760     }
    761 
    762     /**
    763      * The same as isVisible(), but follows the current hidden state of
    764      * the associated app token, not the pending requested hidden state.
    765      */
    766     boolean isVisibleNow() {
    767         return mHasSurface && mPolicyVisibility && !mAttachedHidden
    768                 && !mRootToken.hidden && !mExiting && !mDestroying;
    769     }
    770 
    771     /**
    772      * Can this window possibly be a drag/drop target?  The test here is
    773      * a combination of the above "visible now" with the check that the
    774      * Input Manager uses when discarding windows from input consideration.
    775      */
    776     boolean isPotentialDragTarget() {
    777         return isVisibleNow() && !mRemoved
    778                 && mInputChannel != null && mInputWindowHandle != null;
    779     }
    780 
    781     /**
    782      * Same as isVisible(), but we also count it as visible between the
    783      * call to IWindowSession.add() and the first relayout().
    784      */
    785     boolean isVisibleOrAdding() {
    786         final AppWindowToken atoken = mAppToken;
    787         return (mHasSurface || (!mRelayoutCalled && mViewVisibility == View.VISIBLE))
    788                 && mPolicyVisibility && !mAttachedHidden
    789                 && (atoken == null || !atoken.hiddenRequested)
    790                 && !mExiting && !mDestroying;
    791     }
    792 
    793     /**
    794      * Is this window currently on-screen?  It is on-screen either if it
    795      * is visible or it is currently running an animation before no longer
    796      * being visible.
    797      */
    798     boolean isOnScreen() {
    799         if (!mHasSurface || !mPolicyVisibility || mDestroying) {
    800             return false;
    801         }
    802         final AppWindowToken atoken = mAppToken;
    803         if (atoken != null) {
    804             return ((!mAttachedHidden && !atoken.hiddenRequested)
    805                     || mWinAnimator.mAnimation != null || atoken.mAppAnimator.animation != null);
    806         }
    807         return !mAttachedHidden || mWinAnimator.mAnimation != null;
    808     }
    809 
    810     /**
    811      * Like isOnScreen(), but we don't return true if the window is part
    812      * of a transition that has not yet been started.
    813      */
    814     boolean isReadyForDisplay() {
    815         if (mRootToken.waitingToShow &&
    816                 mService.mAppTransition.isTransitionSet()) {
    817             return false;
    818         }
    819         return mHasSurface && mPolicyVisibility && !mDestroying
    820                 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
    821                                 && !mRootToken.hidden)
    822                         || mWinAnimator.mAnimation != null
    823                         || ((mAppToken != null) && (mAppToken.mAppAnimator.animation != null)));
    824     }
    825 
    826     /**
    827      * Like isReadyForDisplay(), but ignores any force hiding of the window due
    828      * to the keyguard.
    829      */
    830     boolean isReadyForDisplayIgnoringKeyguard() {
    831         if (mRootToken.waitingToShow && mService.mAppTransition.isTransitionSet()) {
    832             return false;
    833         }
    834         final AppWindowToken atoken = mAppToken;
    835         if (atoken == null && !mPolicyVisibility) {
    836             // If this is not an app window, and the policy has asked to force
    837             // hide, then we really do want to hide.
    838             return false;
    839         }
    840         return mHasSurface && !mDestroying
    841                 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE
    842                                 && !mRootToken.hidden)
    843                         || mWinAnimator.mAnimation != null
    844                         || ((atoken != null) && (atoken.mAppAnimator.animation != null)
    845                                 && !mWinAnimator.isDummyAnimation()));
    846     }
    847 
    848     /**
    849      * Like isOnScreen, but returns false if the surface hasn't yet
    850      * been drawn.
    851      */
    852     @Override
    853     public boolean isDisplayedLw() {
    854         final AppWindowToken atoken = mAppToken;
    855         return isDrawnLw() && mPolicyVisibility
    856             && ((!mAttachedHidden &&
    857                     (atoken == null || !atoken.hiddenRequested))
    858                         || mWinAnimator.mAnimating
    859                         || (atoken != null && atoken.mAppAnimator.animation != null));
    860     }
    861 
    862     /**
    863      * Return true if this window or its app token is currently animating.
    864      */
    865     @Override
    866     public boolean isAnimatingLw() {
    867         return mWinAnimator.mAnimation != null
    868                 || (mAppToken != null && mAppToken.mAppAnimator.animation != null);
    869     }
    870 
    871     @Override
    872     public boolean isGoneForLayoutLw() {
    873         final AppWindowToken atoken = mAppToken;
    874         return mViewVisibility == View.GONE
    875                 || !mRelayoutCalled
    876                 || (atoken == null && mRootToken.hidden)
    877                 || (atoken != null && (atoken.hiddenRequested || atoken.hidden))
    878                 || mAttachedHidden
    879                 || mExiting || mDestroying;
    880     }
    881 
    882     /**
    883      * Returns true if the window has a surface that it has drawn a
    884      * complete UI in to.
    885      */
    886     public boolean isDrawFinishedLw() {
    887         return mHasSurface && !mDestroying &&
    888                 (mWinAnimator.mDrawState == WindowStateAnimator.COMMIT_DRAW_PENDING
    889                 || mWinAnimator.mDrawState == WindowStateAnimator.READY_TO_SHOW
    890                 || mWinAnimator.mDrawState == WindowStateAnimator.HAS_DRAWN);
    891     }
    892 
    893     /**
    894      * Returns true if the window has a surface that it has drawn a
    895      * complete UI in to.
    896      */
    897     public boolean isDrawnLw() {
    898         return mHasSurface && !mDestroying &&
    899                 (mWinAnimator.mDrawState == WindowStateAnimator.READY_TO_SHOW
    900                 || mWinAnimator.mDrawState == WindowStateAnimator.HAS_DRAWN);
    901     }
    902 
    903     /**
    904      * Return true if the window is opaque and fully drawn.  This indicates
    905      * it may obscure windows behind it.
    906      */
    907     boolean isOpaqueDrawn() {
    908         return (mAttrs.format == PixelFormat.OPAQUE
    909                         || mAttrs.type == TYPE_WALLPAPER)
    910                 && isDrawnLw() && mWinAnimator.mAnimation == null
    911                 && (mAppToken == null || mAppToken.mAppAnimator.animation == null);
    912     }
    913 
    914     /**
    915      * Return whether this window is wanting to have a translation
    916      * animation applied to it for an in-progress move.  (Only makes
    917      * sense to call from performLayoutAndPlaceSurfacesLockedInner().)
    918      */
    919     boolean shouldAnimateMove() {
    920         return mContentChanged && !mExiting && !mWinAnimator.mLastHidden && mService.okToDisplay()
    921                 && (mFrame.top != mLastFrame.top
    922                         || mFrame.left != mLastFrame.left)
    923                 && (mAttrs.privateFlags&PRIVATE_FLAG_NO_MOVE_ANIMATION) == 0
    924                 && (mAttachedWindow == null || !mAttachedWindow.shouldAnimateMove());
    925     }
    926 
    927     boolean isFullscreen(int screenWidth, int screenHeight) {
    928         return mFrame.left <= 0 && mFrame.top <= 0 &&
    929                 mFrame.right >= screenWidth && mFrame.bottom >= screenHeight;
    930     }
    931 
    932     boolean isConfigChanged() {
    933         boolean configChanged = mConfiguration != mService.mCurConfiguration
    934                 && (mConfiguration == null
    935                         || (mConfiguration.diff(mService.mCurConfiguration) != 0));
    936 
    937         if (mAttrs.type == TYPE_KEYGUARD) {
    938             // Retain configuration changed status until resetConfiguration called.
    939             mConfigHasChanged |= configChanged;
    940             configChanged = mConfigHasChanged;
    941         }
    942 
    943         return configChanged;
    944     }
    945 
    946     boolean isConfigDiff(int mask) {
    947         return mConfiguration != mService.mCurConfiguration
    948                 && mConfiguration != null
    949                 && (mConfiguration.diff(mService.mCurConfiguration) & mask) != 0;
    950     }
    951 
    952     void removeLocked() {
    953         disposeInputChannel();
    954 
    955         if (mAttachedWindow != null) {
    956             if (WindowManagerService.DEBUG_ADD_REMOVE) Slog.v(TAG, "Removing " + this + " from " + mAttachedWindow);
    957             mAttachedWindow.mChildWindows.remove(this);
    958         }
    959         mWinAnimator.destroyDeferredSurfaceLocked();
    960         mWinAnimator.destroySurfaceLocked();
    961         mSession.windowRemovedLocked();
    962         try {
    963             mClient.asBinder().unlinkToDeath(mDeathRecipient, 0);
    964         } catch (RuntimeException e) {
    965             // Ignore if it has already been removed (usually because
    966             // we are doing this as part of processing a death note.)
    967         }
    968     }
    969 
    970     void setConfiguration(final Configuration newConfig) {
    971         mConfiguration = newConfig;
    972         mConfigHasChanged = false;
    973     }
    974 
    975     void setInputChannel(InputChannel inputChannel) {
    976         if (mInputChannel != null) {
    977             throw new IllegalStateException("Window already has an input channel.");
    978         }
    979 
    980         mInputChannel = inputChannel;
    981         mInputWindowHandle.inputChannel = inputChannel;
    982     }
    983 
    984     void disposeInputChannel() {
    985         if (mInputChannel != null) {
    986             mService.mInputManager.unregisterInputChannel(mInputChannel);
    987 
    988             mInputChannel.dispose();
    989             mInputChannel = null;
    990         }
    991 
    992         mInputWindowHandle.inputChannel = null;
    993     }
    994 
    995     private class DeathRecipient implements IBinder.DeathRecipient {
    996         @Override
    997         public void binderDied() {
    998             try {
    999                 synchronized(mService.mWindowMap) {
   1000                     WindowState win = mService.windowForClientLocked(mSession, mClient, false);
   1001                     Slog.i(TAG, "WIN DEATH: " + win);
   1002                     if (win != null) {
   1003                         mService.removeWindowLocked(mSession, win);
   1004                     } else if (WindowState.this.mHasSurface) {
   1005                         Slog.e(TAG, "!!! LEAK !!! Window removed but surface still valid.");
   1006                         mService.removeWindowLocked(mSession, WindowState.this);
   1007                     }
   1008                 }
   1009             } catch (IllegalArgumentException ex) {
   1010                 // This will happen if the window has already been
   1011                 // removed.
   1012             }
   1013         }
   1014     }
   1015 
   1016     /**
   1017      * @return true if this window desires key events.
   1018      */
   1019     public final boolean canReceiveKeys() {
   1020         return isVisibleOrAdding()
   1021                 && (mViewVisibility == View.VISIBLE)
   1022                 && ((mAttrs.flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) == 0);
   1023     }
   1024 
   1025     @Override
   1026     public boolean hasDrawnLw() {
   1027         return mWinAnimator.mDrawState == WindowStateAnimator.HAS_DRAWN;
   1028     }
   1029 
   1030     @Override
   1031     public boolean showLw(boolean doAnimation) {
   1032         return showLw(doAnimation, true);
   1033     }
   1034 
   1035     boolean showLw(boolean doAnimation, boolean requestAnim) {
   1036         if (isHiddenFromUserLocked()) {
   1037             Slog.w(TAG, "current user violation " + mService.mCurrentUserId + " trying to display "
   1038                     + this + ", type " + mAttrs.type + ", belonging to " + mOwnerUid);
   1039             return false;
   1040         }
   1041         if (!mAppOpVisibility) {
   1042             // Being hidden due to app op request.
   1043             return false;
   1044         }
   1045         if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
   1046             // Already showing.
   1047             return false;
   1048         }
   1049         if (DEBUG_VISIBILITY) Slog.v(TAG, "Policy visibility true: " + this);
   1050         if (doAnimation) {
   1051             if (DEBUG_VISIBILITY) Slog.v(TAG, "doAnimation: mPolicyVisibility="
   1052                     + mPolicyVisibility + " mAnimation=" + mWinAnimator.mAnimation);
   1053             if (!mService.okToDisplay()) {
   1054                 doAnimation = false;
   1055             } else if (mPolicyVisibility && mWinAnimator.mAnimation == null) {
   1056                 // Check for the case where we are currently visible and
   1057                 // not animating; we do not want to do animation at such a
   1058                 // point to become visible when we already are.
   1059                 doAnimation = false;
   1060             }
   1061         }
   1062         mPolicyVisibility = true;
   1063         mPolicyVisibilityAfterAnim = true;
   1064         if (doAnimation) {
   1065             mWinAnimator.applyAnimationLocked(WindowManagerPolicy.TRANSIT_ENTER, true);
   1066         }
   1067         if (requestAnim) {
   1068             mService.scheduleAnimationLocked();
   1069         }
   1070         return true;
   1071     }
   1072 
   1073     @Override
   1074     public boolean hideLw(boolean doAnimation) {
   1075         return hideLw(doAnimation, true);
   1076     }
   1077 
   1078     boolean hideLw(boolean doAnimation, boolean requestAnim) {
   1079         if (doAnimation) {
   1080             if (!mService.okToDisplay()) {
   1081                 doAnimation = false;
   1082             }
   1083         }
   1084         boolean current = doAnimation ? mPolicyVisibilityAfterAnim
   1085                 : mPolicyVisibility;
   1086         if (!current) {
   1087             // Already hiding.
   1088             return false;
   1089         }
   1090         if (doAnimation) {
   1091             mWinAnimator.applyAnimationLocked(WindowManagerPolicy.TRANSIT_EXIT, false);
   1092             if (mWinAnimator.mAnimation == null) {
   1093                 doAnimation = false;
   1094             }
   1095         }
   1096         if (doAnimation) {
   1097             mPolicyVisibilityAfterAnim = false;
   1098         } else {
   1099             if (DEBUG_VISIBILITY) Slog.v(TAG, "Policy visibility false: " + this);
   1100             mPolicyVisibilityAfterAnim = false;
   1101             mPolicyVisibility = false;
   1102             // Window is no longer visible -- make sure if we were waiting
   1103             // for it to be displayed before enabling the display, that
   1104             // we allow the display to be enabled now.
   1105             mService.enableScreenIfNeededLocked();
   1106             if (mService.mCurrentFocus == this) {
   1107                 mService.mFocusMayChange = true;
   1108             }
   1109         }
   1110         if (requestAnim) {
   1111             mService.scheduleAnimationLocked();
   1112         }
   1113         return true;
   1114     }
   1115 
   1116     public boolean setAppOpVisibilityLw(boolean state) {
   1117         if (mAppOpVisibility != state) {
   1118             mAppOpVisibility = state;
   1119             if (state) {
   1120                 // If the policy visibility had last been to hide, then this
   1121                 // will incorrectly show at this point since we lost that
   1122                 // information.  Not a big deal -- for the windows that have app
   1123                 // ops modifies they should only be hidden by policy due to the
   1124                 // lock screen, and the user won't be changing this if locked.
   1125                 // Plus it will quickly be fixed the next time we do a layout.
   1126                 showLw(true, false);
   1127             } else {
   1128                 hideLw(true, false);
   1129             }
   1130             return true;
   1131         }
   1132         return false;
   1133     }
   1134 
   1135     @Override
   1136     public boolean isAlive() {
   1137         return mClient.asBinder().isBinderAlive();
   1138     }
   1139 
   1140     boolean isClosing() {
   1141         return mExiting || (mService.mClosingApps.contains(mAppToken));
   1142     }
   1143 
   1144     @Override
   1145     public boolean isDefaultDisplay() {
   1146         return mDisplayContent.isDefaultDisplay;
   1147     }
   1148 
   1149     public void setShowToOwnerOnlyLocked(boolean showToOwnerOnly) {
   1150         mShowToOwnerOnly = showToOwnerOnly;
   1151     }
   1152 
   1153     boolean isHiddenFromUserLocked() {
   1154         // Attached windows are evaluated based on the window that they are attached to.
   1155         WindowState win = this;
   1156         while (win.mAttachedWindow != null) {
   1157             win = win.mAttachedWindow;
   1158         }
   1159         if (win.mAttrs.type < WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW
   1160                 && win.mAppToken != null && win.mAppToken.showWhenLocked) {
   1161             // Save some cycles by not calling getDisplayInfo unless it is an application
   1162             // window intended for all users.
   1163             final DisplayInfo displayInfo = win.mDisplayContent.getDisplayInfo();
   1164             if (win.mFrame.left <= 0 && win.mFrame.top <= 0
   1165                     && win.mFrame.right >= displayInfo.appWidth
   1166                     && win.mFrame.bottom >= displayInfo.appHeight) {
   1167                 // Is a fullscreen window, like the clock alarm. Show to everyone.
   1168                 return false;
   1169             }
   1170         }
   1171 
   1172         return win.mShowToOwnerOnly
   1173                 && UserHandle.getUserId(win.mOwnerUid) != mService.mCurrentUserId;
   1174     }
   1175 
   1176     private static void applyInsets(Region outRegion, Rect frame, Rect inset) {
   1177         outRegion.set(
   1178                 frame.left + inset.left, frame.top + inset.top,
   1179                 frame.right - inset.right, frame.bottom - inset.bottom);
   1180     }
   1181 
   1182     public void getTouchableRegion(Region outRegion) {
   1183         final Rect frame = mFrame;
   1184         switch (mTouchableInsets) {
   1185             default:
   1186             case ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME:
   1187                 outRegion.set(frame);
   1188                 break;
   1189             case ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT:
   1190                 applyInsets(outRegion, frame, mGivenContentInsets);
   1191                 break;
   1192             case ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE:
   1193                 applyInsets(outRegion, frame, mGivenVisibleInsets);
   1194                 break;
   1195             case ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_REGION: {
   1196                 final Region givenTouchableRegion = mGivenTouchableRegion;
   1197                 outRegion.set(givenTouchableRegion);
   1198                 outRegion.translate(frame.left, frame.top);
   1199                 break;
   1200             }
   1201         }
   1202     }
   1203 
   1204     WindowList getWindowList() {
   1205         return mDisplayContent.getWindowList();
   1206     }
   1207 
   1208     /**
   1209      * Report a focus change.  Must be called with no locks held, and consistently
   1210      * from the same serialized thread (such as dispatched from a handler).
   1211      */
   1212     public void reportFocusChangedSerialized(boolean focused, boolean inTouchMode) {
   1213         try {
   1214             mClient.windowFocusChanged(focused, inTouchMode);
   1215         } catch (RemoteException e) {
   1216         }
   1217         if (mFocusCallbacks != null) {
   1218             final int N = mFocusCallbacks.beginBroadcast();
   1219             for (int i=0; i<N; i++) {
   1220                 IWindowFocusObserver obs = mFocusCallbacks.getBroadcastItem(i);
   1221                 try {
   1222                     if (focused) {
   1223                         obs.focusGained(mWindowId.asBinder());
   1224                     } else {
   1225                         obs.focusLost(mWindowId.asBinder());
   1226                     }
   1227                 } catch (RemoteException e) {
   1228                 }
   1229             }
   1230             mFocusCallbacks.finishBroadcast();
   1231         }
   1232     }
   1233 
   1234     public void registerFocusObserver(IWindowFocusObserver observer) {
   1235         synchronized(mService.mWindowMap) {
   1236             if (mFocusCallbacks == null) {
   1237                 mFocusCallbacks = new RemoteCallbackList<IWindowFocusObserver>();
   1238             }
   1239             mFocusCallbacks.register(observer);
   1240         }
   1241     }
   1242 
   1243     public void unregisterFocusObserver(IWindowFocusObserver observer) {
   1244         synchronized(mService.mWindowMap) {
   1245             if (mFocusCallbacks != null) {
   1246                 mFocusCallbacks.unregister(observer);
   1247             }
   1248         }
   1249     }
   1250 
   1251     public boolean isFocused() {
   1252         synchronized(mService.mWindowMap) {
   1253             return mService.mCurrentFocus == this;
   1254         }
   1255     }
   1256 
   1257     void dump(PrintWriter pw, String prefix, boolean dumpAll) {
   1258         pw.print(prefix); pw.print("mDisplayId="); pw.print(mDisplayContent.getDisplayId());
   1259                 pw.print(" mSession="); pw.print(mSession);
   1260                 pw.print(" mClient="); pw.println(mClient.asBinder());
   1261         pw.print(prefix); pw.print("mOwnerUid="); pw.print(mOwnerUid);
   1262                 pw.print(" mShowToOwnerOnly="); pw.print(mShowToOwnerOnly);
   1263                 pw.print(" package="); pw.print(mAttrs.packageName);
   1264                 pw.print(" appop="); pw.println(AppOpsManager.opToName(mAppOp));
   1265         pw.print(prefix); pw.print("mAttrs="); pw.println(mAttrs);
   1266         pw.print(prefix); pw.print("Requested w="); pw.print(mRequestedWidth);
   1267                 pw.print(" h="); pw.print(mRequestedHeight);
   1268                 pw.print(" mLayoutSeq="); pw.println(mLayoutSeq);
   1269         if (mRequestedWidth != mLastRequestedWidth || mRequestedHeight != mLastRequestedHeight) {
   1270             pw.print(prefix); pw.print("LastRequested w="); pw.print(mLastRequestedWidth);
   1271                     pw.print(" h="); pw.println(mLastRequestedHeight);
   1272         }
   1273         if (mAttachedWindow != null || mLayoutAttached) {
   1274             pw.print(prefix); pw.print("mAttachedWindow="); pw.print(mAttachedWindow);
   1275                     pw.print(" mLayoutAttached="); pw.println(mLayoutAttached);
   1276         }
   1277         if (mIsImWindow || mIsWallpaper || mIsFloatingLayer) {
   1278             pw.print(prefix); pw.print("mIsImWindow="); pw.print(mIsImWindow);
   1279                     pw.print(" mIsWallpaper="); pw.print(mIsWallpaper);
   1280                     pw.print(" mIsFloatingLayer="); pw.print(mIsFloatingLayer);
   1281                     pw.print(" mWallpaperVisible="); pw.println(mWallpaperVisible);
   1282         }
   1283         if (dumpAll) {
   1284             pw.print(prefix); pw.print("mBaseLayer="); pw.print(mBaseLayer);
   1285                     pw.print(" mSubLayer="); pw.print(mSubLayer);
   1286                     pw.print(" mAnimLayer="); pw.print(mLayer); pw.print("+");
   1287                     pw.print((mTargetAppToken != null ?
   1288                             mTargetAppToken.mAppAnimator.animLayerAdjustment
   1289                           : (mAppToken != null ? mAppToken.mAppAnimator.animLayerAdjustment : 0)));
   1290                     pw.print("="); pw.print(mWinAnimator.mAnimLayer);
   1291                     pw.print(" mLastLayer="); pw.println(mWinAnimator.mLastLayer);
   1292         }
   1293         if (dumpAll) {
   1294             pw.print(prefix); pw.print("mToken="); pw.println(mToken);
   1295             pw.print(prefix); pw.print("mRootToken="); pw.println(mRootToken);
   1296             if (mAppToken != null) {
   1297                 pw.print(prefix); pw.print("mAppToken="); pw.println(mAppToken);
   1298             }
   1299             if (mTargetAppToken != null) {
   1300                 pw.print(prefix); pw.print("mTargetAppToken="); pw.println(mTargetAppToken);
   1301             }
   1302             pw.print(prefix); pw.print("mViewVisibility=0x");
   1303             pw.print(Integer.toHexString(mViewVisibility));
   1304             pw.print(" mHaveFrame="); pw.print(mHaveFrame);
   1305             pw.print(" mObscured="); pw.println(mObscured);
   1306             pw.print(prefix); pw.print("mSeq="); pw.print(mSeq);
   1307             pw.print(" mSystemUiVisibility=0x");
   1308             pw.println(Integer.toHexString(mSystemUiVisibility));
   1309         }
   1310         if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || !mAppOpVisibility
   1311                 || mAttachedHidden) {
   1312             pw.print(prefix); pw.print("mPolicyVisibility=");
   1313                     pw.print(mPolicyVisibility);
   1314                     pw.print(" mPolicyVisibilityAfterAnim=");
   1315                     pw.print(mPolicyVisibilityAfterAnim);
   1316                     pw.print(" mAppOpVisibility=");
   1317                     pw.print(mAppOpVisibility);
   1318                     pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
   1319         }
   1320         if (!mRelayoutCalled || mLayoutNeeded) {
   1321             pw.print(prefix); pw.print("mRelayoutCalled="); pw.print(mRelayoutCalled);
   1322                     pw.print(" mLayoutNeeded="); pw.println(mLayoutNeeded);
   1323         }
   1324         if (mXOffset != 0 || mYOffset != 0) {
   1325             pw.print(prefix); pw.print("Offsets x="); pw.print(mXOffset);
   1326                     pw.print(" y="); pw.println(mYOffset);
   1327         }
   1328         if (dumpAll) {
   1329             pw.print(prefix); pw.print("mGivenContentInsets=");
   1330                     mGivenContentInsets.printShortString(pw);
   1331                     pw.print(" mGivenVisibleInsets=");
   1332                     mGivenVisibleInsets.printShortString(pw);
   1333                     pw.println();
   1334             if (mTouchableInsets != 0 || mGivenInsetsPending) {
   1335                 pw.print(prefix); pw.print("mTouchableInsets="); pw.print(mTouchableInsets);
   1336                         pw.print(" mGivenInsetsPending="); pw.println(mGivenInsetsPending);
   1337                 Region region = new Region();
   1338                 getTouchableRegion(region);
   1339                 pw.print(prefix); pw.print("touchable region="); pw.println(region);
   1340             }
   1341             pw.print(prefix); pw.print("mConfiguration="); pw.println(mConfiguration);
   1342         }
   1343         pw.print(prefix); pw.print("mHasSurface="); pw.print(mHasSurface);
   1344                 pw.print(" mShownFrame="); mShownFrame.printShortString(pw);
   1345                 pw.print(" isReadyForDisplay()="); pw.println(isReadyForDisplay());
   1346         if (dumpAll) {
   1347             pw.print(prefix); pw.print("mFrame="); mFrame.printShortString(pw);
   1348                     pw.print(" last="); mLastFrame.printShortString(pw);
   1349                     pw.println();
   1350             pw.print(prefix); pw.print("mSystemDecorRect="); mSystemDecorRect.printShortString(pw);
   1351                     pw.print(" last="); mLastSystemDecorRect.printShortString(pw);
   1352                     pw.println();
   1353         }
   1354         if (mEnforceSizeCompat) {
   1355             pw.print(prefix); pw.print("mCompatFrame="); mCompatFrame.printShortString(pw);
   1356                     pw.println();
   1357         }
   1358         if (dumpAll) {
   1359             pw.print(prefix); pw.print("Frames: containing=");
   1360                     mContainingFrame.printShortString(pw);
   1361                     pw.print(" parent="); mParentFrame.printShortString(pw);
   1362                     pw.println();
   1363             pw.print(prefix); pw.print("    display="); mDisplayFrame.printShortString(pw);
   1364                     pw.print(" overscan="); mOverscanFrame.printShortString(pw);
   1365                     pw.println();
   1366             pw.print(prefix); pw.print("    content="); mContentFrame.printShortString(pw);
   1367                     pw.print(" visible="); mVisibleFrame.printShortString(pw);
   1368                     pw.println();
   1369             pw.print(prefix); pw.print("Cur insets: overscan=");
   1370                     mOverscanInsets.printShortString(pw);
   1371                     pw.print(" content="); mContentInsets.printShortString(pw);
   1372                     pw.print(" visible="); mVisibleInsets.printShortString(pw);
   1373                     pw.println();
   1374             pw.print(prefix); pw.print("Lst insets: overscan=");
   1375                     mLastOverscanInsets.printShortString(pw);
   1376                     pw.print(" content="); mLastContentInsets.printShortString(pw);
   1377                     pw.print(" visible="); mLastVisibleInsets.printShortString(pw);
   1378                     pw.println();
   1379         }
   1380         pw.print(prefix); pw.print(mWinAnimator); pw.println(":");
   1381         mWinAnimator.dump(pw, prefix + "  ", dumpAll);
   1382         if (mExiting || mRemoveOnExit || mDestroying || mRemoved) {
   1383             pw.print(prefix); pw.print("mExiting="); pw.print(mExiting);
   1384                     pw.print(" mRemoveOnExit="); pw.print(mRemoveOnExit);
   1385                     pw.print(" mDestroying="); pw.print(mDestroying);
   1386                     pw.print(" mRemoved="); pw.println(mRemoved);
   1387         }
   1388         if (mOrientationChanging || mAppFreezing || mTurnOnScreen) {
   1389             pw.print(prefix); pw.print("mOrientationChanging=");
   1390                     pw.print(mOrientationChanging);
   1391                     pw.print(" mAppFreezing="); pw.print(mAppFreezing);
   1392                     pw.print(" mTurnOnScreen="); pw.println(mTurnOnScreen);
   1393         }
   1394         if (mLastFreezeDuration != 0) {
   1395             pw.print(prefix); pw.print("mLastFreezeDuration=");
   1396                     TimeUtils.formatDuration(mLastFreezeDuration, pw); pw.println();
   1397         }
   1398         if (mHScale != 1 || mVScale != 1) {
   1399             pw.print(prefix); pw.print("mHScale="); pw.print(mHScale);
   1400                     pw.print(" mVScale="); pw.println(mVScale);
   1401         }
   1402         if (mWallpaperX != -1 || mWallpaperY != -1) {
   1403             pw.print(prefix); pw.print("mWallpaperX="); pw.print(mWallpaperX);
   1404                     pw.print(" mWallpaperY="); pw.println(mWallpaperY);
   1405         }
   1406         if (mWallpaperXStep != -1 || mWallpaperYStep != -1) {
   1407             pw.print(prefix); pw.print("mWallpaperXStep="); pw.print(mWallpaperXStep);
   1408                     pw.print(" mWallpaperYStep="); pw.println(mWallpaperYStep);
   1409         }
   1410     }
   1411 
   1412     String makeInputChannelName() {
   1413         return Integer.toHexString(System.identityHashCode(this))
   1414             + " " + mAttrs.getTitle();
   1415     }
   1416 
   1417     @Override
   1418     public String toString() {
   1419         CharSequence title = mAttrs.getTitle();
   1420         if (title == null || title.length() <= 0) {
   1421             title = mAttrs.packageName;
   1422         }
   1423         if (mStringNameCache == null || mLastTitle != title || mWasExiting != mExiting) {
   1424             mLastTitle = title;
   1425             mWasExiting = mExiting;
   1426             mStringNameCache = "Window{" + Integer.toHexString(System.identityHashCode(this))
   1427                     + " u" + UserHandle.getUserId(mSession.mUid)
   1428                     + " " + mLastTitle + (mExiting ? " EXITING}" : "}");
   1429         }
   1430         return mStringNameCache;
   1431     }
   1432 }
   1433