blob: 79476fc53600ca3296f4c6e6cf960929d3a62bbf [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;
Sunny Goyal514dfa22020-03-26 15:28:36 -070059 public static final int OVERVIEW_BUTTONS = 1 << 6;
Sunny Goyal7185dd62018-03-14 17:51:49 -070060
Zak Cohen43a16152020-02-25 14:29:37 -080061 /** Mask of all the items that are contained in the apps view. */
62 public static final int APPS_VIEW_ITEM_MASK =
63 HOTSEAT_SEARCH_BOX | ALL_APPS_HEADER | ALL_APPS_HEADER_EXTRA | ALL_APPS_CONTENT;
64
Sunny Goyal8f965462020-04-23 19:00:36 -070065 // Flag indicating workspace has multiple pages visible.
Sunny Goyal35e7d382020-05-08 13:37:58 -070066 public static final int FLAG_MULTI_PAGE = BaseState.getFlag(0);
Sunny Goyal8f965462020-04-23 19:00:36 -070067 // Flag indicating that workspace and its contents are not accessible
Sunny Goyal35e7d382020-05-08 13:37:58 -070068 public static final int FLAG_WORKSPACE_INACCESSIBLE = BaseState.getFlag(1);
Sunny Goyal8f965462020-04-23 19:00:36 -070069
Sunny Goyal8f965462020-04-23 19:00:36 -070070 // Flag indicating the state allows workspace icons to be dragged.
Sunny Goyal35e7d382020-05-08 13:37:58 -070071 public static final int FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED = BaseState.getFlag(2);
Sunny Goyal8f965462020-04-23 19:00:36 -070072 // Flag to indicate that workspace should draw page background
Sunny Goyal35e7d382020-05-08 13:37:58 -070073 public static final int FLAG_WORKSPACE_HAS_BACKGROUNDS = BaseState.getFlag(3);
Sunny Goyal8f965462020-04-23 19:00:36 -070074 // True if the back button should be hidden when in this state (assuming no floating views are
75 // open, launcher has window focus, etc).
Sunny Goyal35e7d382020-05-08 13:37:58 -070076 public static final int FLAG_HIDE_BACK_BUTTON = BaseState.getFlag(4);
Sunny Goyal8f965462020-04-23 19:00:36 -070077 // Flag to indicate if the state would have scrim over sysui region: statu sbar and nav bar
Sunny Goyal35e7d382020-05-08 13:37:58 -070078 public static final int FLAG_HAS_SYS_UI_SCRIM = BaseState.getFlag(5);
Sunny Goyal8f965462020-04-23 19:00:36 -070079 // Flag to inticate that all popups should be closed when this state is enabled.
Sunny Goyal35e7d382020-05-08 13:37:58 -070080 public static final int FLAG_CLOSE_POPUPS = BaseState.getFlag(6);
81 public static final int FLAG_OVERVIEW_UI = BaseState.getFlag(7);
Sunny Goyal8f965462020-04-23 19:00:36 -070082
Sunny Goyal6c6c2f42018-03-02 14:57:46 -080083
Sunny Goyalf2393f12020-04-01 20:13:12 -070084 public static final float NO_OFFSET = 0;
85 public static final float NO_SCALE = 1;
86
Sunny Goyal0c723352017-12-12 12:02:29 -080087 protected static final PageAlphaProvider DEFAULT_ALPHA_PROVIDER =
88 new PageAlphaProvider(ACCEL_2) {
89 @Override
90 public float getPageAlpha(int pageIndex) {
91 return 1;
92 }
93 };
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070094
Zak Cohena39544d2020-04-27 16:26:55 -070095 private static final LauncherState[] sAllStates = new LauncherState[9];
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070096
Sunny Goyal75c59ac2018-01-15 14:23:11 -080097 /**
98 * TODO: Create a separate class for NORMAL state.
99 */
vadimt6895e402019-02-15 16:42:14 -0800100 public static final LauncherState NORMAL = new LauncherState(NORMAL_STATE_ORDINAL,
Winson Chungf9935182020-10-23 09:26:44 -0700101 LAUNCHER_STATE_HOME,
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700102 FLAG_DISABLE_RESTORE | FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED | FLAG_HIDE_BACK_BUTTON |
Tony Wickhamc7203ad2020-02-03 18:36:06 -0800103 FLAG_HAS_SYS_UI_SCRIM) {
104 @Override
Sunny Goyal8f965462020-04-23 19:00:36 -0700105 public int getTransitionDuration(Context context) {
Tony Wickhamc7203ad2020-02-03 18:36:06 -0800106 // Arbitrary duration, when going to NORMAL we use the state we're coming from instead.
107 return 0;
108 }
109 };
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700110
Sunny Goyal7185dd62018-03-14 17:51:49 -0700111 /**
112 * Various Launcher states arranged in the increasing order of UI layers
113 */
vadimt2a648aa2019-02-14 17:47:11 -0800114 public static final LauncherState SPRING_LOADED = new SpringLoadedState(
115 SPRING_LOADED_STATE_ORDINAL);
vadimt2a648aa2019-02-14 17:47:11 -0800116 public static final LauncherState ALL_APPS = new AllAppsState(ALL_APPS_STATE_ORDINAL);
Tony Wickham4fdba142019-11-04 16:23:51 -0800117 public static final LauncherState HINT_STATE = new HintState(HINT_STATE_ORDINAL);
Tony1787ee92019-03-20 12:38:35 -0500118
119 public static final LauncherState OVERVIEW = new OverviewState(OVERVIEW_STATE_ORDINAL);
Zak Cohena39544d2020-04-27 16:26:55 -0700120 public static final LauncherState OVERVIEW_MODAL_TASK = OverviewState.newModalTaskState(
121 OVERVIEW_MODAL_TASK_STATE_ORDINAL);
Tonye4c2e2b2019-03-25 10:23:39 -0500122 public static final LauncherState QUICK_SWITCH =
123 OverviewState.newSwitchState(QUICK_SWITCH_STATE_ORDINAL);
Tony1787ee92019-03-20 12:38:35 -0500124 public static final LauncherState BACKGROUND_APP =
125 OverviewState.newBackgroundState(BACKGROUND_APP_STATE_ORDINAL);
Tony Wickham6becf7c2018-04-19 11:39:34 -0700126
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700127 public final int ordinal;
128
Sunny Goyalf9403d92017-10-18 10:55:56 -0700129 /**
Winson Chungf9935182020-10-23 09:26:44 -0700130 * Used for {@link com.android.launcher3.logging.StatsLogManager}
Sunny Goyalf9403d92017-10-18 10:55:56 -0700131 */
Winson Chungf9935182020-10-23 09:26:44 -0700132 public final int statsLogOrdinal;
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700133
Sunny Goyalf9403d92017-10-18 10:55:56 -0700134 /**
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800135 * True if the state has overview panel visible.
136 */
137 public final boolean overviewUi;
138
Sunny Goyal8f965462020-04-23 19:00:36 -0700139 private final int mFlags;
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700140
Winson Chungf9935182020-10-23 09:26:44 -0700141 public LauncherState(int id, int statsLogOrdinal, int flags) {
142 this.statsLogOrdinal = statsLogOrdinal;
Sunny Goyal8f965462020-04-23 19:00:36 -0700143 this.mFlags = flags;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800144 this.overviewUi = (flags & FLAG_OVERVIEW_UI) != 0;
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700145 this.ordinal = id;
146 sAllStates[id] = this;
147 }
148
Sunny Goyal8f965462020-04-23 19:00:36 -0700149 /**
150 * Returns if the state has the provided flag
151 */
Sunny Goyal35e7d382020-05-08 13:37:58 -0700152 @Override
Sunny Goyal8f965462020-04-23 19:00:36 -0700153 public final boolean hasFlag(int mask) {
154 return (mFlags & mask) != 0;
155 }
156
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700157 public static LauncherState[] values() {
158 return Arrays.copyOf(sAllStates, sAllStates.length);
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700159 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700160
Tonybc234402019-03-19 13:30:25 -0500161 public ScaleAndTranslation getWorkspaceScaleAndTranslation(Launcher launcher) {
Sunny Goyalf2393f12020-04-01 20:13:12 -0700162 return new ScaleAndTranslation(NO_SCALE, NO_OFFSET, NO_OFFSET);
Sunny Goyalc99cb172017-10-19 16:15:09 -0700163 }
164
Tonybc234402019-03-19 13:30:25 -0500165 public ScaleAndTranslation getHotseatScaleAndTranslation(Launcher launcher) {
Tony Wickham57e489c2019-03-04 14:57:07 -0800166 // For most states, treat the hotseat as if it were part of the workspace.
167 return getWorkspaceScaleAndTranslation(launcher);
Tony4bb4b892019-03-03 15:32:12 -0800168 }
169
Sunny Goyalf2393f12020-04-01 20:13:12 -0700170 /**
171 * Returns an array of two elements.
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600172 * The first specifies the scale for the overview
173 * The second is the factor ([0, 1], 0 => center-screen; 1 => offscreen) by which overview
174 * should be shifted horizontally.
Sunny Goyalf2393f12020-04-01 20:13:12 -0700175 */
176 public float[] getOverviewScaleAndOffset(Launcher launcher) {
177 return launcher.getNormalOverviewScaleAndOffset();
Tony3bb5e8e2018-03-14 12:05:00 +0000178 }
179
Tony Wickham4fdba142019-11-04 16:23:51 -0800180 public ScaleAndTranslation getQsbScaleAndTranslation(Launcher launcher) {
Sunny Goyalf2393f12020-04-01 20:13:12 -0700181 return new ScaleAndTranslation(NO_SCALE, NO_OFFSET, NO_OFFSET);
Tony Wickham4fdba142019-11-04 16:23:51 -0800182 }
183
Tony54068172019-05-12 13:24:51 -0500184 public float getOverviewFullscreenProgress() {
185 return 0;
186 }
187
Sunny Goyal7185dd62018-03-14 17:51:49 -0700188 public int getVisibleElements(Launcher launcher) {
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600189 int flags = HOTSEAT_ICONS | VERTICAL_SWIPE_INDICATOR;
190 if (!FeatureFlags.ENABLE_DEVICE_SEARCH.get()
191 && !launcher.getDeviceProfile().isVerticalBarLayout()) {
192 flags |= HOTSEAT_SEARCH_BOX;
Tony Wickhambd6f05e2018-03-21 08:16:33 -0700193 }
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600194 return flags;
Sunny Goyal7185dd62018-03-14 17:51:49 -0700195 }
196
Sunny Goyal228153d2018-01-04 15:35:22 -0800197 /**
198 * Fraction shift in the vertical translation UI and related properties
199 *
200 * @see com.android.launcher3.allapps.AllAppsTransitionController
201 */
202 public float getVerticalProgress(Launcher launcher) {
203 return 1f;
204 }
205
Sunny Goyal927447e2018-05-04 13:19:29 -0700206 public float getWorkspaceScrimAlpha(Launcher launcher) {
207 return 0;
208 }
209
Tony Wickhamc0880512019-07-24 17:32:08 -0700210 public float getOverviewScrimAlpha(Launcher launcher) {
211 return 0;
212 }
213
Winson Chung8687bc22020-02-27 23:34:24 -0800214 /**
Zak Cohena39544d2020-04-27 16:26:55 -0700215 * For this state, how modal should over view been shown. 0 modalness means all tasks drawn,
216 * 1 modalness means the current task is show on its own.
217 */
218 public float getOverviewModalness() {
219 return 0;
220 }
221
222 /**
Lucas Dupin4918ed32020-03-17 17:11:32 -0700223 * The amount of blur and wallpaper zoom to apply to the background of either the app
224 * or Launcher surface in this state. Should be a number between 0 and 1, inclusive.
225 *
226 * 0 means completely zoomed in, without blurs. 1 is zoomed out, with blurs.
Winson Chung8687bc22020-02-27 23:34:24 -0800227 */
Winson Chungb330d6e2020-04-20 11:57:11 -0700228 public final float getDepth(Context context) {
Jon Miranda36d10152020-06-19 16:35:08 -0700229 return getDepth(context,
230 BaseDraggingActivity.fromContext(context).getDeviceProfile().isMultiWindowMode);
231 }
232
233 /**
234 * Returns the amount of blur and wallpaper zoom for this state with {@param isMultiWindowMode}.
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600235 *
Jon Miranda36d10152020-06-19 16:35:08 -0700236 * @see #getDepth(Context).
237 */
238 public final float getDepth(Context context, boolean isMultiWindowMode) {
239 if (isMultiWindowMode) {
Winson Chungb330d6e2020-04-20 11:57:11 -0700240 return 0;
241 }
242 return getDepthUnchecked(context);
243 }
244
245 protected float getDepthUnchecked(Context context) {
Lucas Dupin4918ed32020-03-17 17:11:32 -0700246 return 0f;
Winson Chung8687bc22020-02-27 23:34:24 -0800247 }
248
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700249 public String getDescription(Launcher launcher) {
250 return launcher.getWorkspace().getCurrentPageDescription();
251 }
252
Sunny Goyalbc683e92017-12-06 10:25:07 -0800253 public PageAlphaProvider getWorkspacePageAlphaProvider(Launcher launcher) {
Jon Mirandae3832222020-07-06 15:38:38 -0700254 if ((this != NORMAL && this != HINT_STATE)
255 || !launcher.getDeviceProfile().shouldFadeAdjacentWorkspaceScreens()) {
Sunny Goyalbc683e92017-12-06 10:25:07 -0800256 return DEFAULT_ALPHA_PROVIDER;
257 }
Sunny Goyal228153d2018-01-04 15:35:22 -0800258 final int centerPage = launcher.getWorkspace().getNextPage();
Sunny Goyal0c723352017-12-12 12:02:29 -0800259 return new PageAlphaProvider(ACCEL_2) {
260 @Override
261 public float getPageAlpha(int pageIndex) {
Samuel Fufa2ea01e42020-12-02 13:48:18 -0600262 return pageIndex != centerPage ? 0 : 1f;
Sunny Goyal0c723352017-12-12 12:02:29 -0800263 }
264 };
Sunny Goyalbc683e92017-12-06 10:25:07 -0800265 }
266
Sunny Goyal35e7d382020-05-08 13:37:58 -0700267 @Override
Sunny Goyal49bcf342018-01-11 13:59:53 -0800268 public LauncherState getHistoryForState(LauncherState previousState) {
269 // No history is supported
270 return NORMAL;
271 }
272
Sunny Goyal35e7d382020-05-08 13:37:58 -0700273 @Override
274 public String toString() {
Tony Wickhamf844ee92020-05-27 15:41:32 -0500275 return TestProtocol.stateOrdinalToString(ordinal);
Sunny Goyal35e7d382020-05-08 13:37:58 -0700276 }
277
Sunny Goyale39690b2018-08-02 13:35:07 -0700278 public void onBackPressed(Launcher launcher) {
279 if (this != NORMAL) {
Sunny Goyal35e7d382020-05-08 13:37:58 -0700280 StateManager<LauncherState> lsm = launcher.getStateManager();
Sunny Goyale39690b2018-08-02 13:35:07 -0700281 LauncherState lastState = lsm.getLastState();
Sunny Goyale39690b2018-08-02 13:35:07 -0700282 lsm.goToState(lastState);
283 }
284 }
285
Sunny Goyal0c723352017-12-12 12:02:29 -0800286 public static abstract class PageAlphaProvider {
Sunny Goyalbc683e92017-12-06 10:25:07 -0800287
Sunny Goyal0c723352017-12-12 12:02:29 -0800288 public final Interpolator interpolator;
289
290 public PageAlphaProvider(Interpolator interpolator) {
291 this.interpolator = interpolator;
292 }
293
294 public abstract float getPageAlpha(int pageIndex);
Sunny Goyalbc683e92017-12-06 10:25:07 -0800295 }
Tonybc234402019-03-19 13:30:25 -0500296
297 public static class ScaleAndTranslation {
298 public float scale;
299 public float translationX;
300 public float translationY;
301
302 public ScaleAndTranslation(float scale, float translationX, float translationY) {
303 this.scale = scale;
304 this.translationX = translationX;
305 this.translationY = translationY;
306 }
307 }
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700308}