blob: 472a5a95c22b10ccb1eb96d79810efbee10dc71f [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 Goyal85525172017-11-06 13:00:42 -080029import com.android.launcher3.uioverrides.OverviewState;
Sunny Goyal75c59ac2018-01-15 14:23:11 -080030import com.android.launcher3.uioverrides.UiFactory;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070031import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
32
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070033import java.util.Arrays;
34
Sunny Goyal4c7f2152017-10-17 17:17:16 -070035
36/**
Sunny Goyalbe93f262017-10-20 17:05:27 -070037 * Base state for various states used for the Launcher
Sunny Goyal4c7f2152017-10-17 17:17:16 -070038 */
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070039public class LauncherState {
Sunny Goyal4c7f2152017-10-17 17:17:16 -070040
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070041 protected static final int FLAG_SHOW_SCRIM = 1 << 0;
42 protected static final int FLAG_MULTI_PAGE = 1 << 1;
Sunny Goyalc4fa8c32017-11-07 12:23:58 -080043 protected static final int FLAG_DISABLE_ACCESSIBILITY = 1 << 2;
Sunny Goyalbc683e92017-12-06 10:25:07 -080044 protected static final int FLAG_DISABLE_RESTORE = 1 << 3;
Tony Wickham53370672017-12-05 10:10:55 -080045 protected static final int FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED = 1 << 4;
Sunny Goyalbc683e92017-12-06 10:25:07 -080046 protected static final int FLAG_DISABLE_PAGE_CLIPPING = 1 << 5;
47
Sunny Goyal0c723352017-12-12 12:02:29 -080048 protected static final PageAlphaProvider DEFAULT_ALPHA_PROVIDER =
49 new PageAlphaProvider(ACCEL_2) {
50 @Override
51 public float getPageAlpha(int pageIndex) {
52 return 1;
53 }
54 };
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070055
56 private static final LauncherState[] sAllStates = new LauncherState[4];
57
Sunny Goyal75c59ac2018-01-15 14:23:11 -080058 /**
59 * TODO: Create a separate class for NORMAL state.
60 */
Sunny Goyalc99cb172017-10-19 16:15:09 -070061 public static final LauncherState NORMAL = new LauncherState(0, ContainerType.WORKSPACE,
Sunny Goyal228153d2018-01-04 15:35:22 -080062 0, FLAG_DISABLE_RESTORE | FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED);
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070063
Sunny Goyalbe93f262017-10-20 17:05:27 -070064 public static final LauncherState ALL_APPS = new AllAppsState(1);
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070065
Sunny Goyalc99cb172017-10-19 16:15:09 -070066 public static final LauncherState SPRING_LOADED = new SpringLoadedState(2);
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070067
Sunny Goyal85525172017-11-06 13:00:42 -080068 public static final LauncherState OVERVIEW = new OverviewState(3);
Sunny Goyal4c7f2152017-10-17 17:17:16 -070069
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070070 public final int ordinal;
71
Sunny Goyalf9403d92017-10-18 10:55:56 -070072 /**
73 * Used for containerType in {@link com.android.launcher3.logging.UserEventDispatcher}
74 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -070075 public final int containerType;
76
Sunny Goyalf9403d92017-10-18 10:55:56 -070077 /**
78 * True if the state can be persisted across activity restarts.
79 */
Sunny Goyalbc683e92017-12-06 10:25:07 -080080 public final boolean disableRestore;
Sunny Goyalf9403d92017-10-18 10:55:56 -070081
82 /**
83 * True if workspace has multiple pages visible.
84 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -070085 public final boolean hasMultipleVisiblePages;
Sunny Goyalf9403d92017-10-18 10:55:56 -070086
87 /**
88 * Accessibility flag for workspace and its pages.
89 * @see android.view.View#setImportantForAccessibility(int)
90 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -070091 public final int workspaceAccessibilityFlag;
92
Sunny Goyalbe93f262017-10-20 17:05:27 -070093 /**
94 * Properties related to state transition animation
95 *
96 * @see WorkspaceStateTransitionAnimation
97 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -070098 public final boolean hasScrim;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070099 public final int transitionDuration;
100
Sunny Goyalbe93f262017-10-20 17:05:27 -0700101 /**
Tony Wickham53370672017-12-05 10:10:55 -0800102 * True if the state allows workspace icons to be dragged.
103 */
104 public final boolean workspaceIconsCanBeDragged;
105
Sunny Goyalbc683e92017-12-06 10:25:07 -0800106 /**
107 * True if the workspace pages should not be clipped relative to the workspace bounds
108 * for this state.
109 */
110 public final boolean disablePageClipping;
111
Sunny Goyal228153d2018-01-04 15:35:22 -0800112 public LauncherState(int id, int containerType, int transitionDuration, int flags) {
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700113 this.containerType = containerType;
114 this.transitionDuration = transitionDuration;
115
116 this.hasScrim = (flags & FLAG_SHOW_SCRIM) != 0;
117 this.hasMultipleVisiblePages = (flags & FLAG_MULTI_PAGE) != 0;
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700118 this.workspaceAccessibilityFlag = (flags & FLAG_DISABLE_ACCESSIBILITY) != 0
119 ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
120 : IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Sunny Goyalbc683e92017-12-06 10:25:07 -0800121 this.disableRestore = (flags & FLAG_DISABLE_RESTORE) != 0;
Tony Wickham53370672017-12-05 10:10:55 -0800122 this.workspaceIconsCanBeDragged = (flags & FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED) != 0;
Sunny Goyalbc683e92017-12-06 10:25:07 -0800123 this.disablePageClipping = (flags & FLAG_DISABLE_PAGE_CLIPPING) != 0;
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700124
125 this.ordinal = id;
126 sAllStates[id] = this;
127 }
128
129 public static LauncherState[] values() {
130 return Arrays.copyOf(sAllStates, sAllStates.length);
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700131 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700132
133 public float[] getWorkspaceScaleAndTranslation(Launcher launcher) {
Sunny Goyal9328a512017-12-21 12:40:38 -0800134 return new float[] {1, 0, 0};
Sunny Goyalc99cb172017-10-19 16:15:09 -0700135 }
136
Tony Wickhama447bd82017-12-05 14:24:37 -0800137 public float getHoseatAlpha(Launcher launcher) {
138 return 1f;
139 }
140
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700141 public void onStateEnabled(Launcher launcher) {
142 dispatchWindowStateChanged(launcher);
143 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700144
145 public void onStateDisabled(Launcher launcher) { }
Sunny Goyalbe93f262017-10-20 17:05:27 -0700146
147 public View getFinalFocus(Launcher launcher) {
148 return launcher.getWorkspace();
149 }
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700150
Sunny Goyal228153d2018-01-04 15:35:22 -0800151 /**
152 * Fraction shift in the vertical translation UI and related properties
153 *
154 * @see com.android.launcher3.allapps.AllAppsTransitionController
155 */
156 public float getVerticalProgress(Launcher launcher) {
157 return 1f;
158 }
159
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700160 public String getDescription(Launcher launcher) {
161 return launcher.getWorkspace().getCurrentPageDescription();
162 }
163
Sunny Goyalbc683e92017-12-06 10:25:07 -0800164 public PageAlphaProvider getWorkspacePageAlphaProvider(Launcher launcher) {
165 if (this != NORMAL || !launcher.getDeviceProfile().shouldFadeAdjacentWorkspaceScreens()) {
166 return DEFAULT_ALPHA_PROVIDER;
167 }
Sunny Goyal228153d2018-01-04 15:35:22 -0800168 final int centerPage = launcher.getWorkspace().getNextPage();
Sunny Goyal0c723352017-12-12 12:02:29 -0800169 return new PageAlphaProvider(ACCEL_2) {
170 @Override
171 public float getPageAlpha(int pageIndex) {
172 return pageIndex != centerPage ? 0 : 1f;
173 }
174 };
Sunny Goyalbc683e92017-12-06 10:25:07 -0800175 }
176
Sunny Goyal49bcf342018-01-11 13:59:53 -0800177 public LauncherState getHistoryForState(LauncherState previousState) {
178 // No history is supported
179 return NORMAL;
180 }
181
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800182 /**
183 * Called when the start transition ends and the user settles on this particular state.
184 */
185 public void onStateTransitionEnd(Launcher launcher) {
186 if (this == NORMAL) {
187 UiFactory.resetOverview(launcher);
188 }
189 }
190
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700191 protected static void dispatchWindowStateChanged(Launcher launcher) {
192 launcher.getWindow().getDecorView().sendAccessibilityEvent(TYPE_WINDOW_STATE_CHANGED);
193 }
Sunny Goyalbc683e92017-12-06 10:25:07 -0800194
Sunny Goyal0c723352017-12-12 12:02:29 -0800195 public static abstract class PageAlphaProvider {
Sunny Goyalbc683e92017-12-06 10:25:07 -0800196
Sunny Goyal0c723352017-12-12 12:02:29 -0800197 public final Interpolator interpolator;
198
199 public PageAlphaProvider(Interpolator interpolator) {
200 this.interpolator = interpolator;
201 }
202
203 public abstract float getPageAlpha(int pageIndex);
Sunny Goyalbc683e92017-12-06 10:25:07 -0800204 }
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700205}