blob: 402d73d4983bdfc9d07e2ceab16e768fbc30eba8 [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;
Sunny Goyal6639a5d2018-02-28 15:09:36 -080047 protected static final int FLAG_PAGE_BACKGROUNDS = 1 << 6;
48 protected static final int FLAG_ALL_APPS_SCRIM = 1 << 7;
Sunny Goyalbc683e92017-12-06 10:25:07 -080049
Sunny Goyal0c723352017-12-12 12:02:29 -080050 protected static final PageAlphaProvider DEFAULT_ALPHA_PROVIDER =
51 new PageAlphaProvider(ACCEL_2) {
52 @Override
53 public float getPageAlpha(int pageIndex) {
54 return 1;
55 }
56 };
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070057
58 private static final LauncherState[] sAllStates = new LauncherState[4];
59
Sunny Goyal75c59ac2018-01-15 14:23:11 -080060 /**
61 * TODO: Create a separate class for NORMAL state.
62 */
Sunny Goyalc99cb172017-10-19 16:15:09 -070063 public static final LauncherState NORMAL = new LauncherState(0, ContainerType.WORKSPACE,
Sunny Goyal228153d2018-01-04 15:35:22 -080064 0, FLAG_DISABLE_RESTORE | FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED);
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070065
Sunny Goyalbe93f262017-10-20 17:05:27 -070066 public static final LauncherState ALL_APPS = new AllAppsState(1);
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070067
Sunny Goyalc99cb172017-10-19 16:15:09 -070068 public static final LauncherState SPRING_LOADED = new SpringLoadedState(2);
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070069
Sunny Goyal85525172017-11-06 13:00:42 -080070 public static final LauncherState OVERVIEW = new OverviewState(3);
Sunny Goyal4c7f2152017-10-17 17:17:16 -070071
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070072 public final int ordinal;
73
Sunny Goyalf9403d92017-10-18 10:55:56 -070074 /**
75 * Used for containerType in {@link com.android.launcher3.logging.UserEventDispatcher}
76 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -070077 public final int containerType;
78
Sunny Goyalf9403d92017-10-18 10:55:56 -070079 /**
80 * True if the state can be persisted across activity restarts.
81 */
Sunny Goyalbc683e92017-12-06 10:25:07 -080082 public final boolean disableRestore;
Sunny Goyalf9403d92017-10-18 10:55:56 -070083
84 /**
85 * True if workspace has multiple pages visible.
86 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -070087 public final boolean hasMultipleVisiblePages;
Sunny Goyalf9403d92017-10-18 10:55:56 -070088
89 /**
90 * Accessibility flag for workspace and its pages.
91 * @see android.view.View#setImportantForAccessibility(int)
92 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -070093 public final int workspaceAccessibilityFlag;
94
Sunny Goyalbe93f262017-10-20 17:05:27 -070095 /**
96 * Properties related to state transition animation
97 *
98 * @see WorkspaceStateTransitionAnimation
99 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700100 public final boolean hasScrim;
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800101 public final boolean hasWorkspacePageBackground;
102 public final boolean hasAllAppsScrim;
103
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700104 public final int transitionDuration;
105
Sunny Goyalbe93f262017-10-20 17:05:27 -0700106 /**
Tony Wickham53370672017-12-05 10:10:55 -0800107 * True if the state allows workspace icons to be dragged.
108 */
109 public final boolean workspaceIconsCanBeDragged;
110
Sunny Goyalbc683e92017-12-06 10:25:07 -0800111 /**
112 * True if the workspace pages should not be clipped relative to the workspace bounds
113 * for this state.
114 */
115 public final boolean disablePageClipping;
116
Sunny Goyal228153d2018-01-04 15:35:22 -0800117 public LauncherState(int id, int containerType, int transitionDuration, int flags) {
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700118 this.containerType = containerType;
119 this.transitionDuration = transitionDuration;
120
121 this.hasScrim = (flags & FLAG_SHOW_SCRIM) != 0;
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800122 this.hasWorkspacePageBackground = (flags & FLAG_PAGE_BACKGROUNDS) != 0;
123 this.hasAllAppsScrim = (flags & FLAG_ALL_APPS_SCRIM) != 0;
124
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700125 this.hasMultipleVisiblePages = (flags & FLAG_MULTI_PAGE) != 0;
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700126 this.workspaceAccessibilityFlag = (flags & FLAG_DISABLE_ACCESSIBILITY) != 0
127 ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
128 : IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Sunny Goyalbc683e92017-12-06 10:25:07 -0800129 this.disableRestore = (flags & FLAG_DISABLE_RESTORE) != 0;
Tony Wickham53370672017-12-05 10:10:55 -0800130 this.workspaceIconsCanBeDragged = (flags & FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED) != 0;
Sunny Goyalbc683e92017-12-06 10:25:07 -0800131 this.disablePageClipping = (flags & FLAG_DISABLE_PAGE_CLIPPING) != 0;
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700132
133 this.ordinal = id;
134 sAllStates[id] = this;
135 }
136
137 public static LauncherState[] values() {
138 return Arrays.copyOf(sAllStates, sAllStates.length);
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700139 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700140
141 public float[] getWorkspaceScaleAndTranslation(Launcher launcher) {
Sunny Goyal9328a512017-12-21 12:40:38 -0800142 return new float[] {1, 0, 0};
Sunny Goyalc99cb172017-10-19 16:15:09 -0700143 }
144
Tony Wickhama447bd82017-12-05 14:24:37 -0800145 public float getHoseatAlpha(Launcher launcher) {
146 return 1f;
147 }
148
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700149 public void onStateEnabled(Launcher launcher) {
150 dispatchWindowStateChanged(launcher);
151 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700152
153 public void onStateDisabled(Launcher launcher) { }
Sunny Goyalbe93f262017-10-20 17:05:27 -0700154
155 public View getFinalFocus(Launcher launcher) {
156 return launcher.getWorkspace();
157 }
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700158
Sunny Goyal228153d2018-01-04 15:35:22 -0800159 /**
160 * Fraction shift in the vertical translation UI and related properties
161 *
162 * @see com.android.launcher3.allapps.AllAppsTransitionController
163 */
164 public float getVerticalProgress(Launcher launcher) {
165 return 1f;
166 }
167
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700168 public String getDescription(Launcher launcher) {
169 return launcher.getWorkspace().getCurrentPageDescription();
170 }
171
Sunny Goyalbc683e92017-12-06 10:25:07 -0800172 public PageAlphaProvider getWorkspacePageAlphaProvider(Launcher launcher) {
173 if (this != NORMAL || !launcher.getDeviceProfile().shouldFadeAdjacentWorkspaceScreens()) {
174 return DEFAULT_ALPHA_PROVIDER;
175 }
Sunny Goyal228153d2018-01-04 15:35:22 -0800176 final int centerPage = launcher.getWorkspace().getNextPage();
Sunny Goyal0c723352017-12-12 12:02:29 -0800177 return new PageAlphaProvider(ACCEL_2) {
178 @Override
179 public float getPageAlpha(int pageIndex) {
180 return pageIndex != centerPage ? 0 : 1f;
181 }
182 };
Sunny Goyalbc683e92017-12-06 10:25:07 -0800183 }
184
Sunny Goyal49bcf342018-01-11 13:59:53 -0800185 public LauncherState getHistoryForState(LauncherState previousState) {
186 // No history is supported
187 return NORMAL;
188 }
189
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800190 /**
191 * Called when the start transition ends and the user settles on this particular state.
192 */
193 public void onStateTransitionEnd(Launcher launcher) {
194 if (this == NORMAL) {
195 UiFactory.resetOverview(launcher);
196 }
197 }
198
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700199 protected static void dispatchWindowStateChanged(Launcher launcher) {
200 launcher.getWindow().getDecorView().sendAccessibilityEvent(TYPE_WINDOW_STATE_CHANGED);
201 }
Sunny Goyalbc683e92017-12-06 10:25:07 -0800202
Sunny Goyal0c723352017-12-12 12:02:29 -0800203 public static abstract class PageAlphaProvider {
Sunny Goyalbc683e92017-12-06 10:25:07 -0800204
Sunny Goyal0c723352017-12-12 12:02:29 -0800205 public final Interpolator interpolator;
206
207 public PageAlphaProvider(Interpolator interpolator) {
208 this.interpolator = interpolator;
209 }
210
211 public abstract float getPageAlpha(int pageIndex);
Sunny Goyalbc683e92017-12-06 10:25:07 -0800212 }
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700213}