Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit ed939eb

Browse files
author
Kaushik Iska
committed
[android] Childview will process its motion events
Prior to this change the parent view, i.e, FlutterView intercepts all the MotionEvents and sent them to the framework for processing, after this it makes it so that the ChildView processes the event.
1 parent 7e101f1 commit ed939eb

File tree

5 files changed

+53
-17
lines changed

5 files changed

+53
-17
lines changed

shell/platform/android/io/flutter/embedding/android/AndroidTouchProcessor.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,18 @@ public class AndroidTouchProcessor {
6969

7070
private static final int _POINTER_BUTTON_PRIMARY = 1;
7171

72+
private final boolean trackMotionEvents;
73+
7274
/**
7375
* Constructs an {@code AndroidTouchProcessor} that will send touch event data to the Flutter
7476
* execution context represented by the given {@link FlutterRenderer}.
7577
*/
7678
// TODO(mattcarroll): consider moving packet behavior to a FlutterInteractionSurface instead of
7779
// FlutterRenderer
78-
public AndroidTouchProcessor(@NonNull FlutterRenderer renderer) {
80+
public AndroidTouchProcessor(@NonNull FlutterRenderer renderer, boolean trackMotionEvents) {
7981
this.renderer = renderer;
8082
this.motionEventTracker = MotionEventTracker.getInstance();
83+
this.trackMotionEvents = trackMotionEvents;
8184
}
8285

8386
/** Sends the given {@link MotionEvent} data to Flutter in a format that Flutter understands. */
@@ -176,8 +179,11 @@ private void addPointerForIndex(
176179
return;
177180
}
178181

179-
// TODO (kaushikiska) : pass this in when we have a way to evict framework only events.
180-
// MotionEventTracker.MotionEventId motionEventId = motionEventTracker.track(event);
182+
long motionEventId = 0;
183+
if (trackMotionEvents) {
184+
MotionEventTracker.MotionEventId trackedEvent = motionEventTracker.track(event);
185+
motionEventId = trackedEvent.getId();
186+
}
181187

182188
int pointerKind = getPointerDeviceTypeForToolType(event.getToolType(pointerIndex));
183189

@@ -188,15 +194,15 @@ private void addPointerForIndex(
188194

189195
long timeStamp = event.getEventTime() * 1000; // Convert from milliseconds to microseconds.
190196

191-
packet.putLong(0); // motionEventId
197+
packet.putLong(motionEventId); // motionEventId
192198
packet.putLong(timeStamp); // time_stamp
193199
packet.putLong(pointerChange); // change
194200
packet.putLong(pointerKind); // kind
195201
packet.putLong(signalKind); // signal_kind
196202
packet.putLong(event.getPointerId(pointerIndex)); // device
197203
packet.putLong(0); // pointer_identifier, will be generated in pointer_data_packet_converter.cc.
198-
packet.putDouble(event.getX(pointerIndex)); // physical_x
199-
packet.putDouble(event.getY(pointerIndex)); // physical_y
204+
packet.putDouble(event.getRawX(pointerIndex)); // physical_x
205+
packet.putDouble(event.getRawY(pointerIndex)); // physical_y
200206
packet.putDouble(
201207
0.0); // physical_delta_x, will be generated in pointer_data_packet_converter.cc.
202208
packet.putDouble(

shell/platform/android/io/flutter/embedding/android/FlutterView.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -782,11 +782,6 @@ public AccessibilityNodeProvider getAccessibilityNodeProvider() {
782782
}
783783
}
784784

785-
@Override
786-
public boolean onInterceptTouchEvent(MotionEvent ev) {
787-
return true;
788-
}
789-
790785
// TODO(mattcarroll): Confer with Ian as to why we need this method. Delete if possible, otherwise
791786
// add comments.
792787
private void resetWillNotDraw(boolean isAccessibilityEnabled, boolean isTouchExplorationEnabled) {
@@ -857,7 +852,8 @@ public void attachToFlutterEngine(@NonNull FlutterEngine flutterEngine) {
857852
localizationPlugin = this.flutterEngine.getLocalizationPlugin();
858853
androidKeyProcessor =
859854
new AndroidKeyProcessor(this.flutterEngine.getKeyEventChannel(), textInputPlugin);
860-
androidTouchProcessor = new AndroidTouchProcessor(this.flutterEngine.getRenderer());
855+
androidTouchProcessor =
856+
new AndroidTouchProcessor(this.flutterEngine.getRenderer(), /*trackMotionEvents=*/ false);
861857
accessibilityBridge =
862858
new AccessibilityBridge(
863859
this,
@@ -873,6 +869,9 @@ public void attachToFlutterEngine(@NonNull FlutterEngine flutterEngine) {
873869
// Connect AccessibilityBridge to the PlatformViewsController within the FlutterEngine.
874870
// This allows platform Views to hook into Flutter's overall accessibility system.
875871
this.flutterEngine.getPlatformViewsController().attachAccessibilityBridge(accessibilityBridge);
872+
this.flutterEngine
873+
.getPlatformViewsController()
874+
.attachToFlutterRenderer(this.flutterEngine.getRenderer());
876875

877876
// Inform the Android framework that it should retrieve a new InputConnection
878877
// now that an engine is attached.

shell/platform/android/io/flutter/embedding/engine/mutatorsstack/FlutterMutatorView.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import android.graphics.Canvas;
55
import android.graphics.Matrix;
66
import android.graphics.Path;
7+
import android.view.MotionEvent;
78
import android.widget.FrameLayout;
89
import androidx.annotation.NonNull;
10+
import io.flutter.embedding.android.AndroidTouchProcessor;
911

1012
/**
1113
* A view that applies the {@link io.flutter.embedding.engine.mutatorsstack.MutatorsStack} to its
@@ -17,19 +19,24 @@ public class FlutterMutatorView extends FrameLayout {
1719
private int left;
1820
private int top;
1921

22+
private final AndroidTouchProcessor androidTouchProcessor;
23+
2024
/**
2125
* Initialize the FlutterMutatorView. Use this to set the screenDensity, which will be used to
2226
* correct the final transform matrix.
2327
*/
24-
public FlutterMutatorView(@NonNull Context context, float screenDensity) {
28+
public FlutterMutatorView(
29+
@NonNull Context context, float screenDensity, AndroidTouchProcessor androidTouchProcessor) {
2530
super(context, null);
2631
this.screenDensity = screenDensity;
32+
this.androidTouchProcessor = androidTouchProcessor;
2733
}
2834

2935
/** Initialize the FlutterMutatorView. */
30-
public FlutterMutatorView(@NonNull Context context) {
36+
public FlutterMutatorView(@NonNull Context context, AndroidTouchProcessor androidTouchProcessor) {
3137
super(context, null);
3238
this.screenDensity = 1;
39+
this.androidTouchProcessor = androidTouchProcessor;
3340
}
3441

3542
/**
@@ -92,4 +99,15 @@ public void dispatchDraw(Canvas canvas) {
9299
super.dispatchDraw(canvas);
93100
canvas.restore();
94101
}
102+
103+
@Override
104+
// Intercept the events here and do not propagate them to the child platform views.
105+
public boolean onInterceptTouchEvent(MotionEvent event) {
106+
return true;
107+
}
108+
109+
@Override
110+
public boolean onTouchEvent(MotionEvent event) {
111+
return androidTouchProcessor.onTouchEvent(event);
112+
}
95113
}

shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
import androidx.annotation.NonNull;
2020
import androidx.annotation.UiThread;
2121
import androidx.annotation.VisibleForTesting;
22+
import io.flutter.embedding.android.AndroidTouchProcessor;
2223
import io.flutter.embedding.android.FlutterImageView;
2324
import io.flutter.embedding.android.FlutterView;
2425
import io.flutter.embedding.android.MotionEventTracker;
2526
import io.flutter.embedding.engine.FlutterOverlaySurface;
2627
import io.flutter.embedding.engine.dart.DartExecutor;
2728
import io.flutter.embedding.engine.mutatorsstack.*;
29+
import io.flutter.embedding.engine.renderer.FlutterRenderer;
2830
import io.flutter.embedding.engine.systemchannels.PlatformViewsChannel;
2931
import io.flutter.plugin.editing.TextInputPlugin;
3032
import io.flutter.view.AccessibilityBridge;
@@ -45,6 +47,8 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
4547

4648
private final PlatformViewRegistryImpl registry;
4749

50+
private AndroidTouchProcessor androidTouchProcessor;
51+
4852
// The context of the Activity or Fragment hosting the render target for the Flutter engine.
4953
private Context context;
5054

@@ -325,10 +329,12 @@ public MotionEvent toMotionEvent(
325329
.toArray(new PointerCoords[touch.pointerCount]);
326330

327331
if (!usingVirtualDiplays && trackedEvent != null) {
332+
// TODO (kaushikiska): investigate why we can not use the tracked
333+
// event directly.
328334
return MotionEvent.obtain(
329335
trackedEvent.getDownTime(),
330336
trackedEvent.getEventTime(),
331-
trackedEvent.getAction(),
337+
touch.action,
332338
touch.pointerCount,
333339
pointerProperties,
334340
pointerCoords,
@@ -673,12 +679,17 @@ private void initializePlatformViewIfNeeded(int viewId) {
673679
platformViews.put(viewId, view);
674680

675681
FlutterMutatorView mutatorView =
676-
new FlutterMutatorView(context, context.getResources().getDisplayMetrics().density);
682+
new FlutterMutatorView(
683+
context, context.getResources().getDisplayMetrics().density, androidTouchProcessor);
677684
mutatorViews.put(viewId, mutatorView);
678685
mutatorView.addView(platformView.getView());
679686
((FlutterView) flutterView).addView(mutatorView);
680687
}
681688

689+
public void attachToFlutterRenderer(FlutterRenderer flutterRenderer) {
690+
androidTouchProcessor = new AndroidTouchProcessor(flutterRenderer, /*trackMotionEvents=*/ true);
691+
}
692+
682693
public void onDisplayPlatformView(
683694
int viewId,
684695
int x,

shell/platform/android/io/flutter/view/FlutterView.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,9 @@ public void onPostResume() {
232232
}
233233
mLocalizationPlugin = new LocalizationPlugin(context, localizationChannel);
234234
androidKeyProcessor = new AndroidKeyProcessor(keyEventChannel, mTextInputPlugin);
235-
androidTouchProcessor = new AndroidTouchProcessor(flutterRenderer);
235+
androidTouchProcessor =
236+
new AndroidTouchProcessor(flutterRenderer, /*trackMotionEvents=*/ false);
237+
platformViewsController.attachToFlutterRenderer(flutterRenderer);
236238
mNativeView
237239
.getPluginRegistry()
238240
.getPlatformViewsController()

0 commit comments

Comments
 (0)