• 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.IntDef;
20 import android.annotation.NonNull;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 
27 /**
28  * Represents an event that is sent out by the system during back navigation gesture.
29  * Holds information about the touch event, swipe direction and overall progress of the gesture
30  * interaction.
31  *
32  * @hide
33  */
34 public class BackEvent implements Parcelable {
35     /** Indicates that the edge swipe starts from the left edge of the screen */
36     public static final int EDGE_LEFT = 0;
37     /** Indicates that the edge swipe starts from the right edge of the screen */
38     public static final int EDGE_RIGHT = 1;
39 
40     @IntDef({
41             EDGE_LEFT,
42             EDGE_RIGHT,
43     })
44     @Retention(RetentionPolicy.SOURCE)
45     public @interface SwipeEdge{}
46 
47     private final float mTouchX;
48     private final float mTouchY;
49     private final float mProgress;
50 
51     @SwipeEdge
52     private final int mSwipeEdge;
53 
54     /**
55      * Creates a new {@link BackEvent} instance.
56      *
57      * @param touchX Absolute X location of the touch point of this event.
58      * @param touchY Absolute Y location of the touch point of this event.
59      * @param progress Value between 0 and 1 on how far along the back gesture is.
60      * @param swipeEdge Indicates which edge the swipe starts from.
61      */
BackEvent(float touchX, float touchY, float progress, @SwipeEdge int swipeEdge)62     public BackEvent(float touchX, float touchY, float progress, @SwipeEdge int swipeEdge) {
63         mTouchX = touchX;
64         mTouchY = touchY;
65         mProgress = progress;
66         mSwipeEdge = swipeEdge;
67     }
68 
BackEvent(@onNull Parcel in)69     private BackEvent(@NonNull Parcel in) {
70         mTouchX = in.readFloat();
71         mTouchY = in.readFloat();
72         mProgress = in.readFloat();
73         mSwipeEdge = in.readInt();
74     }
75 
76     public static final Creator<BackEvent> CREATOR = new Creator<BackEvent>() {
77         @Override
78         public BackEvent createFromParcel(Parcel in) {
79             return new BackEvent(in);
80         }
81 
82         @Override
83         public BackEvent[] newArray(int size) {
84             return new BackEvent[size];
85         }
86     };
87 
88     @Override
describeContents()89     public int describeContents() {
90         return 0;
91     }
92 
93     @Override
writeToParcel(@onNull Parcel dest, int flags)94     public void writeToParcel(@NonNull Parcel dest, int flags) {
95         dest.writeFloat(mTouchX);
96         dest.writeFloat(mTouchY);
97         dest.writeFloat(mProgress);
98         dest.writeInt(mSwipeEdge);
99     }
100 
101     /**
102      * Returns a value between 0 and 1 on how far along the back gesture is. This value is
103      * driven by the horizontal location of the touch point, and should be used as the fraction to
104      * seek the predictive back animation with. Specifically,
105      * <ol>
106      * <li>The progress is 0 when the touch is at the starting edge of the screen (left or right),
107      * and animation should seek to its start state.
108      * <li>The progress is approximately 1 when the touch is at the opposite side of the screen,
109      * and animation should seek to its end state. Exact end value may vary depending on
110      * screen size.
111      * </ol>
112      * <li> After the gesture finishes in cancel state, this method keeps getting invoked until the
113      * progress value animates back to 0.
114      * </ol>
115      * In-between locations are linearly interpolated based on horizontal distance from the starting
116      * edge and smooth clamped to 1 when the distance exceeds a system-wide threshold.
117      */
getProgress()118     public float getProgress() {
119         return mProgress;
120     }
121 
122     /**
123      * Returns the absolute X location of the touch point.
124      */
getTouchX()125     public float getTouchX() {
126         return mTouchX;
127     }
128 
129     /**
130      * Returns the absolute Y location of the touch point.
131      */
getTouchY()132     public float getTouchY() {
133         return mTouchY;
134     }
135 
136     /**
137      * Returns the screen edge that the swipe starts from.
138      */
getSwipeEdge()139     public int getSwipeEdge() {
140         return mSwipeEdge;
141     }
142 
143     @Override
toString()144     public String toString() {
145         return "BackEvent{"
146                 + "mTouchX=" + mTouchX
147                 + ", mTouchY=" + mTouchY
148                 + ", mProgress=" + mProgress
149                 + ", mSwipeEdge" + mSwipeEdge
150                 + "}";
151     }
152 }
153