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