blob: b07d087bc75a662aa6de023a8c4d68f3b6ba9d40 [file] [log] [blame]
Annie Chin09547532016-10-14 10:59:07 -07001/*
2 * Copyright (C) 2016 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.calculator2;
18
19import android.content.Context;
Annie Chin09547532016-10-14 10:59:07 -070020import android.os.Bundle;
21import android.os.Parcelable;
22import android.support.v4.view.ViewCompat;
23import android.support.v4.widget.ViewDragHelper;
24import android.util.AttributeSet;
25import android.view.MotionEvent;
26import android.view.View;
27import android.widget.FrameLayout;
28import android.widget.RelativeLayout;
29
Annie Chind0f87d22016-10-24 09:04:12 -070030import java.util.ArrayList;
31import java.util.List;
32
Annie Chin09547532016-10-14 10:59:07 -070033public class DragLayout extends RelativeLayout {
34
Annie Chind0f87d22016-10-24 09:04:12 -070035 private static final String TAG = "DragLayout";
Annie Chin09547532016-10-14 10:59:07 -070036 private static final double AUTO_OPEN_SPEED_LIMIT = 800.0;
37 private static final String KEY_IS_OPEN = "IS_OPEN";
38 private static final String KEY_SUPER_STATE = "SUPER_STATE";
39
Annie Chin09547532016-10-14 10:59:07 -070040 private FrameLayout mHistoryFrame;
41 private ViewDragHelper mDragHelper;
42
Annie Chind0f87d22016-10-24 09:04:12 -070043 private final List<DragCallback> mDragCallbacks = new ArrayList<>();
Annie Chin9a211132016-11-30 12:52:06 -080044 private CloseCallback mCloseCallback;
Annie Chin09547532016-10-14 10:59:07 -070045
46 private int mDraggingState = ViewDragHelper.STATE_IDLE;
47 private int mDraggingBorder;
48 private int mVerticalRange;
49 private boolean mIsOpen;
50
Annie Chind0f87d22016-10-24 09:04:12 -070051 // Used to determine whether a touch event should be intercepted.
52 private float mInitialDownX;
53 private float mInitialDownY;
54
Annie Chin09547532016-10-14 10:59:07 -070055 public DragLayout(Context context, AttributeSet attrs) {
56 super(context, attrs);
57 }
58
59 @Override
60 protected void onFinishInflate() {
61 mDragHelper = ViewDragHelper.create(this, 1.0f, new DragHelperCallback());
62 mHistoryFrame = (FrameLayout) findViewById(R.id.history_frame);
Annie Chin09547532016-10-14 10:59:07 -070063 super.onFinishInflate();
64 }
65
66 @Override
67 protected void onLayout(boolean changed, int l, int t, int r, int b) {
68 super.onLayout(changed, l, t, r, b);
69 if (changed) {
Annie Chind0f87d22016-10-24 09:04:12 -070070 for (DragCallback c : mDragCallbacks) {
71 c.onLayout(t-b);
72 }
Annie Chin09547532016-10-14 10:59:07 -070073 if (mIsOpen) {
74 setOpen();
Annie Chind0f87d22016-10-24 09:04:12 -070075 } else {
76 setClosed();
Annie Chin09547532016-10-14 10:59:07 -070077 }
78 }
79 }
80
81 @Override
82 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
Annie Chind0f87d22016-10-24 09:04:12 -070083 int height = 0;
84 for (DragCallback c : mDragCallbacks) {
85 height += c.getDisplayHeight();
86 }
87 mVerticalRange = h - height;
Annie Chin09547532016-10-14 10:59:07 -070088 super.onSizeChanged(w, h, oldw, oldh);
89 }
90
91 @Override
92 protected Parcelable onSaveInstanceState() {
93 final Bundle bundle = new Bundle();
94 bundle.putParcelable(KEY_SUPER_STATE, super.onSaveInstanceState());
95 bundle.putBoolean(KEY_IS_OPEN, mIsOpen);
96 return bundle;
97 }
98
99 @Override
100 protected void onRestoreInstanceState(Parcelable state) {
101 if (state instanceof Bundle) {
102 final Bundle bundle = (Bundle) state;
103 mIsOpen = bundle.getBoolean(KEY_IS_OPEN);
104 state = bundle.getParcelable(KEY_SUPER_STATE);
105 }
106 super.onRestoreInstanceState(state);
107 }
108
109 @Override
110 public boolean onInterceptTouchEvent(MotionEvent event) {
Annie Chind0f87d22016-10-24 09:04:12 -0700111 // First verify that we don't have a large deltaX (that the user is not trying to
112 // horizontally scroll).
113 final float x = event.getX();
114 final float y = event.getY();
115 final int action = event.getAction();
116
117 switch (action) {
118 case MotionEvent.ACTION_DOWN:
119 mInitialDownX = x;
120 mInitialDownY = y;
121 break;
122 case MotionEvent.ACTION_MOVE:
123 final float deltaX = Math.abs(x - mInitialDownX);
124 final float deltaY = Math.abs(y - mInitialDownY);
125 final int slop = mDragHelper.getTouchSlop();
126 if (deltaY > slop && deltaX > deltaY) {
127 mDragHelper.cancel();
128 return false;
129 }
130 }
131
132 boolean doDrag = true;
133 for (DragCallback c : mDragCallbacks) {
134 doDrag &= c.allowDrag(event);
135 }
136 return doDrag && mDragHelper.shouldInterceptTouchEvent(event);
Annie Chin09547532016-10-14 10:59:07 -0700137 }
138
139 @Override
140 public boolean onTouchEvent(MotionEvent event) {
Annie Chind0f87d22016-10-24 09:04:12 -0700141 boolean doIntercept = true;
142 for (DragCallback c : mDragCallbacks) {
143 doIntercept &= c.shouldInterceptTouchEvent(event);
144 }
145 if (doIntercept || isMoving()) {
Annie Chin09547532016-10-14 10:59:07 -0700146 mDragHelper.processTouchEvent(event);
147 return true;
148 } else {
149 return super.onTouchEvent(event);
150 }
151 }
152
153 @Override
154 public void computeScroll() {
155 if (mDragHelper.continueSettling(true)) {
156 ViewCompat.postInvalidateOnAnimation(this);
157 }
158 }
159
160 private void onStartDragging() {
Annie Chind0f87d22016-10-24 09:04:12 -0700161 for (DragCallback c : mDragCallbacks) {
Annie Chin9a211132016-11-30 12:52:06 -0800162 c.onStartDraggingOpen();
Annie Chind0f87d22016-10-24 09:04:12 -0700163 }
Annie Chin09547532016-10-14 10:59:07 -0700164 mHistoryFrame.setVisibility(VISIBLE);
165 }
166
Annie Chin09547532016-10-14 10:59:07 -0700167 public boolean isMoving() {
168 return mDraggingState == ViewDragHelper.STATE_DRAGGING
169 || mDraggingState == ViewDragHelper.STATE_SETTLING;
170 }
171
172 public boolean isOpen() {
173 return mIsOpen;
174 }
175
176 public void setOpen() {
177 mDragHelper.smoothSlideViewTo(mHistoryFrame, 0, mVerticalRange);
Annie Chin09547532016-10-14 10:59:07 -0700178 mHistoryFrame.setVisibility(VISIBLE);
179 }
180
181 public void setClosed() {
182 mDragHelper.smoothSlideViewTo(mHistoryFrame, 0, 0);
Annie Chin9a211132016-11-30 12:52:06 -0800183 }
184
185 public void setCloseCallback(CloseCallback callback) {
186 mCloseCallback = callback;
Annie Chin09547532016-10-14 10:59:07 -0700187 }
188
Annie Chind0f87d22016-10-24 09:04:12 -0700189 public void addDragCallback(DragCallback callback) {
190 mDragCallbacks.add(callback);
Annie Chin09547532016-10-14 10:59:07 -0700191 }
192
Annie Chind0f87d22016-10-24 09:04:12 -0700193 public void removeDragCallback(DragCallback callback) {
194 mDragCallbacks.remove(callback);
195 }
196
197 /**
Annie Chin9a211132016-11-30 12:52:06 -0800198 * Callback when the layout is closed.
199 * We use this to pop the HistoryFragment off the backstack.
200 * We can't use a method in DragCallback because we get ConcurrentModificationExceptions on
201 * mDragCallbacks when executePendingTransactions() is called for popping the fragment off the
202 * backstack.
203 */
204 public interface CloseCallback {
205 void onClose();
206 }
207
208 /**
Annie Chind0f87d22016-10-24 09:04:12 -0700209 * Callbacks for coordinating with the RecyclerView or HistoryFragment.
210 */
211 public interface DragCallback {
Annie Chin9a211132016-11-30 12:52:06 -0800212 // Callback when a drag to open begins.
213 void onStartDraggingOpen();
Annie Chin09547532016-10-14 10:59:07 -0700214
Annie Chind0f87d22016-10-24 09:04:12 -0700215 // Animate the RecyclerView text.
216 void whileDragging(float yFraction);
217
Annie Chind0f87d22016-10-24 09:04:12 -0700218 // Whether we should allow the drag to happen
219 boolean allowDrag(MotionEvent event);
220
221 // Whether we should intercept the touch event
222 boolean shouldInterceptTouchEvent(MotionEvent event);
223
224 int getDisplayHeight();
225
226 void onLayout(int translation);
Annie Chin09547532016-10-14 10:59:07 -0700227 }
228
229 public class DragHelperCallback extends ViewDragHelper.Callback {
230 @Override
231 public void onViewDragStateChanged(int state) {
232 if (state == mDraggingState) {
233 // No change.
234 return;
235 }
236 if ((mDraggingState == ViewDragHelper.STATE_DRAGGING
237 || mDraggingState == ViewDragHelper.STATE_SETTLING)
238 && state == ViewDragHelper.STATE_IDLE) {
239 // The view stopped moving.
240 if (mDraggingBorder == 0) {
241 setClosed();
Annie Chin9a211132016-11-30 12:52:06 -0800242 mIsOpen = false;
Annie Chin91796232016-11-16 17:27:36 -0800243 mHistoryFrame.setVisibility(GONE);
Annie Chin9a211132016-11-30 12:52:06 -0800244 if (mCloseCallback != null) {
245 mCloseCallback.onClose();
246 }
Annie Chin09547532016-10-14 10:59:07 -0700247 } else if (mDraggingBorder == mVerticalRange) {
248 setOpen();
Annie Chin9a211132016-11-30 12:52:06 -0800249 mIsOpen = true;
Annie Chin09547532016-10-14 10:59:07 -0700250 }
Annie Chin9a211132016-11-30 12:52:06 -0800251 } else if (state == ViewDragHelper.STATE_DRAGGING && !mIsOpen) {
Annie Chin09547532016-10-14 10:59:07 -0700252 onStartDragging();
253 }
254 mDraggingState = state;
255 }
256
257 @Override
258 public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
259 mDraggingBorder = top;
Annie Chind0f87d22016-10-24 09:04:12 -0700260
261 // Animate RecyclerView text.
262 for (DragCallback c : mDragCallbacks) {
263 c.whileDragging(top / (mVerticalRange * 1.0f));
264 }
Annie Chin09547532016-10-14 10:59:07 -0700265 }
266
267 @Override
268 public int getViewVerticalDragRange(View child) {
269 return mVerticalRange;
270 }
271
272 @Override
273 public boolean tryCaptureView(View view, int i) {
274 return view.getId() == R.id.history_frame;
275 }
276
277 @Override
278 public int clampViewPositionVertical(View child, int top, int dy) {
279 final int topBound = getPaddingTop();
280 final int bottomBound = mVerticalRange;
281 return Math.min(Math.max(top, topBound), bottomBound);
282 }
283
284 @Override
285 public void onViewReleased(View releasedChild, float xvel, float yvel) {
Annie Chin09547532016-10-14 10:59:07 -0700286 boolean settleToOpen = false;
287 final float threshold = mVerticalRange / 2;
288 if (yvel > AUTO_OPEN_SPEED_LIMIT) {
289 // Speed has priority over position.
290 settleToOpen = true;
291 } else if (yvel < -AUTO_OPEN_SPEED_LIMIT) {
292 settleToOpen = false;
293 } else if (mDraggingBorder > threshold) {
294 settleToOpen = true;
295 } else if (mDraggingBorder < threshold) {
296 settleToOpen = false;
297 }
298
299 if (mDragHelper.settleCapturedViewAt(0, settleToOpen ? mVerticalRange : 0)) {
300 ViewCompat.postInvalidateOnAnimation(DragLayout.this);
301 }
302 }
303 }
304}