Craig Mautner | 59c0097 | 2012-07-30 12:10:24 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.server.wm; |
| 18 | |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 19 | import android.os.Debug; |
| 20 | import android.util.Slog; |
Craig Mautner | b1fd65c0 | 2013-02-05 13:34:57 -0800 | [diff] [blame] | 21 | import android.util.SparseArray; |
Craig Mautner | b47bbc3 | 2012-08-22 17:41:48 -0700 | [diff] [blame] | 22 | import android.view.Display; |
Craig Mautner | 59c0097 | 2012-07-30 12:10:24 -0700 | [diff] [blame] | 23 | import android.view.DisplayInfo; |
| 24 | |
| 25 | import java.io.PrintWriter; |
| 26 | import java.util.ArrayList; |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 27 | import java.util.Iterator; |
| 28 | import java.util.NoSuchElementException; |
Craig Mautner | 59c0097 | 2012-07-30 12:10:24 -0700 | [diff] [blame] | 29 | |
| 30 | class DisplayContentList extends ArrayList<DisplayContent> { |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Utility class for keeping track of the WindowStates and other pertinent contents of a |
| 35 | * particular Display. |
| 36 | * |
| 37 | * IMPORTANT: No method from this class should ever be used without holding |
| 38 | * WindowManagerService.mWindowMap. |
| 39 | */ |
| 40 | class DisplayContent { |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 41 | private final static String TAG = "DisplayContent"; |
Craig Mautner | 59c0097 | 2012-07-30 12:10:24 -0700 | [diff] [blame] | 42 | |
| 43 | /** Unique identifier of this stack. */ |
| 44 | private final int mDisplayId; |
| 45 | |
| 46 | /** Z-ordered (bottom-most first) list of all Window objects. Assigned to an element |
| 47 | * from mDisplayWindows; */ |
| 48 | private WindowList mWindows = new WindowList(); |
| 49 | |
Craig Mautner | 59c0097 | 2012-07-30 12:10:24 -0700 | [diff] [blame] | 50 | // This protects the following display size properties, so that |
| 51 | // getDisplaySize() doesn't need to acquire the global lock. This is |
| 52 | // needed because the window manager sometimes needs to use ActivityThread |
| 53 | // while it has its global state locked (for example to load animation |
| 54 | // resources), but the ActivityThread also needs get the current display |
| 55 | // size sometimes when it has its package lock held. |
| 56 | // |
| 57 | // These will only be modified with both mWindowMap and mDisplaySizeLock |
| 58 | // held (in that order) so the window manager doesn't need to acquire this |
| 59 | // lock when needing these values in its normal operation. |
| 60 | final Object mDisplaySizeLock = new Object(); |
| 61 | int mInitialDisplayWidth = 0; |
| 62 | int mInitialDisplayHeight = 0; |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 63 | int mInitialDisplayDensity = 0; |
Craig Mautner | 59c0097 | 2012-07-30 12:10:24 -0700 | [diff] [blame] | 64 | int mBaseDisplayWidth = 0; |
| 65 | int mBaseDisplayHeight = 0; |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 66 | int mBaseDisplayDensity = 0; |
Craig Mautner | 2d5618c | 2012-10-18 13:55:47 -0700 | [diff] [blame] | 67 | private final DisplayInfo mDisplayInfo = new DisplayInfo(); |
| 68 | private final Display mDisplay; |
Craig Mautner | 59c0097 | 2012-07-30 12:10:24 -0700 | [diff] [blame] | 69 | |
Craig Mautner | 3983419 | 2012-09-02 07:47:24 -0700 | [diff] [blame] | 70 | // Accessed directly by all users. |
| 71 | boolean layoutNeeded; |
Craig Mautner | 76a7165 | 2012-09-03 23:23:58 -0700 | [diff] [blame] | 72 | int pendingLayoutChanges; |
Craig Mautner | 69b0818 | 2012-09-05 13:07:13 -0700 | [diff] [blame] | 73 | final boolean isDefaultDisplay; |
Craig Mautner | 3983419 | 2012-09-02 07:47:24 -0700 | [diff] [blame] | 74 | |
Craig Mautner | 2d5618c | 2012-10-18 13:55:47 -0700 | [diff] [blame] | 75 | /** |
Craig Mautner | b1fd65c0 | 2013-02-05 13:34:57 -0800 | [diff] [blame] | 76 | * List controlling the ordering of windows in different applications which must |
| 77 | * be kept in sync with ActivityManager. |
| 78 | */ |
| 79 | final AppTokenList mAppTokens = new AppTokenList(); |
| 80 | |
| 81 | /** |
| 82 | * AppWindowTokens in the Z order they were in at the start of an animation. Between |
| 83 | * animations this list is maintained in the exact order of mAppTokens. If tokens |
| 84 | * are added to mAppTokens during an animation an attempt is made to insert them at the same |
| 85 | * logical location in this list. Note that this list is always in sync with mWindows. |
| 86 | */ |
| 87 | AppTokenList mAnimatingAppTokens = new AppTokenList(); |
| 88 | |
| 89 | /** |
| 90 | * Window tokens that are in the process of exiting, but still |
| 91 | * on screen for animations. |
| 92 | */ |
| 93 | final ArrayList<WindowToken> mExitingTokens = new ArrayList<WindowToken>(); |
| 94 | |
| 95 | /** |
| 96 | * Application tokens that are in the process of exiting, but still |
| 97 | * on screen for animations. |
| 98 | */ |
| 99 | final AppTokenList mExitingAppTokens = new AppTokenList(); |
| 100 | |
| 101 | /** |
| 102 | * Sorted most recent at top, oldest at [0]. |
| 103 | */ |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 104 | ArrayList<TaskList> mTaskLists = new ArrayList<TaskList>(); |
Craig Mautner | b1fd65c0 | 2013-02-05 13:34:57 -0800 | [diff] [blame] | 105 | SparseArray<TaskList> mTaskIdToTaskList = new SparseArray<TaskList>(); |
| 106 | |
| 107 | /** |
Craig Mautner | 2d5618c | 2012-10-18 13:55:47 -0700 | [diff] [blame] | 108 | * @param display May not be null. |
| 109 | */ |
Craig Mautner | b47bbc3 | 2012-08-22 17:41:48 -0700 | [diff] [blame] | 110 | DisplayContent(Display display) { |
| 111 | mDisplay = display; |
| 112 | mDisplayId = display.getDisplayId(); |
| 113 | display.getDisplayInfo(mDisplayInfo); |
Craig Mautner | 69b0818 | 2012-09-05 13:07:13 -0700 | [diff] [blame] | 114 | isDefaultDisplay = mDisplayId == Display.DEFAULT_DISPLAY; |
Craig Mautner | 59c0097 | 2012-07-30 12:10:24 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | int getDisplayId() { |
| 118 | return mDisplayId; |
| 119 | } |
| 120 | |
| 121 | WindowList getWindowList() { |
| 122 | return mWindows; |
| 123 | } |
| 124 | |
Craig Mautner | b47bbc3 | 2012-08-22 17:41:48 -0700 | [diff] [blame] | 125 | Display getDisplay() { |
| 126 | return mDisplay; |
| 127 | } |
| 128 | |
Craig Mautner | 59c0097 | 2012-07-30 12:10:24 -0700 | [diff] [blame] | 129 | DisplayInfo getDisplayInfo() { |
| 130 | return mDisplayInfo; |
| 131 | } |
| 132 | |
Craig Mautner | 722285e | 2012-09-07 13:55:58 -0700 | [diff] [blame] | 133 | public void updateDisplayInfo() { |
| 134 | mDisplay.getDisplayInfo(mDisplayInfo); |
| 135 | } |
| 136 | |
Craig Mautner | b1fd65c0 | 2013-02-05 13:34:57 -0800 | [diff] [blame] | 137 | /** |
| 138 | * Find the location to insert a new AppWindowToken into the window-ordered app token list. |
| 139 | * @param addPos The location the token was inserted into in mAppTokens. |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 140 | * @param wtoken The token to insert. |
Craig Mautner | b1fd65c0 | 2013-02-05 13:34:57 -0800 | [diff] [blame] | 141 | */ |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 142 | void addAppToken(final int addPos, final AppWindowToken wtoken) { |
| 143 | mAppTokens.add(addPos, wtoken); |
Craig Mautner | b1fd65c0 | 2013-02-05 13:34:57 -0800 | [diff] [blame] | 144 | |
| 145 | if (addPos == 0 || addPos == mAnimatingAppTokens.size()) { |
| 146 | // It was inserted into the beginning or end of mAppTokens. Honor that. |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 147 | mAnimatingAppTokens.add(addPos, wtoken); |
Craig Mautner | b1fd65c0 | 2013-02-05 13:34:57 -0800 | [diff] [blame] | 148 | } else { |
| 149 | // Find the item immediately above the mAppTokens insertion point and put the token |
| 150 | // immediately below that one in mAnimatingAppTokens. |
| 151 | final AppWindowToken aboveAnchor = mAppTokens.get(addPos + 1); |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 152 | mAnimatingAppTokens.add(mAnimatingAppTokens.indexOf(aboveAnchor), wtoken); |
Craig Mautner | b1fd65c0 | 2013-02-05 13:34:57 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 155 | TaskList task = mTaskIdToTaskList.get(wtoken.groupId); |
Craig Mautner | b1fd65c0 | 2013-02-05 13:34:57 -0800 | [diff] [blame] | 156 | if (task == null) { |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 157 | task = new TaskList(wtoken, this); |
| 158 | mTaskIdToTaskList.put(wtoken.groupId, task); |
| 159 | mTaskLists.add(task); |
| 160 | } else { |
| 161 | task.mAppTokens.add(wtoken); |
Craig Mautner | b1fd65c0 | 2013-02-05 13:34:57 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | void removeAppToken(final AppWindowToken wtoken) { |
| 166 | mAppTokens.remove(wtoken); |
| 167 | mAnimatingAppTokens.remove(wtoken); |
| 168 | final int taskId = wtoken.groupId; |
| 169 | final TaskList task = mTaskIdToTaskList.get(taskId); |
| 170 | if (task != null) { |
| 171 | AppTokenList appTokens = task.mAppTokens; |
| 172 | appTokens.remove(wtoken); |
| 173 | if (appTokens.size() == 0) { |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 174 | mTaskLists.remove(task); |
Craig Mautner | b1fd65c0 | 2013-02-05 13:34:57 -0800 | [diff] [blame] | 175 | mTaskIdToTaskList.delete(taskId); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void refillAnimatingAppTokens() { |
| 181 | mAnimatingAppTokens.clear(); |
| 182 | mAnimatingAppTokens.addAll(mAppTokens); |
| 183 | } |
| 184 | |
| 185 | void setAppTaskId(AppWindowToken wtoken, int newTaskId) { |
| 186 | final int taskId = wtoken.groupId; |
| 187 | TaskList task = mTaskIdToTaskList.get(taskId); |
| 188 | if (task != null) { |
| 189 | AppTokenList appTokens = task.mAppTokens; |
| 190 | appTokens.remove(wtoken); |
| 191 | if (appTokens.size() == 0) { |
| 192 | mTaskIdToTaskList.delete(taskId); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | task = mTaskIdToTaskList.get(newTaskId); |
| 197 | if (task == null) { |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 198 | task = new TaskList(wtoken, this); |
Craig Mautner | b1fd65c0 | 2013-02-05 13:34:57 -0800 | [diff] [blame] | 199 | mTaskIdToTaskList.put(newTaskId, task); |
| 200 | } else { |
| 201 | task.mAppTokens.add(wtoken); |
| 202 | } |
| 203 | |
| 204 | wtoken.groupId = newTaskId; |
| 205 | } |
| 206 | |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 207 | class TaskListsIterator implements Iterator<TaskList> { |
| 208 | private int mCur; |
| 209 | private boolean mReverse; |
| 210 | |
| 211 | TaskListsIterator() { |
| 212 | this(false); |
| 213 | } |
| 214 | |
| 215 | TaskListsIterator(boolean reverse) { |
| 216 | mReverse = reverse; |
| 217 | int numTaskLists = mTaskLists.size(); |
| 218 | mCur = reverse ? numTaskLists - 1 : 0; |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public boolean hasNext() { |
| 223 | if (mReverse) { |
| 224 | return mCur >= 0; |
| 225 | } |
| 226 | return mCur < mTaskLists.size(); |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public TaskList next() { |
| 231 | if (hasNext()) { |
| 232 | TaskList taskList = mTaskLists.get(mCur); |
| 233 | mCur += (mReverse ? -1 : 1); |
| 234 | return taskList; |
| 235 | } |
| 236 | throw new NoSuchElementException(); |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public void remove() { |
| 241 | throw new IllegalArgumentException(); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | class AppTokenIterator implements Iterator<AppWindowToken> { |
| 246 | final TaskListsIterator mIterator; |
| 247 | final boolean mReverse; |
| 248 | int mCur; |
| 249 | TaskList mTaskList; |
| 250 | |
| 251 | public AppTokenIterator() { |
| 252 | this(false); |
| 253 | } |
| 254 | |
| 255 | public AppTokenIterator(boolean reverse) { |
| 256 | mReverse = reverse; |
| 257 | mIterator = new TaskListsIterator(reverse); |
| 258 | getNextTaskList(); |
| 259 | } |
| 260 | |
| 261 | private void getNextTaskList() { |
| 262 | if (mIterator.hasNext()) { |
| 263 | mTaskList = mIterator.next(); |
| 264 | mCur = mReverse ? mTaskList.mAppTokens.size() - 1 : 0; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public boolean hasNext() { |
| 270 | if (mTaskList == null) { |
| 271 | return false; |
| 272 | } |
| 273 | if (mReverse) { |
| 274 | return mCur >= 0; |
| 275 | } |
| 276 | return mCur < mTaskList.mAppTokens.size(); |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public AppWindowToken next() { |
| 281 | if (hasNext()) { |
| 282 | AppWindowToken wtoken = mTaskList.mAppTokens.get(mCur); |
| 283 | mCur += mReverse ? -1 : 1; |
| 284 | if (!hasNext()) { |
| 285 | getNextTaskList(); |
| 286 | } |
| 287 | return wtoken; |
| 288 | } |
| 289 | throw new NoSuchElementException(); |
| 290 | } |
| 291 | |
| 292 | @Override |
| 293 | public void remove() { |
| 294 | throw new IllegalArgumentException(); |
| 295 | } |
Craig Mautner | 30e2d72 | 2013-02-12 11:30:16 -0800 | [diff] [blame^] | 296 | |
| 297 | int size() { |
| 298 | int size = 0; |
| 299 | final TaskListsIterator iterator = new TaskListsIterator(); |
| 300 | while (iterator.hasNext()) { |
| 301 | size += iterator.next().mAppTokens.size(); |
| 302 | } |
| 303 | return size; |
| 304 | } |
Craig Mautner | 05d6272ba | 2013-02-11 09:39:27 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | void verifyAppTokens() { |
| 308 | AppTokenIterator iterator = new AppTokenIterator(); |
| 309 | for (int i = 0; i < mAppTokens.size(); ++i) { |
| 310 | if (!iterator.hasNext()) { |
| 311 | Slog.e(TAG, "compareAppTokens: More mAppTokens than TaskList tokens. Callers=" |
| 312 | + Debug.getCallers(4)); |
| 313 | while (i < mAppTokens.size()) { |
| 314 | Slog.e(TAG, "compareAppTokens: mAppTokens[" + i + "]=" + mAppTokens.get(i)); |
| 315 | i++; |
| 316 | } |
| 317 | return; |
| 318 | } |
| 319 | AppWindowToken appToken = mAppTokens.get(i); |
| 320 | AppWindowToken taskListToken = iterator.next(); |
| 321 | if (appToken != taskListToken) { |
| 322 | Slog.e(TAG, "compareAppTokens: Mismatch at " + i + " appToken=" + appToken |
| 323 | + " taskListToken=" + taskListToken + ". Callers=" + Debug.getCallers(4)); |
| 324 | } |
| 325 | } |
| 326 | if (iterator.hasNext()) { |
| 327 | Slog.e(TAG, "compareAppTokens: More TaskList tokens than mAppTokens Callers=" |
| 328 | + Debug.getCallers(4)); |
| 329 | } |
| 330 | } |
| 331 | |
Craig Mautner | a91f9e2 | 2012-09-14 16:22:08 -0700 | [diff] [blame] | 332 | public void dump(String prefix, PrintWriter pw) { |
| 333 | pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId); |
| 334 | final String subPrefix = " " + prefix; |
| 335 | pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x"); |
| 336 | pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity); |
| 337 | pw.print("dpi"); |
| 338 | if (mInitialDisplayWidth != mBaseDisplayWidth |
| 339 | || mInitialDisplayHeight != mBaseDisplayHeight |
| 340 | || mInitialDisplayDensity != mBaseDisplayDensity) { |
| 341 | pw.print(" base="); |
| 342 | pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight); |
| 343 | pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi"); |
| 344 | } |
| 345 | pw.print(" cur="); |
| 346 | pw.print(mDisplayInfo.logicalWidth); |
| 347 | pw.print("x"); pw.print(mDisplayInfo.logicalHeight); |
| 348 | pw.print(" app="); |
| 349 | pw.print(mDisplayInfo.appWidth); |
| 350 | pw.print("x"); pw.print(mDisplayInfo.appHeight); |
| 351 | pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth); |
| 352 | pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight); |
| 353 | pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth); |
| 354 | pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight); |
Craig Mautner | b1fd65c0 | 2013-02-05 13:34:57 -0800 | [diff] [blame] | 355 | if (mAppTokens.size() > 0) { |
| 356 | pw.println(); |
| 357 | pw.println(" Application tokens in Z order:"); |
| 358 | for (int i=mAppTokens.size()-1; i>=0; i--) { |
| 359 | pw.print(" App #"); pw.print(i); |
| 360 | pw.print(' '); pw.print(mAppTokens.get(i)); pw.println(":"); |
| 361 | mAppTokens.get(i).dump(pw, " "); |
| 362 | } |
| 363 | } |
| 364 | if (mExitingTokens.size() > 0) { |
| 365 | pw.println(); |
| 366 | pw.println(" Exiting tokens:"); |
| 367 | for (int i=mExitingTokens.size()-1; i>=0; i--) { |
| 368 | WindowToken token = mExitingTokens.get(i); |
| 369 | pw.print(" Exiting #"); pw.print(i); |
| 370 | pw.print(' '); pw.print(token); |
| 371 | pw.println(':'); |
| 372 | token.dump(pw, " "); |
| 373 | } |
| 374 | } |
| 375 | if (mExitingAppTokens.size() > 0) { |
| 376 | pw.println(); |
| 377 | pw.println(" Exiting application tokens:"); |
| 378 | for (int i=mExitingAppTokens.size()-1; i>=0; i--) { |
| 379 | WindowToken token = mExitingAppTokens.get(i); |
| 380 | pw.print(" Exiting App #"); pw.print(i); |
| 381 | pw.print(' '); pw.print(token); |
| 382 | pw.println(':'); |
| 383 | token.dump(pw, " "); |
| 384 | } |
| 385 | } |
| 386 | if (mTaskIdToTaskList.size() > 0) { |
| 387 | pw.println(); |
| 388 | for (int i = 0; i < mTaskIdToTaskList.size(); ++i) { |
| 389 | pw.print(" TaskList #"); pw.print(i); |
| 390 | pw.print(" taskId="); pw.println(mTaskIdToTaskList.keyAt(i)); |
| 391 | pw.print(" mAppTokens="); |
| 392 | pw.println(mTaskIdToTaskList.valueAt(i).mAppTokens); |
| 393 | pw.println(); |
| 394 | } |
| 395 | } |
| 396 | pw.print(subPrefix); pw.print("layoutNeeded="); pw.println(layoutNeeded); |
Craig Mautner | 59c0097 | 2012-07-30 12:10:24 -0700 | [diff] [blame] | 397 | pw.println(); |
| 398 | } |
| 399 | } |