blob: c96d139d1a818cf3522a25725140424b80945bda [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 Chinc5b6e4f2016-12-05 13:34:14 -0800111 final int action = event.getActionMasked();
112
113 // Always handle the case of the touch gesture being complete.
114 if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
115 // Release the scroll.
116 mDragHelper.cancel();
117 return false; // Do not intercept touch event, let the child handle it
118 }
119
Annie Chind0f87d22016-10-24 09:04:12 -0700120 final float x = event.getX();
121 final float y = event.getY();
Annie Chind0f87d22016-10-24 09:04:12 -0700122
123 switch (action) {
124 case MotionEvent.ACTION_DOWN:
125 mInitialDownX = x;
126 mInitialDownY = y;
127 break;
128 case MotionEvent.ACTION_MOVE:
129 final float deltaX = Math.abs(x - mInitialDownX);
130 final float deltaY = Math.abs(y - mInitialDownY);
131 final int slop = mDragHelper.getTouchSlop();
Annie Chinc5b6e4f2016-12-05 13:34:14 -0800132 if (deltaY > slop && deltaY > deltaX) {
133 break;
134 } else {
Annie Chind0f87d22016-10-24 09:04:12 -0700135 return false;
136 }
137 }
Annie Chind0f87d22016-10-24 09:04:12 -0700138 boolean doDrag = true;
139 for (DragCallback c : mDragCallbacks) {
Annie Chinc5b6e4f2016-12-05 13:34:14 -0800140 doDrag &= c.shouldInterceptTouchEvent(event);
Annie Chind0f87d22016-10-24 09:04:12 -0700141 }
142 return doDrag && mDragHelper.shouldInterceptTouchEvent(event);
Annie Chin09547532016-10-14 10:59:07 -0700143 }
144
145 @Override
146 public boolean onTouchEvent(MotionEvent event) {
Annie Chinc5b6e4f2016-12-05 13:34:14 -0800147 mDragHelper.processTouchEvent(event);
148 return super.onTouchEvent(event);
Annie Chin09547532016-10-14 10:59:07 -0700149 }
150
151 @Override
152 public void computeScroll() {
153 if (mDragHelper.continueSettling(true)) {
154 ViewCompat.postInvalidateOnAnimation(this);
155 }
156 }
157
158 private void onStartDragging() {
Annie Chind0f87d22016-10-24 09:04:12 -0700159 for (DragCallback c : mDragCallbacks) {
Annie Chin9a211132016-11-30 12:52:06 -0800160 c.onStartDraggingOpen();
Annie Chind0f87d22016-10-24 09:04:12 -0700161 }
Annie Chin09547532016-10-14 10:59:07 -0700162 mHistoryFrame.setVisibility(VISIBLE);
163 }
164
Annie Chin09547532016-10-14 10:59:07 -0700165 public boolean isMoving() {
166 return mDraggingState == ViewDragHelper.STATE_DRAGGING
167 || mDraggingState == ViewDragHelper.STATE_SETTLING;
168 }
169
170 public boolean isOpen() {
171 return mIsOpen;
172 }
173
174 public void setOpen() {
175 mDragHelper.smoothSlideViewTo(mHistoryFrame, 0, mVerticalRange);
Annie Chin09547532016-10-14 10:59:07 -0700176 mHistoryFrame.setVisibility(VISIBLE);
177 }
178
179 public void setClosed() {
180 mDragHelper.smoothSlideViewTo(mHistoryFrame, 0, 0);
Annie Chin9a211132016-11-30 12:52:06 -0800181 }
182
183 public void setCloseCallback(CloseCallback callback) {
184 mCloseCallback = callback;
Annie Chin09547532016-10-14 10:59:07 -0700185 }
186
Annie Chind0f87d22016-10-24 09:04:12 -0700187 public void addDragCallback(DragCallback callback) {
188 mDragCallbacks.add(callback);
Annie Chin09547532016-10-14 10:59:07 -0700189 }
190
Annie Chind0f87d22016-10-24 09:04:12 -0700191 public void removeDragCallback(DragCallback callback) {
192 mDragCallbacks.remove(callback);
193 }
194
195 /**
Annie Chin9a211132016-11-30 12:52:06 -0800196 * Callback when the layout is closed.
197 * We use this to pop the HistoryFragment off the backstack.
198 * We can't use a method in DragCallback because we get ConcurrentModificationExceptions on
199 * mDragCallbacks when executePendingTransactions() is called for popping the fragment off the
200 * backstack.
201 */
202 public interface CloseCallback {
203 void onClose();
204 }
205
206 /**
Annie Chind0f87d22016-10-24 09:04:12 -0700207 * Callbacks for coordinating with the RecyclerView or HistoryFragment.
208 */
209 public interface DragCallback {
Annie Chin9a211132016-11-30 12:52:06 -0800210 // Callback when a drag to open begins.
211 void onStartDraggingOpen();
Annie Chin09547532016-10-14 10:59:07 -0700212
Annie Chind0f87d22016-10-24 09:04:12 -0700213 // Animate the RecyclerView text.
214 void whileDragging(float yFraction);
215
Annie Chind0f87d22016-10-24 09:04:12 -0700216 // Whether we should intercept the touch event
217 boolean shouldInterceptTouchEvent(MotionEvent event);
218
219 int getDisplayHeight();
220
221 void onLayout(int translation);
Annie Chin09547532016-10-14 10:59:07 -0700222 }
223
224 public class DragHelperCallback extends ViewDragHelper.Callback {
225 @Override
226 public void onViewDragStateChanged(int state) {
227 if (state == mDraggingState) {
228 // No change.
229 return;
230 }
231 if ((mDraggingState == ViewDragHelper.STATE_DRAGGING
232 || mDraggingState == ViewDragHelper.STATE_SETTLING)
233 && state == ViewDragHelper.STATE_IDLE) {
234 // The view stopped moving.
235 if (mDraggingBorder == 0) {
236 setClosed();
Annie Chin9a211132016-11-30 12:52:06 -0800237 mIsOpen = false;
Annie Chin91796232016-11-16 17:27:36 -0800238 mHistoryFrame.setVisibility(GONE);
Annie Chin9a211132016-11-30 12:52:06 -0800239 if (mCloseCallback != null) {
240 mCloseCallback.onClose();
241 }
Annie Chin09547532016-10-14 10:59:07 -0700242 } else if (mDraggingBorder == mVerticalRange) {
243 setOpen();
Annie Chin9a211132016-11-30 12:52:06 -0800244 mIsOpen = true;
Annie Chin09547532016-10-14 10:59:07 -0700245 }
Annie Chin9a211132016-11-30 12:52:06 -0800246 } else if (state == ViewDragHelper.STATE_DRAGGING && !mIsOpen) {
Annie Chin09547532016-10-14 10:59:07 -0700247 onStartDragging();
248 }
249 mDraggingState = state;
250 }
251
252 @Override
253 public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
254 mDraggingBorder = top;
Annie Chind0f87d22016-10-24 09:04:12 -0700255
256 // Animate RecyclerView text.
257 for (DragCallback c : mDragCallbacks) {
258 c.whileDragging(top / (mVerticalRange * 1.0f));
259 }
Annie Chin09547532016-10-14 10:59:07 -0700260 }
261
262 @Override
263 public int getViewVerticalDragRange(View child) {
264 return mVerticalRange;
265 }
266
267 @Override
268 public boolean tryCaptureView(View view, int i) {
269 return view.getId() == R.id.history_frame;
270 }
271
272 @Override
273 public int clampViewPositionVertical(View child, int top, int dy) {
274 final int topBound = getPaddingTop();
275 final int bottomBound = mVerticalRange;
276 return Math.min(Math.max(top, topBound), bottomBound);
277 }
278
279 @Override
280 public void onViewReleased(View releasedChild, float xvel, float yvel) {
Annie Chin09547532016-10-14 10:59:07 -0700281 boolean settleToOpen = false;
282 final float threshold = mVerticalRange / 2;
283 if (yvel > AUTO_OPEN_SPEED_LIMIT) {
284 // Speed has priority over position.
285 settleToOpen = true;
286 } else if (yvel < -AUTO_OPEN_SPEED_LIMIT) {
287 settleToOpen = false;
288 } else if (mDraggingBorder > threshold) {
289 settleToOpen = true;
290 } else if (mDraggingBorder < threshold) {
291 settleToOpen = false;
292 }
293
294 if (mDragHelper.settleCapturedViewAt(0, settleToOpen ? mVerticalRange : 0)) {
295 ViewCompat.postInvalidateOnAnimation(DragLayout.this);
296 }
297 }
298 }
299}