Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.calculator2; |
| 18 | |
| 19 | import android.content.Context; |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 20 | import android.os.Bundle; |
| 21 | import android.os.Parcelable; |
| 22 | import android.support.v4.view.ViewCompat; |
| 23 | import android.support.v4.widget.ViewDragHelper; |
| 24 | import android.util.AttributeSet; |
| 25 | import android.view.MotionEvent; |
| 26 | import android.view.View; |
| 27 | import android.widget.FrameLayout; |
| 28 | import android.widget.RelativeLayout; |
| 29 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 30 | import java.util.ArrayList; |
| 31 | import java.util.List; |
| 32 | |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 33 | public class DragLayout extends RelativeLayout { |
| 34 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 35 | private static final String TAG = "DragLayout"; |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 36 | 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 Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 40 | private FrameLayout mHistoryFrame; |
| 41 | private ViewDragHelper mDragHelper; |
| 42 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 43 | private final List<DragCallback> mDragCallbacks = new ArrayList<>(); |
Annie Chin | 9a21113 | 2016-11-30 12:52:06 -0800 | [diff] [blame] | 44 | private CloseCallback mCloseCallback; |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 45 | |
| 46 | private int mDraggingState = ViewDragHelper.STATE_IDLE; |
| 47 | private int mDraggingBorder; |
| 48 | private int mVerticalRange; |
| 49 | private boolean mIsOpen; |
| 50 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 51 | // Used to determine whether a touch event should be intercepted. |
| 52 | private float mInitialDownX; |
| 53 | private float mInitialDownY; |
| 54 | |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 55 | 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 Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 63 | 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 Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 70 | for (DragCallback c : mDragCallbacks) { |
| 71 | c.onLayout(t-b); |
| 72 | } |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 73 | if (mIsOpen) { |
| 74 | setOpen(); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 75 | } else { |
| 76 | setClosed(); |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 83 | int height = 0; |
| 84 | for (DragCallback c : mDragCallbacks) { |
| 85 | height += c.getDisplayHeight(); |
| 86 | } |
| 87 | mVerticalRange = h - height; |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 88 | 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 Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 111 | // 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 Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public boolean onTouchEvent(MotionEvent event) { |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 141 | boolean doIntercept = true; |
| 142 | for (DragCallback c : mDragCallbacks) { |
| 143 | doIntercept &= c.shouldInterceptTouchEvent(event); |
| 144 | } |
| 145 | if (doIntercept || isMoving()) { |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 146 | 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 Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 161 | for (DragCallback c : mDragCallbacks) { |
Annie Chin | 9a21113 | 2016-11-30 12:52:06 -0800 | [diff] [blame] | 162 | c.onStartDraggingOpen(); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 163 | } |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 164 | mHistoryFrame.setVisibility(VISIBLE); |
| 165 | } |
| 166 | |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 167 | 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 Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 178 | mHistoryFrame.setVisibility(VISIBLE); |
| 179 | } |
| 180 | |
| 181 | public void setClosed() { |
| 182 | mDragHelper.smoothSlideViewTo(mHistoryFrame, 0, 0); |
Annie Chin | 9a21113 | 2016-11-30 12:52:06 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | public void setCloseCallback(CloseCallback callback) { |
| 186 | mCloseCallback = callback; |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 189 | public void addDragCallback(DragCallback callback) { |
| 190 | mDragCallbacks.add(callback); |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 193 | public void removeDragCallback(DragCallback callback) { |
| 194 | mDragCallbacks.remove(callback); |
| 195 | } |
| 196 | |
| 197 | /** |
Annie Chin | 9a21113 | 2016-11-30 12:52:06 -0800 | [diff] [blame] | 198 | * 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 Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 209 | * Callbacks for coordinating with the RecyclerView or HistoryFragment. |
| 210 | */ |
| 211 | public interface DragCallback { |
Annie Chin | 9a21113 | 2016-11-30 12:52:06 -0800 | [diff] [blame] | 212 | // Callback when a drag to open begins. |
| 213 | void onStartDraggingOpen(); |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 214 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 215 | // Animate the RecyclerView text. |
| 216 | void whileDragging(float yFraction); |
| 217 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 218 | // 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 Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 227 | } |
| 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 Chin | 9a21113 | 2016-11-30 12:52:06 -0800 | [diff] [blame] | 242 | mIsOpen = false; |
Annie Chin | 9179623 | 2016-11-16 17:27:36 -0800 | [diff] [blame] | 243 | mHistoryFrame.setVisibility(GONE); |
Annie Chin | 9a21113 | 2016-11-30 12:52:06 -0800 | [diff] [blame] | 244 | if (mCloseCallback != null) { |
| 245 | mCloseCallback.onClose(); |
| 246 | } |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 247 | } else if (mDraggingBorder == mVerticalRange) { |
| 248 | setOpen(); |
Annie Chin | 9a21113 | 2016-11-30 12:52:06 -0800 | [diff] [blame] | 249 | mIsOpen = true; |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 250 | } |
Annie Chin | 9a21113 | 2016-11-30 12:52:06 -0800 | [diff] [blame] | 251 | } else if (state == ViewDragHelper.STATE_DRAGGING && !mIsOpen) { |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 252 | 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 Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 260 | |
| 261 | // Animate RecyclerView text. |
| 262 | for (DragCallback c : mDragCallbacks) { |
| 263 | c.whileDragging(top / (mVerticalRange * 1.0f)); |
| 264 | } |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 265 | } |
| 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 Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 286 | 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 | } |