1/*
2 * Copyright (C) 2017 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.am;
18
19import android.app.RemoteAction;
20import android.content.res.Configuration;
21import android.graphics.Rect;
22
23import com.android.server.am.ActivityStackSupervisor.ActivityContainer;
24import com.android.server.wm.PinnedStackWindowController;
25import com.android.server.wm.PinnedStackWindowListener;
26
27import java.util.ArrayList;
28import java.util.List;
29
30/**
31 * State and management of the pinned stack of activities.
32 */
33class PinnedActivityStack extends ActivityStack<PinnedStackWindowController>
34        implements PinnedStackWindowListener {
35
36    PinnedActivityStack(ActivityContainer activityContainer,
37            RecentTasks recentTasks, boolean onTop) {
38        super(activityContainer, recentTasks, onTop);
39    }
40
41    @Override
42    PinnedStackWindowController createStackWindowController(int displayId, boolean onTop,
43            Rect outBounds) {
44        return new PinnedStackWindowController(mStackId, this, displayId, onTop, outBounds);
45    }
46
47    Rect getDefaultPictureInPictureBounds(float aspectRatio) {
48        return getWindowContainerController().getPictureInPictureBounds(aspectRatio,
49                null /* currentStackBounds */);
50    }
51
52    void animateResizePinnedStack(Rect sourceHintBounds, Rect toBounds, int animationDuration,
53            boolean fromFullscreen) {
54        if (skipResizeAnimation(toBounds == null /* toFullscreen */)) {
55            mService.moveTasksToFullscreenStack(mStackId, true /* onTop */);
56        } else {
57            getWindowContainerController().animateResizePinnedStack(toBounds, sourceHintBounds,
58                    animationDuration, fromFullscreen);
59        }
60    }
61
62    private boolean skipResizeAnimation(boolean toFullscreen) {
63        if (!toFullscreen) {
64            return false;
65        }
66        final Configuration parentConfig = getParent().getConfiguration();
67        final ActivityRecord top = topRunningNonOverlayTaskActivity();
68        return top != null && !top.isConfigurationCompatible(parentConfig);
69    }
70
71    void setPictureInPictureAspectRatio(float aspectRatio) {
72        getWindowContainerController().setPictureInPictureAspectRatio(aspectRatio);
73    }
74
75    void setPictureInPictureActions(List<RemoteAction> actions) {
76        getWindowContainerController().setPictureInPictureActions(actions);
77    }
78
79    boolean isAnimatingBoundsToFullscreen() {
80        return getWindowContainerController().isAnimatingBoundsToFullscreen();
81    }
82
83    /**
84     * Returns whether to defer the scheduling of the multi-window mode.
85     */
86    boolean deferScheduleMultiWindowModeChanged() {
87        // For the pinned stack, the deferring of the multi-window mode changed is tied to the
88        // transition animation into picture-in-picture, and is called once the animation completes,
89        // or is interrupted in a way that would leave the stack in a non-fullscreen state.
90        // @see BoundsAnimationController
91        // @see BoundsAnimationControllerTests
92        return mWindowContainerController.deferScheduleMultiWindowModeChanged();
93    }
94
95    public void updatePictureInPictureModeForPinnedStackAnimation(Rect targetStackBounds) {
96        // It is guaranteed that the activities requiring the update will be in the pinned stack at
97        // this point (either reparented before the animation into PiP, or before reparenting after
98        // the animation out of PiP)
99        synchronized(this) {
100            ArrayList<TaskRecord> tasks = getAllTasks();
101            for (int i = 0; i < tasks.size(); i++ ) {
102                mStackSupervisor.scheduleUpdatePictureInPictureModeIfNeeded(tasks.get(i),
103                        targetStackBounds, true /* immediate */);
104            }
105        }
106    }
107}
108