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