blob: a47d7f468e958d420b5da0f69c57c399da3c4c75 [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 Goyal0c723352017-12-12 12:02:29 -080021import static com.android.launcher3.anim.Interpolators.ACCEL_2;
Sunny Goyal623eddd2018-03-02 12:24:41 -080022import static com.android.launcher3.states.RotationHelper.REQUEST_NONE;
Sunny Goyal0c723352017-12-12 12:02:29 -080023
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 Goyalc99cb172017-10-19 16:15:09 -070027import com.android.launcher3.states.SpringLoadedState;
Sunny Goyal623eddd2018-03-02 12:24:41 -080028import com.android.launcher3.uioverrides.AllAppsState;
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 Goyal7185dd62018-03-14 17:51:49 -070042
43 /**
44 * Set of elements indicating various workspace elements which change visibility across states
45 * Note that workspace is not included here as in that case, we animate individual pages
46 */
47 public static final int NONE = 0;
Tony Wickhambd6f05e2018-03-21 08:16:33 -070048 public static final int HOTSEAT_ICONS = 1 << 0;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -070049 public static final int HOTSEAT_SEARCH_BOX = 1 << 1;
Tony Wickhambd6f05e2018-03-21 08:16:33 -070050 public static final int ALL_APPS_HEADER = 1 << 2;
51 public static final int ALL_APPS_HEADER_EXTRA = 1 << 3; // e.g. app predictions
52 public static final int ALL_APPS_CONTENT = 1 << 4;
Sunny Goyal7185dd62018-03-14 17:51:49 -070053
Sunny Goyal927447e2018-05-04 13:19:29 -070054 protected static final int FLAG_MULTI_PAGE = 1 << 0;
55 protected static final int FLAG_DISABLE_ACCESSIBILITY = 1 << 1;
56 protected static final int FLAG_DISABLE_RESTORE = 1 << 2;
57 protected static final int FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED = 1 << 3;
58 protected static final int FLAG_DISABLE_PAGE_CLIPPING = 1 << 4;
59 protected static final int FLAG_PAGE_BACKGROUNDS = 1 << 5;
60 protected static final int FLAG_DISABLE_INTERACTION = 1 << 6;
61 protected static final int FLAG_OVERVIEW_UI = 1 << 7;
62 protected static final int FLAG_HIDE_BACK_BUTTON = 1 << 8;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -080063
Sunny Goyal0c723352017-12-12 12:02:29 -080064 protected static final PageAlphaProvider DEFAULT_ALPHA_PROVIDER =
65 new PageAlphaProvider(ACCEL_2) {
66 @Override
67 public float getPageAlpha(int pageIndex) {
68 return 1;
69 }
70 };
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070071
Sunny Goyal6c6c2f42018-03-02 14:57:46 -080072 private static final LauncherState[] sAllStates = new LauncherState[5];
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070073
Sunny Goyal75c59ac2018-01-15 14:23:11 -080074 /**
75 * TODO: Create a separate class for NORMAL state.
76 */
Sunny Goyalc99cb172017-10-19 16:15:09 -070077 public static final LauncherState NORMAL = new LauncherState(0, ContainerType.WORKSPACE,
Tony Wickham76cf2362018-03-20 14:13:16 -070078 0, FLAG_DISABLE_RESTORE | FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED | FLAG_HIDE_BACK_BUTTON);
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070079
Sunny Goyal7185dd62018-03-14 17:51:49 -070080 /**
81 * Various Launcher states arranged in the increasing order of UI layers
82 */
83 public static final LauncherState SPRING_LOADED = new SpringLoadedState(1);
84 public static final LauncherState OVERVIEW = new OverviewState(2);
85 public static final LauncherState FAST_OVERVIEW = new FastOverviewState(3);
86 public static final LauncherState ALL_APPS = new AllAppsState(4);
Sunny Goyal6c6c2f42018-03-02 14:57:46 -080087
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070088 public final int ordinal;
89
Sunny Goyalf9403d92017-10-18 10:55:56 -070090 /**
91 * Used for containerType in {@link com.android.launcher3.logging.UserEventDispatcher}
92 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -070093 public final int containerType;
94
Sunny Goyalf9403d92017-10-18 10:55:56 -070095 /**
96 * True if the state can be persisted across activity restarts.
97 */
Sunny Goyalbc683e92017-12-06 10:25:07 -080098 public final boolean disableRestore;
Sunny Goyalf9403d92017-10-18 10:55:56 -070099
100 /**
101 * True if workspace has multiple pages visible.
102 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700103 public final boolean hasMultipleVisiblePages;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700104
105 /**
106 * Accessibility flag for workspace and its pages.
107 * @see android.view.View#setImportantForAccessibility(int)
108 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700109 public final int workspaceAccessibilityFlag;
110
Sunny Goyalbe93f262017-10-20 17:05:27 -0700111 /**
112 * Properties related to state transition animation
113 *
114 * @see WorkspaceStateTransitionAnimation
115 */
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800116 public final boolean hasWorkspacePageBackground;
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800117
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700118 public final int transitionDuration;
119
Sunny Goyalbe93f262017-10-20 17:05:27 -0700120 /**
Tony Wickham53370672017-12-05 10:10:55 -0800121 * True if the state allows workspace icons to be dragged.
122 */
123 public final boolean workspaceIconsCanBeDragged;
124
Sunny Goyalbc683e92017-12-06 10:25:07 -0800125 /**
126 * True if the workspace pages should not be clipped relative to the workspace bounds
127 * for this state.
128 */
129 public final boolean disablePageClipping;
130
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800131 /**
132 * True if launcher can not be directly interacted in this state;
133 */
134 public final boolean disableInteraction;
135
136 /**
137 * True if the state has overview panel visible.
138 */
139 public final boolean overviewUi;
140
Tony Wickham76cf2362018-03-20 14:13:16 -0700141 /**
142 * True if the back button should be hidden when in this state (assuming no floating views are
143 * open, launcher has window focus, etc).
144 */
145 public final boolean hideBackButton;
146
Sunny Goyal228153d2018-01-04 15:35:22 -0800147 public LauncherState(int id, int containerType, int transitionDuration, int flags) {
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700148 this.containerType = containerType;
149 this.transitionDuration = transitionDuration;
150
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800151 this.hasWorkspacePageBackground = (flags & FLAG_PAGE_BACKGROUNDS) != 0;
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700152 this.hasMultipleVisiblePages = (flags & FLAG_MULTI_PAGE) != 0;
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700153 this.workspaceAccessibilityFlag = (flags & FLAG_DISABLE_ACCESSIBILITY) != 0
154 ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
155 : IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Sunny Goyalbc683e92017-12-06 10:25:07 -0800156 this.disableRestore = (flags & FLAG_DISABLE_RESTORE) != 0;
Tony Wickham53370672017-12-05 10:10:55 -0800157 this.workspaceIconsCanBeDragged = (flags & FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED) != 0;
Sunny Goyalbc683e92017-12-06 10:25:07 -0800158 this.disablePageClipping = (flags & FLAG_DISABLE_PAGE_CLIPPING) != 0;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800159 this.disableInteraction = (flags & FLAG_DISABLE_INTERACTION) != 0;
160 this.overviewUi = (flags & FLAG_OVERVIEW_UI) != 0;
Tony Wickham76cf2362018-03-20 14:13:16 -0700161 this.hideBackButton = (flags & FLAG_HIDE_BACK_BUTTON) != 0;
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700162
163 this.ordinal = id;
164 sAllStates[id] = this;
165 }
166
167 public static LauncherState[] values() {
168 return Arrays.copyOf(sAllStates, sAllStates.length);
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700169 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700170
171 public float[] getWorkspaceScaleAndTranslation(Launcher launcher) {
Sunny Goyal9328a512017-12-21 12:40:38 -0800172 return new float[] {1, 0, 0};
Sunny Goyalc99cb172017-10-19 16:15:09 -0700173 }
174
Tonye9054e32018-03-15 12:48:51 +0000175 /**
Tony0c955592018-04-04 10:23:36 -0700176 * Returns 2 floats designating how to transition overview:
177 * scale for the current and adjacent pages
178 * translationY factor where 0 is top aligned and 0.5 is centered vertically
Tonye9054e32018-03-15 12:48:51 +0000179 */
Tony0c955592018-04-04 10:23:36 -0700180 public float[] getOverviewScaleAndTranslationYFactor(Launcher launcher) {
181 return new float[] {1.2f, 0.2f};
Tony3bb5e8e2018-03-14 12:05:00 +0000182 }
183
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700184 public void onStateEnabled(Launcher launcher) {
185 dispatchWindowStateChanged(launcher);
186 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700187
188 public void onStateDisabled(Launcher launcher) { }
Sunny Goyalbe93f262017-10-20 17:05:27 -0700189
190 public View getFinalFocus(Launcher launcher) {
191 return launcher.getWorkspace();
192 }
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700193
Sunny Goyal7185dd62018-03-14 17:51:49 -0700194 public int getVisibleElements(Launcher launcher) {
Tony Wickhambd6f05e2018-03-21 08:16:33 -0700195 if (launcher.getDeviceProfile().isVerticalBarLayout()) {
Sunny Goyalef395402018-05-09 08:42:24 -0700196 return HOTSEAT_ICONS;
Tony Wickhambd6f05e2018-03-21 08:16:33 -0700197 }
Sunny Goyalef395402018-05-09 08:42:24 -0700198 return HOTSEAT_ICONS | HOTSEAT_SEARCH_BOX;
Sunny Goyal7185dd62018-03-14 17:51:49 -0700199 }
200
Sunny Goyal228153d2018-01-04 15:35:22 -0800201 /**
202 * Fraction shift in the vertical translation UI and related properties
203 *
204 * @see com.android.launcher3.allapps.AllAppsTransitionController
205 */
206 public float getVerticalProgress(Launcher launcher) {
207 return 1f;
208 }
209
Sunny Goyal927447e2018-05-04 13:19:29 -0700210 public float getWorkspaceScrimAlpha(Launcher launcher) {
211 return 0;
212 }
213
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700214 public String getDescription(Launcher launcher) {
215 return launcher.getWorkspace().getCurrentPageDescription();
216 }
217
Sunny Goyalbc683e92017-12-06 10:25:07 -0800218 public PageAlphaProvider getWorkspacePageAlphaProvider(Launcher launcher) {
219 if (this != NORMAL || !launcher.getDeviceProfile().shouldFadeAdjacentWorkspaceScreens()) {
220 return DEFAULT_ALPHA_PROVIDER;
221 }
Sunny Goyal228153d2018-01-04 15:35:22 -0800222 final int centerPage = launcher.getWorkspace().getNextPage();
Sunny Goyal0c723352017-12-12 12:02:29 -0800223 return new PageAlphaProvider(ACCEL_2) {
224 @Override
225 public float getPageAlpha(int pageIndex) {
226 return pageIndex != centerPage ? 0 : 1f;
227 }
228 };
Sunny Goyalbc683e92017-12-06 10:25:07 -0800229 }
230
Sunny Goyal49bcf342018-01-11 13:59:53 -0800231 public LauncherState getHistoryForState(LauncherState previousState) {
232 // No history is supported
233 return NORMAL;
234 }
235
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800236 /**
237 * Called when the start transition ends and the user settles on this particular state.
238 */
239 public void onStateTransitionEnd(Launcher launcher) {
240 if (this == NORMAL) {
241 UiFactory.resetOverview(launcher);
Sunny Goyal623eddd2018-03-02 12:24:41 -0800242 // Clear any rotation locks when going to normal state
243 launcher.getRotationHelper().setCurrentStateRequest(REQUEST_NONE);
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800244 }
245 }
246
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700247 protected static void dispatchWindowStateChanged(Launcher launcher) {
248 launcher.getWindow().getDecorView().sendAccessibilityEvent(TYPE_WINDOW_STATE_CHANGED);
249 }
Sunny Goyalbc683e92017-12-06 10:25:07 -0800250
Sunny Goyal0c723352017-12-12 12:02:29 -0800251 public static abstract class PageAlphaProvider {
Sunny Goyalbc683e92017-12-06 10:25:07 -0800252
Sunny Goyal0c723352017-12-12 12:02:29 -0800253 public final Interpolator interpolator;
254
255 public PageAlphaProvider(Interpolator interpolator) {
256 this.interpolator = interpolator;
257 }
258
259 public abstract float getPageAlpha(int pageIndex);
Sunny Goyalbc683e92017-12-06 10:25:07 -0800260 }
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700261}