blob: 6d856126980ab14897b3e918cd99677a9fcc2838 [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;
vadimt6895e402019-02-15 16:42:14 -080024import static com.android.launcher3.TestProtocol.NORMAL_STATE_ORDINAL;
vadimt2a648aa2019-02-14 17:47:11 -080025import static com.android.launcher3.TestProtocol.OVERVIEW_STATE_ORDINAL;
26import static com.android.launcher3.TestProtocol.SPRING_LOADED_STATE_ORDINAL;
Sunny Goyal0c723352017-12-12 12:02:29 -080027import static com.android.launcher3.anim.Interpolators.ACCEL_2;
Sunny Goyal623eddd2018-03-02 12:24:41 -080028import static com.android.launcher3.states.RotationHelper.REQUEST_NONE;
Sunny Goyal0c723352017-12-12 12:02:29 -080029
Sunny Goyal0c723352017-12-12 12:02:29 -080030import android.view.animation.Interpolator;
Sunny Goyalbe93f262017-10-20 17:05:27 -070031
Sunny Goyalc99cb172017-10-19 16:15:09 -070032import com.android.launcher3.states.SpringLoadedState;
Sunny Goyal623eddd2018-03-02 12:24:41 -080033import com.android.launcher3.uioverrides.AllAppsState;
Sunny Goyalcf300fc2018-09-05 12:47:21 -070034import com.android.launcher3.uioverrides.BackgroundAppState;
Sunny Goyal85525172017-11-06 13:00:42 -080035import com.android.launcher3.uioverrides.OverviewState;
Sunny Goyal75c59ac2018-01-15 14:23:11 -080036import com.android.launcher3.uioverrides.UiFactory;
Sunny Goyale39690b2018-08-02 13:35:07 -070037import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070038import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
39
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070040import java.util.Arrays;
41
Sunny Goyal4c7f2152017-10-17 17:17:16 -070042
43/**
Sunny Goyalbe93f262017-10-20 17:05:27 -070044 * Base state for various states used for the Launcher
Sunny Goyal4c7f2152017-10-17 17:17:16 -070045 */
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070046public class LauncherState {
Sunny Goyal4c7f2152017-10-17 17:17:16 -070047
Sunny Goyal7185dd62018-03-14 17:51:49 -070048
49 /**
50 * Set of elements indicating various workspace elements which change visibility across states
51 * Note that workspace is not included here as in that case, we animate individual pages
52 */
53 public static final int NONE = 0;
Tony Wickhambd6f05e2018-03-21 08:16:33 -070054 public static final int HOTSEAT_ICONS = 1 << 0;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -070055 public static final int HOTSEAT_SEARCH_BOX = 1 << 1;
Tony Wickhambd6f05e2018-03-21 08:16:33 -070056 public static final int ALL_APPS_HEADER = 1 << 2;
57 public static final int ALL_APPS_HEADER_EXTRA = 1 << 3; // e.g. app predictions
58 public static final int ALL_APPS_CONTENT = 1 << 4;
Sunny Goyal56e10192018-05-22 13:22:25 -070059 public static final int VERTICAL_SWIPE_INDICATOR = 1 << 5;
Sunny Goyal7185dd62018-03-14 17:51:49 -070060
Sunny Goyal927447e2018-05-04 13:19:29 -070061 protected static final int FLAG_MULTI_PAGE = 1 << 0;
62 protected static final int FLAG_DISABLE_ACCESSIBILITY = 1 << 1;
63 protected static final int FLAG_DISABLE_RESTORE = 1 << 2;
64 protected static final int FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED = 1 << 3;
65 protected static final int FLAG_DISABLE_PAGE_CLIPPING = 1 << 4;
66 protected static final int FLAG_PAGE_BACKGROUNDS = 1 << 5;
67 protected static final int FLAG_DISABLE_INTERACTION = 1 << 6;
68 protected static final int FLAG_OVERVIEW_UI = 1 << 7;
69 protected static final int FLAG_HIDE_BACK_BUTTON = 1 << 8;
Sunny Goyal5d1873a2018-05-08 11:10:44 -070070 protected static final int FLAG_HAS_SYS_UI_SCRIM = 1 << 9;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -080071
Sunny Goyal0c723352017-12-12 12:02:29 -080072 protected static final PageAlphaProvider DEFAULT_ALPHA_PROVIDER =
73 new PageAlphaProvider(ACCEL_2) {
74 @Override
75 public float getPageAlpha(int pageIndex) {
76 return 1;
77 }
78 };
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070079
Sunny Goyalcf300fc2018-09-05 12:47:21 -070080 private static final LauncherState[] sAllStates = new LauncherState[6];
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070081
Sunny Goyal75c59ac2018-01-15 14:23:11 -080082 /**
83 * TODO: Create a separate class for NORMAL state.
84 */
vadimt6895e402019-02-15 16:42:14 -080085 public static final LauncherState NORMAL = new LauncherState(NORMAL_STATE_ORDINAL,
86 ContainerType.WORKSPACE, 0,
Sunny Goyal5d1873a2018-05-08 11:10:44 -070087 FLAG_DISABLE_RESTORE | FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED | FLAG_HIDE_BACK_BUTTON |
88 FLAG_HAS_SYS_UI_SCRIM);
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070089
Sunny Goyal7185dd62018-03-14 17:51:49 -070090 /**
91 * Various Launcher states arranged in the increasing order of UI layers
92 */
vadimt2a648aa2019-02-14 17:47:11 -080093 public static final LauncherState SPRING_LOADED = new SpringLoadedState(
94 SPRING_LOADED_STATE_ORDINAL);
95 public static final LauncherState OVERVIEW = new OverviewState(OVERVIEW_STATE_ORDINAL);
96 public static final LauncherState ALL_APPS = new AllAppsState(ALL_APPS_STATE_ORDINAL);
97 public static final LauncherState BACKGROUND_APP = new BackgroundAppState(
98 BACKGROUND_APP_STATE_ORDINAL);
Tony Wickham6becf7c2018-04-19 11:39:34 -070099
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700100 public final int ordinal;
101
Sunny Goyalf9403d92017-10-18 10:55:56 -0700102 /**
103 * Used for containerType in {@link com.android.launcher3.logging.UserEventDispatcher}
104 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700105 public final int containerType;
106
Sunny Goyalf9403d92017-10-18 10:55:56 -0700107 /**
108 * True if the state can be persisted across activity restarts.
109 */
Sunny Goyalbc683e92017-12-06 10:25:07 -0800110 public final boolean disableRestore;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700111
112 /**
113 * True if workspace has multiple pages visible.
114 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700115 public final boolean hasMultipleVisiblePages;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700116
117 /**
118 * Accessibility flag for workspace and its pages.
119 * @see android.view.View#setImportantForAccessibility(int)
120 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700121 public final int workspaceAccessibilityFlag;
122
Sunny Goyalbe93f262017-10-20 17:05:27 -0700123 /**
124 * Properties related to state transition animation
125 *
126 * @see WorkspaceStateTransitionAnimation
127 */
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800128 public final boolean hasWorkspacePageBackground;
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800129
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700130 public final int transitionDuration;
131
Sunny Goyalbe93f262017-10-20 17:05:27 -0700132 /**
Tony Wickham53370672017-12-05 10:10:55 -0800133 * True if the state allows workspace icons to be dragged.
134 */
135 public final boolean workspaceIconsCanBeDragged;
136
Sunny Goyalbc683e92017-12-06 10:25:07 -0800137 /**
138 * True if the workspace pages should not be clipped relative to the workspace bounds
139 * for this state.
140 */
141 public final boolean disablePageClipping;
142
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800143 /**
144 * True if launcher can not be directly interacted in this state;
145 */
146 public final boolean disableInteraction;
147
148 /**
149 * True if the state has overview panel visible.
150 */
151 public final boolean overviewUi;
152
Tony Wickham76cf2362018-03-20 14:13:16 -0700153 /**
154 * True if the back button should be hidden when in this state (assuming no floating views are
155 * open, launcher has window focus, etc).
156 */
157 public final boolean hideBackButton;
158
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700159 public final boolean hasSysUiScrim;
160
Sunny Goyal228153d2018-01-04 15:35:22 -0800161 public LauncherState(int id, int containerType, int transitionDuration, int flags) {
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700162 this.containerType = containerType;
163 this.transitionDuration = transitionDuration;
164
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800165 this.hasWorkspacePageBackground = (flags & FLAG_PAGE_BACKGROUNDS) != 0;
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700166 this.hasMultipleVisiblePages = (flags & FLAG_MULTI_PAGE) != 0;
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700167 this.workspaceAccessibilityFlag = (flags & FLAG_DISABLE_ACCESSIBILITY) != 0
168 ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
169 : IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Sunny Goyalbc683e92017-12-06 10:25:07 -0800170 this.disableRestore = (flags & FLAG_DISABLE_RESTORE) != 0;
Tony Wickham53370672017-12-05 10:10:55 -0800171 this.workspaceIconsCanBeDragged = (flags & FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED) != 0;
Sunny Goyalbc683e92017-12-06 10:25:07 -0800172 this.disablePageClipping = (flags & FLAG_DISABLE_PAGE_CLIPPING) != 0;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800173 this.disableInteraction = (flags & FLAG_DISABLE_INTERACTION) != 0;
174 this.overviewUi = (flags & FLAG_OVERVIEW_UI) != 0;
Tony Wickham76cf2362018-03-20 14:13:16 -0700175 this.hideBackButton = (flags & FLAG_HIDE_BACK_BUTTON) != 0;
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700176 this.hasSysUiScrim = (flags & FLAG_HAS_SYS_UI_SCRIM) != 0;
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700177
178 this.ordinal = id;
179 sAllStates[id] = this;
180 }
181
182 public static LauncherState[] values() {
183 return Arrays.copyOf(sAllStates, sAllStates.length);
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700184 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700185
186 public float[] getWorkspaceScaleAndTranslation(Launcher launcher) {
Sunny Goyal9328a512017-12-21 12:40:38 -0800187 return new float[] {1, 0, 0};
Sunny Goyalc99cb172017-10-19 16:15:09 -0700188 }
189
Tonye9054e32018-03-15 12:48:51 +0000190 /**
Tony0c955592018-04-04 10:23:36 -0700191 * Returns 2 floats designating how to transition overview:
192 * scale for the current and adjacent pages
193 * translationY factor where 0 is top aligned and 0.5 is centered vertically
Tonye9054e32018-03-15 12:48:51 +0000194 */
Tony0c955592018-04-04 10:23:36 -0700195 public float[] getOverviewScaleAndTranslationYFactor(Launcher launcher) {
Sunny Goyal3f271d42019-02-13 14:57:52 -0800196 // TODO: Simplify to use a constant value instead of a factor.
Tony Wickhamb2ddf102018-05-16 12:23:12 -0700197 return new float[] {1.1f, 0f};
Tony3bb5e8e2018-03-14 12:05:00 +0000198 }
199
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700200 public void onStateEnabled(Launcher launcher) {
201 dispatchWindowStateChanged(launcher);
202 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700203
204 public void onStateDisabled(Launcher launcher) { }
Sunny Goyalbe93f262017-10-20 17:05:27 -0700205
Sunny Goyal7185dd62018-03-14 17:51:49 -0700206 public int getVisibleElements(Launcher launcher) {
Tony Wickhambd6f05e2018-03-21 08:16:33 -0700207 if (launcher.getDeviceProfile().isVerticalBarLayout()) {
Sunny Goyal56e10192018-05-22 13:22:25 -0700208 return HOTSEAT_ICONS | VERTICAL_SWIPE_INDICATOR;
Tony Wickhambd6f05e2018-03-21 08:16:33 -0700209 }
Sunny Goyal56e10192018-05-22 13:22:25 -0700210 return HOTSEAT_ICONS | HOTSEAT_SEARCH_BOX | VERTICAL_SWIPE_INDICATOR;
Sunny Goyal7185dd62018-03-14 17:51:49 -0700211 }
212
Sunny Goyal228153d2018-01-04 15:35:22 -0800213 /**
214 * Fraction shift in the vertical translation UI and related properties
215 *
216 * @see com.android.launcher3.allapps.AllAppsTransitionController
217 */
218 public float getVerticalProgress(Launcher launcher) {
219 return 1f;
220 }
221
Sunny Goyal927447e2018-05-04 13:19:29 -0700222 public float getWorkspaceScrimAlpha(Launcher launcher) {
223 return 0;
224 }
225
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700226 public String getDescription(Launcher launcher) {
227 return launcher.getWorkspace().getCurrentPageDescription();
228 }
229
Sunny Goyalbc683e92017-12-06 10:25:07 -0800230 public PageAlphaProvider getWorkspacePageAlphaProvider(Launcher launcher) {
231 if (this != NORMAL || !launcher.getDeviceProfile().shouldFadeAdjacentWorkspaceScreens()) {
232 return DEFAULT_ALPHA_PROVIDER;
233 }
Sunny Goyal228153d2018-01-04 15:35:22 -0800234 final int centerPage = launcher.getWorkspace().getNextPage();
Sunny Goyal0c723352017-12-12 12:02:29 -0800235 return new PageAlphaProvider(ACCEL_2) {
236 @Override
237 public float getPageAlpha(int pageIndex) {
238 return pageIndex != centerPage ? 0 : 1f;
239 }
240 };
Sunny Goyalbc683e92017-12-06 10:25:07 -0800241 }
242
Sunny Goyal49bcf342018-01-11 13:59:53 -0800243 public LauncherState getHistoryForState(LauncherState previousState) {
244 // No history is supported
245 return NORMAL;
246 }
247
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800248 /**
249 * Called when the start transition ends and the user settles on this particular state.
250 */
251 public void onStateTransitionEnd(Launcher launcher) {
Hyunyoung Song4bd47252018-06-05 16:26:32 -0700252 if (this == NORMAL || this == SPRING_LOADED) {
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800253 UiFactory.resetOverview(launcher);
Hyunyoung Song4bd47252018-06-05 16:26:32 -0700254 }
255 if (this == NORMAL) {
Sunny Goyal623eddd2018-03-02 12:24:41 -0800256 // Clear any rotation locks when going to normal state
257 launcher.getRotationHelper().setCurrentStateRequest(REQUEST_NONE);
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800258 }
259 }
260
Sunny Goyale39690b2018-08-02 13:35:07 -0700261 public void onBackPressed(Launcher launcher) {
262 if (this != NORMAL) {
263 LauncherStateManager lsm = launcher.getStateManager();
264 LauncherState lastState = lsm.getLastState();
265 launcher.getUserEventDispatcher().logActionCommand(Action.Command.BACK,
266 containerType, lastState.containerType);
267 lsm.goToState(lastState);
268 }
269 }
270
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700271 protected static void dispatchWindowStateChanged(Launcher launcher) {
272 launcher.getWindow().getDecorView().sendAccessibilityEvent(TYPE_WINDOW_STATE_CHANGED);
273 }
Sunny Goyalbc683e92017-12-06 10:25:07 -0800274
Sunny Goyal0c723352017-12-12 12:02:29 -0800275 public static abstract class PageAlphaProvider {
Sunny Goyalbc683e92017-12-06 10:25:07 -0800276
Sunny Goyal0c723352017-12-12 12:02:29 -0800277 public final Interpolator interpolator;
278
279 public PageAlphaProvider(Interpolator interpolator) {
280 this.interpolator = interpolator;
281 }
282
283 public abstract float getPageAlpha(int pageIndex);
Sunny Goyalbc683e92017-12-06 10:25:07 -0800284 }
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700285}