blob: dcfd272b474dfe3b73491ed21d0746ad748c7932 [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;
Tony Wickham59c69012019-06-18 16:34:37 -070020import static android.view.View.VISIBLE;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070021import static android.view.accessibility.AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
Tony Wickham59c69012019-06-18 16:34:37 -070022
23import static com.android.launcher3.anim.AnimatorSetBuilder.ANIM_OVERVIEW_FADE;
24import static com.android.launcher3.anim.AnimatorSetBuilder.ANIM_OVERVIEW_SCALE;
25import static com.android.launcher3.anim.AnimatorSetBuilder.ANIM_OVERVIEW_TRANSLATE_X;
26import static com.android.launcher3.anim.AnimatorSetBuilder.ANIM_WORKSPACE_FADE;
27import static com.android.launcher3.anim.AnimatorSetBuilder.ANIM_WORKSPACE_SCALE;
28import static com.android.launcher3.anim.Interpolators.ACCEL;
29import static com.android.launcher3.anim.Interpolators.DEACCEL;
30import static com.android.launcher3.anim.Interpolators.DEACCEL_1_7;
31import static com.android.launcher3.anim.Interpolators.clampToProgress;
Sunny Goyalab3963d2019-05-23 00:50:08 -070032import static com.android.launcher3.testing.TestProtocol.ALL_APPS_STATE_ORDINAL;
33import static com.android.launcher3.testing.TestProtocol.BACKGROUND_APP_STATE_ORDINAL;
34import static com.android.launcher3.testing.TestProtocol.NORMAL_STATE_ORDINAL;
35import static com.android.launcher3.testing.TestProtocol.OVERVIEW_PEEK_STATE_ORDINAL;
36import static com.android.launcher3.testing.TestProtocol.OVERVIEW_STATE_ORDINAL;
37import static com.android.launcher3.testing.TestProtocol.QUICK_SWITCH_STATE_ORDINAL;
38import static com.android.launcher3.testing.TestProtocol.SPRING_LOADED_STATE_ORDINAL;
Sunny Goyal0c723352017-12-12 12:02:29 -080039import static com.android.launcher3.anim.Interpolators.ACCEL_2;
Sunny Goyal623eddd2018-03-02 12:24:41 -080040import static com.android.launcher3.states.RotationHelper.REQUEST_NONE;
Sunny Goyal0c723352017-12-12 12:02:29 -080041
Sunny Goyal0c723352017-12-12 12:02:29 -080042import android.view.animation.Interpolator;
Sunny Goyalbe93f262017-10-20 17:05:27 -070043
Tony Wickham59c69012019-06-18 16:34:37 -070044import com.android.launcher3.anim.AnimatorSetBuilder;
Sunny Goyalc99cb172017-10-19 16:15:09 -070045import com.android.launcher3.states.SpringLoadedState;
Tonye4c2e2b2019-03-25 10:23:39 -050046import com.android.launcher3.uioverrides.UiFactory;
Tonye06fef42019-03-22 15:44:28 -050047import com.android.launcher3.uioverrides.states.AllAppsState;
48import com.android.launcher3.uioverrides.states.OverviewState;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070049import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
50
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070051import java.util.Arrays;
52
Sunny Goyal4c7f2152017-10-17 17:17:16 -070053
54/**
Sunny Goyalbe93f262017-10-20 17:05:27 -070055 * Base state for various states used for the Launcher
Sunny Goyal4c7f2152017-10-17 17:17:16 -070056 */
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070057public class LauncherState {
Sunny Goyal4c7f2152017-10-17 17:17:16 -070058
Sunny Goyal7185dd62018-03-14 17:51:49 -070059
60 /**
61 * Set of elements indicating various workspace elements which change visibility across states
62 * Note that workspace is not included here as in that case, we animate individual pages
63 */
64 public static final int NONE = 0;
Tony Wickhambd6f05e2018-03-21 08:16:33 -070065 public static final int HOTSEAT_ICONS = 1 << 0;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -070066 public static final int HOTSEAT_SEARCH_BOX = 1 << 1;
Tony Wickhambd6f05e2018-03-21 08:16:33 -070067 public static final int ALL_APPS_HEADER = 1 << 2;
68 public static final int ALL_APPS_HEADER_EXTRA = 1 << 3; // e.g. app predictions
69 public static final int ALL_APPS_CONTENT = 1 << 4;
Sunny Goyal56e10192018-05-22 13:22:25 -070070 public static final int VERTICAL_SWIPE_INDICATOR = 1 << 5;
Tony Wickham66d1c2f2019-04-24 11:32:59 -070071 public static final int RECENTS_CLEAR_ALL_BUTTON = 1 << 6;
Sunny Goyal7185dd62018-03-14 17:51:49 -070072
Sunny Goyal927447e2018-05-04 13:19:29 -070073 protected static final int FLAG_MULTI_PAGE = 1 << 0;
74 protected static final int FLAG_DISABLE_ACCESSIBILITY = 1 << 1;
75 protected static final int FLAG_DISABLE_RESTORE = 1 << 2;
76 protected static final int FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED = 1 << 3;
77 protected static final int FLAG_DISABLE_PAGE_CLIPPING = 1 << 4;
78 protected static final int FLAG_PAGE_BACKGROUNDS = 1 << 5;
79 protected static final int FLAG_DISABLE_INTERACTION = 1 << 6;
80 protected static final int FLAG_OVERVIEW_UI = 1 << 7;
81 protected static final int FLAG_HIDE_BACK_BUTTON = 1 << 8;
Sunny Goyal5d1873a2018-05-08 11:10:44 -070082 protected static final int FLAG_HAS_SYS_UI_SCRIM = 1 << 9;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -080083
Sunny Goyal0c723352017-12-12 12:02:29 -080084 protected static final PageAlphaProvider DEFAULT_ALPHA_PROVIDER =
85 new PageAlphaProvider(ACCEL_2) {
86 @Override
87 public float getPageAlpha(int pageIndex) {
88 return 1;
89 }
90 };
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070091
Tonye4c2e2b2019-03-25 10:23:39 -050092 private static final LauncherState[] sAllStates = new LauncherState[7];
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070093
Sunny Goyal75c59ac2018-01-15 14:23:11 -080094 /**
95 * TODO: Create a separate class for NORMAL state.
96 */
vadimt6895e402019-02-15 16:42:14 -080097 public static final LauncherState NORMAL = new LauncherState(NORMAL_STATE_ORDINAL,
98 ContainerType.WORKSPACE, 0,
Sunny Goyal5d1873a2018-05-08 11:10:44 -070099 FLAG_DISABLE_RESTORE | FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED | FLAG_HIDE_BACK_BUTTON |
100 FLAG_HAS_SYS_UI_SCRIM);
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700101
Sunny Goyal7185dd62018-03-14 17:51:49 -0700102 /**
103 * Various Launcher states arranged in the increasing order of UI layers
104 */
vadimt2a648aa2019-02-14 17:47:11 -0800105 public static final LauncherState SPRING_LOADED = new SpringLoadedState(
106 SPRING_LOADED_STATE_ORDINAL);
vadimt2a648aa2019-02-14 17:47:11 -0800107 public static final LauncherState ALL_APPS = new AllAppsState(ALL_APPS_STATE_ORDINAL);
Tony1787ee92019-03-20 12:38:35 -0500108
109 public static final LauncherState OVERVIEW = new OverviewState(OVERVIEW_STATE_ORDINAL);
110 public static final LauncherState OVERVIEW_PEEK =
111 OverviewState.newPeekState(OVERVIEW_PEEK_STATE_ORDINAL);
Tonye4c2e2b2019-03-25 10:23:39 -0500112 public static final LauncherState QUICK_SWITCH =
113 OverviewState.newSwitchState(QUICK_SWITCH_STATE_ORDINAL);
Tony1787ee92019-03-20 12:38:35 -0500114 public static final LauncherState BACKGROUND_APP =
115 OverviewState.newBackgroundState(BACKGROUND_APP_STATE_ORDINAL);
Tony Wickham6becf7c2018-04-19 11:39:34 -0700116
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700117 public final int ordinal;
118
Sunny Goyalf9403d92017-10-18 10:55:56 -0700119 /**
120 * Used for containerType in {@link com.android.launcher3.logging.UserEventDispatcher}
121 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700122 public final int containerType;
123
Sunny Goyalf9403d92017-10-18 10:55:56 -0700124 /**
125 * True if the state can be persisted across activity restarts.
126 */
Sunny Goyalbc683e92017-12-06 10:25:07 -0800127 public final boolean disableRestore;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700128
129 /**
130 * True if workspace has multiple pages visible.
131 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700132 public final boolean hasMultipleVisiblePages;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700133
134 /**
135 * Accessibility flag for workspace and its pages.
136 * @see android.view.View#setImportantForAccessibility(int)
137 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700138 public final int workspaceAccessibilityFlag;
139
Sunny Goyalbe93f262017-10-20 17:05:27 -0700140 /**
141 * Properties related to state transition animation
142 *
143 * @see WorkspaceStateTransitionAnimation
144 */
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800145 public final boolean hasWorkspacePageBackground;
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800146
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700147 public final int transitionDuration;
148
Sunny Goyalbe93f262017-10-20 17:05:27 -0700149 /**
Tony Wickham53370672017-12-05 10:10:55 -0800150 * True if the state allows workspace icons to be dragged.
151 */
152 public final boolean workspaceIconsCanBeDragged;
153
Sunny Goyalbc683e92017-12-06 10:25:07 -0800154 /**
155 * True if the workspace pages should not be clipped relative to the workspace bounds
156 * for this state.
157 */
158 public final boolean disablePageClipping;
159
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800160 /**
161 * True if launcher can not be directly interacted in this state;
162 */
163 public final boolean disableInteraction;
164
165 /**
166 * True if the state has overview panel visible.
167 */
168 public final boolean overviewUi;
169
Tony Wickham76cf2362018-03-20 14:13:16 -0700170 /**
171 * True if the back button should be hidden when in this state (assuming no floating views are
172 * open, launcher has window focus, etc).
173 */
174 public final boolean hideBackButton;
175
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700176 public final boolean hasSysUiScrim;
177
Sunny Goyal228153d2018-01-04 15:35:22 -0800178 public LauncherState(int id, int containerType, int transitionDuration, int flags) {
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700179 this.containerType = containerType;
180 this.transitionDuration = transitionDuration;
181
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800182 this.hasWorkspacePageBackground = (flags & FLAG_PAGE_BACKGROUNDS) != 0;
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700183 this.hasMultipleVisiblePages = (flags & FLAG_MULTI_PAGE) != 0;
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700184 this.workspaceAccessibilityFlag = (flags & FLAG_DISABLE_ACCESSIBILITY) != 0
185 ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
186 : IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Sunny Goyalbc683e92017-12-06 10:25:07 -0800187 this.disableRestore = (flags & FLAG_DISABLE_RESTORE) != 0;
Tony Wickham53370672017-12-05 10:10:55 -0800188 this.workspaceIconsCanBeDragged = (flags & FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED) != 0;
Sunny Goyalbc683e92017-12-06 10:25:07 -0800189 this.disablePageClipping = (flags & FLAG_DISABLE_PAGE_CLIPPING) != 0;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800190 this.disableInteraction = (flags & FLAG_DISABLE_INTERACTION) != 0;
191 this.overviewUi = (flags & FLAG_OVERVIEW_UI) != 0;
Tony Wickham76cf2362018-03-20 14:13:16 -0700192 this.hideBackButton = (flags & FLAG_HIDE_BACK_BUTTON) != 0;
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700193 this.hasSysUiScrim = (flags & FLAG_HAS_SYS_UI_SCRIM) != 0;
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700194
195 this.ordinal = id;
196 sAllStates[id] = this;
197 }
198
199 public static LauncherState[] values() {
200 return Arrays.copyOf(sAllStates, sAllStates.length);
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700201 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700202
Tonybc234402019-03-19 13:30:25 -0500203 public ScaleAndTranslation getWorkspaceScaleAndTranslation(Launcher launcher) {
204 return new ScaleAndTranslation(1, 0, 0);
Sunny Goyalc99cb172017-10-19 16:15:09 -0700205 }
206
Tonybc234402019-03-19 13:30:25 -0500207 public ScaleAndTranslation getHotseatScaleAndTranslation(Launcher launcher) {
Tony Wickham57e489c2019-03-04 14:57:07 -0800208 // For most states, treat the hotseat as if it were part of the workspace.
209 return getWorkspaceScaleAndTranslation(launcher);
Tony4bb4b892019-03-03 15:32:12 -0800210 }
211
Tonybc234402019-03-19 13:30:25 -0500212 public ScaleAndTranslation getOverviewScaleAndTranslation(Launcher launcher) {
Sunny Goyal9e84e0c2019-04-03 16:44:32 -0700213 return UiFactory.getOverviewScaleAndTranslationForNormalState(launcher);
Tony3bb5e8e2018-03-14 12:05:00 +0000214 }
215
Tony54068172019-05-12 13:24:51 -0500216 public float getOverviewFullscreenProgress() {
217 return 0;
218 }
219
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700220 public void onStateEnabled(Launcher launcher) {
221 dispatchWindowStateChanged(launcher);
222 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700223
224 public void onStateDisabled(Launcher launcher) { }
Sunny Goyalbe93f262017-10-20 17:05:27 -0700225
Sunny Goyal7185dd62018-03-14 17:51:49 -0700226 public int getVisibleElements(Launcher launcher) {
Tony Wickhambd6f05e2018-03-21 08:16:33 -0700227 if (launcher.getDeviceProfile().isVerticalBarLayout()) {
Sunny Goyal56e10192018-05-22 13:22:25 -0700228 return HOTSEAT_ICONS | VERTICAL_SWIPE_INDICATOR;
Tony Wickhambd6f05e2018-03-21 08:16:33 -0700229 }
Sunny Goyal56e10192018-05-22 13:22:25 -0700230 return HOTSEAT_ICONS | HOTSEAT_SEARCH_BOX | VERTICAL_SWIPE_INDICATOR;
Sunny Goyal7185dd62018-03-14 17:51:49 -0700231 }
232
Sunny Goyal228153d2018-01-04 15:35:22 -0800233 /**
234 * Fraction shift in the vertical translation UI and related properties
235 *
236 * @see com.android.launcher3.allapps.AllAppsTransitionController
237 */
238 public float getVerticalProgress(Launcher launcher) {
239 return 1f;
240 }
241
Sunny Goyal927447e2018-05-04 13:19:29 -0700242 public float getWorkspaceScrimAlpha(Launcher launcher) {
243 return 0;
244 }
245
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700246 public String getDescription(Launcher launcher) {
247 return launcher.getWorkspace().getCurrentPageDescription();
248 }
249
Sunny Goyalbc683e92017-12-06 10:25:07 -0800250 public PageAlphaProvider getWorkspacePageAlphaProvider(Launcher launcher) {
251 if (this != NORMAL || !launcher.getDeviceProfile().shouldFadeAdjacentWorkspaceScreens()) {
252 return DEFAULT_ALPHA_PROVIDER;
253 }
Sunny Goyal228153d2018-01-04 15:35:22 -0800254 final int centerPage = launcher.getWorkspace().getNextPage();
Sunny Goyal0c723352017-12-12 12:02:29 -0800255 return new PageAlphaProvider(ACCEL_2) {
256 @Override
257 public float getPageAlpha(int pageIndex) {
258 return pageIndex != centerPage ? 0 : 1f;
259 }
260 };
Sunny Goyalbc683e92017-12-06 10:25:07 -0800261 }
262
Sunny Goyal49bcf342018-01-11 13:59:53 -0800263 public LauncherState getHistoryForState(LauncherState previousState) {
264 // No history is supported
265 return NORMAL;
266 }
267
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800268 /**
269 * Called when the start transition ends and the user settles on this particular state.
270 */
271 public void onStateTransitionEnd(Launcher launcher) {
Hyunyoung Song4bd47252018-06-05 16:26:32 -0700272 if (this == NORMAL) {
Sunny Goyal623eddd2018-03-02 12:24:41 -0800273 // Clear any rotation locks when going to normal state
274 launcher.getRotationHelper().setCurrentStateRequest(REQUEST_NONE);
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800275 }
276 }
277
Sunny Goyale39690b2018-08-02 13:35:07 -0700278 public void onBackPressed(Launcher launcher) {
279 if (this != NORMAL) {
280 LauncherStateManager lsm = launcher.getStateManager();
281 LauncherState lastState = lsm.getLastState();
Sunny Goyale39690b2018-08-02 13:35:07 -0700282 lsm.goToState(lastState);
283 }
284 }
285
Tony Wickham59c69012019-06-18 16:34:37 -0700286 /**
287 * Prepares for a non-user controlled animation from fromState to this state. Preparations
288 * include:
289 * - Setting interpolators for various animations included in the state transition.
290 * - Setting some start values (e.g. scale) for views that are hidden but about to be shown.
291 */
292 public void prepareForAtomicAnimation(Launcher launcher, LauncherState fromState,
293 AnimatorSetBuilder builder) {
294 if (this == NORMAL && fromState == OVERVIEW) {
295 builder.setInterpolator(ANIM_WORKSPACE_SCALE, DEACCEL);
296 builder.setInterpolator(ANIM_WORKSPACE_FADE, ACCEL);
297 builder.setInterpolator(ANIM_OVERVIEW_SCALE, clampToProgress(ACCEL, 0, 0.9f));
298 builder.setInterpolator(ANIM_OVERVIEW_TRANSLATE_X, ACCEL);
299 builder.setInterpolator(ANIM_OVERVIEW_FADE, DEACCEL_1_7);
300 Workspace workspace = launcher.getWorkspace();
301
302 // Start from a higher workspace scale, but only if we're invisible so we don't jump.
303 boolean isWorkspaceVisible = workspace.getVisibility() == VISIBLE;
304 if (isWorkspaceVisible) {
305 CellLayout currentChild = (CellLayout) workspace.getChildAt(
306 workspace.getCurrentPage());
307 isWorkspaceVisible = currentChild.getVisibility() == VISIBLE
308 && currentChild.getShortcutsAndWidgets().getAlpha() > 0;
309 }
310 if (!isWorkspaceVisible) {
311 workspace.setScaleX(0.92f);
312 workspace.setScaleY(0.92f);
313 }
314 Hotseat hotseat = launcher.getHotseat();
315 boolean isHotseatVisible = hotseat.getVisibility() == VISIBLE && hotseat.getAlpha() > 0;
316 if (!isHotseatVisible) {
317 hotseat.setScaleX(0.92f);
318 hotseat.setScaleY(0.92f);
319 }
320 } else if (this == NORMAL && fromState == OVERVIEW_PEEK) {
321 // Keep fully visible until the very end (when overview is offscreen) to make invisible.
322 builder.setInterpolator(ANIM_OVERVIEW_FADE, t -> t < 1 ? 0 : 1);
323 }
324 }
325
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700326 protected static void dispatchWindowStateChanged(Launcher launcher) {
327 launcher.getWindow().getDecorView().sendAccessibilityEvent(TYPE_WINDOW_STATE_CHANGED);
328 }
Sunny Goyalbc683e92017-12-06 10:25:07 -0800329
Sunny Goyal0c723352017-12-12 12:02:29 -0800330 public static abstract class PageAlphaProvider {
Sunny Goyalbc683e92017-12-06 10:25:07 -0800331
Sunny Goyal0c723352017-12-12 12:02:29 -0800332 public final Interpolator interpolator;
333
334 public PageAlphaProvider(Interpolator interpolator) {
335 this.interpolator = interpolator;
336 }
337
338 public abstract float getPageAlpha(int pageIndex);
Sunny Goyalbc683e92017-12-06 10:25:07 -0800339 }
Tonybc234402019-03-19 13:30:25 -0500340
341 public static class ScaleAndTranslation {
342 public float scale;
343 public float translationX;
344 public float translationY;
345
346 public ScaleAndTranslation(float scale, float translationX, float translationY) {
347 this.scale = scale;
348 this.translationX = translationX;
349 this.translationY = translationY;
350 }
351 }
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700352}