blob: 471d28d3f44bd564846daffcf9c51084ac671c31 [file] [log] [blame]
Sunny Goyal4c7f2152017-10-17 17:17:16 -07001/*
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 */
16package com.android.launcher3;
17
18import static android.view.View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
19import static android.view.View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070020import static android.view.accessibility.AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
Vadim Tryshev1b0c5032018-05-23 12:54:27 -070021
vadimt2a648aa2019-02-14 17:47:11 -080022import static com.android.launcher3.TestProtocol.ALL_APPS_STATE_ORDINAL;
23import static com.android.launcher3.TestProtocol.BACKGROUND_APP_STATE_ORDINAL;
24import static com.android.launcher3.TestProtocol.OVERVIEW_STATE_ORDINAL;
25import static com.android.launcher3.TestProtocol.SPRING_LOADED_STATE_ORDINAL;
Sunny Goyal0c723352017-12-12 12:02:29 -080026import static com.android.launcher3.anim.Interpolators.ACCEL_2;
Sunny Goyal623eddd2018-03-02 12:24:41 -080027import static com.android.launcher3.states.RotationHelper.REQUEST_NONE;
Sunny Goyal0c723352017-12-12 12:02:29 -080028
Sunny Goyal0c723352017-12-12 12:02:29 -080029import android.view.animation.Interpolator;
Sunny Goyalbe93f262017-10-20 17:05:27 -070030
Sunny Goyalc99cb172017-10-19 16:15:09 -070031import com.android.launcher3.states.SpringLoadedState;
Sunny Goyal623eddd2018-03-02 12:24:41 -080032import com.android.launcher3.uioverrides.AllAppsState;
Sunny Goyalcf300fc2018-09-05 12:47:21 -070033import com.android.launcher3.uioverrides.BackgroundAppState;
Sunny Goyal85525172017-11-06 13:00:42 -080034import com.android.launcher3.uioverrides.OverviewState;
Sunny Goyal75c59ac2018-01-15 14:23:11 -080035import com.android.launcher3.uioverrides.UiFactory;
Sunny Goyale39690b2018-08-02 13:35:07 -070036import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070037import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
38
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070039import java.util.Arrays;
40
Sunny Goyal4c7f2152017-10-17 17:17:16 -070041
42/**
Sunny Goyalbe93f262017-10-20 17:05:27 -070043 * Base state for various states used for the Launcher
Sunny Goyal4c7f2152017-10-17 17:17:16 -070044 */
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070045public class LauncherState {
Sunny Goyal4c7f2152017-10-17 17:17:16 -070046
Sunny Goyal7185dd62018-03-14 17:51:49 -070047
48 /**
49 * Set of elements indicating various workspace elements which change visibility across states
50 * Note that workspace is not included here as in that case, we animate individual pages
51 */
52 public static final int NONE = 0;
Tony Wickhambd6f05e2018-03-21 08:16:33 -070053 public static final int HOTSEAT_ICONS = 1 << 0;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -070054 public static final int HOTSEAT_SEARCH_BOX = 1 << 1;
Tony Wickhambd6f05e2018-03-21 08:16:33 -070055 public static final int ALL_APPS_HEADER = 1 << 2;
56 public static final int ALL_APPS_HEADER_EXTRA = 1 << 3; // e.g. app predictions
57 public static final int ALL_APPS_CONTENT = 1 << 4;
Sunny Goyal56e10192018-05-22 13:22:25 -070058 public static final int VERTICAL_SWIPE_INDICATOR = 1 << 5;
Sunny Goyal7185dd62018-03-14 17:51:49 -070059
Sunny Goyal927447e2018-05-04 13:19:29 -070060 protected static final int FLAG_MULTI_PAGE = 1 << 0;
61 protected static final int FLAG_DISABLE_ACCESSIBILITY = 1 << 1;
62 protected static final int FLAG_DISABLE_RESTORE = 1 << 2;
63 protected static final int FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED = 1 << 3;
64 protected static final int FLAG_DISABLE_PAGE_CLIPPING = 1 << 4;
65 protected static final int FLAG_PAGE_BACKGROUNDS = 1 << 5;
66 protected static final int FLAG_DISABLE_INTERACTION = 1 << 6;
67 protected static final int FLAG_OVERVIEW_UI = 1 << 7;
68 protected static final int FLAG_HIDE_BACK_BUTTON = 1 << 8;
Sunny Goyal5d1873a2018-05-08 11:10:44 -070069 protected static final int FLAG_HAS_SYS_UI_SCRIM = 1 << 9;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -080070
Sunny Goyal0c723352017-12-12 12:02:29 -080071 protected static final PageAlphaProvider DEFAULT_ALPHA_PROVIDER =
72 new PageAlphaProvider(ACCEL_2) {
73 @Override
74 public float getPageAlpha(int pageIndex) {
75 return 1;
76 }
77 };
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070078
Sunny Goyalcf300fc2018-09-05 12:47:21 -070079 private static final LauncherState[] sAllStates = new LauncherState[6];
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070080
Sunny Goyal75c59ac2018-01-15 14:23:11 -080081 /**
82 * TODO: Create a separate class for NORMAL state.
83 */
Sunny Goyal5d1873a2018-05-08 11:10:44 -070084 public static final LauncherState NORMAL = new LauncherState(0, ContainerType.WORKSPACE, 0,
85 FLAG_DISABLE_RESTORE | FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED | FLAG_HIDE_BACK_BUTTON |
86 FLAG_HAS_SYS_UI_SCRIM);
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070087
Sunny Goyal7185dd62018-03-14 17:51:49 -070088 /**
89 * Various Launcher states arranged in the increasing order of UI layers
90 */
vadimt2a648aa2019-02-14 17:47:11 -080091 public static final LauncherState SPRING_LOADED = new SpringLoadedState(
92 SPRING_LOADED_STATE_ORDINAL);
93 public static final LauncherState OVERVIEW = new OverviewState(OVERVIEW_STATE_ORDINAL);
94 public static final LauncherState ALL_APPS = new AllAppsState(ALL_APPS_STATE_ORDINAL);
95 public static final LauncherState BACKGROUND_APP = new BackgroundAppState(
96 BACKGROUND_APP_STATE_ORDINAL);
Tony Wickham6becf7c2018-04-19 11:39:34 -070097
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070098 public final int ordinal;
99
Sunny Goyalf9403d92017-10-18 10:55:56 -0700100 /**
101 * Used for containerType in {@link com.android.launcher3.logging.UserEventDispatcher}
102 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700103 public final int containerType;
104
Sunny Goyalf9403d92017-10-18 10:55:56 -0700105 /**
106 * True if the state can be persisted across activity restarts.
107 */
Sunny Goyalbc683e92017-12-06 10:25:07 -0800108 public final boolean disableRestore;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700109
110 /**
111 * True if workspace has multiple pages visible.
112 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700113 public final boolean hasMultipleVisiblePages;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700114
115 /**
116 * Accessibility flag for workspace and its pages.
117 * @see android.view.View#setImportantForAccessibility(int)
118 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700119 public final int workspaceAccessibilityFlag;
120
Sunny Goyalbe93f262017-10-20 17:05:27 -0700121 /**
122 * Properties related to state transition animation
123 *
124 * @see WorkspaceStateTransitionAnimation
125 */
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800126 public final boolean hasWorkspacePageBackground;
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800127
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700128 public final int transitionDuration;
129
Sunny Goyalbe93f262017-10-20 17:05:27 -0700130 /**
Tony Wickham53370672017-12-05 10:10:55 -0800131 * True if the state allows workspace icons to be dragged.
132 */
133 public final boolean workspaceIconsCanBeDragged;
134
Sunny Goyalbc683e92017-12-06 10:25:07 -0800135 /**
136 * True if the workspace pages should not be clipped relative to the workspace bounds
137 * for this state.
138 */
139 public final boolean disablePageClipping;
140
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800141 /**
142 * True if launcher can not be directly interacted in this state;
143 */
144 public final boolean disableInteraction;
145
146 /**
147 * True if the state has overview panel visible.
148 */
149 public final boolean overviewUi;
150
Tony Wickham76cf2362018-03-20 14:13:16 -0700151 /**
152 * True if the back button should be hidden when in this state (assuming no floating views are
153 * open, launcher has window focus, etc).
154 */
155 public final boolean hideBackButton;
156
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700157 public final boolean hasSysUiScrim;
158
Sunny Goyal228153d2018-01-04 15:35:22 -0800159 public LauncherState(int id, int containerType, int transitionDuration, int flags) {
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700160 this.containerType = containerType;
161 this.transitionDuration = transitionDuration;
162
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800163 this.hasWorkspacePageBackground = (flags & FLAG_PAGE_BACKGROUNDS) != 0;
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700164 this.hasMultipleVisiblePages = (flags & FLAG_MULTI_PAGE) != 0;
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700165 this.workspaceAccessibilityFlag = (flags & FLAG_DISABLE_ACCESSIBILITY) != 0
166 ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
167 : IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Sunny Goyalbc683e92017-12-06 10:25:07 -0800168 this.disableRestore = (flags & FLAG_DISABLE_RESTORE) != 0;
Tony Wickham53370672017-12-05 10:10:55 -0800169 this.workspaceIconsCanBeDragged = (flags & FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED) != 0;
Sunny Goyalbc683e92017-12-06 10:25:07 -0800170 this.disablePageClipping = (flags & FLAG_DISABLE_PAGE_CLIPPING) != 0;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800171 this.disableInteraction = (flags & FLAG_DISABLE_INTERACTION) != 0;
172 this.overviewUi = (flags & FLAG_OVERVIEW_UI) != 0;
Tony Wickham76cf2362018-03-20 14:13:16 -0700173 this.hideBackButton = (flags & FLAG_HIDE_BACK_BUTTON) != 0;
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700174 this.hasSysUiScrim = (flags & FLAG_HAS_SYS_UI_SCRIM) != 0;
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700175
176 this.ordinal = id;
177 sAllStates[id] = this;
178 }
179
180 public static LauncherState[] values() {
181 return Arrays.copyOf(sAllStates, sAllStates.length);
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700182 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700183
184 public float[] getWorkspaceScaleAndTranslation(Launcher launcher) {
Sunny Goyal9328a512017-12-21 12:40:38 -0800185 return new float[] {1, 0, 0};
Sunny Goyalc99cb172017-10-19 16:15:09 -0700186 }
187
Tonye9054e32018-03-15 12:48:51 +0000188 /**
Tony0c955592018-04-04 10:23:36 -0700189 * Returns 2 floats designating how to transition overview:
190 * scale for the current and adjacent pages
191 * translationY factor where 0 is top aligned and 0.5 is centered vertically
Tonye9054e32018-03-15 12:48:51 +0000192 */
Tony0c955592018-04-04 10:23:36 -0700193 public float[] getOverviewScaleAndTranslationYFactor(Launcher launcher) {
Sunny Goyal3f271d42019-02-13 14:57:52 -0800194 // TODO: Simplify to use a constant value instead of a factor.
Tony Wickhamb2ddf102018-05-16 12:23:12 -0700195 return new float[] {1.1f, 0f};
Tony3bb5e8e2018-03-14 12:05:00 +0000196 }
197
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700198 public void onStateEnabled(Launcher launcher) {
199 dispatchWindowStateChanged(launcher);
200 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700201
202 public void onStateDisabled(Launcher launcher) { }
Sunny Goyalbe93f262017-10-20 17:05:27 -0700203
Sunny Goyal7185dd62018-03-14 17:51:49 -0700204 public int getVisibleElements(Launcher launcher) {
Tony Wickhambd6f05e2018-03-21 08:16:33 -0700205 if (launcher.getDeviceProfile().isVerticalBarLayout()) {
Sunny Goyal56e10192018-05-22 13:22:25 -0700206 return HOTSEAT_ICONS | VERTICAL_SWIPE_INDICATOR;
Tony Wickhambd6f05e2018-03-21 08:16:33 -0700207 }
Sunny Goyal56e10192018-05-22 13:22:25 -0700208 return HOTSEAT_ICONS | HOTSEAT_SEARCH_BOX | VERTICAL_SWIPE_INDICATOR;
Sunny Goyal7185dd62018-03-14 17:51:49 -0700209 }
210
Sunny Goyal228153d2018-01-04 15:35:22 -0800211 /**
212 * Fraction shift in the vertical translation UI and related properties
213 *
214 * @see com.android.launcher3.allapps.AllAppsTransitionController
215 */
216 public float getVerticalProgress(Launcher launcher) {
217 return 1f;
218 }
219
Sunny Goyal927447e2018-05-04 13:19:29 -0700220 public float getWorkspaceScrimAlpha(Launcher launcher) {
221 return 0;
222 }
223
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700224 public String getDescription(Launcher launcher) {
225 return launcher.getWorkspace().getCurrentPageDescription();
226 }
227
Sunny Goyalbc683e92017-12-06 10:25:07 -0800228 public PageAlphaProvider getWorkspacePageAlphaProvider(Launcher launcher) {
229 if (this != NORMAL || !launcher.getDeviceProfile().shouldFadeAdjacentWorkspaceScreens()) {
230 return DEFAULT_ALPHA_PROVIDER;
231 }
Sunny Goyal228153d2018-01-04 15:35:22 -0800232 final int centerPage = launcher.getWorkspace().getNextPage();
Sunny Goyal0c723352017-12-12 12:02:29 -0800233 return new PageAlphaProvider(ACCEL_2) {
234 @Override
235 public float getPageAlpha(int pageIndex) {
236 return pageIndex != centerPage ? 0 : 1f;
237 }
238 };
Sunny Goyalbc683e92017-12-06 10:25:07 -0800239 }
240
Sunny Goyal49bcf342018-01-11 13:59:53 -0800241 public LauncherState getHistoryForState(LauncherState previousState) {
242 // No history is supported
243 return NORMAL;
244 }
245
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800246 /**
247 * Called when the start transition ends and the user settles on this particular state.
248 */
249 public void onStateTransitionEnd(Launcher launcher) {
Hyunyoung Song4bd47252018-06-05 16:26:32 -0700250 if (this == NORMAL || this == SPRING_LOADED) {
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800251 UiFactory.resetOverview(launcher);
Hyunyoung Song4bd47252018-06-05 16:26:32 -0700252 }
253 if (this == NORMAL) {
Sunny Goyal623eddd2018-03-02 12:24:41 -0800254 // Clear any rotation locks when going to normal state
255 launcher.getRotationHelper().setCurrentStateRequest(REQUEST_NONE);
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800256 }
257 }
258
Sunny Goyale39690b2018-08-02 13:35:07 -0700259 public void onBackPressed(Launcher launcher) {
260 if (this != NORMAL) {
261 LauncherStateManager lsm = launcher.getStateManager();
262 LauncherState lastState = lsm.getLastState();
263 launcher.getUserEventDispatcher().logActionCommand(Action.Command.BACK,
264 containerType, lastState.containerType);
265 lsm.goToState(lastState);
266 }
267 }
268
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700269 protected static void dispatchWindowStateChanged(Launcher launcher) {
270 launcher.getWindow().getDecorView().sendAccessibilityEvent(TYPE_WINDOW_STATE_CHANGED);
271 }
Sunny Goyalbc683e92017-12-06 10:25:07 -0800272
Sunny Goyal0c723352017-12-12 12:02:29 -0800273 public static abstract class PageAlphaProvider {
Sunny Goyalbc683e92017-12-06 10:25:07 -0800274
Sunny Goyal0c723352017-12-12 12:02:29 -0800275 public final Interpolator interpolator;
276
277 public PageAlphaProvider(Interpolator interpolator) {
278 this.interpolator = interpolator;
279 }
280
281 public abstract float getPageAlpha(int pageIndex);
Sunny Goyalbc683e92017-12-06 10:25:07 -0800282 }
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700283}