| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.compat.annotation.UnsupportedAppUsage; |
| 21 |
import android.graphics.Rect; |
| 22 |
import android.graphics.Region; |
| 23 |
import android.util.ArrayMap; |
| 24 |
import android.view.accessibility.AccessibilityNodeInfo.TouchDelegateInfo; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class TouchDelegate { |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
private View mDelegateView; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
private Rect mBounds; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
private Rect mSlopBounds; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
@UnsupportedAppUsage |
| 60 |
private boolean mDelegateTargeted; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public static final int ABOVE = 1; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public static final int BELOW = 2; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public static final int TO_LEFT = 4; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public static final int TO_RIGHT = 8; |
| 81 |
|
| 82 |
private int mSlop; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
private TouchDelegateInfo mTouchDelegateInfo; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public TouchDelegate(Rect bounds, View delegateView) { |
| 97 |
mBounds = bounds; |
| 98 |
|
| 99 |
mSlop = ViewConfiguration.get(delegateView.getContext()).getScaledTouchSlop(); |
| 100 |
mSlopBounds = new Rect(bounds); |
| 101 |
mSlopBounds.inset(-mSlop, -mSlop); |
| 102 |
mDelegateView = delegateView; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public boolean onTouchEvent(@NonNull MotionEvent event) { |
| 113 |
int x = (int)event.getX(); |
| 114 |
int y = (int)event.getY(); |
| 115 |
boolean sendToDelegate = false; |
| 116 |
boolean hit = true; |
| 117 |
boolean handled = false; |
| 118 |
|
| 119 |
switch (event.getActionMasked()) { |
| 120 |
case MotionEvent.ACTION_DOWN: |
| 121 |
mDelegateTargeted = mBounds.contains(x, y); |
| 122 |
sendToDelegate = mDelegateTargeted; |
| 123 |
break; |
| 124 |
case MotionEvent.ACTION_POINTER_DOWN: |
| 125 |
case MotionEvent.ACTION_POINTER_UP: |
| 126 |
case MotionEvent.ACTION_UP: |
| 127 |
case MotionEvent.ACTION_MOVE: |
| 128 |
sendToDelegate = mDelegateTargeted; |
| 129 |
if (sendToDelegate) { |
| 130 |
Rect slopBounds = mSlopBounds; |
| 131 |
if (!slopBounds.contains(x, y)) { |
| 132 |
hit = false; |
| 133 |
} |
| 134 |
} |
| 135 |
break; |
| 136 |
case MotionEvent.ACTION_CANCEL: |
| 137 |
sendToDelegate = mDelegateTargeted; |
| 138 |
mDelegateTargeted = false; |
| 139 |
break; |
| 140 |
} |
| 141 |
if (sendToDelegate) { |
| 142 |
if (hit) { |
| 143 |
|
| 144 |
event.setLocation(mDelegateView.getWidth() / 2, mDelegateView.getHeight() / 2); |
| 145 |
} else { |
| 146 |
|
| 147 |
|
| 148 |
int slop = mSlop; |
| 149 |
event.setLocation(-(slop * 2), -(slop * 2)); |
| 150 |
} |
| 151 |
handled = mDelegateView.dispatchTouchEvent(event); |
| 152 |
} |
| 153 |
return handled; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public boolean onTouchExplorationHoverEvent(@NonNull MotionEvent event) { |
| 171 |
if (mBounds == null) { |
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
final int x = (int) event.getX(); |
| 176 |
final int y = (int) event.getY(); |
| 177 |
boolean hit = true; |
| 178 |
boolean handled = false; |
| 179 |
|
| 180 |
final boolean isInbound = mBounds.contains(x, y); |
| 181 |
switch (event.getActionMasked()) { |
| 182 |
case MotionEvent.ACTION_HOVER_ENTER: |
| 183 |
mDelegateTargeted = isInbound; |
| 184 |
break; |
| 185 |
case MotionEvent.ACTION_HOVER_MOVE: |
| 186 |
if (isInbound) { |
| 187 |
mDelegateTargeted = true; |
| 188 |
} else { |
| 189 |
|
| 190 |
if (mDelegateTargeted && !mSlopBounds.contains(x, y)) { |
| 191 |
hit = false; |
| 192 |
} |
| 193 |
} |
| 194 |
break; |
| 195 |
case MotionEvent.ACTION_HOVER_EXIT: |
| 196 |
mDelegateTargeted = true; |
| 197 |
break; |
| 198 |
} |
| 199 |
if (mDelegateTargeted) { |
| 200 |
if (hit) { |
| 201 |
event.setLocation(mDelegateView.getWidth() / 2, mDelegateView.getHeight() / 2); |
| 202 |
} else { |
| 203 |
mDelegateTargeted = false; |
| 204 |
} |
| 205 |
handled = mDelegateView.dispatchHoverEvent(event); |
| 206 |
} |
| 207 |
return handled; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
@NonNull |
| 217 |
public TouchDelegateInfo getTouchDelegateInfo() { |
| 218 |
if (mTouchDelegateInfo == null) { |
| 219 |
final ArrayMap<Region, View> targetMap = new ArrayMap<>(1); |
| 220 |
Rect bounds = mBounds; |
| 221 |
if (bounds == null) { |
| 222 |
bounds = new Rect(); |
| 223 |
} |
| 224 |
targetMap.put(new Region(bounds), mDelegateView); |
| 225 |
mTouchDelegateInfo = new TouchDelegateInfo(targetMap); |
| 226 |
} |
| 227 |
return mTouchDelegateInfo; |
| 228 |
} |
| 229 |
} |