• 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.anim.Interpolators.DEACCEL_2;
20 import static com.android.launcher3.logging.StatsLogManager.LAUNCHER_STATE_HOME;
21 import static com.android.launcher3.logging.StatsLogManager.LAUNCHER_STATE_OVERVIEW;
22 import static com.android.launcher3.testing.shared.TestProtocol.ALL_APPS_STATE_ORDINAL;
23 import static com.android.launcher3.testing.shared.TestProtocol.BACKGROUND_APP_STATE_ORDINAL;
24 import static com.android.launcher3.testing.shared.TestProtocol.HINT_STATE_ORDINAL;
25 import static com.android.launcher3.testing.shared.TestProtocol.HINT_STATE_TWO_BUTTON_ORDINAL;
26 import static com.android.launcher3.testing.shared.TestProtocol.NORMAL_STATE_ORDINAL;
27 import static com.android.launcher3.testing.shared.TestProtocol.OVERVIEW_MODAL_TASK_STATE_ORDINAL;
28 import static com.android.launcher3.testing.shared.TestProtocol.OVERVIEW_SPLIT_SELECT_ORDINAL;
29 import static com.android.launcher3.testing.shared.TestProtocol.OVERVIEW_STATE_ORDINAL;
30 import static com.android.launcher3.testing.shared.TestProtocol.QUICK_SWITCH_STATE_ORDINAL;
31 import static com.android.launcher3.testing.shared.TestProtocol.SPRING_LOADED_STATE_ORDINAL;
32 
33 import android.content.Context;
34 import android.graphics.Color;
35 import android.view.animation.Interpolator;
36 
37 import androidx.annotation.FloatRange;
38 
39 import com.android.launcher3.statemanager.BaseState;
40 import com.android.launcher3.statemanager.StateManager;
41 import com.android.launcher3.states.HintState;
42 import com.android.launcher3.states.SpringLoadedState;
43 import com.android.launcher3.testing.shared.TestProtocol;
44 import com.android.launcher3.uioverrides.states.AllAppsState;
45 import com.android.launcher3.uioverrides.states.OverviewState;
46 import com.android.launcher3.views.ActivityContext;
47 
48 import java.util.Arrays;
49 
50 /**
51  * Base state for various states used for the Launcher
52  */
53 public abstract class LauncherState implements BaseState<LauncherState> {
54 
55     /**
56      * Set of elements indicating various workspace elements which change visibility across states
57      * Note that workspace is not included here as in that case, we animate individual pages
58      */
59     public static final int NONE = 0;
60     public static final int HOTSEAT_ICONS = 1 << 0;
61     public static final int ALL_APPS_CONTENT = 1 << 1;
62     public static final int VERTICAL_SWIPE_INDICATOR = 1 << 2;
63     public static final int OVERVIEW_ACTIONS = 1 << 3;
64     public static final int CLEAR_ALL_BUTTON = 1 << 4;
65     public static final int WORKSPACE_PAGE_INDICATOR = 1 << 5;
66     public static final int SPLIT_PLACHOLDER_VIEW = 1 << 6;
67 
68     // Flag indicating workspace has multiple pages visible.
69     public static final int FLAG_MULTI_PAGE = BaseState.getFlag(0);
70     // Flag indicating that workspace and its contents are not accessible
71     public static final int FLAG_WORKSPACE_INACCESSIBLE = BaseState.getFlag(1);
72 
73     // Flag indicating the state allows workspace icons to be dragged.
74     public static final int FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED = BaseState.getFlag(2);
75     // Flag to indicate that workspace should draw page background
76     public static final int FLAG_WORKSPACE_HAS_BACKGROUNDS = BaseState.getFlag(3);
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(4);
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(5);
81     public static final int FLAG_OVERVIEW_UI = BaseState.getFlag(6);
82 
83     // Flag indicating that hotseat and its contents are not accessible.
84     public static final int FLAG_HOTSEAT_INACCESSIBLE = BaseState.getFlag(7);
85 
86 
87     public static final float NO_OFFSET = 0;
88     public static final float NO_SCALE = 1;
89 
90     protected static final PageAlphaProvider DEFAULT_ALPHA_PROVIDER =
91             new PageAlphaProvider(ACCEL_2) {
92                 @Override
93                 public float getPageAlpha(int pageIndex) {
94                     return 1;
95                 }
96             };
97 
98     protected static final PageTranslationProvider DEFAULT_PAGE_TRANSLATION_PROVIDER =
99             new PageTranslationProvider(DEACCEL_2) {
100                 @Override
101                 public float getPageTranslation(int pageIndex) {
102                     return 0;
103                 }
104             };
105 
106     private static final LauncherState[] sAllStates = new LauncherState[10];
107 
108     /**
109      * TODO: Create a separate class for NORMAL state.
110      */
111     public static final LauncherState NORMAL = new LauncherState(NORMAL_STATE_ORDINAL,
112             LAUNCHER_STATE_HOME,
113             FLAG_DISABLE_RESTORE | FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED | FLAG_HAS_SYS_UI_SCRIM) {
114         @Override
115         public int getTransitionDuration(Context context, boolean isToState) {
116             // Arbitrary duration, when going to NORMAL we use the state we're coming from instead.
117             return 0;
118         }
119     };
120 
121     /**
122      * Various Launcher states arranged in the increasing order of UI layers
123      */
124     public static final LauncherState SPRING_LOADED = new SpringLoadedState(
125             SPRING_LOADED_STATE_ORDINAL);
126     public static final LauncherState ALL_APPS = new AllAppsState(ALL_APPS_STATE_ORDINAL);
127     public static final LauncherState HINT_STATE = new HintState(HINT_STATE_ORDINAL);
128     public static final LauncherState HINT_STATE_TWO_BUTTON = new HintState(
129             HINT_STATE_TWO_BUTTON_ORDINAL, LAUNCHER_STATE_OVERVIEW);
130 
131     public static final LauncherState OVERVIEW = new OverviewState(OVERVIEW_STATE_ORDINAL);
132     public static final LauncherState OVERVIEW_MODAL_TASK = OverviewState.newModalTaskState(
133             OVERVIEW_MODAL_TASK_STATE_ORDINAL);
134     /**
135      * State when user performs a quickswitch gesture from home/workspace to the most recent
136      * app
137      */
138     public static final LauncherState QUICK_SWITCH_FROM_HOME =
139             OverviewState.newSwitchState(QUICK_SWITCH_STATE_ORDINAL);
140     public static final LauncherState BACKGROUND_APP =
141             OverviewState.newBackgroundState(BACKGROUND_APP_STATE_ORDINAL);
142     public static final LauncherState OVERVIEW_SPLIT_SELECT =
143             OverviewState.newSplitSelectState(OVERVIEW_SPLIT_SELECT_ORDINAL);
144 
145     public final int ordinal;
146 
147     /**
148      * Used for {@link com.android.launcher3.logging.StatsLogManager}
149      */
150     public final int statsLogOrdinal;
151 
152     /**
153      * True if the state has overview panel visible.
154      */
155     public final boolean overviewUi;
156 
157     private final int mFlags;
158 
LauncherState(int id, int statsLogOrdinal, int flags)159     public LauncherState(int id, int statsLogOrdinal, int flags) {
160         this.statsLogOrdinal = statsLogOrdinal;
161         this.mFlags = flags;
162         this.overviewUi = (flags & FLAG_OVERVIEW_UI) != 0;
163         this.ordinal = id;
164         sAllStates[id] = this;
165     }
166 
167     /**
168      * Returns if the state has the provided flag
169      */
170     @Override
hasFlag(int mask)171     public final boolean hasFlag(int mask) {
172         return (mFlags & mask) != 0;
173     }
174 
values()175     public static LauncherState[] values() {
176         return Arrays.copyOf(sAllStates, sAllStates.length);
177     }
178 
getWorkspaceScaleAndTranslation(Launcher launcher)179     public ScaleAndTranslation getWorkspaceScaleAndTranslation(Launcher launcher) {
180         return new ScaleAndTranslation(NO_SCALE, NO_OFFSET, NO_OFFSET);
181     }
182 
getHotseatScaleAndTranslation(Launcher launcher)183     public ScaleAndTranslation getHotseatScaleAndTranslation(Launcher launcher) {
184         // For most states, treat the hotseat as if it were part of the workspace.
185         return getWorkspaceScaleAndTranslation(launcher);
186     }
187 
188     /**
189      * Returns an array of two elements.
190      * The first specifies the scale for the overview
191      * The second is the factor ([0, 1], 0 => center-screen; 1 => offscreen) by which overview
192      * should be shifted horizontally.
193      */
getOverviewScaleAndOffset(Launcher launcher)194     public float[] getOverviewScaleAndOffset(Launcher launcher) {
195         return launcher.getNormalOverviewScaleAndOffset();
196     }
197 
getOverviewFullscreenProgress()198     public float getOverviewFullscreenProgress() {
199         return 0;
200     }
201 
getVisibleElements(Launcher launcher)202     public int getVisibleElements(Launcher launcher) {
203         return HOTSEAT_ICONS | WORKSPACE_PAGE_INDICATOR | VERTICAL_SWIPE_INDICATOR;
204     }
205 
206     /**
207      * A shorthand for checking getVisibleElements() & elements == elements.
208      * @return Whether all of the given elements are visible.
209      */
areElementsVisible(Launcher launcher, int elements)210     public boolean areElementsVisible(Launcher launcher, int elements) {
211         return (getVisibleElements(launcher) & elements) == elements;
212     }
213 
214     /**
215      * Returns whether taskbar is stashed and thus should either:
216      * 1) replace hotseat or taskbar icons with a handle in gesture navigation mode or
217      * 2) fade out the hotseat or taskbar icons in 3-button navigation mode.
218      */
isTaskbarStashed(Launcher launcher)219     public boolean isTaskbarStashed(Launcher launcher) {
220         return false;
221     }
222 
223     /** Returns whether taskbar is aligned with the hotseat vs position inside apps */
isTaskbarAlignedWithHotseat(Launcher launcher)224     public boolean isTaskbarAlignedWithHotseat(Launcher launcher) {
225         return true;
226     }
227 
228     /**
229      * Returns whether taskbar global drag is disallowed in this state.
230      */
disallowTaskbarGlobalDrag()231     public boolean disallowTaskbarGlobalDrag() {
232         return false;
233     }
234 
235     /**
236      * Returns whether the taskbar shortcut should trigger split selection mode.
237      */
allowTaskbarInitialSplitSelection()238     public boolean allowTaskbarInitialSplitSelection() {
239         return false;
240     }
241 
242     /**
243      * Fraction shift in the vertical translation UI and related properties
244      *
245      * @see com.android.launcher3.allapps.AllAppsTransitionController
246      */
getVerticalProgress(Launcher launcher)247     public float getVerticalProgress(Launcher launcher) {
248         return 1f;
249     }
250 
getWorkspaceBackgroundAlpha(Launcher launcher)251     public float getWorkspaceBackgroundAlpha(Launcher launcher) {
252         return 0;
253     }
254 
255     /**
256      * What color should the workspace scrim be in when at rest in this state.
257      * Return {@link Color#TRANSPARENT} for no scrim.
258      */
getWorkspaceScrimColor(Launcher launcher)259     public int getWorkspaceScrimColor(Launcher launcher) {
260         return Color.TRANSPARENT;
261     }
262 
263     /**
264      * For this state, how modal should over view been shown. 0 modalness means all tasks drawn,
265      * 1 modalness means the current task is show on its own.
266      */
getOverviewModalness()267     public float getOverviewModalness() {
268         return 0;
269     }
270 
271     /**
272      * For this state, how much additional translation there should be for each of the
273      * child TaskViews. Note that the translation can be its primary or secondary dimension.
274      */
getSplitSelectTranslation(Launcher launcher)275     public float getSplitSelectTranslation(Launcher launcher) {
276         return 0;
277     }
278 
279     /**
280      * The amount of blur and wallpaper zoom to apply to the background of either the app
281      * or Launcher surface in this state. Should be a number between 0 and 1, inclusive.
282      *
283      * 0 means completely zoomed in, without blurs. 1 is zoomed out, with blurs.
284      */
285     public final  <DEVICE_PROFILE_CONTEXT extends Context & ActivityContext>
getDepth(DEVICE_PROFILE_CONTEXT context)286             float getDepth(DEVICE_PROFILE_CONTEXT context) {
287         return getDepth(context,
288                 BaseDraggingActivity.fromContext(context).getDeviceProfile().isMultiWindowMode);
289     }
290 
291     /**
292      * Returns the amount of blur and wallpaper zoom for this state with {@param isMultiWindowMode}.
293      *
294      * @see #getDepth(Context).
295      */
296     public final <DEVICE_PROFILE_CONTEXT extends Context & ActivityContext>
getDepth(DEVICE_PROFILE_CONTEXT context, boolean isMultiWindowMode)297             float getDepth(DEVICE_PROFILE_CONTEXT context, boolean isMultiWindowMode) {
298         if (isMultiWindowMode) {
299             return 0;
300         }
301         return getDepthUnchecked(context);
302     }
303 
304     protected <DEVICE_PROFILE_CONTEXT extends Context & ActivityContext>
getDepthUnchecked(DEVICE_PROFILE_CONTEXT context)305             float getDepthUnchecked(DEVICE_PROFILE_CONTEXT context) {
306         return 0f;
307     }
308 
getDescription(Launcher launcher)309     public String getDescription(Launcher launcher) {
310         return launcher.getWorkspace().getCurrentPageDescription();
311     }
312 
getWorkspacePageAlphaProvider(Launcher launcher)313     public PageAlphaProvider getWorkspacePageAlphaProvider(Launcher launcher) {
314         if ((this != NORMAL && this != HINT_STATE)
315                 || !launcher.getDeviceProfile().shouldFadeAdjacentWorkspaceScreens()) {
316             return DEFAULT_ALPHA_PROVIDER;
317         }
318         final int centerPage = launcher.getWorkspace().getNextPage();
319         return new PageAlphaProvider(ACCEL_2) {
320             @Override
321             public float getPageAlpha(int pageIndex) {
322                 return pageIndex != centerPage ? 0 : 1f;
323             }
324         };
325     }
326 
327     /**
328      * Gets the translation provider for workspace pages.
329      */
330     public PageTranslationProvider getWorkspacePageTranslationProvider(Launcher launcher) {
331         if (this != SPRING_LOADED || !launcher.getDeviceProfile().isTwoPanels) {
332             return DEFAULT_PAGE_TRANSLATION_PROVIDER;
333         }
334         final float quarterPageSpacing = launcher.getWorkspace().getPageSpacing() / 4f;
335         return new PageTranslationProvider(DEACCEL_2) {
336             @Override
337             public float getPageTranslation(int pageIndex) {
338                 boolean isRtl = launcher.getWorkspace().mIsRtl;
339                 boolean isFirstPage = pageIndex % 2 == 0;
340                 return ((isFirstPage && !isRtl) || (!isFirstPage && isRtl)) ? -quarterPageSpacing
341                         : quarterPageSpacing;
342             }
343         };
344     }
345 
346     @Override
347     public LauncherState getHistoryForState(LauncherState previousState) {
348         // No history is supported
349         return NORMAL;
350     }
351 
352     @Override
353     public String toString() {
354         return TestProtocol.stateOrdinalToString(ordinal);
355     }
356 
357     public void onBackPressed(Launcher launcher) {
358         if (this != NORMAL) {
359             StateManager<LauncherState> lsm = launcher.getStateManager();
360             LauncherState lastState = lsm.getLastState();
361             lsm.goToState(lastState);
362         }
363     }
364 
365     /**
366      * Find {@link StateManager} and target {@link LauncherState} to handle back progress in
367      * predictive back gesture.
368      */
369     public void onBackProgressed(
370             Launcher launcher, @FloatRange(from = 0.0, to = 1.0) float backProgress) {
371         StateManager<LauncherState> lsm = launcher.getStateManager();
372         LauncherState toState = lsm.getLastState();
373         lsm.onBackProgressed(toState, backProgress);
374     }
375 
376     /**
377      * Find {@link StateManager} and target {@link LauncherState} to handle backProgress in
378      * predictive back gesture.
379      */
380     public void onBackCancelled(Launcher launcher) {
381         StateManager<LauncherState> lsm = launcher.getStateManager();
382         LauncherState toState = lsm.getLastState();
383         lsm.onBackCancelled(toState);
384     }
385 
386     public static abstract class PageAlphaProvider {
387 
388         public final Interpolator interpolator;
389 
390         public PageAlphaProvider(Interpolator interpolator) {
391             this.interpolator = interpolator;
392         }
393 
394         public abstract float getPageAlpha(int pageIndex);
395     }
396 
397     /**
398      * Provider for the translation and animation interpolation of workspace pages.
399      */
400     public abstract static class PageTranslationProvider {
401 
402         public final Interpolator interpolator;
403 
404         public PageTranslationProvider(Interpolator interpolator) {
405             this.interpolator = interpolator;
406         }
407 
408         /**
409          * Gets the translation of the workspace page at the provided page index.
410          */
411         public abstract float getPageTranslation(int pageIndex);
412     }
413 
414     public static class ScaleAndTranslation {
415         public float scale;
416         public float translationX;
417         public float translationY;
418 
419         public ScaleAndTranslation(float scale, float translationX, float translationY) {
420             this.scale = scale;
421             this.translationX = translationX;
422             this.translationY = translationY;
423         }
424     }
425 }
426