blob: 51079b0e6ffe6995e9f3bdd02d2171cc345a5853 [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;
Tony Wickham66d1c2f2019-04-24 11:32:59 -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;
Tony1787ee92019-03-20 12:38:35 -050025import static com.android.launcher3.TestProtocol.OVERVIEW_PEEK_STATE_ORDINAL;
vadimt2a648aa2019-02-14 17:47:11 -080026import static com.android.launcher3.TestProtocol.OVERVIEW_STATE_ORDINAL;
Tonye4c2e2b2019-03-25 10:23:39 -050027import static com.android.launcher3.TestProtocol.QUICK_SWITCH_STATE_ORDINAL;
vadimt2a648aa2019-02-14 17:47:11 -080028import static com.android.launcher3.TestProtocol.SPRING_LOADED_STATE_ORDINAL;
Sunny Goyal0c723352017-12-12 12:02:29 -080029import static com.android.launcher3.anim.Interpolators.ACCEL_2;
Sunny Goyal623eddd2018-03-02 12:24:41 -080030import static com.android.launcher3.states.RotationHelper.REQUEST_NONE;
Sunny Goyal0c723352017-12-12 12:02:29 -080031
Sunny Goyal0c723352017-12-12 12:02:29 -080032import android.view.animation.Interpolator;
Sunny Goyalbe93f262017-10-20 17:05:27 -070033
Sunny Goyalc99cb172017-10-19 16:15:09 -070034import com.android.launcher3.states.SpringLoadedState;
Tonye4c2e2b2019-03-25 10:23:39 -050035import com.android.launcher3.uioverrides.UiFactory;
Tonye06fef42019-03-22 15:44:28 -050036import com.android.launcher3.uioverrides.states.AllAppsState;
37import com.android.launcher3.uioverrides.states.OverviewState;
Sunny Goyale39690b2018-08-02 13:35:07 -070038import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070039import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
40
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070041import java.util.Arrays;
42
Sunny Goyal4c7f2152017-10-17 17:17:16 -070043
44/**
Sunny Goyalbe93f262017-10-20 17:05:27 -070045 * Base state for various states used for the Launcher
Sunny Goyal4c7f2152017-10-17 17:17:16 -070046 */
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070047public class LauncherState {
Sunny Goyal4c7f2152017-10-17 17:17:16 -070048
Sunny Goyal7185dd62018-03-14 17:51:49 -070049
50 /**
51 * Set of elements indicating various workspace elements which change visibility across states
52 * Note that workspace is not included here as in that case, we animate individual pages
53 */
54 public static final int NONE = 0;
Tony Wickhambd6f05e2018-03-21 08:16:33 -070055 public static final int HOTSEAT_ICONS = 1 << 0;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -070056 public static final int HOTSEAT_SEARCH_BOX = 1 << 1;
Tony Wickhambd6f05e2018-03-21 08:16:33 -070057 public static final int ALL_APPS_HEADER = 1 << 2;
58 public static final int ALL_APPS_HEADER_EXTRA = 1 << 3; // e.g. app predictions
59 public static final int ALL_APPS_CONTENT = 1 << 4;
Sunny Goyal56e10192018-05-22 13:22:25 -070060 public static final int VERTICAL_SWIPE_INDICATOR = 1 << 5;
Tony Wickham66d1c2f2019-04-24 11:32:59 -070061 public static final int RECENTS_CLEAR_ALL_BUTTON = 1 << 6;
Sunny Goyal7185dd62018-03-14 17:51:49 -070062
Sunny Goyal927447e2018-05-04 13:19:29 -070063 protected static final int FLAG_MULTI_PAGE = 1 << 0;
64 protected static final int FLAG_DISABLE_ACCESSIBILITY = 1 << 1;
65 protected static final int FLAG_DISABLE_RESTORE = 1 << 2;
66 protected static final int FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED = 1 << 3;
67 protected static final int FLAG_DISABLE_PAGE_CLIPPING = 1 << 4;
68 protected static final int FLAG_PAGE_BACKGROUNDS = 1 << 5;
69 protected static final int FLAG_DISABLE_INTERACTION = 1 << 6;
70 protected static final int FLAG_OVERVIEW_UI = 1 << 7;
71 protected static final int FLAG_HIDE_BACK_BUTTON = 1 << 8;
Sunny Goyal5d1873a2018-05-08 11:10:44 -070072 protected static final int FLAG_HAS_SYS_UI_SCRIM = 1 << 9;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -080073
Sunny Goyal0c723352017-12-12 12:02:29 -080074 protected static final PageAlphaProvider DEFAULT_ALPHA_PROVIDER =
75 new PageAlphaProvider(ACCEL_2) {
76 @Override
77 public float getPageAlpha(int pageIndex) {
78 return 1;
79 }
80 };
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070081
Tonye4c2e2b2019-03-25 10:23:39 -050082 private static final LauncherState[] sAllStates = new LauncherState[7];
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070083
Sunny Goyal75c59ac2018-01-15 14:23:11 -080084 /**
85 * TODO: Create a separate class for NORMAL state.
86 */
vadimt6895e402019-02-15 16:42:14 -080087 public static final LauncherState NORMAL = new LauncherState(NORMAL_STATE_ORDINAL,
88 ContainerType.WORKSPACE, 0,
Sunny Goyal5d1873a2018-05-08 11:10:44 -070089 FLAG_DISABLE_RESTORE | FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED | FLAG_HIDE_BACK_BUTTON |
90 FLAG_HAS_SYS_UI_SCRIM);
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070091
Sunny Goyal7185dd62018-03-14 17:51:49 -070092 /**
93 * Various Launcher states arranged in the increasing order of UI layers
94 */
vadimt2a648aa2019-02-14 17:47:11 -080095 public static final LauncherState SPRING_LOADED = new SpringLoadedState(
96 SPRING_LOADED_STATE_ORDINAL);
vadimt2a648aa2019-02-14 17:47:11 -080097 public static final LauncherState ALL_APPS = new AllAppsState(ALL_APPS_STATE_ORDINAL);
Tony1787ee92019-03-20 12:38:35 -050098
99 public static final LauncherState OVERVIEW = new OverviewState(OVERVIEW_STATE_ORDINAL);
100 public static final LauncherState OVERVIEW_PEEK =
101 OverviewState.newPeekState(OVERVIEW_PEEK_STATE_ORDINAL);
Tonye4c2e2b2019-03-25 10:23:39 -0500102 public static final LauncherState QUICK_SWITCH =
103 OverviewState.newSwitchState(QUICK_SWITCH_STATE_ORDINAL);
Tony1787ee92019-03-20 12:38:35 -0500104 public static final LauncherState BACKGROUND_APP =
105 OverviewState.newBackgroundState(BACKGROUND_APP_STATE_ORDINAL);
Tony Wickham6becf7c2018-04-19 11:39:34 -0700106
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700107 public final int ordinal;
108
Sunny Goyalf9403d92017-10-18 10:55:56 -0700109 /**
110 * Used for containerType in {@link com.android.launcher3.logging.UserEventDispatcher}
111 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700112 public final int containerType;
113
Sunny Goyalf9403d92017-10-18 10:55:56 -0700114 /**
115 * True if the state can be persisted across activity restarts.
116 */
Sunny Goyalbc683e92017-12-06 10:25:07 -0800117 public final boolean disableRestore;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700118
119 /**
120 * True if workspace has multiple pages visible.
121 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700122 public final boolean hasMultipleVisiblePages;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700123
124 /**
125 * Accessibility flag for workspace and its pages.
126 * @see android.view.View#setImportantForAccessibility(int)
127 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700128 public final int workspaceAccessibilityFlag;
129
Sunny Goyalbe93f262017-10-20 17:05:27 -0700130 /**
131 * Properties related to state transition animation
132 *
133 * @see WorkspaceStateTransitionAnimation
134 */
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800135 public final boolean hasWorkspacePageBackground;
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800136
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700137 public final int transitionDuration;
138
Sunny Goyalbe93f262017-10-20 17:05:27 -0700139 /**
Tony Wickham53370672017-12-05 10:10:55 -0800140 * True if the state allows workspace icons to be dragged.
141 */
142 public final boolean workspaceIconsCanBeDragged;
143
Sunny Goyalbc683e92017-12-06 10:25:07 -0800144 /**
145 * True if the workspace pages should not be clipped relative to the workspace bounds
146 * for this state.
147 */
148 public final boolean disablePageClipping;
149
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800150 /**
151 * True if launcher can not be directly interacted in this state;
152 */
153 public final boolean disableInteraction;
154
155 /**
156 * True if the state has overview panel visible.
157 */
158 public final boolean overviewUi;
159
Tony Wickham76cf2362018-03-20 14:13:16 -0700160 /**
161 * True if the back button should be hidden when in this state (assuming no floating views are
162 * open, launcher has window focus, etc).
163 */
164 public final boolean hideBackButton;
165
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700166 public final boolean hasSysUiScrim;
167
Sunny Goyal228153d2018-01-04 15:35:22 -0800168 public LauncherState(int id, int containerType, int transitionDuration, int flags) {
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700169 this.containerType = containerType;
170 this.transitionDuration = transitionDuration;
171
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800172 this.hasWorkspacePageBackground = (flags & FLAG_PAGE_BACKGROUNDS) != 0;
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700173 this.hasMultipleVisiblePages = (flags & FLAG_MULTI_PAGE) != 0;
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700174 this.workspaceAccessibilityFlag = (flags & FLAG_DISABLE_ACCESSIBILITY) != 0
175 ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
176 : IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Sunny Goyalbc683e92017-12-06 10:25:07 -0800177 this.disableRestore = (flags & FLAG_DISABLE_RESTORE) != 0;
Tony Wickham53370672017-12-05 10:10:55 -0800178 this.workspaceIconsCanBeDragged = (flags & FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED) != 0;
Sunny Goyalbc683e92017-12-06 10:25:07 -0800179 this.disablePageClipping = (flags & FLAG_DISABLE_PAGE_CLIPPING) != 0;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800180 this.disableInteraction = (flags & FLAG_DISABLE_INTERACTION) != 0;
181 this.overviewUi = (flags & FLAG_OVERVIEW_UI) != 0;
Tony Wickham76cf2362018-03-20 14:13:16 -0700182 this.hideBackButton = (flags & FLAG_HIDE_BACK_BUTTON) != 0;
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700183 this.hasSysUiScrim = (flags & FLAG_HAS_SYS_UI_SCRIM) != 0;
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700184
185 this.ordinal = id;
186 sAllStates[id] = this;
187 }
188
189 public static LauncherState[] values() {
190 return Arrays.copyOf(sAllStates, sAllStates.length);
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700191 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700192
Tonybc234402019-03-19 13:30:25 -0500193 public ScaleAndTranslation getWorkspaceScaleAndTranslation(Launcher launcher) {
194 return new ScaleAndTranslation(1, 0, 0);
Sunny Goyalc99cb172017-10-19 16:15:09 -0700195 }
196
Tonybc234402019-03-19 13:30:25 -0500197 public ScaleAndTranslation getHotseatScaleAndTranslation(Launcher launcher) {
Tony Wickham57e489c2019-03-04 14:57:07 -0800198 // For most states, treat the hotseat as if it were part of the workspace.
199 return getWorkspaceScaleAndTranslation(launcher);
Tony4bb4b892019-03-03 15:32:12 -0800200 }
201
Tonybc234402019-03-19 13:30:25 -0500202 public ScaleAndTranslation getOverviewScaleAndTranslation(Launcher launcher) {
Sunny Goyal9e84e0c2019-04-03 16:44:32 -0700203 return UiFactory.getOverviewScaleAndTranslationForNormalState(launcher);
Tony3bb5e8e2018-03-14 12:05:00 +0000204 }
205
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700206 public void onStateEnabled(Launcher launcher) {
207 dispatchWindowStateChanged(launcher);
208 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700209
210 public void onStateDisabled(Launcher launcher) { }
Sunny Goyalbe93f262017-10-20 17:05:27 -0700211
Sunny Goyal7185dd62018-03-14 17:51:49 -0700212 public int getVisibleElements(Launcher launcher) {
Tony Wickhambd6f05e2018-03-21 08:16:33 -0700213 if (launcher.getDeviceProfile().isVerticalBarLayout()) {
Sunny Goyal56e10192018-05-22 13:22:25 -0700214 return HOTSEAT_ICONS | VERTICAL_SWIPE_INDICATOR;
Tony Wickhambd6f05e2018-03-21 08:16:33 -0700215 }
Sunny Goyal56e10192018-05-22 13:22:25 -0700216 return HOTSEAT_ICONS | HOTSEAT_SEARCH_BOX | VERTICAL_SWIPE_INDICATOR;
Sunny Goyal7185dd62018-03-14 17:51:49 -0700217 }
218
Sunny Goyal228153d2018-01-04 15:35:22 -0800219 /**
220 * Fraction shift in the vertical translation UI and related properties
221 *
222 * @see com.android.launcher3.allapps.AllAppsTransitionController
223 */
224 public float getVerticalProgress(Launcher launcher) {
225 return 1f;
226 }
227
Sunny Goyal927447e2018-05-04 13:19:29 -0700228 public float getWorkspaceScrimAlpha(Launcher launcher) {
229 return 0;
230 }
231
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700232 public String getDescription(Launcher launcher) {
233 return launcher.getWorkspace().getCurrentPageDescription();
234 }
235
Sunny Goyalbc683e92017-12-06 10:25:07 -0800236 public PageAlphaProvider getWorkspacePageAlphaProvider(Launcher launcher) {
237 if (this != NORMAL || !launcher.getDeviceProfile().shouldFadeAdjacentWorkspaceScreens()) {
238 return DEFAULT_ALPHA_PROVIDER;
239 }
Sunny Goyal228153d2018-01-04 15:35:22 -0800240 final int centerPage = launcher.getWorkspace().getNextPage();
Sunny Goyal0c723352017-12-12 12:02:29 -0800241 return new PageAlphaProvider(ACCEL_2) {
242 @Override
243 public float getPageAlpha(int pageIndex) {
244 return pageIndex != centerPage ? 0 : 1f;
245 }
246 };
Sunny Goyalbc683e92017-12-06 10:25:07 -0800247 }
248
Sunny Goyal49bcf342018-01-11 13:59:53 -0800249 public LauncherState getHistoryForState(LauncherState previousState) {
250 // No history is supported
251 return NORMAL;
252 }
253
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800254 /**
255 * Called when the start transition ends and the user settles on this particular state.
256 */
257 public void onStateTransitionEnd(Launcher launcher) {
Hyunyoung Song4bd47252018-06-05 16:26:32 -0700258 if (this == NORMAL || this == SPRING_LOADED) {
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800259 UiFactory.resetOverview(launcher);
Hyunyoung Song4bd47252018-06-05 16:26:32 -0700260 }
261 if (this == NORMAL) {
Sunny Goyal623eddd2018-03-02 12:24:41 -0800262 // Clear any rotation locks when going to normal state
263 launcher.getRotationHelper().setCurrentStateRequest(REQUEST_NONE);
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800264 }
265 }
266
Sunny Goyale39690b2018-08-02 13:35:07 -0700267 public void onBackPressed(Launcher launcher) {
268 if (this != NORMAL) {
269 LauncherStateManager lsm = launcher.getStateManager();
270 LauncherState lastState = lsm.getLastState();
271 launcher.getUserEventDispatcher().logActionCommand(Action.Command.BACK,
272 containerType, lastState.containerType);
273 lsm.goToState(lastState);
274 }
275 }
276
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700277 protected static void dispatchWindowStateChanged(Launcher launcher) {
278 launcher.getWindow().getDecorView().sendAccessibilityEvent(TYPE_WINDOW_STATE_CHANGED);
279 }
Sunny Goyalbc683e92017-12-06 10:25:07 -0800280
Sunny Goyal0c723352017-12-12 12:02:29 -0800281 public static abstract class PageAlphaProvider {
Sunny Goyalbc683e92017-12-06 10:25:07 -0800282
Sunny Goyal0c723352017-12-12 12:02:29 -0800283 public final Interpolator interpolator;
284
285 public PageAlphaProvider(Interpolator interpolator) {
286 this.interpolator = interpolator;
287 }
288
289 public abstract float getPageAlpha(int pageIndex);
Sunny Goyalbc683e92017-12-06 10:25:07 -0800290 }
Tonybc234402019-03-19 13:30:25 -0500291
292 public static class ScaleAndTranslation {
293 public float scale;
294 public float translationX;
295 public float translationY;
296
297 public ScaleAndTranslation(float scale, float translationX, float translationY) {
298 this.scale = scale;
299 this.translationX = translationX;
300 this.translationY = translationY;
301 }
302 }
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700303}