blob: 189ad8472bebf2190507d9cd6422a39f5da62807 [file] [log] [blame]
Eric Erfanian938468d2017-10-24 14:05:52 -07001/*
2 * Copyright (C) 2017 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
17package com.android.newbubble;
18
19import android.content.Context;
20import android.graphics.Point;
21import android.support.animation.FloatPropertyCompat;
22import android.support.animation.SpringAnimation;
23import android.support.animation.SpringForce;
24import android.support.annotation.NonNull;
25import android.view.Gravity;
26import android.view.MotionEvent;
27import android.view.VelocityTracker;
28import android.view.View;
29import android.view.View.OnTouchListener;
30import android.view.ViewConfiguration;
31import android.view.WindowManager;
32import android.view.WindowManager.LayoutParams;
33import android.widget.Scroller;
34
35/** Handles touches and manages moving the bubble in response */
36class NewMoveHandler implements OnTouchListener {
37
38 // Amount the ViewConfiguration's minFlingVelocity will be scaled by for our own minVelocity
39 private static final int MIN_FLING_VELOCITY_FACTOR = 8;
40 // The friction multiplier to control how slippery the bubble is when flung
41 private static final float SCROLL_FRICTION_MULTIPLIER = 4f;
42
43 private final Context context;
44 private final WindowManager windowManager;
45 private final NewBubble bubble;
46 private final int minX;
47 private final int minY;
48 private final int maxX;
49 private final int maxY;
50 private final int bubbleSize;
51 private final float touchSlopSquared;
52
53 private boolean clickable = true;
54 private boolean isMoving;
55 private float firstX;
56 private float firstY;
57
58 private SpringAnimation moveXAnimation;
59 private SpringAnimation moveYAnimation;
60 private VelocityTracker velocityTracker;
61 private Scroller scroller;
62
63 private static float clamp(float value, float min, float max) {
64 return Math.min(max, Math.max(min, value));
65 }
66
67 // Handles the left/right gravity conversion and centering
68 private final FloatPropertyCompat<WindowManager.LayoutParams> xProperty =
69 new FloatPropertyCompat<LayoutParams>("xProperty") {
70 @Override
71 public float getValue(LayoutParams windowParams) {
72 int realX = windowParams.x;
73 realX = realX + bubbleSize / 2;
74 if (relativeToRight(windowParams)) {
75 int displayWidth = context.getResources().getDisplayMetrics().widthPixels;
76 realX = displayWidth - realX;
77 }
78 return clamp(realX, minX, maxX);
79 }
80
81 @Override
82 public void setValue(LayoutParams windowParams, float value) {
83 int displayWidth = context.getResources().getDisplayMetrics().widthPixels;
84 boolean onRight;
85 Integer gravityOverride = bubble.getGravityOverride();
86 if (gravityOverride == null) {
87 onRight = value > displayWidth / 2;
88 } else {
89 onRight = (gravityOverride & Gravity.RIGHT) == Gravity.RIGHT;
90 }
91 int centeringOffset = bubbleSize / 2;
92 windowParams.x =
93 (int) (onRight ? (displayWidth - value - centeringOffset) : value - centeringOffset);
94 windowParams.gravity = Gravity.TOP | (onRight ? Gravity.RIGHT : Gravity.LEFT);
95 if (bubble.isVisible()) {
96 windowManager.updateViewLayout(bubble.getRootView(), windowParams);
97 }
98 }
99 };
100
101 private final FloatPropertyCompat<WindowManager.LayoutParams> yProperty =
102 new FloatPropertyCompat<LayoutParams>("yProperty") {
103 @Override
104 public float getValue(LayoutParams object) {
105 return clamp(object.y + bubbleSize, minY, maxY);
106 }
107
108 @Override
109 public void setValue(LayoutParams object, float value) {
110 object.y = (int) value - bubbleSize;
111 if (bubble.isVisible()) {
112 windowManager.updateViewLayout(bubble.getRootView(), object);
113 }
114 }
115 };
116
117 public NewMoveHandler(@NonNull View targetView, @NonNull NewBubble bubble) {
118 this.bubble = bubble;
119 context = targetView.getContext();
120 windowManager = context.getSystemService(WindowManager.class);
121
122 bubbleSize = context.getResources().getDimensionPixelSize(R.dimen.bubble_size);
123 minX =
124 context.getResources().getDimensionPixelOffset(R.dimen.bubble_safe_margin_horizontal)
125 + bubbleSize / 2;
126 minY =
127 context.getResources().getDimensionPixelOffset(R.dimen.bubble_safe_margin_vertical)
128 + bubbleSize / 2;
129 maxX = context.getResources().getDisplayMetrics().widthPixels - minX;
130 maxY = context.getResources().getDisplayMetrics().heightPixels - minY;
131
132 // Squared because it will be compared against the square of the touch delta. This is more
133 // efficient than needing to take a square root.
134 touchSlopSquared = (float) Math.pow(ViewConfiguration.get(context).getScaledTouchSlop(), 2);
135
136 targetView.setOnTouchListener(this);
137 }
138
139 public void setClickable(boolean clickable) {
140 this.clickable = clickable;
141 }
142
143 public boolean isMoving() {
144 return isMoving;
145 }
146
147 public void undoGravityOverride() {
148 LayoutParams windowParams = bubble.getWindowParams();
149 xProperty.setValue(windowParams, xProperty.getValue(windowParams));
150 }
151
152 public void snapToBounds() {
153 ensureSprings();
154
155 moveXAnimation.animateToFinalPosition(relativeToRight(bubble.getWindowParams()) ? maxX : minX);
156 moveYAnimation.animateToFinalPosition(yProperty.getValue(bubble.getWindowParams()));
157 }
158
159 @Override
160 public boolean onTouch(View v, MotionEvent event) {
161 float eventX = event.getRawX();
162 float eventY = event.getRawY();
163 switch (event.getActionMasked()) {
164 case MotionEvent.ACTION_DOWN:
165 firstX = eventX;
166 firstY = eventY;
167 velocityTracker = VelocityTracker.obtain();
168 break;
169 case MotionEvent.ACTION_MOVE:
170 if (isMoving || hasExceededTouchSlop(event)) {
171 if (!isMoving) {
172 isMoving = true;
173 bubble.onMoveStart();
174 }
175
176 ensureSprings();
177
178 moveXAnimation.animateToFinalPosition(clamp(eventX, minX, maxX));
179 moveYAnimation.animateToFinalPosition(clamp(eventY, minY, maxY));
180 }
181
182 velocityTracker.addMovement(event);
183 break;
184 case MotionEvent.ACTION_UP:
185 if (isMoving) {
186 ViewConfiguration viewConfiguration = ViewConfiguration.get(context);
187 velocityTracker.computeCurrentVelocity(
188 1000, viewConfiguration.getScaledMaximumFlingVelocity());
189 float xVelocity = velocityTracker.getXVelocity();
190 float yVelocity = velocityTracker.getYVelocity();
191 boolean isFling = isFling(xVelocity, yVelocity);
192
193 if (isFling) {
194 Point target =
195 findTarget(
196 xVelocity,
197 yVelocity,
198 (int) xProperty.getValue(bubble.getWindowParams()),
199 (int) yProperty.getValue(bubble.getWindowParams()));
200
201 moveXAnimation.animateToFinalPosition(target.x);
202 moveYAnimation.animateToFinalPosition(target.y);
203 } else {
204 snapX();
205 }
206 isMoving = false;
207 bubble.onMoveFinish();
208 } else {
209 v.performClick();
210 if (clickable) {
211 bubble.primaryButtonClick();
212 }
213 }
214 break;
215 default: // fall out
216 }
217 return true;
218 }
219
220 private void ensureSprings() {
221 if (moveXAnimation == null) {
222 moveXAnimation = new SpringAnimation(bubble.getWindowParams(), xProperty);
223 moveXAnimation.setSpring(new SpringForce());
224 moveXAnimation.getSpring().setDampingRatio(SpringForce.DAMPING_RATIO_NO_BOUNCY);
225 }
226
227 if (moveYAnimation == null) {
228 moveYAnimation = new SpringAnimation(bubble.getWindowParams(), yProperty);
229 moveYAnimation.setSpring(new SpringForce());
230 moveYAnimation.getSpring().setDampingRatio(SpringForce.DAMPING_RATIO_NO_BOUNCY);
231 }
232 }
233
234 private Point findTarget(float xVelocity, float yVelocity, int startX, int startY) {
235 if (scroller == null) {
236 scroller = new Scroller(context);
237 scroller.setFriction(ViewConfiguration.getScrollFriction() * SCROLL_FRICTION_MULTIPLIER);
238 }
239
240 // Find where a fling would end vertically
241 scroller.fling(startX, startY, (int) xVelocity, (int) yVelocity, minX, maxX, minY, maxY);
242 int targetY = scroller.getFinalY();
243 scroller.abortAnimation();
244
245 // If the x component of the velocity is above the minimum fling velocity, use velocity to
246 // determine edge. Otherwise use its starting position
247 boolean pullRight = isFling(xVelocity, 0) ? xVelocity > 0 : isOnRightHalf(startX);
248 return new Point(pullRight ? maxX : minX, targetY);
249 }
250
251 private boolean isFling(float xVelocity, float yVelocity) {
252 int minFlingVelocity =
253 ViewConfiguration.get(context).getScaledMinimumFlingVelocity() * MIN_FLING_VELOCITY_FACTOR;
254 return getMagnitudeSquared(xVelocity, yVelocity) > minFlingVelocity * minFlingVelocity;
255 }
256
257 private boolean isOnRightHalf(float currentX) {
258 return currentX > (minX + maxX) / 2;
259 }
260
261 private void snapX() {
262 // Check if x value is closer to min or max
263 boolean pullRight = isOnRightHalf(xProperty.getValue(bubble.getWindowParams()));
264 moveXAnimation.animateToFinalPosition(pullRight ? maxX : minX);
265 }
266
267 private boolean relativeToRight(LayoutParams windowParams) {
268 return (windowParams.gravity & Gravity.RIGHT) == Gravity.RIGHT;
269 }
270
271 private boolean hasExceededTouchSlop(MotionEvent event) {
272 return getMagnitudeSquared(event.getRawX() - firstX, event.getRawY() - firstY)
273 > touchSlopSquared;
274 }
275
276 private float getMagnitudeSquared(float deltaX, float deltaY) {
277 return deltaX * deltaX + deltaY * deltaY;
278 }
279}