Debian Codegraph @

core/java/android/window/BackProgressAnimator.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2022 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
17 package android.window;
18
19 import android.util.FloatProperty;
20
21 import com.android.internal.dynamicanimation.animation.SpringAnimation;
22 import com.android.internal.dynamicanimation.animation.SpringForce;
23
24 /**
25 * An animator that drives the predictive back progress with a spring.
26 *
27 * The back gesture's latest touch point and committal state determines the final position of
28 * the spring. The continuous movement of the spring is used to produce {@link BackEvent}s with
29 * smoothly transitioning progress values.
30 *
31 * @hide
32 */
33 public class BackProgressAnimator {
34 /**
35 * A factor to scale the input progress by, so that it works better with the spring.
36 * We divide the output progress by this value before sending it to apps, so that apps
37 * always receive progress values in [0, 1].
38 */
39 private static final float SCALE_FACTOR = 100f;
40 private final SpringAnimation mSpring;
41 private ProgressCallback mCallback;
42 private float mProgress = 0;
43 private BackEvent mLastBackEvent;
44 private boolean mStarted = false;
45
46 private void setProgress(float progress) {
47 mProgress = progress;
48 }
49
50 private float getProgress() {
51 return mProgress;
52 }
53
54 private static final FloatProperty<BackProgressAnimator> PROGRESS_PROP =
55 new FloatProperty<BackProgressAnimator>("progress") {
56 @Override
57 public void setValue(BackProgressAnimator animator, float value) {
58 animator.setProgress(value);
59 animator.updateProgressValue(value);
60 }
61
62 @Override
63 public Float get(BackProgressAnimator object) {
64 return object.getProgress();
65 }
66 };
67
68
69 /** A callback to be invoked when there's a progress value update from the animator. */
70 public interface ProgressCallback {
71 /** Called when there's a progress value update. */
72 void onProgressUpdate(BackEvent event);
73 }
74
75 public BackProgressAnimator() {
76 mSpring = new SpringAnimation(this, PROGRESS_PROP);
77 mSpring.setSpring(new SpringForce()
78 .setStiffness(SpringForce.STIFFNESS_MEDIUM)
79 .setDampingRatio(SpringForce.DAMPING_RATIO_NO_BOUNCY));
80 }
81
82 /**
83 * Sets a new target position for the back progress.
84 *
85 * @param event the {@link BackEvent} containing the latest target progress.
86 */
87 public void onBackProgressed(BackEvent event) {
88 if (!mStarted) {
89 return;
90 }
91 mLastBackEvent = event;
92 mSpring.animateToFinalPosition(event.getProgress() * SCALE_FACTOR);
93 }
94
95 /**
96 * Starts the back progress animation.
97 *
98 * @param event the {@link BackEvent} that started the gesture.
99 * @param callback the back callback to invoke for the gesture. It will receive back progress
100 * dispatches as the progress animation updates.
101 */
102 public void onBackStarted(BackEvent event, ProgressCallback callback) {
103 reset();
104 mLastBackEvent = event;
105 mCallback = callback;
106 mStarted = true;
107 }
108
109 /**
110 * Resets the back progress animation. This should be called when back is invoked or cancelled.
111 */
112 public void reset() {
113 mSpring.animateToFinalPosition(0);
114 if (mSpring.canSkipToEnd()) {
115 mSpring.skipToEnd();
116 } else {
117 // Should never happen.
118 mSpring.cancel();
119 }
120 mStarted = false;
121 mLastBackEvent = null;
122 mCallback = null;
123 mProgress = 0;
124 }
125
126 private void updateProgressValue(float progress) {
127 if (mLastBackEvent == null || mCallback == null || !mStarted) {
128 return;
129 }
130 mCallback.onProgressUpdate(
131 new BackEvent(mLastBackEvent.getTouchX(), mLastBackEvent.getTouchY(),
132 progress / SCALE_FACTOR, mLastBackEvent.getSwipeEdge(),
133 mLastBackEvent.getDepartingAnimationTarget()));
134 }
135
136 }