DisplayContent.java revision dc548483ae90ba26ad9e2e2cb79f4673140edb49
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 com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
20import static com.android.server.wm.WindowManagerService.DEBUG_VISIBILITY;
21import static com.android.server.wm.WindowManagerService.TAG;
22
23import android.graphics.Rect;
24import android.graphics.Region;
25import android.util.Slog;
26import android.view.Display;
27import android.view.DisplayInfo;
28import android.view.Surface;
29
30import java.io.PrintWriter;
31import java.util.ArrayList;
32
33class DisplayContentList extends ArrayList<DisplayContent> {
34}
35
36/**
37 * Utility class for keeping track of the WindowStates and other pertinent contents of a
38 * particular Display.
39 *
40 * IMPORTANT: No method from this class should ever be used without holding
41 * WindowManagerService.mWindowMap.
42 */
43class DisplayContent {
44
45    /** Unique identifier of this stack. */
46    private final int mDisplayId;
47
48    /** Z-ordered (bottom-most first) list of all Window objects. Assigned to an element
49     * from mDisplayWindows; */
50    private final WindowList mWindows = new WindowList();
51
52    // This protects the following display size properties, so that
53    // getDisplaySize() doesn't need to acquire the global lock.  This is
54    // needed because the window manager sometimes needs to use ActivityThread
55    // while it has its global state locked (for example to load animation
56    // resources), but the ActivityThread also needs get the current display
57    // size sometimes when it has its package lock held.
58    //
59    // These will only be modified with both mWindowMap and mDisplaySizeLock
60    // held (in that order) so the window manager doesn't need to acquire this
61    // lock when needing these values in its normal operation.
62    final Object mDisplaySizeLock = new Object();
63    int mInitialDisplayWidth = 0;
64    int mInitialDisplayHeight = 0;
65    int mInitialDisplayDensity = 0;
66    int mBaseDisplayWidth = 0;
67    int mBaseDisplayHeight = 0;
68    int mBaseDisplayDensity = 0;
69    private final DisplayInfo mDisplayInfo = new DisplayInfo();
70    private final Display mDisplay;
71
72    Rect mBaseDisplayRect = new Rect();
73    Rect mContentRect = new Rect();
74
75    // Accessed directly by all users.
76    boolean layoutNeeded;
77    int pendingLayoutChanges;
78    final boolean isDefaultDisplay;
79
80    /** Window tokens that are in the process of exiting, but still on screen for animations. */
81    final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();
82
83    /** Array containing all TaskStacks on this display.  Array
84     * is stored in display order with the current bottom stack at 0. */
85    private final ArrayList<TaskStack> mStacks = new ArrayList<TaskStack>();
86
87    /** A special TaskStack with id==HOME_STACK_ID that moves to the bottom whenever any TaskStack
88     * (except a future lockscreen TaskStack) moves to the top. */
89    private TaskStack mHomeStack = null;
90
91    /** Detect user tapping outside of current focused stack bounds .*/
92    StackTapPointerEventListener mTapDetector;
93
94    /** Detect user tapping outside of current focused stack bounds .*/
95    Region mTouchExcludeRegion = new Region();
96
97    /** Save allocating when calculating rects */
98    Rect mTmpRect = new Rect();
99
100    /** For gathering Task objects in order. */
101    final ArrayList<Task> mTmpTaskHistory = new ArrayList<Task>();
102
103    final WindowManagerService mService;
104
105    /**
106     * @param display May not be null.
107     * @param service You know.
108     */
109    DisplayContent(Display display, WindowManagerService service) {
110        mDisplay = display;
111        mDisplayId = display.getDisplayId();
112        display.getDisplayInfo(mDisplayInfo);
113        isDefaultDisplay = mDisplayId == Display.DEFAULT_DISPLAY;
114        mService = service;
115    }
116
117    int getDisplayId() {
118        return mDisplayId;
119    }
120
121    WindowList getWindowList() {
122        return mWindows;
123    }
124
125    Display getDisplay() {
126        return mDisplay;
127    }
128
129    DisplayInfo getDisplayInfo() {
130        return mDisplayInfo;
131    }
132
133    /**
134     * Returns true if the specified UID has access to this display.
135     */
136    public boolean hasAccess(int uid) {
137        return mDisplay.hasAccess(uid);
138    }
139
140    public boolean isPrivate() {
141        return (mDisplay.getFlags() & Display.FLAG_PRIVATE) != 0;
142    }
143
144    ArrayList<TaskStack> getStacks() {
145        return mStacks;
146    }
147
148    /**
149     * Retrieve the tasks on this display in stack order from the bottommost TaskStack up.
150     * @return All the Tasks, in order, on this display.
151     */
152    ArrayList<Task> getTasks() {
153        mTmpTaskHistory.clear();
154        final int numStacks = mStacks.size();
155        for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
156            mTmpTaskHistory.addAll(mStacks.get(stackNdx).getTasks());
157        }
158        return mTmpTaskHistory;
159    }
160
161    TaskStack getHomeStack() {
162        if (mHomeStack == null) {
163            Slog.e(TAG, "getHomeStack: Returning null from this=" + this);
164        }
165        return mHomeStack;
166    }
167
168    void updateDisplayInfo() {
169        // Save old size.
170        int oldWidth = mDisplayInfo.logicalWidth;
171        int oldHeight = mDisplayInfo.logicalHeight;
172        mDisplay.getDisplayInfo(mDisplayInfo);
173
174        for (int i = mStacks.size() - 1; i >= 0; --i) {
175            final TaskStack stack = mStacks.get(i);
176            if (!stack.isFullscreen()) {
177                stack.resizeBounds(oldWidth, oldHeight, mDisplayInfo.logicalWidth,
178                        mDisplayInfo.logicalHeight);
179            }
180        }
181    }
182
183    void getLogicalDisplayRect(Rect out) {
184        updateDisplayInfo();
185        // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
186        final int orientation = mDisplayInfo.rotation;
187        boolean rotated = (orientation == Surface.ROTATION_90
188                || orientation == Surface.ROTATION_270);
189        final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
190        final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
191        int width = mDisplayInfo.logicalWidth;
192        int left = (physWidth - width) / 2;
193        int height = mDisplayInfo.logicalHeight;
194        int top = (physHeight - height) / 2;
195        out.set(left, top, left + width, top + height);
196    }
197
198    /** Refer to {@link WindowManagerService#attachStack(int, int)} */
199    void attachStack(TaskStack stack) {
200        if (stack.mStackId == HOME_STACK_ID) {
201            if (mHomeStack != null) {
202                throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
203            }
204            mHomeStack = stack;
205        }
206        mStacks.add(stack);
207        layoutNeeded = true;
208    }
209
210    void moveStack(TaskStack stack, boolean toTop) {
211        mStacks.remove(stack);
212        mStacks.add(toTop ? mStacks.size() : 0, stack);
213    }
214
215    void detachStack(TaskStack stack) {
216        mStacks.remove(stack);
217    }
218
219    /**
220     * Propagate the new bounds to all child stacks.
221     * @param contentRect The bounds to apply at the top level.
222     */
223    void resize(Rect contentRect) {
224        mContentRect.set(contentRect);
225    }
226
227    int stackIdFromPoint(int x, int y) {
228        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
229            final TaskStack stack = mStacks.get(stackNdx);
230            stack.getBounds(mTmpRect);
231            if (mTmpRect.contains(x, y)) {
232                return stack.mStackId;
233            }
234        }
235        return -1;
236    }
237
238    void setTouchExcludeRegion(TaskStack focusedStack) {
239        mTouchExcludeRegion.set(mBaseDisplayRect);
240        WindowList windows = getWindowList();
241        for (int i = windows.size() - 1; i >= 0; --i) {
242            final WindowState win = windows.get(i);
243            final TaskStack stack = win.getStack();
244            if (win.isVisibleLw() && stack != null && stack != focusedStack) {
245                mTmpRect.set(win.mVisibleFrame);
246                mTmpRect.intersect(win.mVisibleInsets);
247                mTouchExcludeRegion.op(mTmpRect, Region.Op.DIFFERENCE);
248            }
249        }
250    }
251
252    void switchUserStacks(int newUserId) {
253        final WindowList windows = getWindowList();
254        for (int i = 0; i < windows.size(); i++) {
255            final WindowState win = windows.get(i);
256            if (win.isHiddenFromUserLocked()) {
257                if (DEBUG_VISIBILITY) Slog.w(TAG, "user changing " + newUserId + " hiding "
258                        + win + ", attrs=" + win.mAttrs.type + ", belonging to "
259                        + win.mOwnerUid);
260                win.hideLw(false);
261            }
262        }
263
264        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
265            mStacks.get(stackNdx).switchUser(newUserId);
266        }
267    }
268
269    void resetAnimationBackgroundAnimator() {
270        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
271            mStacks.get(stackNdx).resetAnimationBackgroundAnimator();
272        }
273    }
274
275    boolean animateDimLayers() {
276        boolean result = false;
277        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
278            result |= mStacks.get(stackNdx).animateDimLayers();
279        }
280        return result;
281    }
282
283    void resetDimming() {
284        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
285            mStacks.get(stackNdx).resetDimmingTag();
286        }
287    }
288
289    boolean isDimming() {
290        boolean result = false;
291        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
292            result |= mStacks.get(stackNdx).isDimming();
293        }
294        return result;
295    }
296
297    void stopDimmingIfNeeded() {
298        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
299            mStacks.get(stackNdx).stopDimmingIfNeeded();
300        }
301    }
302
303    void close() {
304        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
305            mStacks.get(stackNdx).close();
306        }
307    }
308
309    public void dump(String prefix, PrintWriter pw) {
310        pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
311        final String subPrefix = "  " + prefix;
312        pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
313            pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
314            pw.print("dpi");
315            if (mInitialDisplayWidth != mBaseDisplayWidth
316                    || mInitialDisplayHeight != mBaseDisplayHeight
317                    || mInitialDisplayDensity != mBaseDisplayDensity) {
318                pw.print(" base=");
319                pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
320                pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
321            }
322            pw.print(" cur=");
323            pw.print(mDisplayInfo.logicalWidth);
324            pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
325            pw.print(" app=");
326            pw.print(mDisplayInfo.appWidth);
327            pw.print("x"); pw.print(mDisplayInfo.appHeight);
328            pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
329            pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
330            pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
331            pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
332            pw.print(subPrefix); pw.print("layoutNeeded="); pw.println(layoutNeeded);
333        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
334            final TaskStack stack = mStacks.get(stackNdx);
335            pw.print(prefix); pw.print("mStacks[" + stackNdx + "]"); pw.println(stack.mStackId);
336            stack.dump(prefix + "  ", pw);
337        }
338        pw.println();
339        pw.println("  Application tokens in bottom up Z order:");
340        int ndx = 0;
341        final int numStacks = mStacks.size();
342        for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
343            ArrayList<Task> tasks = mStacks.get(stackNdx).getTasks();
344            for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
345                AppTokenList tokens = tasks.get(taskNdx).mAppTokens;
346                for (int tokenNdx = tokens.size() - 1; tokenNdx >= 0; --tokenNdx) {
347                    final AppWindowToken wtoken = tokens.get(tokenNdx);
348                    pw.print("  App #"); pw.print(ndx++);
349                            pw.print(' '); pw.print(wtoken); pw.println(":");
350                    wtoken.dump(pw, "    ");
351                }
352            }
353        }
354        if (ndx == 0) {
355            pw.println("    None");
356        }
357        pw.println();
358        if (!mExitingTokens.isEmpty()) {
359            pw.println();
360            pw.println("  Exiting tokens:");
361            for (int i=mExitingTokens.size()-1; i>=0; i--) {
362                WindowToken token = mExitingTokens.get(i);
363                pw.print("  Exiting #"); pw.print(i);
364                pw.print(' '); pw.print(token);
365                pw.println(':');
366                token.dump(pw, "    ");
367            }
368        }
369        pw.println();
370    }
371
372    @Override
373    public String toString() {
374        return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mStacks;
375    }
376}
377