blob: aa17c4aa79b1d15f9412a59006fbe04ab6bdd19f [file] [log] [blame]
Jason Monk02158022016-05-27 10:47:51 -04001/*
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
15package com.android.systemui.qs;
16
17import android.content.Context;
18import android.util.AttributeSet;
19import android.view.MotionEvent;
Selim Cinek2a1cab12020-06-01 19:19:58 -070020import android.view.View;
21import android.view.ViewConfiguration;
22import android.view.ViewParent;
Jason Monk02158022016-05-27 10:47:51 -040023import android.widget.ScrollView;
24
25/**
26 * ScrollView that disallows intercepting for touches that can cause scrolling.
27 */
28public class NonInterceptingScrollView extends ScrollView {
29
Selim Cinek2a1cab12020-06-01 19:19:58 -070030 private final int mTouchSlop;
31 private float mDownY;
32
Jason Monk02158022016-05-27 10:47:51 -040033 public NonInterceptingScrollView(Context context, AttributeSet attrs) {
34 super(context, attrs);
Selim Cinek2a1cab12020-06-01 19:19:58 -070035 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Jason Monk02158022016-05-27 10:47:51 -040036 }
37
38 @Override
39 public boolean onTouchEvent(MotionEvent ev) {
40 int action = ev.getActionMasked();
41 switch (action) {
42 case MotionEvent.ACTION_DOWN:
Jason Monk35742d82016-05-31 10:30:46 -040043 if (canScrollVertically(1)) {
Selim Cinek2a1cab12020-06-01 19:19:58 -070044 // 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 Monk02158022016-05-27 10:47:51 -040049 }
50 break;
51 }
52 return super.onTouchEvent(ev);
53 }
Selim Cinek2a1cab12020-06-01 19:19:58 -070054
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 Monk02158022016-05-27 10:47:51 -040092}