• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
16 package com.android.launcher3;
17 
18 import static com.android.launcher3.anim.Interpolators.ACCEL_2;
19 import static com.android.launcher3.logging.StatsLogManager.LAUNCHER_STATE_HOME;
20 import static com.android.launcher3.logging.StatsLogManager.LAUNCHER_STATE_OVERVIEW;
21 import static com.android.launcher3.testing.TestProtocol.ALL_APPS_STATE_ORDINAL;
22 import static com.android.launcher3.testing.TestProtocol.BACKGROUND_APP_STATE_ORDINAL;
23 import static com.android.launcher3.testing.TestProtocol.HINT_STATE_ORDINAL;
24 import static com.android.launcher3.testing.TestProtocol.HINT_STATE_TWO_BUTTON_ORDINAL;
25 import static com.android.launcher3.testing.TestProtocol.NORMAL_STATE_ORDINAL;
26 import static com.android.launcher3.testing.TestProtocol.OVERVIEW_MODAL_TASK_STATE_ORDINAL;
27 import static com.android.launcher3.testing.TestProtocol.OVERVIEW_SPLIT_SELECT_ORDINAL;
28 import static com.android.launcher3.testing.TestProtocol.OVERVIEW_STATE_ORDINAL;
29 import static com.android.launcher3.testing.TestProtocol.QUICK_SWITCH_STATE_ORDINAL;
30 import static com.android.launcher3.testing.TestProtocol.SPRING_LOADED_STATE_ORDINAL;
31 
32 import android.content.Context;
33 import android.graphics.Color;
34 import android.view.animation.Interpolator;
35 
36 import com.android.launcher3.statemanager.BaseState;
37 import com.android.launcher3.statemanager.StateManager;
38 import com.android.launcher3.states.HintState;
39 import com.android.launcher3.states.SpringLoadedState;
40 import com.android.launcher3.testing.TestProtocol;
41 import com.android.launcher3.uioverrides.states.AllAppsState;
42 import com.android.launcher3.uioverrides.states.OverviewState;
43 
44 import java.util.Arrays;
45 
46 /**
47  * Base state for various states used for the Launcher
48  */
49 public abstract class LauncherState implements BaseState<LauncherState> {
50 
51     /**
52      * Set of elements indicating various workspace elements which change visibility across states
53      * Note that workspace is not included here as in that case, we animate individual pages
54      */
55     public static final int NONE = 0;
56     public static final int HOTSEAT_ICONS = 1 << 0;
57     public static final int ALL_APPS_CONTENT = 1 << 1;
58     public static final int VERTICAL_SWIPE_INDICATOR = 1 << 2;
59     public static final int OVERVIEW_ACTIONS = 1 << 3;
60     public static final int TASKBAR = 1 << 4;
61     public static final int CLEAR_ALL_BUTTON = 1 << 5;
62     public static final int WORKSPACE_PAGE_INDICATOR = 1 << 6;
63     public static final int SPLIT_PLACHOLDER_VIEW = 1 << 7;
64 
65     // Flag indicating workspace has multiple pages visible.
66     public static final int FLAG_MULTI_PAGE = BaseState.getFlag(0);
67     // Flag indicating that workspace and its contents are not accessible
68     public static final int FLAG_WORKSPACE_INACCESSIBLE = BaseState.getFlag(1);
69 
70     // Flag indicating the state allows workspace icons to be dragged.
71     public static final int FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED = BaseState.getFlag(2);
72     // Flag to indicate that workspace should draw page background
73     public static final int FLAG_WORKSPACE_HAS_BACKGROUNDS = BaseState.getFlag(3);
74     // True if the back button should be hidden when in this state (assuming no floating views are
75     // open, launcher has window focus, etc).
76     public static final int FLAG_HIDE_BACK_BUTTON = BaseState.getFlag(4);
77     // Flag to indicate if the state would have scrim over sysui region: statu sbar and nav bar
78     public static final int FLAG_HAS_SYS_UI_SCRIM = BaseState.getFlag(5);
79     // Flag to inticate that all popups should be closed when this state is enabled.
80     public static final int FLAG_CLOSE_POPUPS = BaseState.getFlag(6);
81     public static final int FLAG_OVERVIEW_UI = BaseState.getFlag(7);
82 
83 
84     public static final float NO_OFFSET = 0;
85     public static final float NO_SCALE = 1;
86 
87     protected static final PageAlphaProvider DEFAULT_ALPHA_PROVIDER =
88             new PageAlphaProvider(ACCEL_2) {
89                 @Override
90                 public float getPageAlpha(int pageIndex) {
91                     return 1;
92                 }
93             };
94 
95     private static final LauncherState[] sAllStates = new LauncherState[10];
96 
97     /**
98      * TODO: Create a separate class for NORMAL state.
99      */
100     public static final LauncherState NORMAL = new LauncherState(NORMAL_STATE_ORDINAL,
101             LAUNCHER_STATE_HOME,
102             FLAG_DISABLE_RESTORE | FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED | FLAG_HIDE_BACK_BUTTON |
103                     FLAG_HAS_SYS_UI_SCRIM) {
104         @Override
105         public int getTransitionDuration(Context context) {
106             // Arbitrary duration, when going to NORMAL we use the state we're coming from instead.
107             return 0;
108         }
109     };
110 
111     /**
112      * Various Launcher states arranged in the increasing order of UI layers
113      */
114     public static final LauncherState SPRING_LOADED = new SpringLoadedState(
115             SPRING_LOADED_STATE_ORDINAL);
116     public static final LauncherState ALL_APPS = new AllAppsState(ALL_APPS_STATE_ORDINAL);
117     public static final LauncherState HINT_STATE = new HintState(HINT_STATE_ORDINAL);
118     public static final LauncherState HINT_STATE_TWO_BUTTON = new HintState(
119             HINT_STATE_TWO_BUTTON_ORDINAL, LAUNCHER_STATE_OVERVIEW);
120 
121     public static final LauncherState OVERVIEW = new OverviewState(OVERVIEW_STATE_ORDINAL);
122     public static final LauncherState OVERVIEW_MODAL_TASK = OverviewState.newModalTaskState(
123             OVERVIEW_MODAL_TASK_STATE_ORDINAL);
124     public static final LauncherState QUICK_SWITCH =
125             OverviewState.newSwitchState(QUICK_SWITCH_STATE_ORDINAL);
126     public static final LauncherState BACKGROUND_APP =
127             OverviewState.newBackgroundState(BACKGROUND_APP_STATE_ORDINAL);
128     public static final LauncherState OVERVIEW_SPLIT_SELECT =
129             OverviewState.newSplitSelectState(OVERVIEW_SPLIT_SELECT_ORDINAL);
130 
131     public final int ordinal;
132 
133     /**
134      * Used for {@link com.android.launcher3.logging.StatsLogManager}
135      */
136     public final int statsLogOrdinal;
137 
138     /**
139      * True if the state has overview panel visible.
140      */
141     public final boolean overviewUi;
142 
143     private final int mFlags;
144 
LauncherState(int id, int statsLogOrdinal, int flags)145     public LauncherState(int id, int statsLogOrdinal, int flags) {
146         this.statsLogOrdinal = statsLogOrdinal;
147         this.mFlags = flags;
148         this.overviewUi = (flags & FLAG_OVERVIEW_UI) != 0;
149         this.ordinal = id;
150         sAllStates[id] = this;
151     }
152 
153     /**
154      * Returns if the state has the provided flag
155      */
156     @Override
hasFlag(int mask)157     public final boolean hasFlag(int mask) {
158         return (mFlags & mask) != 0;
159     }
160 
values()161     public static LauncherState[] values() {
162         return Arrays.copyOf(sAllStates, sAllStates.length);
163     }
164 
getWorkspaceScaleAndTranslation(Launcher launcher)165     public ScaleAndTranslation getWorkspaceScaleAndTranslation(Launcher launcher) {
166         return new ScaleAndTranslation(NO_SCALE, NO_OFFSET, NO_OFFSET);
167     }
168 
getHotseatScaleAndTranslation(Launcher launcher)169     public ScaleAndTranslation getHotseatScaleAndTranslation(Launcher launcher) {
170         // For most states, treat the hotseat as if it were part of the workspace.
171         return getWorkspaceScaleAndTranslation(launcher);
172     }
173 
174     /**
175      * Returns an array of two elements.
176      * The first specifies the scale for the overview
177      * The second is the factor ([0, 1], 0 => center-screen; 1 => offscreen) by which overview
178      * should be shifted horizontally.
179      */
getOverviewScaleAndOffset(Launcher launcher)180     public float[] getOverviewScaleAndOffset(Launcher launcher) {
181         return launcher.getNormalOverviewScaleAndOffset();
182     }
183 
getTaskbarScale(Launcher launcher)184     public float getTaskbarScale(Launcher launcher) {
185         return launcher.getNormalTaskbarScale();
186     }
187 
getTaskbarTranslationY(Launcher launcher)188     public float getTaskbarTranslationY(Launcher launcher) {
189         return -launcher.getHotseat().getTaskbarOffsetY();
190     }
191 
getOverviewFullscreenProgress()192     public float getOverviewFullscreenProgress() {
193         return 0;
194     }
195 
getVisibleElements(Launcher launcher)196     public int getVisibleElements(Launcher launcher) {
197         return HOTSEAT_ICONS | WORKSPACE_PAGE_INDICATOR | VERTICAL_SWIPE_INDICATOR | TASKBAR;
198     }
199 
200     /**
201      * A shorthand for checking getVisibleElements() & elements == elements.
202      * @return Whether all of the given elements are visible.
203      */
areElementsVisible(Launcher launcher, int elements)204     public boolean areElementsVisible(Launcher launcher, int elements) {
205         return (getVisibleElements(launcher) & elements) == elements;
206     }
207 
208     /**
209      * Fraction shift in the vertical translation UI and related properties
210      *
211      * @see com.android.launcher3.allapps.AllAppsTransitionController
212      */
getVerticalProgress(Launcher launcher)213     public float getVerticalProgress(Launcher launcher) {
214         return 1f;
215     }
216 
getWorkspaceBackgroundAlpha(Launcher launcher)217     public float getWorkspaceBackgroundAlpha(Launcher launcher) {
218         return 0;
219     }
220 
221     /**
222      * What color should the workspace scrim be in when at rest in this state.
223      * Return {@link Color#TRANSPARENT} for no scrim.
224      */
getWorkspaceScrimColor(Launcher launcher)225     public int getWorkspaceScrimColor(Launcher launcher) {
226         return Color.TRANSPARENT;
227     }
228 
229     /**
230      * For this state, how modal should over view been shown. 0 modalness means all tasks drawn,
231      * 1 modalness means the current task is show on its own.
232      */
getOverviewModalness()233     public float getOverviewModalness() {
234         return 0;
235     }
236 
237     /**
238      * For this state, how much additional translation there should be for each of the
239      * child TaskViews. Note that the translation can be its primary or secondary dimension.
240      */
getSplitSelectTranslation(Launcher launcher)241     public float getSplitSelectTranslation(Launcher launcher) {
242         return 0;
243     }
244 
245     /**
246      * The amount of blur and wallpaper zoom to apply to the background of either the app
247      * or Launcher surface in this state. Should be a number between 0 and 1, inclusive.
248      *
249      * 0 means completely zoomed in, without blurs. 1 is zoomed out, with blurs.
250      */
getDepth(Context context)251     public final float getDepth(Context context) {
252         return getDepth(context,
253                 BaseDraggingActivity.fromContext(context).getDeviceProfile().isMultiWindowMode);
254     }
255 
256     /**
257      * Returns the amount of blur and wallpaper zoom for this state with {@param isMultiWindowMode}.
258      *
259      * @see #getDepth(Context).
260      */
getDepth(Context context, boolean isMultiWindowMode)261     public final float getDepth(Context context, boolean isMultiWindowMode) {
262         if (isMultiWindowMode) {
263             return 0;
264         }
265         return getDepthUnchecked(context);
266     }
267 
getDepthUnchecked(Context context)268     protected float getDepthUnchecked(Context context) {
269         return 0f;
270     }
271 
getDescription(Launcher launcher)272     public String getDescription(Launcher launcher) {
273         return launcher.getWorkspace().getCurrentPageDescription();
274     }
275 
getWorkspacePageAlphaProvider(Launcher launcher)276     public PageAlphaProvider getWorkspacePageAlphaProvider(Launcher launcher) {
277         if ((this != NORMAL && this != HINT_STATE)
278                 || !launcher.getDeviceProfile().shouldFadeAdjacentWorkspaceScreens()) {
279             return DEFAULT_ALPHA_PROVIDER;
280         }
281         final int centerPage = launcher.getWorkspace().getNextPage();
282         return new PageAlphaProvider(ACCEL_2) {
283             @Override
284             public float getPageAlpha(int pageIndex) {
285                 return pageIndex != centerPage ? 0 : 1f;
286             }
287         };
288     }
289 
290     @Override
291     public LauncherState getHistoryForState(LauncherState previousState) {
292         // No history is supported
293         return NORMAL;
294     }
295 
296     @Override
297     public String toString() {
298         return TestProtocol.stateOrdinalToString(ordinal);
299     }
300 
301     public void onBackPressed(Launcher launcher) {
302         if (this != NORMAL) {
303             StateManager<LauncherState> lsm = launcher.getStateManager();
304             LauncherState lastState = lsm.getLastState();
305             lsm.goToState(lastState);
306         }
307     }
308 
309     public static abstract class PageAlphaProvider {
310 
311         public final Interpolator interpolator;
312 
313         public PageAlphaProvider(Interpolator interpolator) {
314             this.interpolator = interpolator;
315         }
316 
317         public abstract float getPageAlpha(int pageIndex);
318     }
319 
320     public static class ScaleAndTranslation {
321         public float scale;
322         public float translationX;
323         public float translationY;
324 
325         public ScaleAndTranslation(float scale, float translationX, float translationY) {
326             this.scale = scale;
327             this.translationX = translationX;
328             this.translationY = translationY;
329         }
330     }
331 }
332