• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.annotation.FloatRange;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 import android.view.RemoteAnimationTarget;
25 
26 /**
27  * Object used to report back gesture progress. Holds information about a {@link BackEvent} plus
28  * any {@link RemoteAnimationTarget} the gesture manipulates.
29  *
30  * @see BackEvent
31  * @hide
32  */
33 public final class BackMotionEvent implements Parcelable {
34     private final float mTouchX;
35     private final float mTouchY;
36     private final float mProgress;
37     private final float mVelocityX;
38     private final float mVelocityY;
39 
40     @BackEvent.SwipeEdge
41     private final int mSwipeEdge;
42     @Nullable
43     private final RemoteAnimationTarget mDepartingAnimationTarget;
44 
45     /**
46      * Creates a new {@link BackMotionEvent} instance.
47      *
48      * <p>Note: Velocity is only computed for last event, for performance reasons.</p>
49      *
50      * @param touchX Absolute X location of the touch point of this event.
51      * @param touchY Absolute Y location of the touch point of this event.
52      * @param progress Value between 0 and 1 on how far along the back gesture is.
53      * @param velocityX X velocity computed from the touch point of this event.
54      *                  Value in pixels/second. {@link Float#NaN} if was not computed.
55      * @param velocityY Y velocity computed from the touch point of this event.
56      *                  Value in pixels/second. {@link Float#NaN} if was not computed.
57      * @param swipeEdge Indicates which edge the swipe starts from.
58      * @param departingAnimationTarget The remote animation target of the departing
59      *                                 application window.
60      */
BackMotionEvent( float touchX, float touchY, float progress, float velocityX, float velocityY, @BackEvent.SwipeEdge int swipeEdge, @Nullable RemoteAnimationTarget departingAnimationTarget)61     public BackMotionEvent(
62             float touchX,
63             float touchY,
64             float progress,
65             float velocityX,
66             float velocityY,
67             @BackEvent.SwipeEdge int swipeEdge,
68             @Nullable RemoteAnimationTarget departingAnimationTarget) {
69         mTouchX = touchX;
70         mTouchY = touchY;
71         mProgress = progress;
72         mVelocityX = velocityX;
73         mVelocityY = velocityY;
74         mSwipeEdge = swipeEdge;
75         mDepartingAnimationTarget = departingAnimationTarget;
76     }
77 
BackMotionEvent(@onNull Parcel in)78     private BackMotionEvent(@NonNull Parcel in) {
79         mTouchX = in.readFloat();
80         mTouchY = in.readFloat();
81         mProgress = in.readFloat();
82         mVelocityX = in.readFloat();
83         mVelocityY = in.readFloat();
84         mSwipeEdge = in.readInt();
85         mDepartingAnimationTarget = in.readTypedObject(RemoteAnimationTarget.CREATOR);
86     }
87 
88     @NonNull
89     public static final Creator<BackMotionEvent> CREATOR = new Creator<BackMotionEvent>() {
90         @Override
91         public BackMotionEvent createFromParcel(Parcel in) {
92             return new BackMotionEvent(in);
93         }
94 
95         @Override
96         public BackMotionEvent[] newArray(int size) {
97             return new BackMotionEvent[size];
98         }
99     };
100 
101     @Override
describeContents()102     public int describeContents() {
103         return 0;
104     }
105 
106     @Override
writeToParcel(@onNull Parcel dest, int flags)107     public void writeToParcel(@NonNull Parcel dest, int flags) {
108         dest.writeFloat(mTouchX);
109         dest.writeFloat(mTouchY);
110         dest.writeFloat(mProgress);
111         dest.writeFloat(mVelocityX);
112         dest.writeFloat(mVelocityY);
113         dest.writeInt(mSwipeEdge);
114         dest.writeTypedObject(mDepartingAnimationTarget, flags);
115     }
116 
117     /**
118      * Returns the absolute X location of the touch point.
119      */
getTouchX()120     public float getTouchX() {
121         return mTouchX;
122     }
123 
124     /**
125      * Returns the absolute Y location of the touch point.
126      */
getTouchY()127     public float getTouchY() {
128         return mTouchY;
129     }
130 
131     /**
132      * Returns the progress of a {@link BackEvent}.
133      *
134      * @see BackEvent#getProgress()
135      */
136     @FloatRange(from = 0, to = 1)
getProgress()137     public float getProgress() {
138         return mProgress;
139     }
140 
141     /**
142      * Returns the X velocity computed from the touch point.
143      *
144      * @return value in pixels/second or {@link Float#NaN} if was not computed.
145      */
getVelocityX()146     public float getVelocityX() {
147         return mVelocityX;
148     }
149 
150     /**
151      * Returns the Y velocity computed from the touch point.
152      *
153      * @return value in pixels/second or {@link Float#NaN} if was not computed.
154      */
getVelocityY()155     public float getVelocityY() {
156         return mVelocityY;
157     }
158 
159     /**
160      * Returns the screen edge that the swipe starts from.
161      */
162     @BackEvent.SwipeEdge
getSwipeEdge()163     public int getSwipeEdge() {
164         return mSwipeEdge;
165     }
166 
167     /**
168      * Returns the {@link RemoteAnimationTarget} of the top departing application window,
169      * or {@code null} if the top window should not be moved for the current type of back
170      * destination.
171      */
172     @Nullable
getDepartingAnimationTarget()173     public RemoteAnimationTarget getDepartingAnimationTarget() {
174         return mDepartingAnimationTarget;
175     }
176 
177     @Override
toString()178     public String toString() {
179         return "BackMotionEvent{"
180                 + "mTouchX=" + mTouchX
181                 + ", mTouchY=" + mTouchY
182                 + ", mProgress=" + mProgress
183                 + ", mVelocityX=" + mVelocityX
184                 + ", mVelocityY=" + mVelocityY
185                 + ", mSwipeEdge" + mSwipeEdge
186                 + ", mDepartingAnimationTarget" + mDepartingAnimationTarget
187                 + "}";
188     }
189 }
190