Skip to content

Commit

Permalink
Android embedding refactor PR1: JNI Extraction to FlutterJNI.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Carroll committed Dec 5, 2018
1 parent 7375a0f commit 89e5be1
Show file tree
Hide file tree
Showing 10 changed files with 781 additions and 253 deletions.
3 changes: 3 additions & 0 deletions shell/platform/android/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Generated by Intellij's Android plugin
gen
android.iml
out/

5 changes: 5 additions & 0 deletions shell/platform/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ java_library("flutter_shell_java") {
"io/flutter/app/FlutterApplication.java",
"io/flutter/app/FlutterFragmentActivity.java",
"io/flutter/app/FlutterPluginRegistry.java",
"io/flutter/embedding/engine/FlutterEngine.java",
"io/flutter/embedding/engine/FlutterJNI.java",
"io/flutter/embedding/engine/dart/PlatformMessageHandler.java",
"io/flutter/embedding/engine/renderer/FlutterRenderer.java",
"io/flutter/embedding/engine/renderer/OnFirstFrameRenderedListener.java",
"io/flutter/plugin/common/ActivityLifecycleListener.java",
"io/flutter/plugin/common/BasicMessageChannel.java",
"io/flutter/plugin/common/BinaryCodec.java",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2013 The Flutter 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.embedding.engine;

/**
* WARNING: THIS CLASS IS EXPERIMENTAL. DO NOT SHIP A DEPENDENCY ON THIS CODE.
* IF YOU USE IT, WE WILL BREAK YOU.
*/
public class FlutterEngine {
// TODO(mattcarroll): bring in FlutterEngine implementation in future PR

/**
* Lifecycle callbacks for Flutter engine lifecycle events.
*/
public interface EngineLifecycleListener {
/**
* Lifecycle callback invoked after a hot restart of the Flutter engine.
*/
void onPreEngineRestart();
}
}
217 changes: 217 additions & 0 deletions shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
// Copyright 2013 The Flutter 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.embedding.engine;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.SurfaceTexture;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Surface;

import java.nio.ByteBuffer;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

import io.flutter.embedding.engine.dart.PlatformMessageHandler;
import io.flutter.embedding.engine.FlutterEngine.EngineLifecycleListener;
import io.flutter.embedding.engine.renderer.FlutterRenderer;
import io.flutter.embedding.engine.renderer.OnFirstFrameRenderedListener;

/**
* WARNING: THIS CLASS IS EXPERIMENTAL. DO NOT SHIP A DEPENDENCY ON THIS CODE.
* IF YOU USE IT, WE WILL BREAK YOU.
*/
public class FlutterJNI {
private static final String TAG = "FlutterJNI";

private FlutterRenderer.RenderSurface renderSurface;
private PlatformMessageHandler platformMessageHandler;
private final Set<EngineLifecycleListener> engineLifecycleListeners = new CopyOnWriteArraySet<>();
private final Set<OnFirstFrameRenderedListener> firstFrameListeners = new CopyOnWriteArraySet<>();

public void setRenderSurface(@Nullable FlutterRenderer.RenderSurface renderSurface) {
this.renderSurface = renderSurface;
}

public void setPlatformMessageHandler(@Nullable PlatformMessageHandler platformMessageHandler) {
this.platformMessageHandler = platformMessageHandler;
}

public void addEngineLifecycleListener(@NonNull EngineLifecycleListener engineLifecycleListener) {
engineLifecycleListeners.add(engineLifecycleListener);
}

public void removeEngineLifecycleListener(@NonNull EngineLifecycleListener engineLifecycleListener) {
engineLifecycleListeners.remove(engineLifecycleListener);
}

public void addOnFirstFrameRenderedListener(@NonNull OnFirstFrameRenderedListener listener) {
firstFrameListeners.add(listener);
}

public void removeOnFirstFrameRenderedListener(@NonNull OnFirstFrameRenderedListener listener) {
firstFrameListeners.remove(listener);
}

//------ START RENDER SURFACE CALLBACKS -----
// TODO(mattcarroll): define "update"
// Called by native to update the semantics/accessibility tree.
@SuppressWarnings("unused")
public void updateSemantics(ByteBuffer buffer, String[] strings) {
Log.d(TAG, "updateSemantics()");
if (renderSurface != null) {
renderSurface.updateSemantics(buffer, strings);
}
}

// TODO(mattcarroll): define "update"
// Called by native to update the custom accessibility actions.
@SuppressWarnings("unused")
public void updateCustomAccessibilityActions(ByteBuffer buffer, String[] strings) {
Log.d(TAG, "updateCustomAccessibilityActions()");
if (renderSurface != null) {
renderSurface.updateCustomAccessibilityActions(buffer, strings);
}
}

// Called by native to notify first Flutter frame rendered.
@SuppressWarnings("unused")
private void onFirstFrame() {
Log.d(TAG, "onFirstFrame()");
if (renderSurface != null) {
renderSurface.onFirstFrameRendered();
}

for (OnFirstFrameRenderedListener listener : firstFrameListeners) {
listener.onFirstFrameRendered();
}
}
//------ END RENDER SURFACE CALLBACKS ------

//------ START PLATFORM MESSAGE CALLBACKS ----
@SuppressWarnings("unused")
private void handlePlatformMessage(final String channel, byte[] message, final int replyId) {
if (platformMessageHandler != null) {
platformMessageHandler.handlePlatformMessage(channel, message, replyId);
}
}

// Called by native to respond to a platform message that we sent.
@SuppressWarnings("unused")
private void handlePlatformMessageResponse(int replyId, byte[] reply) {
if (platformMessageHandler != null) {
platformMessageHandler.handlePlatformMessageResponse(replyId, reply);
}
}
//------ END PLATFORM MESSAGE CALLBACKS ----

// TODO(mattcarroll): rename comments after refactor is done and their origin no longer matters
//----- Start from FlutterView -----
public native void nativeSurfaceCreated(long nativePlatformViewAndroid, Surface surface);

public native void nativeSurfaceChanged(long nativePlatformViewAndroid,
int width,
int height);

public native void nativeSurfaceDestroyed(long nativePlatformViewAndroid);

public native void nativeSetViewportMetrics(long nativePlatformViewAndroid,
float devicePixelRatio,
int physicalWidth,
int physicalHeight,
int physicalPaddingTop,
int physicalPaddingRight,
int physicalPaddingBottom,
int physicalPaddingLeft,
int physicalViewInsetTop,
int physicalViewInsetRight,
int physicalViewInsetBottom,
int physicalViewInsetLeft);

public native Bitmap nativeGetBitmap(long nativePlatformViewAndroid);

public native void nativeDispatchPointerDataPacket(long nativePlatformViewAndroid,
ByteBuffer buffer,
int position);

public native void nativeDispatchSemanticsAction(long nativePlatformViewAndroid,
int id,
int action,
ByteBuffer args,
int argsPosition);

public native void nativeSetSemanticsEnabled(long nativePlatformViewAndroid, boolean enabled);

public native void nativeSetAccessibilityFeatures(long nativePlatformViewAndroid, int flags);

public native boolean nativeGetIsSoftwareRenderingEnabled();

public native void nativeRegisterTexture(long nativePlatformViewAndroid, long textureId, SurfaceTexture surfaceTexture);

public native void nativeMarkTextureFrameAvailable(long nativePlatformViewAndroid, long textureId);

public native void nativeUnregisterTexture(long nativePlatformViewAndroid, long textureId);
//------- End from FlutterView -----

// TODO(mattcarroll): rename comments after refactor is done and their origin no longer matters
//------ Start from FlutterNativeView ----
public native long nativeAttach(FlutterJNI flutterJNI, boolean isBackgroundView);
public native void nativeDestroy(long nativePlatformViewAndroid);
public native void nativeDetach(long nativePlatformViewAndroid);

public native void nativeRunBundleAndSnapshotFromLibrary(
long nativePlatformViewAndroid,
@NonNull String pathToBundleWithEntrypoint,
@Nullable String pathToFallbackBundle,
@Nullable String entrypointFunctionName,
@Nullable String pathToEntrypointFunction,
@NonNull AssetManager manager
);

public native String nativeGetObservatoryUri();

// Send an empty platform message to Dart.
public native void nativeDispatchEmptyPlatformMessage(
long nativePlatformViewAndroid,
String channel,
int responseId
);

// Send a data-carrying platform message to Dart.
public native void nativeDispatchPlatformMessage(
long nativePlatformViewAndroid,
String channel,
ByteBuffer message,
int position,
int responseId
);

// Send an empty response to a platform message received from Dart.
public native void nativeInvokePlatformMessageEmptyResponseCallback(
long nativePlatformViewAndroid,
int responseId
);

// Send a data-carrying response to a platform message received from Dart.
public native void nativeInvokePlatformMessageResponseCallback(
long nativePlatformViewAndroid,
int responseId,
ByteBuffer message,
int position
);
//------ End from FlutterNativeView ----

// TODO(mattcarroll): rename comments after refactor is done and their origin no longer matters
//------ Start from Engine ---
@SuppressWarnings("unused")
private void onPreEngineRestart() {
for (EngineLifecycleListener listener : engineLifecycleListeners) {
listener.onPreEngineRestart();
}
}
//------ End from Engine ---
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2013 The Flutter 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.embedding.engine.dart;

/**
* WARNING: THIS CLASS IS EXPERIMENTAL. DO NOT SHIP A DEPENDENCY ON THIS CODE.
* IF YOU USE IT, WE WILL BREAK YOU.
*/
public interface PlatformMessageHandler {
void handlePlatformMessage(final String channel, byte[] message, final int replyId);

void handlePlatformMessageResponse(int replyId, byte[] reply);
}
Loading

0 comments on commit 89e5be1

Please sign in to comment.