Skip to content

Commit

Permalink
Introduced a number of Java system channels in io/flutter/embedding/e…
Browse files Browse the repository at this point in the history
…ngine/systemchannels/ (flutter#7500)
  • Loading branch information
matthew-carroll committed Feb 7, 2019
1 parent fdf57a6 commit 211adea
Show file tree
Hide file tree
Showing 16 changed files with 379 additions and 112 deletions.
6 changes: 6 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,13 @@ FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/dart/D
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/dart/PlatformMessageHandler.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/renderer/FlutterRenderer.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/renderer/OnFirstFrameRenderedListener.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/KeyEventChannel.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/LifecycleChannel.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/LocalizationChannel.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/NavigationChannel.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/SettingsChannel.java
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/SystemChannel.java
FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/common/ActivityLifecycleListener.java
FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/common/BasicMessageChannel.java
FILE: ../../../flutter/shell/platform/android/io/flutter/plugin/common/BinaryCodec.java
Expand Down
6 changes: 6 additions & 0 deletions shell/platform/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,13 @@ java_library("flutter_shell_java") {
"io/flutter/embedding/engine/dart/PlatformMessageHandler.java",
"io/flutter/embedding/engine/renderer/FlutterRenderer.java",
"io/flutter/embedding/engine/renderer/OnFirstFrameRenderedListener.java",
"io/flutter/embedding/engine/systemchannels/KeyEventChannel.java",
"io/flutter/embedding/engine/systemchannels/LifecycleChannel.java",
"io/flutter/embedding/engine/systemchannels/LocalizationChannel.java",
"io/flutter/embedding/engine/systemchannels/NavigationChannel.java",
"io/flutter/embedding/engine/systemchannels/PlatformChannel.java",
"io/flutter/embedding/engine/systemchannels/SettingsChannel.java",
"io/flutter/embedding/engine/systemchannels/SystemChannel.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
Expand Up @@ -199,7 +199,7 @@ public void setPlatformMessageHandler(@Nullable PlatformMessageHandler platformM
@SuppressWarnings("unused")
private void handlePlatformMessage(final String channel, byte[] message, final int replyId) {
if (platformMessageHandler != null) {
platformMessageHandler.handlePlatformMessage(channel, message, replyId);
platformMessageHandler.handleMessageFromDart(channel, message, replyId);
}
// TODO(mattcarroll): log dropped messages when in debug mode (https://github.com/flutter/flutter/issues/25391)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,33 @@
*/
public class DartExecutor implements BinaryMessenger {
private static final String TAG = "DartExecutor";


@NonNull
private final FlutterJNI flutterJNI;
@NonNull
private final DartMessenger messenger;
private boolean isApplicationRunning = false;

public DartExecutor(@NonNull FlutterJNI flutterJNI) {
this.flutterJNI = flutterJNI;
this.messenger = new DartMessenger(flutterJNI);
}

/**
* Invoked when the {@link io.flutter.embedding.engine.FlutterEngine} that owns this
* {@link DartExecutor} attaches to JNI.
* <p>
* When attached to JNI, this {@link DartExecutor} begins handling 2-way communication to/from
* the Dart execution context. This communication is facilitate via 2 APIs:
* - {@link BinaryMessenger}, which sends messages to Dart
* - {@link PlatformMessageHandler}, which receives messages from Dart
* <ul>
* <li>{@link BinaryMessenger}, which sends messages to Dart</li>
* <li>{@link PlatformMessageHandler}, which receives messages from Dart</li>
* </ul>
*/
public void onAttachedToJNI() {
flutterJNI.setPlatformMessageHandler(messenger);
}

/**
* Invoked when the {@link io.flutter.embedding.engine.FlutterEngine} that owns this
* {@link DartExecutor} detaches from JNI.
Expand All @@ -70,7 +74,7 @@ public void onAttachedToJNI() {
public void onDetachedFromJNI() {
flutterJNI.setPlatformMessageHandler(null);
}

/**
* Is this {@link DartExecutor} currently executing Dart code?
*
Expand All @@ -79,20 +83,20 @@ public void onDetachedFromJNI() {
public boolean isExecutingDart() {
return isApplicationRunning;
}

/**
* Starts executing Dart code based on the given {@code dartEntrypoint}.
* <p>
* See {@link DartEntrypoint} for configuration options.
*
* @param dartEntrypoint specifies which Dart function to run, and where to find it
*/
public void executeDartEntrypoint(DartEntrypoint dartEntrypoint) {
public void executeDartEntrypoint(@NonNull DartEntrypoint dartEntrypoint) {
if (isApplicationRunning) {
Log.w(TAG, "Attempted to run a DartExecutor that is already running.");
return;
}

flutterJNI.runBundleAndSnapshotFromLibrary(
new String[]{
dartEntrypoint.pathToPrimaryBundle,
Expand All @@ -102,23 +106,23 @@ public void executeDartEntrypoint(DartEntrypoint dartEntrypoint) {
null,
dartEntrypoint.androidAssetManager
);

isApplicationRunning = true;
}

/**
* Starts executing Dart code based on the given {@code dartCallback}.
* <p>
* See {@link DartCallback} for configuration options.
*
* @param dartCallback specifies which Dart callback to run, and where to find it
*/
public void executeDartCallback(DartCallback dartCallback) {
public void executeDartCallback(@NonNull DartCallback dartCallback) {
if (isApplicationRunning) {
Log.w(TAG, "Attempted to run a DartExecutor that is already running.");
return;
}

flutterJNI.runBundleAndSnapshotFromLibrary(
new String[]{
dartCallback.pathToPrimaryBundle,
Expand All @@ -128,23 +132,23 @@ public void executeDartCallback(DartCallback dartCallback) {
dartCallback.callbackHandle.callbackLibraryPath,
dartCallback.androidAssetManager
);

isApplicationRunning = true;
}

//------ START BinaryMessenger -----

/**
* Sends the given {@code message} from Android to Dart over the given {@code channel}.
*
* @param channel the name of the logical channel used for the message.
* @param message the message payload, a direct-allocated {@link ByteBuffer} with the message bytes
*/
@Override
public void send(String channel, ByteBuffer message) {
public void send(@NonNull String channel, @Nullable ByteBuffer message) {
messenger.send(channel, message, null);
}

/**
* Sends the given {@code messages} from Android to Dart over the given {@code channel} and
* then has the provided {@code callback} invoked when the Dart side responds.
Expand All @@ -155,10 +159,10 @@ public void send(String channel, ByteBuffer message) {
* @param callback a callback invoked when the Dart application responds to the message
*/
@Override
public void send(String channel, ByteBuffer message, BinaryMessenger.BinaryReply callback) {
public void send(@NonNull String channel, @Nullable ByteBuffer message, @Nullable BinaryMessenger.BinaryReply callback) {
messenger.send(channel, message, callback);
}

/**
* Sets the given {@link io.flutter.plugin.common.BinaryMessenger.BinaryMessageHandler} as the
* singular handler for all incoming messages received from the Dart side of this Dart execution
Expand All @@ -168,11 +172,11 @@ public void send(String channel, ByteBuffer message, BinaryMessenger.BinaryReply
* @param handler a {@link BinaryMessageHandler} to be invoked on incoming messages, or null.
*/
@Override
public void setMessageHandler(String channel, BinaryMessenger.BinaryMessageHandler handler) {
public void setMessageHandler(@NonNull String channel, @Nullable BinaryMessenger.BinaryMessageHandler handler) {
messenger.setMessageHandler(channel, handler);
}
//------ END BinaryMessenger -----

/**
* Configuration options that specify which Dart entrypoint function is executed and where
* to find that entrypoint and other assets required for Dart execution.
Expand All @@ -181,23 +185,27 @@ public static class DartEntrypoint {
/**
* Standard Android AssetManager, provided from some {@code Context} or {@code Resources}.
*/
@NonNull
public final AssetManager androidAssetManager;

/**
* The first place that Dart will look for a given function or asset.
*/
@NonNull
public final String pathToPrimaryBundle;

/**
* A secondary fallback location that Dart will look for a given function or asset.
*/
@Nullable
public final String pathToFallbackBundle;

/**
* The name of a Dart function to execute.
*/
@NonNull
public final String dartEntrypointFunctionName;

public DartEntrypoint(
@NonNull AssetManager androidAssetManager,
@NonNull String pathToBundle,
Expand All @@ -210,7 +218,7 @@ public DartEntrypoint(
dartEntrypointFunctionName
);
}

public DartEntrypoint(
@NonNull AssetManager androidAssetManager,
@NonNull String pathToPrimaryBundle,
Expand All @@ -223,7 +231,7 @@ public DartEntrypoint(
this.dartEntrypointFunctionName = dartEntrypointFunctionName;
}
}

/**
* Configuration options that specify which Dart callback function is executed and where
* to find that callback and other assets required for Dart execution.
Expand All @@ -233,22 +241,22 @@ public static class DartCallback {
* Standard Android AssetManager, provided from some {@code Context} or {@code Resources}.
*/
public final AssetManager androidAssetManager;

/**
* The first place that Dart will look for a given function or asset.
*/
public final String pathToPrimaryBundle;

/**
* A secondary fallback location that Dart will look for a given function or asset.
*/
public final String pathToFallbackBundle;

/**
* A Dart callback that was previously registered with the Dart VM.
*/
public final FlutterCallbackInformation callbackHandle;

public DartCallback(
@NonNull AssetManager androidAssetManager,
@NonNull String pathToPrimaryBundle,
Expand All @@ -261,7 +269,7 @@ public DartCallback(
callbackHandle
);
}

public DartCallback(
@NonNull AssetManager androidAssetManager,
@NonNull String pathToPrimaryBundle,
Expand Down
Loading

0 comments on commit 211adea

Please sign in to comment.