blob: e133d318f16279385c44adee9f36de7168d6598b [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;
Sunny Goyalab3963d2019-05-23 00:50:08 -070019import static com.android.launcher3.testing.TestProtocol.ALL_APPS_STATE_ORDINAL;
20import static com.android.launcher3.testing.TestProtocol.BACKGROUND_APP_STATE_ORDINAL;
Tony Wickham4fdba142019-11-04 16:23:51 -080021import static com.android.launcher3.testing.TestProtocol.HINT_STATE_ORDINAL;
Sunny Goyalab3963d2019-05-23 00:50:08 -070022import static com.android.launcher3.testing.TestProtocol.NORMAL_STATE_ORDINAL;
Zak Cohena39544d2020-04-27 16:26:55 -070023import static com.android.launcher3.testing.TestProtocol.OVERVIEW_MODAL_TASK_STATE_ORDINAL;
Sunny Goyalab3963d2019-05-23 00:50:08 -070024import static com.android.launcher3.testing.TestProtocol.OVERVIEW_PEEK_STATE_ORDINAL;
25import 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
Tony Wickham4fdba142019-11-04 16:23:51 -080032import com.android.launcher3.states.HintState;
Sunny Goyalc99cb172017-10-19 16:15:09 -070033import com.android.launcher3.states.SpringLoadedState;
Tonye06fef42019-03-22 15:44:28 -050034import com.android.launcher3.uioverrides.states.AllAppsState;
35import com.android.launcher3.uioverrides.states.OverviewState;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070036import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
37
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070038import java.util.Arrays;
39
Sunny Goyal4c7f2152017-10-17 17:17:16 -070040/**
Sunny Goyalbe93f262017-10-20 17:05:27 -070041 * Base state for various states used for the Launcher
Sunny Goyal4c7f2152017-10-17 17:17:16 -070042 */
Tony Wickhamc7203ad2020-02-03 18:36:06 -080043public abstract class LauncherState {
Sunny Goyal4c7f2152017-10-17 17:17:16 -070044
Sunny Goyal7185dd62018-03-14 17:51:49 -070045 /**
46 * Set of elements indicating various workspace elements which change visibility across states
47 * Note that workspace is not included here as in that case, we animate individual pages
48 */
49 public static final int NONE = 0;
Tony Wickhambd6f05e2018-03-21 08:16:33 -070050 public static final int HOTSEAT_ICONS = 1 << 0;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -070051 public static final int HOTSEAT_SEARCH_BOX = 1 << 1;
Tony Wickhambd6f05e2018-03-21 08:16:33 -070052 public static final int ALL_APPS_HEADER = 1 << 2;
53 public static final int ALL_APPS_HEADER_EXTRA = 1 << 3; // e.g. app predictions
54 public static final int ALL_APPS_CONTENT = 1 << 4;
Sunny Goyal56e10192018-05-22 13:22:25 -070055 public static final int VERTICAL_SWIPE_INDICATOR = 1 << 5;
Sunny Goyal514dfa22020-03-26 15:28:36 -070056 public static final int OVERVIEW_BUTTONS = 1 << 6;
Sunny Goyal7185dd62018-03-14 17:51:49 -070057
Zak Cohen43a16152020-02-25 14:29:37 -080058 /** Mask of all the items that are contained in the apps view. */
59 public static final int APPS_VIEW_ITEM_MASK =
60 HOTSEAT_SEARCH_BOX | ALL_APPS_HEADER | ALL_APPS_HEADER_EXTRA | ALL_APPS_CONTENT;
61
Sunny Goyal8f965462020-04-23 19:00:36 -070062 // Flag indicating workspace has multiple pages visible.
63 public static final int FLAG_MULTI_PAGE = 1 << 0;
64 // Flag indicating that workspace and its contents are not accessible
65 public static final int FLAG_WORKSPACE_INACCESSIBLE = 1 << 1;
66
67 public static final int FLAG_DISABLE_RESTORE = 1 << 2;
68 // Flag indicating the state allows workspace icons to be dragged.
69 public static final int FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED = 1 << 3;
70 // Flag to indicate that workspace should draw page background
71 public static final int FLAG_WORKSPACE_HAS_BACKGROUNDS = 1 << 4;
72 // Flag to indicate that Launcher is non-interactive in this state
73 public static final int FLAG_NON_INTERACTIVE = 1 << 5;
74 // True if the back button should be hidden when in this state (assuming no floating views are
75 // open, launcher has window focus, etc).
76 public static final int FLAG_HIDE_BACK_BUTTON = 1 << 6;
77 // Flag to indicate if the state would have scrim over sysui region: statu sbar and nav bar
78 public static final int FLAG_HAS_SYS_UI_SCRIM = 1 << 7;
79 // Flag to inticate that all popups should be closed when this state is enabled.
80 public static final int FLAG_CLOSE_POPUPS = 1 << 8;
81 public static final int FLAG_OVERVIEW_UI = 1 << 9;
82
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,
Tony Wickhamc7203ad2020-02-03 18:36:06 -0800101 ContainerType.WORKSPACE,
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);
120 public static final LauncherState OVERVIEW_PEEK =
121 OverviewState.newPeekState(OVERVIEW_PEEK_STATE_ORDINAL);
Zak Cohena39544d2020-04-27 16:26:55 -0700122 public static final LauncherState OVERVIEW_MODAL_TASK = OverviewState.newModalTaskState(
123 OVERVIEW_MODAL_TASK_STATE_ORDINAL);
Tonye4c2e2b2019-03-25 10:23:39 -0500124 public static final LauncherState QUICK_SWITCH =
125 OverviewState.newSwitchState(QUICK_SWITCH_STATE_ORDINAL);
Tony1787ee92019-03-20 12:38:35 -0500126 public static final LauncherState BACKGROUND_APP =
127 OverviewState.newBackgroundState(BACKGROUND_APP_STATE_ORDINAL);
Tony Wickham6becf7c2018-04-19 11:39:34 -0700128
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700129 public final int ordinal;
130
Sunny Goyalf9403d92017-10-18 10:55:56 -0700131 /**
132 * Used for containerType in {@link com.android.launcher3.logging.UserEventDispatcher}
133 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700134 public final int containerType;
135
Sunny Goyalf9403d92017-10-18 10:55:56 -0700136 /**
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800137 * True if the state has overview panel visible.
138 */
139 public final boolean overviewUi;
140
Sunny Goyal8f965462020-04-23 19:00:36 -0700141 private final int mFlags;
Sunny Goyal5d1873a2018-05-08 11:10:44 -0700142
Tony Wickhamc7203ad2020-02-03 18:36:06 -0800143 public LauncherState(int id, int containerType, int flags) {
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700144 this.containerType = containerType;
Sunny Goyal8f965462020-04-23 19:00:36 -0700145 this.mFlags = flags;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800146 this.overviewUi = (flags & FLAG_OVERVIEW_UI) != 0;
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700147 this.ordinal = id;
148 sAllStates[id] = this;
149 }
150
Sunny Goyal8f965462020-04-23 19:00:36 -0700151 /**
152 * Returns if the state has the provided flag
153 */
154 public final boolean hasFlag(int mask) {
155 return (mFlags & mask) != 0;
156 }
157
158 /**
159 * @return true if the state can be persisted across activity restarts.
160 */
161 public final boolean shouldDisableRestore() {
162 return hasFlag(FLAG_DISABLE_RESTORE);
163 }
164
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700165 public static LauncherState[] values() {
166 return Arrays.copyOf(sAllStates, sAllStates.length);
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700167 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700168
Tony Wickhamc7203ad2020-02-03 18:36:06 -0800169 /**
170 * @return How long the animation to this state should take (or from this state to NORMAL).
Tony Wickhamc7203ad2020-02-03 18:36:06 -0800171 */
Sunny Goyal8f965462020-04-23 19:00:36 -0700172 public abstract int getTransitionDuration(Context context);
Tony Wickhamc7203ad2020-02-03 18:36:06 -0800173
Tonybc234402019-03-19 13:30:25 -0500174 public ScaleAndTranslation getWorkspaceScaleAndTranslation(Launcher launcher) {
Sunny Goyalf2393f12020-04-01 20:13:12 -0700175 return new ScaleAndTranslation(NO_SCALE, NO_OFFSET, NO_OFFSET);
Sunny Goyalc99cb172017-10-19 16:15:09 -0700176 }
177
Tonybc234402019-03-19 13:30:25 -0500178 public ScaleAndTranslation getHotseatScaleAndTranslation(Launcher launcher) {
Tony Wickham57e489c2019-03-04 14:57:07 -0800179 // For most states, treat the hotseat as if it were part of the workspace.
180 return getWorkspaceScaleAndTranslation(launcher);
Tony4bb4b892019-03-03 15:32:12 -0800181 }
182
Sunny Goyalf2393f12020-04-01 20:13:12 -0700183 /**
184 * Returns an array of two elements.
185 * The first specifies the scale for the overview
186 * The second is the factor ([0, 1], 0 => center-screen; 1 => offscreen) by which overview
187 * should be shifted horizontally.
188 */
189 public float[] getOverviewScaleAndOffset(Launcher launcher) {
190 return launcher.getNormalOverviewScaleAndOffset();
Tony3bb5e8e2018-03-14 12:05:00 +0000191 }
192
Tony Wickham4fdba142019-11-04 16:23:51 -0800193 public ScaleAndTranslation getQsbScaleAndTranslation(Launcher launcher) {
Sunny Goyalf2393f12020-04-01 20:13:12 -0700194 return new ScaleAndTranslation(NO_SCALE, NO_OFFSET, NO_OFFSET);
Tony Wickham4fdba142019-11-04 16:23:51 -0800195 }
196
Tony54068172019-05-12 13:24:51 -0500197 public float getOverviewFullscreenProgress() {
198 return 0;
199 }
200
Sunny Goyal7185dd62018-03-14 17:51:49 -0700201 public int getVisibleElements(Launcher launcher) {
Tony Wickhambd6f05e2018-03-21 08:16:33 -0700202 if (launcher.getDeviceProfile().isVerticalBarLayout()) {
Sunny Goyal56e10192018-05-22 13:22:25 -0700203 return HOTSEAT_ICONS | VERTICAL_SWIPE_INDICATOR;
Tony Wickhambd6f05e2018-03-21 08:16:33 -0700204 }
Sunny Goyal56e10192018-05-22 13:22:25 -0700205 return HOTSEAT_ICONS | HOTSEAT_SEARCH_BOX | VERTICAL_SWIPE_INDICATOR;
Sunny Goyal7185dd62018-03-14 17:51:49 -0700206 }
207
Sunny Goyal228153d2018-01-04 15:35:22 -0800208 /**
209 * Fraction shift in the vertical translation UI and related properties
210 *
211 * @see com.android.launcher3.allapps.AllAppsTransitionController
212 */
213 public float getVerticalProgress(Launcher launcher) {
214 return 1f;
215 }
216
Sunny Goyal927447e2018-05-04 13:19:29 -0700217 public float getWorkspaceScrimAlpha(Launcher launcher) {
218 return 0;
219 }
220
Tony Wickhamc0880512019-07-24 17:32:08 -0700221 public float getOverviewScrimAlpha(Launcher launcher) {
222 return 0;
223 }
224
Winson Chung8687bc22020-02-27 23:34:24 -0800225 /**
Zak Cohena39544d2020-04-27 16:26:55 -0700226 * For this state, how modal should over view been shown. 0 modalness means all tasks drawn,
227 * 1 modalness means the current task is show on its own.
228 */
229 public float getOverviewModalness() {
230 return 0;
231 }
232
233 /**
Lucas Dupin4918ed32020-03-17 17:11:32 -0700234 * The amount of blur and wallpaper zoom to apply to the background of either the app
235 * or Launcher surface in this state. Should be a number between 0 and 1, inclusive.
236 *
237 * 0 means completely zoomed in, without blurs. 1 is zoomed out, with blurs.
Winson Chung8687bc22020-02-27 23:34:24 -0800238 */
Winson Chungb330d6e2020-04-20 11:57:11 -0700239 public final float getDepth(Context context) {
240 if (BaseDraggingActivity.fromContext(context).getDeviceProfile().isMultiWindowMode) {
241 return 0;
242 }
243 return getDepthUnchecked(context);
244 }
245
246 protected float getDepthUnchecked(Context context) {
Lucas Dupin4918ed32020-03-17 17:11:32 -0700247 return 0f;
Winson Chung8687bc22020-02-27 23:34:24 -0800248 }
249
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700250 public String getDescription(Launcher launcher) {
251 return launcher.getWorkspace().getCurrentPageDescription();
252 }
253
Sunny Goyalbc683e92017-12-06 10:25:07 -0800254 public PageAlphaProvider getWorkspacePageAlphaProvider(Launcher launcher) {
255 if (this != NORMAL || !launcher.getDeviceProfile().shouldFadeAdjacentWorkspaceScreens()) {
256 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) {
262 return pageIndex != centerPage ? 0 : 1f;
263 }
264 };
Sunny Goyalbc683e92017-12-06 10:25:07 -0800265 }
266
Sunny Goyal49bcf342018-01-11 13:59:53 -0800267 public LauncherState getHistoryForState(LauncherState previousState) {
268 // No history is supported
269 return NORMAL;
270 }
271
Sunny Goyale39690b2018-08-02 13:35:07 -0700272 public void onBackPressed(Launcher launcher) {
273 if (this != NORMAL) {
274 LauncherStateManager lsm = launcher.getStateManager();
275 LauncherState lastState = lsm.getLastState();
Sunny Goyale39690b2018-08-02 13:35:07 -0700276 lsm.goToState(lastState);
277 }
278 }
279
Sunny Goyal0c723352017-12-12 12:02:29 -0800280 public static abstract class PageAlphaProvider {
Sunny Goyalbc683e92017-12-06 10:25:07 -0800281
Sunny Goyal0c723352017-12-12 12:02:29 -0800282 public final Interpolator interpolator;
283
284 public PageAlphaProvider(Interpolator interpolator) {
285 this.interpolator = interpolator;
286 }
287
288 public abstract float getPageAlpha(int pageIndex);
Sunny Goyalbc683e92017-12-06 10:25:07 -0800289 }
Tonybc234402019-03-19 13:30:25 -0500290
291 public static class ScaleAndTranslation {
292 public float scale;
293 public float translationX;
294 public float translationY;
295
296 public ScaleAndTranslation(float scale, float translationX, float translationY) {
297 this.scale = scale;
298 this.translationX = translationX;
299 this.translationY = translationY;
300 }
301 }
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700302}