Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: ui/android/java/src/org/chromium/ui/base/EventForwarder.java

Issue 2896993002: Route OnDragEvent through ViewAndroid tree (Closed)
Patch Set: rebase Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.ui.base; 5 package org.chromium.ui.base;
6 6
7 import android.annotation.TargetApi; 7 import android.annotation.TargetApi;
8 import android.content.ClipData;
9 import android.content.ClipDescription;
8 import android.os.Build; 10 import android.os.Build;
11 import android.view.DragEvent;
9 import android.view.MotionEvent; 12 import android.view.MotionEvent;
13 import android.view.View;
10 14
11 import org.chromium.base.TraceEvent; 15 import org.chromium.base.TraceEvent;
12 import org.chromium.base.annotations.CalledByNative; 16 import org.chromium.base.annotations.CalledByNative;
13 import org.chromium.base.annotations.JNINamespace; 17 import org.chromium.base.annotations.JNINamespace;
14 18
15 /** 19 /**
16 * Class used to forward view, input events down to native. 20 * Class used to forward view, input events down to native.
17 */ 21 */
18 @JNINamespace("ui") 22 @JNINamespace("ui")
19 public class EventForwarder { 23 public class EventForwarder {
20 private long mNativeEventForwarder; 24 private long mNativeEventForwarder;
21 25
26 private final boolean mIsDragDropEnabled;
boliu 2017/05/24 22:54:02 nit: final above non-final member variables
Jinsuk Kim 2017/05/25 03:18:18 Done.
27
22 // Offsets for the events that passes through. 28 // Offsets for the events that passes through.
23 private float mCurrentTouchOffsetX; 29 private float mCurrentTouchOffsetX;
24 private float mCurrentTouchOffsetY; 30 private float mCurrentTouchOffsetY;
25 31
26 private int mLastMouseButtonState; 32 private int mLastMouseButtonState;
27 33
28 @CalledByNative 34 @CalledByNative
29 private static EventForwarder create(long nativeEventForwarder) { 35 private static EventForwarder create(long nativeEventForwarder, boolean isDr agDropEnabled) {
30 return new EventForwarder(nativeEventForwarder); 36 return new EventForwarder(nativeEventForwarder, isDragDropEnabled);
31 } 37 }
32 38
33 private EventForwarder(long nativeEventForwarder) { 39 private EventForwarder(long nativeEventForwarder, boolean isDragDropEnabled) {
34 mNativeEventForwarder = nativeEventForwarder; 40 mNativeEventForwarder = nativeEventForwarder;
41 mIsDragDropEnabled = isDragDropEnabled;
35 } 42 }
36 43
37 @CalledByNative 44 @CalledByNative
38 private void destroy() { 45 private void destroy() {
39 mNativeEventForwarder = 0; 46 mNativeEventForwarder = 0;
40 } 47 }
41 48
42 /** 49 /**
43 * @see View#onTouchEvent(MotionEvent) 50 * @see View#onTouchEvent(MotionEvent)
44 */ 51 */
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 return 0; 223 return 0;
217 } 224 }
218 225
219 public boolean onMouseWheelEvent( 226 public boolean onMouseWheelEvent(
220 long timeMs, float x, float y, float ticksX, float ticksY, float pix elsPerTick) { 227 long timeMs, float x, float y, float ticksX, float ticksY, float pix elsPerTick) {
221 assert mNativeEventForwarder != 0; 228 assert mNativeEventForwarder != 0;
222 nativeOnMouseWheelEvent(mNativeEventForwarder, timeMs, x, y, ticksX, tic ksY, pixelsPerTick); 229 nativeOnMouseWheelEvent(mNativeEventForwarder, timeMs, x, y, ticksX, tic ksY, pixelsPerTick);
223 return true; 230 return true;
224 } 231 }
225 232
233 /**
234 * @see View#onDragEvent(DragEvent)
235 * @param event {@link DragEvent} instance.
236 * @param containerView A view on which the drag event is taking place.
237 */
238 @TargetApi(Build.VERSION_CODES.N)
239 public boolean onDragEvent(DragEvent event, View containerView) {
240 if (mNativeEventForwarder == 0 || Build.VERSION.SDK_INT <= Build.VERSION _CODES.M) {
241 return false;
242 }
243
244 ClipDescription clipDescription = event.getClipDescription();
245
246 // text/* will match text/uri-list, text/html, text/plain.
247 String[] mimeTypes =
248 clipDescription == null ? new String[0] : clipDescription.filter MimeTypes("text/*");
249
250 if (event.getAction() == DragEvent.ACTION_DRAG_STARTED) {
251 // TODO(hush): support dragging more than just text.
252 return mimeTypes != null && mimeTypes.length > 0 && mIsDragDropEnabl ed;
253 }
254
255 StringBuilder content = new StringBuilder("");
256 if (event.getAction() == DragEvent.ACTION_DROP) {
257 // TODO(hush): obtain dragdrop permissions, when dragging files into Chrome/WebView is
258 // supported. Not necessary to do so for now, because only text drag ging is supported.
259 ClipData clipData = event.getClipData();
260 final int itemCount = clipData.getItemCount();
261 for (int i = 0; i < itemCount; i++) {
262 ClipData.Item item = clipData.getItemAt(i);
263 content.append(item.coerceToStyledText(containerView.getContext( )));
264 }
265 }
266
267 int[] locationOnScreen = new int[2];
268 containerView.getLocationOnScreen(locationOnScreen);
269
270 // All coordinates are in device pixel. Conversion to DIP happens in the native.
271 int x = (int) (event.getX() + mCurrentTouchOffsetX);
272 int y = (int) (event.getY() + mCurrentTouchOffsetY);
273 int screenX = x + locationOnScreen[0];
274 int screenY = y + locationOnScreen[1];
275
276 nativeOnDragEvent(mNativeEventForwarder, event.getAction(), x, y, screen X, screenY,
277 mimeTypes, content.toString());
278 return true;
279 }
280
226 // All touch events (including flings, scrolls etc) accept coordinates in ph ysical pixels. 281 // All touch events (including flings, scrolls etc) accept coordinates in ph ysical pixels.
227 private native boolean nativeOnTouchEvent(long nativeEventForwarder, MotionE vent event, 282 private native boolean nativeOnTouchEvent(long nativeEventForwarder, MotionE vent event,
228 long timeMs, int action, int pointerCount, int historySize, int acti onIndex, float x0, 283 long timeMs, int action, int pointerCount, int historySize, int acti onIndex, float x0,
229 float y0, float x1, float y1, int pointerId0, int pointerId1, float touchMajor0, 284 float y0, float x1, float y1, int pointerId0, int pointerId1, float touchMajor0,
230 float touchMajor1, float touchMinor0, float touchMinor1, float orien tation0, 285 float touchMajor1, float touchMinor0, float touchMinor1, float orien tation0,
231 float orientation1, float tilt0, float tilt1, float rawX, float rawY , 286 float orientation1, float tilt0, float tilt1, float rawX, float rawY ,
232 int androidToolType0, int androidToolType1, int androidButtonState, 287 int androidToolType0, int androidToolType1, int androidButtonState,
233 int androidMetaState, boolean isTouchHandleEvent); 288 int androidMetaState, boolean isTouchHandleEvent);
234 private native void nativeOnMouseEvent(long nativeEventForwarder, long timeM s, int action, 289 private native void nativeOnMouseEvent(long nativeEventForwarder, long timeM s, int action,
235 float x, float y, int pointerId, float pressure, float orientation, float tilt, 290 float x, float y, int pointerId, float pressure, float orientation, float tilt,
236 int changedButton, int buttonState, int metaState, int toolType); 291 int changedButton, int buttonState, int metaState, int toolType);
237 private native void nativeOnMouseWheelEvent(long nativeEventForwarder, long timeMs, float x, 292 private native void nativeOnMouseWheelEvent(long nativeEventForwarder, long timeMs, float x,
238 float y, float ticksX, float ticksY, float pixelsPerTick); 293 float y, float ticksX, float ticksY, float pixelsPerTick);
294 private native void nativeOnDragEvent(long nativeEventForwarder, int action, int x, int y,
295 int screenX, int screenY, String[] mimeTypes, String content);
239 } 296 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698