blob: 1c4986728f597b1a64f10ff355cdae026cb933b9 [file] [log] [blame]
Sunny Goyalf3ac7032020-03-13 13:01:33 -07001/*
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 */
16package com.android.launcher3.states;
17
18import android.view.animation.Interpolator;
19
20import androidx.annotation.IntDef;
21
22import java.lang.annotation.Retention;
23import java.lang.annotation.RetentionPolicy;
24
25/**
26 * Utility class for building animator set
27 */
28public 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,
Jon Mirandadc186e62020-04-15 15:19:49 -070040 SKIP_DEPTH_CONTROLLER
Sunny Goyalf3ac7032020-03-13 13:01:33 -070041 })
42 @Retention(RetentionPolicy.SOURCE)
43 public @interface AnimationFlags {}
44 public static final int PLAY_NON_ATOMIC = 1 << 0;
45 public static final int PLAY_ATOMIC_OVERVIEW_SCALE = 1 << 1;
46 public static final int PLAY_ATOMIC_OVERVIEW_PEEK = 1 << 2;
47 public static final int SKIP_OVERVIEW = 1 << 3;
Jon Mirandadc186e62020-04-15 15:19:49 -070048 public static final int SKIP_DEPTH_CONTROLLER = 1 << 4;
Sunny Goyalf3ac7032020-03-13 13:01:33 -070049
50 public long duration;
51 public boolean userControlled;
52 public @AnimationFlags int animFlags = ANIM_ALL_COMPONENTS;
53
54 public static final int ANIM_ALL_COMPONENTS = PLAY_NON_ATOMIC | PLAY_ATOMIC_OVERVIEW_SCALE
55 | PLAY_ATOMIC_OVERVIEW_PEEK;
56
57 // Various types of animation state transition
58 @IntDef(value = {
59 ANIM_VERTICAL_PROGRESS,
60 ANIM_WORKSPACE_SCALE,
61 ANIM_WORKSPACE_TRANSLATE,
62 ANIM_WORKSPACE_FADE,
63 ANIM_HOTSEAT_SCALE,
64 ANIM_HOTSEAT_TRANSLATE,
65 ANIM_OVERVIEW_SCALE,
66 ANIM_OVERVIEW_TRANSLATE_X,
67 ANIM_OVERVIEW_TRANSLATE_Y,
68 ANIM_OVERVIEW_FADE,
69 ANIM_ALL_APPS_FADE,
70 ANIM_OVERVIEW_SCRIM_FADE,
71 ANIM_ALL_APPS_HEADER_FADE,
Zak Cohena39544d2020-04-27 16:26:55 -070072 ANIM_OVERVIEW_MODAL
Sunny Goyalf3ac7032020-03-13 13:01:33 -070073 })
74 @Retention(RetentionPolicy.SOURCE)
75 public @interface AnimType {}
76 public static final int ANIM_VERTICAL_PROGRESS = 0;
77 public static final int ANIM_WORKSPACE_SCALE = 1;
78 public static final int ANIM_WORKSPACE_TRANSLATE = 2;
79 public static final int ANIM_WORKSPACE_FADE = 3;
80 public static final int ANIM_HOTSEAT_SCALE = 4;
81 public static final int ANIM_HOTSEAT_TRANSLATE = 5;
82 public static final int ANIM_OVERVIEW_SCALE = 6;
83 public static final int ANIM_OVERVIEW_TRANSLATE_X = 7;
84 public static final int ANIM_OVERVIEW_TRANSLATE_Y = 8;
85 public static final int ANIM_OVERVIEW_FADE = 9;
86 public static final int ANIM_ALL_APPS_FADE = 10;
87 public static final int ANIM_OVERVIEW_SCRIM_FADE = 11;
88 public static final int ANIM_ALL_APPS_HEADER_FADE = 12; // e.g. predictions
Zak Cohena39544d2020-04-27 16:26:55 -070089 public static final int ANIM_OVERVIEW_MODAL = 13;
Sunny Goyalf3ac7032020-03-13 13:01:33 -070090
Zak Cohena39544d2020-04-27 16:26:55 -070091 private static final int ANIM_TYPES_COUNT = 14;
Sunny Goyalf3ac7032020-03-13 13:01:33 -070092
93 private final Interpolator[] mInterpolators = new Interpolator[ANIM_TYPES_COUNT];
94
95 public StateAnimationConfig() { }
96
97 /**
98 * Copies the config to target
99 */
100 public void copyTo(StateAnimationConfig target) {
101 target.duration = duration;
102 target.animFlags = animFlags;
103 target.userControlled = userControlled;
104 for (int i = 0; i < ANIM_TYPES_COUNT; i++) {
105 target.mInterpolators[i] = mInterpolators[i];
106 }
107 }
108
109 /**
110 * Returns the interpolator set for animId or fallback if nothing is set
111 *
112 * @see #setInterpolator(int, Interpolator)
113 */
114 public Interpolator getInterpolator(@AnimType int animId, Interpolator fallback) {
115 return mInterpolators[animId] == null ? fallback : mInterpolators[animId];
116 }
117
118 /**
119 * Sets an interpolator for a given animation type
120 */
121 public void setInterpolator(@AnimType int animId, Interpolator interpolator) {
122 mInterpolators[animId] = interpolator;
123 }
124
125 /**
126 * @return Whether Overview is scaling as part of this animation. If this is the only
127 * component (i.e. NON_ATOMIC_COMPONENT isn't included), then this scaling is happening
128 * atomically, rather than being part of a normal state animation. StateHandlers can use
129 * this to designate part of their animation that should scale with Overview.
130 */
131 public boolean playAtomicOverviewScaleComponent() {
132 return hasAnimationFlag(StateAnimationConfig.PLAY_ATOMIC_OVERVIEW_SCALE);
133 }
134
135 /**
136 * @return Whether this animation will play atomically at the same time as a different,
137 * user-controlled state transition. StateHandlers, which contribute to both animations, can
138 * use this to avoid animating the same properties in both animations, since they'd conflict
139 * with one another.
140 */
141 public boolean onlyPlayAtomicComponent() {
142 return getAnimComponents() == StateAnimationConfig.PLAY_ATOMIC_OVERVIEW_SCALE
143 || getAnimComponents() == StateAnimationConfig.PLAY_ATOMIC_OVERVIEW_PEEK;
144 }
145
146 /**
147 * Returns true if the config and any of the provided component flags
148 */
149 public boolean hasAnimationFlag(@AnimationFlags int a) {
150 return (animFlags & a) != 0;
151 }
152
153 /**
154 * @return Only the flags that determine which animation components to play.
155 */
156 public @AnimationFlags int getAnimComponents() {
157 return animFlags & StateAnimationConfig.ANIM_ALL_COMPONENTS;
158 }
159}