diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 45b8e7d63b..155a0f8612 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -550,6 +550,9 @@ 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/plugins/FlutterPlugin.java FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/plugins/PluginRegistry.java +FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/plugins/activity/ActivityAware.java +FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/plugins/activity/ActivityControlSurface.java +FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/plugins/activity/ActivityPluginBinding.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/AccessibilityChannel.java diff --git a/engine/src/flutter/shell/platform/android/BUILD.gn b/engine/src/flutter/shell/platform/android/BUILD.gn index 679a5b7076..a0bc69e7df 100644 --- a/engine/src/flutter/shell/platform/android/BUILD.gn +++ b/engine/src/flutter/shell/platform/android/BUILD.gn @@ -135,6 +135,9 @@ action("flutter_shell_java") { "io/flutter/embedding/engine/dart/PlatformMessageHandler.java", "io/flutter/embedding/engine/plugins/FlutterPlugin.java", "io/flutter/embedding/engine/plugins/PluginRegistry.java", + "io/flutter/embedding/engine/plugins/activity/ActivityAware.java", + "io/flutter/embedding/engine/plugins/activity/ActivityControlSurface.java", + "io/flutter/embedding/engine/plugins/activity/ActivityPluginBinding.java", "io/flutter/embedding/engine/renderer/FlutterRenderer.java", "io/flutter/embedding/engine/renderer/OnFirstFrameRenderedListener.java", "io/flutter/embedding/engine/systemchannels/AccessibilityChannel.java", diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterFragment.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterFragment.java index ef13efb4bc..9dc152917d 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterFragment.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterFragment.java @@ -302,6 +302,11 @@ public class FlutterFragment extends Fragment { // control of the entire window. This is unacceptable for non-fullscreen // use-cases. platformPlugin = new PlatformPlugin(getActivity(), flutterEngine.getPlatformChannel()); + + // Notify any plugins that are currently attached to our FlutterEngine that they + // are now attached to an Activity. + // TODO(mattcarroll): send in a real lifecycle. + flutterEngine.getActivityControlSurface().attachToActivity(getActivity(), null); } private void initializeFlutter(@NonNull Context context) { @@ -538,6 +543,10 @@ public class FlutterFragment extends Fragment { super.onDetach(); Log.d(TAG, "onDetach()"); + // Notify plugins that they are no longer attached to an Activity. + // TODO(mattcarroll): differentiate between detaching for config changes and otherwise. + flutterEngine.getActivityControlSurface().detachFromActivity(); + // Null out the platformPlugin to avoid a possible retain cycle between the plugin, this Fragment, // and this Fragment's Activity. platformPlugin = null; @@ -587,6 +596,11 @@ public class FlutterFragment extends Fragment { * @param grantResults permission grants or denials */ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + if (flutterEngine != null) { + flutterEngine.getActivityControlSurface().onRequestPermissionsResult(requestCode, permissions, grantResults); + } else { + Log.w(TAG, "onRequestPermissionResult() invoked before FlutterFragment was attached to an Activity."); + } } /** @@ -598,6 +612,11 @@ public class FlutterFragment extends Fragment { * @param intent new Intent */ public void onNewIntent(@NonNull Intent intent) { + if (flutterEngine != null) { + flutterEngine.getActivityControlSurface().onNewIntent(intent); + } else { + Log.w(TAG, "onNewIntent() invoked before FlutterFragment was attached to an Activity."); + } } /** @@ -609,6 +628,11 @@ public class FlutterFragment extends Fragment { */ @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { + if (flutterEngine != null) { + flutterEngine.getActivityControlSurface().onActivityResult(requestCode, resultCode, data); + } else { + Log.w(TAG, "onActivityResult() invoked before FlutterFragment was attached to an Activity."); + } } /** @@ -618,6 +642,11 @@ public class FlutterFragment extends Fragment { * See {@link android.app.Activity#onUserLeaveHint()} */ public void onUserLeaveHint() { + if (flutterEngine != null) { + flutterEngine.getActivityControlSurface().onUserLeaveHint(); + } else { + Log.w(TAG, "onUserLeaveHint() invoked before FlutterFragment was attached to an Activity."); + } } /** diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngine.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngine.java index 3232df1274..6bc704f605 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngine.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngine.java @@ -4,17 +4,25 @@ package io.flutter.embedding.engine; +import android.app.Activity; import android.arch.lifecycle.Lifecycle; import android.content.Context; +import android.content.Intent; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.util.Log; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; import java.util.Set; import io.flutter.embedding.engine.dart.DartExecutor; import io.flutter.embedding.engine.plugins.FlutterPlugin; import io.flutter.embedding.engine.plugins.PluginRegistry; +import io.flutter.embedding.engine.plugins.activity.ActivityAware; +import io.flutter.embedding.engine.plugins.activity.ActivityControlSurface; +import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding; import io.flutter.embedding.engine.renderer.FlutterRenderer; import io.flutter.embedding.engine.systemchannels.AccessibilityChannel; import io.flutter.embedding.engine.systemchannels.KeyEventChannel; @@ -60,7 +68,7 @@ public class FlutterEngine { @NonNull private final DartExecutor dartExecutor; @NonNull - private final PluginRegistry pluginRegistry; + private final FlutterEnginePluginRegistry pluginRegistry; // System channels. @NonNull @@ -270,10 +278,19 @@ public class FlutterEngine { return pluginRegistry; } - private static class FlutterEnginePluginRegistry implements PluginRegistry { + @NonNull + public ActivityControlSurface getActivityControlSurface() { + return pluginRegistry; + } + + private static class FlutterEnginePluginRegistry implements PluginRegistry, ActivityControlSurface { private final Map, FlutterPlugin> plugins = new HashMap<>(); private final FlutterPlugin.FlutterPluginBinding pluginBinding; + private final Map, ActivityAware> activityAwarePlugins = new HashMap<>(); + private Activity activity; + private FlutterEngineActivityPluginBinding activityPluginBinding; + FlutterEnginePluginRegistry( @NonNull Context appContext, @NonNull FlutterEngine flutterEngine, @@ -287,8 +304,26 @@ public class FlutterEngine { } public void add(@NonNull FlutterPlugin plugin) { + // Add the plugin to our generic set of plugins and notify the plugin + // that is has been attached to an engine. plugins.put(plugin.getClass(), plugin); plugin.onAttachedToEngine(pluginBinding); + + // For ActivityAware plugins, add the plugin to our set of ActivityAware + // plugins, and if this engine is currently attached to an Activity, + // notify the ActivityAware plugin that it is now attached to an Activity. + if (plugin instanceof ActivityAware) { + ActivityAware activityAware = (ActivityAware) plugin; + activityAwarePlugins.put(plugin.getClass(), activityAware); + + if (isAttachedToActivity()) { + activityAware.onAttachedToActivity(activityPluginBinding); + } + } + + // TODO(mattcarroll): ServiceAware + // TODO(mattcarroll): BroadcastReceiverAware + // TODO(mattcarroll): ContentProviderAware } public void add(@NonNull Set plugins) { @@ -308,6 +343,23 @@ public class FlutterEngine { public void remove(@NonNull Class pluginClass) { FlutterPlugin plugin = plugins.get(pluginClass); if (plugin != null) { + // For ActivityAware plugins, notify the plugin that it is detached from + // an Activity if an Activity is currently attached to this engine. Then + // remove the plugin from our set of ActivityAware plugins. + if (plugin instanceof ActivityAware) { + if (isAttachedToActivity()) { + ActivityAware activityAware = (ActivityAware) plugin; + activityAware.onDetachedFromActivity(); + } + activityAwarePlugins.remove(pluginClass); + } + + // TODO(mattcarroll): ServiceAware + // TODO(mattcarroll): BroadcastReceiverAware + // TODO(mattcarroll): ContentProviderAware + + // Notify the plugin that is now detached from this engine. Then remove + // it from our set of generic plugins. plugin.onDetachedFromEngine(pluginBinding); plugins.remove(pluginClass); } @@ -320,11 +372,250 @@ public class FlutterEngine { } public void removeAll() { - for (FlutterPlugin plugin : plugins.values()) { - plugin.onDetachedFromEngine(pluginBinding); - } + // We copy the keys to a new set so that we can mutate the set while using + // the keys. + remove(new HashSet<>(plugins.keySet())); plugins.clear(); } + + //-------- Start ActivityControlSurface ------- + boolean isAttachedToActivity() { + return activity != null; + } + + @Override + public void attachToActivity(@NonNull Activity activity, @NonNull Lifecycle lifecycle) { + Log.d(TAG, "Attaching to an Activity."); + // If we were already attached to an Activity, detach from it before attaching to + // the new Activity. + if (isAttachedToActivity()) { + for (ActivityAware activityAware : activityAwarePlugins.values()) { + activityAware.onDetachedFromActivity(); + } + } + + this.activity = activity; + this.activityPluginBinding = new FlutterEngineActivityPluginBinding(activity); + // TODO(mattcarroll): resolve possibility of different lifecycles between this and engine attachment + + // Notify all ActivityAware plugins that they are now attached to a new Activity. + for (ActivityAware activityAware : activityAwarePlugins.values()) { + activityAware.onAttachedToActivity(activityPluginBinding); + } + } + + @Override + public void detachFromActivityForConfigChanges() { + Log.d(TAG, "Detaching from an Activity for config changes."); + if (isAttachedToActivity()) { + for (ActivityAware activityAware : activityAwarePlugins.values()) { + activityAware.onDetachedFromActivityForConfigChanges(); + } + + activity = null; + activityPluginBinding = null; + } else { + Log.e(TAG, "Attempted to detach plugins from an Activity when no Activity was attached."); + } + } + + @Override + public void reattachToActivityAfterConfigChange(@NonNull Activity activity) { + Log.d(TAG, "Re-attaching to an Activity after config change."); + if (!isAttachedToActivity()) { + this.activity = activity; + activityPluginBinding = new FlutterEngineActivityPluginBinding(activity); + + for (ActivityAware activityAware : activityAwarePlugins.values()) { + activityAware.onReattachedToActivityForConfigChanges(activityPluginBinding); + } + } else { + Log.e(TAG, "Attempted to reattach plugins to an Activity after config changes, but an Activity was already attached."); + } + } + + @Override + public void detachFromActivity() { + Log.d(TAG, "Detaching from an Activity."); + if (isAttachedToActivity()) { + for (ActivityAware activityAware : activityAwarePlugins.values()) { + activityAware.onDetachedFromActivity(); + } + + activity = null; + activityPluginBinding = null; + } else { + Log.e(TAG, "Attempted to detach plugins from an Activity when no Activity was attached."); + } + } + + @Override + public boolean onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResult) { + Log.d(TAG, "Forwarding onRequestPermissionsResult() to plugins."); + if (isAttachedToActivity()) { + return activityPluginBinding.onRequestPermissionsResult(requestCode, permissions, grantResult); + } else { + Log.e(TAG, "Attempted to notify ActivityAware plugins of onRequestPermissionsResult, but no Activity was attached."); + return false; + } + } + + @Override + public boolean onActivityResult(int requestCode, int resultCode, @NonNull Intent data) { + Log.d(TAG, "Forwarding onActivityResult() to plugins."); + if (isAttachedToActivity()) { + return activityPluginBinding.onActivityResult(requestCode, resultCode, data); + } else { + Log.e(TAG, "Attempted to notify ActivityAware plugins of onActivityResult, but no Activity was attached."); + return false; + } + } + + @Override + public void onNewIntent(@NonNull Intent intent) { + Log.d(TAG, "Forwarding onNewIntent() to plugins."); + if (isAttachedToActivity()) { + activityPluginBinding.onNewIntent(intent); + } else { + Log.e(TAG, "Attempted to notify ActivityAware plugins of onNewIntent, but no Activity was attached."); + } + } + + @Override + public void onUserLeaveHint() { + Log.d(TAG, "Forwarding onUserLeaveHint() to plugins."); + if (isAttachedToActivity()) { + activityPluginBinding.onUserLeaveHint(); + } else { + Log.e(TAG, "Attempted to notify ActivityAware plugins of onUserLeaveHint, but no Activity was attached."); + } + } + //------- End ActivityControlSurface ----- + } + + private static class FlutterEngineActivityPluginBinding implements ActivityPluginBinding { + private final Activity activity; + private final Set onRequestPermissionsResultListeners = new HashSet<>(); + private final Set onActivityResultListeners = new HashSet<>(); + private final Set onNewIntentListeners = new HashSet<>(); + private final Set onUserLeaveHintListeners = new HashSet<>(); + + public FlutterEngineActivityPluginBinding(@NonNull Activity activity) { + this.activity = activity; + } + + /** + * Returns the {@link Activity} that is currently attached to the {@link FlutterEngine} that + * owns this {@code ActivityPluginBinding}. + */ + @NonNull + public Activity getActivity() { + return activity; + } + + /** + * Adds a listener that is invoked whenever the associated {@link Activity}'s + * {@code onRequestPermissionsResult(...)} method is invoked. + */ + public void addRequestPermissionsResultListener(@NonNull io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener listener) { + onRequestPermissionsResultListeners.add(listener); + } + + /** + * Removes a listener that was added in {@link #addRequestPermissionsResultListener(io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener)}. + */ + public void removeRequestPermissionsResultListener(@NonNull io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener listener) { + onRequestPermissionsResultListeners.remove(listener); + } + + /** + * Invoked by the {@link FlutterEngine} that owns this {@code ActivityPluginBinding} when its + * associated {@link Activity} has its {@code onRequestPermissionsResult(...)} method invoked. + */ + boolean onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResult) { + boolean didConsumeResult = false; + for (io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener listener : onRequestPermissionsResultListeners) { + didConsumeResult = listener.onRequestPermissionsResult(requestCode, permissions, grantResult) || didConsumeResult; + } + return didConsumeResult; + } + + /** + * Adds a listener that is invoked whenever the associated {@link Activity}'s + * {@code onActivityResult(...)} method is invoked. + */ + public void addActivityResultListener(@NonNull io.flutter.plugin.common.PluginRegistry.ActivityResultListener listener) { + onActivityResultListeners.add(listener); + } + + /** + * Removes a listener that was added in {@link #addActivityResultListener(io.flutter.plugin.common.PluginRegistry.ActivityResultListener)}. + */ + public void removeActivityResultListener(@NonNull io.flutter.plugin.common.PluginRegistry.ActivityResultListener listener) { + onActivityResultListeners.remove(listener); + } + + /** + * Invoked by the {@link FlutterEngine} that owns this {@code ActivityPluginBinding} when its + * associated {@link Activity} has its {@code onActivityResult(...)} method invoked. + */ + boolean onActivityResult(int requestCode, int resultCode, @NonNull Intent data) { + boolean didConsumeResult = false; + for (io.flutter.plugin.common.PluginRegistry.ActivityResultListener listener : onActivityResultListeners) { + didConsumeResult = listener.onActivityResult(requestCode, resultCode, data) || didConsumeResult; + } + return didConsumeResult; + } + + /** + * Adds a listener that is invoked whenever the associated {@link Activity}'s + * {@code onNewIntent(...)} method is invoked. + */ + public void addOnNewIntentListener(@NonNull io.flutter.plugin.common.PluginRegistry.NewIntentListener listener) { + onNewIntentListeners.add(listener); + } + + /** + * Removes a listener that was added in {@link #addOnNewIntentListener(io.flutter.plugin.common.PluginRegistry.NewIntentListener)}. + */ + public void removeOnNewIntentListener(@NonNull io.flutter.plugin.common.PluginRegistry.NewIntentListener listener) { + onNewIntentListeners.remove(listener); + } + + /** + * Invoked by the {@link FlutterEngine} that owns this {@code ActivityPluginBinding} when its + * associated {@link Activity} has its {@code onNewIntent(...)} method invoked. + */ + void onNewIntent(@Nullable Intent intent) { + for (io.flutter.plugin.common.PluginRegistry.NewIntentListener listener : onNewIntentListeners) { + listener.onNewIntent(intent); + } + } + + /** + * Adds a listener that is invoked whenever the associated {@link Activity}'s + * {@code onUserLeaveHint()} method is invoked. + */ + public void addOnUserLeaveHintListener(@NonNull io.flutter.plugin.common.PluginRegistry.UserLeaveHintListener listener) { + onUserLeaveHintListeners.add(listener); + } + + /** + * Removes a listener that was added in {@link #addOnUserLeaveHintListener(io.flutter.plugin.common.PluginRegistry.UserLeaveHintListener)}. + */ + public void removeOnUserLeaveHintListener(@NonNull io.flutter.plugin.common.PluginRegistry.UserLeaveHintListener listener) { + onUserLeaveHintListeners.remove(listener); + } + + /** + * Invoked by the {@link FlutterEngine} that owns this {@code ActivityPluginBinding} when its + * associated {@link Activity} has its {@code onUserLeaveHint()} method invoked. + */ + void onUserLeaveHint() { + for (io.flutter.plugin.common.PluginRegistry.UserLeaveHintListener listener : onUserLeaveHintListeners) { + listener.onUserLeaveHint(); + } + } } /** diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/plugins/activity/ActivityAware.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/plugins/activity/ActivityAware.java new file mode 100644 index 0000000000..332a275c03 --- /dev/null +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/plugins/activity/ActivityAware.java @@ -0,0 +1,81 @@ +// 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.plugins.activity; + +import android.support.annotation.NonNull; + +/** + * {@link FlutterPlugin} that is interested in {@link Activity} lifecycle events related to a + * {@link FlutterEngine} running within the given {@link Activity}. + */ +public interface ActivityAware { + /** + * This {@code ActivityAware} {@link FlutterPlugin} is now associated with an {@link Activity}. + *

+ * This method can be invoked in 1 of 2 situations: + *

    + *
  • This {@code ActivityAware} {@link FlutterPlugin} was just added to a {@link FlutterEngine} + * that was already connected to a running {@link Activity}.
  • + *
  • This {@code ActivityAware} {@link FlutterPlugin} was already added to a + * {@link FlutterEngine} and that {@link FlutterEngine} was just connected to an + * {@link Activity}.
  • + *
+ * The given {@link ActivityPluginBinding} contains {@link Activity}-related references that an + * {@code ActivityAware} {@link FlutterPlugin} may require, such as a reference to the actual + * {@link Activity} in question. The {@link ActivityPluginBinding} may be referenced until either + * {@link #onDetachedFromActivityForConfigChanges()} or {@link #onDetachedFromActivity()} is + * invoked. At the conclusion of either of those methods, the binding is no longer valid. Clear + * any references to the binding or its resources, and do not invoke any further methods on the + * binding or its resources. + */ + void onAttachedToActivity(@NonNull ActivityPluginBinding binding); + + /** + * The {@link Activity} that was attached and made available in + * {@link #onAttachedToActivity(ActivityPluginBinding)} has been detached from this + * {@code ActivityAware}'s {@link FlutterEngine} for the purpose of processing a configuration + * change. + *

+ * By the end of this method, the {@link Activity} that was made available in + * {@link #onAttachedToActivity(ActivityPluginBinding)} is no longer valid. Any references to the + * associated {@link Activity} or {@link ActivityPluginBinding} should be cleared. + *

+ * This method should be quickly followed by + * {@link #onReattachedToActivityForConfigChanges(ActivityPluginBinding)}, which signifies that a + * new {@link Activity} has been created with the new configuration options. That method provides + * a new {@link ActivityPluginBinding}, which references the newly created and associated + * {@link Activity}. + */ + void onDetachedFromActivityForConfigChanges(); + + /** + * This plugin and its {@link FlutterEngine} have been re-attached to an {@link Activity} after + * the {@link Activity} was recreated to handle configuration changes. + *

+ * {@code binding} includes a reference to the new instance of the {@link Activity}. {@code binding} + * and its references may be cached and used from now until either + * {@link #onDetachedFromActivityForConfigChanges()} or {@link #onDetachedFromActivity()} is + * invoked. At the conclusion of either of those methods, the binding is no longer valid. Clear + * any references to the binding or its resources, and do not invoke any further methods on the + * binding or its resources. + */ + void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding); + + /** + * This plugin has been detached from an {@link Activity}. + *

+ * Detachment can occur for a number of reasons. + *

    + *
  • The app is no longer visible and the {@link Activity} instance has been destroyed.
  • + *
  • The {@link FlutterEngine} that this plugin is connected to has been detached from + * its {@link FlutterView}.
  • + *
  • This {@code ActivityAware} plugin has been removed from its {@link FlutterEngine}.
  • + *
+ * By the end of this method, the {@link Activity} that was made available in + * {@link #onAttachedToActivity(ActivityPluginBinding)} is no longer valid. Any references to the + * associated {@link Activity} or {@link ActivityPluginBinding} should be cleared. + */ + void onDetachedFromActivity(); +} diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/plugins/activity/ActivityControlSurface.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/plugins/activity/ActivityControlSurface.java new file mode 100644 index 0000000000..52e1ab3f44 --- /dev/null +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/plugins/activity/ActivityControlSurface.java @@ -0,0 +1,108 @@ +// 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.plugins.activity; + +import android.app.Activity; +import android.arch.lifecycle.Lifecycle; +import android.content.Intent; +import android.support.annotation.NonNull; + +/** + * Control surface through which an {@link Activity} attaches to a {@link FlutterEngine}. + *

+ * An {@link Activity} that contains a {@link FlutterView} and associated {@link FlutterEngine} + * should coordinate itself with the {@link FlutterEngine}'s {@code ActivityControlSurface}. + *

    + *
  1. Once an {@link Activity} is created, and its associated {@link FlutterEngine} is + * executing Dart code, the {@link Activity} should invoke + * {@link #attachToActivity(Activity, Lifecycle)}. At this point the {@link FlutterEngine} + * is considered "attached" to the {@link Activity} and all {@link ActivityAware} plugins + * are given access to the {@link Activity}.
  2. + *
  3. Just before an attached {@link Activity} is destroyed for configuration change purposes, + * that {@link Activity} should invoke {@link #detachFromActivityForConfigChanges()}, giving + * each {@link ActivityAware} plugin an opportunity to clean up its references before the + * {@link Activity is destroyed}.
  4. + *
  5. When an {@link Activity} is recreated after configuration changes, that {@link Activity} + * should invoke {@link #reattachToActivityAfterConfigChange(Activity)} so that all + * {@link ActivityAware} plugins can re-establish references to the {@link Activity}.
  6. + *
  7. When an {@link Activity} is destroyed for non-configuration-change purposes, or when + * the {@link Activity} is no longer interested in displaying a {@link FlutterEngine}'s content, + * the {@link Activity} should invoke {@link #detachFromActivity()}.
  8. + *
+ * The attached {@link Activity} should also forward all {@link Activity} calls that this + * {@code ActivityControlSurface} supports, e.g., + * {@link #onRequestPermissionsResult(int, String[], int[])}. These forwarded calls are made + * available to all {@link ActivityAware} plugins that are added to the attached {@link FlutterEngine}. + */ +public interface ActivityControlSurface { + /** + * Call this method from the {@link Activity} that is displaying the visual content of the + * {@link FlutterEngine} that is associated with this {@code ActivityControlSurface}. + *

+ * Once an {@link Activity} is created, and its associated {@link FlutterEngine} is + * executing Dart code, the {@link Activity} should invoke this method. At that point the + * {@link FlutterEngine} is considered "attached" to the {@link Activity} and all + * {@link ActivityAware} plugins are given access to the {@link Activity}. + */ + void attachToActivity(@NonNull Activity activity, @NonNull Lifecycle lifecycle); + + /** + * Call this method from the {@link Activity} that is attached to this {@code ActivityControlSurfaces}'s + * {@link FlutterEngine} when the {@link Activity} is about to be destroyed due to configuration + * changes. + *

+ * This method gives each {@link ActivityAware} plugin an opportunity to clean up its references + * before the {@link Activity is destroyed}. + */ + void detachFromActivityForConfigChanges(); + + /** + * Call this method from the {@link Activity} that was previously attached to this + * {@code ActivityControlSurface}'s {@link FlutterEngine}, and was just recreated after a + * configuration change. + *

+ * This method gives each {@link ActivityAware} plugin an opportunity to re-establish necessary + * references to the given {@link Activity}. + */ + void reattachToActivityAfterConfigChange(@NonNull Activity activity); + + /** + * Call this method from the {@link Activity} that is attached to this {@code ActivityControlSurfaces}'s + * {@link FlutterEngine} when the {@link Activity} is about to be destroyed for non-configuration-change + * reasons. + *

+ * This method gives each {@link ActivityAware} plugin an opportunity to clean up its references + * before the {@link Activity is destroyed}. + */ + void detachFromActivity(); + + /** + * Call this method from the {@link Activity} that is attached to this {@code ActivityControlSurface}'s + * {@link FlutterEngine} and the associated method in the {@link Activity} is invoked. + *

+ * Returns true if one or more plugins utilized this permission result. + */ + boolean onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResult); + + /** + * Call this method from the {@link Activity} that is attached to this {@code ActivityControlSurface}'s + * {@link FlutterEngine} and the associated method in the {@link Activity} is invoked. + *

+ * Returns true if one or more plugins utilized this {@link Activity} result. + */ + boolean onActivityResult(int requestCode, int resultCode, Intent data); + + /** + * Call this method from the {@link Activity} that is attached to this {@code ActivityControlSurface}'s + * {@link FlutterEngine} and the associated method in the {@link Activity} is invoked. + */ + void onNewIntent(@NonNull Intent intent); + + /** + * Call this method from the {@link Activity} that is attached to this {@code ActivityControlSurface}'s + * {@link FlutterEngine} and the associated method in the {@link Activity} is invoked. + */ + void onUserLeaveHint(); +} diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/plugins/activity/ActivityPluginBinding.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/plugins/activity/ActivityPluginBinding.java new file mode 100644 index 0000000000..9420c43c57 --- /dev/null +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/plugins/activity/ActivityPluginBinding.java @@ -0,0 +1,68 @@ +// 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.plugins.activity; + +import android.app.Activity; +import android.support.annotation.NonNull; + +import io.flutter.plugin.common.PluginRegistry; + +/** + * Binding that gives {@link ActivityAware} plugins access to an associated {@link Activity} and + * the {@link Activity}'s lifecycle methods. + */ +public interface ActivityPluginBinding { + + /** + * Returns the {@link Activity} that is currently attached to the {@link FlutterEngine} that + * owns this {@code ActivityPluginBinding}. + */ + @NonNull + Activity getActivity(); + + /** + * Adds a listener that is invoked whenever the associated {@link Activity}'s + * {@code onRequestPermissionsResult(...)} method is invoked. + */ + void addRequestPermissionsResultListener(@NonNull PluginRegistry.RequestPermissionsResultListener listener); + + /** + * Removes a listener that was added in {@link #addRequestPermissionsResultListener(PluginRegistry.RequestPermissionsResultListener)}. + */ + void removeRequestPermissionsResultListener(@NonNull PluginRegistry.RequestPermissionsResultListener listener); + + /** + * Adds a listener that is invoked whenever the associated {@link Activity}'s + * {@code onActivityResult(...)} method is invoked. + */ + void addActivityResultListener(@NonNull PluginRegistry.ActivityResultListener listener); + + /** + * Removes a listener that was added in {@link #addActivityResultListener(PluginRegistry.ActivityResultListener)}. + */ + void removeActivityResultListener(@NonNull PluginRegistry.ActivityResultListener listener); + + /** + * Adds a listener that is invoked whenever the associated {@link Activity}'s + * {@code onNewIntent(...)} method is invoked. + */ + void addOnNewIntentListener(@NonNull PluginRegistry.NewIntentListener listener); + + /** + * Removes a listener that was added in {@link #addOnNewIntentListener(PluginRegistry.NewIntentListener)}. + */ + void removeOnNewIntentListener(@NonNull PluginRegistry.NewIntentListener listener); + + /** + * Adds a listener that is invoked whenever the associated {@link Activity}'s + * {@code onUserLeaveHint()} method is invoked. + */ + void addOnUserLeaveHintListener(@NonNull PluginRegistry.UserLeaveHintListener listener); + + /** + * Removes a listener that was added in {@link #addOnUserLeaveHintListener(PluginRegistry.UserLeaveHintListener)}. + */ + void removeOnUserLeaveHintListener(@NonNull PluginRegistry.UserLeaveHintListener listener); +}