FlutterView.java 34.1 KB
Newer Older
1 2 3 4
// Copyright 2013 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.

5
package io.flutter.view;
6

7
import android.app.Activity;
8
import android.content.BroadcastReceiver;
9
import android.content.Context;
10 11 12
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
13
import android.content.res.Configuration;
H
Hixie 已提交
14
import android.opengl.Matrix;
15
import android.graphics.Bitmap;
H
Hixie 已提交
16
import android.graphics.Rect;
A
Adam Barth 已提交
17
import android.os.Build;
18
import android.util.AttributeSet;
19
import android.util.Log;
20
import android.util.TypedValue;
21
import android.view.KeyEvent;
22
import android.view.MotionEvent;
23 24 25 26
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
H
Hixie 已提交
27 28 29 30
import android.view.WindowInsets;
import android.view.accessibility.AccessibilityManager;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.accessibility.AccessibilityNodeProvider;
31 32
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
33 34
import org.json.JSONException;
import org.json.JSONObject;
35

36
import org.chromium.base.CalledByNative;
37
import org.chromium.base.JNINamespace;
38
import org.chromium.mojo.bindings.Interface.Binding;
39 40
import org.chromium.mojo.bindings.InterfaceRequest;
import org.chromium.mojo.system.Core;
41
import org.chromium.mojo.system.MessagePipeHandle;
H
Hixie 已提交
42
import org.chromium.mojo.system.MojoException;
43
import org.chromium.mojo.system.Pair;
H
Hixie 已提交
44
import org.chromium.mojo.system.impl.CoreImpl;
A
Adam Barth 已提交
45
import org.chromium.mojom.editing.Keyboard;
46
import org.chromium.mojom.flutter.platform.ApplicationMessages;
A
Adam Barth 已提交
47
import org.chromium.mojom.mojo.ServiceProvider;
48
import org.chromium.mojom.sky.AppLifecycleState;
A
Adam Barth 已提交
49
import org.chromium.mojom.sky.ServicesData;
50
import org.chromium.mojom.sky.SkyEngine;
A
Adam Barth 已提交
51
import org.chromium.mojom.sky.ViewportMetrics;
52

53 54
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
55
import java.nio.charset.StandardCharsets;
56
import java.util.ArrayList;
H
Hixie 已提交
57
import java.util.HashMap;
58
import java.util.List;
59
import java.util.Locale;
H
Hixie 已提交
60
import java.util.Map;
61

A
Adam Barth 已提交
62
import io.flutter.plugin.common.ActivityLifecycleListener;
63
import io.flutter.plugin.editing.TextInputPlugin;
A
Adam Barth 已提交
64 65
import io.flutter.plugin.platform.PlatformPlugin;

A
Adam Barth 已提交
66 67
import org.domokit.editing.KeyboardImpl;
import org.domokit.editing.KeyboardViewState;
68

69
/**
70
 * An Android view containing a Flutter app.
71
 */
72
@JNINamespace("shell")
73
public class FlutterView extends SurfaceView
74
  implements AccessibilityManager.AccessibilityStateChangeListener {
75
    private static final String TAG = "FlutterView";
76

77 78
    private static final String ACTION_DISCOVER = "io.flutter.view.DISCOVER";

A
Adam Barth 已提交
79
    private long mNativePlatformView;
80 81
    private TextInputPlugin mTextInputPlugin;

82
    private SkyEngine.Proxy mSkyEngine;
83
    private ServiceProviderImpl mPlatformServiceProvider;
84
    private Binding mPlatformServiceProviderBinding;
85
    private ServiceProviderImpl mViewServiceProvider;
86
    private Binding mViewServiceProviderBinding;
H
Hixie 已提交
87
    private ServiceProvider.Proxy mDartServiceProvider;
88 89 90
    private ApplicationMessages.Proxy mFlutterAppMessages;
    private HashMap<String, OnMessageListener> mOnMessageListeners;
    private HashMap<String, OnMessageListenerAsync> mAsyncOnMessageListeners;
91
    private final SurfaceHolder.Callback mSurfaceCallback;
92
    private final ViewportMetrics mMetrics;
A
Adam Barth 已提交
93
    private final KeyboardViewState mKeyboardState;
H
Hixie 已提交
94
    private final AccessibilityManager mAccessibilityManager;
95
    private BroadcastReceiver discoveryReceiver;
96
    private List<ActivityLifecycleListener> mActivityLifecycleListeners;
97

98
    public FlutterView(Context context) {
99 100 101
        this(context, null);
    }

102
    public FlutterView(Context context, AttributeSet attrs) {
103
        super(context, attrs);
104

105 106
        mMetrics = new ViewportMetrics();
        mMetrics.devicePixelRatio = context.getResources().getDisplayMetrics().density;
107 108 109
        setFocusable(true);
        setFocusableInTouchMode(true);

110
        attach();
A
Adam Barth 已提交
111
        assert mNativePlatformView != 0;
112

113 114 115 116 117 118 119 120
        int color = 0xFF000000;
        TypedValue typedValue = new TypedValue();
        context.getTheme().resolveAttribute(android.R.attr.colorBackground, typedValue, true);
        if (typedValue.type >= TypedValue.TYPE_FIRST_COLOR_INT && typedValue.type <= TypedValue.TYPE_LAST_COLOR_INT)
          color = typedValue.data;
        // TODO(abarth): Consider letting the developer override this color.
        final int backgroundColor = color;

121 122
        mSurfaceCallback = new SurfaceHolder.Callback() {
            @Override
123 124
            public void surfaceCreated(SurfaceHolder holder) {
                assert mNativePlatformView != 0;
125
                nativeSurfaceCreated(mNativePlatformView, holder.getSurface(), backgroundColor);
126 127 128
            }

            @Override
129
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
A
Adam Barth 已提交
130
                assert mNativePlatformView != 0;
131
                nativeSurfaceChanged(mNativePlatformView, width, height);
132 133 134 135
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
A
Adam Barth 已提交
136 137
                assert mNativePlatformView != 0;
                nativeSurfaceDestroyed(mNativePlatformView);
138 139 140
            }
        };
        getHolder().addCallback(mSurfaceCallback);
141

A
Adam Barth 已提交
142
        mKeyboardState = new KeyboardViewState(this);
H
Hixie 已提交
143

144 145
        Core core = CoreImpl.getInstance();

146
        mPlatformServiceProvider = new ServiceProviderImpl(core, this, ServiceRegistry.SHARED);
147 148 149

        ServiceRegistry localRegistry = new ServiceRegistry();
        configureLocalServices(localRegistry);
150
        mViewServiceProvider = new ServiceProviderImpl(core, this, localRegistry);
151

H
Hixie 已提交
152
        mAccessibilityManager = (AccessibilityManager)getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
153

154 155
        mOnMessageListeners = new HashMap<String, OnMessageListener>();
        mAsyncOnMessageListeners = new HashMap<String, OnMessageListenerAsync>();
156
        mActivityLifecycleListeners = new ArrayList<ActivityLifecycleListener>();
157

158
        setLocale(getResources().getConfiguration().locale);
159

160 161 162 163
        // Configure the platform plugin.
        PlatformPlugin platformPlugin = new PlatformPlugin((Activity)getContext());
        addOnMessageListener("flutter/platform", platformPlugin);
        addActivityLifecycleListener(platformPlugin);
164 165
        mTextInputPlugin = new TextInputPlugin((Activity)getContext());
        addOnMessageListener("flutter/textinput", mTextInputPlugin);
A
Adam Barth 已提交
166

167 168 169 170
        if ((context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
            discoveryReceiver = new DiscoveryReceiver();
            context.registerReceiver(discoveryReceiver, new IntentFilter(ACTION_DISCOVER));
        }
171 172
    }

173 174
    private void encodeKeyEvent(KeyEvent event, JSONObject message) throws JSONException {
        message.put("flags", event.getFlags());
175 176
        message.put("codePoint", event.getUnicodeChar());
        message.put("keyCode", event.getKeyCode());
177 178 179 180
        message.put("scanCode", event.getScanCode());
        message.put("metaState", event.getMetaState());
    }

181 182
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
183 184 185 186 187
        try {
            JSONObject message = new JSONObject();
            message.put("type", "keyup");
            message.put("keymap", "android");
            encodeKeyEvent(event, message);
188
            sendPlatformMessage("flutter/keyevent", message.toString(), null);
189 190 191
        } catch (JSONException e) {
            Log.e(TAG, "Failed to serialize key event", e);
        }
192
        return super.onKeyUp(keyCode, event);
193 194 195 196
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
197 198 199 200 201
        try {
            JSONObject message = new JSONObject();
            message.put("type", "keydown");
            message.put("keymap", "android");
            encodeKeyEvent(event, message);
202
            sendPlatformMessage("flutter/keyevent", message.toString(), null);
203 204 205
        } catch (JSONException e) {
            Log.e(TAG, "Failed to serialize key event", e);
        }
206
        return super.onKeyDown(keyCode, event);
207 208
    }

209
    SkyEngine getEngine() {
210 211 212
        return mSkyEngine;
    }

213 214 215 216
    public void addActivityLifecycleListener(ActivityLifecycleListener listener) {
        mActivityLifecycleListeners.add(listener);
    }

217 218 219 220
    public void onPause() {
        mSkyEngine.onAppLifecycleStateChanged(AppLifecycleState.PAUSED);
    }

221
    public void onPostResume() {
222 223 224
        for (ActivityLifecycleListener listener : mActivityLifecycleListeners)
            listener.onPostResume();

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
        mSkyEngine.onAppLifecycleStateChanged(AppLifecycleState.RESUMED);
    }

    public void pushRoute(String route) {
        mSkyEngine.pushRoute(route);
    }

    public void popRoute() {
        mSkyEngine.popRoute();
    }

    private void setLocale(Locale locale) {
        mSkyEngine.onLocaleChanged(locale.getLanguage(), locale.getCountry());
    }

    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        setLocale(newConfig.locale);
    }

H
Hixie 已提交
246 247 248 249
    float getDevicePixelRatio() {
        return mMetrics.devicePixelRatio;
    }

250
    public void destroy() {
251 252 253 254
        if (discoveryReceiver != null) {
            getContext().unregisterReceiver(discoveryReceiver);
        }

255 256 257 258 259 260 261 262 263 264
        if (mPlatformServiceProviderBinding != null) {
            mPlatformServiceProviderBinding.unbind().close();
            mPlatformServiceProvider.unbindServices();
        }

        if (mViewServiceProviderBinding != null) {
            mViewServiceProviderBinding.unbind().close();
            mViewServiceProvider.unbindServices();
        }

265
        getHolder().removeCallback(mSurfaceCallback);
A
Adam Barth 已提交
266 267
        nativeDetach(mNativePlatformView);
        mNativePlatformView = 0;
268 269 270 271

        mSkyEngine.close();
        mDartServiceProvider.close();
        mFlutterAppMessages.close();
272 273
    }

274 275
    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
276 277 278 279
        InputConnection connection = mKeyboardState.createInputConnection(outAttrs);
        if (connection == null)
          connection = mTextInputPlugin.createInputConnection(this, outAttrs);
        return connection;
280 281
    }

282
    // Must match the PointerChange enum in pointer.dart.
283 284 285 286 287 288
    private static final int kPointerChangeCancel = 0;
    private static final int kPointerChangeAdd = 1;
    private static final int kPointerChangeRemove = 2;
    private static final int kPointerChangeDown = 3;
    private static final int kPointerChangeMove = 4;
    private static final int kPointerChangeUp = 5;
289 290 291 292 293 294 295 296

    // Must match the PointerDeviceKind enum in pointer.dart.
    private static final int kPointerDeviceKindTouch = 0;
    private static final int kPointerDeviceKindMouse = 1;
    private static final int kPointerDeviceKindStylus = 2;
    private static final int kPointerDeviceKindInvertedStylus = 3;

    private int getPointerChangeForAction(int maskedAction) {
297
        // Primary pointer:
298
        if (maskedAction == MotionEvent.ACTION_DOWN) {
299
            return kPointerChangeDown;
300 301
        }
        if (maskedAction == MotionEvent.ACTION_UP) {
302
            return kPointerChangeUp;
303
        }
304
        // Secondary pointer:
305
        if (maskedAction == MotionEvent.ACTION_POINTER_DOWN) {
306
            return kPointerChangeDown;
307 308
        }
        if (maskedAction == MotionEvent.ACTION_POINTER_UP) {
309
            return kPointerChangeUp;
310
        }
311
        // All pointers:
312
        if (maskedAction == MotionEvent.ACTION_MOVE) {
313
            return kPointerChangeMove;
314 315
        }
        if (maskedAction == MotionEvent.ACTION_CANCEL) {
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
            return kPointerChangeCancel;
        }
        return -1;
    }

    private int getPointerDeviceTypeForToolType(int toolType) {
        switch (toolType) {
            case MotionEvent.TOOL_TYPE_FINGER:
                return kPointerDeviceKindTouch;
            case MotionEvent.TOOL_TYPE_STYLUS:
                return kPointerDeviceKindStylus;
            case MotionEvent.TOOL_TYPE_MOUSE:
                return kPointerDeviceKindMouse;
            default:
                // MotionEvent.TOOL_TYPE_UNKNOWN will reach here.
                return -1;
332
        }
333 334
    }

335
    private void addPointerForIndex(MotionEvent event, int pointerIndex,
336 337 338
                                    ByteBuffer packet) {
        int pointerChange = getPointerChangeForAction(event.getActionMasked());
        if (pointerChange == -1) {
339 340 341
            return;
        }

342 343 344 345
        int pointerKind = event.getToolType(pointerIndex);
        if (pointerKind == -1) {
            return;
        }
346

347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
        long timeStamp = event.getEventTime() * 1000; // Convert from milliseconds to microseconds.

        packet.putLong(timeStamp); // time_stamp
        packet.putLong(event.getPointerId(pointerIndex)); // pointer
        packet.putLong(pointerChange); // change
        packet.putLong(pointerKind); // kind
        packet.putDouble(event.getX(pointerIndex)); // physical_x
        packet.putDouble(event.getY(pointerIndex)); // physical_y

        if (pointerKind == kPointerDeviceKindMouse) {
          packet.putLong(event.getButtonState() & 0x1F); // buttons
        } else if (pointerKind == kPointerDeviceKindStylus) {
          packet.putLong((event.getButtonState() >> 4) & 0xF); // buttons
        } else {
          packet.putLong(0); // buttons
        }
363

364
        packet.putLong(0); // obscured
365

366 367
        // TODO(eseidel): Could get the calibrated range if necessary:
        // event.getDevice().getMotionRange(MotionEvent.AXIS_PRESSURE)
368 369 370
        packet.putDouble(event.getPressure(pointerIndex)); // presure
        packet.putDouble(0.0); // pressure_min
        packet.putDouble(1.0); // pressure_max
371

372 373 374 375 376 377 378 379 380 381
        if (pointerKind == kPointerDeviceKindStylus) {
          packet.putDouble(event.getAxisValue(MotionEvent.AXIS_DISTANCE, pointerIndex)); // distance
          packet.putDouble(0.0); // distance_max
        } else {
          packet.putDouble(0.0); // distance
          packet.putDouble(0.0); // distance_max
        }

        packet.putDouble(event.getToolMajor(pointerIndex)); // radius_major
        packet.putDouble(event.getToolMinor(pointerIndex)); // radius_minor
382

383 384
        packet.putDouble(0.0); // radius_min
        packet.putDouble(0.0); // radius_max
385

386
        packet.putDouble(event.getAxisValue(MotionEvent.AXIS_ORIENTATION, pointerIndex)); // orientation
387

388 389 390 391 392
        if (pointerKind == kPointerDeviceKindStylus) {
          packet.putDouble(event.getAxisValue(MotionEvent.AXIS_TILT, pointerIndex)); // tilt
        } else {
          packet.putDouble(0.0); // tilt
        }
393 394 395 396
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
A
Adam Barth 已提交
397 398 399 400 401 402 403 404
        // TODO(abarth): This version check might not be effective in some
        // versions of Android that statically compile code and will be upset
        // at the lack of |requestUnbufferedDispatch|. Instead, we should factor
        // version-dependent code into separate classes for each supported
        // version and dispatch dynamically.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            requestUnbufferedDispatch(event);
        }
405

406 407 408 409 410 411 412 413
        // These values must match the unpacking code in hooks.dart.
        final int kPointerDataFieldCount = 19;
        final int kBytePerField = 8;

        int pointerCount = event.getPointerCount();

        ByteBuffer packet = ByteBuffer.allocateDirect(pointerCount * kPointerDataFieldCount * kBytePerField);
        packet.order(ByteOrder.LITTLE_ENDIAN);
414

415 416 417 418 419 420 421
        int maskedAction = event.getActionMasked();
        // ACTION_UP, ACTION_POINTER_UP, ACTION_DOWN, and ACTION_POINTER_DOWN
        // only apply to a single pointer, other events apply to all pointers.
        if (maskedAction == MotionEvent.ACTION_UP
                || maskedAction == MotionEvent.ACTION_POINTER_UP
                || maskedAction == MotionEvent.ACTION_DOWN
                || maskedAction == MotionEvent.ACTION_POINTER_DOWN) {
422
            addPointerForIndex(event, event.getActionIndex(), packet);
423 424 425 426
        } else {
            // ACTION_MOVE may not actually mean all pointers have moved
            // but it's the responsibility of a later part of the system to
            // ignore 0-deltas if desired.
427 428
            for (int p = 0; p < pointerCount; p++) {
                addPointerForIndex(event, p, packet);
429 430
            }
        }
431

432 433
        assert packet.position() % (kPointerDataFieldCount * kBytePerField) == 0;
        nativeDispatchPointerDataPacket(mNativePlatformView, packet, packet.position());
434 435 436
        return true;
    }

437 438 439 440 441 442 443 444 445 446
    @Override
    public boolean onHoverEvent(MotionEvent event) {
        boolean handled = handleAccessibilityHoverEvent(event);
        if (!handled) {
            // TODO(ianh): Expose hover events to the platform,
            // implementing ADD, REMOVE, etc.
        }
        return handled;
    }

447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
    @Override
    protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
        mMetrics.physicalWidth = width;
        mMetrics.physicalHeight = height;
        mSkyEngine.onViewportMetricsChanged(mMetrics);
        super.onSizeChanged(width, height, oldWidth, oldHeight);
    }

    @Override
    public final WindowInsets onApplyWindowInsets(WindowInsets insets) {
        mMetrics.physicalPaddingTop = insets.getSystemWindowInsetTop();
        mMetrics.physicalPaddingRight = insets.getSystemWindowInsetRight();
        mMetrics.physicalPaddingBottom = insets.getSystemWindowInsetBottom();
        mMetrics.physicalPaddingLeft = insets.getSystemWindowInsetLeft();
        mSkyEngine.onViewportMetricsChanged(mMetrics);
        return super.onApplyWindowInsets(insets);
    }

465
    private void configureLocalServices(ServiceRegistry registry) {
A
Adam Barth 已提交
466
        registry.register(Keyboard.MANAGER.getName(), new ServiceFactory() {
467
            @Override
468 469
            public Binding connectToService(FlutterView view, Core core, MessagePipeHandle pipe) {
                return Keyboard.MANAGER.bind(new KeyboardImpl(view.getContext(), mKeyboardState), pipe);
470 471 472
            }
        });

473 474
        registry.register(ApplicationMessages.MANAGER.getName(), new ServiceFactory() {
            @Override
475
            public Binding connectToService(FlutterView view, Core core, MessagePipeHandle pipe) {
476
                return ApplicationMessages.MANAGER.bind(new ApplicationMessagesImpl(), pipe);
477 478
            }
        });
479 480
    }

481 482
    private void attach() {
        Core core = CoreImpl.getInstance();
483
        Pair<SkyEngine.Proxy, InterfaceRequest<SkyEngine>> engine =
484
                SkyEngine.MANAGER.getInterfaceRequest(core);
485
        mSkyEngine = engine.first;
486 487
        mNativePlatformView =
            nativeAttach(engine.second.passHandle().releaseNativeHandle(), this);
488
    }
489

490
    private void preRun() {
491 492 493 494 495 496 497
        if (mPlatformServiceProviderBinding != null) {
            mPlatformServiceProviderBinding.unbind().close();
            mPlatformServiceProvider.unbindServices();
        }
        if (mViewServiceProviderBinding != null) {
            mViewServiceProviderBinding.unbind().close();
            mViewServiceProvider.unbindServices();
H
Hixie 已提交
498 499 500 501 502
        }
        if (mDartServiceProvider != null) {
            mDartServiceProvider.close();
        }

503
        Core core = CoreImpl.getInstance();
504

H
Hixie 已提交
505 506 507 508
        Pair<ServiceProvider.Proxy, InterfaceRequest<ServiceProvider>> dartServiceProvider =
                ServiceProvider.MANAGER.getInterfaceRequest(core);
        mDartServiceProvider = dartServiceProvider.first;

509 510 511 512 513 514 515 516 517 518
        Pair<ServiceProvider.Proxy, InterfaceRequest<ServiceProvider>> platformServiceProvider =
                ServiceProvider.MANAGER.getInterfaceRequest(core);
        mPlatformServiceProviderBinding = ServiceProvider.MANAGER.bind(
                mPlatformServiceProvider, platformServiceProvider.second);

        Pair<ServiceProvider.Proxy, InterfaceRequest<ServiceProvider>> viewServiceProvider =
                ServiceProvider.MANAGER.getInterfaceRequest(core);
        mViewServiceProviderBinding = ServiceProvider.MANAGER.bind(
                mViewServiceProvider, viewServiceProvider.second);

519
        ServicesData services = new ServicesData();
520
        services.incomingServices = platformServiceProvider.first;
A
Adam Barth 已提交
521
        services.outgoingServices = dartServiceProvider.second;
522
        services.viewServices = viewServiceProvider.first;
523
        mSkyEngine.setServices(services);
524

H
Hixie 已提交
525
        resetAccessibilityTree();
526 527 528 529 530 531 532 533 534 535 536 537 538 539
    }

    private void postRun() {
        Core core = CoreImpl.getInstance();
        // Connect to the ApplicationMessages service exported by the Flutter framework
        Pair<ApplicationMessages.Proxy, InterfaceRequest<ApplicationMessages>> appMessages =
                  ApplicationMessages.MANAGER.getInterfaceRequest(core);
        mDartServiceProvider.connectToService(ApplicationMessages.MANAGER.getName(),
                                              appMessages.second.passHandle());
        mFlutterAppMessages = appMessages.first;
    }

    public void runFromBundle(String bundlePath, String snapshotPath) {
        preRun();
H
Hixie 已提交
540

541 542
        if (FlutterMain.isRunningPrecompiledCode()) {
            mSkyEngine.runFromPrecompiledSnapshot(bundlePath);
543
        } else {
544 545 546 547 548 549
            String scriptUri = "file://" + bundlePath;
            if (snapshotPath != null) {
                mSkyEngine.runFromBundleAndSnapshot(scriptUri, bundlePath, snapshotPath);
            } else {
                mSkyEngine.runFromBundle(scriptUri, bundlePath);
            }
550
        }
551

552 553 554
        postRun();
    }

555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
    public void runFromSource(final String main,
                              final String packages,
                              final String assetsDirectory) {
        Runnable runnable = new Runnable() {
            public void run() {
                preRun();
                mSkyEngine.runFromFile(main,
                                       packages,
                                       assetsDirectory);
                postRun();
                synchronized (this) {
                    notify();
                }
            }
        };

        try {
            synchronized (runnable) {
                // Post to the Android UI thread and wait for the response.
                post(runnable);
                runnable.wait();
            }
        } catch (InterruptedException e) {
            Log.e(TAG, "Thread got interrupted waiting for " +
                       "RunFromSourceRunnable to finish", e);
        }
581 582
    }

583 584 585 586 587
    /** Return the most recent frame as a bitmap. */
    public Bitmap getBitmap() {
        return nativeGetBitmap(mNativePlatformView);
    }

588 589
    private static native long nativeAttach(int skyEngineHandle,
                                            FlutterView view);
590
    private static native int nativeGetObservatoryPort();
591 592
    private static native void nativeDetach(long nativePlatformViewAndroid);
    private static native void nativeSurfaceCreated(long nativePlatformViewAndroid,
593 594 595 596 597
                                                    Surface surface,
                                                    int backgroundColor);
    private static native void nativeSurfaceChanged(long nativePlatformViewAndroid,
                                                    int width,
                                                    int height);
598
    private static native void nativeSurfaceDestroyed(long nativePlatformViewAndroid);
599
    private static native Bitmap nativeGetBitmap(long nativePlatformViewAndroid);
H
Hixie 已提交
600

601 602
    // Send a platform message to Dart.
    private static native void nativeDispatchPlatformMessage(long nativePlatformViewAndroid, String name, String message, int responseId);
603
    private static native void nativeDispatchPointerDataPacket(long nativePlatformViewAndroid, ByteBuffer buffer, int position);
604 605
    private static native void nativeDispatchSemanticsAction(long nativePlatformViewAndroid, int id, int action);
    private static native void nativeSetSemanticsEnabled(long nativePlatformViewAndroid, boolean enabled);
606

607 608 609 610
    // Send a response to a platform message received from Dart.
    private static native void nativeInvokePlatformMessageResponseCallback(long nativePlatformViewAndroid, int responseId, String message);

    // Called by native to send us a platform message.
611
    @CalledByNative
A
Adam Barth 已提交
612 613
    private void handlePlatformMessage(String name, String message, final int responseId) {
        OnMessageListener listener = mOnMessageListeners.get(name);
614 615 616 617 618
        if (listener != null) {
            nativeInvokePlatformMessageResponseCallback(mNativePlatformView, responseId, listener.onMessage(this, message));
            return;
        }

A
Adam Barth 已提交
619
        OnMessageListenerAsync asyncListener = mAsyncOnMessageListeners.get(name);
620 621
        if (asyncListener != null) {
            asyncListener.onMessage(this, message, new MessageResponse() {
622 623 624 625
                @Override
                public void send(String response) {
                    nativeInvokePlatformMessageResponseCallback(mNativePlatformView, responseId, response);
                }
626 627 628 629 630 631
            });
            return;
        }

        nativeInvokePlatformMessageResponseCallback(mNativePlatformView, responseId, null);
    }
H
Hixie 已提交
632

633 634 635 636 637 638 639 640 641 642 643
    private int mNextResponseId = 1;
    private final Map<Integer, MessageReplyCallback> mPendingResponses = new HashMap<Integer, MessageReplyCallback>();

    // Called by native to respond to a platform message that we sent.
    @CalledByNative
    private void handlePlatformMessageResponse(int responseId, String response) {
        MessageReplyCallback callback = mPendingResponses.remove(responseId);
        if (callback != null)
            callback.onReply(response);
    }

644 645
    @CalledByNative
    private void updateSemantics(ByteBuffer buffer, String[] strings) {
A
Adam Barth 已提交
646 647
        if (mAccessibilityNodeProvider != null) {
            buffer.order(ByteOrder.LITTLE_ENDIAN);
648
            mAccessibilityNodeProvider.updateSemantics(buffer, strings);
A
Adam Barth 已提交
649
        }
650 651
    }

H
Hixie 已提交
652 653
    // ACCESSIBILITY

H
Hixie 已提交
654
    private boolean mAccessibilityEnabled = false;
655
    private boolean mTouchExplorationEnabled = false;
656
    private TouchExplorationListener mTouchExplorationListener;
657

658 659 660 661
    protected void dispatchSemanticsAction(int id, int action) {
        nativeDispatchSemanticsAction(mNativePlatformView, id, action);
    }

H
Hixie 已提交
662 663 664
    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
H
Hixie 已提交
665
        mAccessibilityEnabled = mAccessibilityManager.isEnabled();
666
        mTouchExplorationEnabled = mAccessibilityManager.isTouchExplorationEnabled();
H
Hixie 已提交
667
        if (mAccessibilityEnabled || mTouchExplorationEnabled)
H
Hixie 已提交
668
          ensureAccessibilityEnabled();
H
Hixie 已提交
669
        resetWillNotDraw();
H
Hixie 已提交
670
        mAccessibilityManager.addAccessibilityStateChangeListener(this);
671 672 673 674 675
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (mTouchExplorationListener == null)
                mTouchExplorationListener = new TouchExplorationListener();
            mAccessibilityManager.addTouchExplorationStateChangeListener(mTouchExplorationListener);
        }
H
Hixie 已提交
676 677
    }

H
Hixie 已提交
678 679 680 681
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mAccessibilityManager.removeAccessibilityStateChangeListener(this);
682 683
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
            mAccessibilityManager.removeTouchExplorationStateChangeListener(mTouchExplorationListener);
H
Hixie 已提交
684 685 686 687 688 689
    }

    private void resetWillNotDraw() {
        setWillNotDraw(!(mAccessibilityEnabled || mTouchExplorationEnabled));
    }

H
Hixie 已提交
690 691
    @Override
    public void onAccessibilityStateChanged(boolean enabled) {
H
Hixie 已提交
692 693
        if (enabled) {
            mAccessibilityEnabled = true;
H
Hixie 已提交
694
            ensureAccessibilityEnabled();
H
Hixie 已提交
695 696 697 698 699 700
        } else {
            mAccessibilityEnabled = false;
        }
        if (mAccessibilityNodeProvider != null) {
            mAccessibilityNodeProvider.setAccessibilityEnabled(mAccessibilityEnabled);
        }
H
Hixie 已提交
701
        resetWillNotDraw();
H
Hixie 已提交
702 703
    }

704 705 706 707 708 709 710 711 712 713 714 715
    class TouchExplorationListener
      implements AccessibilityManager.TouchExplorationStateChangeListener {
        @Override
        public void onTouchExplorationStateChanged(boolean enabled) {
            if (enabled) {
                mTouchExplorationEnabled = true;
                ensureAccessibilityEnabled();
            } else {
                mTouchExplorationEnabled = false;
                if (mAccessibilityNodeProvider != null) {
                    mAccessibilityNodeProvider.handleTouchExplorationExit();
                }
716
            }
717
            resetWillNotDraw();
718
        }
H
Hixie 已提交
719 720
    }

H
Hixie 已提交
721 722 723 724 725 726
    @Override
    public AccessibilityNodeProvider getAccessibilityNodeProvider() {
        ensureAccessibilityEnabled();
        return mAccessibilityNodeProvider;
    }

727
    private AccessibilityBridge mAccessibilityNodeProvider;
H
Hixie 已提交
728 729 730

    void ensureAccessibilityEnabled() {
        if (mAccessibilityNodeProvider == null) {
731 732
            mAccessibilityNodeProvider = new AccessibilityBridge(this);
            nativeSetSemanticsEnabled(mNativePlatformView, true);
H
Hixie 已提交
733 734 735
        }
    }

H
Hixie 已提交
736 737
    void resetAccessibilityTree() {
        if (mAccessibilityNodeProvider != null) {
738
            mAccessibilityNodeProvider.reset();
H
Hixie 已提交
739
        }
H
Hixie 已提交
740 741
    }

742 743 744
    private boolean handleAccessibilityHoverEvent(MotionEvent event) {
        if (!mTouchExplorationEnabled)
            return false;
H
Hixie 已提交
745 746 747 748
        if (event.getAction() == MotionEvent.ACTION_HOVER_ENTER ||
                   event.getAction() == MotionEvent.ACTION_HOVER_MOVE) {
            mAccessibilityNodeProvider.handleTouchExploration(event.getX(), event.getY());
        } else if (event.getAction() == MotionEvent.ACTION_HOVER_EXIT) {
749 750
            mAccessibilityNodeProvider.handleTouchExplorationExit();
        } else {
H
Hixie 已提交
751 752
            Log.d("flutter", "unexpected accessibility hover event: " + event);
            return false;
753 754 755
        }
        return true;
    }
H
Hixie 已提交
756

757 758 759 760 761 762
    /**
     * Send a message to the Flutter application. The Flutter application can
     * register a platform message handler that will receive these messages with
     * the PlatformMessages object.
     */
    public void sendPlatformMessage(String name, String message, MessageReplyCallback callback) {
763 764 765 766 767 768 769 770
        int responseId = 0;
        if (callback != null) {
            responseId = mNextResponseId++;
            mPendingResponses.put(responseId, callback);
        }
        nativeDispatchPlatformMessage(mNativePlatformView, name, message, responseId);
    }

771 772 773 774 775 776
    /**
     * Send a message to the Flutter application.  The Flutter Dart code can register a
     * host message handler that will receive these messages.
     */
    public void sendToFlutter(String messageName, String message,
                              final MessageReplyCallback callback) {
777 778
        // TODO(abarth): Switch to dispatchPlatformMessage once the framework
        // side has been converted.
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
        mFlutterAppMessages.sendString(messageName, message,
            new ApplicationMessages.SendStringResponse() {
                @Override
                public void call(String reply) {
                    if (callback != null) {
                        callback.onReply(reply);
                    }
                }
            });
    }

    public void sendToFlutter(String messageName, String message) {
        sendToFlutter(messageName, message, null);
    }

    /** Callback invoked when the app replies to a message sent with sendToFlutter. */
    public interface MessageReplyCallback {
        void onReply(String reply);
    }

    /**
     * Register a callback to be invoked when the Flutter application sends a message
     * to its host.
     */
    public void addOnMessageListener(String messageName, OnMessageListener listener) {
        mOnMessageListeners.put(messageName, listener);
    }

    /**
     * Register a callback to be invoked when the Flutter application sends a message
     * to its host.  The reply to the message can be provided asynchronously.
     */
    public void addOnMessageListenerAsync(String messageName, OnMessageListenerAsync listener) {
        mAsyncOnMessageListeners.put(messageName, listener);
    }

    public interface OnMessageListener {
        /**
         * Called when a message is received from the Flutter app.
         * @return the reply to the message (can be null)
         */
820
        String onMessage(FlutterView view, String message);
821 822 823 824 825 826 827
    };

    public interface OnMessageListenerAsync {
        /**
         * Called when a message is received from the Flutter app.
         * @param response Used to send a reply back to the app.
         */
828
        void onMessage(FlutterView view, String message, MessageResponse response);
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
    }

    public interface MessageResponse {
        void send(String reply);
    }

    private class ApplicationMessagesImpl implements ApplicationMessages {
        @Override
        public void close() {}

        @Override
        public void onConnectionError(MojoException e) {}

        @Override
        public void sendString(String messageName, String message, SendStringResponse callback) {
            OnMessageListener listener = mOnMessageListeners.get(messageName);
            if (listener != null) {
846
                callback.call(listener.onMessage(FlutterView.this, message));
847 848 849 850 851
                return;
            }

            OnMessageListenerAsync asyncListener = mAsyncOnMessageListeners.get(messageName);
            if (asyncListener != null) {
852
                asyncListener.onMessage(FlutterView.this, message, new MessageResponseAdapter(callback));
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
                return;
            }

            callback.call(null);
        }
    }

    /**
     * This class wraps the raw Mojo callback object in an interface that is owned
     * by Flutter and can be safely given to host apps.
     */
    private static class MessageResponseAdapter implements MessageResponse {
        private ApplicationMessages.SendStringResponse callback;

        MessageResponseAdapter(ApplicationMessages.SendStringResponse callback) {
            this.callback = callback;
        }

        @Override
        public void send(String reply) {
            callback.call(reply);
        }
    }
876 877 878 879 880 881 882 883 884 885 886 887 888

    /** Broadcast receiver used to discover active Flutter instances. */
    private class DiscoveryReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            JSONObject discover = new JSONObject();
            try {
                discover.put("id", getContext().getPackageName());
                discover.put("observatoryPort", nativeGetObservatoryPort());
                Log.i(TAG, "DISCOVER: " + discover);
            } catch (JSONException e) {}
        }
    }
889
}