blob: c65a871b6c384f5355bf2beb9a1edf549634f09a [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;
vadimt2a648aa2019-02-14 17:47:11 -080021import static com.android.launcher3.TestProtocol.ALL_APPS_STATE_ORDINAL;
22import static com.android.launcher3.TestProtocol.BACKGROUND_APP_STATE_ORDINAL;
vadimt6895e402019-02-15 16:42:14 -080023import static com.android.launcher3.TestProtocol.NORMAL_STATE_ORDINAL;
Tony1787ee92019-03-20 12:38:35 -050024import static com.android.launcher3.TestProtocol.OVERVIEW_PEEK_STATE_ORDINAL;
vadimt2a648aa2019-02-14 17:47:11 -080025import static com.android.launcher3.TestProtocol.OVERVIEW_STATE_ORDINAL;
Tonye4c2e2b2019-03-25 10:23:39 -050026import static com.android.launcher3.TestProtocol.QUICK_SWITCH_STATE_ORDINAL;
vadimt2a648aa2019-02-14 17:47:11 -080027import static com.android.launcher3.TestProtocol.SPRING_LOADED_STATE_ORDINAL;
Sunny Goyal0c723352017-12-12 12:02:29 -080028import static com.android.launcher3.anim.Interpolators.ACCEL_2;
Sunny Goyal623eddd2018-03-02 12:24:41 -080029import static com.android.launcher3.states.RotationHelper.REQUEST_NONE;
Sunny Goyal0c723352017-12-12 12:02:29 -080030
Sunny Goyal0c723352017-12-12 12:02:29 -080031import android.view.animation.Interpolator;
Sunny Goyalbe93f262017-10-20 17:05:27 -070032
Tony1787ee92019-03-20 12:38:35 -050033import com.android.launcher3.config.FeatureFlags;
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;
Sunny Goyal7185dd62018-03-14 17:51:49 -070061
Sunny Goyal927447e2018-05-04 13:19:29 -070062 protected static final int FLAG_MULTI_PAGE = 1 << 0;
63 protected static final int FLAG_DISABLE_ACCESSIBILITY = 1 << 1;
64 protected static final int FLAG_DISABLE_RESTORE = 1 << 2;
65 protected static final int FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED = 1 << 3;
66 protected static final int FLAG_DISABLE_PAGE_CLIPPING = 1 << 4;
67 protected static final int FLAG_PAGE_BACKGROUNDS = 1 << 5;
68 protected static final int FLAG_DISABLE_INTERACTION = 1 << 6;
69 protected static final int FLAG_OVERVIEW_UI = 1 << 7;
70 protected static final int FLAG_HIDE_BACK_BUTTON = 1 << 8;
Sunny Goyal5d1873a2018-05-08 11:10:44 -070071 protected static final int FLAG_HAS_SYS_UI_SCRIM = 1 << 9;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -080072
Sunny Goyal0c723352017-12-12 12:02:29 -080073 protected static final PageAlphaProvider DEFAULT_ALPHA_PROVIDER =
74 new PageAlphaProvider(ACCEL_2) {
75 @Override
76 public float getPageAlpha(int pageIndex) {
77 return 1;
78 }
79 };
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070080
Tonye4c2e2b2019-03-25 10:23:39 -050081 private static final LauncherState[] sAllStates = new LauncherState[7];
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070082
Sunny Goyal75c59ac2018-01-15 14:23:11 -080083 /**
84 * TODO: Create a separate class for NORMAL state.
85 */
vadimt6895e402019-02-15 16:42:14 -080086 public static final LauncherState NORMAL = new LauncherState(NORMAL_STATE_ORDINAL,
87 ContainerType.WORKSPACE, 0,
Sunny Goyal5d1873a2018-05-08 11:10:44 -070088 FLAG_DISABLE_RESTORE | FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED | FLAG_HIDE_BACK_BUTTON |
89 FLAG_HAS_SYS_UI_SCRIM);
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070090
Sunny Goyal7185dd62018-03-14 17:51:49 -070091 /**
92 * Various Launcher states arranged in the increasing order of UI layers
93 */
vadimt2a648aa2019-02-14 17:47:11 -080094 public static final LauncherState SPRING_LOADED = new SpringLoadedState(
95 SPRING_LOADED_STATE_ORDINAL);
vadimt2a648aa2019-02-14 17:47:11 -080096 public static final LauncherState ALL_APPS = new AllAppsState(ALL_APPS_STATE_ORDINAL);
Tony1787ee92019-03-20 12:38:35 -050097
98 public static final LauncherState OVERVIEW = new OverviewState(OVERVIEW_STATE_ORDINAL);
99 public static final LauncherState OVERVIEW_PEEK =
100 OverviewState.newPeekState(OVERVIEW_PEEK_STATE_ORDINAL);
Tonye4c2e2b2019-03-25 10:23:39 -0500101 public static final LauncherState QUICK_SWITCH =
102 OverviewState.newSwitchState(QUICK_SWITCH_STATE_ORDINAL);
Tony1787ee92019-03-20 12:38:35 -0500103 public static final LauncherState BACKGROUND_APP =
104 OverviewState.newBackgroundState(BACKGROUND_APP_STATE_ORDINAL);
Tony Wickham6becf7c2018-04-19 11:39:34 -0700105
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700106 public final int ordinal;
107
Sunny Goyalf9403d92017-10-18 10:55:56 -0700108 /**
109 * Used for containerType in {@link com.android.launcher3.logging.UserEventDispatcher}
110 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700111 public final int containerType;
112
Sunny Goyalf9403d92017-10-18 10:55:56 -0700113 /**
114 * True if the state can be persisted across activity restarts.
115 */
Sunny Goyalbc683e92017-12-06 10:25:07 -0800116 public final boolean disableRestore;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700117
118 /**
119 * True if workspace has multiple pages visible.
120 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700121 public final boolean hasMultipleVisiblePages;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700122
123 /**
124 * Accessibility flag for workspace and its pages.
125 * @see android.view.View#setImportantForAccessibility(int)
126 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700127 public final int workspaceAccessibilityFlag;
128
Sunny Goyalbe93f262017-10-20 17:05:27 -0700129 /**
130 * Properties related to state transition animation
131 *
132 * @see WorkspaceStateTransitionAnimation
133 */
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800134 public final boolean hasWorkspacePageBackground;
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800135
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700136 public final int transitionDuration;
137
Sunny Goyalbe93f262017-10-20 17:05:27 -0700138 /**
Tony Wickham53370672017-12-05 10:10:55 -0800139 * True if the state allows workspace icons to be dragged.
140 */
141 public final boolean workspaceIconsCanBeDragged;
142
Sunny Goyalbc683e92017-12-06 10:25:07 -0800143 /**
144 * True if the workspace pages should not be clipped relative to the workspace bounds
145 * for this state.
146 */
147 public final boolean disablePageClipping;
148
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800149 /**
150 * True if launcher can not be directly interacted in this state;
151 */
152 public final boolean disableInteraction;
153
154 /**
155 * True if the state has overview panel visible.
156 */
157 public final boolean overviewUi;
158
Tony Wickham76cf2362018-03-20 14:13:16 -0700159 /**
160 * True if the back button should be hidden when in this state (assuming no floating views are
161 * open, launcher has window focus, etc).
162 */
163 public final boolean hideBackButton;
164
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700165 public final boolean hasSysUiScrim;
166
Sunny Goyal228153d2018-01-04 15:35:22 -0800167 public LauncherState(int id, int containerType, int transitionDuration, int flags) {
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700168 this.containerType = containerType;
169 this.transitionDuration = transitionDuration;
170
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800171 this.hasWorkspacePageBackground = (flags & FLAG_PAGE_BACKGROUNDS) != 0;
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700172 this.hasMultipleVisiblePages = (flags & FLAG_MULTI_PAGE) != 0;
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700173 this.workspaceAccessibilityFlag = (flags & FLAG_DISABLE_ACCESSIBILITY) != 0
174 ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
175 : IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Sunny Goyalbc683e92017-12-06 10:25:07 -0800176 this.disableRestore = (flags & FLAG_DISABLE_RESTORE) != 0;
Tony Wickham53370672017-12-05 10:10:55 -0800177 this.workspaceIconsCanBeDragged = (flags & FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED) != 0;
Sunny Goyalbc683e92017-12-06 10:25:07 -0800178 this.disablePageClipping = (flags & FLAG_DISABLE_PAGE_CLIPPING) != 0;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800179 this.disableInteraction = (flags & FLAG_DISABLE_INTERACTION) != 0;
180 this.overviewUi = (flags & FLAG_OVERVIEW_UI) != 0;
Tony Wickham76cf2362018-03-20 14:13:16 -0700181 this.hideBackButton = (flags & FLAG_HIDE_BACK_BUTTON) != 0;
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700182 this.hasSysUiScrim = (flags & FLAG_HAS_SYS_UI_SCRIM) != 0;
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700183
184 this.ordinal = id;
185 sAllStates[id] = this;
186 }
187
188 public static LauncherState[] values() {
189 return Arrays.copyOf(sAllStates, sAllStates.length);
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700190 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700191
Tonybc234402019-03-19 13:30:25 -0500192 public ScaleAndTranslation getWorkspaceScaleAndTranslation(Launcher launcher) {
193 return new ScaleAndTranslation(1, 0, 0);
Sunny Goyalc99cb172017-10-19 16:15:09 -0700194 }
195
Tonybc234402019-03-19 13:30:25 -0500196 public ScaleAndTranslation getHotseatScaleAndTranslation(Launcher launcher) {
Tony Wickham57e489c2019-03-04 14:57:07 -0800197 // For most states, treat the hotseat as if it were part of the workspace.
198 return getWorkspaceScaleAndTranslation(launcher);
Tony4bb4b892019-03-03 15:32:12 -0800199 }
200
Tonybc234402019-03-19 13:30:25 -0500201 public ScaleAndTranslation getOverviewScaleAndTranslation(Launcher launcher) {
Tony1787ee92019-03-20 12:38:35 -0500202 if (FeatureFlags.SWIPE_HOME.get()) {
203 float offscreenTranslationX = launcher.getDragLayer().getWidth()
204 - launcher.getOverviewPanel().getPaddingStart();
205 return new ScaleAndTranslation(1f, offscreenTranslationX, 0f);
206 }
Tonybc234402019-03-19 13:30:25 -0500207 return new ScaleAndTranslation(1.1f, 0f, 0f);
Tony3bb5e8e2018-03-14 12:05:00 +0000208 }
209
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700210 public void onStateEnabled(Launcher launcher) {
211 dispatchWindowStateChanged(launcher);
212 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700213
214 public void onStateDisabled(Launcher launcher) { }
Sunny Goyalbe93f262017-10-20 17:05:27 -0700215
Sunny Goyal7185dd62018-03-14 17:51:49 -0700216 public int getVisibleElements(Launcher launcher) {
Tony Wickhambd6f05e2018-03-21 08:16:33 -0700217 if (launcher.getDeviceProfile().isVerticalBarLayout()) {
Sunny Goyal56e10192018-05-22 13:22:25 -0700218 return HOTSEAT_ICONS | VERTICAL_SWIPE_INDICATOR;
Tony Wickhambd6f05e2018-03-21 08:16:33 -0700219 }
Sunny Goyal56e10192018-05-22 13:22:25 -0700220 return HOTSEAT_ICONS | HOTSEAT_SEARCH_BOX | VERTICAL_SWIPE_INDICATOR;
Sunny Goyal7185dd62018-03-14 17:51:49 -0700221 }
222
Sunny Goyal228153d2018-01-04 15:35:22 -0800223 /**
224 * Fraction shift in the vertical translation UI and related properties
225 *
226 * @see com.android.launcher3.allapps.AllAppsTransitionController
227 */
228 public float getVerticalProgress(Launcher launcher) {
229 return 1f;
230 }
231
Sunny Goyal927447e2018-05-04 13:19:29 -0700232 public float getWorkspaceScrimAlpha(Launcher launcher) {
233 return 0;
234 }
235
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700236 public String getDescription(Launcher launcher) {
237 return launcher.getWorkspace().getCurrentPageDescription();
238 }
239
Sunny Goyalbc683e92017-12-06 10:25:07 -0800240 public PageAlphaProvider getWorkspacePageAlphaProvider(Launcher launcher) {
241 if (this != NORMAL || !launcher.getDeviceProfile().shouldFadeAdjacentWorkspaceScreens()) {
242 return DEFAULT_ALPHA_PROVIDER;
243 }
Sunny Goyal228153d2018-01-04 15:35:22 -0800244 final int centerPage = launcher.getWorkspace().getNextPage();
Sunny Goyal0c723352017-12-12 12:02:29 -0800245 return new PageAlphaProvider(ACCEL_2) {
246 @Override
247 public float getPageAlpha(int pageIndex) {
248 return pageIndex != centerPage ? 0 : 1f;
249 }
250 };
Sunny Goyalbc683e92017-12-06 10:25:07 -0800251 }
252
Sunny Goyal49bcf342018-01-11 13:59:53 -0800253 public LauncherState getHistoryForState(LauncherState previousState) {
254 // No history is supported
255 return NORMAL;
256 }
257
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800258 /**
259 * Called when the start transition ends and the user settles on this particular state.
260 */
261 public void onStateTransitionEnd(Launcher launcher) {
Hyunyoung Song4bd47252018-06-05 16:26:32 -0700262 if (this == NORMAL || this == SPRING_LOADED) {
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800263 UiFactory.resetOverview(launcher);
Hyunyoung Song4bd47252018-06-05 16:26:32 -0700264 }
265 if (this == NORMAL) {
Sunny Goyal623eddd2018-03-02 12:24:41 -0800266 // Clear any rotation locks when going to normal state
267 launcher.getRotationHelper().setCurrentStateRequest(REQUEST_NONE);
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800268 }
269 }
270
Sunny Goyale39690b2018-08-02 13:35:07 -0700271 public void onBackPressed(Launcher launcher) {
272 if (this != NORMAL) {
273 LauncherStateManager lsm = launcher.getStateManager();
274 LauncherState lastState = lsm.getLastState();
275 launcher.getUserEventDispatcher().logActionCommand(Action.Command.BACK,
276 containerType, lastState.containerType);
277 lsm.goToState(lastState);
278 }
279 }
280
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700281 protected static void dispatchWindowStateChanged(Launcher launcher) {
282 launcher.getWindow().getDecorView().sendAccessibilityEvent(TYPE_WINDOW_STATE_CHANGED);
283 }
Sunny Goyalbc683e92017-12-06 10:25:07 -0800284
Sunny Goyal0c723352017-12-12 12:02:29 -0800285 public static abstract class PageAlphaProvider {
Sunny Goyalbc683e92017-12-06 10:25:07 -0800286
Sunny Goyal0c723352017-12-12 12:02:29 -0800287 public final Interpolator interpolator;
288
289 public PageAlphaProvider(Interpolator interpolator) {
290 this.interpolator = interpolator;
291 }
292
293 public abstract float getPageAlpha(int pageIndex);
Sunny Goyalbc683e92017-12-06 10:25:07 -0800294 }
Tonybc234402019-03-19 13:30:25 -0500295
296 public static class ScaleAndTranslation {
297 public float scale;
298 public float translationX;
299 public float translationY;
300
301 public ScaleAndTranslation(float scale, float translationX, float translationY) {
302 this.scale = scale;
303 this.translationX = translationX;
304 this.translationY = translationY;
305 }
306 }
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700307}