AppWindowToken.java revision bb742462781a73bb25516067c8fe6311c1c8a93e
1/*
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.input.InputApplicationHandle;
22import com.android.server.wm.WindowManagerService.H;
23
24import android.content.pm.ActivityInfo;
25import android.os.Message;
26import android.os.RemoteException;
27import android.util.Slog;
28import android.view.IApplicationToken;
29import android.view.View;
30import android.view.WindowManager;
31
32import java.io.PrintWriter;
33import java.util.ArrayList;
34
35class AppTokenList extends ArrayList<AppWindowToken> {
36}
37
38/**
39 * Version of WindowToken that is specifically for a particular application (or
40 * really activity) that is displaying windows.
41 */
42class AppWindowToken extends WindowToken {
43    // Non-null only for application tokens.
44    final IApplicationToken appToken;
45
46    // All of the windows and child windows that are included in this
47    // application token.  Note this list is NOT sorted!
48    final WindowList allAppWindows = new WindowList();
49    final AppWindowAnimator mAppAnimator;
50
51    final WindowAnimator mAnimator;
52
53    final boolean voiceInteraction;
54
55    int groupId = -1;
56    boolean appFullscreen;
57    int requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
58    boolean layoutConfigChanges;
59    boolean showWhenLocked;
60
61    // The input dispatching timeout for this application token in nanoseconds.
62    long inputDispatchingTimeoutNanos;
63
64    // These are used for determining when all windows associated with
65    // an activity have been drawn, so they can be made visible together
66    // at the same time.
67    // initialize so that it doesn't match mTransactionSequence which is an int.
68    long lastTransactionSequence = Long.MIN_VALUE;
69    int numInterestingWindows;
70    int numDrawnWindows;
71    boolean inPendingTransaction;
72    boolean allDrawn;
73    // Set to true when this app creates a surface while in the middle of an animation. In that
74    // case do not clear allDrawn until the animation completes.
75    boolean deferClearAllDrawn;
76
77    // Is this token going to be hidden in a little while?  If so, it
78    // won't be taken into account for setting the screen orientation.
79    boolean willBeHidden;
80
81    // Is this window's surface needed?  This is almost like hidden, except
82    // it will sometimes be true a little earlier: when the token has
83    // been shown, but is still waiting for its app transition to execute
84    // before making its windows shown.
85    boolean hiddenRequested;
86
87    // Have we told the window clients to hide themselves?
88    boolean clientHidden;
89
90    // Last visibility state we reported to the app token.
91    boolean reportedVisible;
92
93    // Last drawn state we reported to the app token.
94    boolean reportedDrawn;
95
96    // Set to true when the token has been removed from the window mgr.
97    boolean removed;
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.
108    final InputApplicationHandle mInputApplicationHandle;
109
110    boolean mDeferRemoval;
111
112    boolean mLaunchTaskBehind;
113
114    AppWindowToken(WindowManagerService _service, IApplicationToken _token,
115            boolean _voiceInteraction) {
116        super(_service, _token.asBinder(),
117                WindowManager.LayoutParams.TYPE_APPLICATION, true);
118        appWindowToken = this;
119        appToken = _token;
120        voiceInteraction = _voiceInteraction;
121        mInputApplicationHandle = new InputApplicationHandle(this);
122        mAnimator = service.mAnimator;
123        mAppAnimator = new AppWindowAnimator(this);
124    }
125
126    void sendAppVisibilityToClients() {
127        final int N = allAppWindows.size();
128        for (int i=0; i<N; i++) {
129            WindowState win = allAppWindows.get(i);
130            if (win == startingWindow && clientHidden) {
131                // Don't hide the starting window.
132                continue;
133            }
134            try {
135                if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(WindowManagerService.TAG,
136                        "Setting visibility of " + win + ": " + (!clientHidden));
137                win.mClient.dispatchAppVisibility(!clientHidden);
138            } catch (RemoteException e) {
139            }
140        }
141    }
142
143    void updateReportedVisibilityLocked() {
144        if (appToken == null) {
145            return;
146        }
147
148        int numInteresting = 0;
149        int numVisible = 0;
150        int numDrawn = 0;
151        boolean nowGone = true;
152
153        if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(WindowManagerService.TAG,
154                "Update reported visibility: " + this);
155        final int N = allAppWindows.size();
156        for (int i=0; i<N; i++) {
157            WindowState win = allAppWindows.get(i);
158            if (win == startingWindow || win.mAppFreezing
159                    || win.mViewVisibility != View.VISIBLE
160                    || win.mAttrs.type == TYPE_APPLICATION_STARTING
161                    || win.mDestroying) {
162                continue;
163            }
164            if (WindowManagerService.DEBUG_VISIBILITY) {
165                Slog.v(WindowManagerService.TAG, "Win " + win + ": isDrawn="
166                        + win.isDrawnLw()
167                        + ", isAnimating=" + win.mWinAnimator.isAnimating());
168                if (!win.isDrawnLw()) {
169                    Slog.v(WindowManagerService.TAG, "Not displayed: s=" + win.mWinAnimator.mSurfaceControl
170                            + " pv=" + win.mPolicyVisibility
171                            + " mDrawState=" + win.mWinAnimator.mDrawState
172                            + " ah=" + win.mAttachedHidden
173                            + " th="
174                            + (win.mAppToken != null
175                                    ? win.mAppToken.hiddenRequested : false)
176                            + " a=" + win.mWinAnimator.mAnimating);
177                }
178            }
179            numInteresting++;
180            if (win.isDrawnLw()) {
181                numDrawn++;
182                if (!win.mWinAnimator.isAnimating()) {
183                    numVisible++;
184                }
185                nowGone = false;
186            } else if (win.mWinAnimator.isAnimating()) {
187                nowGone = false;
188            }
189        }
190
191        boolean nowDrawn = numInteresting > 0 && numDrawn >= numInteresting;
192        boolean nowVisible = numInteresting > 0 && numVisible >= numInteresting;
193        if (!nowGone) {
194            // If the app is not yet gone, then it can only become visible/drawn.
195            if (!nowDrawn) {
196                nowDrawn = reportedDrawn;
197            }
198            if (!nowVisible) {
199                nowVisible = reportedVisible;
200            }
201        }
202        if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(WindowManagerService.TAG, "VIS " + this + ": interesting="
203                + numInteresting + " visible=" + numVisible);
204        if (nowDrawn != reportedDrawn) {
205            if (nowDrawn) {
206                Message m = service.mH.obtainMessage(
207                        H.REPORT_APPLICATION_TOKEN_DRAWN, this);
208                service.mH.sendMessage(m);
209            }
210            reportedDrawn = nowDrawn;
211        }
212        if (nowVisible != reportedVisible) {
213            if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(
214                    WindowManagerService.TAG, "Visibility changed in " + this
215                    + ": vis=" + nowVisible);
216            reportedVisible = nowVisible;
217            Message m = service.mH.obtainMessage(
218                    H.REPORT_APPLICATION_TOKEN_WINDOWS,
219                    nowVisible ? 1 : 0,
220                    nowGone ? 1 : 0,
221                    this);
222            service.mH.sendMessage(m);
223        }
224    }
225
226    WindowState findMainWindow() {
227        int j = windows.size();
228        while (j > 0) {
229            j--;
230            WindowState win = windows.get(j);
231            if (win.mAttrs.type == WindowManager.LayoutParams.TYPE_BASE_APPLICATION
232                    || win.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
233                return win;
234            }
235        }
236        return null;
237    }
238
239    boolean isVisible() {
240        final int N = allAppWindows.size();
241        for (int i=0; i<N; i++) {
242            WindowState win = allAppWindows.get(i);
243            if (!win.mAppFreezing
244                    && (win.mViewVisibility == View.VISIBLE ||
245                        (win.mWinAnimator.isAnimating() &&
246                                !service.mAppTransition.isTransitionSet()))
247                    && !win.mDestroying && win.isDrawnLw()) {
248                return true;
249            }
250        }
251        return false;
252    }
253
254    @Override
255    void dump(PrintWriter pw, String prefix) {
256        super.dump(pw, prefix);
257        if (appToken != null) {
258            pw.print(prefix); pw.print("app=true voiceInteraction="); pw.println(voiceInteraction);
259        }
260        if (allAppWindows.size() > 0) {
261            pw.print(prefix); pw.print("allAppWindows="); pw.println(allAppWindows);
262        }
263        pw.print(prefix); pw.print("groupId="); pw.print(groupId);
264                pw.print(" appFullscreen="); pw.print(appFullscreen);
265                pw.print(" requestedOrientation="); pw.println(requestedOrientation);
266        pw.print(prefix); pw.print("hiddenRequested="); pw.print(hiddenRequested);
267                pw.print(" clientHidden="); pw.print(clientHidden);
268                pw.print(" willBeHidden="); pw.print(willBeHidden);
269                pw.print(" reportedDrawn="); pw.print(reportedDrawn);
270                pw.print(" reportedVisible="); pw.println(reportedVisible);
271        if (paused) {
272            pw.print(prefix); pw.print("paused="); pw.println(paused);
273        }
274        if (numInterestingWindows != 0 || numDrawnWindows != 0
275                || allDrawn || mAppAnimator.allDrawn) {
276            pw.print(prefix); pw.print("numInterestingWindows=");
277                    pw.print(numInterestingWindows);
278                    pw.print(" numDrawnWindows="); pw.print(numDrawnWindows);
279                    pw.print(" inPendingTransaction="); pw.print(inPendingTransaction);
280                    pw.print(" allDrawn="); pw.print(allDrawn);
281                    pw.print(" (animator="); pw.print(mAppAnimator.allDrawn);
282                    pw.println(")");
283        }
284        if (inPendingTransaction) {
285            pw.print(prefix); pw.print("inPendingTransaction=");
286                    pw.println(inPendingTransaction);
287        }
288        if (startingData != null || removed || firstWindowDrawn) {
289            pw.print(prefix); pw.print("startingData="); pw.print(startingData);
290                    pw.print(" removed="); pw.print(removed);
291                    pw.print(" firstWindowDrawn="); pw.println(firstWindowDrawn);
292        }
293        if (startingWindow != null || startingView != null
294                || startingDisplayed || startingMoved) {
295            pw.print(prefix); pw.print("startingWindow="); pw.print(startingWindow);
296                    pw.print(" startingView="); pw.print(startingView);
297                    pw.print(" startingDisplayed="); pw.print(startingDisplayed);
298                    pw.print(" startingMoved"); pw.println(startingMoved);
299        }
300    }
301
302    @Override
303    public String toString() {
304        if (stringName == null) {
305            StringBuilder sb = new StringBuilder();
306            sb.append("AppWindowToken{");
307            sb.append(Integer.toHexString(System.identityHashCode(this)));
308            sb.append(" token="); sb.append(token); sb.append('}');
309            stringName = sb.toString();
310        }
311        return stringName;
312    }
313}
314