1/*
2 * Copyright (C) 2014 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.systemui.recents;
18
19/**
20 * The launch state of the RecentsActivity.
21 *
22 * Current Constraints:
23 *  - needed in onStart() before onNewIntent()
24 *  - needs to be reset when Recents is hidden
25 *  - needs to be computed in Recents component
26 *  - needs to be accessible by views
27 */
28public class RecentsActivityLaunchState {
29
30    public boolean launchedWithAltTab;
31    public boolean launchedFromApp;
32    // Set if the activity that we launched from entered PiP during the transition into Recents
33    public boolean launchedFromPipApp;
34    // Set if the next activity that quick-switch will launch is the PiP activity
35    public boolean launchedWithNextPipApp;
36    public boolean launchedFromBlacklistedApp;
37    public boolean launchedFromHome;
38    public boolean launchedViaDragGesture;
39    public boolean launchedViaDockGesture;
40    public int launchedToTaskId;
41    public int launchedNumVisibleTasks;
42    public int launchedNumVisibleThumbnails;
43
44    public void reset() {
45        launchedFromHome = false;
46        launchedFromApp = false;
47        launchedFromBlacklistedApp = false;
48        launchedFromPipApp = false;
49        launchedWithNextPipApp = false;
50        launchedToTaskId = -1;
51        launchedWithAltTab = false;
52        launchedViaDragGesture = false;
53        launchedViaDockGesture = false;
54    }
55
56    /**
57     * Returns the task to focus given the current launch state.
58     */
59    public int getInitialFocusTaskIndex(int numTasks, boolean useGridLayout) {
60        RecentsDebugFlags debugFlags = Recents.getDebugFlags();
61        RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
62        if (launchedFromApp) {
63            if (!launchState.launchedWithAltTab && debugFlags.isFastToggleRecentsEnabled()) {
64                // If fast toggling, focus the front most task so that the next tap will launch the
65                // task
66                return numTasks - 1;
67            }
68
69            if (launchState.launchedFromBlacklistedApp) {
70                // If we are launching from a blacklisted app, focus the front most task so that the
71                // next tap will launch the task
72                return numTasks - 1;
73            }
74
75            if (useGridLayout) {
76                // If coming from another app to the grid layout, focus the front most task
77                return numTasks - 1;
78            }
79
80            // If coming from another app, focus the next task
81            return Math.max(0, numTasks - 2);
82        } else {
83            if (!launchState.launchedWithAltTab && debugFlags.isFastToggleRecentsEnabled()) {
84                // If fast toggling, defer focusing until the next tap (which will automatically
85                // focus the front most task)
86                return -1;
87            }
88
89            // If coming from home, focus the front most task
90            return numTasks - 1;
91        }
92    }
93}
94