DisplayContent.java revision 94cee2b6125db6cc47e210c9dd45906d6a7c4382
1/*
2 * Copyright (C) 2012 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 android.app.ActivityManager.StackId.DOCKED_STACK_ID;
20import static android.app.ActivityManager.StackId.HOME_STACK_ID;
21import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
22import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
23import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
24import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
25import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
26import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
27import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
28import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
29
30import android.app.ActivityManager.StackId;
31import android.graphics.Rect;
32import android.graphics.Region;
33import android.graphics.Region.Op;
34import android.hardware.display.DisplayManagerInternal;
35import android.util.DisplayMetrics;
36import android.util.Slog;
37import android.view.Display;
38import android.view.DisplayInfo;
39import android.view.Surface;
40import android.view.animation.Animation;
41
42import java.io.PrintWriter;
43import java.util.ArrayList;
44
45class DisplayContentList extends ArrayList<DisplayContent> {
46}
47
48/**
49 * Utility class for keeping track of the WindowStates and other pertinent contents of a
50 * particular Display.
51 *
52 * IMPORTANT: No method from this class should ever be used without holding
53 * WindowManagerService.mWindowMap.
54 */
55class DisplayContent {
56
57    /** Unique identifier of this stack. */
58    private final int mDisplayId;
59
60    /** Z-ordered (bottom-most first) list of all Window objects. Assigned to an element
61     * from mDisplayWindows; */
62    private final WindowList mWindows = new WindowList();
63
64    int mInitialDisplayWidth = 0;
65    int mInitialDisplayHeight = 0;
66    int mInitialDisplayDensity = 0;
67    int mBaseDisplayWidth = 0;
68    int mBaseDisplayHeight = 0;
69    int mBaseDisplayDensity = 0;
70    boolean mDisplayScalingDisabled;
71    private final DisplayInfo mDisplayInfo = new DisplayInfo();
72    private final Display mDisplay;
73    private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
74
75    Rect mBaseDisplayRect = new Rect();
76    Rect mContentRect = new Rect();
77
78    // Accessed directly by all users.
79    boolean layoutNeeded;
80    int pendingLayoutChanges;
81    final boolean isDefaultDisplay;
82
83    /** Window tokens that are in the process of exiting, but still on screen for animations. */
84    final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
85
86    /** Array containing all TaskStacks on this display.  Array
87     * is stored in display order with the current bottom stack at 0. */
88    private final ArrayList<TaskStack> mStacks = new ArrayList<>();
89
90    /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
91     * (except a future lockscreen TaskStack) moves to the top. */
92    private TaskStack mHomeStack = null;
93
94    /** Detect user tapping outside of current focused task bounds .*/
95    TaskTapPointerEventListener mTapDetector;
96
97    /** Detect user tapping outside of current focused stack bounds .*/
98    Region mTouchExcludeRegion = new Region();
99
100    /** Save allocating when calculating rects */
101    private final Rect mTmpRect = new Rect();
102    private final Rect mTmpRect2 = new Rect();
103    private final Region mTmpRegion = new Region();
104
105    /** For gathering Task objects in order. */
106    final ArrayList<Task> mTmpTaskHistory = new ArrayList<Task>();
107
108    final WindowManagerService mService;
109
110    /** Remove this display when animation on it has completed. */
111    boolean mDeferredRemoval;
112
113    final DockedStackDividerController mDividerControllerLocked;
114
115    final DimLayerController mDimLayerController;
116
117    final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
118
119    /**
120     * @param display May not be null.
121     * @param service You know.
122     */
123    DisplayContent(Display display, WindowManagerService service) {
124        mDisplay = display;
125        mDisplayId = display.getDisplayId();
126        display.getDisplayInfo(mDisplayInfo);
127        display.getMetrics(mDisplayMetrics);
128        isDefaultDisplay = mDisplayId == Display.DEFAULT_DISPLAY;
129        mService = service;
130        initializeDisplayBaseInfo();
131        mDividerControllerLocked = new DockedStackDividerController(service, this);
132        mDimLayerController = new DimLayerController(this);
133    }
134
135    int getDisplayId() {
136        return mDisplayId;
137    }
138
139    WindowList getWindowList() {
140        return mWindows;
141    }
142
143    Display getDisplay() {
144        return mDisplay;
145    }
146
147    DisplayInfo getDisplayInfo() {
148        return mDisplayInfo;
149    }
150
151    DisplayMetrics getDisplayMetrics() {
152        return mDisplayMetrics;
153    }
154
155    DockedStackDividerController getDockedDividerController() {
156        return mDividerControllerLocked;
157    }
158
159    /**
160     * Returns true if the specified UID has access to this display.
161     */
162    public boolean hasAccess(int uid) {
163        return mDisplay.hasAccess(uid);
164    }
165
166    public boolean isPrivate() {
167        return (mDisplay.getFlags() & Display.FLAG_PRIVATE) != 0;
168    }
169
170    ArrayList<TaskStack> getStacks() {
171        return mStacks;
172    }
173
174    /**
175     * Retrieve the tasks on this display in stack order from the bottommost TaskStack up.
176     * @return All the Tasks, in order, on this display.
177     */
178    ArrayList<Task> getTasks() {
179        mTmpTaskHistory.clear();
180        final int numStacks = mStacks.size();
181        for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
182            mTmpTaskHistory.addAll(mStacks.get(stackNdx).getTasks());
183        }
184        return mTmpTaskHistory;
185    }
186
187    TaskStack getHomeStack() {
188        if (mHomeStack == null && mDisplayId == Display.DEFAULT_DISPLAY) {
189            Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
190        }
191        return mHomeStack;
192    }
193
194    TaskStack getStackById(int stackId) {
195        for (int i = mStacks.size() - 1; i >= 0; --i) {
196            final TaskStack stack = mStacks.get(i);
197            if (stack.mStackId == stackId) {
198                return stack;
199            }
200        }
201        return null;
202    }
203
204    void updateDisplayInfo() {
205        mDisplay.getDisplayInfo(mDisplayInfo);
206        mDisplay.getMetrics(mDisplayMetrics);
207        for (int i = mStacks.size() - 1; i >= 0; --i) {
208            mStacks.get(i).updateDisplayInfo(null);
209        }
210    }
211
212    void initializeDisplayBaseInfo() {
213        final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
214        if (displayManagerInternal != null) {
215            // Bootstrap the default logical display from the display manager.
216            final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
217            if (newDisplayInfo != null) {
218                mDisplayInfo.copyFrom(newDisplayInfo);
219            }
220        }
221
222        mBaseDisplayWidth = mInitialDisplayWidth = mDisplayInfo.logicalWidth;
223        mBaseDisplayHeight = mInitialDisplayHeight = mDisplayInfo.logicalHeight;
224        mBaseDisplayDensity = mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
225        mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
226    }
227
228    void getLogicalDisplayRect(Rect out) {
229        // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
230        final int orientation = mDisplayInfo.rotation;
231        boolean rotated = (orientation == Surface.ROTATION_90
232                || orientation == Surface.ROTATION_270);
233        final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
234        final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
235        int width = mDisplayInfo.logicalWidth;
236        int left = (physWidth - width) / 2;
237        int height = mDisplayInfo.logicalHeight;
238        int top = (physHeight - height) / 2;
239        out.set(left, top, left + width, top + height);
240    }
241
242    void getContentRect(Rect out) {
243        out.set(mContentRect);
244    }
245
246    /** Refer to {@link WindowManagerService#attachStack(int, int, boolean)} */
247    void attachStack(TaskStack stack, boolean onTop) {
248        if (stack.mStackId == HOME_STACK_ID) {
249            if (mHomeStack != null) {
250                throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
251            }
252            mHomeStack = stack;
253        }
254        if (onTop) {
255            mStacks.add(stack);
256        } else {
257            mStacks.add(0, stack);
258        }
259        layoutNeeded = true;
260    }
261
262    void moveStack(TaskStack stack, boolean toTop) {
263        if (StackId.isAlwaysOnTop(stack.mStackId) && !toTop) {
264            // This stack is always-on-top silly...
265            Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + stack + " to bottom");
266            return;
267        }
268
269        if (!mStacks.remove(stack)) {
270            Slog.wtf(TAG_WM, "moving stack that was not added: " + stack, new Throwable());
271        }
272
273        int addIndex = toTop ? mStacks.size() : 0;
274
275        if (toTop
276                && mService.isStackVisibleLocked(PINNED_STACK_ID)
277                && stack.mStackId != PINNED_STACK_ID) {
278            // The pinned stack is always the top most stack (always-on-top) when it is visible.
279            // So, stack is moved just below the pinned stack.
280            addIndex--;
281            TaskStack topStack = mStacks.get(addIndex);
282            if (topStack.mStackId != PINNED_STACK_ID) {
283                throw new IllegalStateException("Pinned stack isn't top stack??? " + mStacks);
284            }
285        }
286        mStacks.add(addIndex, stack);
287    }
288
289    void detachStack(TaskStack stack) {
290        mDimLayerController.removeDimLayerUser(stack);
291        mStacks.remove(stack);
292    }
293
294    /**
295     * Propagate the new bounds to all child stacks.
296     * @param contentRect The bounds to apply at the top level.
297     */
298    void resize(Rect contentRect) {
299        mContentRect.set(contentRect);
300    }
301
302    int taskIdFromPoint(int x, int y) {
303        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
304            TaskStack stack = mStacks.get(stackNdx);
305            stack.getBounds(mTmpRect);
306            if (!mTmpRect.contains(x, y) || stack.isAdjustedForMinimizedDockedStack()) {
307                continue;
308            }
309            final ArrayList<Task> tasks = stack.getTasks();
310            for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
311                final Task task = tasks.get(taskNdx);
312                final WindowState win = task.getTopVisibleAppMainWindow();
313                if (win == null) {
314                    continue;
315                }
316                // We need to use the task's dim bounds (which is derived from the visible
317                // bounds of its apps windows) for any touch-related tests. Can't use
318                // the task's original bounds because it might be adjusted to fit the
319                // content frame. For example, the presence of the IME adjusting the
320                // windows frames when the app window is the IME target.
321                task.getDimBounds(mTmpRect);
322                if (mTmpRect.contains(x, y)) {
323                    return task.mTaskId;
324                }
325            }
326        }
327        return -1;
328    }
329
330    /**
331     * Find the task whose outside touch area (for resizing) (x, y) falls within.
332     * Returns null if the touch doesn't fall into a resizing area.
333     */
334    Task findTaskForControlPoint(int x, int y) {
335        final int delta = mService.dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
336        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
337            TaskStack stack = mStacks.get(stackNdx);
338            if (!StackId.isTaskResizeAllowed(stack.mStackId)) {
339                break;
340            }
341            final ArrayList<Task> tasks = stack.getTasks();
342            for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
343                final Task task = tasks.get(taskNdx);
344                if (task.isFullscreen()) {
345                    return null;
346                }
347
348                // We need to use the task's dim bounds (which is derived from the visible
349                // bounds of its apps windows) for any touch-related tests. Can't use
350                // the task's original bounds because it might be adjusted to fit the
351                // content frame. One example is when the task is put to top-left quadrant,
352                // the actual visible area would not start at (0,0) after it's adjusted
353                // for the status bar.
354                task.getDimBounds(mTmpRect);
355                mTmpRect.inset(-delta, -delta);
356                if (mTmpRect.contains(x, y)) {
357                    mTmpRect.inset(delta, delta);
358                    if (!mTmpRect.contains(x, y)) {
359                        return task;
360                    }
361                    // User touched inside the task. No need to look further,
362                    // focus transfer will be handled in ACTION_UP.
363                    return null;
364                }
365            }
366        }
367        return null;
368    }
369
370    void setTouchExcludeRegion(Task focusedTask) {
371        mTouchExcludeRegion.set(mBaseDisplayRect);
372        final int delta = mService.dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
373        boolean addBackFocusedTask = false;
374        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
375            TaskStack stack = mStacks.get(stackNdx);
376            final ArrayList<Task> tasks = stack.getTasks();
377            for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
378                final Task task = tasks.get(taskNdx);
379                AppWindowToken token = task.getTopVisibleAppToken();
380                if (token == null || !token.isVisible()) {
381                    continue;
382                }
383
384                /**
385                 * Exclusion region is the region that TapDetector doesn't care about.
386                 * Here we want to remove all non-focused tasks from the exclusion region.
387                 * We also remove the outside touch area for resizing for all freeform
388                 * tasks (including the focused).
389                 *
390                 * We save the focused task region once we find it, and add it back at the end.
391                 */
392
393                task.getDimBounds(mTmpRect);
394
395                if (task == focusedTask) {
396                    addBackFocusedTask = true;
397                    mTmpRect2.set(mTmpRect);
398                }
399
400                final boolean isFreeformed = task.inFreeformWorkspace();
401                if (task != focusedTask || isFreeformed) {
402                    if (isFreeformed) {
403                        // If the task is freeformed, enlarge the area to account for outside
404                        // touch area for resize.
405                        mTmpRect.inset(-delta, -delta);
406                        // Intersect with display content rect. If we have system decor (status bar/
407                        // navigation bar), we want to exclude that from the tap detection.
408                        // Otherwise, if the app is partially placed under some system button (eg.
409                        // Recents, Home), pressing that button would cause a full series of
410                        // unwanted transfer focus/resume/pause, before we could go home.
411                        mTmpRect.intersect(mContentRect);
412                    }
413                    mTouchExcludeRegion.op(mTmpRect, Region.Op.DIFFERENCE);
414                }
415            }
416        }
417        // If we removed the focused task above, add it back and only leave its
418        // outside touch area in the exclusion. TapDectector is not interested in
419        // any touch inside the focused task itself.
420        if (addBackFocusedTask) {
421            mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
422        }
423        final WindowState inputMethod = mService.mInputMethodWindow;
424        if (inputMethod != null && inputMethod.isVisibleLw()) {
425            // If the input method is visible and the user is typing, we don't want these touch
426            // events to be intercepted and used to change focus. This would likely cause a
427            // disappearance of the input method.
428            inputMethod.getTouchableRegion(mTmpRegion);
429            mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
430        }
431        for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
432            WindowState win = mTapExcludedWindows.get(i);
433            win.getTouchableRegion(mTmpRegion);
434            mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
435        }
436        if (getDockedStackVisibleForUserLocked() != null) {
437            mDividerControllerLocked.getTouchRegion(mTmpRect);
438            mTmpRegion.set(mTmpRect);
439            mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
440        }
441        if (mTapDetector != null) {
442            mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
443        }
444    }
445
446    void switchUserStacks() {
447        final WindowList windows = getWindowList();
448        for (int i = 0; i < windows.size(); i++) {
449            final WindowState win = windows.get(i);
450            if (win.isHiddenFromUserLocked()) {
451                if (DEBUG_VISIBILITY) Slog.w(TAG_WM, "user changing, hiding " + win
452                        + ", attrs=" + win.mAttrs.type + ", belonging to " + win.mOwnerUid);
453                win.hideLw(false);
454            }
455        }
456
457        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
458            mStacks.get(stackNdx).switchUser();
459        }
460    }
461
462    void resetAnimationBackgroundAnimator() {
463        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
464            mStacks.get(stackNdx).resetAnimationBackgroundAnimator();
465        }
466    }
467
468    boolean animateDimLayers() {
469        return mDimLayerController.animateDimLayers();
470    }
471
472    void resetDimming() {
473        mDimLayerController.resetDimming();
474    }
475
476    boolean isDimming() {
477        return mDimLayerController.isDimming();
478    }
479
480    void stopDimmingIfNeeded() {
481        mDimLayerController.stopDimmingIfNeeded();
482    }
483
484    void close() {
485        mDimLayerController.close();
486        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
487            mStacks.get(stackNdx).close();
488        }
489    }
490
491    boolean isAnimating() {
492        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
493            final TaskStack stack = mStacks.get(stackNdx);
494            if (stack.isAnimating()) {
495                return true;
496            }
497        }
498        return false;
499    }
500
501    void checkForDeferredActions() {
502        boolean animating = false;
503        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
504            final TaskStack stack = mStacks.get(stackNdx);
505            if (stack.isAnimating()) {
506                animating = true;
507            } else {
508                if (stack.mDeferDetach) {
509                    mService.detachStackLocked(this, stack);
510                }
511                final ArrayList<Task> tasks = stack.getTasks();
512                for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
513                    final Task task = tasks.get(taskNdx);
514                    AppTokenList tokens = task.mAppTokens;
515                    for (int tokenNdx = tokens.size() - 1; tokenNdx >= 0; --tokenNdx) {
516                        AppWindowToken wtoken = tokens.get(tokenNdx);
517                        if (wtoken.mIsExiting) {
518                            wtoken.removeIfPossible();
519                        }
520                    }
521                }
522            }
523        }
524        if (!animating && mDeferredRemoval) {
525            mService.onDisplayRemoved(mDisplayId);
526        }
527    }
528
529    void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
530        final int rotationDelta = DisplayContent.deltaRotation(oldRotation, newRotation);
531        getLogicalDisplayRect(mTmpRect);
532        switch (rotationDelta) {
533            case Surface.ROTATION_0:
534                mTmpRect2.set(bounds);
535                break;
536            case Surface.ROTATION_90:
537                mTmpRect2.top = mTmpRect.bottom - bounds.right;
538                mTmpRect2.left = bounds.top;
539                mTmpRect2.right = mTmpRect2.left + bounds.height();
540                mTmpRect2.bottom = mTmpRect2.top + bounds.width();
541                break;
542            case Surface.ROTATION_180:
543                mTmpRect2.top = mTmpRect.bottom - bounds.bottom;
544                mTmpRect2.left = mTmpRect.right - bounds.right;
545                mTmpRect2.right = mTmpRect2.left + bounds.width();
546                mTmpRect2.bottom = mTmpRect2.top + bounds.height();
547                break;
548            case Surface.ROTATION_270:
549                mTmpRect2.top = bounds.left;
550                mTmpRect2.left = mTmpRect.right - bounds.bottom;
551                mTmpRect2.right = mTmpRect2.left + bounds.height();
552                mTmpRect2.bottom = mTmpRect2.top + bounds.width();
553                break;
554        }
555        bounds.set(mTmpRect2);
556    }
557
558    static int deltaRotation(int oldRotation, int newRotation) {
559        int delta = newRotation - oldRotation;
560        if (delta < 0) delta += 4;
561        return delta;
562    }
563
564    public void dump(String prefix, PrintWriter pw) {
565        pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
566        final String subPrefix = "  " + prefix;
567        pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
568            pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
569            pw.print("dpi");
570            if (mInitialDisplayWidth != mBaseDisplayWidth
571                    || mInitialDisplayHeight != mBaseDisplayHeight
572                    || mInitialDisplayDensity != mBaseDisplayDensity) {
573                pw.print(" base=");
574                pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
575                pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
576            }
577            if (mDisplayScalingDisabled) {
578                pw.println(" noscale");
579            }
580            pw.print(" cur=");
581            pw.print(mDisplayInfo.logicalWidth);
582            pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
583            pw.print(" app=");
584            pw.print(mDisplayInfo.appWidth);
585            pw.print("x"); pw.print(mDisplayInfo.appHeight);
586            pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
587            pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
588            pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
589            pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
590            pw.print(subPrefix); pw.print("deferred="); pw.print(mDeferredRemoval);
591                pw.print(" layoutNeeded="); pw.println(layoutNeeded);
592
593        pw.println();
594        pw.println("  Application tokens in top down Z order:");
595        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
596            final TaskStack stack = mStacks.get(stackNdx);
597            stack.dump(prefix + "  ", pw);
598        }
599
600        pw.println();
601        if (!mExitingTokens.isEmpty()) {
602            pw.println();
603            pw.println("  Exiting tokens:");
604            for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
605                WindowToken token = mExitingTokens.get(i);
606                pw.print("  Exiting #"); pw.print(i);
607                pw.print(' '); pw.print(token);
608                pw.println(':');
609                token.dump(pw, "    ");
610            }
611        }
612        pw.println();
613        mDimLayerController.dump(prefix + "  ", pw);
614        pw.println();
615        mDividerControllerLocked.dump(prefix + "  ", pw);
616    }
617
618    @Override
619    public String toString() {
620        return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mStacks;
621    }
622
623    /**
624     * @return The docked stack, but only if it is visible, and {@code null} otherwise.
625     */
626    TaskStack getDockedStackLocked() {
627        final TaskStack stack = mService.mStackIdToStack.get(DOCKED_STACK_ID);
628        return (stack != null && stack.isVisible()) ? stack : null;
629    }
630
631    /**
632     * Like {@link #getDockedStackLocked}, but also returns the docked stack if it's currently not
633     * visible, as long as it's not hidden because the current user doesn't have any tasks there.
634     */
635    TaskStack getDockedStackVisibleForUserLocked() {
636        final TaskStack stack = mService.mStackIdToStack.get(DOCKED_STACK_ID);
637        return (stack != null && stack.isVisible(true /* ignoreKeyguard */)) ? stack : null;
638    }
639
640    /**
641     * Find the visible, touch-deliverable window under the given point
642     */
643    WindowState getTouchableWinAtPointLocked(float xf, float yf) {
644        WindowState touchedWin = null;
645        final int x = (int) xf;
646        final int y = (int) yf;
647
648        for (int i = mWindows.size() - 1; i >= 0; i--) {
649            WindowState window = mWindows.get(i);
650            final int flags = window.mAttrs.flags;
651            if (!window.isVisibleLw()) {
652                continue;
653            }
654            if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
655                continue;
656            }
657
658            window.getVisibleBounds(mTmpRect);
659            if (!mTmpRect.contains(x, y)) {
660                continue;
661            }
662
663            window.getTouchableRegion(mTmpRegion);
664
665            final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
666            if (mTmpRegion.contains(x, y) || touchFlags == 0) {
667                touchedWin = window;
668                break;
669            }
670        }
671
672        return touchedWin;
673    }
674
675    /**
676     * See {@link WindowManagerService#overridePlayingAppAnimationsLw}.
677     */
678    void overridePlayingAppAnimationsLw(Animation a) {
679        for (int i = mStacks.size() - 1; i >= 0; i--) {
680            mStacks.get(i).overridePlayingAppAnimations(a);
681        }
682    }
683
684    boolean canAddToastWindowForUid(int uid) {
685        // We allow one toast window per UID being shown at a time.
686        WindowList windows = getWindowList();
687        final int windowCount = windows.size();
688        for (int i = 0; i < windowCount; i++) {
689            WindowState window = windows.get(i);
690            if (window.mAttrs.type == TYPE_TOAST && window.mOwnerUid == uid
691                    && !window.mPermanentlyHidden && !window.mAnimatingExit) {
692                return false;
693            }
694        }
695        return true;
696    }
697
698    void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus,
699                                                   WindowState newFocus) {
700        if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
701            return;
702        }
703        final int lostFocusUid = oldFocus.mOwnerUid;
704        WindowList windows = getWindowList();
705        final int windowCount = windows.size();
706        for (int i = 0; i < windowCount; i++) {
707            WindowState window = windows.get(i);
708            if (window.mAttrs.type == TYPE_TOAST && window.mOwnerUid == lostFocusUid) {
709                if (!mService.mH.hasMessages(WindowManagerService.H.WINDOW_HIDE_TIMEOUT, window)) {
710                    mService.mH.sendMessageDelayed(
711                            mService.mH.obtainMessage(
712                                    WindowManagerService.H.WINDOW_HIDE_TIMEOUT, window),
713                            window.mAttrs.hideTimeoutMilliseconds);
714                }
715            }
716        }
717    }
718}
719