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 messageHandlers; + private final Map mPendingReplies = new HashMap<>(); + private int mNextReplyId = 1; + + DartMessenger(@NonNull FlutterJNI flutterJNI) { + this.flutterJNI = flutterJNI; + this.messageHandlers = new HashMap<>(); + } + + @Override + public void setMessageHandler(String channel, BinaryMessenger.BinaryMessageHandler handler) { + if (handler == null) { + messageHandlers.remove(channel); + } else { + messageHandlers.put(channel, handler); + } + } + + @Override + public void send(String channel, ByteBuffer message) { + send(channel, message, null); + } + + @Override + public void send(String channel, ByteBuffer message, BinaryMessenger.BinaryReply callback) { + int replyId = 0; + if (callback != null) { + replyId = mNextReplyId++; + mPendingReplies.put(replyId, callback); + } + if (message == null) { + flutterJNI.dispatchEmptyPlatformMessage(channel, replyId); + } else { + flutterJNI.dispatchPlatformMessage(channel, message, message.position(), replyId); + } + } + + @Override + public void handlePlatformMessage(final String channel, byte[] message, final int replyId) { + BinaryMessenger.BinaryMessageHandler handler = messageHandlers.get(channel); + if (handler != null) { + try { + final ByteBuffer buffer = (message == null ? null : ByteBuffer.wrap(message)); + handler.onMessage(buffer, new BinaryMessenger.BinaryReply() { + private final AtomicBoolean done = new AtomicBoolean(false); + + @Override + public void reply(ByteBuffer reply) { + if (done.getAndSet(true)) { + throw new IllegalStateException("Reply already submitted"); + } + if (reply == null) { + flutterJNI.invokePlatformMessageEmptyResponseCallback(replyId); + } else { + flutterJNI.invokePlatformMessageResponseCallback(replyId, reply, reply.position()); + } + } + }); + } catch (Exception ex) { + Log.e(TAG, "Uncaught exception in binary message listener", ex); + flutterJNI.invokePlatformMessageEmptyResponseCallback(replyId); + } + return; + } + flutterJNI.invokePlatformMessageEmptyResponseCallback(replyId); + } + + @Override + public void handlePlatformMessageResponse(int replyId, byte[] reply) { + BinaryMessenger.BinaryReply callback = mPendingReplies.remove(replyId); + if (callback != null) { + try { + callback.reply(reply == null ? null : ByteBuffer.wrap(reply)); + } catch (Exception ex) { + Log.e(TAG, "Uncaught exception in binary message reply handler", ex); + } + } + } +} \ No newline at end of file diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/SettingsChannel.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/SettingsChannel.java new file mode 100644 index 0000000000..612fe8fe49 --- /dev/null +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/SettingsChannel.java @@ -0,0 +1,68 @@ +package io.flutter.embedding.engine.systemchannels; + +import android.support.annotation.NonNull; +import io.flutter.embedding.engine.dart.DartExecutor; +import io.flutter.plugin.common.BasicMessageChannel; +import io.flutter.plugin.common.JSONMessageCodec; + +import java.util.HashMap; +import java.util.Map; + +public class SettingsChannel { + public static final String CHANNEL_NAME = "flutter/settings"; + + public final BasicMessageChannel channel; + + public SettingsChannel(@NonNull DartExecutor dartExecutor) { + this.channel = new BasicMessageChannel<>(dartExecutor, CHANNEL_NAME, JSONMessageCodec.INSTANCE); + } + + public MessageBuilder startMessage() { + return new MessageBuilder(channel); + } + + public static class MessageBuilder { + private final BasicMessageChannel channel; + private Map message = new HashMap<>(); + + MessageBuilder(@NonNull BasicMessageChannel channel) { + this.channel = channel; + } + + public MessageBuilder setTextScaleFactor(float textScaleFactor) { + message.put("textScaleFactor", textScaleFactor); + return this; + } + + public MessageBuilder setUse24HourFormat(boolean use24HourFormat) { + message.put("alwaysUse24HourFormat", use24HourFormat); + return this; + } + + public MessageBuilder setPlatformBrightness(@NonNull PlatformBrightness brightness) { + message.put("platformBrightness", brightness.name); + return this; + } + + public void send() { + channel.send(message); + } + } + + /** + * The brightness mode of the host platform. + * + * The {@code name} property is the serialized representation of each + * brightness mode when communicated via message channel. + */ + public enum PlatformBrightness { + light("light"), + dark("dark"); + + public String name; + + PlatformBrightness(@NonNull String name) { + this.name = name; + } + } +} \ No newline at end of file diff --git a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java index a5aa0d532a..e7d8c74ac6 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java @@ -26,6 +26,8 @@ import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputMethodManager; import io.flutter.app.FlutterPluginRegistry; +import io.flutter.embedding.engine.dart.DartExecutor; +import io.flutter.embedding.engine.systemchannels.SettingsChannel; import io.flutter.plugin.common.*; import io.flutter.plugin.editing.TextInputPlugin; import io.flutter.plugin.platform.PlatformPlugin; @@ -78,6 +80,7 @@ public class FlutterView extends SurfaceView int physicalViewInsetLeft = 0; } + private final DartExecutor dartExecutor; private final InputMethodManager mImm; private final TextInputPlugin mTextInputPlugin; private final SurfaceHolder.Callback mSurfaceCallback; @@ -88,7 +91,7 @@ public class FlutterView extends SurfaceView private final BasicMessageChannel mFlutterKeyEventChannel; private final BasicMessageChannel mFlutterLifecycleChannel; private final BasicMessageChannel mFlutterSystemChannel; - private final BasicMessageChannel mFlutterSettingsChannel; + private final SettingsChannel settingsChannel; private final List mActivityLifecycleListeners; private final List mFirstFrameListeners; private final AtomicLong nextTextureId = new AtomicLong(0L); @@ -114,6 +117,7 @@ public class FlutterView extends SurfaceView } else { mNativeView = nativeView; } + dartExecutor = new DartExecutor(mNativeView.getFlutterJNI()); mIsSoftwareRenderingEnabled = mNativeView.getFlutterJNI().nativeGetIsSoftwareRenderingEnabled(); mAnimationScaleObserver = new AnimationScaleObserver(new Handler()); mMetrics = new ViewportMetrics(); @@ -155,7 +159,7 @@ public class FlutterView extends SurfaceView mFlutterKeyEventChannel = new BasicMessageChannel<>(this, "flutter/keyevent", JSONMessageCodec.INSTANCE); mFlutterLifecycleChannel = new BasicMessageChannel<>(this, "flutter/lifecycle", StringCodec.INSTANCE); mFlutterSystemChannel = new BasicMessageChannel<>(this, "flutter/system", JSONMessageCodec.INSTANCE); - mFlutterSettingsChannel = new BasicMessageChannel<>(this, "flutter/settings", JSONMessageCodec.INSTANCE); + settingsChannel = new SettingsChannel(dartExecutor); PlatformPlugin platformPlugin = new PlatformPlugin(activity); MethodChannel flutterPlatformChannel = new MethodChannel(this, "flutter/platform", JSONMethodCodec.INSTANCE); @@ -166,7 +170,7 @@ public class FlutterView extends SurfaceView setLocales(getResources().getConfiguration()); - setUserSettings(); + sendUserPlatformSettingsToDart(); } private void encodeKeyEvent(KeyEvent event, Map message) { @@ -304,11 +308,19 @@ public class FlutterView extends SurfaceView mFlutterNavigationChannel.invokeMethod("popRoute", null); } - private void setUserSettings() { - Map message = new HashMap<>(); - message.put("textScaleFactor", getResources().getConfiguration().fontScale); - message.put("alwaysUse24HourFormat", DateFormat.is24HourFormat(getContext())); - mFlutterSettingsChannel.send(message); + private void sendUserPlatformSettingsToDart() { + // Lookup the current brightness of the Android OS. + boolean isNightModeOn = (getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES; + SettingsChannel.PlatformBrightness brightness = isNightModeOn + ? SettingsChannel.PlatformBrightness.dark + : SettingsChannel.PlatformBrightness.light; + + settingsChannel + .startMessage() + .setTextScaleFactor(getResources().getConfiguration().fontScale) + .setUse24HourFormat(DateFormat.is24HourFormat(getContext())) + .setPlatformBrightness(brightness) + .send(); } private void setLocales(Configuration config) { @@ -344,7 +356,7 @@ public class FlutterView extends SurfaceView protected void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); setLocales(newConfig); - setUserSettings(); + sendUserPlatformSettingsToDart(); } float getDevicePixelRatio() { diff --git a/engine/src/flutter/testing/dart/window_hooks_integration_test.dart b/engine/src/flutter/testing/dart/window_hooks_integration_test.dart index a7a8b81a9a..3925594fd3 100644 --- a/engine/src/flutter/testing/dart/window_hooks_integration_test.dart +++ b/engine/src/flutter/testing/dart/window_hooks_integration_test.dart @@ -246,5 +246,25 @@ void main() { expect(runZone, same(innerZone)); expect(textScaleFactor, equals(0.5)); }); + + test('onThemeBrightnessMode preserves callback zone', () { + Zone innerZone; + Zone runZone; + Brightness platformBrightness; + + runZoned(() { + innerZone = Zone.current; + window.onPlatformBrightnessChanged = () { + runZone = Zone.current; + platformBrightness = window.platformBrightness; + }; + }); + + window.onPlatformBrightnessChanged(); + _updatePlatformBrightness('dark'); + expect(runZone, isNotNull); + expect(runZone, same(innerZone)); + expect(platformBrightness, equals(Brightness.dark)); + }); }); }