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 57b25707bf..cf71acf0c2 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 @@ -60,24 +60,58 @@ import static android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW; public class FlutterFragment extends Fragment { private static final String TAG = "FlutterFragment"; - private static final String ARG_DART_ENTRYPOINT = "dart_entrypoint"; - private static final String ARG_INITIAL_ROUTE = "initial_route"; - private static final String ARG_APP_BUNDLE_PATH = "app_bundle_path"; - private static final String ARG_FLUTTER_INITIALIZATION_ARGS = "initialization_args"; - private static final String ARG_FLUTTERVIEW_RENDER_MODE = "flutterview_render_mode"; - private static final String ARG_FLUTTERVIEW_TRANSPARENCY_MODE = "flutterview_transparency_mode"; + protected static final String ARG_DART_ENTRYPOINT = "dart_entrypoint"; + protected static final String ARG_INITIAL_ROUTE = "initial_route"; + protected static final String ARG_APP_BUNDLE_PATH = "app_bundle_path"; + protected static final String ARG_FLUTTER_INITIALIZATION_ARGS = "initialization_args"; + protected static final String ARG_FLUTTERVIEW_RENDER_MODE = "flutterview_render_mode"; + protected static final String ARG_FLUTTERVIEW_TRANSPARENCY_MODE = "flutterview_transparency_mode"; /** * Builder that creates a new {@code FlutterFragment} with {@code arguments} that correspond * to the values set on this {@code Builder}. *
* To create a {@code FlutterFragment} with default {@code arguments}, invoke {@code build()} - * immeidately: + * without setting any builder properties: * {@code * FlutterFragment fragment = new FlutterFragment.Builder().build(); * } + *
+ * Subclasses of {@code FlutterFragment} that do not introduce any new arguments can use this
+ * {@code Builder} to construct instances of the subclass without subclassing this {@code Builder}.
+ * {@code
+ * MyFlutterFragment f = new FlutterFragment.Builder(MyFlutterFragment.class)
+ * .someProperty(...)
+ * .someOtherProperty(...)
+ * .build
+ * Subclasses of {@code FlutterFragment} that introduce new arguments should subclass this
+ * {@code Builder} to add the new properties:
+ *
+ * Subclasses should override this method to add new properties to the {@link Bundle}. Subclasses
+ * must call through to the super method to collect all existing property values.
+ */
@NonNull
- public FlutterFragment build() {
- FlutterFragment frag = new FlutterFragment();
-
- Bundle args = createArgsBundle(
- dartEntrypoint,
- initialRoute,
- appBundlePath,
- shellArgs,
- renderMode,
- transparencyMode
- );
- frag.setArguments(args);
-
- return frag;
+ protected Bundle createArgs() {
+ Bundle args = new Bundle();
+ args.putString(ARG_INITIAL_ROUTE, initialRoute);
+ args.putString(ARG_APP_BUNDLE_PATH, appBundlePath);
+ args.putString(ARG_DART_ENTRYPOINT, dartEntrypoint);
+ // TODO(mattcarroll): determine if we should have an explicit FlutterTestFragment instead of conflating.
+ if (null != shellArgs) {
+ args.putStringArray(ARG_FLUTTER_INITIALIZATION_ARGS, shellArgs.toArray());
+ }
+ args.putString(ARG_FLUTTERVIEW_RENDER_MODE, renderMode != null ? renderMode.name() : FlutterView.RenderMode.surface.name());
+ args.putString(ARG_FLUTTERVIEW_TRANSPARENCY_MODE, transparencyMode != null ? transparencyMode.name() : FlutterView.TransparencyMode.transparent.name());
+ return args;
}
- }
- /**
- * Creates a {@link Bundle} of arguments that can be used to configure a {@link FlutterFragment}.
- * This method is exposed so that developers can create subclasses of {@link FlutterFragment}.
- * Subclasses should declare static factories that use this method to create arguments that will
- * be understood by the base class, and then the subclass can add any additional arguments it
- * wants to this {@link Bundle}. Example:
- *
- * First, the {@link FragmentActivity} that owns this {@code FlutterFragment} is
+ * First, {@code FlutterFragment} subclasses are given an opportunity to provide a
+ * {@link FlutterEngine} by overriding {@link #createFlutterEngine(Context)}.
+ *
+ * Second, the {@link FragmentActivity} that owns this {@code FlutterFragment} is
* given the opportunity to provide a {@link FlutterEngine} as a {@link FlutterEngineProvider}.
*
- * If the owning {@link FragmentActivity} does not implement {@link FlutterEngineProvider}, or
- * chooses to return {@code null}, then a new {@link FlutterEngine} is instantiated. Subclasses
- * may override this method to provide a {@link FlutterEngine} of choice.
+ * If subclasses do not provide a {@link FlutterEngine}, and the owning {@link FragmentActivity}
+ * does not implement {@link FlutterEngineProvider} or chooses to return {@code null}, then a new
+ * {@link FlutterEngine} is instantiated.
*/
- protected void setupFlutterEngine() {
- // First, defer to the FragmentActivity that owns us to see if it wants to provide a
+ private void setupFlutterEngine() {
+ // First, defer to subclasses for a custom FlutterEngine.
+ flutterEngine = createFlutterEngine(getContextCompat());
+ if (flutterEngine != null) {
+ return;
+ }
+
+ // Second, defer to the FragmentActivity that owns us to see if it wants to provide a
// FlutterEngine.
FragmentActivity attachedActivity = getActivity();
if (attachedActivity instanceof FlutterEngineProvider) {
@@ -315,18 +344,33 @@ public class FlutterFragment extends Fragment {
if (flutterEngine != null) {
isFlutterEngineFromActivity = true;
}
+ return;
}
- // If flutterEngine is null then either our Activity is not a FlutterEngineProvider,
- // or our Activity decided that it didn't want to provide a FlutterEngine. Either way,
- // we will now create a FlutterEngine for this FlutterFragment.
- if (flutterEngine == null) {
- // Create a FlutterEngine to back our FlutterView.
- Log.d(TAG, "Our attached Activity did not want to provide a FlutterEngine. Creating a "
- + "new FlutterEngine for this FlutterFragment.");
- flutterEngine = new FlutterEngine(getContext());
- isFlutterEngineFromActivity = false;
- }
+ // Neither our subclass, nor our owning Activity wanted to provide a custom FlutterEngine.
+ // Create a FlutterEngine to back our FlutterView.
+ Log.d(TAG, "No subclass or our attached Activity provided a custom FlutterEngine. Creating a "
+ + "new FlutterEngine for this FlutterFragment.");
+ flutterEngine = new FlutterEngine(getContext());
+ isFlutterEngineFromActivity = false;
+ }
+
+ /**
+ * Hook for subclasses to return a {@link FlutterEngine} with whatever configuration
+ * is desired.
+ *
+ * This method takes precedence for creation of a {@link FlutterEngine} over any owning
+ * {@code Activity} that may implement {@link FlutterEngineProvider}.
+ *
+ * Consider returning a cached {@link FlutterEngine} instance from this method to avoid the
+ * typical warm-up time that a new {@link FlutterEngine} instance requires.
+ *
+ * If null is returned then a new default {@link FlutterEngine} will be created to back this
+ * {@code FlutterFragment}.
+ */
+ @Nullable
+ protected FlutterEngine createFlutterEngine(@NonNull Context context) {
+ return null;
}
@Nullable
+ *
+ * Once a {@code Builder} subclass is defined, the {@code FlutterFragment} subclass can be
+ * instantiated as follows.
+ * {@code
+ * MyFlutterFragment f = new MyBuilder()
+ * .someExistingProperty(...)
+ * .someNewProperty(...)
+ * .build{@code
- * public static MyFlutterFragment newInstance(String myNewArg) {
- * // Create an instance of your subclass Fragment.
- * MyFlutterFragment myFrag = new MyFlutterFragment();
- *
- * // Create the Bundle or args that FlutterFragment understands.
- * Bundle args = FlutterFragment.createArgsBundle(...);
- *
- * // Add your new args to the bundle.
- * args.putString(ARG_MY_NEW_ARG, myNewArg);
- *
- * // Give the args to your subclass Fragment.
- * myFrag.setArguments(args);
- *
- * // Return the newly created subclass Fragment.
- * return myFrag;
- * }
- * }
- *
- * @param dartEntrypoint the name of the initial Dart method to invoke, defaults to "main"
- * @param initialRoute the first route that a Flutter app will render in this {@link FlutterFragment}, defaults to "/"
- * @param appBundlePath the path to the app bundle which contains the Dart app to execute
- * @param flutterShellArgs any special configuration arguments for the Flutter engine
- * @param renderMode render Flutter either as a {@link FlutterView.RenderMode#surface} or a
- * {@link FlutterView.RenderMode#texture}. You should use {@code surface} unless
- * you have a specific reason to use {@code texture}. {@code texture} comes with
- * a significant performance impact, but {@code texture} can be displayed
- * beneath other Android {@code View}s and animated, whereas {@code surface}
- * cannot.
- *
- * @return Bundle of arguments that configure a {@link FlutterFragment}
- */
- protected static Bundle createArgsBundle(@Nullable String dartEntrypoint,
- @Nullable String initialRoute,
- @Nullable String appBundlePath,
- @Nullable FlutterShellArgs flutterShellArgs,
- @Nullable FlutterView.RenderMode renderMode,
- @Nullable FlutterView.TransparencyMode transparencyMode) {
- Bundle args = new Bundle();
- args.putString(ARG_INITIAL_ROUTE, initialRoute);
- args.putString(ARG_APP_BUNDLE_PATH, appBundlePath);
- args.putString(ARG_DART_ENTRYPOINT, dartEntrypoint);
- // TODO(mattcarroll): determine if we should have an explicit FlutterTestFragment instead of conflating.
- if (null != flutterShellArgs) {
- args.putStringArray(ARG_FLUTTER_INITIALIZATION_ARGS, flutterShellArgs.toArray());
+ /**
+ * Constructs a new {@code FlutterFragment} (or a subclass) that is configured based on
+ * properties set on this {@code Builder}.
+ */
+ @NonNull
+ public