blob: 3ae9f240564977f4fd84ce255e85140f753e93bb [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 }
Craig Mautner22ce1412012-03-20 10:16:26 -0700138 // Start out animation gone if window is gone, or visible if window is visible.
139 transformation.clear();
140 transformation.setAlpha(reportedVisible ? 1 : 0);
141 hasTransformation = true;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800142 }
143
144 public void setDummyAnimation() {
145 if (animation == null) {
146 if (WindowManagerService.localLOGV) Slog.v(
147 WindowManagerService.TAG, "Setting dummy animation in " + this);
148 animation = WindowManagerService.sDummyAnimation;
149 }
150 }
151
152 public void clearAnimation() {
153 if (animation != null) {
154 animation = null;
155 animating = true;
156 }
157 }
158
159 void updateLayers() {
160 final int N = allAppWindows.size();
161 final int adj = animLayerAdjustment;
162 for (int i=0; i<N; i++) {
163 WindowState w = allAppWindows.get(i);
164 w.mAnimLayer = w.mLayer + adj;
165 if (WindowManagerService.DEBUG_LAYERS) Slog.v(WindowManagerService.TAG, "Updating layer " + w + ": "
166 + w.mAnimLayer);
167 if (w == service.mInputMethodTarget && !service.mInputMethodTargetWaitingAnim) {
168 service.setInputMethodAnimLayerAdjustment(adj);
169 }
170 if (w == service.mWallpaperTarget && service.mLowerWallpaperTarget == null) {
171 service.setWallpaperAnimLayerAdjustmentLocked(adj);
172 }
173 }
174 }
175
176 void sendAppVisibilityToClients() {
177 final int N = allAppWindows.size();
178 for (int i=0; i<N; i++) {
179 WindowState win = allAppWindows.get(i);
180 if (win == startingWindow && clientHidden) {
181 // Don't hide the starting window.
182 continue;
183 }
184 try {
185 if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(WindowManagerService.TAG,
186 "Setting visibility of " + win + ": " + (!clientHidden));
187 win.mClient.dispatchAppVisibility(!clientHidden);
188 } catch (RemoteException e) {
189 }
190 }
191 }
192
193 void showAllWindowsLocked() {
194 final int NW = allAppWindows.size();
195 for (int i=0; i<NW; i++) {
196 WindowState w = allAppWindows.get(i);
197 if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(WindowManagerService.TAG,
198 "performing show on: " + w);
199 w.performShowLocked();
200 }
201 }
202
Craig Mautnere32c3072012-03-12 15:25:35 -0700203
204 private boolean stepAnimation(long currentTime) {
Craig Mautnerdbb79912012-03-01 18:59:14 -0800205 if (animation == null) {
206 return false;
207 }
208 transformation.clear();
209 final boolean more = animation.getTransformation(currentTime, transformation);
210 if (WindowManagerService.DEBUG_ANIM) Slog.v(
211 WindowManagerService.TAG, "Stepped animation in " + this +
212 ": more=" + more + ", xform=" + transformation);
213 if (!more) {
214 animation = null;
215 if (WindowManagerService.DEBUG_ANIM) Slog.v(
216 WindowManagerService.TAG, "Finished animation in " + this +
217 " @ " + currentTime);
218 }
219 hasTransformation = more;
220 return more;
221 }
222
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800223 // This must be called while inside a transaction.
Craig Mautnere32c3072012-03-12 15:25:35 -0700224 boolean stepAnimationLocked(long currentTime, int dw, int dh) {
Dianne Hackbornbc1aa7b2011-09-20 11:20:31 -0700225 if (!service.mDisplayFrozen && service.mPolicy.isScreenOnFully()) {
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800226 // We will run animations as long as the display isn't frozen.
227
228 if (animation == WindowManagerService.sDummyAnimation) {
229 // This guy is going to animate, but not yet. For now count
230 // it as not animating for purposes of scheduling transactions;
231 // when it is really time to animate, this will be set to
232 // a real animation and the next call will execute normally.
233 return false;
234 }
235
236 if ((allDrawn || animating || startingDisplayed) && animation != null) {
237 if (!animating) {
238 if (WindowManagerService.DEBUG_ANIM) Slog.v(
239 WindowManagerService.TAG, "Starting animation in " + this +
240 " @ " + currentTime + ": dw=" + dw + " dh=" + dh
241 + " scale=" + service.mTransitionAnimationScale
242 + " allDrawn=" + allDrawn + " animating=" + animating);
243 animation.initialize(dw, dh, dw, dh);
244 animation.setStartTime(currentTime);
245 animating = true;
246 }
Craig Mautner1dd3ed02012-03-16 14:01:16 -0700247 if (stepAnimation(currentTime)) {
248 // we're done!
249 return true;
250 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800251 }
252 } else if (animation != null) {
253 // If the display is frozen, and there is a pending animation,
254 // clear it and make sure we run the cleanup code.
255 animating = true;
256 animation = null;
257 }
258
259 hasTransformation = false;
260
261 if (!animating) {
262 return false;
263 }
264
Craig Mautnere32c3072012-03-12 15:25:35 -0700265 service.mPendingLayoutChanges |= WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800266 clearAnimation();
267 animating = false;
268 if (animLayerAdjustment != 0) {
269 animLayerAdjustment = 0;
270 updateLayers();
271 }
272 if (service.mInputMethodTarget != null && service.mInputMethodTarget.mAppToken == this) {
273 service.moveInputMethodWindowsIfNeededLocked(true);
274 }
275
276 if (WindowManagerService.DEBUG_ANIM) Slog.v(
277 WindowManagerService.TAG, "Animation done in " + this
278 + ": reportedVisible=" + reportedVisible);
279
280 transformation.clear();
281
282 final int N = windows.size();
283 for (int i=0; i<N; i++) {
284 windows.get(i).finishExit();
285 }
286 updateReportedVisibilityLocked();
287
288 return false;
289 }
290
291 void updateReportedVisibilityLocked() {
292 if (appToken == null) {
293 return;
294 }
295
296 int numInteresting = 0;
297 int numVisible = 0;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700298 int numDrawn = 0;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800299 boolean nowGone = true;
300
301 if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(WindowManagerService.TAG, "Update reported visibility: " + this);
302 final int N = allAppWindows.size();
303 for (int i=0; i<N; i++) {
304 WindowState win = allAppWindows.get(i);
305 if (win == startingWindow || win.mAppFreezing
306 || win.mViewVisibility != View.VISIBLE
307 || win.mAttrs.type == TYPE_APPLICATION_STARTING
308 || win.mDestroying) {
309 continue;
310 }
311 if (WindowManagerService.DEBUG_VISIBILITY) {
312 Slog.v(WindowManagerService.TAG, "Win " + win + ": isDrawn="
313 + win.isDrawnLw()
314 + ", isAnimating=" + win.isAnimating());
315 if (!win.isDrawnLw()) {
316 Slog.v(WindowManagerService.TAG, "Not displayed: s=" + win.mSurface
317 + " pv=" + win.mPolicyVisibility
318 + " dp=" + win.mDrawPending
319 + " cdp=" + win.mCommitDrawPending
320 + " ah=" + win.mAttachedHidden
321 + " th="
322 + (win.mAppToken != null
323 ? win.mAppToken.hiddenRequested : false)
324 + " a=" + win.mAnimating);
325 }
326 }
327 numInteresting++;
328 if (win.isDrawnLw()) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700329 numDrawn++;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800330 if (!win.isAnimating()) {
331 numVisible++;
332 }
333 nowGone = false;
334 } else if (win.isAnimating()) {
335 nowGone = false;
336 }
337 }
338
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700339 boolean nowDrawn = numInteresting > 0 && numDrawn >= numInteresting;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800340 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700341 if (!nowGone) {
342 // If the app is not yet gone, then it can only become visible/drawn.
343 if (!nowDrawn) {
344 nowDrawn = reportedDrawn;
345 }
346 if (!nowVisible) {
347 nowVisible = reportedVisible;
348 }
349 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800350 if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(WindowManagerService.TAG, "VIS " + this + ": interesting="
351 + numInteresting + " visible=" + numVisible);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700352 if (nowDrawn != reportedDrawn) {
353 if (nowDrawn) {
354 Message m = service.mH.obtainMessage(
355 H.REPORT_APPLICATION_TOKEN_DRAWN, this);
356 service.mH.sendMessage(m);
357 }
358 reportedDrawn = nowDrawn;
359 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800360 if (nowVisible != reportedVisible) {
361 if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(
362 WindowManagerService.TAG, "Visibility changed in " + this
363 + ": vis=" + nowVisible);
364 reportedVisible = nowVisible;
365 Message m = service.mH.obtainMessage(
366 H.REPORT_APPLICATION_TOKEN_WINDOWS,
367 nowVisible ? 1 : 0,
368 nowGone ? 1 : 0,
369 this);
370 service.mH.sendMessage(m);
371 }
372 }
373
374 WindowState findMainWindow() {
375 int j = windows.size();
376 while (j > 0) {
377 j--;
378 WindowState win = windows.get(j);
379 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
380 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
381 return win;
382 }
383 }
384 return null;
385 }
386
Craig Mautnerdbb79912012-03-01 18:59:14 -0800387 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800388 void dump(PrintWriter pw, String prefix) {
389 super.dump(pw, prefix);
390 if (appToken != null) {
391 pw.print(prefix); pw.println("app=true");
392 }
393 if (allAppWindows.size() > 0) {
394 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
395 }
396 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
397 pw.print(" appFullscreen="); pw.print(appFullscreen);
398 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
399 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
400 pw.print(" clientHidden="); pw.print(clientHidden);
401 pw.print(" willBeHidden="); pw.print(willBeHidden);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700402 pw.print(" reportedDrawn="); pw.print(reportedDrawn);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800403 pw.print(" reportedVisible="); pw.println(reportedVisible);
404 if (paused || freezingScreen) {
405 pw.print(prefix); pw.print("paused="); pw.print(paused);
406 pw.print(" freezingScreen="); pw.println(freezingScreen);
407 }
408 if (numInterestingWindows != 0 || numDrawnWindows != 0
409 || inPendingTransaction || allDrawn) {
410 pw.print(prefix); pw.print("numInterestingWindows=");
411 pw.print(numInterestingWindows);
412 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
413 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
414 pw.print(" allDrawn="); pw.println(allDrawn);
415 }
416 if (animating || animation != null) {
417 pw.print(prefix); pw.print("animating="); pw.print(animating);
418 pw.print(" animation="); pw.println(animation);
419 }
420 if (hasTransformation) {
421 pw.print(prefix); pw.print("XForm: ");
422 transformation.printShortString(pw);
423 pw.println();
424 }
425 if (animLayerAdjustment != 0) {
426 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
427 }
428 if (startingData != null || removed || firstWindowDrawn) {
429 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
430 pw.print(" removed="); pw.print(removed);
431 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
432 }
433 if (startingWindow != null || startingView != null
434 || startingDisplayed || startingMoved) {
435 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
436 pw.print(" startingView="); pw.print(startingView);
437 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
438 pw.print(" startingMoved"); pw.println(startingMoved);
439 }
440 }
441
442 @Override
443 public String toString() {
444 if (stringName == null) {
445 StringBuilder sb = new StringBuilder();
446 sb.append("AppWindowToken{");
447 sb.append(Integer.toHexString(System.identityHashCode(this)));
448 sb.append(" token="); sb.append(token); sb.append('}');
449 stringName = sb.toString();
450 }
451 return stringName;
452 }
453}