blob: 01ab56317bb106765de6bb46143688ec78a0127e [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 Goyalbe93f262017-10-20 17:05:27 -070022import android.view.View;
23
Sunny Goyalbc683e92017-12-06 10:25:07 -080024import com.android.launcher3.uioverrides.AllAppsState;
Sunny Goyalc99cb172017-10-19 16:15:09 -070025import com.android.launcher3.states.SpringLoadedState;
Sunny Goyal85525172017-11-06 13:00:42 -080026import com.android.launcher3.uioverrides.OverviewState;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070027import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
28
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070029import java.util.Arrays;
30
Sunny Goyal4c7f2152017-10-17 17:17:16 -070031
32/**
Sunny Goyalbe93f262017-10-20 17:05:27 -070033 * Base state for various states used for the Launcher
Sunny Goyal4c7f2152017-10-17 17:17:16 -070034 */
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070035public class LauncherState {
Sunny Goyal4c7f2152017-10-17 17:17:16 -070036
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070037 protected static final int FLAG_SHOW_SCRIM = 1 << 0;
38 protected static final int FLAG_MULTI_PAGE = 1 << 1;
Sunny Goyalc4fa8c32017-11-07 12:23:58 -080039 protected static final int FLAG_DISABLE_ACCESSIBILITY = 1 << 2;
Sunny Goyalbc683e92017-12-06 10:25:07 -080040 protected static final int FLAG_DISABLE_RESTORE = 1 << 3;
Tony Wickham53370672017-12-05 10:10:55 -080041 protected static final int FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED = 1 << 4;
Sunny Goyalbc683e92017-12-06 10:25:07 -080042 protected static final int FLAG_DISABLE_PAGE_CLIPPING = 1 << 5;
43
44 protected static final PageAlphaProvider DEFAULT_ALPHA_PROVIDER = (i) -> 1f;
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070045
46 private static final LauncherState[] sAllStates = new LauncherState[4];
47
Sunny Goyalc99cb172017-10-19 16:15:09 -070048 public static final LauncherState NORMAL = new LauncherState(0, ContainerType.WORKSPACE,
Sunny Goyalbc683e92017-12-06 10:25:07 -080049 0, 1f, FLAG_DISABLE_RESTORE | FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED);
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070050
Sunny Goyalbe93f262017-10-20 17:05:27 -070051 public static final LauncherState ALL_APPS = new AllAppsState(1);
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070052
Sunny Goyalc99cb172017-10-19 16:15:09 -070053 public static final LauncherState SPRING_LOADED = new SpringLoadedState(2);
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070054
Sunny Goyal85525172017-11-06 13:00:42 -080055 public static final LauncherState OVERVIEW = new OverviewState(3);
Sunny Goyal4c7f2152017-10-17 17:17:16 -070056
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -070057 public final int ordinal;
58
Sunny Goyalf9403d92017-10-18 10:55:56 -070059 /**
60 * Used for containerType in {@link com.android.launcher3.logging.UserEventDispatcher}
61 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -070062 public final int containerType;
63
Sunny Goyalf9403d92017-10-18 10:55:56 -070064 /**
65 * True if the state can be persisted across activity restarts.
66 */
Sunny Goyalbc683e92017-12-06 10:25:07 -080067 public final boolean disableRestore;
Sunny Goyalf9403d92017-10-18 10:55:56 -070068
69 /**
70 * True if workspace has multiple pages visible.
71 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -070072 public final boolean hasMultipleVisiblePages;
Sunny Goyalf9403d92017-10-18 10:55:56 -070073
74 /**
75 * Accessibility flag for workspace and its pages.
76 * @see android.view.View#setImportantForAccessibility(int)
77 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -070078 public final int workspaceAccessibilityFlag;
79
Sunny Goyalbe93f262017-10-20 17:05:27 -070080 /**
81 * Properties related to state transition animation
82 *
83 * @see WorkspaceStateTransitionAnimation
84 */
Sunny Goyal4c7f2152017-10-17 17:17:16 -070085 public final boolean hasScrim;
Sunny Goyal4c7f2152017-10-17 17:17:16 -070086 public final int transitionDuration;
87
Sunny Goyalbe93f262017-10-20 17:05:27 -070088 /**
89 * Fraction shift in the vertical translation UI and related properties
90 *
91 * @see com.android.launcher3.allapps.AllAppsTransitionController
92 */
93 public final float verticalProgress;
Sunny Goyalbe93f262017-10-20 17:05:27 -070094
Tony Wickham53370672017-12-05 10:10:55 -080095 /**
96 * True if the state allows workspace icons to be dragged.
97 */
98 public final boolean workspaceIconsCanBeDragged;
99
Sunny Goyalbc683e92017-12-06 10:25:07 -0800100 /**
101 * True if the workspace pages should not be clipped relative to the workspace bounds
102 * for this state.
103 */
104 public final boolean disablePageClipping;
105
Sunny Goyalbe93f262017-10-20 17:05:27 -0700106 public LauncherState(int id, int containerType, int transitionDuration, float verticalProgress,
107 int flags) {
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700108 this.containerType = containerType;
109 this.transitionDuration = transitionDuration;
110
111 this.hasScrim = (flags & FLAG_SHOW_SCRIM) != 0;
112 this.hasMultipleVisiblePages = (flags & FLAG_MULTI_PAGE) != 0;
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700113 this.workspaceAccessibilityFlag = (flags & FLAG_DISABLE_ACCESSIBILITY) != 0
114 ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
115 : IMPORTANT_FOR_ACCESSIBILITY_AUTO;
Sunny Goyalbc683e92017-12-06 10:25:07 -0800116 this.disableRestore = (flags & FLAG_DISABLE_RESTORE) != 0;
Tony Wickham53370672017-12-05 10:10:55 -0800117 this.workspaceIconsCanBeDragged = (flags & FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED) != 0;
Sunny Goyalbc683e92017-12-06 10:25:07 -0800118 this.disablePageClipping = (flags & FLAG_DISABLE_PAGE_CLIPPING) != 0;
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700119
Sunny Goyalbe93f262017-10-20 17:05:27 -0700120 this.verticalProgress = verticalProgress;
Sunny Goyalbe93f262017-10-20 17:05:27 -0700121
Sunny Goyalcd7c0aa2017-10-19 12:36:27 -0700122 this.ordinal = id;
123 sAllStates[id] = this;
124 }
125
126 public static LauncherState[] values() {
127 return Arrays.copyOf(sAllStates, sAllStates.length);
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700128 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700129
130 public float[] getWorkspaceScaleAndTranslation(Launcher launcher) {
131 return new float[] {1, 0};
132 }
133
Tony Wickhama447bd82017-12-05 14:24:37 -0800134 public float getHoseatAlpha(Launcher launcher) {
135 return 1f;
136 }
137
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700138 public void onStateEnabled(Launcher launcher) {
139 dispatchWindowStateChanged(launcher);
140 }
Sunny Goyalc99cb172017-10-19 16:15:09 -0700141
142 public void onStateDisabled(Launcher launcher) { }
Sunny Goyalbe93f262017-10-20 17:05:27 -0700143
144 public View getFinalFocus(Launcher launcher) {
145 return launcher.getWorkspace();
146 }
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700147
148 public String getDescription(Launcher launcher) {
149 return launcher.getWorkspace().getCurrentPageDescription();
150 }
151
Sunny Goyalbc683e92017-12-06 10:25:07 -0800152 public PageAlphaProvider getWorkspacePageAlphaProvider(Launcher launcher) {
153 if (this != NORMAL || !launcher.getDeviceProfile().shouldFadeAdjacentWorkspaceScreens()) {
154 return DEFAULT_ALPHA_PROVIDER;
155 }
156 int centerPage = launcher.getWorkspace().getPageNearestToCenterOfScreen();
157 return (childIndex) -> childIndex != centerPage ? 0 : 1f;
158 }
159
Sunny Goyal3e3f44c2017-10-23 17:14:52 -0700160 protected static void dispatchWindowStateChanged(Launcher launcher) {
161 launcher.getWindow().getDecorView().sendAccessibilityEvent(TYPE_WINDOW_STATE_CHANGED);
162 }
Sunny Goyalbc683e92017-12-06 10:25:07 -0800163
164 public interface PageAlphaProvider {
165
166 float getPageAlpha(int pageIndex);
167 }
Sunny Goyal4c7f2152017-10-17 17:17:16 -0700168}