blob: 6185844cc8ceaabf14e008b0208b46a2efa9be2f [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;
Sunny Goyal623eddd2018-03-02 12:24:41 -080023import static com.android.launcher3.states.RotationHelper.REQUEST_NONE;
Sunny Goyal0c723352017-12-12 12:02:29 -080024
Sunny Goyalbe93f262017-10-20 17:05:27 -070025import android.view.View;
Sunny Goyal0c723352017-12-12 12:02:29 -080026import android.view.animation.Interpolator;
Sunny Goyalbe93f262017-10-20 17:05:27 -070027
Sunny Goyalc99cb172017-10-19 16:15:09 -070028import com.android.launcher3.states.SpringLoadedState;
Sunny Goyal623eddd2018-03-02 12:24:41 -080029import com.android.launcher3.uioverrides.AllAppsState;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -080030import com.android.launcher3.uioverrides.FastOverviewState;
Sunny Goyal85525172017-11-06 13:00:42 -080031import com.android.launcher3.uioverrides.OverviewState;
Sunny Goyal75c59ac2018-01-15 14:23:11 -080032import com.android.launcher3.uioverrides.UiFactory;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070033import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
34
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070035import java.util.Arrays;
36
Sunny Goyal4c7f2152017-10-17 17:17:16 -070037
38/**
Sunny Goyalbe93f262017-10-20 17:05:27 -070039 * Base state for various states used for the Launcher
Sunny Goyal4c7f2152017-10-17 17:17:16 -070040 */
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070041public class LauncherState {
Sunny Goyal4c7f2152017-10-17 17:17:16 -070042
Sunny Goyal7185dd62018-03-14 17:51:49 -070043
44 /**
45 * Set of elements indicating various workspace elements which change visibility across states
46 * Note that workspace is not included here as in that case, we animate individual pages
47 */
48 public static final int NONE = 0;
Tony Wickhambd6f05e2018-03-21 08:16:33 -070049 public static final int HOTSEAT_ICONS = 1 << 0;
50 public static final int HOTSEAT_EXTRA = 1 << 1; // e.g. a search box
51 public static final int ALL_APPS_HEADER = 1 << 2;
52 public static final int ALL_APPS_HEADER_EXTRA = 1 << 3; // e.g. app predictions
53 public static final int ALL_APPS_CONTENT = 1 << 4;
Sunny Goyal7185dd62018-03-14 17:51:49 -070054
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070055 protected static final int FLAG_SHOW_SCRIM = 1 << 0;
56 protected static final int FLAG_MULTI_PAGE = 1 << 1;
Sunny Goyalc4fa8c32017-11-07 12:23:58 -080057 protected static final int FLAG_DISABLE_ACCESSIBILITY = 1 << 2;
Sunny Goyalbc683e92017-12-06 10:25:07 -080058 protected static final int FLAG_DISABLE_RESTORE = 1 << 3;
Tony Wickham53370672017-12-05 10:10:55 -080059 protected static final int FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED = 1 << 4;
Sunny Goyalbc683e92017-12-06 10:25:07 -080060 protected static final int FLAG_DISABLE_PAGE_CLIPPING = 1 << 5;
Sunny Goyal6639a5d2018-02-28 15:09:36 -080061 protected static final int FLAG_PAGE_BACKGROUNDS = 1 << 6;
62 protected static final int FLAG_ALL_APPS_SCRIM = 1 << 7;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -080063 protected static final int FLAG_DISABLE_INTERACTION = 1 << 8;
64 protected static final int FLAG_OVERVIEW_UI = 1 << 9;
Tony Wickham76cf2362018-03-20 14:13:16 -070065 protected static final int FLAG_HIDE_BACK_BUTTON = 1 << 10;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -080066
Sunny Goyal0c723352017-12-12 12:02:29 -080067 protected static final PageAlphaProvider DEFAULT_ALPHA_PROVIDER =
68 new PageAlphaProvider(ACCEL_2) {
69 @Override
70 public float getPageAlpha(int pageIndex) {
71 return 1;
72 }
73 };
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070074
Sunny Goyal6c6c2f42018-03-02 14:57:46 -080075 private static final LauncherState[] sAllStates = new LauncherState[5];
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070076
Sunny Goyal75c59ac2018-01-15 14:23:11 -080077 /**
78 * TODO: Create a separate class for NORMAL state.
79 */
Sunny Goyalc99cb172017-10-19 16:15:09 -070080 public static final LauncherState NORMAL = new LauncherState(0, ContainerType.WORKSPACE,
Tony Wickham76cf2362018-03-20 14:13:16 -070081 0, FLAG_DISABLE_RESTORE | FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED | FLAG_HIDE_BACK_BUTTON);
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070082
Sunny Goyal7185dd62018-03-14 17:51:49 -070083 /**
84 * Various Launcher states arranged in the increasing order of UI layers
85 */
86 public static final LauncherState SPRING_LOADED = new SpringLoadedState(1);
87 public static final LauncherState OVERVIEW = new OverviewState(2);
88 public static final LauncherState FAST_OVERVIEW = new FastOverviewState(3);
89 public static final LauncherState ALL_APPS = new AllAppsState(4);
Sunny Goyal6c6c2f42018-03-02 14:57:46 -080090
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070091 public final int ordinal;
92
Sunny Goyalf9403d92017-10-18 10:55:56 -070093 /**
94 * Used for containerType in {@link com.android.launcher3.logging.UserEventDispatcher}
95 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -070096 public final int containerType;
97
Sunny Goyalf9403d92017-10-18 10:55:56 -070098 /**
99 * True if the state can be persisted across activity restarts.
100 */
Sunny Goyalbc683e92017-12-06 10:25:07 -0800101 public final boolean disableRestore;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700102
103 /**
104 * True if workspace has multiple pages visible.
105 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700106 public final boolean hasMultipleVisiblePages;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700107
108 /**
109 * Accessibility flag for workspace and its pages.
110 * @see android.view.View#setImportantForAccessibility(int)
111 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700112 public final int workspaceAccessibilityFlag;
113
Sunny Goyalbe93f262017-10-20 17:05:27 -0700114 /**
115 * Properties related to state transition animation
116 *
117 * @see WorkspaceStateTransitionAnimation
118 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700119 public final boolean hasScrim;
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800120 public final boolean hasWorkspacePageBackground;
121 public final boolean hasAllAppsScrim;
122
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700123 public final int transitionDuration;
124
Sunny Goyalbe93f262017-10-20 17:05:27 -0700125 /**
Tony Wickham53370672017-12-05 10:10:55 -0800126 * True if the state allows workspace icons to be dragged.
127 */
128 public final boolean workspaceIconsCanBeDragged;
129
Sunny Goyalbc683e92017-12-06 10:25:07 -0800130 /**
131 * True if the workspace pages should not be clipped relative to the workspace bounds
132 * for this state.
133 */
134 public final boolean disablePageClipping;
135
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800136 /**
137 * True if launcher can not be directly interacted in this state;
138 */
139 public final boolean disableInteraction;
140
141 /**
142 * True if the state has overview panel visible.
143 */
144 public final boolean overviewUi;
145
Tony Wickham76cf2362018-03-20 14:13:16 -0700146 /**
147 * True if the back button should be hidden when in this state (assuming no floating views are
148 * open, launcher has window focus, etc).
149 */
150 public final boolean hideBackButton;
151
Sunny Goyal228153d2018-01-04 15:35:22 -0800152 public LauncherState(int id, int containerType, int transitionDuration, int flags) {
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700153 this.containerType = containerType;
154 this.transitionDuration = transitionDuration;
155
156 this.hasScrim = (flags & FLAG_SHOW_SCRIM) != 0;
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800157 this.hasWorkspacePageBackground = (flags & FLAG_PAGE_BACKGROUNDS) != 0;
158 this.hasAllAppsScrim = (flags & FLAG_ALL_APPS_SCRIM) != 0;
159
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700160 this.hasMultipleVisiblePages = (flags & FLAG_MULTI_PAGE) != 0;
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700161 this.workspaceAccessibilityFlag = (flags & FLAG_DISABLE_ACCESSIBILITY) != 0
162 ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
163 : IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Sunny Goyalbc683e92017-12-06 10:25:07 -0800164 this.disableRestore = (flags & FLAG_DISABLE_RESTORE) != 0;
Tony Wickham53370672017-12-05 10:10:55 -0800165 this.workspaceIconsCanBeDragged = (flags & FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED) != 0;
Sunny Goyalbc683e92017-12-06 10:25:07 -0800166 this.disablePageClipping = (flags & FLAG_DISABLE_PAGE_CLIPPING) != 0;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800167 this.disableInteraction = (flags & FLAG_DISABLE_INTERACTION) != 0;
168 this.overviewUi = (flags & FLAG_OVERVIEW_UI) != 0;
Tony Wickham76cf2362018-03-20 14:13:16 -0700169 this.hideBackButton = (flags & FLAG_HIDE_BACK_BUTTON) != 0;
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700170
171 this.ordinal = id;
172 sAllStates[id] = this;
173 }
174
175 public static LauncherState[] values() {
176 return Arrays.copyOf(sAllStates, sAllStates.length);
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700177 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700178
179 public float[] getWorkspaceScaleAndTranslation(Launcher launcher) {
Sunny Goyal9328a512017-12-21 12:40:38 -0800180 return new float[] {1, 0, 0};
Sunny Goyalc99cb172017-10-19 16:15:09 -0700181 }
182
Tony3bb5e8e2018-03-14 12:05:00 +0000183 public float getOverviewTranslationX(Launcher launcher) {
184 return launcher.getDragLayer().getMeasuredWidth();
185 }
186
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700187 public void onStateEnabled(Launcher launcher) {
188 dispatchWindowStateChanged(launcher);
189 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700190
191 public void onStateDisabled(Launcher launcher) { }
Sunny Goyalbe93f262017-10-20 17:05:27 -0700192
193 public View getFinalFocus(Launcher launcher) {
194 return launcher.getWorkspace();
195 }
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700196
Sunny Goyal7185dd62018-03-14 17:51:49 -0700197 public int getVisibleElements(Launcher launcher) {
Tony Wickhambd6f05e2018-03-21 08:16:33 -0700198 if (launcher.getDeviceProfile().isVerticalBarLayout()) {
199 return HOTSEAT_ICONS;
200 }
201 return HOTSEAT_ICONS | HOTSEAT_EXTRA;
Sunny Goyal7185dd62018-03-14 17:51:49 -0700202 }
203
Sunny Goyal228153d2018-01-04 15:35:22 -0800204 /**
205 * Fraction shift in the vertical translation UI and related properties
206 *
207 * @see com.android.launcher3.allapps.AllAppsTransitionController
208 */
209 public float getVerticalProgress(Launcher launcher) {
210 return 1f;
211 }
212
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700213 public String getDescription(Launcher launcher) {
214 return launcher.getWorkspace().getCurrentPageDescription();
215 }
216
Sunny Goyalbc683e92017-12-06 10:25:07 -0800217 public PageAlphaProvider getWorkspacePageAlphaProvider(Launcher launcher) {
218 if (this != NORMAL || !launcher.getDeviceProfile().shouldFadeAdjacentWorkspaceScreens()) {
219 return DEFAULT_ALPHA_PROVIDER;
220 }
Sunny Goyal228153d2018-01-04 15:35:22 -0800221 final int centerPage = launcher.getWorkspace().getNextPage();
Sunny Goyal0c723352017-12-12 12:02:29 -0800222 return new PageAlphaProvider(ACCEL_2) {
223 @Override
224 public float getPageAlpha(int pageIndex) {
225 return pageIndex != centerPage ? 0 : 1f;
226 }
227 };
Sunny Goyalbc683e92017-12-06 10:25:07 -0800228 }
229
Sunny Goyal49bcf342018-01-11 13:59:53 -0800230 public LauncherState getHistoryForState(LauncherState previousState) {
231 // No history is supported
232 return NORMAL;
233 }
234
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800235 /**
236 * Called when the start transition ends and the user settles on this particular state.
237 */
238 public void onStateTransitionEnd(Launcher launcher) {
239 if (this == NORMAL) {
240 UiFactory.resetOverview(launcher);
Sunny Goyal623eddd2018-03-02 12:24:41 -0800241 // Clear any rotation locks when going to normal state
242 launcher.getRotationHelper().setCurrentStateRequest(REQUEST_NONE);
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800243 }
244 }
245
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700246 protected static void dispatchWindowStateChanged(Launcher launcher) {
247 launcher.getWindow().getDecorView().sendAccessibilityEvent(TYPE_WINDOW_STATE_CHANGED);
248 }
Sunny Goyalbc683e92017-12-06 10:25:07 -0800249
Sunny Goyal0c723352017-12-12 12:02:29 -0800250 public static abstract class PageAlphaProvider {
Sunny Goyalbc683e92017-12-06 10:25:07 -0800251
Sunny Goyal0c723352017-12-12 12:02:29 -0800252 public final Interpolator interpolator;
253
254 public PageAlphaProvider(Interpolator interpolator) {
255 this.interpolator = interpolator;
256 }
257
258 public abstract float getPageAlpha(int pageIndex);
Sunny Goyalbc683e92017-12-06 10:25:07 -0800259 }
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700260}