TaskStack.java revision 9158825f9c41869689d6b1786d7c7aa8bdd524ce
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 final 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    final 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    final 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    TaskStack(WindowManagerService service, int stackId, DisplayContent displayContent) {
71        mService = service;
72        mStackId = stackId;
73        mDisplayContent = displayContent;
74        mDimLayer = new DimLayer(service, this);
75        mAnimationBackgroundSurface = new DimLayer(service, this);
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 (mBounds.isEmpty()) {
125            mDisplayContent.getLogicalDisplayRect(out);
126        } else {
127            out.set(mBounds);
128        }
129        out.intersect(mDisplayContent.mContentRect);
130    }
131
132    boolean isFullscreen() {
133        return mBounds.isEmpty();
134    }
135
136    void resizeBounds(float oldWidth, float oldHeight, float newWidth, float newHeight) {
137        if (oldWidth == newWidth && oldHeight == newHeight) {
138            return;
139        }
140        float widthScale = newWidth / oldWidth;
141        float heightScale = newHeight / oldHeight;
142        mBounds.left = (int)(mBounds.left * widthScale + 0.5);
143        mBounds.top = (int)(mBounds.top * heightScale + 0.5);
144        mBounds.right = (int)(mBounds.right * widthScale + 0.5);
145        mBounds.bottom = (int)(mBounds.bottom * heightScale + 0.5);
146        resizeWindows();
147    }
148
149    /**
150     * Put a Task in this stack. Used for adding and moving.
151     * @param task The task to add.
152     * @param toTop Whether to add it to the top or bottom.
153     */
154    void addTask(Task task, boolean toTop) {
155        int stackNdx;
156        if (!toTop) {
157            stackNdx = 0;
158        } else {
159            stackNdx = mTasks.size();
160            final int currentUserId = mService.mCurrentUserId;
161            if (task.mUserId != currentUserId) {
162                // Place the task below all current user tasks.
163                while (--stackNdx >= 0) {
164                    if (currentUserId != mTasks.get(stackNdx).mUserId) {
165                        break;
166                    }
167                }
168                ++stackNdx;
169            }
170        }
171        if (DEBUG_TASK_MOVEMENT) Slog.d(TAG, "addTask: task=" + task + " toTop=" + toTop
172                + " pos=" + stackNdx);
173        mTasks.add(stackNdx, task);
174
175        task.mStack = this;
176        mDisplayContent.addTask(task, toTop);
177        mDisplayContent.moveStack(this, true);
178    }
179
180    void moveTaskToTop(Task task) {
181        if (DEBUG_TASK_MOVEMENT) Slog.d(TAG, "moveTaskToTop: task=" + task + " Callers="
182                + Debug.getCallers(6));
183        mTasks.remove(task);
184        addTask(task, true);
185    }
186
187    void moveTaskToBottom(Task task) {
188        if (DEBUG_TASK_MOVEMENT) Slog.d(TAG, "moveTaskToBottom: task=" + task);
189        mTasks.remove(task);
190        addTask(task, false);
191    }
192
193    /**
194     * Delete a Task from this stack. If it is the last Task in the stack, remove this stack from
195     * its parent StackBox and merge the parent.
196     * @param task The Task to delete.
197     */
198    void removeTask(Task task) {
199        if (DEBUG_TASK_MOVEMENT) Slog.d(TAG, "removeTask: task=" + task);
200        mTasks.remove(task);
201        mDisplayContent.removeTask(task);
202    }
203
204    int remove() {
205        mAnimationBackgroundSurface.destroySurface();
206        mDimLayer.destroySurface();
207        EventLog.writeEvent(EventLogTags.WM_STACK_REMOVED, mStackId);
208        TaskStack next = mDisplayContent.removeStack(this);
209        if (next != null) {
210            return next.mStackId;
211        }
212        return -1;
213    }
214
215    void resetAnimationBackgroundAnimator() {
216        mAnimationBackgroundAnimator = null;
217        mAnimationBackgroundSurface.hide();
218    }
219
220    private long getDimBehindFadeDuration(long duration) {
221        TypedValue tv = new TypedValue();
222        mService.mContext.getResources().getValue(
223                com.android.internal.R.fraction.config_dimBehindFadeDuration, tv, true);
224        if (tv.type == TypedValue.TYPE_FRACTION) {
225            duration = (long)tv.getFraction(duration, duration);
226        } else if (tv.type >= TypedValue.TYPE_FIRST_INT && tv.type <= TypedValue.TYPE_LAST_INT) {
227            duration = tv.data;
228        }
229        return duration;
230    }
231
232    boolean animateDimLayers() {
233        final int dimLayer;
234        final float dimAmount;
235        if (mDimWinAnimator == null) {
236            dimLayer = mDimLayer.getLayer();
237            dimAmount = 0;
238        } else {
239            dimLayer = mDimWinAnimator.mAnimLayer - WindowManagerService.LAYER_OFFSET_DIM;
240            dimAmount = mDimWinAnimator.mWin.mAttrs.dimAmount;
241        }
242        final float targetAlpha = mDimLayer.getTargetAlpha();
243        if (targetAlpha != dimAmount) {
244            if (mDimWinAnimator == null) {
245                mDimLayer.hide(DEFAULT_DIM_DURATION);
246            } else {
247                long duration = (mDimWinAnimator.mAnimating && mDimWinAnimator.mAnimation != null)
248                        ? mDimWinAnimator.mAnimation.computeDurationHint()
249                        : DEFAULT_DIM_DURATION;
250                if (targetAlpha > dimAmount) {
251                    duration = getDimBehindFadeDuration(duration);
252                }
253                mDimLayer.show(dimLayer, dimAmount, duration);
254            }
255        } else if (mDimLayer.getLayer() != dimLayer) {
256            mDimLayer.setLayer(dimLayer);
257        }
258        if (mDimLayer.isAnimating()) {
259            if (!mService.okToDisplay()) {
260                // Jump to the end of the animation.
261                mDimLayer.show();
262            } else {
263                return mDimLayer.stepAnimation();
264            }
265        }
266        return false;
267    }
268
269    void resetDimmingTag() {
270        mDimmingTag = false;
271    }
272
273    void setDimmingTag() {
274        mDimmingTag = true;
275    }
276
277    boolean testDimmingTag() {
278        return mDimmingTag;
279    }
280
281    boolean isDimming() {
282        return mDimLayer.isDimming();
283    }
284
285    boolean isDimming(WindowStateAnimator winAnimator) {
286        return mDimWinAnimator == winAnimator && mDimLayer.isDimming();
287    }
288
289    void startDimmingIfNeeded(WindowStateAnimator newWinAnimator) {
290        // Only set dim params on the highest dimmed layer.
291        final WindowStateAnimator existingDimWinAnimator = mDimWinAnimator;
292        // Don't turn on for an unshown surface, or for any layer but the highest dimmed layer.
293        if (newWinAnimator.mSurfaceShown && (existingDimWinAnimator == null
294                || !existingDimWinAnimator.mSurfaceShown
295                || existingDimWinAnimator.mAnimLayer < newWinAnimator.mAnimLayer)) {
296            mDimWinAnimator = newWinAnimator;
297        }
298    }
299
300    void stopDimmingIfNeeded() {
301        if (!mDimmingTag && isDimming()) {
302            mDimWinAnimator = null;
303        }
304    }
305
306    void setAnimationBackground(WindowStateAnimator winAnimator, int color) {
307        int animLayer = winAnimator.mAnimLayer;
308        if (mAnimationBackgroundAnimator == null
309                || animLayer < mAnimationBackgroundAnimator.mAnimLayer) {
310            mAnimationBackgroundAnimator = winAnimator;
311            animLayer = mService.adjustAnimationBackground(winAnimator);
312            mAnimationBackgroundSurface.show(animLayer - WindowManagerService.LAYER_OFFSET_DIM,
313                    ((color >> 24) & 0xff) / 255f, 0);
314        }
315    }
316
317    void switchUser(int userId) {
318        int top = mTasks.size();
319        for (int taskNdx = 0; taskNdx < top; ++taskNdx) {
320            Task task = mTasks.get(taskNdx);
321            if (task.mUserId == userId) {
322                mTasks.remove(taskNdx);
323                mTasks.add(task);
324                --top;
325            }
326        }
327    }
328
329    void close() {
330        mDimLayer.mDimSurface.destroy();
331        mAnimationBackgroundSurface.mDimSurface.destroy();
332    }
333
334    public void dump(String prefix, PrintWriter pw) {
335        pw.print(prefix); pw.print("mStackId="); pw.println(mStackId);
336        for (int taskNdx = 0; taskNdx < mTasks.size(); ++taskNdx) {
337            pw.print(prefix); pw.println(mTasks.get(taskNdx));
338        }
339        if (mAnimationBackgroundSurface.isDimming()) {
340            pw.print(prefix); pw.println("mWindowAnimationBackgroundSurface:");
341            mAnimationBackgroundSurface.printTo(prefix + "  ", pw);
342        }
343        if (mDimLayer.isDimming()) {
344            pw.print(prefix); pw.println("mDimLayer:");
345            mDimLayer.printTo(prefix, pw);
346            pw.print(prefix); pw.print("mDimWinAnimator="); pw.println(mDimWinAnimator);
347        }
348    }
349
350    @Override
351    public String toString() {
352        return "{stackId=" + mStackId + " tasks=" + mTasks + "}";
353    }
354}
355