blob: 8b7217709f0848ca376cfdfa14014e5ddced00b5 [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,
Tony Wickham10c2b4f2020-06-01 14:20:30 -050072 ANIM_OVERVIEW_MODAL,
73 ANIM_DEPTH,
Tony Wickham03a4a0c2020-07-17 13:06:57 -070074 ANIM_OVERVIEW_ACTIONS_FADE,
Sunny Goyalf3ac7032020-03-13 13:01:33 -070075 })
76 @Retention(RetentionPolicy.SOURCE)
77 public @interface AnimType {}
78 public static final int ANIM_VERTICAL_PROGRESS = 0;
79 public static final int ANIM_WORKSPACE_SCALE = 1;
80 public static final int ANIM_WORKSPACE_TRANSLATE = 2;
81 public static final int ANIM_WORKSPACE_FADE = 3;
82 public static final int ANIM_HOTSEAT_SCALE = 4;
83 public static final int ANIM_HOTSEAT_TRANSLATE = 5;
84 public static final int ANIM_OVERVIEW_SCALE = 6;
85 public static final int ANIM_OVERVIEW_TRANSLATE_X = 7;
86 public static final int ANIM_OVERVIEW_TRANSLATE_Y = 8;
87 public static final int ANIM_OVERVIEW_FADE = 9;
88 public static final int ANIM_ALL_APPS_FADE = 10;
89 public static final int ANIM_OVERVIEW_SCRIM_FADE = 11;
90 public static final int ANIM_ALL_APPS_HEADER_FADE = 12; // e.g. predictions
Zak Cohena39544d2020-04-27 16:26:55 -070091 public static final int ANIM_OVERVIEW_MODAL = 13;
Tony Wickham10c2b4f2020-06-01 14:20:30 -050092 public static final int ANIM_DEPTH = 14;
Tony Wickham03a4a0c2020-07-17 13:06:57 -070093 public static final int ANIM_OVERVIEW_ACTIONS_FADE = 15;
Sunny Goyalf3ac7032020-03-13 13:01:33 -070094
Tony Wickham03a4a0c2020-07-17 13:06:57 -070095 private static final int ANIM_TYPES_COUNT = 16;
Sunny Goyalf3ac7032020-03-13 13:01:33 -070096
Tony Wickham03a4a0c2020-07-17 13:06:57 -070097 protected final Interpolator[] mInterpolators = new Interpolator[ANIM_TYPES_COUNT];
Sunny Goyalf3ac7032020-03-13 13:01:33 -070098
99 public StateAnimationConfig() { }
100
101 /**
102 * Copies the config to target
103 */
104 public void copyTo(StateAnimationConfig target) {
105 target.duration = duration;
106 target.animFlags = animFlags;
107 target.userControlled = userControlled;
108 for (int i = 0; i < ANIM_TYPES_COUNT; i++) {
109 target.mInterpolators[i] = mInterpolators[i];
110 }
111 }
112
113 /**
114 * Returns the interpolator set for animId or fallback if nothing is set
115 *
116 * @see #setInterpolator(int, Interpolator)
117 */
118 public Interpolator getInterpolator(@AnimType int animId, Interpolator fallback) {
119 return mInterpolators[animId] == null ? fallback : mInterpolators[animId];
120 }
121
122 /**
123 * Sets an interpolator for a given animation type
124 */
125 public void setInterpolator(@AnimType int animId, Interpolator interpolator) {
126 mInterpolators[animId] = interpolator;
127 }
128
129 /**
130 * @return Whether Overview is scaling as part of this animation. If this is the only
131 * component (i.e. NON_ATOMIC_COMPONENT isn't included), then this scaling is happening
132 * atomically, rather than being part of a normal state animation. StateHandlers can use
133 * this to designate part of their animation that should scale with Overview.
134 */
135 public boolean playAtomicOverviewScaleComponent() {
136 return hasAnimationFlag(StateAnimationConfig.PLAY_ATOMIC_OVERVIEW_SCALE);
137 }
138
139 /**
140 * @return Whether this animation will play atomically at the same time as a different,
141 * user-controlled state transition. StateHandlers, which contribute to both animations, can
142 * use this to avoid animating the same properties in both animations, since they'd conflict
143 * with one another.
144 */
145 public boolean onlyPlayAtomicComponent() {
146 return getAnimComponents() == StateAnimationConfig.PLAY_ATOMIC_OVERVIEW_SCALE
147 || getAnimComponents() == StateAnimationConfig.PLAY_ATOMIC_OVERVIEW_PEEK;
148 }
149
150 /**
151 * Returns true if the config and any of the provided component flags
152 */
153 public boolean hasAnimationFlag(@AnimationFlags int a) {
154 return (animFlags & a) != 0;
155 }
156
157 /**
158 * @return Only the flags that determine which animation components to play.
159 */
160 public @AnimationFlags int getAnimComponents() {
161 return animFlags & StateAnimationConfig.ANIM_ALL_COMPONENTS;
162 }
163}