blob: eba0ac9633d1a66b67ab67b8be461607bf46adf1 [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
Sunny Goyal35e7d382020-05-08 13:37:58 -070032import com.android.launcher3.statemanager.BaseState;
33import com.android.launcher3.statemanager.StateManager;
Tony Wickham4fdba142019-11-04 16:23:51 -080034import com.android.launcher3.states.HintState;
Sunny Goyalc99cb172017-10-19 16:15:09 -070035import com.android.launcher3.states.SpringLoadedState;
Tony Wickhamf844ee92020-05-27 15:41:32 -050036import com.android.launcher3.testing.TestProtocol;
Tonye06fef42019-03-22 15:44:28 -050037import com.android.launcher3.uioverrides.states.AllAppsState;
38import com.android.launcher3.uioverrides.states.OverviewState;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070039
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070040import java.util.Arrays;
41
Sunny Goyal4c7f2152017-10-17 17:17:16 -070042/**
Sunny Goyalbe93f262017-10-20 17:05:27 -070043 * Base state for various states used for the Launcher
Sunny Goyal4c7f2152017-10-17 17:17:16 -070044 */
Sunny Goyal35e7d382020-05-08 13:37:58 -070045public abstract class LauncherState implements BaseState<LauncherState> {
Sunny Goyal4c7f2152017-10-17 17:17:16 -070046
Sunny Goyal7185dd62018-03-14 17:51:49 -070047 /**
48 * Set of elements indicating various workspace elements which change visibility across states
49 * Note that workspace is not included here as in that case, we animate individual pages
50 */
51 public static final int NONE = 0;
Tony Wickhambd6f05e2018-03-21 08:16:33 -070052 public static final int HOTSEAT_ICONS = 1 << 0;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -070053 public static final int HOTSEAT_SEARCH_BOX = 1 << 1;
Tony Wickhambd6f05e2018-03-21 08:16:33 -070054 public static final int ALL_APPS_HEADER = 1 << 2;
55 public static final int ALL_APPS_HEADER_EXTRA = 1 << 3; // e.g. app predictions
56 public static final int ALL_APPS_CONTENT = 1 << 4;
Sunny Goyal56e10192018-05-22 13:22:25 -070057 public static final int VERTICAL_SWIPE_INDICATOR = 1 << 5;
Sunny Goyal514dfa22020-03-26 15:28:36 -070058 public static final int OVERVIEW_BUTTONS = 1 << 6;
Sunny Goyal7185dd62018-03-14 17:51:49 -070059
Zak Cohen43a16152020-02-25 14:29:37 -080060 /** Mask of all the items that are contained in the apps view. */
61 public static final int APPS_VIEW_ITEM_MASK =
62 HOTSEAT_SEARCH_BOX | ALL_APPS_HEADER | ALL_APPS_HEADER_EXTRA | ALL_APPS_CONTENT;
63
Sunny Goyal8f965462020-04-23 19:00:36 -070064 // Flag indicating workspace has multiple pages visible.
Sunny Goyal35e7d382020-05-08 13:37:58 -070065 public static final int FLAG_MULTI_PAGE = BaseState.getFlag(0);
Sunny Goyal8f965462020-04-23 19:00:36 -070066 // Flag indicating that workspace and its contents are not accessible
Sunny Goyal35e7d382020-05-08 13:37:58 -070067 public static final int FLAG_WORKSPACE_INACCESSIBLE = BaseState.getFlag(1);
Sunny Goyal8f965462020-04-23 19:00:36 -070068
Sunny Goyal8f965462020-04-23 19:00:36 -070069 // Flag indicating the state allows workspace icons to be dragged.
Sunny Goyal35e7d382020-05-08 13:37:58 -070070 public static final int FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED = BaseState.getFlag(2);
Sunny Goyal8f965462020-04-23 19:00:36 -070071 // Flag to indicate that workspace should draw page background
Sunny Goyal35e7d382020-05-08 13:37:58 -070072 public static final int FLAG_WORKSPACE_HAS_BACKGROUNDS = BaseState.getFlag(3);
Sunny Goyal8f965462020-04-23 19:00:36 -070073 // True if the back button should be hidden when in this state (assuming no floating views are
74 // open, launcher has window focus, etc).
Sunny Goyal35e7d382020-05-08 13:37:58 -070075 public static final int FLAG_HIDE_BACK_BUTTON = BaseState.getFlag(4);
Sunny Goyal8f965462020-04-23 19:00:36 -070076 // Flag to indicate if the state would have scrim over sysui region: statu sbar and nav bar
Sunny Goyal35e7d382020-05-08 13:37:58 -070077 public static final int FLAG_HAS_SYS_UI_SCRIM = BaseState.getFlag(5);
Sunny Goyal8f965462020-04-23 19:00:36 -070078 // Flag to inticate that all popups should be closed when this state is enabled.
Sunny Goyal35e7d382020-05-08 13:37:58 -070079 public static final int FLAG_CLOSE_POPUPS = BaseState.getFlag(6);
80 public static final int FLAG_OVERVIEW_UI = BaseState.getFlag(7);
Sunny Goyal8f965462020-04-23 19:00:36 -070081
Sunny Goyal6c6c2f42018-03-02 14:57:46 -080082
Sunny Goyalf2393f12020-04-01 20:13:12 -070083 public static final float NO_OFFSET = 0;
84 public static final float NO_SCALE = 1;
85
Sunny Goyal0c723352017-12-12 12:02:29 -080086 protected static final PageAlphaProvider DEFAULT_ALPHA_PROVIDER =
87 new PageAlphaProvider(ACCEL_2) {
88 @Override
89 public float getPageAlpha(int pageIndex) {
90 return 1;
91 }
92 };
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070093
Zak Cohena39544d2020-04-27 16:26:55 -070094 private static final LauncherState[] sAllStates = new LauncherState[9];
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070095
Sunny Goyal75c59ac2018-01-15 14:23:11 -080096 /**
97 * TODO: Create a separate class for NORMAL state.
98 */
vadimt6895e402019-02-15 16:42:14 -080099 public static final LauncherState NORMAL = new LauncherState(NORMAL_STATE_ORDINAL,
Winson Chungf9935182020-10-23 09:26:44 -0700100 LAUNCHER_STATE_HOME,
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700101 FLAG_DISABLE_RESTORE | FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED | FLAG_HIDE_BACK_BUTTON |
Tony Wickhamc7203ad2020-02-03 18:36:06 -0800102 FLAG_HAS_SYS_UI_SCRIM) {
103 @Override
Sunny Goyal8f965462020-04-23 19:00:36 -0700104 public int getTransitionDuration(Context context) {
Tony Wickhamc7203ad2020-02-03 18:36:06 -0800105 // Arbitrary duration, when going to NORMAL we use the state we're coming from instead.
106 return 0;
107 }
108 };
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700109
Sunny Goyal7185dd62018-03-14 17:51:49 -0700110 /**
111 * Various Launcher states arranged in the increasing order of UI layers
112 */
vadimt2a648aa2019-02-14 17:47:11 -0800113 public static final LauncherState SPRING_LOADED = new SpringLoadedState(
114 SPRING_LOADED_STATE_ORDINAL);
vadimt2a648aa2019-02-14 17:47:11 -0800115 public static final LauncherState ALL_APPS = new AllAppsState(ALL_APPS_STATE_ORDINAL);
Tony Wickham4fdba142019-11-04 16:23:51 -0800116 public static final LauncherState HINT_STATE = new HintState(HINT_STATE_ORDINAL);
Tony1787ee92019-03-20 12:38:35 -0500117
118 public static final LauncherState OVERVIEW = new OverviewState(OVERVIEW_STATE_ORDINAL);
Zak Cohena39544d2020-04-27 16:26:55 -0700119 public static final LauncherState OVERVIEW_MODAL_TASK = OverviewState.newModalTaskState(
120 OVERVIEW_MODAL_TASK_STATE_ORDINAL);
Tonye4c2e2b2019-03-25 10:23:39 -0500121 public static final LauncherState QUICK_SWITCH =
122 OverviewState.newSwitchState(QUICK_SWITCH_STATE_ORDINAL);
Tony1787ee92019-03-20 12:38:35 -0500123 public static final LauncherState BACKGROUND_APP =
124 OverviewState.newBackgroundState(BACKGROUND_APP_STATE_ORDINAL);
Tony Wickham6becf7c2018-04-19 11:39:34 -0700125
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700126 public final int ordinal;
127
Sunny Goyalf9403d92017-10-18 10:55:56 -0700128 /**
Winson Chungf9935182020-10-23 09:26:44 -0700129 * Used for {@link com.android.launcher3.logging.StatsLogManager}
Sunny Goyalf9403d92017-10-18 10:55:56 -0700130 */
Winson Chungf9935182020-10-23 09:26:44 -0700131 public final int statsLogOrdinal;
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700132
Sunny Goyalf9403d92017-10-18 10:55:56 -0700133 /**
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800134 * True if the state has overview panel visible.
135 */
136 public final boolean overviewUi;
137
Sunny Goyal8f965462020-04-23 19:00:36 -0700138 private final int mFlags;
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700139
Winson Chungf9935182020-10-23 09:26:44 -0700140 public LauncherState(int id, int statsLogOrdinal, int flags) {
141 this.statsLogOrdinal = statsLogOrdinal;
Sunny Goyal8f965462020-04-23 19:00:36 -0700142 this.mFlags = flags;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800143 this.overviewUi = (flags & FLAG_OVERVIEW_UI) != 0;
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700144 this.ordinal = id;
145 sAllStates[id] = this;
146 }
147
Sunny Goyal8f965462020-04-23 19:00:36 -0700148 /**
149 * Returns if the state has the provided flag
150 */
Sunny Goyal35e7d382020-05-08 13:37:58 -0700151 @Override
Sunny Goyal8f965462020-04-23 19:00:36 -0700152 public final boolean hasFlag(int mask) {
153 return (mFlags & mask) != 0;
154 }
155
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700156 public static LauncherState[] values() {
157 return Arrays.copyOf(sAllStates, sAllStates.length);
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700158 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700159
Tonybc234402019-03-19 13:30:25 -0500160 public ScaleAndTranslation getWorkspaceScaleAndTranslation(Launcher launcher) {
Sunny Goyalf2393f12020-04-01 20:13:12 -0700161 return new ScaleAndTranslation(NO_SCALE, NO_OFFSET, NO_OFFSET);
Sunny Goyalc99cb172017-10-19 16:15:09 -0700162 }
163
Tonybc234402019-03-19 13:30:25 -0500164 public ScaleAndTranslation getHotseatScaleAndTranslation(Launcher launcher) {
Tony Wickham57e489c2019-03-04 14:57:07 -0800165 // For most states, treat the hotseat as if it were part of the workspace.
166 return getWorkspaceScaleAndTranslation(launcher);
Tony4bb4b892019-03-03 15:32:12 -0800167 }
168
Sunny Goyalf2393f12020-04-01 20:13:12 -0700169 /**
170 * Returns an array of two elements.
171 * The first specifies the scale for the overview
172 * The second is the factor ([0, 1], 0 => center-screen; 1 => offscreen) by which overview
173 * should be shifted horizontally.
174 */
175 public float[] getOverviewScaleAndOffset(Launcher launcher) {
176 return launcher.getNormalOverviewScaleAndOffset();
Tony3bb5e8e2018-03-14 12:05:00 +0000177 }
178
Tony Wickham4fdba142019-11-04 16:23:51 -0800179 public ScaleAndTranslation getQsbScaleAndTranslation(Launcher launcher) {
Sunny Goyalf2393f12020-04-01 20:13:12 -0700180 return new ScaleAndTranslation(NO_SCALE, NO_OFFSET, NO_OFFSET);
Tony Wickham4fdba142019-11-04 16:23:51 -0800181 }
182
Tony54068172019-05-12 13:24:51 -0500183 public float getOverviewFullscreenProgress() {
184 return 0;
185 }
186
Sunny Goyal7185dd62018-03-14 17:51:49 -0700187 public int getVisibleElements(Launcher launcher) {
Tony Wickhambd6f05e2018-03-21 08:16:33 -0700188 if (launcher.getDeviceProfile().isVerticalBarLayout()) {
Sunny Goyal56e10192018-05-22 13:22:25 -0700189 return HOTSEAT_ICONS | VERTICAL_SWIPE_INDICATOR;
Tony Wickhambd6f05e2018-03-21 08:16:33 -0700190 }
Sunny Goyal56e10192018-05-22 13:22:25 -0700191 return HOTSEAT_ICONS | HOTSEAT_SEARCH_BOX | VERTICAL_SWIPE_INDICATOR;
Sunny Goyal7185dd62018-03-14 17:51:49 -0700192 }
193
Sunny Goyal228153d2018-01-04 15:35:22 -0800194 /**
195 * Fraction shift in the vertical translation UI and related properties
196 *
197 * @see com.android.launcher3.allapps.AllAppsTransitionController
198 */
199 public float getVerticalProgress(Launcher launcher) {
200 return 1f;
201 }
202
Sunny Goyal927447e2018-05-04 13:19:29 -0700203 public float getWorkspaceScrimAlpha(Launcher launcher) {
204 return 0;
205 }
206
Tony Wickhamc0880512019-07-24 17:32:08 -0700207 public float getOverviewScrimAlpha(Launcher launcher) {
208 return 0;
209 }
210
Winson Chung8687bc22020-02-27 23:34:24 -0800211 /**
Zak Cohena39544d2020-04-27 16:26:55 -0700212 * For this state, how modal should over view been shown. 0 modalness means all tasks drawn,
213 * 1 modalness means the current task is show on its own.
214 */
215 public float getOverviewModalness() {
216 return 0;
217 }
218
219 /**
Lucas Dupin4918ed32020-03-17 17:11:32 -0700220 * The amount of blur and wallpaper zoom to apply to the background of either the app
221 * or Launcher surface in this state. Should be a number between 0 and 1, inclusive.
222 *
223 * 0 means completely zoomed in, without blurs. 1 is zoomed out, with blurs.
Winson Chung8687bc22020-02-27 23:34:24 -0800224 */
Winson Chungb330d6e2020-04-20 11:57:11 -0700225 public final float getDepth(Context context) {
Jon Miranda36d10152020-06-19 16:35:08 -0700226 return getDepth(context,
227 BaseDraggingActivity.fromContext(context).getDeviceProfile().isMultiWindowMode);
228 }
229
230 /**
231 * Returns the amount of blur and wallpaper zoom for this state with {@param isMultiWindowMode}.
232 * @see #getDepth(Context).
233 */
234 public final float getDepth(Context context, boolean isMultiWindowMode) {
235 if (isMultiWindowMode) {
Winson Chungb330d6e2020-04-20 11:57:11 -0700236 return 0;
237 }
238 return getDepthUnchecked(context);
239 }
240
241 protected float getDepthUnchecked(Context context) {
Lucas Dupin4918ed32020-03-17 17:11:32 -0700242 return 0f;
Winson Chung8687bc22020-02-27 23:34:24 -0800243 }
244
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700245 public String getDescription(Launcher launcher) {
246 return launcher.getWorkspace().getCurrentPageDescription();
247 }
248
Sunny Goyalbc683e92017-12-06 10:25:07 -0800249 public PageAlphaProvider getWorkspacePageAlphaProvider(Launcher launcher) {
Jon Mirandae3832222020-07-06 15:38:38 -0700250 if ((this != NORMAL && this != HINT_STATE)
251 || !launcher.getDeviceProfile().shouldFadeAdjacentWorkspaceScreens()) {
Sunny Goyalbc683e92017-12-06 10:25:07 -0800252 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 Goyal35e7d382020-05-08 13:37:58 -0700263 @Override
Sunny Goyal49bcf342018-01-11 13:59:53 -0800264 public LauncherState getHistoryForState(LauncherState previousState) {
265 // No history is supported
266 return NORMAL;
267 }
268
Sunny Goyal35e7d382020-05-08 13:37:58 -0700269 @Override
270 public String toString() {
Tony Wickhamf844ee92020-05-27 15:41:32 -0500271 return TestProtocol.stateOrdinalToString(ordinal);
Sunny Goyal35e7d382020-05-08 13:37:58 -0700272 }
273
Sunny Goyale39690b2018-08-02 13:35:07 -0700274 public void onBackPressed(Launcher launcher) {
275 if (this != NORMAL) {
Sunny Goyal35e7d382020-05-08 13:37:58 -0700276 StateManager<LauncherState> lsm = launcher.getStateManager();
Sunny Goyale39690b2018-08-02 13:35:07 -0700277 LauncherState lastState = lsm.getLastState();
Sunny Goyale39690b2018-08-02 13:35:07 -0700278 lsm.goToState(lastState);
279 }
280 }
281
Sunny Goyal0c723352017-12-12 12:02:29 -0800282 public static abstract class PageAlphaProvider {
Sunny Goyalbc683e92017-12-06 10:25:07 -0800283
Sunny Goyal0c723352017-12-12 12:02:29 -0800284 public final Interpolator interpolator;
285
286 public PageAlphaProvider(Interpolator interpolator) {
287 this.interpolator = interpolator;
288 }
289
290 public abstract float getPageAlpha(int pageIndex);
Sunny Goyalbc683e92017-12-06 10:25:07 -0800291 }
Tonybc234402019-03-19 13:30:25 -0500292
293 public static class ScaleAndTranslation {
294 public float scale;
295 public float translationX;
296 public float translationY;
297
298 public ScaleAndTranslation(float scale, float translationX, float translationY) {
299 this.scale = scale;
300 this.translationX = translationX;
301 this.translationY = translationY;
302 }
303 }
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700304}