Jason Monk | 0215802 | 2016-05-27 10:47:51 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | |
| 15 | package com.android.systemui.qs; |
| 16 | |
| 17 | import android.content.Context; |
| 18 | import android.util.AttributeSet; |
| 19 | import android.view.MotionEvent; |
Selim Cinek | 2a1cab1 | 2020-06-01 19:19:58 -0700 | [diff] [blame] | 20 | import android.view.View; |
| 21 | import android.view.ViewConfiguration; |
| 22 | import android.view.ViewParent; |
Jason Monk | 0215802 | 2016-05-27 10:47:51 -0400 | [diff] [blame] | 23 | import android.widget.ScrollView; |
| 24 | |
| 25 | /** |
| 26 | * ScrollView that disallows intercepting for touches that can cause scrolling. |
| 27 | */ |
| 28 | public class NonInterceptingScrollView extends ScrollView { |
| 29 | |
Selim Cinek | 2a1cab1 | 2020-06-01 19:19:58 -0700 | [diff] [blame] | 30 | private final int mTouchSlop; |
| 31 | private float mDownY; |
| 32 | |
Jason Monk | 0215802 | 2016-05-27 10:47:51 -0400 | [diff] [blame] | 33 | public NonInterceptingScrollView(Context context, AttributeSet attrs) { |
| 34 | super(context, attrs); |
Selim Cinek | 2a1cab1 | 2020-06-01 19:19:58 -0700 | [diff] [blame] | 35 | mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); |
Jason Monk | 0215802 | 2016-05-27 10:47:51 -0400 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | @Override |
| 39 | public boolean onTouchEvent(MotionEvent ev) { |
| 40 | int action = ev.getActionMasked(); |
| 41 | switch (action) { |
| 42 | case MotionEvent.ACTION_DOWN: |
Jason Monk | 35742d8 | 2016-05-31 10:30:46 -0400 | [diff] [blame] | 43 | if (canScrollVertically(1)) { |
Selim Cinek | 2a1cab1 | 2020-06-01 19:19:58 -0700 | [diff] [blame] | 44 | // If we can scroll down, make sure we're not intercepted by the parent |
| 45 | final ViewParent parent = getParent(); |
| 46 | if (parent != null) { |
| 47 | parent.requestDisallowInterceptTouchEvent(true); |
| 48 | } |
Jason Monk | 0215802 | 2016-05-27 10:47:51 -0400 | [diff] [blame] | 49 | } |
| 50 | break; |
| 51 | } |
| 52 | return super.onTouchEvent(ev); |
| 53 | } |
Selim Cinek | 2a1cab1 | 2020-06-01 19:19:58 -0700 | [diff] [blame] | 54 | |
| 55 | @Override |
| 56 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 57 | // If there's a touch on this view and we can scroll down, we don't want to be intercepted |
| 58 | int action = ev.getActionMasked(); |
| 59 | switch (action) { |
| 60 | case MotionEvent.ACTION_DOWN: |
| 61 | // If we can scroll down, make sure non of our parents intercepts us. |
| 62 | if (canScrollVertically(1)) { |
| 63 | final ViewParent parent = getParent(); |
| 64 | if (parent != null) { |
| 65 | parent.requestDisallowInterceptTouchEvent(true); |
| 66 | } |
| 67 | } |
| 68 | mDownY = ev.getY(); |
| 69 | break; |
| 70 | case MotionEvent.ACTION_MOVE: { |
| 71 | final int y = (int) ev.getY(); |
| 72 | final float yDiff = y - mDownY; |
| 73 | if (yDiff < -mTouchSlop && !canScrollVertically(1)) { |
| 74 | // Don't intercept touches that are overscrolling. |
| 75 | return false; |
| 76 | } |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | return super.onInterceptTouchEvent(ev); |
| 81 | } |
| 82 | |
| 83 | public int getScrollRange() { |
| 84 | int scrollRange = 0; |
| 85 | if (getChildCount() > 0) { |
| 86 | View child = getChildAt(0); |
| 87 | scrollRange = Math.max(0, |
| 88 | child.getHeight() - (getHeight() - mPaddingBottom - mPaddingTop)); |
| 89 | } |
| 90 | return scrollRange; |
| 91 | } |
Jason Monk | 0215802 | 2016-05-27 10:47:51 -0400 | [diff] [blame] | 92 | } |