TaskStack.java revision dc548483ae90ba26ad9e2e2cb79f4673140edb49
1/*
2 * Copyright (C) 2013 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 com.android.server.wm.WindowManagerService.DEBUG_TASK_MOVEMENT;
20import static com.android.server.wm.WindowManagerService.TAG;
21
22import android.graphics.Rect;
23import android.os.Debug;
24import android.util.EventLog;
25import android.util.Slog;
26import android.util.TypedValue;
27import com.android.server.EventLogTags;
28
29import java.io.PrintWriter;
30import java.util.ArrayList;
31
32public class TaskStack {
33    /** Amount of time in milliseconds to animate the dim surface from one value to another,
34     * when no window animation is driving it. */
35    private static final int DEFAULT_DIM_DURATION = 200;
36
37    /** Unique identifier */
38    final int mStackId;
39
40    /** The service */
41    private final WindowManagerService mService;
42
43    /** The display this stack sits under. */
44    private DisplayContent mDisplayContent;
45
46    /** The Tasks that define this stack. Oldest Tasks are at the bottom. The ordering must match
47     * mTaskHistory in the ActivityStack with the same mStackId */
48    private final ArrayList<Task> mTasks = new ArrayList<Task>();
49
50    /** Content limits relative to the DisplayContent this sits in. Empty indicates fullscreen,
51     * Nonempty is size of this TaskStack but is also used to scale if DisplayContent changes. */
52    Rect mBounds = new Rect();
53
54    /** Used to support {@link android.view.WindowManager.LayoutParams#FLAG_DIM_BEHIND} */
55    DimLayer mDimLayer;
56
57    /** The particular window with FLAG_DIM_BEHIND set. If null, hide mDimLayer. */
58    WindowStateAnimator mDimWinAnimator;
59
60    /** Support for non-zero {@link android.view.animation.Animation#getBackgroundColor()} */
61    DimLayer mAnimationBackgroundSurface;
62
63    /** The particular window with an Animation with non-zero background color. */
64    WindowStateAnimator mAnimationBackgroundAnimator;
65
66    /** Set to false at the start of performLayoutAndPlaceSurfaces. If it is still false by the end
67     * then stop any dimming. */
68    boolean mDimmingTag;
69
70    /** Application tokens that are exiting, but still on screen for animations. */
71    final AppTokenList mExitingAppTokens = new AppTokenList();
72
73    TaskStack(WindowManagerService service, int stackId) {
74        mService = service;
75        mStackId = stackId;
76        // TODO: remove bounds from log, they are always 0.
77        EventLog.writeEvent(EventLogTags.WM_STACK_CREATED, stackId, mBounds.left, mBounds.top,
78                mBounds.right, mBounds.bottom);
79    }
80
81    DisplayContent getDisplayContent() {
82        return mDisplayContent;
83    }
84
85    ArrayList<Task> getTasks() {
86        return mTasks;
87    }
88
89    private void resizeWindows() {
90        final boolean underStatusBar = mBounds.top == 0;
91
92        final ArrayList<WindowState> resizingWindows = mService.mResizingWindows;
93        for (int taskNdx = mTasks.size() - 1; taskNdx >= 0; --taskNdx) {
94            final ArrayList<AppWindowToken> activities = mTasks.get(taskNdx).mAppTokens;
95            for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
96                final ArrayList<WindowState> windows = activities.get(activityNdx).allAppWindows;
97                for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
98                    final WindowState win = windows.get(winNdx);
99                    if (!resizingWindows.contains(win)) {
100                        if (WindowManagerService.DEBUG_RESIZE) Slog.d(TAG,
101                                "setBounds: Resizing " + win);
102                        resizingWindows.add(win);
103                    }
104                    win.mUnderStatusBar = underStatusBar;
105                }
106            }
107        }
108    }
109
110    boolean setBounds(Rect bounds) {
111        if (mBounds.equals(bounds)) {
112            return false;
113        }
114
115        mDimLayer.setBounds(bounds);
116        mAnimationBackgroundSurface.setBounds(bounds);
117        mBounds.set(bounds);
118
119        resizeWindows();
120        return true;
121    }
122
123    void getBounds(Rect out) {
124        if (mDisplayContent != null) {
125            if (mBounds.isEmpty()) {
126                mDisplayContent.getLogicalDisplayRect(out);
127            } else {
128                out.set(mBounds);
129            }
130            out.intersect(mDisplayContent.mContentRect);
131        } else {
132            out.set(mBounds);
133        }
134    }
135
136    boolean isFullscreen() {
137        return mBounds.isEmpty();
138    }
139
140    void resizeBounds(float oldWidth, float oldHeight, float newWidth, float newHeight) {
141        if (oldWidth == newWidth && oldHeight == newHeight) {
142            return;
143        }
144        float widthScale = newWidth / oldWidth;
145        float heightScale = newHeight / oldHeight;
146        mBounds.left = (int)(mBounds.left * widthScale + 0.5);
147        mBounds.top = (int)(mBounds.top * heightScale + 0.5);
148        mBounds.right = (int)(mBounds.right * widthScale + 0.5);
149        mBounds.bottom = (int)(mBounds.bottom * heightScale + 0.5);
150        resizeWindows();
151    }
152
153    /**
154     * Put a Task in this stack. Used for adding and moving.
155     * @param task The task to add.
156     * @param toTop Whether to add it to the top or bottom.
157     */
158    void addTask(Task task, boolean toTop) {
159        int stackNdx;
160        if (!toTop) {
161            stackNdx = 0;
162        } else {
163            stackNdx = mTasks.size();
164            final int currentUserId = mService.mCurrentUserId;
165            if (task.mUserId != currentUserId) {
166                // Place the task below all current user tasks.
167                while (--stackNdx >= 0) {
168                    if (currentUserId != mTasks.get(stackNdx).mUserId) {
169                        break;
170                    }
171                }
172                ++stackNdx;
173            }
174        }
175        if (DEBUG_TASK_MOVEMENT) Slog.d(TAG, "addTask: task=" + task + " toTop=" + toTop
176                + " pos=" + stackNdx);
177        mTasks.add(stackNdx, task);
178
179        task.mStack = this;
180        mDisplayContent.moveStack(this, true);
181        EventLog.writeEvent(EventLogTags.WM_TASK_MOVED, task.taskId, toTop ? 1 : 0, stackNdx);
182    }
183
184    void moveTaskToTop(Task task) {
185        if (DEBUG_TASK_MOVEMENT) Slog.d(TAG, "moveTaskToTop: task=" + task + " Callers="
186                + Debug.getCallers(6));
187        mTasks.remove(task);
188        addTask(task, true);
189    }
190
191    void moveTaskToBottom(Task task) {
192        if (DEBUG_TASK_MOVEMENT) Slog.d(TAG, "moveTaskToBottom: task=" + task);
193        mTasks.remove(task);
194        addTask(task, false);
195    }
196
197    /**
198     * Delete a Task from this stack. If it is the last Task in the stack, move this stack to the
199     * back.
200     * @param task The Task to delete.
201     */
202    void removeTask(Task task) {
203        if (DEBUG_TASK_MOVEMENT) Slog.d(TAG, "removeTask: task=" + task);
204        mTasks.remove(task);
205        if (mDisplayContent != null) {
206            if (mTasks.isEmpty()) {
207                mDisplayContent.moveStack(this, false);
208            }
209            mDisplayContent.layoutNeeded = true;
210        }
211    }
212
213    void attachDisplayContent(DisplayContent displayContent) {
214        if (mDisplayContent != null) {
215            throw new IllegalStateException("attachDisplayContent: Already attached");
216        }
217
218        mDisplayContent = displayContent;
219        mDimLayer = new DimLayer(mService, this, displayContent);
220        mAnimationBackgroundSurface = new DimLayer(mService, this, displayContent);
221    }
222
223    void detachDisplay() {
224        EventLog.writeEvent(EventLogTags.WM_STACK_REMOVED, mStackId);
225        for (int taskNdx = mTasks.size() - 1; taskNdx >= 0; --taskNdx) {
226            mService.tmpRemoveTaskWindowsLocked(mTasks.get(taskNdx));
227        }
228        mAnimationBackgroundSurface.destroySurface();
229        mAnimationBackgroundSurface = null;
230        mDimLayer.destroySurface();
231        mDimLayer = null;
232        mDisplayContent = null;
233    }
234
235    void resetAnimationBackgroundAnimator() {
236        mAnimationBackgroundAnimator = null;
237        mAnimationBackgroundSurface.hide();
238    }
239
240    private long getDimBehindFadeDuration(long duration) {
241        TypedValue tv = new TypedValue();
242        mService.mContext.getResources().getValue(
243                com.android.internal.R.fraction.config_dimBehindFadeDuration, tv, true);
244        if (tv.type == TypedValue.TYPE_FRACTION) {
245            duration = (long)tv.getFraction(duration, duration);
246        } else if (tv.type >= TypedValue.TYPE_FIRST_INT && tv.type <= TypedValue.TYPE_LAST_INT) {
247            duration = tv.data;
248        }
249        return duration;
250    }
251
252    boolean animateDimLayers() {
253        final int dimLayer;
254        final float dimAmount;
255        if (mDimWinAnimator == null) {
256            dimLayer = mDimLayer.getLayer();
257            dimAmount = 0;
258        } else {
259            dimLayer = mDimWinAnimator.mAnimLayer - WindowManagerService.LAYER_OFFSET_DIM;
260            dimAmount = mDimWinAnimator.mWin.mAttrs.dimAmount;
261        }
262        final float targetAlpha = mDimLayer.getTargetAlpha();
263        if (targetAlpha != dimAmount) {
264            if (mDimWinAnimator == null) {
265                mDimLayer.hide(DEFAULT_DIM_DURATION);
266            } else {
267                long duration = (mDimWinAnimator.mAnimating && mDimWinAnimator.mAnimation != null)
268                        ? mDimWinAnimator.mAnimation.computeDurationHint()
269                        : DEFAULT_DIM_DURATION;
270                if (targetAlpha > dimAmount) {
271                    duration = getDimBehindFadeDuration(duration);
272                }
273                mDimLayer.show(dimLayer, dimAmount, duration);
274            }
275        } else if (mDimLayer.getLayer() != dimLayer) {
276            mDimLayer.setLayer(dimLayer);
277        }
278        if (mDimLayer.isAnimating()) {
279            if (!mService.okToDisplay()) {
280                // Jump to the end of the animation.
281                mDimLayer.show();
282            } else {
283                return mDimLayer.stepAnimation();
284            }
285        }
286        return false;
287    }
288
289    void resetDimmingTag() {
290        mDimmingTag = false;
291    }
292
293    void setDimmingTag() {
294        mDimmingTag = true;
295    }
296
297    boolean testDimmingTag() {
298        return mDimmingTag;
299    }
300
301    boolean isDimming() {
302        return mDimLayer.isDimming();
303    }
304
305    boolean isDimming(WindowStateAnimator winAnimator) {
306        return mDimWinAnimator == winAnimator && mDimLayer.isDimming();
307    }
308
309    void startDimmingIfNeeded(WindowStateAnimator newWinAnimator) {
310        // Only set dim params on the highest dimmed layer.
311        final WindowStateAnimator existingDimWinAnimator = mDimWinAnimator;
312        // Don't turn on for an unshown surface, or for any layer but the highest dimmed layer.
313        if (newWinAnimator.mSurfaceShown && (existingDimWinAnimator == null
314                || !existingDimWinAnimator.mSurfaceShown
315                || existingDimWinAnimator.mAnimLayer < newWinAnimator.mAnimLayer)) {
316            mDimWinAnimator = newWinAnimator;
317        }
318    }
319
320    void stopDimmingIfNeeded() {
321        if (!mDimmingTag && isDimming()) {
322            mDimWinAnimator = null;
323        }
324    }
325
326    void setAnimationBackground(WindowStateAnimator winAnimator, int color) {
327        int animLayer = winAnimator.mAnimLayer;
328        if (mAnimationBackgroundAnimator == null
329                || animLayer < mAnimationBackgroundAnimator.mAnimLayer) {
330            mAnimationBackgroundAnimator = winAnimator;
331            animLayer = mService.adjustAnimationBackground(winAnimator);
332            mAnimationBackgroundSurface.show(animLayer - WindowManagerService.LAYER_OFFSET_DIM,
333                    ((color >> 24) & 0xff) / 255f, 0);
334        }
335    }
336
337    void switchUser(int userId) {
338        int top = mTasks.size();
339        for (int taskNdx = 0; taskNdx < top; ++taskNdx) {
340            Task task = mTasks.get(taskNdx);
341            if (task.mUserId == userId) {
342                mTasks.remove(taskNdx);
343                mTasks.add(task);
344                --top;
345            }
346        }
347    }
348
349    void close() {
350        mDimLayer.mDimSurface.destroy();
351        mAnimationBackgroundSurface.mDimSurface.destroy();
352    }
353
354    public void dump(String prefix, PrintWriter pw) {
355        pw.print(prefix); pw.print("mStackId="); pw.println(mStackId);
356        for (int taskNdx = 0; taskNdx < mTasks.size(); ++taskNdx) {
357            pw.print(prefix); pw.println(mTasks.get(taskNdx));
358        }
359        if (mAnimationBackgroundSurface.isDimming()) {
360            pw.print(prefix); pw.println("mWindowAnimationBackgroundSurface:");
361            mAnimationBackgroundSurface.printTo(prefix + "  ", pw);
362        }
363        if (mDimLayer.isDimming()) {
364            pw.print(prefix); pw.println("mDimLayer:");
365            mDimLayer.printTo(prefix, pw);
366            pw.print(prefix); pw.print("mDimWinAnimator="); pw.println(mDimWinAnimator);
367        }
368        if (!mExitingAppTokens.isEmpty()) {
369            pw.println();
370            pw.println("  Exiting application tokens:");
371            for (int i=mExitingAppTokens.size()-1; i>=0; i--) {
372                WindowToken token = mExitingAppTokens.get(i);
373                pw.print("  Exiting App #"); pw.print(i);
374                pw.print(' '); pw.print(token);
375                pw.println(':');
376                token.dump(pw, "    ");
377            }
378        }
379    }
380
381    @Override
382    public String toString() {
383        return "{stackId=" + mStackId + " tasks=" + mTasks + "}";
384    }
385}
386