diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index b2ed5bab64..91d15b8fcd 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -423,9 +423,12 @@ FILE: ../../../flutter/shell/platform/android/io/flutter/app/FlutterFragmentActi FILE: ../../../flutter/shell/platform/android/io/flutter/app/FlutterPluginRegistry.java FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngine.java FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java +FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/dart/DartExecutor.java +FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/dart/DartMessenger.java 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/SettingsChannel.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 diff --git a/engine/src/flutter/lib/ui/hooks.dart b/engine/src/flutter/lib/ui/hooks.dart index 424028b391..3f536d2fa1 100644 --- a/engine/src/flutter/lib/ui/hooks.dart +++ b/engine/src/flutter/lib/ui/hooks.dart @@ -86,6 +86,7 @@ void _updateUserSettingsData(String jsonData) { } _updateTextScaleFactor(data['textScaleFactor'].toDouble()); _updateAlwaysUse24HourFormat(data['alwaysUse24HourFormat']); + _updatePlatformBrightness(data['platformBrightness']); } void _updateTextScaleFactor(double textScaleFactor) { @@ -97,6 +98,11 @@ void _updateAlwaysUse24HourFormat(bool alwaysUse24HourFormat) { window._alwaysUse24HourFormat = alwaysUse24HourFormat; } +void _updatePlatformBrightness(String brightnessName) { + window._platformBrightness = brightnessName == 'dark' ? Brightness.dark : Brightness.light; + _invoke(window.onPlatformBrightnessChanged, window._onPlatformBrightnessChangedZone); +} + @pragma('vm:entry-point') // ignore: unused_element void _updateSemanticsEnabled(bool enabled) { diff --git a/engine/src/flutter/lib/ui/window.dart b/engine/src/flutter/lib/ui/window.dart index 72046ff859..16dc145a06 100644 --- a/engine/src/flutter/lib/ui/window.dart +++ b/engine/src/flutter/lib/ui/window.dart @@ -590,6 +590,28 @@ class Window { _onTextScaleFactorChangedZone = Zone.current; } + /// The setting indicating the current brightness mode of the host platform. + /// If the platform has no preference, [platformBrightness] defaults to [Brightness.light]. + Brightness get platformBrightness => _platformBrightness; + Brightness _platformBrightness = Brightness.light; + + /// A callback that is invoked whenever [platformBrightness] changes value. + /// + /// The framework invokes this callback in the same zone in which the + /// callback was set. + /// + /// See also: + /// + /// * [WidgetsBindingObserver], for a mechanism at the widgets layer to + /// observe when this callback is invoked. + VoidCallback get onPlatformBrightnessChanged => _onPlatformBrightnessChanged; + VoidCallback _onPlatformBrightnessChanged; + Zone _onPlatformBrightnessChangedZone; + set onPlatformBrightnessChanged(VoidCallback callback) { + _onPlatformBrightnessChanged = callback; + _onPlatformBrightnessChangedZone = Zone.current; + } + /// A callback that is invoked to notify the application that it is an /// appropriate time to provide a scene using the [SceneBuilder] API and the /// [render] method. When possible, this is driven by the hardware VSync diff --git a/engine/src/flutter/shell/platform/android/BUILD.gn b/engine/src/flutter/shell/platform/android/BUILD.gn index e9d2a961f8..1b49602f25 100644 --- a/engine/src/flutter/shell/platform/android/BUILD.gn +++ b/engine/src/flutter/shell/platform/android/BUILD.gn @@ -108,9 +108,12 @@ java_library("flutter_shell_java") { "io/flutter/app/FlutterPluginRegistry.java", "io/flutter/embedding/engine/FlutterEngine.java", "io/flutter/embedding/engine/FlutterJNI.java", + "io/flutter/embedding/engine/dart/DartExecutor.java", + "io/flutter/embedding/engine/dart/DartMessenger.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/SettingsChannel.java", "io/flutter/plugin/common/ActivityLifecycleListener.java", "io/flutter/plugin/common/BasicMessageChannel.java", "io/flutter/plugin/common/BinaryCodec.java", diff --git a/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterActivity.java b/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterActivity.java index 33c7e75d17..7b8052b261 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterActivity.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterActivity.java @@ -9,6 +9,8 @@ import android.content.Context; import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle; +import android.support.annotation.NonNull; +import android.util.Log; import io.flutter.app.FlutterActivityDelegate.ViewFactory; import io.flutter.plugin.common.PluginRegistry; import io.flutter.view.FlutterNativeView; @@ -18,6 +20,8 @@ import io.flutter.view.FlutterView; * Base class for activities that use Flutter. */ public class FlutterActivity extends Activity implements FlutterView.Provider, PluginRegistry, ViewFactory { + private static final String TAG = "FlutterActivity"; + private final FlutterActivityDelegate delegate = new FlutterActivityDelegate(this, this); // These aliases ensure that the methods we forward to the delegate adhere @@ -129,7 +133,7 @@ public class FlutterActivity extends Activity implements FlutterView.Provider, P } // @Override - added in API level 23 - public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { eventDelegate.onRequestPermissionsResult(requestCode, permissions, grantResults); } diff --git a/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterActivityDelegate.java b/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterActivityDelegate.java index 5f56699360..4230208eb4 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterActivityDelegate.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterActivityDelegate.java @@ -8,6 +8,7 @@ import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.app.Activity; import android.app.Application; +import android.app.UiModeManager; import android.content.Context; import android.content.Intent; import android.content.pm.ActivityInfo; @@ -283,8 +284,7 @@ public final class FlutterActivityDelegate } @Override - public void onConfigurationChanged(Configuration newConfig) { - } + public void onConfigurationChanged(Configuration newConfig) {} private static String[] getArgsFromIntent(Intent intent) { // Before adding more entries to this list, consider that arbitrary @@ -399,7 +399,7 @@ public final class FlutterActivityDelegate if (!activity.getTheme().resolveAttribute( android.R.attr.windowBackground, typedValue, - true)) {; + true)) { return null; } if (typedValue.resourceId == 0) { diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/dart/DartExecutor.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/dart/DartExecutor.java new file mode 100644 index 0000000000..28d6f52772 --- /dev/null +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/dart/DartExecutor.java @@ -0,0 +1,277 @@ +// 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; + +import android.content.res.AssetManager; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.util.Log; + +import java.nio.ByteBuffer; + +import io.flutter.embedding.engine.FlutterJNI; +import io.flutter.plugin.common.BinaryMessenger; +import io.flutter.view.FlutterCallbackInformation; + +/** + * Configures, bootstraps, and starts executing Dart code. + *
+ * WARNING: THIS CLASS IS EXPERIMENTAL. DO NOT SHIP A DEPENDENCY ON THIS CODE. + * IF YOU USE IT, WE WILL BREAK YOU. + *
+ * To specify a top-level Dart function to execute, use a {@link DartEntrypoint} to tell + * {@link DartExecutor} where to find the Dart code to execute, and which Dart function to use as + * the entrypoint. To execute the entrypoint, pass the {@link DartEntrypoint} to + * {@link #executeDartEntrypoint(DartEntrypoint)}. + *
+ * To specify a Dart callback to execute, use a {@link DartCallback}. A given Dart callback must + * be registered with the Dart VM to be invoked by a {@link DartExecutor}. To execute the callback, + * pass the {@link DartCallback} to {@link #executeDartCallback(DartCallback)}. + * TODO(mattcarroll): add a reference to docs about background/plugin execution + *
+ * Once started, a {@link DartExecutor} cannot be stopped. The associated Dart code will execute + * until it completes, or until the {@link io.flutter.embedding.engine.FlutterEngine} that owns + * this {@link DartExecutor} is destroyed. + */ +public class DartExecutor implements BinaryMessenger { + private static final String TAG = "DartExecutor"; + + private final FlutterJNI flutterJNI; + 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. + *
+ * 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 + */ + public void onAttachedToJNI() { + flutterJNI.setPlatformMessageHandler(messenger); + } + + /** + * Invoked when the {@link io.flutter.embedding.engine.FlutterEngine} that owns this + * {@link DartExecutor} detaches from JNI. + *
+ * When detached from JNI, this {@link DartExecutor} stops handling 2-way communication to/from + * the Dart execution context. + */ + public void onDetachedFromJNI() { + flutterJNI.setPlatformMessageHandler(null); + } + + /** + * Is this {@link DartExecutor} currently executing Dart code? + * + * @return true if Dart code is being executed, false otherwise + */ + public boolean isExecutingDart() { + return isApplicationRunning; + } + + /** + * Starts executing Dart code based on the given {@code dartEntrypoint}. + *
+ * See {@link DartEntrypoint} for configuration options. + * + * @param dartEntrypoint specifies which Dart function to run, and where to find it + */ + public void executeDartEntrypoint(DartEntrypoint dartEntrypoint) { + if (isApplicationRunning) { + Log.w(TAG, "Attempted to run a DartExecutor that is already running."); + return; + } + + flutterJNI.runBundleAndSnapshotFromLibrary( + new String[]{ + dartEntrypoint.pathToPrimaryBundle, + dartEntrypoint.pathToFallbackBundle + }, + dartEntrypoint.dartEntrypointFunctionName, + null, + dartEntrypoint.androidAssetManager + ); + + isApplicationRunning = true; + } + + /** + * Starts executing Dart code based on the given {@code dartCallback}. + *
+ * See {@link DartCallback} for configuration options. + * + * @param dartCallback specifies which Dart callback to run, and where to find it + */ + public void executeDartCallback(DartCallback dartCallback) { + if (isApplicationRunning) { + Log.w(TAG, "Attempted to run a DartExecutor that is already running."); + return; + } + + flutterJNI.runBundleAndSnapshotFromLibrary( + new String[]{ + dartCallback.pathToPrimaryBundle, + dartCallback.pathToFallbackBundle + }, + dartCallback.callbackHandle.callbackName, + 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) { + 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. + * + * @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 + * between position zero and current position, or null. + * @param callback a callback invoked when the Dart application responds to the message + */ + @Override + public void send(String channel, ByteBuffer message, 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 + * context. + * + * @param channel the name of the channel. + * @param handler a {@link BinaryMessageHandler} to be invoked on incoming messages, or null. + */ + @Override + public void setMessageHandler(String channel, 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. + */ + public static class DartEntrypoint { + /** + * 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; + + /** + * The name of a Dart function to execute. + */ + public final String dartEntrypointFunctionName; + + public DartEntrypoint( + @NonNull AssetManager androidAssetManager, + @NonNull String pathToBundle, + @NonNull String dartEntrypointFunctionName + ) { + this( + androidAssetManager, + pathToBundle, + null, + dartEntrypointFunctionName + ); + } + + public DartEntrypoint( + @NonNull AssetManager androidAssetManager, + @NonNull String pathToPrimaryBundle, + @Nullable String pathToFallbackBundle, + @NonNull String dartEntrypointFunctionName + ) { + this.androidAssetManager = androidAssetManager; + this.pathToPrimaryBundle = pathToPrimaryBundle; + this.pathToFallbackBundle = pathToFallbackBundle; + 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. + */ + 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, + @NonNull FlutterCallbackInformation callbackHandle + ) { + this( + androidAssetManager, + pathToPrimaryBundle, + null, + callbackHandle + ); + } + + public DartCallback( + @NonNull AssetManager androidAssetManager, + @NonNull String pathToPrimaryBundle, + @Nullable String pathToFallbackBundle, + @NonNull FlutterCallbackInformation callbackHandle + ) { + this.androidAssetManager = androidAssetManager; + this.pathToPrimaryBundle = pathToPrimaryBundle; + this.pathToFallbackBundle = pathToFallbackBundle; + this.callbackHandle = callbackHandle; + } + } +} \ No newline at end of file diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/dart/DartMessenger.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/dart/DartMessenger.java new file mode 100644 index 0000000000..16a6b10b8a --- /dev/null +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/dart/DartMessenger.java @@ -0,0 +1,109 @@ +// 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; + +import android.support.annotation.NonNull; +import android.util.Log; + +import java.nio.ByteBuffer; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; + +import io.flutter.embedding.engine.FlutterJNI; +import io.flutter.plugin.common.BinaryMessenger; + +/** + * Message conduit for 2-way communication between Android and Dart. + *
+ * WARNING: THIS CLASS IS EXPERIMENTAL. DO NOT SHIP A DEPENDENCY ON THIS CODE. + * IF YOU USE IT, WE WILL BREAK YOU. + *
+ * See {@link BinaryMessenger}, which sends messages from Android to Dart
+ * See {@link PlatformMessageHandler}, which handles messages to Android from Dart
+ */
+class DartMessenger implements BinaryMessenger, PlatformMessageHandler {
+ private static final String TAG = "DartMessenger";
+
+ private final FlutterJNI flutterJNI;
+ private final Map