blob: b361ab456b2a39be43fda36ba5b55fac016c540b [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 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 android.view;
18
Mathew Inwoode5ad5982018-08-17 15:07:52 +010019import android.annotation.UnsupportedAppUsage;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021
22/**
23 * Helper class to handle situations where you want a view to have a larger touch area than its
24 * actual view bounds. The view whose touch area is changed is called the delegate view. This
25 * class should be used by an ancestor of the delegate. To use a TouchDelegate, first create an
26 * instance that specifies the bounds that should be mapped to the delegate and the delegate
27 * view itself.
28 * <p>
29 * The ancestor should then forward all of its touch events received in its
30 * {@link android.view.View#onTouchEvent(MotionEvent)} to {@link #onTouchEvent(MotionEvent)}.
31 * </p>
32 */
33public class TouchDelegate {
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035 /**
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070036 * View that should receive forwarded touch events
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037 */
38 private View mDelegateView;
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 /**
41 * Bounds in local coordinates of the containing view that should be mapped to the delegate
42 * view. This rect is used for initial hit testing.
43 */
44 private Rect mBounds;
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 /**
47 * mBounds inflated to include some slop. This rect is to track whether the motion events
Siarhei Vishniakoud27a6312017-10-20 18:06:57 -070048 * should be considered to be within the delegate view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 */
50 private Rect mSlopBounds;
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 /**
53 * True if the delegate had been targeted on a down event (intersected mBounds).
54 */
Mathew Inwoode5ad5982018-08-17 15:07:52 +010055 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 private boolean mDelegateTargeted;
57
58 /**
59 * The touchable region of the View extends above its actual extent.
60 */
61 public static final int ABOVE = 1;
62
63 /**
64 * The touchable region of the View extends below its actual extent.
65 */
66 public static final int BELOW = 2;
67
68 /**
Siarhei Vishniakoud27a6312017-10-20 18:06:57 -070069 * The touchable region of the View extends to the left of its actual extent.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 */
71 public static final int TO_LEFT = 4;
72
73 /**
Siarhei Vishniakoud27a6312017-10-20 18:06:57 -070074 * The touchable region of the View extends to the right of its actual extent.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 */
76 public static final int TO_RIGHT = 8;
77
78 private int mSlop;
79
80 /**
81 * Constructor
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070082 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 * @param bounds Bounds in local coordinates of the containing view that should be mapped to
84 * the delegate view
85 * @param delegateView The view that should receive motion events
86 */
87 public TouchDelegate(Rect bounds, View delegateView) {
88 mBounds = bounds;
89
90 mSlop = ViewConfiguration.get(delegateView.getContext()).getScaledTouchSlop();
91 mSlopBounds = new Rect(bounds);
92 mSlopBounds.inset(-mSlop, -mSlop);
93 mDelegateView = delegateView;
94 }
95
96 /**
97 * Will forward touch events to the delegate view if the event is within the bounds
98 * specified in the constructor.
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070099 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 * @param event The touch event to forward
101 * @return True if the event was forwarded to the delegate, false otherwise.
102 */
103 public boolean onTouchEvent(MotionEvent event) {
104 int x = (int)event.getX();
105 int y = (int)event.getY();
106 boolean sendToDelegate = false;
107 boolean hit = true;
108 boolean handled = false;
109
Siarhei Vishniakou01b729f2018-02-16 17:27:22 -0600110 switch (event.getActionMasked()) {
Siarhei Vishniakoud27a6312017-10-20 18:06:57 -0700111 case MotionEvent.ACTION_DOWN:
112 mDelegateTargeted = mBounds.contains(x, y);
113 sendToDelegate = mDelegateTargeted;
114 break;
Siarhei Vishniakou01b729f2018-02-16 17:27:22 -0600115 case MotionEvent.ACTION_POINTER_DOWN:
116 case MotionEvent.ACTION_POINTER_UP:
Siarhei Vishniakoud27a6312017-10-20 18:06:57 -0700117 case MotionEvent.ACTION_UP:
118 case MotionEvent.ACTION_MOVE:
119 sendToDelegate = mDelegateTargeted;
120 if (sendToDelegate) {
121 Rect slopBounds = mSlopBounds;
122 if (!slopBounds.contains(x, y)) {
123 hit = false;
124 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 }
Siarhei Vishniakoud27a6312017-10-20 18:06:57 -0700126 break;
127 case MotionEvent.ACTION_CANCEL:
128 sendToDelegate = mDelegateTargeted;
129 mDelegateTargeted = false;
130 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 }
132 if (sendToDelegate) {
133 final View delegateView = mDelegateView;
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -0700134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 if (hit) {
136 // Offset event coordinates to be inside the target view
137 event.setLocation(delegateView.getWidth() / 2, delegateView.getHeight() / 2);
138 } else {
139 // Offset event coordinates to be outside the target view (in case it does
140 // something like tracking pressed state)
141 int slop = mSlop;
142 event.setLocation(-(slop * 2), -(slop * 2));
143 }
144 handled = delegateView.dispatchTouchEvent(event);
145 }
146 return handled;
147 }
148}