blob: 5ca09e78bb8aa0f67190f473b9ef10641d0c6996 [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 Mautnerdbb79912012-03-01 18:59:14 -0800243 // we're done!
Craig Mautnere32c3072012-03-12 15:25:35 -0700244 return stepAnimation(currentTime);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800245 }
246 } else if (animation != null) {
247 // If the display is frozen, and there is a pending animation,
248 // clear it and make sure we run the cleanup code.
249 animating = true;
250 animation = null;
251 }
252
253 hasTransformation = false;
254
255 if (!animating) {
256 return false;
257 }
258
Craig Mautnere32c3072012-03-12 15:25:35 -0700259 service.mPendingLayoutChanges |= WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800260 clearAnimation();
261 animating = false;
262 if (animLayerAdjustment != 0) {
263 animLayerAdjustment = 0;
264 updateLayers();
265 }
266 if (service.mInputMethodTarget != null && service.mInputMethodTarget.mAppToken == this) {
267 service.moveInputMethodWindowsIfNeededLocked(true);
268 }
269
270 if (WindowManagerService.DEBUG_ANIM) Slog.v(
271 WindowManagerService.TAG, "Animation done in " + this
272 + ": reportedVisible=" + reportedVisible);
273
274 transformation.clear();
275
276 final int N = windows.size();
277 for (int i=0; i<N; i++) {
278 windows.get(i).finishExit();
279 }
280 updateReportedVisibilityLocked();
281
282 return false;
283 }
284
285 void updateReportedVisibilityLocked() {
286 if (appToken == null) {
287 return;
288 }
289
290 int numInteresting = 0;
291 int numVisible = 0;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700292 int numDrawn = 0;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800293 boolean nowGone = true;
294
295 if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(WindowManagerService.TAG, "Update reported visibility: " + this);
296 final int N = allAppWindows.size();
297 for (int i=0; i<N; i++) {
298 WindowState win = allAppWindows.get(i);
299 if (win == startingWindow || win.mAppFreezing
300 || win.mViewVisibility != View.VISIBLE
301 || win.mAttrs.type == TYPE_APPLICATION_STARTING
302 || win.mDestroying) {
303 continue;
304 }
305 if (WindowManagerService.DEBUG_VISIBILITY) {
306 Slog.v(WindowManagerService.TAG, "Win " + win + ": isDrawn="
307 + win.isDrawnLw()
308 + ", isAnimating=" + win.isAnimating());
309 if (!win.isDrawnLw()) {
310 Slog.v(WindowManagerService.TAG, "Not displayed: s=" + win.mSurface
311 + " pv=" + win.mPolicyVisibility
312 + " dp=" + win.mDrawPending
313 + " cdp=" + win.mCommitDrawPending
314 + " ah=" + win.mAttachedHidden
315 + " th="
316 + (win.mAppToken != null
317 ? win.mAppToken.hiddenRequested : false)
318 + " a=" + win.mAnimating);
319 }
320 }
321 numInteresting++;
322 if (win.isDrawnLw()) {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700323 numDrawn++;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800324 if (!win.isAnimating()) {
325 numVisible++;
326 }
327 nowGone = false;
328 } else if (win.isAnimating()) {
329 nowGone = false;
330 }
331 }
332
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700333 boolean nowDrawn = numInteresting > 0 && numDrawn >= numInteresting;
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800334 boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700335 if (!nowGone) {
336 // If the app is not yet gone, then it can only become visible/drawn.
337 if (!nowDrawn) {
338 nowDrawn = reportedDrawn;
339 }
340 if (!nowVisible) {
341 nowVisible = reportedVisible;
342 }
343 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800344 if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(WindowManagerService.TAG, "VIS " + this + ": interesting="
345 + numInteresting + " visible=" + numVisible);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700346 if (nowDrawn != reportedDrawn) {
347 if (nowDrawn) {
348 Message m = service.mH.obtainMessage(
349 H.REPORT_APPLICATION_TOKEN_DRAWN, this);
350 service.mH.sendMessage(m);
351 }
352 reportedDrawn = nowDrawn;
353 }
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800354 if (nowVisible != reportedVisible) {
355 if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(
356 WindowManagerService.TAG, "Visibility changed in " + this
357 + ": vis=" + nowVisible);
358 reportedVisible = nowVisible;
359 Message m = service.mH.obtainMessage(
360 H.REPORT_APPLICATION_TOKEN_WINDOWS,
361 nowVisible ? 1 : 0,
362 nowGone ? 1 : 0,
363 this);
364 service.mH.sendMessage(m);
365 }
366 }
367
368 WindowState findMainWindow() {
369 int j = windows.size();
370 while (j > 0) {
371 j--;
372 WindowState win = windows.get(j);
373 if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
374 || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
375 return win;
376 }
377 }
378 return null;
379 }
380
Craig Mautnerdbb79912012-03-01 18:59:14 -0800381 @Override
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800382 void dump(PrintWriter pw, String prefix) {
383 super.dump(pw, prefix);
384 if (appToken != null) {
385 pw.print(prefix); pw.println("app=true");
386 }
387 if (allAppWindows.size() > 0) {
388 pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
389 }
390 pw.print(prefix); pw.print("groupId="); pw.print(groupId);
391 pw.print(" appFullscreen="); pw.print(appFullscreen);
392 pw.print(" requestedOrientation="); pw.println(requestedOrientation);
393 pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
394 pw.print(" clientHidden="); pw.print(clientHidden);
395 pw.print(" willBeHidden="); pw.print(willBeHidden);
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -0700396 pw.print(" reportedDrawn="); pw.print(reportedDrawn);
Dianne Hackborn6e1eb762011-02-17 16:07:28 -0800397 pw.print(" reportedVisible="); pw.println(reportedVisible);
398 if (paused || freezingScreen) {
399 pw.print(prefix); pw.print("paused="); pw.print(paused);
400 pw.print(" freezingScreen="); pw.println(freezingScreen);
401 }
402 if (numInterestingWindows != 0 || numDrawnWindows != 0
403 || inPendingTransaction || allDrawn) {
404 pw.print(prefix); pw.print("numInterestingWindows=");
405 pw.print(numInterestingWindows);
406 pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
407 pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
408 pw.print(" allDrawn="); pw.println(allDrawn);
409 }
410 if (animating || animation != null) {
411 pw.print(prefix); pw.print("animating="); pw.print(animating);
412 pw.print(" animation="); pw.println(animation);
413 }
414 if (hasTransformation) {
415 pw.print(prefix); pw.print("XForm: ");
416 transformation.printShortString(pw);
417 pw.println();
418 }
419 if (animLayerAdjustment != 0) {
420 pw.print(prefix); pw.print("animLayerAdjustment="); pw.println(animLayerAdjustment);
421 }
422 if (startingData != null || removed || firstWindowDrawn) {
423 pw.print(prefix); pw.print("startingData="); pw.print(startingData);
424 pw.print(" removed="); pw.print(removed);
425 pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
426 }
427 if (startingWindow != null || startingView != null
428 || startingDisplayed || startingMoved) {
429 pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
430 pw.print(" startingView="); pw.print(startingView);
431 pw.print(" startingDisplayed="); pw.print(startingDisplayed);
432 pw.print(" startingMoved"); pw.println(startingMoved);
433 }
434 }
435
436 @Override
437 public String toString() {
438 if (stringName == null) {
439 StringBuilder sb = new StringBuilder();
440 sb.append("AppWindowToken{");
441 sb.append(Integer.toHexString(System.identityHashCode(this)));
442 sb.append(" token="); sb.append(token); sb.append('}');
443 stringName = sb.toString();
444 }
445 return stringName;
446 }
447}