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