blob: 6790b7f0c389c6b24377858292558409930d7929 [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
Tony Wickham6becf7c2018-04-19 11:39:34 -070024import android.graphics.Rect;
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;
Sunny Goyal81b4c7b2018-03-26 12:10:31 -070050 public static final int HOTSEAT_SEARCH_BOX = 1 << 1;
Tony Wickhambd6f05e2018-03-21 08:16:33 -070051 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 Goyal81b4c7b2018-03-26 12:10:31 -070054 public static final int DRAG_HANDLE_INDICATOR = 1 << 5;
Sunny Goyal7185dd62018-03-14 17:51:49 -070055
Sunny Goyal927447e2018-05-04 13:19:29 -070056 protected static final int FLAG_MULTI_PAGE = 1 << 0;
57 protected static final int FLAG_DISABLE_ACCESSIBILITY = 1 << 1;
58 protected static final int FLAG_DISABLE_RESTORE = 1 << 2;
59 protected static final int FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED = 1 << 3;
60 protected static final int FLAG_DISABLE_PAGE_CLIPPING = 1 << 4;
61 protected static final int FLAG_PAGE_BACKGROUNDS = 1 << 5;
62 protected static final int FLAG_DISABLE_INTERACTION = 1 << 6;
63 protected static final int FLAG_OVERVIEW_UI = 1 << 7;
64 protected static final int FLAG_HIDE_BACK_BUTTON = 1 << 8;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -080065
Sunny Goyal0c723352017-12-12 12:02:29 -080066 protected static final PageAlphaProvider DEFAULT_ALPHA_PROVIDER =
67 new PageAlphaProvider(ACCEL_2) {
68 @Override
69 public float getPageAlpha(int pageIndex) {
70 return 1;
71 }
72 };
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070073
Sunny Goyal6c6c2f42018-03-02 14:57:46 -080074 private static final LauncherState[] sAllStates = new LauncherState[5];
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070075
Sunny Goyal75c59ac2018-01-15 14:23:11 -080076 /**
77 * TODO: Create a separate class for NORMAL state.
78 */
Sunny Goyalc99cb172017-10-19 16:15:09 -070079 public static final LauncherState NORMAL = new LauncherState(0, ContainerType.WORKSPACE,
Tony Wickham76cf2362018-03-20 14:13:16 -070080 0, FLAG_DISABLE_RESTORE | FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED | FLAG_HIDE_BACK_BUTTON);
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070081
Sunny Goyal7185dd62018-03-14 17:51:49 -070082 /**
83 * Various Launcher states arranged in the increasing order of UI layers
84 */
85 public static final LauncherState SPRING_LOADED = new SpringLoadedState(1);
86 public static final LauncherState OVERVIEW = new OverviewState(2);
87 public static final LauncherState FAST_OVERVIEW = new FastOverviewState(3);
88 public static final LauncherState ALL_APPS = new AllAppsState(4);
Sunny Goyal6c6c2f42018-03-02 14:57:46 -080089
Tony Wickham6becf7c2018-04-19 11:39:34 -070090 protected static final Rect sTempRect = new Rect();
91
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070092 public final int ordinal;
93
Sunny Goyalf9403d92017-10-18 10:55:56 -070094 /**
95 * Used for containerType in {@link com.android.launcher3.logging.UserEventDispatcher}
96 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -070097 public final int containerType;
98
Sunny Goyalf9403d92017-10-18 10:55:56 -070099 /**
100 * True if the state can be persisted across activity restarts.
101 */
Sunny Goyalbc683e92017-12-06 10:25:07 -0800102 public final boolean disableRestore;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700103
104 /**
105 * True if workspace has multiple pages visible.
106 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700107 public final boolean hasMultipleVisiblePages;
Sunny Goyalf9403d92017-10-18 10:55:56 -0700108
109 /**
110 * Accessibility flag for workspace and its pages.
111 * @see android.view.View#setImportantForAccessibility(int)
112 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700113 public final int workspaceAccessibilityFlag;
114
Sunny Goyalbe93f262017-10-20 17:05:27 -0700115 /**
116 * Properties related to state transition animation
117 *
118 * @see WorkspaceStateTransitionAnimation
119 */
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800120 public final boolean hasWorkspacePageBackground;
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800121
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700122 public final int transitionDuration;
123
Sunny Goyalbe93f262017-10-20 17:05:27 -0700124 /**
Tony Wickham53370672017-12-05 10:10:55 -0800125 * True if the state allows workspace icons to be dragged.
126 */
127 public final boolean workspaceIconsCanBeDragged;
128
Sunny Goyalbc683e92017-12-06 10:25:07 -0800129 /**
130 * True if the workspace pages should not be clipped relative to the workspace bounds
131 * for this state.
132 */
133 public final boolean disablePageClipping;
134
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800135 /**
136 * True if launcher can not be directly interacted in this state;
137 */
138 public final boolean disableInteraction;
139
140 /**
141 * True if the state has overview panel visible.
142 */
143 public final boolean overviewUi;
144
Tony Wickham76cf2362018-03-20 14:13:16 -0700145 /**
146 * True if the back button should be hidden when in this state (assuming no floating views are
147 * open, launcher has window focus, etc).
148 */
149 public final boolean hideBackButton;
150
Sunny Goyal228153d2018-01-04 15:35:22 -0800151 public LauncherState(int id, int containerType, int transitionDuration, int flags) {
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700152 this.containerType = containerType;
153 this.transitionDuration = transitionDuration;
154
Sunny Goyal6639a5d2018-02-28 15:09:36 -0800155 this.hasWorkspacePageBackground = (flags & FLAG_PAGE_BACKGROUNDS) != 0;
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700156 this.hasMultipleVisiblePages = (flags & FLAG_MULTI_PAGE) != 0;
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700157 this.workspaceAccessibilityFlag = (flags & FLAG_DISABLE_ACCESSIBILITY) != 0
158 ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
159 : IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Sunny Goyalbc683e92017-12-06 10:25:07 -0800160 this.disableRestore = (flags & FLAG_DISABLE_RESTORE) != 0;
Tony Wickham53370672017-12-05 10:10:55 -0800161 this.workspaceIconsCanBeDragged = (flags & FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED) != 0;
Sunny Goyalbc683e92017-12-06 10:25:07 -0800162 this.disablePageClipping = (flags & FLAG_DISABLE_PAGE_CLIPPING) != 0;
Sunny Goyal6c6c2f42018-03-02 14:57:46 -0800163 this.disableInteraction = (flags & FLAG_DISABLE_INTERACTION) != 0;
164 this.overviewUi = (flags & FLAG_OVERVIEW_UI) != 0;
Tony Wickham76cf2362018-03-20 14:13:16 -0700165 this.hideBackButton = (flags & FLAG_HIDE_BACK_BUTTON) != 0;
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700166
167 this.ordinal = id;
168 sAllStates[id] = this;
169 }
170
171 public static LauncherState[] values() {
172 return Arrays.copyOf(sAllStates, sAllStates.length);
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700173 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700174
175 public float[] getWorkspaceScaleAndTranslation(Launcher launcher) {
Sunny Goyal9328a512017-12-21 12:40:38 -0800176 return new float[] {1, 0, 0};
Sunny Goyalc99cb172017-10-19 16:15:09 -0700177 }
178
Tonye9054e32018-03-15 12:48:51 +0000179 /**
Tony0c955592018-04-04 10:23:36 -0700180 * Returns 2 floats designating how to transition overview:
181 * scale for the current and adjacent pages
182 * translationY factor where 0 is top aligned and 0.5 is centered vertically
Tonye9054e32018-03-15 12:48:51 +0000183 */
Tony0c955592018-04-04 10:23:36 -0700184 public float[] getOverviewScaleAndTranslationYFactor(Launcher launcher) {
185 return new float[] {1.2f, 0.2f};
Tony3bb5e8e2018-03-14 12:05:00 +0000186 }
187
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700188 public void onStateEnabled(Launcher launcher) {
189 dispatchWindowStateChanged(launcher);
190 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700191
192 public void onStateDisabled(Launcher launcher) { }
Sunny Goyalbe93f262017-10-20 17:05:27 -0700193
194 public View getFinalFocus(Launcher launcher) {
195 return launcher.getWorkspace();
196 }
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700197
Sunny Goyal7185dd62018-03-14 17:51:49 -0700198 public int getVisibleElements(Launcher launcher) {
Tony Wickhambd6f05e2018-03-21 08:16:33 -0700199 if (launcher.getDeviceProfile().isVerticalBarLayout()) {
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700200 return HOTSEAT_ICONS | DRAG_HANDLE_INDICATOR;
Tony Wickhambd6f05e2018-03-21 08:16:33 -0700201 }
Sunny Goyal81b4c7b2018-03-26 12:10:31 -0700202 return HOTSEAT_ICONS | DRAG_HANDLE_INDICATOR | HOTSEAT_SEARCH_BOX;
Sunny Goyal7185dd62018-03-14 17:51:49 -0700203 }
204
Sunny Goyal228153d2018-01-04 15:35:22 -0800205 /**
206 * Fraction shift in the vertical translation UI and related properties
207 *
208 * @see com.android.launcher3.allapps.AllAppsTransitionController
209 */
210 public float getVerticalProgress(Launcher launcher) {
211 return 1f;
212 }
213
Sunny Goyal927447e2018-05-04 13:19:29 -0700214 public float getWorkspaceScrimAlpha(Launcher launcher) {
215 return 0;
216 }
217
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700218 public String getDescription(Launcher launcher) {
219 return launcher.getWorkspace().getCurrentPageDescription();
220 }
221
Sunny Goyalbc683e92017-12-06 10:25:07 -0800222 public PageAlphaProvider getWorkspacePageAlphaProvider(Launcher launcher) {
223 if (this != NORMAL || !launcher.getDeviceProfile().shouldFadeAdjacentWorkspaceScreens()) {
224 return DEFAULT_ALPHA_PROVIDER;
225 }
Sunny Goyal228153d2018-01-04 15:35:22 -0800226 final int centerPage = launcher.getWorkspace().getNextPage();
Sunny Goyal0c723352017-12-12 12:02:29 -0800227 return new PageAlphaProvider(ACCEL_2) {
228 @Override
229 public float getPageAlpha(int pageIndex) {
230 return pageIndex != centerPage ? 0 : 1f;
231 }
232 };
Sunny Goyalbc683e92017-12-06 10:25:07 -0800233 }
234
Sunny Goyal49bcf342018-01-11 13:59:53 -0800235 public LauncherState getHistoryForState(LauncherState previousState) {
236 // No history is supported
237 return NORMAL;
238 }
239
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800240 /**
241 * Called when the start transition ends and the user settles on this particular state.
242 */
243 public void onStateTransitionEnd(Launcher launcher) {
244 if (this == NORMAL) {
245 UiFactory.resetOverview(launcher);
Sunny Goyal623eddd2018-03-02 12:24:41 -0800246 // Clear any rotation locks when going to normal state
247 launcher.getRotationHelper().setCurrentStateRequest(REQUEST_NONE);
Sunny Goyal75c59ac2018-01-15 14:23:11 -0800248 }
249 }
250
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700251 protected static void dispatchWindowStateChanged(Launcher launcher) {
252 launcher.getWindow().getDecorView().sendAccessibilityEvent(TYPE_WINDOW_STATE_CHANGED);
253 }
Sunny Goyalbc683e92017-12-06 10:25:07 -0800254
Sunny Goyal0c723352017-12-12 12:02:29 -0800255 public static abstract class PageAlphaProvider {
Sunny Goyalbc683e92017-12-06 10:25:07 -0800256
Sunny Goyal0c723352017-12-12 12:02:29 -0800257 public final Interpolator interpolator;
258
259 public PageAlphaProvider(Interpolator interpolator) {
260 this.interpolator = interpolator;
261 }
262
263 public abstract float getPageAlpha(int pageIndex);
Sunny Goyalbc683e92017-12-06 10:25:07 -0800264 }
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700265}