Home | History | Annotate | Download | only in launcher3
      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 android.view.View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
     19 import static android.view.View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS;
     20 import static android.view.accessibility.AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
     21 
     22 import static com.android.launcher3.anim.Interpolators.ACCEL_2;
     23 import static com.android.launcher3.states.RotationHelper.REQUEST_NONE;
     24 
     25 import android.graphics.Rect;
     26 import android.view.animation.Interpolator;
     27 
     28 import com.android.launcher3.states.SpringLoadedState;
     29 import com.android.launcher3.uioverrides.AllAppsState;
     30 import com.android.launcher3.uioverrides.FastOverviewState;
     31 import com.android.launcher3.uioverrides.OverviewState;
     32 import com.android.launcher3.uioverrides.UiFactory;
     33 import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
     34 
     35 import java.util.Arrays;
     36 
     37 
     38 /**
     39  * Base state for various states used for the Launcher
     40  */
     41 public class LauncherState {
     42 
     43 
     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;
     49     public static final int HOTSEAT_ICONS = 1 << 0;
     50     public static final int HOTSEAT_SEARCH_BOX = 1 << 1;
     51     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;
     54     public static final int VERTICAL_SWIPE_INDICATOR = 1 << 5;
     55 
     56     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;
     65     protected static final int FLAG_HAS_SYS_UI_SCRIM = 1 << 9;
     66 
     67     protected static final PageAlphaProvider DEFAULT_ALPHA_PROVIDER =
     68             new PageAlphaProvider(ACCEL_2) {
     69                 @Override
     70                 public float getPageAlpha(int pageIndex) {
     71                     return 1;
     72                 }
     73             };
     74 
     75     private static final LauncherState[] sAllStates = new LauncherState[5];
     76 
     77     /**
     78      * TODO: Create a separate class for NORMAL state.
     79      */
     80     public static final LauncherState NORMAL = new LauncherState(0, ContainerType.WORKSPACE, 0,
     81             FLAG_DISABLE_RESTORE | FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED | FLAG_HIDE_BACK_BUTTON |
     82             FLAG_HAS_SYS_UI_SCRIM);
     83 
     84     /**
     85      * Various Launcher states arranged in the increasing order of UI layers
     86      */
     87     public static final LauncherState SPRING_LOADED = new SpringLoadedState(1);
     88     public static final LauncherState OVERVIEW = new OverviewState(2);
     89     public static final LauncherState FAST_OVERVIEW = new FastOverviewState(3);
     90     public static final LauncherState ALL_APPS = new AllAppsState(4);
     91 
     92     protected static final Rect sTempRect = new Rect();
     93 
     94     public final int ordinal;
     95 
     96     /**
     97      * Used for containerType in {@link com.android.launcher3.logging.UserEventDispatcher}
     98      */
     99     public final int containerType;
    100 
    101     /**
    102      * True if the state can be persisted across activity restarts.
    103      */
    104     public final boolean disableRestore;
    105 
    106     /**
    107      * True if workspace has multiple pages visible.
    108      */
    109     public final boolean hasMultipleVisiblePages;
    110 
    111     /**
    112      * Accessibility flag for workspace and its pages.
    113      * @see android.view.View#setImportantForAccessibility(int)
    114      */
    115     public final int workspaceAccessibilityFlag;
    116 
    117     /**
    118      * Properties related to state transition animation
    119      *
    120      * @see WorkspaceStateTransitionAnimation
    121      */
    122     public final boolean hasWorkspacePageBackground;
    123 
    124     public final int transitionDuration;
    125 
    126     /**
    127      * True if the state allows workspace icons to be dragged.
    128      */
    129     public final boolean workspaceIconsCanBeDragged;
    130 
    131     /**
    132      * True if the workspace pages should not be clipped relative to the workspace bounds
    133      * for this state.
    134      */
    135     public final boolean disablePageClipping;
    136 
    137     /**
    138      * True if launcher can not be directly interacted in this state;
    139      */
    140     public final boolean disableInteraction;
    141 
    142     /**
    143      * True if the state has overview panel visible.
    144      */
    145     public final boolean overviewUi;
    146 
    147     /**
    148      * True if the back button should be hidden when in this state (assuming no floating views are
    149      * open, launcher has window focus, etc).
    150      */
    151     public final boolean hideBackButton;
    152 
    153     public final boolean hasSysUiScrim;
    154 
    155     public LauncherState(int id, int containerType, int transitionDuration, int flags) {
    156         this.containerType = containerType;
    157         this.transitionDuration = transitionDuration;
    158 
    159         this.hasWorkspacePageBackground = (flags & FLAG_PAGE_BACKGROUNDS) != 0;
    160         this.hasMultipleVisiblePages = (flags & FLAG_MULTI_PAGE) != 0;
    161         this.workspaceAccessibilityFlag = (flags & FLAG_DISABLE_ACCESSIBILITY) != 0
    162                 ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
    163                 : IMPORTANT_FOR_ACCESSIBILITY_AUTO;
    164         this.disableRestore = (flags & FLAG_DISABLE_RESTORE) != 0;
    165         this.workspaceIconsCanBeDragged = (flags & FLAG_WORKSPACE_ICONS_CAN_BE_DRAGGED) != 0;
    166         this.disablePageClipping = (flags & FLAG_DISABLE_PAGE_CLIPPING) != 0;
    167         this.disableInteraction = (flags & FLAG_DISABLE_INTERACTION) != 0;
    168         this.overviewUi = (flags & FLAG_OVERVIEW_UI) != 0;
    169         this.hideBackButton = (flags & FLAG_HIDE_BACK_BUTTON) != 0;
    170         this.hasSysUiScrim = (flags & FLAG_HAS_SYS_UI_SCRIM) != 0;
    171 
    172         this.ordinal = id;
    173         sAllStates[id] = this;
    174     }
    175 
    176     public static LauncherState[] values() {
    177         return Arrays.copyOf(sAllStates, sAllStates.length);
    178     }
    179 
    180     public float[] getWorkspaceScaleAndTranslation(Launcher launcher) {
    181         return new float[] {1, 0, 0};
    182     }
    183 
    184     /**
    185      * Returns 2 floats designating how to transition overview:
    186      *   scale for the current and adjacent pages
    187      *   translationY factor where 0 is top aligned and 0.5 is centered vertically
    188      */
    189     public float[] getOverviewScaleAndTranslationYFactor(Launcher launcher) {
    190         return new float[] {1.1f, 0f};
    191     }
    192 
    193     public void onStateEnabled(Launcher launcher) {
    194         dispatchWindowStateChanged(launcher);
    195     }
    196 
    197     public void onStateDisabled(Launcher launcher) { }
    198 
    199     public int getVisibleElements(Launcher launcher) {
    200         if (launcher.getDeviceProfile().isVerticalBarLayout()) {
    201             return HOTSEAT_ICONS | VERTICAL_SWIPE_INDICATOR;
    202         }
    203         return HOTSEAT_ICONS | HOTSEAT_SEARCH_BOX | VERTICAL_SWIPE_INDICATOR;
    204     }
    205 
    206     /**
    207      * Fraction shift in the vertical translation UI and related properties
    208      *
    209      * @see com.android.launcher3.allapps.AllAppsTransitionController
    210      */
    211     public float getVerticalProgress(Launcher launcher) {
    212         return 1f;
    213     }
    214 
    215     public float getWorkspaceScrimAlpha(Launcher launcher) {
    216         return 0;
    217     }
    218 
    219     public String getDescription(Launcher launcher) {
    220         return launcher.getWorkspace().getCurrentPageDescription();
    221     }
    222 
    223     public PageAlphaProvider getWorkspacePageAlphaProvider(Launcher launcher) {
    224         if (this != NORMAL || !launcher.getDeviceProfile().shouldFadeAdjacentWorkspaceScreens()) {
    225             return DEFAULT_ALPHA_PROVIDER;
    226         }
    227         final int centerPage = launcher.getWorkspace().getNextPage();
    228         return new PageAlphaProvider(ACCEL_2) {
    229             @Override
    230             public float getPageAlpha(int pageIndex) {
    231                 return  pageIndex != centerPage ? 0 : 1f;
    232             }
    233         };
    234     }
    235 
    236     public LauncherState getHistoryForState(LauncherState previousState) {
    237         // No history is supported
    238         return NORMAL;
    239     }
    240 
    241     /**
    242      * Called when the start transition ends and the user settles on this particular state.
    243      */
    244     public void onStateTransitionEnd(Launcher launcher) {
    245         if (this == NORMAL) {
    246             UiFactory.resetOverview(launcher);
    247             // Clear any rotation locks when going to normal state
    248             launcher.getRotationHelper().setCurrentStateRequest(REQUEST_NONE);
    249         }
    250     }
    251 
    252     protected static void dispatchWindowStateChanged(Launcher launcher) {
    253         launcher.getWindow().getDecorView().sendAccessibilityEvent(TYPE_WINDOW_STATE_CHANGED);
    254     }
    255 
    256     public static abstract class PageAlphaProvider {
    257 
    258         public final Interpolator interpolator;
    259 
    260         public PageAlphaProvider(Interpolator interpolator) {
    261             this.interpolator = interpolator;
    262         }
    263 
    264         public abstract float getPageAlpha(int pageIndex);
    265     }
    266 }
    267