blob: 21c40efe30eee9b0fb0f3ccbe49b212fc33ef549 [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;
Sunny Goyalab3963d2019-05-23 00:50:08 -070025import static com.android.launcher3.testing.TestProtocol.OVERVIEW_STATE_ORDINAL;
26import static com.android.launcher3.testing.TestProtocol.QUICK_SWITCH_STATE_ORDINAL;
27import static com.android.launcher3.testing.TestProtocol.SPRING_LOADED_STATE_ORDINAL;
Sunny Goyal0c723352017-12-12 12:02:29 -080028
Winson Chung8687bc22020-02-27 23:34:24 -080029import android.content.Context;
Sunny Goyal0c723352017-12-12 12:02:29 -080030import android.view.animation.Interpolator;
Sunny Goyalbe93f262017-10-20 17:05:27 -070031
Samuel Fufa2ea01e42020-12-02 13:48:18 -060032import com.android.launcher3.config.FeatureFlags;
Sunny Goyal35e7d382020-05-08 13:37:58 -070033import com.android.launcher3.statemanager.BaseState;
34import com.android.launcher3.statemanager.StateManager;
Tony Wickham4fdba142019-11-04 16:23:51 -080035import com.android.launcher3.states.HintState;
Sunny Goyalc99cb172017-10-19 16:15:09 -070036import com.android.launcher3.states.SpringLoadedState;
Tony Wickhamf844ee92020-05-27 15:41:32 -050037import com.android.launcher3.testing.TestProtocol;
Tonye06fef42019-03-22 15:44:28 -050038import com.android.launcher3.uioverrides.states.AllAppsState;
39import com.android.launcher3.uioverrides.states.OverviewState;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070040
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070041import java.util.Arrays;
42
Sunny Goyal4c7f2152017-10-17 17:17:16 -070043/**
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 Goyal35e7d382020-05-08 13:37:58 -070046public abstract class LauncherState implements BaseState<LauncherState> {
Sunny Goyal4c7f2152017-10-17 17:17:16 -070047
Sunny Goyal7185dd62018-03-14 17:51:49 -070048 /**
49 * Set of elements indicating various workspace elements which change visibility across states
50 * Note that workspace is not included here as in that case, we animate individual pages
51 */
52 public static final int NONE = 0;
Tony Wickhambd6f05e2018-03-21 08:16:33 -070053 public static final int HOTSEAT_ICONS = 1 << 0;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -070054 public static final int HOTSEAT_SEARCH_BOX = 1 << 1;
Tony Wickhambd6f05e2018-03-21 08:16:33 -070055 public static final int ALL_APPS_HEADER = 1 << 2;
56 public static final int ALL_APPS_HEADER_EXTRA = 1 << 3; // e.g. app predictions
57 public static final int ALL_APPS_CONTENT = 1 << 4;
Sunny Goyal56e10192018-05-22 13:22:25 -070058 public static final int VERTICAL_SWIPE_INDICATOR = 1 << 5;
Alex Chaub794ea62021-02-03 18:00:30 +000059 public static final int OVERVIEW_ACTIONS = 1 << 6;
Tony Wickhamd683d982020-12-23 16:12:18 -060060 public static final int TASKBAR = 1 << 7;
Alex Chaub794ea62021-02-03 18:00:30 +000061 public static final int CLEAR_ALL_BUTTON = 1 << 8;
Tony Wickhama29a0462021-03-02 12:28:25 -080062 public static final int WORKSPACE_PAGE_INDICATOR = 1 << 9;
Sunny Goyal7185dd62018-03-14 17:51:49 -070063
Zak Cohen43a16152020-02-25 14:29:37 -080064 /** Mask of all the items that are contained in the apps view. */
65 public static final int APPS_VIEW_ITEM_MASK =
66 HOTSEAT_SEARCH_BOX | ALL_APPS_HEADER | ALL_APPS_HEADER_EXTRA | ALL_APPS_CONTENT;
67
Sunny Goyal8f965462020-04-23 19:00:36 -070068 // Flag indicating workspace has multiple pages visible.
Sunny Goyal35e7d382020-05-08 13:37:58 -070069 public static final int FLAG_MULTI_PAGE = BaseState.getFlag(0);
Sunny Goyal8f965462020-04-23 19:00:36 -070070 // Flag indicating that workspace and its contents are not accessible
Sunny Goyal35e7d382020-05-08 13:37:58 -070071 public static final int FLAG_WORKSPACE_INACCESSIBLE = BaseState.getFlag(1);
Sunny Goyal8f965462020-04-23 19:00:36 -070072
Sunny Goyal8f965462020-04-23 19:00:36 -070073 // Flag indicating the state allows workspace icons to be dragged.
Sunny Goyal35e7d382020-05-08 13:37:58 -070074 public static final int FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED = BaseState.getFlag(2);
Sunny Goyal8f965462020-04-23 19:00:36 -070075 // Flag to indicate that workspace should draw page background
Sunny Goyal35e7d382020-05-08 13:37:58 -070076 public static final int FLAG_WORKSPACE_HAS_BACKGROUNDS = BaseState.getFlag(3);
Sunny Goyal8f965462020-04-23 19:00:36 -070077 // True if the back button should be hidden when in this state (assuming no floating views are
78 // open, launcher has window focus, etc).
Sunny Goyal35e7d382020-05-08 13:37:58 -070079 public static final int FLAG_HIDE_BACK_BUTTON = BaseState.getFlag(4);
Sunny Goyal8f965462020-04-23 19:00:36 -070080 // Flag to indicate if the state would have scrim over sysui region: statu sbar and nav bar
Sunny Goyal35e7d382020-05-08 13:37:58 -070081 public static final int FLAG_HAS_SYS_UI_SCRIM = BaseState.getFlag(5);
Sunny Goyal8f965462020-04-23 19:00:36 -070082 // Flag to inticate that all popups should be closed when this state is enabled.
Sunny Goyal35e7d382020-05-08 13:37:58 -070083 public static final int FLAG_CLOSE_POPUPS = BaseState.getFlag(6);
84 public static final int FLAG_OVERVIEW_UI = BaseState.getFlag(7);
Sunny Goyal8f965462020-04-23 19:00:36 -070085
Sunny Goyal6c6c2f42018-03-02 14:57:46 -080086
Sunny Goyalf2393f12020-04-01 20:13:12 -070087 public static final float NO_OFFSET = 0;
88 public static final float NO_SCALE = 1;
89
Sunny Goyal0c723352017-12-12 12:02:29 -080090 protected static final PageAlphaProvider DEFAULT_ALPHA_PROVIDER =
91 new PageAlphaProvider(ACCEL_2) {
92 @Override
93 public float getPageAlpha(int pageIndex) {
94 return 1;
95 }
96 };
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070097
Zak Cohena39544d2020-04-27 16:26:55 -070098 private static final LauncherState[] sAllStates = new LauncherState[9];
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070099
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800100 /**
101 * TODO: Create a separate class for NORMAL state.
102 */
vadimt6895e402019-02-15 16:42:14 -0800103 public static final LauncherState NORMAL = new LauncherState(NORMAL_STATE_ORDINAL,
Winson Chungf9935182020-10-23 09:26:44 -0700104 LAUNCHER_STATE_HOME,
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700105 FLAG_DISABLE_RESTORE | FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED | FLAG_HIDE_BACK_BUTTON |
Tony Wickhamc7203ad2020-02-03 18:36:06 -0800106 FLAG_HAS_SYS_UI_SCRIM) {
107 @Override
Sunny Goyal8f965462020-04-23 19:00:36 -0700108 public int getTransitionDuration(Context context) {
Tony Wickhamc7203ad2020-02-03 18:36:06 -0800109 // Arbitrary duration, when going to NORMAL we use the state we're coming from instead.
110 return 0;
111 }
112 };
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700113
Sunny Goyal7185dd62018-03-14 17:51:49 -0700114 /**
115 * Various Launcher states arranged in the increasing order of UI layers
116 */
vadimt2a648aa2019-02-14 17:47:11 -0800117 public static final LauncherState SPRING_LOADED = new SpringLoadedState(
118 SPRING_LOADED_STATE_ORDINAL);
vadimt2a648aa2019-02-14 17:47:11 -0800119 public static final LauncherState ALL_APPS = new AllAppsState(ALL_APPS_STATE_ORDINAL);
Tony Wickham4fdba142019-11-04 16:23:51 -0800120 public static final LauncherState HINT_STATE = new HintState(HINT_STATE_ORDINAL);
Tony1787ee92019-03-20 12:38:35 -0500121
122 public static final LauncherState OVERVIEW = new OverviewState(OVERVIEW_STATE_ORDINAL);
Zak Cohena39544d2020-04-27 16:26:55 -0700123 public static final LauncherState OVERVIEW_MODAL_TASK = OverviewState.newModalTaskState(
124 OVERVIEW_MODAL_TASK_STATE_ORDINAL);
Tonye4c2e2b2019-03-25 10:23:39 -0500125 public static final LauncherState QUICK_SWITCH =
126 OverviewState.newSwitchState(QUICK_SWITCH_STATE_ORDINAL);
Tony1787ee92019-03-20 12:38:35 -0500127 public static final LauncherState BACKGROUND_APP =
128 OverviewState.newBackgroundState(BACKGROUND_APP_STATE_ORDINAL);
Tony Wickham6becf7c2018-04-19 11:39:34 -0700129
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700130 public final int ordinal;
131
Sunny Goyalf9403d92017-10-18 10:55:56 -0700132 /**
Winson Chungf9935182020-10-23 09:26:44 -0700133 * Used for {@link com.android.launcher3.logging.StatsLogManager}
Sunny Goyalf9403d92017-10-18 10:55:56 -0700134 */
Winson Chungf9935182020-10-23 09:26:44 -0700135 public final int statsLogOrdinal;
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700136
Sunny Goyalf9403d92017-10-18 10:55:56 -0700137 /**
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800138 * True if the state has overview panel visible.
139 */
140 public final boolean overviewUi;
141
Sunny Goyal8f965462020-04-23 19:00:36 -0700142 private final int mFlags;
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700143
Winson Chungf9935182020-10-23 09:26:44 -0700144 public LauncherState(int id, int statsLogOrdinal, int flags) {
145 this.statsLogOrdinal = statsLogOrdinal;
Sunny Goyal8f965462020-04-23 19:00:36 -0700146 this.mFlags = flags;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800147 this.overviewUi = (flags & FLAG_OVERVIEW_UI) != 0;
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700148 this.ordinal = id;
149 sAllStates[id] = this;
150 }
151
Sunny Goyal8f965462020-04-23 19:00:36 -0700152 /**
153 * Returns if the state has the provided flag
154 */
Sunny Goyal35e7d382020-05-08 13:37:58 -0700155 @Override
Sunny Goyal8f965462020-04-23 19:00:36 -0700156 public final boolean hasFlag(int mask) {
157 return (mFlags & mask) != 0;
158 }
159
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700160 public static LauncherState[] values() {
161 return Arrays.copyOf(sAllStates, sAllStates.length);
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700162 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700163
Tonybc234402019-03-19 13:30:25 -0500164 public ScaleAndTranslation getWorkspaceScaleAndTranslation(Launcher launcher) {
Sunny Goyalf2393f12020-04-01 20:13:12 -0700165 return new ScaleAndTranslation(NO_SCALE, NO_OFFSET, NO_OFFSET);
Sunny Goyalc99cb172017-10-19 16:15:09 -0700166 }
167
Tonybc234402019-03-19 13:30:25 -0500168 public ScaleAndTranslation getHotseatScaleAndTranslation(Launcher launcher) {
Tony Wickham57e489c2019-03-04 14:57:07 -0800169 // For most states, treat the hotseat as if it were part of the workspace.
170 return getWorkspaceScaleAndTranslation(launcher);
Tony4bb4b892019-03-03 15:32:12 -0800171 }
172
Sunny Goyalf2393f12020-04-01 20:13:12 -0700173 /**
174 * Returns an array of two elements.
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600175 * The first specifies the scale for the overview
176 * The second is the factor ([0, 1], 0 => center-screen; 1 => offscreen) by which overview
177 * should be shifted horizontally.
Sunny Goyalf2393f12020-04-01 20:13:12 -0700178 */
179 public float[] getOverviewScaleAndOffset(Launcher launcher) {
180 return launcher.getNormalOverviewScaleAndOffset();
Tony3bb5e8e2018-03-14 12:05:00 +0000181 }
182
Tony Wickham4fdba142019-11-04 16:23:51 -0800183 public ScaleAndTranslation getQsbScaleAndTranslation(Launcher launcher) {
Sunny Goyalf2393f12020-04-01 20:13:12 -0700184 return new ScaleAndTranslation(NO_SCALE, NO_OFFSET, NO_OFFSET);
Tony Wickham4fdba142019-11-04 16:23:51 -0800185 }
186
Tony54068172019-05-12 13:24:51 -0500187 public float getOverviewFullscreenProgress() {
188 return 0;
189 }
190
Sunny Goyal7185dd62018-03-14 17:51:49 -0700191 public int getVisibleElements(Launcher launcher) {
Tony Wickhama29a0462021-03-02 12:28:25 -0800192 DeviceProfile deviceProfile = launcher.getDeviceProfile();
193 int flags = WORKSPACE_PAGE_INDICATOR | VERTICAL_SWIPE_INDICATOR | TASKBAR;
194 if (!FeatureFlags.ENABLE_DEVICE_SEARCH.get() && !deviceProfile.isVerticalBarLayout()) {
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600195 flags |= HOTSEAT_SEARCH_BOX;
Tony Wickhambd6f05e2018-03-21 08:16:33 -0700196 }
Tony Wickhama29a0462021-03-02 12:28:25 -0800197 if (!deviceProfile.isTaskbarPresent) {
198 flags |= HOTSEAT_ICONS;
199 }
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600200 return flags;
Sunny Goyal7185dd62018-03-14 17:51:49 -0700201 }
202
Sunny Goyal228153d2018-01-04 15:35:22 -0800203 /**
Tony Wickhama29a0462021-03-02 12:28:25 -0800204 * A shorthand for checking getVisibleElements() & elements == elements.
205 * @return Whether all of the given elements are visible.
206 */
207 public boolean areElementsVisible(Launcher launcher, int elements) {
208 return (getVisibleElements(launcher) & elements) == elements;
209 }
210
211 /**
Sunny Goyal228153d2018-01-04 15:35:22 -0800212 * Fraction shift in the vertical translation UI and related properties
213 *
214 * @see com.android.launcher3.allapps.AllAppsTransitionController
215 */
216 public float getVerticalProgress(Launcher launcher) {
217 return 1f;
218 }
219
Sunny Goyal927447e2018-05-04 13:19:29 -0700220 public float getWorkspaceScrimAlpha(Launcher launcher) {
221 return 0;
222 }
223
Tony Wickhamc0880512019-07-24 17:32:08 -0700224 public float getOverviewScrimAlpha(Launcher launcher) {
225 return 0;
226 }
227
Winson Chung8687bc22020-02-27 23:34:24 -0800228 /**
Zak Cohena39544d2020-04-27 16:26:55 -0700229 * For this state, how modal should over view been shown. 0 modalness means all tasks drawn,
230 * 1 modalness means the current task is show on its own.
231 */
232 public float getOverviewModalness() {
233 return 0;
234 }
235
236 /**
Alex Chaub794ea62021-02-03 18:00:30 +0000237 * For this state, whether tasks should layout as a grid rather than a list.
238 */
239 public boolean displayOverviewTasksAsGrid(Launcher launcher) {
240 return false;
241 }
242
243 /**
Lucas Dupin4918ed32020-03-17 17:11:32 -0700244 * The amount of blur and wallpaper zoom to apply to the background of either the app
245 * or Launcher surface in this state. Should be a number between 0 and 1, inclusive.
246 *
247 * 0 means completely zoomed in, without blurs. 1 is zoomed out, with blurs.
Winson Chung8687bc22020-02-27 23:34:24 -0800248 */
Winson Chungb330d6e2020-04-20 11:57:11 -0700249 public final float getDepth(Context context) {
Jon Miranda36d10152020-06-19 16:35:08 -0700250 return getDepth(context,
251 BaseDraggingActivity.fromContext(context).getDeviceProfile().isMultiWindowMode);
252 }
253
254 /**
255 * Returns the amount of blur and wallpaper zoom for this state with {@param isMultiWindowMode}.
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600256 *
Jon Miranda36d10152020-06-19 16:35:08 -0700257 * @see #getDepth(Context).
258 */
259 public final float getDepth(Context context, boolean isMultiWindowMode) {
260 if (isMultiWindowMode) {
Winson Chungb330d6e2020-04-20 11:57:11 -0700261 return 0;
262 }
263 return getDepthUnchecked(context);
264 }
265
266 protected float getDepthUnchecked(Context context) {
Lucas Dupin4918ed32020-03-17 17:11:32 -0700267 return 0f;
Winson Chung8687bc22020-02-27 23:34:24 -0800268 }
269
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700270 public String getDescription(Launcher launcher) {
271 return launcher.getWorkspace().getCurrentPageDescription();
272 }
273
Sunny Goyalbc683e92017-12-06 10:25:07 -0800274 public PageAlphaProvider getWorkspacePageAlphaProvider(Launcher launcher) {
Jon Mirandae3832222020-07-06 15:38:38 -0700275 if ((this != NORMAL && this != HINT_STATE)
276 || !launcher.getDeviceProfile().shouldFadeAdjacentWorkspaceScreens()) {
Sunny Goyalbc683e92017-12-06 10:25:07 -0800277 return DEFAULT_ALPHA_PROVIDER;
278 }
Sunny Goyal228153d2018-01-04 15:35:22 -0800279 final int centerPage = launcher.getWorkspace().getNextPage();
Sunny Goyal0c723352017-12-12 12:02:29 -0800280 return new PageAlphaProvider(ACCEL_2) {
281 @Override
282 public float getPageAlpha(int pageIndex) {
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600283 return pageIndex != centerPage ? 0 : 1f;
Sunny Goyal0c723352017-12-12 12:02:29 -0800284 }
285 };
Sunny Goyalbc683e92017-12-06 10:25:07 -0800286 }
287
Sunny Goyal35e7d382020-05-08 13:37:58 -0700288 @Override
Sunny Goyal49bcf342018-01-11 13:59:53 -0800289 public LauncherState getHistoryForState(LauncherState previousState) {
290 // No history is supported
291 return NORMAL;
292 }
293
Sunny Goyal35e7d382020-05-08 13:37:58 -0700294 @Override
295 public String toString() {
Tony Wickhamf844ee92020-05-27 15:41:32 -0500296 return TestProtocol.stateOrdinalToString(ordinal);
Sunny Goyal35e7d382020-05-08 13:37:58 -0700297 }
298
Sunny Goyale39690b2018-08-02 13:35:07 -0700299 public void onBackPressed(Launcher launcher) {
300 if (this != NORMAL) {
Sunny Goyal35e7d382020-05-08 13:37:58 -0700301 StateManager<LauncherState> lsm = launcher.getStateManager();
Sunny Goyale39690b2018-08-02 13:35:07 -0700302 LauncherState lastState = lsm.getLastState();
Sunny Goyale39690b2018-08-02 13:35:07 -0700303 lsm.goToState(lastState);
304 }
305 }
306
Sunny Goyal0c723352017-12-12 12:02:29 -0800307 public static abstract class PageAlphaProvider {
Sunny Goyalbc683e92017-12-06 10:25:07 -0800308
Sunny Goyal0c723352017-12-12 12:02:29 -0800309 public final Interpolator interpolator;
310
311 public PageAlphaProvider(Interpolator interpolator) {
312 this.interpolator = interpolator;
313 }
314
315 public abstract float getPageAlpha(int pageIndex);
Sunny Goyalbc683e92017-12-06 10:25:07 -0800316 }
Tonybc234402019-03-19 13:30:25 -0500317
318 public static class ScaleAndTranslation {
319 public float scale;
320 public float translationX;
321 public float translationY;
322
323 public ScaleAndTranslation(float scale, float translationX, float translationY) {
324 this.scale = scale;
325 this.translationX = translationX;
326 this.translationY = translationY;
327 }
328 }
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700329}