blob: 67b667ad1557ba32bba42b5be46d458307b00fd6 [file] [log] [blame]
Dianne Hackborn6e1eb762011-02-17 16:07:28 -08001/*
2 * Copyright (C) 2011 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 android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
20
21import com.android.server.wm.WindowManagerService.H;
22
23import android.content.pm.ActivityInfo;
24import android.os.Message;
25import android.os.RemoteException;
26import android.util.Slog;
27import android.view.IApplicationToken;
28import android.view.View;
29import android.view.WindowManager;
Craig Mautnere32c3072012-03-12 15:25:35 -070030import android.view.WindowManagerPolicy;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080031import android.view.animation.Animation;
32import android.view.animation.Transformation;
33
34import java.io.PrintWriter;
35import java.util.ArrayList;
36
37/**
38 * Version of WindowToken that is specifically for a particular application (or
39 * really activity) that is displaying windows.
40 */
Craig Mautnere32c3072012-03-12 15:25:35 -070041class AppWindowToken extends WindowToken {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080042 // Non-null only for application tokens.
43 final IApplicationToken appToken;
44
45 // All of the windows and child windows that are included in this
46 // application token. Note this list is NOT sorted!
47 final ArrayList<WindowState> allAppWindows = new ArrayList<WindowState>();
48
49 int groupId = -1;
50 boolean appFullscreen;
51 int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
52
53 // The input dispatching timeout for this application token in nanoseconds.
54 long inputDispatchingTimeoutNanos;
55
56 // These are used for determining when all windows associated with
57 // an activity have been drawn, so they can be made visible together
58 // at the same time.
59 int lastTransactionSequence;
60 int numInterestingWindows;
61 int numDrawnWindows;
62 boolean inPendingTransaction;
63 boolean allDrawn;
64
65 // Is this token going to be hidden in a little while? If so, it
66 // won't be taken into account for setting the screen orientation.
67 boolean willBeHidden;
68
69 // Is this window's surface needed? This is almost like hidden, except
70 // it will sometimes be true a little earlier: when the token has
71 // been shown, but is still waiting for its app transition to execute
72 // before making its windows shown.
73 boolean hiddenRequested;
74
75 // Have we told the window clients to hide themselves?
76 boolean clientHidden;
77
78 // Last visibility state we reported to the app token.
79 boolean reportedVisible;
80
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -070081 // Last drawn state we reported to the app token.
82 boolean reportedDrawn;
83
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080084 // Set to true when the token has been removed from the window mgr.
85 boolean removed;
86
87 // Have we been asked to have this token keep the screen frozen?
88 boolean freezingScreen;
89
90 boolean animating;
91 Animation animation;
92 boolean hasTransformation;
93 final Transformation transformation = new Transformation();
94
95 // Offset to the window of all layers in the token, for use by
96 // AppWindowToken animations.
97 int animLayerAdjustment;
98
99 // Information about an application starting window if displayed.
100 StartingData startingData;
101 WindowState startingWindow;
102 View startingView;
103 boolean startingDisplayed;
104 boolean startingMoved;
105 boolean firstWindowDrawn;
106
107 // Input application handle used by the input dispatcher.
Jeff Brown9302c872011-07-13 22:51:29 -0700108 final InputApplicationHandle mInputApplicationHandle;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800109
110 AppWindowToken(WindowManagerService _service, IApplicationToken _token) {
111 super(_service, _token.asBinder(),
112 WindowManager.LayoutParams.TYPE_APPLICATION, true);
113 appWindowToken = this;
114 appToken = _token;
115 mInputApplicationHandle = new InputApplicationHandle(this);
116 lastTransactionSequence = service.mTransactionSequence-1;
117 }
118
119 public void setAnimation(Animation anim) {
120 if (WindowManagerService.localLOGV) Slog.v(
121 WindowManagerService.TAG, "Setting animation in " + this + ": " + anim);
122 animation = anim;
123 animating = false;
124 anim.restrictDuration(WindowManagerService.MAX_ANIMATION_DURATION);
125 anim.scaleCurrentDuration(service.mTransitionAnimationScale);
126 int zorder = anim.getZAdjustment();
127 int adj = 0;
128 if (zorder == Animation.ZORDER_TOP) {
129 adj = WindowManagerService.TYPE_LAYER_OFFSET;
130 } else if (zorder == Animation.ZORDER_BOTTOM) {
131 adj = -WindowManagerService.TYPE_LAYER_OFFSET;
132 }
133
134 if (animLayerAdjustment != adj) {
135 animLayerAdjustment = adj;
136 updateLayers();
137 }
138 }
139
140 public void setDummyAnimation() {
141 if (animation == null) {
142 if (WindowManagerService.localLOGV) Slog.v(
143 WindowManagerService.TAG, "Setting dummy animation in " + this);
144 animation = WindowManagerService.sDummyAnimation;
145 }
146 }
147
148 public void clearAnimation() {
149 if (animation != null) {
150 animation = null;
151 animating = true;
152 }
153 }
154
155 void updateLayers() {
156 final int N = allAppWindows.size();
157 final int adj = animLayerAdjustment;
158 for (int i=0; i<N; i++) {
159 WindowState w = allAppWindows.get(i);
160 w.mAnimLayer = w.mLayer + adj;
161 if (WindowManagerService.DEBUG_LAYERS) Slog.v(WindowManagerService.TAG, "Updating layer " + w + ": "
162 + w.mAnimLayer);
163 if (w == service.mInputMethodTarget && !service.mInputMethodTargetWaitingAnim) {
164 service.setInputMethodAnimLayerAdjustment(adj);
165 }
166 if (w == service.mWallpaperTarget && service.mLowerWallpaperTarget == null) {
167 service.setWallpaperAnimLayerAdjustmentLocked(adj);
168 }
169 }
170 }
171
172 void sendAppVisibilityToClients() {
173 final int N = allAppWindows.size();
174 for (int i=0; i<N; i++) {
175 WindowState win = allAppWindows.get(i);
176 if (win == startingWindow && clientHidden) {
177 // Don't hide the starting window.
178 continue;
179 }
180 try {
181 if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(WindowManagerService.TAG,
182 "Setting visibility of " + win + ": " + (!clientHidden));
183 win.mClient.dispatchAppVisibility(!clientHidden);
184 } catch (RemoteException e) {
185 }
186 }
187 }
188
189 void showAllWindowsLocked() {
190 final int NW = allAppWindows.size();
191 for (int i=0; i<NW; i++) {
192 WindowState w = allAppWindows.get(i);
193 if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(WindowManagerService.TAG,
194 "performing show on: " + w);
195 w.performShowLocked();
196 }
197 }
198
Craig Mautnere32c3072012-03-12 15:25:35 -0700199
200 private boolean stepAnimation(long currentTime) {
Craig Mautnerdbb79912012-03-01 18:59:14 -0800201 if (animation == null) {
202 return false;
203 }
204 transformation.clear();
205 final boolean more = animation.getTransformation(currentTime, transformation);
206 if (WindowManagerService.DEBUG_ANIM) Slog.v(
207 WindowManagerService.TAG, "Stepped animation in " + this +
208 ": more=" + more + ", xform=" + transformation);
209 if (!more) {
210 animation = null;
211 if (WindowManagerService.DEBUG_ANIM) Slog.v(
212 WindowManagerService.TAG, "Finished animation in " + this +
213 " @ " + currentTime);
214 }
215 hasTransformation = more;
216 return more;
217 }
218
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800219 // This must be called while inside a transaction.
Craig Mautnere32c3072012-03-12 15:25:35 -0700220 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
Dianne Hackbornbc1aa7b2011-09-20 11:20:31 -0700221 if (!service.mDisplayFrozen && service.mPolicy.isScreenOnFully()) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800222 // We will run animations as long as the display isn't frozen.
223
224 if (animation == WindowManagerService.sDummyAnimation) {
225 // This guy is going to animate, but not yet. For now count
226 // it as not animating for purposes of scheduling transactions;
227 // when it is really time to animate, this will be set to
228 // a real animation and the next call will execute normally.
229 return false;
230 }
231
232 if ((allDrawn || animating || startingDisplayed) && animation != null) {
233 if (!animating) {
234 if (WindowManagerService.DEBUG_ANIM) Slog.v(
235 WindowManagerService.TAG, "Starting animation in " + this +
236 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
237 + " scale=" + service.mTransitionAnimationScale
238 + " allDrawn=" + allDrawn + " animating=" + animating);
239 animation.initialize(dw, dh, dw, dh);
240 animation.setStartTime(currentTime);
241 animating = true;
242 }
Craig Mautner1dd3ed02012-03-16 14:01:16 -0700243 if (stepAnimation(currentTime)) {
244 // we're done!
245 return true;
246 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800247 }
248 } else if (animation != null) {
249 // If the display is frozen, and there is a pending animation,
250 // clear it and make sure we run the cleanup code.
251 animating = true;
252 animation = null;
253 }
254
255 hasTransformation = false;
256
257 if (!animating) {
258 return false;
259 }
260
Craig Mautnere32c3072012-03-12 15:25:35 -0700261 service.mPendingLayoutChanges |= WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800262 clearAnimation();
263 animating = false;
264 if (animLayerAdjustment != 0) {
265 animLayerAdjustment = 0;
266 updateLayers();
267 }
268 if (service.mInputMethodTarget != null && service.mInputMethodTarget.mAppToken == this) {
269 service.moveInputMethodWindowsIfNeededLocked(true);
270 }
271
272 if (WindowManagerService.DEBUG_ANIM) Slog.v(
273 WindowManagerService.TAG, "Animation done in " + this
274 + ": reportedVisible=" + reportedVisible);
275
276 transformation.clear();
277
278 final int N = windows.size();
279 for (int i=0; i<N; i++) {
280 windows.get(i).finishExit();
281 }
282 updateReportedVisibilityLocked();
283
284 return false;
285 }
286
287 void updateReportedVisibilityLocked() {
288 if (appToken == null) {
289 return;
290 }
291
292 int numInteresting = 0;
293 int numVisible = 0;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700294 int numDrawn = 0;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800295 boolean nowGone = true;
296
297 if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(WindowManagerService.TAG, "Update reported visibility: " + this);
298 final int N = allAppWindows.size();
299 for (int i=0; i<N; i++) {
300 WindowState win = allAppWindows.get(i);
301 if (win == startingWindow || win.mAppFreezing
302 || win.mViewVisibility != View.VISIBLE
303 || win.mAttrs.type == TYPE_APPLICATION_STARTING
304 || win.mDestroying) {
305 continue;
306 }
307 if (WindowManagerService.DEBUG_VISIBILITY) {
308 Slog.v(WindowManagerService.TAG, "Win " + win + ": isDrawn="
309 + win.isDrawnLw()
310 + ", isAnimating=" + win.isAnimating());
311 if (!win.isDrawnLw()) {
312 Slog.v(WindowManagerService.TAG, "Not displayed: s=" + win.mSurface
313 + " pv=" + win.mPolicyVisibility
314 + " dp=" + win.mDrawPending
315 + " cdp=" + win.mCommitDrawPending
316 + " ah=" + win.mAttachedHidden
317 + " th="
318 + (win.mAppToken != null
319 ? win.mAppToken.hiddenRequested : false)
320 + " a=" + win.mAnimating);
321 }
322 }
323 numInteresting++;
324 if (win.isDrawnLw()) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700325 numDrawn++;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800326 if (!win.isAnimating()) {
327 numVisible++;
328 }
329 nowGone = false;
330 } else if (win.isAnimating()) {
331 nowGone = false;
332 }
333 }
334
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700335 boolean nowDrawn = numInteresting > 0 && numDrawn >= numInteresting;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800336 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700337 if (!nowGone) {
338 // If the app is not yet gone, then it can only become visible/drawn.
339 if (!nowDrawn) {
340 nowDrawn = reportedDrawn;
341 }
342 if (!nowVisible) {
343 nowVisible = reportedVisible;
344 }
345 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800346 if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(WindowManagerService.TAG, "VIS " + this + ": interesting="
347 + numInteresting + " visible=" + numVisible);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700348 if (nowDrawn != reportedDrawn) {
349 if (nowDrawn) {
350 Message m = service.mH.obtainMessage(
351 H.REPORT_APPLICATION_TOKEN_DRAWN, this);
352 service.mH.sendMessage(m);
353 }
354 reportedDrawn = nowDrawn;
355 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800356 if (nowVisible != reportedVisible) {
357 if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(
358 WindowManagerService.TAG, "Visibility changed in " + this
359 + ": vis=" + nowVisible);
360 reportedVisible = nowVisible;
361 Message m = service.mH.obtainMessage(
362 H.REPORT_APPLICATION_TOKEN_WINDOWS,
363 nowVisible ? 1 : 0,
364 nowGone ? 1 : 0,
365 this);
366 service.mH.sendMessage(m);
367 }
368 }
369
370 WindowState findMainWindow() {
371 int j = windows.size();
372 while (j > 0) {
373 j--;
374 WindowState win = windows.get(j);
375 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
376 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
377 return win;
378 }
379 }
380 return null;
381 }
382
Craig Mautnerdbb79912012-03-01 18:59:14 -0800383 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800384 void dump(PrintWriter pw, String prefix) {
385 super.dump(pw, prefix);
386 if (appToken != null) {
387 pw.print(prefix); pw.println("app=true");
388 }
389 if (allAppWindows.size() > 0) {
390 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
391 }
392 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
393 pw.print(" appFullscreen="); pw.print(appFullscreen);
394 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
395 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
396 pw.print(" clientHidden="); pw.print(clientHidden);
397 pw.print(" willBeHidden="); pw.print(willBeHidden);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700398 pw.print(" reportedDrawn="); pw.print(reportedDrawn);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800399 pw.print(" reportedVisible="); pw.println(reportedVisible);
400 if (paused || freezingScreen) {
401 pw.print(prefix); pw.print("paused="); pw.print(paused);
402 pw.print(" freezingScreen="); pw.println(freezingScreen);
403 }
404 if (numInterestingWindows != 0 || numDrawnWindows != 0
405 || inPendingTransaction || allDrawn) {
406 pw.print(prefix); pw.print("numInterestingWindows=");
407 pw.print(numInterestingWindows);
408 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
409 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
410 pw.print(" allDrawn="); pw.println(allDrawn);
411 }
412 if (animating || animation != null) {
413 pw.print(prefix); pw.print("animating="); pw.print(animating);
414 pw.print(" animation="); pw.println(animation);
415 }
416 if (hasTransformation) {
417 pw.print(prefix); pw.print("XForm: ");
418 transformation.printShortString(pw);
419 pw.println();
420 }
421 if (animLayerAdjustment != 0) {
422 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
423 }
424 if (startingData != null || removed || firstWindowDrawn) {
425 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
426 pw.print(" removed="); pw.print(removed);
427 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
428 }
429 if (startingWindow != null || startingView != null
430 || startingDisplayed || startingMoved) {
431 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
432 pw.print(" startingView="); pw.print(startingView);
433 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
434 pw.print(" startingMoved"); pw.println(startingMoved);
435 }
436 }
437
438 @Override
439 public String toString() {
440 if (stringName == null) {
441 StringBuilder sb = new StringBuilder();
442 sb.append("AppWindowToken{");
443 sb.append(Integer.toHexString(System.identityHashCode(this)));
444 sb.append(" token="); sb.append(token); sb.append('}');
445 stringName = sb.toString();
446 }
447 return stringName;
448 }
449}