blob: 54df1dad0e220c39779b8ff778ef3131643e580b [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
18import static android.view.View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
19import static android.view.View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS;
Sunny Goyal3e3f44c2017-10-23 17:14:52 -070020import static android.view.accessibility.AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070021
Sunny Goyal0c723352017-12-12 12:02:29 -080022import static com.android.launcher3.anim.Interpolators.ACCEL_2;
23
Sunny Goyalbe93f262017-10-20 17:05:27 -070024import android.view.View;
Sunny Goyal0c723352017-12-12 12:02:29 -080025import android.view.animation.Interpolator;
Sunny Goyalbe93f262017-10-20 17:05:27 -070026
Sunny Goyalbc683e92017-12-06 10:25:07 -080027import com.android.launcher3.uioverrides.AllAppsState;
Sunny Goyalc99cb172017-10-19 16:15:09 -070028import com.android.launcher3.states.SpringLoadedState;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -080029import com.android.launcher3.uioverrides.FastOverviewState;
Sunny Goyal85525172017-11-06 13:00:42 -080030import com.android.launcher3.uioverrides.OverviewState;
Sunny Goyal75c59ac2018-01-15 14:23:11 -080031import com.android.launcher3.uioverrides.UiFactory;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070032import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
33
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070034import java.util.Arrays;
35
Sunny Goyal4c7f2152017-10-17 17:17:16 -070036
37/**
Sunny Goyalbe93f262017-10-20 17:05:27 -070038 * Base state for various states used for the Launcher
Sunny Goyal4c7f2152017-10-17 17:17:16 -070039 */
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070040public class LauncherState {
Sunny Goyal4c7f2152017-10-17 17:17:16 -070041
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070042 protected static final int FLAG_SHOW_SCRIM = 1 << 0;
43 protected static final int FLAG_MULTI_PAGE = 1 << 1;
Sunny Goyalc4fa8c32017-11-07 12:23:58 -080044 protected static final int FLAG_DISABLE_ACCESSIBILITY = 1 << 2;
Sunny Goyalbc683e92017-12-06 10:25:07 -080045 protected static final int FLAG_DISABLE_RESTORE = 1 << 3;
Tony Wickham53370672017-12-05 10:10:55 -080046 protected static final int FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED = 1 << 4;
Sunny Goyalbc683e92017-12-06 10:25:07 -080047 protected static final int FLAG_DISABLE_PAGE_CLIPPING = 1 << 5;
Sunny Goyal6639a5d2018-02-28 15:09:36 -080048 protected static final int FLAG_PAGE_BACKGROUNDS = 1 << 6;
49 protected static final int FLAG_ALL_APPS_SCRIM = 1 << 7;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -080050 protected static final int FLAG_DISABLE_INTERACTION = 1 << 8;
51 protected static final int FLAG_OVERVIEW_UI = 1 << 9;
52
Sunny Goyalbc683e92017-12-06 10:25:07 -080053
Sunny Goyal0c723352017-12-12 12:02:29 -080054 protected static final PageAlphaProvider DEFAULT_ALPHA_PROVIDER =
55 new PageAlphaProvider(ACCEL_2) {
56 @Override
57 public float getPageAlpha(int pageIndex) {
58 return 1;
59 }
60 };
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070061
Sunny Goyal6c6c2f42018-03-02 14:57:46 -080062 private static final LauncherState[] sAllStates = new LauncherState[5];
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070063
Sunny Goyal75c59ac2018-01-15 14:23:11 -080064 /**
65 * TODO: Create a separate class for NORMAL state.
66 */
Sunny Goyalc99cb172017-10-19 16:15:09 -070067 public static final LauncherState NORMAL = new LauncherState(0, ContainerType.WORKSPACE,
Sunny Goyal228153d2018-01-04 15:35:22 -080068 0, FLAG_DISABLE_RESTORE | FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED);
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070069
Sunny Goyalbe93f262017-10-20 17:05:27 -070070 public static final LauncherState ALL_APPS = new AllAppsState(1);
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070071
Sunny Goyalc99cb172017-10-19 16:15:09 -070072 public static final LauncherState SPRING_LOADED = new SpringLoadedState(2);
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070073
Sunny Goyal85525172017-11-06 13:00:42 -080074 public static final LauncherState OVERVIEW = new OverviewState(3);
Sunny Goyal4c7f2152017-10-17 17:17:16 -070075
Sunny Goyal6c6c2f42018-03-02 14:57:46 -080076 public static final LauncherState FAST_OVERVIEW = new FastOverviewState(4);
77
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070078 public final int ordinal;
79
Sunny Goyalf9403d92017-10-18 10:55:56 -070080 /**
81 * Used for containerType in {@link com.android.launcher3.logging.UserEventDispatcher}
82 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -070083 public final int containerType;
84
Sunny Goyalf9403d92017-10-18 10:55:56 -070085 /**
86 * True if the state can be persisted across activity restarts.
87 */
Sunny Goyalbc683e92017-12-06 10:25:07 -080088 public final boolean disableRestore;
Sunny Goyalf9403d92017-10-18 10:55:56 -070089
90 /**
91 * True if workspace has multiple pages visible.
92 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -070093 public final boolean hasMultipleVisiblePages;
Sunny Goyalf9403d92017-10-18 10:55:56 -070094
95 /**
96 * Accessibility flag for workspace and its pages.
97 * @see android.view.View#setImportantForAccessibility(int)
98 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -070099 public final int workspaceAccessibilityFlag;
100
Sunny Goyalbe93f262017-10-20 17:05:27 -0700101 /**
102 * Properties related to state transition animation
103 *
104 * @see WorkspaceStateTransitionAnimation
105 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700106 public final boolean hasScrim;
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800107 public final boolean hasWorkspacePageBackground;
108 public final boolean hasAllAppsScrim;
109
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700110 public final int transitionDuration;
111
Sunny Goyalbe93f262017-10-20 17:05:27 -0700112 /**
Tony Wickham53370672017-12-05 10:10:55 -0800113 * True if the state allows workspace icons to be dragged.
114 */
115 public final boolean workspaceIconsCanBeDragged;
116
Sunny Goyalbc683e92017-12-06 10:25:07 -0800117 /**
118 * True if the workspace pages should not be clipped relative to the workspace bounds
119 * for this state.
120 */
121 public final boolean disablePageClipping;
122
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800123 /**
124 * True if launcher can not be directly interacted in this state;
125 */
126 public final boolean disableInteraction;
127
128 /**
129 * True if the state has overview panel visible.
130 */
131 public final boolean overviewUi;
132
Sunny Goyal228153d2018-01-04 15:35:22 -0800133 public LauncherState(int id, int containerType, int transitionDuration, int flags) {
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700134 this.containerType = containerType;
135 this.transitionDuration = transitionDuration;
136
137 this.hasScrim = (flags & FLAG_SHOW_SCRIM) != 0;
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800138 this.hasWorkspacePageBackground = (flags & FLAG_PAGE_BACKGROUNDS) != 0;
139 this.hasAllAppsScrim = (flags & FLAG_ALL_APPS_SCRIM) != 0;
140
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700141 this.hasMultipleVisiblePages = (flags & FLAG_MULTI_PAGE) != 0;
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700142 this.workspaceAccessibilityFlag = (flags & FLAG_DISABLE_ACCESSIBILITY) != 0
143 ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
144 : IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Sunny Goyalbc683e92017-12-06 10:25:07 -0800145 this.disableRestore = (flags & FLAG_DISABLE_RESTORE) != 0;
Tony Wickham53370672017-12-05 10:10:55 -0800146 this.workspaceIconsCanBeDragged = (flags & FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED) != 0;
Sunny Goyalbc683e92017-12-06 10:25:07 -0800147 this.disablePageClipping = (flags & FLAG_DISABLE_PAGE_CLIPPING) != 0;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800148 this.disableInteraction = (flags & FLAG_DISABLE_INTERACTION) != 0;
149 this.overviewUi = (flags & FLAG_OVERVIEW_UI) != 0;
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700150
151 this.ordinal = id;
152 sAllStates[id] = this;
153 }
154
155 public static LauncherState[] values() {
156 return Arrays.copyOf(sAllStates, sAllStates.length);
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700157 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700158
159 public float[] getWorkspaceScaleAndTranslation(Launcher launcher) {
Sunny Goyal9328a512017-12-21 12:40:38 -0800160 return new float[] {1, 0, 0};
Sunny Goyalc99cb172017-10-19 16:15:09 -0700161 }
162
Tony Wickhama447bd82017-12-05 14:24:37 -0800163 public float getHoseatAlpha(Launcher launcher) {
164 return 1f;
165 }
166
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700167 public void onStateEnabled(Launcher launcher) {
168 dispatchWindowStateChanged(launcher);
169 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700170
171 public void onStateDisabled(Launcher launcher) { }
Sunny Goyalbe93f262017-10-20 17:05:27 -0700172
173 public View getFinalFocus(Launcher launcher) {
174 return launcher.getWorkspace();
175 }
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700176
Sunny Goyal228153d2018-01-04 15:35:22 -0800177 /**
178 * Fraction shift in the vertical translation UI and related properties
179 *
180 * @see com.android.launcher3.allapps.AllAppsTransitionController
181 */
182 public float getVerticalProgress(Launcher launcher) {
183 return 1f;
184 }
185
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700186 public String getDescription(Launcher launcher) {
187 return launcher.getWorkspace().getCurrentPageDescription();
188 }
189
Sunny Goyalbc683e92017-12-06 10:25:07 -0800190 public PageAlphaProvider getWorkspacePageAlphaProvider(Launcher launcher) {
191 if (this != NORMAL || !launcher.getDeviceProfile().shouldFadeAdjacentWorkspaceScreens()) {
192 return DEFAULT_ALPHA_PROVIDER;
193 }
Sunny Goyal228153d2018-01-04 15:35:22 -0800194 final int centerPage = launcher.getWorkspace().getNextPage();
Sunny Goyal0c723352017-12-12 12:02:29 -0800195 return new PageAlphaProvider(ACCEL_2) {
196 @Override
197 public float getPageAlpha(int pageIndex) {
198 return pageIndex != centerPage ? 0 : 1f;
199 }
200 };
Sunny Goyalbc683e92017-12-06 10:25:07 -0800201 }
202
Sunny Goyal49bcf342018-01-11 13:59:53 -0800203 public LauncherState getHistoryForState(LauncherState previousState) {
204 // No history is supported
205 return NORMAL;
206 }
207
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800208 /**
209 * Called when the start transition ends and the user settles on this particular state.
210 */
211 public void onStateTransitionEnd(Launcher launcher) {
212 if (this == NORMAL) {
213 UiFactory.resetOverview(launcher);
214 }
215 }
216
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700217 protected static void dispatchWindowStateChanged(Launcher launcher) {
218 launcher.getWindow().getDecorView().sendAccessibilityEvent(TYPE_WINDOW_STATE_CHANGED);
219 }
Sunny Goyalbc683e92017-12-06 10:25:07 -0800220
Sunny Goyal0c723352017-12-12 12:02:29 -0800221 public static abstract class PageAlphaProvider {
Sunny Goyalbc683e92017-12-06 10:25:07 -0800222
Sunny Goyal0c723352017-12-12 12:02:29 -0800223 public final Interpolator interpolator;
224
225 public PageAlphaProvider(Interpolator interpolator) {
226 this.interpolator = interpolator;
227 }
228
229 public abstract float getPageAlpha(int pageIndex);
Sunny Goyalbc683e92017-12-06 10:25:07 -0800230 }
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700231}