Skip to content

Commit

Permalink
Add touch events to the platform views method channel API. (flutter#5796
Browse files Browse the repository at this point in the history
)
  • Loading branch information
amirh authored Jul 18, 2018
1 parent 91c16af commit 3054f31
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
package io.flutter.plugin.platform;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.StandardMethodCodec;
import io.flutter.view.FlutterView;
import io.flutter.view.TextureRegistry;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -86,6 +88,9 @@ public void onMethodCall(final MethodCall call, final MethodChannel.Result resul
case "resize":
resizePlatformView(call, result);
return;
case "touch":
onTouch(call, result);
return;
}
result.notImplemented();
}
Expand Down Expand Up @@ -183,6 +188,54 @@ private void resizePlatformView(MethodCall call, MethodChannel.Result result) {
result.success(null);
}

private void onTouch(MethodCall call, MethodChannel.Result result) {
List<Object> args = call.arguments();

int id = (int) args.get(0);
int downTime = (int) args.get(1);
int eventTime = (int) args.get(2);
int action = (int) args.get(3);
double x = (double) args.get(4);
double y = (double) args.get(5);
double pressure = (double) args.get(6);
double size = (double) args.get(7);
int metaState = (int) args.get(8);
double xPrecision = (double) args.get(9);
double yPrecision = (double) args.get(10);
int deviceId = (int) args.get(11);
int edgeFlags = (int) args.get(12);

View view = vdControllers.get(id).getView();
if (view == null) {
result.error(
"error",
"Sending touch to an unknown view with id: " + id,
null
);
return;
}

float density = mFlutterView.getContext().getResources().getDisplayMetrics().density;

MotionEvent event = MotionEvent.obtain(
downTime,
eventTime,
action,
(float) x * density,
(float) y * density,
(float) pressure,
(float) size,
metaState,
(float) xPrecision,
(float) yPrecision,
deviceId,
edgeFlags
);

view.onTouchEvent(event);
result.success(null);
}

private int toPhysicalPixels(double logicalPixels) {
float density = mFlutterView.getContext().getResources().getDisplayMetrics().density;
return (int) Math.round(logicalPixels * density);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.os.Build;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.FrameLayout;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Expand Down Expand Up @@ -61,4 +62,10 @@ public PlatformView detachView() {
mContainer.removeView(mView.getView());
return mView;
}

public View getView() {
if (mView == null)
return null;
return mView.getView();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,10 @@ public void dispose() {
mPresentation.detachView().dispose();
mVirtualDisplay.release();
}

public View getView() {
if (mPresentation == null)
return null;
return mPresentation.getView();
}
}

0 comments on commit 3054f31

Please sign in to comment.