Sunny Goyal | f3ac703 | 2020-03-13 13:01:33 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2020 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.states; |
| 17 | |
| 18 | import android.view.animation.Interpolator; |
| 19 | |
| 20 | import androidx.annotation.IntDef; |
| 21 | |
| 22 | import java.lang.annotation.Retention; |
| 23 | import java.lang.annotation.RetentionPolicy; |
| 24 | |
| 25 | /** |
| 26 | * Utility class for building animator set |
| 27 | */ |
| 28 | public class StateAnimationConfig { |
| 29 | |
| 30 | // We separate the state animations into "atomic" and "non-atomic" components. The atomic |
| 31 | // components may be run atomically - that is, all at once, instead of user-controlled. However, |
| 32 | // atomic components are not restricted to this purpose; they can be user-controlled alongside |
| 33 | // non atomic components as well. Note that each gesture model has exactly one atomic component, |
| 34 | // PLAY_ATOMIC_OVERVIEW_SCALE *or* PLAY_ATOMIC_OVERVIEW_PEEK. |
| 35 | @IntDef(flag = true, value = { |
| 36 | PLAY_NON_ATOMIC, |
| 37 | PLAY_ATOMIC_OVERVIEW_SCALE, |
| 38 | PLAY_ATOMIC_OVERVIEW_PEEK, |
| 39 | SKIP_OVERVIEW, |
| 40 | }) |
| 41 | @Retention(RetentionPolicy.SOURCE) |
| 42 | public @interface AnimationFlags {} |
| 43 | public static final int PLAY_NON_ATOMIC = 1 << 0; |
| 44 | public static final int PLAY_ATOMIC_OVERVIEW_SCALE = 1 << 1; |
| 45 | public static final int PLAY_ATOMIC_OVERVIEW_PEEK = 1 << 2; |
| 46 | public static final int SKIP_OVERVIEW = 1 << 3; |
| 47 | |
| 48 | public long duration; |
| 49 | public boolean userControlled; |
| 50 | public @AnimationFlags int animFlags = ANIM_ALL_COMPONENTS; |
| 51 | |
| 52 | public static final int ANIM_ALL_COMPONENTS = PLAY_NON_ATOMIC | PLAY_ATOMIC_OVERVIEW_SCALE |
| 53 | | PLAY_ATOMIC_OVERVIEW_PEEK; |
| 54 | |
| 55 | // Various types of animation state transition |
| 56 | @IntDef(value = { |
| 57 | ANIM_VERTICAL_PROGRESS, |
| 58 | ANIM_WORKSPACE_SCALE, |
| 59 | ANIM_WORKSPACE_TRANSLATE, |
| 60 | ANIM_WORKSPACE_FADE, |
| 61 | ANIM_HOTSEAT_SCALE, |
| 62 | ANIM_HOTSEAT_TRANSLATE, |
| 63 | ANIM_OVERVIEW_SCALE, |
| 64 | ANIM_OVERVIEW_TRANSLATE_X, |
| 65 | ANIM_OVERVIEW_TRANSLATE_Y, |
| 66 | ANIM_OVERVIEW_FADE, |
| 67 | ANIM_ALL_APPS_FADE, |
| 68 | ANIM_OVERVIEW_SCRIM_FADE, |
| 69 | ANIM_ALL_APPS_HEADER_FADE, |
| 70 | }) |
| 71 | @Retention(RetentionPolicy.SOURCE) |
| 72 | public @interface AnimType {} |
| 73 | public static final int ANIM_VERTICAL_PROGRESS = 0; |
| 74 | public static final int ANIM_WORKSPACE_SCALE = 1; |
| 75 | public static final int ANIM_WORKSPACE_TRANSLATE = 2; |
| 76 | public static final int ANIM_WORKSPACE_FADE = 3; |
| 77 | public static final int ANIM_HOTSEAT_SCALE = 4; |
| 78 | public static final int ANIM_HOTSEAT_TRANSLATE = 5; |
| 79 | public static final int ANIM_OVERVIEW_SCALE = 6; |
| 80 | public static final int ANIM_OVERVIEW_TRANSLATE_X = 7; |
| 81 | public static final int ANIM_OVERVIEW_TRANSLATE_Y = 8; |
| 82 | public static final int ANIM_OVERVIEW_FADE = 9; |
| 83 | public static final int ANIM_ALL_APPS_FADE = 10; |
| 84 | public static final int ANIM_OVERVIEW_SCRIM_FADE = 11; |
| 85 | public static final int ANIM_ALL_APPS_HEADER_FADE = 12; // e.g. predictions |
| 86 | |
| 87 | private static final int ANIM_TYPES_COUNT = 13; |
| 88 | |
| 89 | private final Interpolator[] mInterpolators = new Interpolator[ANIM_TYPES_COUNT]; |
| 90 | |
| 91 | public StateAnimationConfig() { } |
| 92 | |
| 93 | /** |
| 94 | * Copies the config to target |
| 95 | */ |
| 96 | public void copyTo(StateAnimationConfig target) { |
| 97 | target.duration = duration; |
| 98 | target.animFlags = animFlags; |
| 99 | target.userControlled = userControlled; |
| 100 | for (int i = 0; i < ANIM_TYPES_COUNT; i++) { |
| 101 | target.mInterpolators[i] = mInterpolators[i]; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Returns the interpolator set for animId or fallback if nothing is set |
| 107 | * |
| 108 | * @see #setInterpolator(int, Interpolator) |
| 109 | */ |
| 110 | public Interpolator getInterpolator(@AnimType int animId, Interpolator fallback) { |
| 111 | return mInterpolators[animId] == null ? fallback : mInterpolators[animId]; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Sets an interpolator for a given animation type |
| 116 | */ |
| 117 | public void setInterpolator(@AnimType int animId, Interpolator interpolator) { |
| 118 | mInterpolators[animId] = interpolator; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @return Whether Overview is scaling as part of this animation. If this is the only |
| 123 | * component (i.e. NON_ATOMIC_COMPONENT isn't included), then this scaling is happening |
| 124 | * atomically, rather than being part of a normal state animation. StateHandlers can use |
| 125 | * this to designate part of their animation that should scale with Overview. |
| 126 | */ |
| 127 | public boolean playAtomicOverviewScaleComponent() { |
| 128 | return hasAnimationFlag(StateAnimationConfig.PLAY_ATOMIC_OVERVIEW_SCALE); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @return Whether this animation will play atomically at the same time as a different, |
| 133 | * user-controlled state transition. StateHandlers, which contribute to both animations, can |
| 134 | * use this to avoid animating the same properties in both animations, since they'd conflict |
| 135 | * with one another. |
| 136 | */ |
| 137 | public boolean onlyPlayAtomicComponent() { |
| 138 | return getAnimComponents() == StateAnimationConfig.PLAY_ATOMIC_OVERVIEW_SCALE |
| 139 | || getAnimComponents() == StateAnimationConfig.PLAY_ATOMIC_OVERVIEW_PEEK; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Returns true if the config and any of the provided component flags |
| 144 | */ |
| 145 | public boolean hasAnimationFlag(@AnimationFlags int a) { |
| 146 | return (animFlags & a) != 0; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @return Only the flags that determine which animation components to play. |
| 151 | */ |
| 152 | public @AnimationFlags int getAnimComponents() { |
| 153 | return animFlags & StateAnimationConfig.ANIM_ALL_COMPONENTS; |
| 154 | } |
| 155 | } |