blob: 561d0ec717aeb7ea768c7e9c244e532cf90929a9 [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.List;
Annie Chinb9ce4d02016-12-09 15:26:41 -080031import java.util.concurrent.CopyOnWriteArrayList;
Annie Chind0f87d22016-10-24 09:04:12 -070032
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 Chinb9ce4d02016-12-09 15:26:41 -080043 // No concurrency; allow modifications while iterating.
44 private final List<DragCallback> mDragCallbacks = new CopyOnWriteArrayList<>();
Annie Chin9a211132016-11-30 12:52:06 -080045 private CloseCallback mCloseCallback;
Annie Chin09547532016-10-14 10:59:07 -070046
47 private int mDraggingState = ViewDragHelper.STATE_IDLE;
48 private int mDraggingBorder;
49 private int mVerticalRange;
50 private boolean mIsOpen;
51
Annie Chind0f87d22016-10-24 09:04:12 -070052 // Used to determine whether a touch event should be intercepted.
53 private float mInitialDownX;
54 private float mInitialDownY;
55
Annie Chin09547532016-10-14 10:59:07 -070056 public DragLayout(Context context, AttributeSet attrs) {
57 super(context, attrs);
58 }
59
60 @Override
61 protected void onFinishInflate() {
62 mDragHelper = ViewDragHelper.create(this, 1.0f, new DragHelperCallback());
63 mHistoryFrame = (FrameLayout) findViewById(R.id.history_frame);
Annie Chin09547532016-10-14 10:59:07 -070064 super.onFinishInflate();
65 }
66
67 @Override
68 protected void onLayout(boolean changed, int l, int t, int r, int b) {
69 super.onLayout(changed, l, t, r, b);
70 if (changed) {
Annie Chind0f87d22016-10-24 09:04:12 -070071 for (DragCallback c : mDragCallbacks) {
72 c.onLayout(t-b);
73 }
Annie Chin09547532016-10-14 10:59:07 -070074 if (mIsOpen) {
75 setOpen();
Annie Chind0f87d22016-10-24 09:04:12 -070076 } else {
77 setClosed();
Annie Chin09547532016-10-14 10:59:07 -070078 }
79 }
80 }
81
82 @Override
83 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
Annie Chind0f87d22016-10-24 09:04:12 -070084 int height = 0;
85 for (DragCallback c : mDragCallbacks) {
86 height += c.getDisplayHeight();
87 }
88 mVerticalRange = h - height;
Annie Chin09547532016-10-14 10:59:07 -070089 super.onSizeChanged(w, h, oldw, oldh);
90 }
91
92 @Override
93 protected Parcelable onSaveInstanceState() {
94 final Bundle bundle = new Bundle();
95 bundle.putParcelable(KEY_SUPER_STATE, super.onSaveInstanceState());
96 bundle.putBoolean(KEY_IS_OPEN, mIsOpen);
97 return bundle;
98 }
99
100 @Override
101 protected void onRestoreInstanceState(Parcelable state) {
102 if (state instanceof Bundle) {
103 final Bundle bundle = (Bundle) state;
104 mIsOpen = bundle.getBoolean(KEY_IS_OPEN);
105 state = bundle.getParcelable(KEY_SUPER_STATE);
106 }
107 super.onRestoreInstanceState(state);
108 }
109
110 @Override
111 public boolean onInterceptTouchEvent(MotionEvent event) {
Annie Chinc5b6e4f2016-12-05 13:34:14 -0800112 final int action = event.getActionMasked();
113
114 // Always handle the case of the touch gesture being complete.
115 if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
116 // Release the scroll.
117 mDragHelper.cancel();
118 return false; // Do not intercept touch event, let the child handle it
119 }
120
Annie Chind0f87d22016-10-24 09:04:12 -0700121 final float x = event.getX();
122 final float y = event.getY();
Annie Chind0f87d22016-10-24 09:04:12 -0700123
124 switch (action) {
125 case MotionEvent.ACTION_DOWN:
126 mInitialDownX = x;
127 mInitialDownY = y;
128 break;
129 case MotionEvent.ACTION_MOVE:
130 final float deltaX = Math.abs(x - mInitialDownX);
131 final float deltaY = Math.abs(y - mInitialDownY);
132 final int slop = mDragHelper.getTouchSlop();
Annie Chinc5b6e4f2016-12-05 13:34:14 -0800133 if (deltaY > slop && deltaY > deltaX) {
134 break;
135 } else {
Annie Chind0f87d22016-10-24 09:04:12 -0700136 return false;
137 }
138 }
Annie Chind0f87d22016-10-24 09:04:12 -0700139 boolean doDrag = true;
140 for (DragCallback c : mDragCallbacks) {
Annie Chinc5b6e4f2016-12-05 13:34:14 -0800141 doDrag &= c.shouldInterceptTouchEvent(event);
Annie Chind0f87d22016-10-24 09:04:12 -0700142 }
143 return doDrag && mDragHelper.shouldInterceptTouchEvent(event);
Annie Chin09547532016-10-14 10:59:07 -0700144 }
145
146 @Override
147 public boolean onTouchEvent(MotionEvent event) {
Annie Chinc5b6e4f2016-12-05 13:34:14 -0800148 mDragHelper.processTouchEvent(event);
149 return super.onTouchEvent(event);
Annie Chin09547532016-10-14 10:59:07 -0700150 }
151
152 @Override
153 public void computeScroll() {
154 if (mDragHelper.continueSettling(true)) {
155 ViewCompat.postInvalidateOnAnimation(this);
156 }
157 }
158
159 private void onStartDragging() {
Annie Chind0f87d22016-10-24 09:04:12 -0700160 for (DragCallback c : mDragCallbacks) {
Annie Chin9a211132016-11-30 12:52:06 -0800161 c.onStartDraggingOpen();
Annie Chind0f87d22016-10-24 09:04:12 -0700162 }
Annie Chin09547532016-10-14 10:59:07 -0700163 mHistoryFrame.setVisibility(VISIBLE);
164 }
165
Annie Chin09547532016-10-14 10:59:07 -0700166 public boolean isMoving() {
167 return mDraggingState == ViewDragHelper.STATE_DRAGGING
168 || mDraggingState == ViewDragHelper.STATE_SETTLING;
169 }
170
171 public boolean isOpen() {
172 return mIsOpen;
173 }
174
175 public void setOpen() {
176 mDragHelper.smoothSlideViewTo(mHistoryFrame, 0, mVerticalRange);
Annie Chin09547532016-10-14 10:59:07 -0700177 mHistoryFrame.setVisibility(VISIBLE);
178 }
179
180 public void setClosed() {
181 mDragHelper.smoothSlideViewTo(mHistoryFrame, 0, 0);
Annie Chin9a211132016-11-30 12:52:06 -0800182 }
183
184 public void setCloseCallback(CloseCallback callback) {
185 mCloseCallback = callback;
Annie Chin09547532016-10-14 10:59:07 -0700186 }
187
Annie Chind0f87d22016-10-24 09:04:12 -0700188 public void addDragCallback(DragCallback callback) {
189 mDragCallbacks.add(callback);
Annie Chin09547532016-10-14 10:59:07 -0700190 }
191
Annie Chind0f87d22016-10-24 09:04:12 -0700192 public void removeDragCallback(DragCallback callback) {
193 mDragCallbacks.remove(callback);
194 }
195
196 /**
Annie Chin9a211132016-11-30 12:52:06 -0800197 * Callback when the layout is closed.
198 * We use this to pop the HistoryFragment off the backstack.
199 * We can't use a method in DragCallback because we get ConcurrentModificationExceptions on
200 * mDragCallbacks when executePendingTransactions() is called for popping the fragment off the
201 * backstack.
202 */
203 public interface CloseCallback {
204 void onClose();
205 }
206
207 /**
Annie Chind0f87d22016-10-24 09:04:12 -0700208 * Callbacks for coordinating with the RecyclerView or HistoryFragment.
209 */
210 public interface DragCallback {
Annie Chin9a211132016-11-30 12:52:06 -0800211 // Callback when a drag to open begins.
212 void onStartDraggingOpen();
Annie Chin09547532016-10-14 10:59:07 -0700213
Annie Chind0f87d22016-10-24 09:04:12 -0700214 // Animate the RecyclerView text.
215 void whileDragging(float yFraction);
216
Annie Chind0f87d22016-10-24 09:04:12 -0700217 // Whether we should intercept the touch event
218 boolean shouldInterceptTouchEvent(MotionEvent event);
219
220 int getDisplayHeight();
221
222 void onLayout(int translation);
Annie Chin09547532016-10-14 10:59:07 -0700223 }
224
225 public class DragHelperCallback extends ViewDragHelper.Callback {
226 @Override
227 public void onViewDragStateChanged(int state) {
228 if (state == mDraggingState) {
229 // No change.
230 return;
231 }
232 if ((mDraggingState == ViewDragHelper.STATE_DRAGGING
233 || mDraggingState == ViewDragHelper.STATE_SETTLING)
234 && state == ViewDragHelper.STATE_IDLE) {
235 // The view stopped moving.
236 if (mDraggingBorder == 0) {
237 setClosed();
Annie Chin9a211132016-11-30 12:52:06 -0800238 mIsOpen = false;
Annie Chin91796232016-11-16 17:27:36 -0800239 mHistoryFrame.setVisibility(GONE);
Annie Chin9a211132016-11-30 12:52:06 -0800240 if (mCloseCallback != null) {
241 mCloseCallback.onClose();
242 }
Annie Chin09547532016-10-14 10:59:07 -0700243 } else if (mDraggingBorder == mVerticalRange) {
244 setOpen();
Annie Chin9a211132016-11-30 12:52:06 -0800245 mIsOpen = true;
Annie Chin09547532016-10-14 10:59:07 -0700246 }
Annie Chin9a211132016-11-30 12:52:06 -0800247 } else if (state == ViewDragHelper.STATE_DRAGGING && !mIsOpen) {
Annie Chin09547532016-10-14 10:59:07 -0700248 onStartDragging();
249 }
250 mDraggingState = state;
251 }
252
253 @Override
254 public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
255 mDraggingBorder = top;
Annie Chind0f87d22016-10-24 09:04:12 -0700256
257 // Animate RecyclerView text.
258 for (DragCallback c : mDragCallbacks) {
259 c.whileDragging(top / (mVerticalRange * 1.0f));
260 }
Annie Chin09547532016-10-14 10:59:07 -0700261 }
262
263 @Override
264 public int getViewVerticalDragRange(View child) {
265 return mVerticalRange;
266 }
267
268 @Override
269 public boolean tryCaptureView(View view, int i) {
270 return view.getId() == R.id.history_frame;
271 }
272
273 @Override
274 public int clampViewPositionVertical(View child, int top, int dy) {
275 final int topBound = getPaddingTop();
276 final int bottomBound = mVerticalRange;
277 return Math.min(Math.max(top, topBound), bottomBound);
278 }
279
280 @Override
281 public void onViewReleased(View releasedChild, float xvel, float yvel) {
Annie Chin09547532016-10-14 10:59:07 -0700282 boolean settleToOpen = false;
283 final float threshold = mVerticalRange / 2;
284 if (yvel > AUTO_OPEN_SPEED_LIMIT) {
285 // Speed has priority over position.
286 settleToOpen = true;
287 } else if (yvel < -AUTO_OPEN_SPEED_LIMIT) {
288 settleToOpen = false;
289 } else if (mDraggingBorder > threshold) {
290 settleToOpen = true;
291 } else if (mDraggingBorder < threshold) {
292 settleToOpen = false;
293 }
294
295 if (mDragHelper.settleCapturedViewAt(0, settleToOpen ? mVerticalRange : 0)) {
296 ViewCompat.postInvalidateOnAnimation(DragLayout.this);
297 }
298 }
299 }
300}