blob: ae75b5179993add554ceffc26fe5b337a948019b [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
Sunny Goyal210e1742019-10-17 12:05:38 -070018import static com.android.launcher3.anim.Interpolators.ACCEL_2;
Winson Chungf9935182020-10-23 09:26:44 -070019import static com.android.launcher3.logging.StatsLogManager.LAUNCHER_STATE_HOME;
Sunny Goyalab3963d2019-05-23 00:50:08 -070020import static com.android.launcher3.testing.TestProtocol.ALL_APPS_STATE_ORDINAL;
21import static com.android.launcher3.testing.TestProtocol.BACKGROUND_APP_STATE_ORDINAL;
Tony Wickham4fdba142019-11-04 16:23:51 -080022import static com.android.launcher3.testing.TestProtocol.HINT_STATE_ORDINAL;
Sunny Goyalab3963d2019-05-23 00:50:08 -070023import static com.android.launcher3.testing.TestProtocol.NORMAL_STATE_ORDINAL;
Zak Cohena39544d2020-04-27 16:26:55 -070024import static com.android.launcher3.testing.TestProtocol.OVERVIEW_MODAL_TASK_STATE_ORDINAL;
Vinit Nayakd987a822021-02-22 14:49:27 -080025import static com.android.launcher3.testing.TestProtocol.OVERVIEW_SPLIT_SELECT_ORDINAL;
Sunny Goyalab3963d2019-05-23 00:50:08 -070026import static com.android.launcher3.testing.TestProtocol.OVERVIEW_STATE_ORDINAL;
27import static com.android.launcher3.testing.TestProtocol.QUICK_SWITCH_STATE_ORDINAL;
28import static com.android.launcher3.testing.TestProtocol.SPRING_LOADED_STATE_ORDINAL;
Sunny Goyal0c723352017-12-12 12:02:29 -080029
Winson Chung8687bc22020-02-27 23:34:24 -080030import android.content.Context;
Sunny Goyal0c723352017-12-12 12:02:29 -080031import android.view.animation.Interpolator;
Sunny Goyalbe93f262017-10-20 17:05:27 -070032
Samuel Fufa2ea01e42020-12-02 13:48:18 -060033import com.android.launcher3.config.FeatureFlags;
Sunny Goyal35e7d382020-05-08 13:37:58 -070034import com.android.launcher3.statemanager.BaseState;
35import com.android.launcher3.statemanager.StateManager;
Tony Wickham4fdba142019-11-04 16:23:51 -080036import com.android.launcher3.states.HintState;
Sunny Goyalc99cb172017-10-19 16:15:09 -070037import com.android.launcher3.states.SpringLoadedState;
Tony Wickhamf844ee92020-05-27 15:41:32 -050038import com.android.launcher3.testing.TestProtocol;
Tonye06fef42019-03-22 15:44:28 -050039import com.android.launcher3.uioverrides.states.AllAppsState;
40import com.android.launcher3.uioverrides.states.OverviewState;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070041
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070042import java.util.Arrays;
43
Sunny Goyal4c7f2152017-10-17 17:17:16 -070044/**
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 Goyal35e7d382020-05-08 13:37:58 -070047public abstract class LauncherState implements BaseState<LauncherState> {
Sunny Goyal4c7f2152017-10-17 17:17:16 -070048
Sunny Goyal7185dd62018-03-14 17:51:49 -070049 /**
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;
Alex Chaub794ea62021-02-03 18:00:30 +000060 public static final int OVERVIEW_ACTIONS = 1 << 6;
Tony Wickhamd683d982020-12-23 16:12:18 -060061 public static final int TASKBAR = 1 << 7;
Alex Chaub794ea62021-02-03 18:00:30 +000062 public static final int CLEAR_ALL_BUTTON = 1 << 8;
Tony Wickhama29a0462021-03-02 12:28:25 -080063 public static final int WORKSPACE_PAGE_INDICATOR = 1 << 9;
Vinit Nayakd987a822021-02-22 14:49:27 -080064 public static final int SPLIT_PLACHOLDER_VIEW = 1 << 10;
Sunny Goyal7185dd62018-03-14 17:51:49 -070065
Zak Cohen43a16152020-02-25 14:29:37 -080066 /** Mask of all the items that are contained in the apps view. */
67 public static final int APPS_VIEW_ITEM_MASK =
68 HOTSEAT_SEARCH_BOX | ALL_APPS_HEADER | ALL_APPS_HEADER_EXTRA | ALL_APPS_CONTENT;
69
Sunny Goyal8f965462020-04-23 19:00:36 -070070 // Flag indicating workspace has multiple pages visible.
Sunny Goyal35e7d382020-05-08 13:37:58 -070071 public static final int FLAG_MULTI_PAGE = BaseState.getFlag(0);
Sunny Goyal8f965462020-04-23 19:00:36 -070072 // Flag indicating that workspace and its contents are not accessible
Sunny Goyal35e7d382020-05-08 13:37:58 -070073 public static final int FLAG_WORKSPACE_INACCESSIBLE = BaseState.getFlag(1);
Sunny Goyal8f965462020-04-23 19:00:36 -070074
Sunny Goyal8f965462020-04-23 19:00:36 -070075 // Flag indicating the state allows workspace icons to be dragged.
Sunny Goyal35e7d382020-05-08 13:37:58 -070076 public static final int FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED = BaseState.getFlag(2);
Sunny Goyal8f965462020-04-23 19:00:36 -070077 // Flag to indicate that workspace should draw page background
Sunny Goyal35e7d382020-05-08 13:37:58 -070078 public static final int FLAG_WORKSPACE_HAS_BACKGROUNDS = BaseState.getFlag(3);
Sunny Goyal8f965462020-04-23 19:00:36 -070079 // True if the back button should be hidden when in this state (assuming no floating views are
80 // open, launcher has window focus, etc).
Sunny Goyal35e7d382020-05-08 13:37:58 -070081 public static final int FLAG_HIDE_BACK_BUTTON = BaseState.getFlag(4);
Sunny Goyal8f965462020-04-23 19:00:36 -070082 // Flag to indicate if the state would have scrim over sysui region: statu sbar and nav bar
Sunny Goyal35e7d382020-05-08 13:37:58 -070083 public static final int FLAG_HAS_SYS_UI_SCRIM = BaseState.getFlag(5);
Sunny Goyal8f965462020-04-23 19:00:36 -070084 // Flag to inticate that all popups should be closed when this state is enabled.
Sunny Goyal35e7d382020-05-08 13:37:58 -070085 public static final int FLAG_CLOSE_POPUPS = BaseState.getFlag(6);
86 public static final int FLAG_OVERVIEW_UI = BaseState.getFlag(7);
Sunny Goyal8f965462020-04-23 19:00:36 -070087
Sunny Goyal6c6c2f42018-03-02 14:57:46 -080088
Sunny Goyalf2393f12020-04-01 20:13:12 -070089 public static final float NO_OFFSET = 0;
90 public static final float NO_SCALE = 1;
91
Sunny Goyal0c723352017-12-12 12:02:29 -080092 protected static final PageAlphaProvider DEFAULT_ALPHA_PROVIDER =
93 new PageAlphaProvider(ACCEL_2) {
94 @Override
95 public float getPageAlpha(int pageIndex) {
96 return 1;
97 }
98 };
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070099
Zak Cohena39544d2020-04-27 16:26:55 -0700100 private static final LauncherState[] sAllStates = new LauncherState[9];
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700101
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800102 /**
103 * TODO: Create a separate class for NORMAL state.
104 */
vadimt6895e402019-02-15 16:42:14 -0800105 public static final LauncherState NORMAL = new LauncherState(NORMAL_STATE_ORDINAL,
Winson Chungf9935182020-10-23 09:26:44 -0700106 LAUNCHER_STATE_HOME,
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700107 FLAG_DISABLE_RESTORE | FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED | FLAG_HIDE_BACK_BUTTON |
Tony Wickhamc7203ad2020-02-03 18:36:06 -0800108 FLAG_HAS_SYS_UI_SCRIM) {
109 @Override
Sunny Goyal8f965462020-04-23 19:00:36 -0700110 public int getTransitionDuration(Context context) {
Tony Wickhamc7203ad2020-02-03 18:36:06 -0800111 // Arbitrary duration, when going to NORMAL we use the state we're coming from instead.
112 return 0;
113 }
114 };
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700115
Sunny Goyal7185dd62018-03-14 17:51:49 -0700116 /**
117 * Various Launcher states arranged in the increasing order of UI layers
118 */
vadimt2a648aa2019-02-14 17:47:11 -0800119 public static final LauncherState SPRING_LOADED = new SpringLoadedState(
120 SPRING_LOADED_STATE_ORDINAL);
vadimt2a648aa2019-02-14 17:47:11 -0800121 public static final LauncherState ALL_APPS = new AllAppsState(ALL_APPS_STATE_ORDINAL);
Tony Wickham4fdba142019-11-04 16:23:51 -0800122 public static final LauncherState HINT_STATE = new HintState(HINT_STATE_ORDINAL);
Tony1787ee92019-03-20 12:38:35 -0500123
124 public static final LauncherState OVERVIEW = new OverviewState(OVERVIEW_STATE_ORDINAL);
Zak Cohena39544d2020-04-27 16:26:55 -0700125 public static final LauncherState OVERVIEW_MODAL_TASK = OverviewState.newModalTaskState(
126 OVERVIEW_MODAL_TASK_STATE_ORDINAL);
Tonye4c2e2b2019-03-25 10:23:39 -0500127 public static final LauncherState QUICK_SWITCH =
128 OverviewState.newSwitchState(QUICK_SWITCH_STATE_ORDINAL);
Tony1787ee92019-03-20 12:38:35 -0500129 public static final LauncherState BACKGROUND_APP =
130 OverviewState.newBackgroundState(BACKGROUND_APP_STATE_ORDINAL);
Vinit Nayakd987a822021-02-22 14:49:27 -0800131 public static final LauncherState OVERVIEW_SPLIT_SELECT =
132 OverviewState.newSplitSelectState(OVERVIEW_SPLIT_SELECT_ORDINAL);
Tony Wickham6becf7c2018-04-19 11:39:34 -0700133
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700134 public final int ordinal;
135
Sunny Goyalf9403d92017-10-18 10:55:56 -0700136 /**
Winson Chungf9935182020-10-23 09:26:44 -0700137 * Used for {@link com.android.launcher3.logging.StatsLogManager}
Sunny Goyalf9403d92017-10-18 10:55:56 -0700138 */
Winson Chungf9935182020-10-23 09:26:44 -0700139 public final int statsLogOrdinal;
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700140
Sunny Goyalf9403d92017-10-18 10:55:56 -0700141 /**
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800142 * True if the state has overview panel visible.
143 */
144 public final boolean overviewUi;
145
Sunny Goyal8f965462020-04-23 19:00:36 -0700146 private final int mFlags;
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700147
Winson Chungf9935182020-10-23 09:26:44 -0700148 public LauncherState(int id, int statsLogOrdinal, int flags) {
149 this.statsLogOrdinal = statsLogOrdinal;
Sunny Goyal8f965462020-04-23 19:00:36 -0700150 this.mFlags = flags;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800151 this.overviewUi = (flags & FLAG_OVERVIEW_UI) != 0;
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700152 this.ordinal = id;
153 sAllStates[id] = this;
154 }
155
Sunny Goyal8f965462020-04-23 19:00:36 -0700156 /**
157 * Returns if the state has the provided flag
158 */
Sunny Goyal35e7d382020-05-08 13:37:58 -0700159 @Override
Sunny Goyal8f965462020-04-23 19:00:36 -0700160 public final boolean hasFlag(int mask) {
161 return (mFlags & mask) != 0;
162 }
163
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700164 public static LauncherState[] values() {
165 return Arrays.copyOf(sAllStates, sAllStates.length);
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700166 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700167
Tonybc234402019-03-19 13:30:25 -0500168 public ScaleAndTranslation getWorkspaceScaleAndTranslation(Launcher launcher) {
Sunny Goyalf2393f12020-04-01 20:13:12 -0700169 return new ScaleAndTranslation(NO_SCALE, NO_OFFSET, NO_OFFSET);
Sunny Goyalc99cb172017-10-19 16:15:09 -0700170 }
171
Tonybc234402019-03-19 13:30:25 -0500172 public ScaleAndTranslation getHotseatScaleAndTranslation(Launcher launcher) {
Tony Wickham57e489c2019-03-04 14:57:07 -0800173 // For most states, treat the hotseat as if it were part of the workspace.
174 return getWorkspaceScaleAndTranslation(launcher);
Tony4bb4b892019-03-03 15:32:12 -0800175 }
176
Sunny Goyalf2393f12020-04-01 20:13:12 -0700177 /**
178 * Returns an array of two elements.
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600179 * The first specifies the scale for the overview
180 * The second is the factor ([0, 1], 0 => center-screen; 1 => offscreen) by which overview
181 * should be shifted horizontally.
Sunny Goyalf2393f12020-04-01 20:13:12 -0700182 */
183 public float[] getOverviewScaleAndOffset(Launcher launcher) {
184 return launcher.getNormalOverviewScaleAndOffset();
Tony3bb5e8e2018-03-14 12:05:00 +0000185 }
186
Tony Wickham4fdba142019-11-04 16:23:51 -0800187 public ScaleAndTranslation getQsbScaleAndTranslation(Launcher launcher) {
Sunny Goyalf2393f12020-04-01 20:13:12 -0700188 return new ScaleAndTranslation(NO_SCALE, NO_OFFSET, NO_OFFSET);
Tony Wickham4fdba142019-11-04 16:23:51 -0800189 }
190
Tony54068172019-05-12 13:24:51 -0500191 public float getOverviewFullscreenProgress() {
192 return 0;
193 }
194
Sunny Goyal7185dd62018-03-14 17:51:49 -0700195 public int getVisibleElements(Launcher launcher) {
Tony Wickhama29a0462021-03-02 12:28:25 -0800196 DeviceProfile deviceProfile = launcher.getDeviceProfile();
197 int flags = WORKSPACE_PAGE_INDICATOR | VERTICAL_SWIPE_INDICATOR | TASKBAR;
198 if (!FeatureFlags.ENABLE_DEVICE_SEARCH.get() && !deviceProfile.isVerticalBarLayout()) {
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600199 flags |= HOTSEAT_SEARCH_BOX;
Tony Wickhambd6f05e2018-03-21 08:16:33 -0700200 }
Tony Wickhama29a0462021-03-02 12:28:25 -0800201 if (!deviceProfile.isTaskbarPresent) {
202 flags |= HOTSEAT_ICONS;
203 }
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600204 return flags;
Sunny Goyal7185dd62018-03-14 17:51:49 -0700205 }
206
Sunny Goyal228153d2018-01-04 15:35:22 -0800207 /**
Tony Wickhama29a0462021-03-02 12:28:25 -0800208 * A shorthand for checking getVisibleElements() & elements == elements.
209 * @return Whether all of the given elements are visible.
210 */
211 public boolean areElementsVisible(Launcher launcher, int elements) {
212 return (getVisibleElements(launcher) & elements) == elements;
213 }
214
215 /**
Sunny Goyal228153d2018-01-04 15:35:22 -0800216 * Fraction shift in the vertical translation UI and related properties
217 *
218 * @see com.android.launcher3.allapps.AllAppsTransitionController
219 */
220 public float getVerticalProgress(Launcher launcher) {
221 return 1f;
222 }
223
Sunny Goyal927447e2018-05-04 13:19:29 -0700224 public float getWorkspaceScrimAlpha(Launcher launcher) {
225 return 0;
226 }
227
Tony Wickhamc0880512019-07-24 17:32:08 -0700228 public float getOverviewScrimAlpha(Launcher launcher) {
229 return 0;
230 }
231
Winson Chung8687bc22020-02-27 23:34:24 -0800232 /**
Zak Cohena39544d2020-04-27 16:26:55 -0700233 * For this state, how modal should over view been shown. 0 modalness means all tasks drawn,
234 * 1 modalness means the current task is show on its own.
235 */
236 public float getOverviewModalness() {
237 return 0;
238 }
239
240 /**
Vinit Nayakd987a822021-02-22 14:49:27 -0800241 * For this state, how much additional vertical translation there should be for each of the
242 * child TaskViews.
243 */
244 public float getOverviewSecondaryTranslation(Launcher launcher) {
245 return 0;
246 }
247
248 /**
Lucas Dupin4918ed32020-03-17 17:11:32 -0700249 * The amount of blur and wallpaper zoom to apply to the background of either the app
250 * or Launcher surface in this state. Should be a number between 0 and 1, inclusive.
251 *
252 * 0 means completely zoomed in, without blurs. 1 is zoomed out, with blurs.
Winson Chung8687bc22020-02-27 23:34:24 -0800253 */
Winson Chungb330d6e2020-04-20 11:57:11 -0700254 public final float getDepth(Context context) {
Jon Miranda36d10152020-06-19 16:35:08 -0700255 return getDepth(context,
256 BaseDraggingActivity.fromContext(context).getDeviceProfile().isMultiWindowMode);
257 }
258
259 /**
260 * Returns the amount of blur and wallpaper zoom for this state with {@param isMultiWindowMode}.
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600261 *
Jon Miranda36d10152020-06-19 16:35:08 -0700262 * @see #getDepth(Context).
263 */
264 public final float getDepth(Context context, boolean isMultiWindowMode) {
265 if (isMultiWindowMode) {
Winson Chungb330d6e2020-04-20 11:57:11 -0700266 return 0;
267 }
268 return getDepthUnchecked(context);
269 }
270
271 protected float getDepthUnchecked(Context context) {
Lucas Dupin4918ed32020-03-17 17:11:32 -0700272 return 0f;
Winson Chung8687bc22020-02-27 23:34:24 -0800273 }
274
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700275 public String getDescription(Launcher launcher) {
276 return launcher.getWorkspace().getCurrentPageDescription();
277 }
278
Sunny Goyalbc683e92017-12-06 10:25:07 -0800279 public PageAlphaProvider getWorkspacePageAlphaProvider(Launcher launcher) {
Jon Mirandae3832222020-07-06 15:38:38 -0700280 if ((this != NORMAL && this != HINT_STATE)
281 || !launcher.getDeviceProfile().shouldFadeAdjacentWorkspaceScreens()) {
Sunny Goyalbc683e92017-12-06 10:25:07 -0800282 return DEFAULT_ALPHA_PROVIDER;
283 }
Sunny Goyal228153d2018-01-04 15:35:22 -0800284 final int centerPage = launcher.getWorkspace().getNextPage();
Sunny Goyal0c723352017-12-12 12:02:29 -0800285 return new PageAlphaProvider(ACCEL_2) {
286 @Override
287 public float getPageAlpha(int pageIndex) {
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600288 return pageIndex != centerPage ? 0 : 1f;
Sunny Goyal0c723352017-12-12 12:02:29 -0800289 }
290 };
Sunny Goyalbc683e92017-12-06 10:25:07 -0800291 }
292
Sunny Goyal35e7d382020-05-08 13:37:58 -0700293 @Override
Sunny Goyal49bcf342018-01-11 13:59:53 -0800294 public LauncherState getHistoryForState(LauncherState previousState) {
295 // No history is supported
296 return NORMAL;
297 }
298
Sunny Goyal35e7d382020-05-08 13:37:58 -0700299 @Override
300 public String toString() {
Tony Wickhamf844ee92020-05-27 15:41:32 -0500301 return TestProtocol.stateOrdinalToString(ordinal);
Sunny Goyal35e7d382020-05-08 13:37:58 -0700302 }
303
Sunny Goyale39690b2018-08-02 13:35:07 -0700304 public void onBackPressed(Launcher launcher) {
305 if (this != NORMAL) {
Sunny Goyal35e7d382020-05-08 13:37:58 -0700306 StateManager<LauncherState> lsm = launcher.getStateManager();
Sunny Goyale39690b2018-08-02 13:35:07 -0700307 LauncherState lastState = lsm.getLastState();
Sunny Goyale39690b2018-08-02 13:35:07 -0700308 lsm.goToState(lastState);
309 }
310 }
311
Sunny Goyal0c723352017-12-12 12:02:29 -0800312 public static abstract class PageAlphaProvider {
Sunny Goyalbc683e92017-12-06 10:25:07 -0800313
Sunny Goyal0c723352017-12-12 12:02:29 -0800314 public final Interpolator interpolator;
315
316 public PageAlphaProvider(Interpolator interpolator) {
317 this.interpolator = interpolator;
318 }
319
320 public abstract float getPageAlpha(int pageIndex);
Sunny Goyalbc683e92017-12-06 10:25:07 -0800321 }
Tonybc234402019-03-19 13:30:25 -0500322
323 public static class ScaleAndTranslation {
324 public float scale;
325 public float translationX;
326 public float translationY;
327
328 public ScaleAndTranslation(float scale, float translationX, float translationY) {
329 this.scale = scale;
330 this.translationX = translationX;
331 this.translationY = translationY;
332 }
333 }
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700334}