Integration test for embeded Android Views touch support.
The test places an embedded Android view at the top left, and verifies that motion events that get to FlutterView are equivalent to the synthesized motion events that gets to the embedded view. See the README.md for more high level details.
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="io.flutter.integration.androidviews">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||
|
||||
<application
|
||||
android:name="io.flutter.app.FlutterApplication"
|
||||
android:label="android_views">
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:launchMode="singleTop"
|
||||
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
|
||||
android:hardwareAccelerated="true"
|
||||
android:windowSoftInputMode="adjustResize">
|
||||
<meta-data
|
||||
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
|
||||
android:value="true" />
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN"/>
|
||||
<category android:name="android.intent.category.LAUNCHER"/>
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
</manifest>
|
||||
@@ -0,0 +1,104 @@
|
||||
// Copyright 2018 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
package io.flutter.integration.androidviews;
|
||||
|
||||
import android.Manifest;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.view.MotionEvent;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import io.flutter.app.FlutterActivity;
|
||||
import io.flutter.plugin.common.MethodCall;
|
||||
import io.flutter.plugin.common.MethodChannel;
|
||||
import io.flutter.plugins.GeneratedPluginRegistrant;
|
||||
|
||||
public class MainActivity extends FlutterActivity implements MethodChannel.MethodCallHandler {
|
||||
final static int STORAGE_PERMISSION_CODE = 1;
|
||||
|
||||
MethodChannel mMethodChannel;
|
||||
TouchPipe mFlutterViewTouchPipe;
|
||||
|
||||
// The method result to complete with the Android permission request result.
|
||||
// This is null when not waiting for the Android permission request;
|
||||
private MethodChannel.Result permissionResult;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
GeneratedPluginRegistrant.registerWith(this);
|
||||
getFlutterView().getPluginRegistry()
|
||||
.registrarFor("io.flutter.integration.android_views").platformViewRegistry()
|
||||
.registerViewFactory("simple_view", new SimpleViewFactory(getFlutterView()));
|
||||
mMethodChannel = new MethodChannel(this.getFlutterView(), "android_views_integration");
|
||||
mMethodChannel.setMethodCallHandler(this);
|
||||
mFlutterViewTouchPipe = new TouchPipe(mMethodChannel, getFlutterView());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
|
||||
switch(methodCall.method) {
|
||||
case "pipeFlutterViewEvents":
|
||||
mFlutterViewTouchPipe.enable();
|
||||
result.success(null);
|
||||
return;
|
||||
case "stopFlutterViewEvents":
|
||||
mFlutterViewTouchPipe.disable();
|
||||
result.success(null);
|
||||
return;
|
||||
case "getStoragePermission":
|
||||
if (permissionResult != null) {
|
||||
result.error("error", "already waiting for permissions", null);
|
||||
return;
|
||||
}
|
||||
permissionResult = result;
|
||||
getExternalStoragePermissions();
|
||||
return;
|
||||
case "synthesizeEvent":
|
||||
synthesizeEvent(methodCall, result);
|
||||
return;
|
||||
}
|
||||
result.notImplemented();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void synthesizeEvent(MethodCall methodCall, MethodChannel.Result result) {
|
||||
MotionEvent event = MotionEventCodec.decode((HashMap<String, Object>) methodCall.arguments());
|
||||
getFlutterView().dispatchTouchEvent(event);
|
||||
result.success(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
||||
if (requestCode != STORAGE_PERMISSION_CODE || permissionResult == null)
|
||||
return;
|
||||
boolean permisisonGranted = grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED;
|
||||
sendPermissionResult(permisisonGranted);
|
||||
}
|
||||
|
||||
|
||||
private void getExternalStoragePermissions() {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
|
||||
return;
|
||||
|
||||
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||
== PackageManager.PERMISSION_GRANTED) {
|
||||
sendPermissionResult(true);
|
||||
return;
|
||||
}
|
||||
|
||||
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
|
||||
}
|
||||
|
||||
private void sendPermissionResult(boolean result) {
|
||||
if (permissionResult == null)
|
||||
return;
|
||||
permissionResult.success(result);
|
||||
permissionResult = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
// Copyright 2018 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
package io.flutter.integration.androidviews;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.os.Build;
|
||||
import android.view.MotionEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import static android.view.MotionEvent.PointerCoords;
|
||||
import static android.view.MotionEvent.PointerProperties;
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
|
||||
public class MotionEventCodec {
|
||||
public static HashMap<String, Object> encode(MotionEvent event) {
|
||||
ArrayList<HashMap<String,Object>> pointerProperties = new ArrayList<>();
|
||||
ArrayList<HashMap<String,Object>> pointerCoords = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < event.getPointerCount(); i++) {
|
||||
MotionEvent.PointerProperties properties = new MotionEvent.PointerProperties();
|
||||
event.getPointerProperties(i, properties);
|
||||
pointerProperties.add(encodePointerProperties(properties));
|
||||
|
||||
MotionEvent.PointerCoords coords = new MotionEvent.PointerCoords();
|
||||
event.getPointerCoords(i, coords);
|
||||
pointerCoords.add(encodePointerCoords(coords));
|
||||
}
|
||||
|
||||
HashMap<String, Object> eventMap = new HashMap<>();
|
||||
eventMap.put("downTime", event.getDownTime());
|
||||
eventMap.put("eventTime", event.getEventTime());
|
||||
eventMap.put("action", event.getAction());
|
||||
eventMap.put("pointerCount", event.getPointerCount());
|
||||
eventMap.put("pointerProperties", pointerProperties);
|
||||
eventMap.put("pointerCoords", pointerCoords);
|
||||
eventMap.put("metaState", event.getMetaState());
|
||||
eventMap.put("buttonState", event.getButtonState());
|
||||
eventMap.put("xPrecision", event.getXPrecision());
|
||||
eventMap.put("yPrecision", event.getYPrecision());
|
||||
eventMap.put("deviceId", event.getDeviceId());
|
||||
eventMap.put("edgeFlags", event.getEdgeFlags());
|
||||
eventMap.put("source", event.getSource());
|
||||
eventMap.put("flags", event.getFlags());
|
||||
|
||||
return eventMap;
|
||||
}
|
||||
|
||||
private static HashMap<String, Object> encodePointerProperties(PointerProperties properties) {
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
map.put("id", properties.id);
|
||||
map.put("toolType", properties.toolType);
|
||||
return map;
|
||||
}
|
||||
|
||||
private static HashMap<String, Object> encodePointerCoords(PointerCoords coords) {
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
map.put("orientation", coords.orientation);
|
||||
map.put("pressure", coords.pressure);
|
||||
map.put("size", coords.size);
|
||||
map.put("toolMajor", coords.toolMajor);
|
||||
map.put("toolMinor", coords.toolMinor);
|
||||
map.put("touchMajor", coords.touchMajor);
|
||||
map.put("touchMinor", coords.touchMinor);
|
||||
map.put("x", coords.x);
|
||||
map.put("y", coords.y);
|
||||
return map;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static MotionEvent decode(HashMap<String, Object> data) {
|
||||
List<PointerProperties> pointerProperties = new ArrayList<>();
|
||||
List<PointerCoords> pointerCoords = new ArrayList<>();
|
||||
|
||||
for (HashMap<String, Object> property : (List<HashMap<String, Object>>) data.get("pointerProperties")) {
|
||||
pointerProperties.add(decodePointerProperties(property)) ;
|
||||
}
|
||||
|
||||
for (HashMap<String, Object> coord : (List<HashMap<String, Object>>) data.get("pointerCoords")) {
|
||||
pointerCoords.add(decodePointerCoords(coord)) ;
|
||||
}
|
||||
|
||||
return MotionEvent.obtain(
|
||||
(int) data.get("downTime"),
|
||||
(int) data.get("eventTime"),
|
||||
(int) data.get("action"),
|
||||
(int) data.get("pointerCount"),
|
||||
pointerProperties.toArray(new PointerProperties[pointerProperties.size()]),
|
||||
pointerCoords.toArray(new PointerCoords[pointerCoords.size()]),
|
||||
(int) data.get("metaState"),
|
||||
(int) data.get("buttonState"),
|
||||
(float) (double) data.get("xPrecision"),
|
||||
(float) (double) data.get("yPrecision"),
|
||||
(int) data.get("deviceId"),
|
||||
(int) data.get("edgeFlags"),
|
||||
(int) data.get("source"),
|
||||
(int) data.get("flags")
|
||||
);
|
||||
}
|
||||
|
||||
private static PointerProperties decodePointerProperties(HashMap<String, Object> data) {
|
||||
PointerProperties properties = new PointerProperties();
|
||||
properties.id = (int) data.get("id");
|
||||
properties.toolType = (int) data.get("toolType");
|
||||
return properties;
|
||||
}
|
||||
|
||||
private static PointerCoords decodePointerCoords(HashMap<String, Object> data) {
|
||||
PointerCoords coords = new PointerCoords();
|
||||
coords.orientation = (float) (double) data.get("orientation");
|
||||
coords.pressure = (float) (double) data.get("pressure");
|
||||
coords.size = (float) (double) data.get("size");
|
||||
coords.toolMajor = (float) (double) data.get("toolMajor");
|
||||
coords.toolMinor = (float) (double) data.get("toolMinor");
|
||||
coords.touchMajor = (float) (double) data.get("touchMajor");
|
||||
coords.touchMinor = (float) (double) data.get("touchMinor");
|
||||
coords.x = (float) (double) data.get("x");
|
||||
coords.y = (float) (double) data.get("y");
|
||||
return coords;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// Copyright 2018 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
package io.flutter.integration.androidviews;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
|
||||
import io.flutter.plugin.common.MethodCall;
|
||||
import io.flutter.plugin.common.MethodChannel;
|
||||
import io.flutter.plugin.platform.PlatformView;
|
||||
|
||||
public class SimplePlatformView implements PlatformView, MethodChannel.MethodCallHandler {
|
||||
private final View mView;
|
||||
private final MethodChannel mMethodChannel;
|
||||
private final TouchPipe mTouchPipe;
|
||||
|
||||
SimplePlatformView(Context context, MethodChannel methodChannel) {
|
||||
mMethodChannel = methodChannel;
|
||||
mView = new View(context) {
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent event) {
|
||||
return super.onTouchEvent(event);
|
||||
}
|
||||
};
|
||||
mView.setBackgroundColor(0xff0000ff);
|
||||
mMethodChannel.setMethodCallHandler(this);
|
||||
mTouchPipe = new TouchPipe(mMethodChannel, mView);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView() {
|
||||
return mView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
|
||||
switch(methodCall.method) {
|
||||
case "pipeTouchEvents":
|
||||
mTouchPipe.enable();
|
||||
result.success(null);
|
||||
return;
|
||||
case "stopTouchEvents":
|
||||
mTouchPipe.disable();
|
||||
result.success(null);
|
||||
return;
|
||||
}
|
||||
result.notImplemented();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright 2018 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
package io.flutter.integration.androidviews;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import io.flutter.plugin.common.BinaryMessenger;
|
||||
import io.flutter.plugin.common.MethodChannel;
|
||||
import io.flutter.plugin.platform.PlatformView;
|
||||
import io.flutter.plugin.platform.PlatformViewFactory;
|
||||
|
||||
public class SimpleViewFactory implements PlatformViewFactory {
|
||||
final BinaryMessenger messenger;
|
||||
|
||||
public SimpleViewFactory(BinaryMessenger messenger) {
|
||||
this.messenger = messenger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlatformView create(Context context, int id) {
|
||||
MethodChannel methodChannel = new MethodChannel(messenger, "simple_view/" + id);
|
||||
return new SimplePlatformView(context, methodChannel);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
// Copyright 2018 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
package io.flutter.integration.androidviews;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.os.Build;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
|
||||
import io.flutter.plugin.common.MethodChannel;
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
|
||||
class TouchPipe implements View.OnTouchListener {
|
||||
private final MethodChannel mMethodChannel;
|
||||
private final View mView;
|
||||
|
||||
private boolean mEnabled;
|
||||
|
||||
TouchPipe(MethodChannel methodChannel, View view) {
|
||||
mMethodChannel = methodChannel;
|
||||
mView = view;
|
||||
}
|
||||
|
||||
public void enable() {
|
||||
if (mEnabled)
|
||||
return;
|
||||
mEnabled = true;
|
||||
mView.setOnTouchListener(this);
|
||||
}
|
||||
|
||||
public void disable() {
|
||||
if(!mEnabled)
|
||||
return;
|
||||
mEnabled = false;
|
||||
mView.setOnTouchListener(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTouch(View v, MotionEvent event) {
|
||||
mMethodChannel.invokeMethod("onTouch", MotionEventCodec.encode(event));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user