|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +package io.flutter.integration.platformviews; |
| 6 | + |
| 7 | +import android.Manifest; |
| 8 | +import android.content.pm.PackageManager; |
| 9 | +import android.os.Build; |
| 10 | +import android.os.Bundle; |
| 11 | +import android.view.MotionEvent; |
| 12 | +import android.view.View; |
| 13 | +import android.view.ViewGroup; |
| 14 | + |
| 15 | +import java.lang.StringBuilder; |
| 16 | +import java.util.HashMap; |
| 17 | + |
| 18 | +import io.flutter.embedding.android.FlutterActivity; |
| 19 | +import io.flutter.embedding.android.FlutterImageView; |
| 20 | +import io.flutter.embedding.android.FlutterSurfaceView; |
| 21 | +import io.flutter.embedding.android.FlutterTextureView; |
| 22 | +import io.flutter.embedding.android.FlutterView; |
| 23 | +import io.flutter.embedding.engine.dart.DartExecutor; |
| 24 | +import io.flutter.embedding.engine.FlutterEngine; |
| 25 | +import io.flutter.plugin.common.MethodCall; |
| 26 | +import io.flutter.plugin.common.MethodChannel; |
| 27 | +import io.flutter.plugins.GeneratedPluginRegistrant; |
| 28 | + |
| 29 | +public class MainActivity extends FlutterActivity implements MethodChannel.MethodCallHandler { |
| 30 | + final static int STORAGE_PERMISSION_CODE = 1; |
| 31 | + |
| 32 | + MethodChannel mMethodChannel; |
| 33 | + |
| 34 | + // The method result to complete with the Android permission request result. |
| 35 | + // This is null when not waiting for the Android permission request; |
| 36 | + private MethodChannel.Result permissionResult; |
| 37 | + |
| 38 | + private View getFlutterView() { |
| 39 | + return findViewById(FLUTTER_VIEW_ID); |
| 40 | + } |
| 41 | + |
| 42 | + private String getViewName(View view) { |
| 43 | + if (view instanceof FlutterImageView) { |
| 44 | + return "FlutterImageView"; |
| 45 | + } |
| 46 | + if (view instanceof FlutterSurfaceView) { |
| 47 | + return "FlutterSurfaceView"; |
| 48 | + } |
| 49 | + if (view instanceof FlutterTextureView) { |
| 50 | + return "FlutterTextureView"; |
| 51 | + } |
| 52 | + if (view instanceof FlutterView) { |
| 53 | + return "FlutterView"; |
| 54 | + } |
| 55 | + if (view instanceof ViewGroup) { |
| 56 | + return "ViewGroup"; |
| 57 | + } |
| 58 | + return "View"; |
| 59 | + } |
| 60 | + |
| 61 | + private void recurseViewHierarchy(View current, String padding, StringBuilder builder) { |
| 62 | + if (current.getVisibility() != View.VISIBLE || current.getAlpha() == 0) { |
| 63 | + return; |
| 64 | + } |
| 65 | + String name = getViewName(current); |
| 66 | + builder.append(padding); |
| 67 | + builder.append("|-"); |
| 68 | + builder.append(name); |
| 69 | + builder.append("\n"); |
| 70 | + |
| 71 | + if (current instanceof ViewGroup) { |
| 72 | + ViewGroup viewGroup = (ViewGroup) current; |
| 73 | + for (int index = 0; index < viewGroup.getChildCount(); index++) { |
| 74 | + recurseViewHierarchy(viewGroup.getChildAt(index), padding + " ", builder); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Serializes the view hierarchy, so it can be sent to Dart over the method channel. |
| 81 | + * |
| 82 | + * Notation: |
| 83 | + * |- <view-name> |
| 84 | + * |- ... child view ordered by z order. |
| 85 | + * |
| 86 | + * Example output: |
| 87 | + * |- FlutterView |
| 88 | + * |- FlutterImageView |
| 89 | + * |- ViewGroup |
| 90 | + * |- View |
| 91 | + */ |
| 92 | + private String getSerializedViewHierarchy() { |
| 93 | + View root = getFlutterView(); |
| 94 | + StringBuilder builder = new StringBuilder(); |
| 95 | + recurseViewHierarchy(root, "", builder); |
| 96 | + return builder.toString(); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void configureFlutterEngine(FlutterEngine flutterEngine) { |
| 101 | + DartExecutor executor = flutterEngine.getDartExecutor(); |
| 102 | + flutterEngine |
| 103 | + .getPlatformViewsController() |
| 104 | + .getRegistry() |
| 105 | + .registerViewFactory("simple_view", new SimpleViewFactory(executor)); |
| 106 | + mMethodChannel = new MethodChannel(executor, "android_views_integration"); |
| 107 | + mMethodChannel.setMethodCallHandler(this); |
| 108 | + GeneratedPluginRegistrant.registerWith(flutterEngine); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) { |
| 113 | + switch (methodCall.method) { |
| 114 | + case "getStoragePermission": |
| 115 | + if (permissionResult != null) { |
| 116 | + result.error("error", "already waiting for permissions", null); |
| 117 | + return; |
| 118 | + } |
| 119 | + permissionResult = result; |
| 120 | + getExternalStoragePermissions(); |
| 121 | + return; |
| 122 | + case "synthesizeEvent": |
| 123 | + synthesizeEvent(methodCall); |
| 124 | + result.success(null); |
| 125 | + return; |
| 126 | + case "getViewHierarchy": |
| 127 | + String viewHierarchy = getSerializedViewHierarchy(); |
| 128 | + result.success(viewHierarchy); |
| 129 | + return; |
| 130 | + } |
| 131 | + result.notImplemented(); |
| 132 | + } |
| 133 | + |
| 134 | + @SuppressWarnings("unchecked") |
| 135 | + public void synthesizeEvent(MethodCall methodCall) { |
| 136 | + MotionEvent event = MotionEventCodec.decode((HashMap<String, Object>) methodCall.arguments()); |
| 137 | + getFlutterView().dispatchTouchEvent(event); |
| 138 | + // TODO(egarciad): Remove invokeMethod since it is not necessary. |
| 139 | + mMethodChannel.invokeMethod("onTouch", MotionEventCodec.encode(event)); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { |
| 144 | + if (requestCode != STORAGE_PERMISSION_CODE || permissionResult == null) |
| 145 | + return; |
| 146 | + boolean permissionGranted = grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED; |
| 147 | + sendPermissionResult(permissionGranted); |
| 148 | + } |
| 149 | + |
| 150 | + private void getExternalStoragePermissions() { |
| 151 | + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) |
| 152 | + return; |
| 153 | + |
| 154 | + if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) |
| 155 | + == PackageManager.PERMISSION_GRANTED) { |
| 156 | + sendPermissionResult(true); |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE); |
| 161 | + } |
| 162 | + |
| 163 | + private void sendPermissionResult(boolean result) { |
| 164 | + if (permissionResult == null) |
| 165 | + return; |
| 166 | + permissionResult.success(result); |
| 167 | + permissionResult = null; |
| 168 | + } |
| 169 | +} |
0 commit comments