Flutter Engine
The Flutter Engine
FlutterInjector.java
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package io.flutter;
6
7import androidx.annotation.NonNull;
8import androidx.annotation.Nullable;
9import androidx.annotation.VisibleForTesting;
10import io.flutter.embedding.engine.FlutterJNI;
11import io.flutter.embedding.engine.deferredcomponents.DeferredComponentManager;
12import io.flutter.embedding.engine.loader.FlutterLoader;
13import java.util.concurrent.ExecutorService;
14import java.util.concurrent.Executors;
15import java.util.concurrent.ThreadFactory;
16
17/**
18 * This class is a simple dependency injector for the relatively thin Android part of the Flutter
19 * engine.
20 *
21 * <p>This simple solution is used facilitate testability without bringing in heavier
22 * app-development centric dependency injection frameworks such as Guice or Dagger2 or spreading
23 * construction injection everywhere.
24 */
25public final class FlutterInjector {
26
27 private static FlutterInjector instance;
28 private static boolean accessed;
29
30 /**
31 * Use {@link FlutterInjector.Builder} to specify members to be injected via the static {@code
32 * FlutterInjector}.
33 *
34 * <p>This can only be called at the beginning of the program before the {@link #instance()} is
35 * accessed.
36 */
37 public static void setInstance(@NonNull FlutterInjector injector) {
38 if (accessed) {
39 throw new IllegalStateException(
40 "Cannot change the FlutterInjector instance once it's been "
41 + "read. If you're trying to dependency inject, be sure to do so at the beginning of "
42 + "the program");
43 }
44 instance = injector;
45 }
46
47 /**
48 * Retrieve the static instance of the {@code FlutterInjector} to use in your program.
49 *
50 * <p>Once you access it, you can no longer change the values injected.
51 *
52 * <p>If no override is provided for the injector, reasonable defaults are provided.
53 */
54 public static FlutterInjector instance() {
55 accessed = true;
56 if (instance == null) {
57 instance = new Builder().build();
58 }
59 return instance;
60 }
61
62 // This whole class is here to enable testing so to test the thing that lets you test, some degree
63 // of hack is needed.
64 @VisibleForTesting
65 public static void reset() {
66 accessed = false;
67 instance = null;
68 }
69
70 private FlutterInjector(
71 @NonNull FlutterLoader flutterLoader,
72 @Nullable DeferredComponentManager deferredComponentManager,
73 @NonNull FlutterJNI.Factory flutterJniFactory,
74 @NonNull ExecutorService executorService) {
75 this.flutterLoader = flutterLoader;
76 this.deferredComponentManager = deferredComponentManager;
77 this.flutterJniFactory = flutterJniFactory;
78 this.executorService = executorService;
79 }
80
81 private FlutterLoader flutterLoader;
82 private DeferredComponentManager deferredComponentManager;
83 private FlutterJNI.Factory flutterJniFactory;
84 private ExecutorService executorService;
85 /**
86 * Returns the {@link io.flutter.embedding.engine.loader.FlutterLoader} instance to use for the
87 * Flutter Android engine embedding.
88 */
89 @NonNull
91 return flutterLoader;
92 }
93
94 /**
95 * Returns the {@link DeferredComponentManager} instance to use for the Flutter Android engine
96 * embedding.
97 */
98 @Nullable
100 return deferredComponentManager;
101 }
102
103 public ExecutorService executorService() {
104 return executorService;
105 }
106
107 @NonNull
108 public FlutterJNI.Factory getFlutterJNIFactory() {
109 return flutterJniFactory;
110 }
111
112 /**
113 * Builder used to supply a custom FlutterInjector instance to {@link
114 * FlutterInjector#setInstance(FlutterInjector)}.
115 *
116 * <p>Non-overridden values have reasonable defaults.
117 */
118 public static final class Builder {
119 private class NamedThreadFactory implements ThreadFactory {
120 private int threadId = 0;
121
122 public Thread newThread(Runnable command) {
123 Thread thread = new Thread(command);
124 thread.setName("flutter-worker-" + threadId++);
125 return thread;
126 }
127 }
128
129 private FlutterLoader flutterLoader;
130 private DeferredComponentManager deferredComponentManager;
131 private FlutterJNI.Factory flutterJniFactory;
132 private ExecutorService executorService;
133 /**
134 * Sets a {@link io.flutter.embedding.engine.loader.FlutterLoader} override.
135 *
136 * <p>A reasonable default will be used if unspecified.
137 */
138 public Builder setFlutterLoader(@NonNull FlutterLoader flutterLoader) {
139 this.flutterLoader = flutterLoader;
140 return this;
141 }
142
144 @Nullable DeferredComponentManager deferredComponentManager) {
145 this.deferredComponentManager = deferredComponentManager;
146 return this;
147 }
148
150 this.flutterJniFactory = factory;
151 return this;
152 }
153
154 public Builder setExecutorService(@NonNull ExecutorService executorService) {
155 this.executorService = executorService;
156 return this;
157 }
158
159 private void fillDefaults() {
160 if (flutterJniFactory == null) {
161 flutterJniFactory = new FlutterJNI.Factory();
162 }
163
164 if (executorService == null) {
165 executorService = Executors.newCachedThreadPool(new NamedThreadFactory());
166 }
167
168 if (flutterLoader == null) {
169 flutterLoader = new FlutterLoader(flutterJniFactory.provideFlutterJNI(), executorService);
170 }
171 // DeferredComponentManager's intended default is null.
172 }
173
174 /**
175 * Builds a {@link FlutterInjector} from the builder. Unspecified properties will have
176 * reasonable defaults.
177 */
179 fillDefaults();
180
181 return new FlutterInjector(
182 flutterLoader, deferredComponentManager, flutterJniFactory, executorService);
183 }
184 }
185}
std::shared_ptr< DlVertices > build()
Finalizes and the constructed DlVertices object.
Definition: dl_vertices.cc:279
Builder setFlutterLoader(@NonNull FlutterLoader flutterLoader)
Builder setFlutterJNIFactory(@NonNull FlutterJNI.Factory factory)
Builder setDeferredComponentManager( @Nullable DeferredComponentManager deferredComponentManager)
Builder setExecutorService(@NonNull ExecutorService executorService)
DeferredComponentManager deferredComponentManager()
FlutterJNI.Factory getFlutterJNIFactory()
ExecutorService executorService()
static void setInstance(@NonNull FlutterInjector injector)
static FlutterInjector instance()
VkInstance instance
Definition: main.cc:48
DlVertices::Builder Builder
list command
Definition: valgrind.py:24