summaryrefslogtreecommitdiffstats
path: root/services/java/com/android/server/wm/DisplayContent.java
blob: 0b6c514de8a27251cf1d27ed067bacc0fab9e76d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.wm;

import android.os.Debug;
import android.util.Slog;
import android.util.SparseArray;
import android.view.Display;
import android.view.DisplayInfo;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;

class DisplayContentList extends ArrayList<DisplayContent> {
}

/**
 * Utility class for keeping track of the WindowStates and other pertinent contents of a
 * particular Display.
 *
 * IMPORTANT: No method from this class should ever be used without holding
 * WindowManagerService.mWindowMap.
 */
class DisplayContent {
    private final static String TAG = "DisplayContent";

    /** Unique identifier of this stack. */
    private final int mDisplayId;

    /** Z-ordered (bottom-most first) list of all Window objects. Assigned to an element
     * from mDisplayWindows; */
    private WindowList mWindows = new WindowList();

    // This protects the following display size properties, so that
    // getDisplaySize() doesn't need to acquire the global lock.  This is
    // needed because the window manager sometimes needs to use ActivityThread
    // while it has its global state locked (for example to load animation
    // resources), but the ActivityThread also needs get the current display
    // size sometimes when it has its package lock held.
    //
    // These will only be modified with both mWindowMap and mDisplaySizeLock
    // held (in that order) so the window manager doesn't need to acquire this
    // lock when needing these values in its normal operation.
    final Object mDisplaySizeLock = new Object();
    int mInitialDisplayWidth = 0;
    int mInitialDisplayHeight = 0;
    int mInitialDisplayDensity = 0;
    int mBaseDisplayWidth = 0;
    int mBaseDisplayHeight = 0;
    int mBaseDisplayDensity = 0;
    private final DisplayInfo mDisplayInfo = new DisplayInfo();
    private final Display mDisplay;

    // Accessed directly by all users.
    boolean layoutNeeded;
    int pendingLayoutChanges;
    final boolean isDefaultDisplay;

    /**
     * List controlling the ordering of windows in different applications which must
     * be kept in sync with ActivityManager.
     */
    final AppTokenList mAppTokens = new AppTokenList();

    /**
     * AppWindowTokens in the Z order they were in at the start of an animation. Between
     * animations this list is maintained in the exact order of mAppTokens. If tokens
     * are added to mAppTokens during an animation an attempt is made to insert them at the same
     * logical location in this list. Note that this list is always in sync with mWindows.
     */
    AppTokenList mAnimatingAppTokens = new AppTokenList();

    /**
     * Window tokens that are in the process of exiting, but still
     * on screen for animations.
     */
    final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>();

    /**
     * Application tokens that are in the process of exiting, but still
     * on screen for animations.
     */
    final AppTokenList mExitingAppTokens = new AppTokenList();

    /**
     * Sorted most recent at top, oldest at [0].
     */
    ArrayList<TaskList> mTaskLists = new ArrayList<TaskList>();
    SparseArray<TaskList> mTaskIdToTaskList = new SparseArray<TaskList>();

    /**
     * @param display May not be null.
     */
    DisplayContent(Display display) {
        mDisplay = display;
        mDisplayId = display.getDisplayId();
        display.getDisplayInfo(mDisplayInfo);
        isDefaultDisplay = mDisplayId == Display.DEFAULT_DISPLAY;
    }

    int getDisplayId() {
        return mDisplayId;
    }

    WindowList getWindowList() {
        return mWindows;
    }

    Display getDisplay() {
        return mDisplay;
    }

    DisplayInfo getDisplayInfo() {
        return mDisplayInfo;
    }

    public void updateDisplayInfo() {
        mDisplay.getDisplayInfo(mDisplayInfo);
    }

    /**
     *  Find the location to insert a new AppWindowToken into the window-ordered app token list.
     * @param addPos The location the token was inserted into in mAppTokens.
     * @param wtoken The token to insert.
     */
    void addAppToken(final int addPos, final AppWindowToken wtoken) {
        mAppTokens.add(addPos, wtoken);

        if (addPos == 0 || addPos == mAnimatingAppTokens.size()) {
            // It was inserted into the beginning or end of mAppTokens. Honor that.
            mAnimatingAppTokens.add(addPos, wtoken);
        } else {
            // Find the item immediately above the mAppTokens insertion point and put the token
            // immediately below that one in mAnimatingAppTokens.
            final AppWindowToken aboveAnchor = mAppTokens.get(addPos + 1);
            mAnimatingAppTokens.add(mAnimatingAppTokens.indexOf(aboveAnchor), wtoken);
        }

        TaskList task = mTaskIdToTaskList.get(wtoken.groupId);
        if (task == null) {
            task = new TaskList(wtoken, this);
            mTaskIdToTaskList.put(wtoken.groupId, task);
            mTaskLists.add(task);
        } else {
            task.mAppTokens.add(wtoken);
        }
    }

    void removeAppToken(final AppWindowToken wtoken) {
        mAppTokens.remove(wtoken);
        mAnimatingAppTokens.remove(wtoken);
        final int taskId = wtoken.groupId;
        final TaskList task = mTaskIdToTaskList.get(taskId);
        if (task != null) {
            AppTokenList appTokens = task.mAppTokens;
            appTokens.remove(wtoken);
            if (appTokens.size() == 0) {
                mTaskLists.remove(task);
                mTaskIdToTaskList.delete(taskId);
            }
        }
    }

    void refillAnimatingAppTokens() {
        mAnimatingAppTokens.clear();
        AppTokenIterator iterator = new AppTokenIterator();
        while (iterator.hasNext()) {
            mAnimatingAppTokens.add(iterator.next());
        }
    }

    void setAppTaskId(AppWindowToken wtoken, int newTaskId) {
        final int taskId = wtoken.groupId;
        TaskList task = mTaskIdToTaskList.get(taskId);
        if (task != null) {
            AppTokenList appTokens = task.mAppTokens;
            appTokens.remove(wtoken);
            if (appTokens.size() == 0) {
                mTaskIdToTaskList.delete(taskId);
            }
        }

        task = mTaskIdToTaskList.get(newTaskId);
        if (task == null) {
            task = new TaskList(wtoken, this);
            mTaskIdToTaskList.put(newTaskId, task);
        } else {
            task.mAppTokens.add(wtoken);
        }

        wtoken.groupId = newTaskId;
    }

    class TaskListsIterator implements Iterator<TaskList> {
        private int mCur;
        private boolean mReverse;

        TaskListsIterator() {
            this(false);
        }

        TaskListsIterator(boolean reverse) {
            mReverse = reverse;
            int numTaskLists = mTaskLists.size();
            mCur = reverse ? numTaskLists - 1 : 0;
        }

        @Override
        public boolean hasNext() {
            if (mReverse) {
                return mCur >= 0;
            }
            return mCur < mTaskLists.size();
        }

        @Override
        public TaskList next() {
            if (hasNext()) {
                TaskList taskList = mTaskLists.get(mCur);
                mCur += (mReverse ? -1 : 1);
                return taskList;
            }
            throw new NoSuchElementException();
        }

        @Override
        public void remove() {
            throw new IllegalArgumentException();
        }
    }

    class AppTokenIterator implements Iterator<AppWindowToken> {
        final TaskListsIterator mIterator;
        final boolean mReverse;
        int mCur;
        TaskList mTaskList;

        public AppTokenIterator() {
            this(false);
        }

        public AppTokenIterator(boolean reverse) {
            mReverse = reverse;
            mIterator = new TaskListsIterator(reverse);
            getNextTaskList();
        }

        private void getNextTaskList() {
            if (mIterator.hasNext()) {
                mTaskList = mIterator.next();
                mCur = mReverse ? mTaskList.mAppTokens.size() - 1 : 0;
            }
        }

        @Override
        public boolean hasNext() {
            if (mTaskList == null) {
                return false;
            }
            if (mReverse) {
                return mCur >= 0;
            }
            return mCur < mTaskList.mAppTokens.size();
        }

        @Override
        public AppWindowToken next() {
            if (hasNext()) {
                AppWindowToken wtoken = mTaskList.mAppTokens.get(mCur);
                mCur += mReverse ? -1 : 1;
                if (!hasNext()) {
                    getNextTaskList();
                }
                return wtoken;
            }
            throw new NoSuchElementException();
        }

        @Override
        public void remove() {
            throw new IllegalArgumentException();
        }

        int size() {
            int size = 0;
            final TaskListsIterator iterator = new TaskListsIterator();
            while (iterator.hasNext()) {
                size += iterator.next().mAppTokens.size();
            }
            return size;
        }
    }

    public void dump(String prefix, PrintWriter pw) {
        pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
        final String subPrefix = "  " + prefix;
        pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
            pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
            pw.print("dpi");
            if (mInitialDisplayWidth != mBaseDisplayWidth
                    || mInitialDisplayHeight != mBaseDisplayHeight
                    || mInitialDisplayDensity != mBaseDisplayDensity) {
                pw.print(" base=");
                pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
                pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
            }
            pw.print(" cur=");
            pw.print(mDisplayInfo.logicalWidth);
            pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
            pw.print(" app=");
            pw.print(mDisplayInfo.appWidth);
            pw.print("x"); pw.print(mDisplayInfo.appHeight);
            pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
            pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
            pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
            pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
            AppTokenIterator iterator = new AppTokenIterator(true);
            int ndx = iterator.size() - 1;
            if (ndx >= 0) {
                pw.println();
                pw.println("  Application tokens in Z order:");
                while (iterator.hasNext()) {
                    AppWindowToken wtoken = iterator.next();
                    pw.print("  App #"); pw.print(ndx--);
                            pw.print(' '); pw.print(wtoken); pw.println(":");
                    wtoken.dump(pw, "    ");
                }
            }
            if (mExitingTokens.size() > 0) {
                pw.println();
                pw.println("  Exiting tokens:");
                for (int i=mExitingTokens.size()-1; i>=0; i--) {
                    WindowToken token = mExitingTokens.get(i);
                    pw.print("  Exiting #"); pw.print(i);
                    pw.print(' '); pw.print(token);
                    pw.println(':');
                    token.dump(pw, "    ");
                }
            }
            if (mExitingAppTokens.size() > 0) {
                pw.println();
                pw.println("  Exiting application tokens:");
                for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
                    WindowToken token = mExitingAppTokens.get(i);
                    pw.print("  Exiting App #"); pw.print(i);
                      pw.print(' '); pw.print(token);
                      pw.println(':');
                      token.dump(pw, "    ");
                }
            }
            if (mTaskIdToTaskList.size() > 0) {
                pw.println();
                for (int i = 0; i < mTaskIdToTaskList.size(); ++i) {
                    pw.print("  TaskList #"); pw.print(i);
                      pw.print(" taskId="); pw.println(mTaskIdToTaskList.keyAt(i));
                    pw.print("    mAppTokens=");
                      pw.println(mTaskIdToTaskList.valueAt(i).mAppTokens);
                    pw.println();
                }
            }
        pw.print(subPrefix); pw.print("layoutNeeded="); pw.println(layoutNeeded);
        pw.println();
    }
}