DisplayContent.java revision 1bf2b873470d2ba8a4ac218da73516cc2b20aa76
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    static final int DEFER_DETACH = 1;
106    static final int DEFER_REMOVAL = 2;
107    int mDeferredActions;
108
109    /**
110     * @param display May not be null.
111     * @param service You know.
112     */
113    DisplayContent(Display display, WindowManagerService service) {
114        mDisplay = display;
115        mDisplayId = display.getDisplayId();
116        display.getDisplayInfo(mDisplayInfo);
117        isDefaultDisplay = mDisplayId == Display.DEFAULT_DISPLAY;
118        mService = service;
119    }
120
121    int getDisplayId() {
122        return mDisplayId;
123    }
124
125    WindowList getWindowList() {
126        return mWindows;
127    }
128
129    Display getDisplay() {
130        return mDisplay;
131    }
132
133    DisplayInfo getDisplayInfo() {
134        return mDisplayInfo;
135    }
136
137    /**
138     * Returns true if the specified UID has access to this display.
139     */
140    public boolean hasAccess(int uid) {
141        return mDisplay.hasAccess(uid);
142    }
143
144    public boolean isPrivate() {
145        return (mDisplay.getFlags() & Display.FLAG_PRIVATE) != 0;
146    }
147
148    ArrayList<TaskStack> getStacks() {
149        return mStacks;
150    }
151
152    /**
153     * Retrieve the tasks on this display in stack order from the bottommost TaskStack up.
154     * @return All the Tasks, in order, on this display.
155     */
156    ArrayList<Task> getTasks() {
157        mTmpTaskHistory.clear();
158        final int numStacks = mStacks.size();
159        for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
160            mTmpTaskHistory.addAll(mStacks.get(stackNdx).getTasks());
161        }
162        return mTmpTaskHistory;
163    }
164
165    TaskStack getHomeStack() {
166        if (mHomeStack == null) {
167            Slog.e(TAG, "getHomeStack: Returning null from this=" + this);
168        }
169        return mHomeStack;
170    }
171
172    void updateDisplayInfo() {
173        // Save old size.
174        int oldWidth = mDisplayInfo.logicalWidth;
175        int oldHeight = mDisplayInfo.logicalHeight;
176        mDisplay.getDisplayInfo(mDisplayInfo);
177
178        for (int i = mStacks.size() - 1; i >= 0; --i) {
179            final TaskStack stack = mStacks.get(i);
180            if (!stack.isFullscreen()) {
181                stack.resizeBounds(oldWidth, oldHeight, mDisplayInfo.logicalWidth,
182                        mDisplayInfo.logicalHeight);
183            }
184        }
185    }
186
187    void getLogicalDisplayRect(Rect out) {
188        updateDisplayInfo();
189        // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
190        final int orientation = mDisplayInfo.rotation;
191        boolean rotated = (orientation == Surface.ROTATION_90
192                || orientation == Surface.ROTATION_270);
193        final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
194        final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
195        int width = mDisplayInfo.logicalWidth;
196        int left = (physWidth - width) / 2;
197        int height = mDisplayInfo.logicalHeight;
198        int top = (physHeight - height) / 2;
199        out.set(left, top, left + width, top + height);
200    }
201
202    /** Refer to {@link WindowManagerService#attachStack(int, int)} */
203    void attachStack(TaskStack stack) {
204        if (stack.mStackId == HOME_STACK_ID) {
205            if (mHomeStack != null) {
206                throw new IllegalArgumentException("attachStack: HOME_STACK_ID (0) not first.");
207            }
208            mHomeStack = stack;
209        }
210        mStacks.add(stack);
211        layoutNeeded = true;
212    }
213
214    void moveStack(TaskStack stack, boolean toTop) {
215        mStacks.remove(stack);
216        mStacks.add(toTop ? mStacks.size() : 0, stack);
217    }
218
219    void detachStack(TaskStack stack) {
220        mStacks.remove(stack);
221    }
222
223    /**
224     * Propagate the new bounds to all child stacks.
225     * @param contentRect The bounds to apply at the top level.
226     */
227    void resize(Rect contentRect) {
228        mContentRect.set(contentRect);
229    }
230
231    int stackIdFromPoint(int x, int y) {
232        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
233            final TaskStack stack = mStacks.get(stackNdx);
234            stack.getBounds(mTmpRect);
235            if (mTmpRect.contains(x, y)) {
236                return stack.mStackId;
237            }
238        }
239        return -1;
240    }
241
242    void setTouchExcludeRegion(TaskStack focusedStack) {
243        mTouchExcludeRegion.set(mBaseDisplayRect);
244        WindowList windows = getWindowList();
245        for (int i = windows.size() - 1; i >= 0; --i) {
246            final WindowState win = windows.get(i);
247            final TaskStack stack = win.getStack();
248            if (win.isVisibleLw() && stack != null && stack != focusedStack) {
249                mTmpRect.set(win.mVisibleFrame);
250                mTmpRect.intersect(win.mVisibleInsets);
251                mTouchExcludeRegion.op(mTmpRect, Region.Op.DIFFERENCE);
252            }
253        }
254    }
255
256    void switchUserStacks(int newUserId) {
257        final WindowList windows = getWindowList();
258        for (int i = 0; i < windows.size(); i++) {
259            final WindowState win = windows.get(i);
260            if (win.isHiddenFromUserLocked()) {
261                if (DEBUG_VISIBILITY) Slog.w(TAG, "user changing " + newUserId + " hiding "
262                        + win + ", attrs=" + win.mAttrs.type + ", belonging to "
263                        + win.mOwnerUid);
264                win.hideLw(false);
265            }
266        }
267
268        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
269            mStacks.get(stackNdx).switchUser(newUserId);
270        }
271    }
272
273    void resetAnimationBackgroundAnimator() {
274        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
275            mStacks.get(stackNdx).resetAnimationBackgroundAnimator();
276        }
277    }
278
279    boolean animateDimLayers() {
280        boolean result = false;
281        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
282            result |= mStacks.get(stackNdx).animateDimLayers();
283        }
284        return result;
285    }
286
287    void resetDimming() {
288        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
289            mStacks.get(stackNdx).resetDimmingTag();
290        }
291    }
292
293    boolean isDimming() {
294        boolean result = false;
295        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
296            result |= mStacks.get(stackNdx).isDimming();
297        }
298        return result;
299    }
300
301    void stopDimmingIfNeeded() {
302        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
303            mStacks.get(stackNdx).stopDimmingIfNeeded();
304        }
305    }
306
307    void close() {
308        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
309            mStacks.get(stackNdx).close();
310        }
311    }
312
313    public void dump(String prefix, PrintWriter pw) {
314        pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
315        final String subPrefix = "  " + prefix;
316        pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
317            pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
318            pw.print("dpi");
319            if (mInitialDisplayWidth != mBaseDisplayWidth
320                    || mInitialDisplayHeight != mBaseDisplayHeight
321                    || mInitialDisplayDensity != mBaseDisplayDensity) {
322                pw.print(" base=");
323                pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
324                pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
325            }
326            pw.print(" cur=");
327            pw.print(mDisplayInfo.logicalWidth);
328            pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
329            pw.print(" app=");
330            pw.print(mDisplayInfo.appWidth);
331            pw.print("x"); pw.print(mDisplayInfo.appHeight);
332            pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
333            pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
334            pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
335            pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
336            pw.print(subPrefix); pw.print("layoutNeeded="); pw.println(layoutNeeded);
337        for (int stackNdx = mStacks.size() - 1; stackNdx >= 0; --stackNdx) {
338            final TaskStack stack = mStacks.get(stackNdx);
339            pw.print(prefix); pw.print("mStacks[" + stackNdx + "]"); pw.println(stack.mStackId);
340            stack.dump(prefix + "  ", pw);
341        }
342        pw.println();
343        pw.println("  Application tokens in bottom up Z order:");
344        int ndx = 0;
345        final int numStacks = mStacks.size();
346        for (int stackNdx = 0; stackNdx < numStacks; ++stackNdx) {
347            ArrayList<Task> tasks = mStacks.get(stackNdx).getTasks();
348            for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
349                AppTokenList tokens = tasks.get(taskNdx).mAppTokens;
350                for (int tokenNdx = tokens.size() - 1; tokenNdx >= 0; --tokenNdx) {
351                    final AppWindowToken wtoken = tokens.get(tokenNdx);
352                    pw.print("  App #"); pw.print(ndx++);
353                            pw.print(' '); pw.print(wtoken); pw.println(":");
354                    wtoken.dump(pw, "    ");
355                }
356            }
357        }
358        if (ndx == 0) {
359            pw.println("    None");
360        }
361        pw.println();
362        if (!mExitingTokens.isEmpty()) {
363            pw.println();
364            pw.println("  Exiting tokens:");
365            for (int i=mExitingTokens.size()-1; i>=0; i--) {
366                WindowToken token = mExitingTokens.get(i);
367                pw.print("  Exiting #"); pw.print(i);
368                pw.print(' '); pw.print(token);
369                pw.println(':');
370                token.dump(pw, "    ");
371            }
372        }
373        pw.println();
374    }
375
376    @Override
377    public String toString() {
378        return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mStacks;
379    }
380}
381