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