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.List; |
Annie Chin | b9ce4d0 | 2016-12-09 15:26:41 -0800 | [diff] [blame^] | 31 | import java.util.concurrent.CopyOnWriteArrayList; |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 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 | b9ce4d0 | 2016-12-09 15:26:41 -0800 | [diff] [blame^] | 43 | // No concurrency; allow modifications while iterating. |
| 44 | private final List<DragCallback> mDragCallbacks = new CopyOnWriteArrayList<>(); |
Annie Chin | 9a21113 | 2016-11-30 12:52:06 -0800 | [diff] [blame] | 45 | private CloseCallback mCloseCallback; |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 46 | |
| 47 | private int mDraggingState = ViewDragHelper.STATE_IDLE; |
| 48 | private int mDraggingBorder; |
| 49 | private int mVerticalRange; |
| 50 | private boolean mIsOpen; |
| 51 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 52 | // Used to determine whether a touch event should be intercepted. |
| 53 | private float mInitialDownX; |
| 54 | private float mInitialDownY; |
| 55 | |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 56 | 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 Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 64 | 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 Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 71 | for (DragCallback c : mDragCallbacks) { |
| 72 | c.onLayout(t-b); |
| 73 | } |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 74 | if (mIsOpen) { |
| 75 | setOpen(); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 76 | } else { |
| 77 | setClosed(); |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 84 | int height = 0; |
| 85 | for (DragCallback c : mDragCallbacks) { |
| 86 | height += c.getDisplayHeight(); |
| 87 | } |
| 88 | mVerticalRange = h - height; |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 89 | 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 Chin | c5b6e4f | 2016-12-05 13:34:14 -0800 | [diff] [blame] | 112 | 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 Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 121 | final float x = event.getX(); |
| 122 | final float y = event.getY(); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 123 | |
| 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 Chin | c5b6e4f | 2016-12-05 13:34:14 -0800 | [diff] [blame] | 133 | if (deltaY > slop && deltaY > deltaX) { |
| 134 | break; |
| 135 | } else { |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 136 | return false; |
| 137 | } |
| 138 | } |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 139 | boolean doDrag = true; |
| 140 | for (DragCallback c : mDragCallbacks) { |
Annie Chin | c5b6e4f | 2016-12-05 13:34:14 -0800 | [diff] [blame] | 141 | doDrag &= c.shouldInterceptTouchEvent(event); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 142 | } |
| 143 | return doDrag && mDragHelper.shouldInterceptTouchEvent(event); |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public boolean onTouchEvent(MotionEvent event) { |
Annie Chin | c5b6e4f | 2016-12-05 13:34:14 -0800 | [diff] [blame] | 148 | mDragHelper.processTouchEvent(event); |
| 149 | return super.onTouchEvent(event); |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public void computeScroll() { |
| 154 | if (mDragHelper.continueSettling(true)) { |
| 155 | ViewCompat.postInvalidateOnAnimation(this); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | private void onStartDragging() { |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 160 | for (DragCallback c : mDragCallbacks) { |
Annie Chin | 9a21113 | 2016-11-30 12:52:06 -0800 | [diff] [blame] | 161 | c.onStartDraggingOpen(); |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 162 | } |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 163 | mHistoryFrame.setVisibility(VISIBLE); |
| 164 | } |
| 165 | |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 166 | 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 Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 177 | mHistoryFrame.setVisibility(VISIBLE); |
| 178 | } |
| 179 | |
| 180 | public void setClosed() { |
| 181 | mDragHelper.smoothSlideViewTo(mHistoryFrame, 0, 0); |
Annie Chin | 9a21113 | 2016-11-30 12:52:06 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | public void setCloseCallback(CloseCallback callback) { |
| 185 | mCloseCallback = callback; |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 188 | public void addDragCallback(DragCallback callback) { |
| 189 | mDragCallbacks.add(callback); |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 192 | public void removeDragCallback(DragCallback callback) { |
| 193 | mDragCallbacks.remove(callback); |
| 194 | } |
| 195 | |
| 196 | /** |
Annie Chin | 9a21113 | 2016-11-30 12:52:06 -0800 | [diff] [blame] | 197 | * 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 Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 208 | * Callbacks for coordinating with the RecyclerView or HistoryFragment. |
| 209 | */ |
| 210 | public interface DragCallback { |
Annie Chin | 9a21113 | 2016-11-30 12:52:06 -0800 | [diff] [blame] | 211 | // Callback when a drag to open begins. |
| 212 | void onStartDraggingOpen(); |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 213 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 214 | // Animate the RecyclerView text. |
| 215 | void whileDragging(float yFraction); |
| 216 | |
Annie Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 217 | // Whether we should intercept the touch event |
| 218 | boolean shouldInterceptTouchEvent(MotionEvent event); |
| 219 | |
| 220 | int getDisplayHeight(); |
| 221 | |
| 222 | void onLayout(int translation); |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 223 | } |
| 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 Chin | 9a21113 | 2016-11-30 12:52:06 -0800 | [diff] [blame] | 238 | mIsOpen = false; |
Annie Chin | 9179623 | 2016-11-16 17:27:36 -0800 | [diff] [blame] | 239 | mHistoryFrame.setVisibility(GONE); |
Annie Chin | 9a21113 | 2016-11-30 12:52:06 -0800 | [diff] [blame] | 240 | if (mCloseCallback != null) { |
| 241 | mCloseCallback.onClose(); |
| 242 | } |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 243 | } else if (mDraggingBorder == mVerticalRange) { |
| 244 | setOpen(); |
Annie Chin | 9a21113 | 2016-11-30 12:52:06 -0800 | [diff] [blame] | 245 | mIsOpen = true; |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 246 | } |
Annie Chin | 9a21113 | 2016-11-30 12:52:06 -0800 | [diff] [blame] | 247 | } else if (state == ViewDragHelper.STATE_DRAGGING && !mIsOpen) { |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 248 | 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 Chin | d0f87d2 | 2016-10-24 09:04:12 -0700 | [diff] [blame] | 256 | |
| 257 | // Animate RecyclerView text. |
| 258 | for (DragCallback c : mDragCallbacks) { |
| 259 | c.whileDragging(top / (mVerticalRange * 1.0f)); |
| 260 | } |
Annie Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 261 | } |
| 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 Chin | 0954753 | 2016-10-14 10:59:07 -0700 | [diff] [blame] | 282 | 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 | } |