Commit c1c82460 authored by yangwu.jia's avatar yangwu.jia

新增engine destroy 的方式,默认和app共存

parent 01ab95a8
...@@ -84,6 +84,13 @@ public class FlutterViewContainerManager implements IContainerManager { ...@@ -84,6 +84,13 @@ public class FlutterViewContainerManager implements IContainerManager {
void removeRecord(IContainerRecord record) { void removeRecord(IContainerRecord record) {
mRecordStack.remove(record); mRecordStack.remove(record);
mRecordMap.remove(record.getContainer()); mRecordMap.remove(record.getContainer());
if(mRecordStack.empty()){
if( NewFlutterBoost.instance().platform().whenEngineDestroy()== NewFlutterBoost.ConfigBuilder.All_FLUTTER_ACTIVITY_DESTROY){
NewFlutterBoost.instance().boostDestroy();
}
}
} }
void setContainerResult(IContainerRecord record,int requestCode, int resultCode, Map<String,Object> result) { void setContainerResult(IContainerRecord record,int requestCode, int resultCode, Map<String,Object> result) {
......
...@@ -10,6 +10,7 @@ import com.idlefish.flutterboost.interfaces.*; ...@@ -10,6 +10,7 @@ import com.idlefish.flutterboost.interfaces.*;
import io.flutter.Log; import io.flutter.Log;
import io.flutter.embedding.android.FlutterView; import io.flutter.embedding.android.FlutterView;
import io.flutter.embedding.engine.FlutterEngine; import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterShellArgs;
import io.flutter.embedding.engine.dart.DartExecutor; import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.embedding.engine.plugins.shim.ShimPluginRegistry; import io.flutter.embedding.engine.plugins.shim.ShimPluginRegistry;
import io.flutter.plugin.common.PluginRegistry; import io.flutter.plugin.common.PluginRegistry;
...@@ -23,7 +24,7 @@ public class NewFlutterBoost { ...@@ -23,7 +24,7 @@ public class NewFlutterBoost {
private Platform mPlatform; private Platform mPlatform;
private FlutterViewContainerManager mManager; private FlutterViewContainerManager mManager;
private FlutterEngine mEngine;
private Activity mCurrentActiveActivity; private Activity mCurrentActiveActivity;
private PluginRegistry mRegistry; private PluginRegistry mRegistry;
static NewFlutterBoost sInstance = null; static NewFlutterBoost sInstance = null;
...@@ -53,7 +54,7 @@ public class NewFlutterBoost { ...@@ -53,7 +54,7 @@ public class NewFlutterBoost {
if (mPlatform.whenEngineStart() == ConfigBuilder.ANY_ACTIVITY_CREATED) { if (mPlatform.whenEngineStart() == ConfigBuilder.ANY_ACTIVITY_CREATED) {
Log.e("bbbb2", "xxxxx"); Log.e("bbbb2", "xxxxx");
doInitialFlutter(mPlatform); doInitialFlutter();
} }
} }
...@@ -87,7 +88,7 @@ public class NewFlutterBoost { ...@@ -87,7 +88,7 @@ public class NewFlutterBoost {
if (mCurrentActiveActivity == activity) { if (mCurrentActiveActivity == activity) {
Debuger.log("Application entry background"); Debuger.log("Application entry background");
if (mPlatform.engineProvider() != null) { if (engineProvider() != null) {
HashMap<String, String> map = new HashMap<>(); HashMap<String, String> map = new HashMap<>();
map.put("type", "background"); map.put("type", "background");
channel().sendEvent("lifecycle", map); channel().sendEvent("lifecycle", map);
...@@ -106,7 +107,7 @@ public class NewFlutterBoost { ...@@ -106,7 +107,7 @@ public class NewFlutterBoost {
if (mCurrentActiveActivity == activity) { if (mCurrentActiveActivity == activity) {
Debuger.log("Application entry background"); Debuger.log("Application entry background");
if (mPlatform.engineProvider() != null) { if (engineProvider() != null) {
HashMap<String, String> map = new HashMap<>(); HashMap<String, String> map = new HashMap<>();
map.put("type", "background"); map.put("type", "background");
channel().sendEvent("lifecycle", map); channel().sendEvent("lifecycle", map);
...@@ -118,19 +119,19 @@ public class NewFlutterBoost { ...@@ -118,19 +119,19 @@ public class NewFlutterBoost {
if (mPlatform.whenEngineStart() == ConfigBuilder.IMMEDIATELY) { if (mPlatform.whenEngineStart() == ConfigBuilder.IMMEDIATELY) {
doInitialFlutter(mPlatform); doInitialFlutter();
} }
} }
private void doInitialFlutter(Platform platform ) { public void doInitialFlutter() {
if(platform.getEngine()!=null) return; if(mEngine!=null) return;
FlutterEngine flutterEngine = platform.engineProvider(); FlutterEngine flutterEngine = engineProvider();
if (flutterEngine.getDartExecutor().isExecutingDart()) { if (flutterEngine.getDartExecutor().isExecutingDart()) {
// No warning is logged because this situation will happen on every config // No warning is logged because this situation will happen on every config
...@@ -139,13 +140,12 @@ public class NewFlutterBoost { ...@@ -139,13 +140,12 @@ public class NewFlutterBoost {
return; return;
} }
mRegistry = new ShimPluginRegistry(mPlatform.engineProvider());
// The engine needs to receive the Flutter app's initial route before executing any // The engine needs to receive the Flutter app's initial route before executing any
// Dart code to ensure that the initial route arrives in time to be applied. // Dart code to ensure that the initial route arrives in time to be applied.
if (platform.initialRoute() != null) { if (mPlatform.initialRoute() != null) {
flutterEngine.getNavigationChannel().setInitialRoute(platform.initialRoute()); flutterEngine.getNavigationChannel().setInitialRoute(mPlatform.initialRoute());
} }
// Configure the Dart entrypoint and execute it. // Configure the Dart entrypoint and execute it.
DartExecutor.DartEntrypoint entrypoint = new DartExecutor.DartEntrypoint( DartExecutor.DartEntrypoint entrypoint = new DartExecutor.DartEntrypoint(
...@@ -153,6 +153,9 @@ public class NewFlutterBoost { ...@@ -153,6 +153,9 @@ public class NewFlutterBoost {
"main" "main"
); );
flutterEngine.getDartExecutor().executeDartEntrypoint(entrypoint); flutterEngine.getDartExecutor().executeDartEntrypoint(entrypoint);
mRegistry = new ShimPluginRegistry(engineProvider());
} }
...@@ -164,10 +167,17 @@ public class NewFlutterBoost { ...@@ -164,10 +167,17 @@ public class NewFlutterBoost {
public static int ANY_ACTIVITY_CREATED = 1; //当有任何Activity创建时,启动引擎 public static int ANY_ACTIVITY_CREATED = 1; //当有任何Activity创建时,启动引擎
public static int FLUTTER_ACTIVITY_CREATED = 2; //当有任何Activity创建时,启动引擎
public static int APP_EXit = 0; //所有flutter Activity destory 时,销毁engine
public static int All_FLUTTER_ACTIVITY_DESTROY = 1; //所有flutter Activity destory 时,销毁engine
private String dartEntrypoint = DEFAULT_DART_ENTRYPOINT; private String dartEntrypoint = DEFAULT_DART_ENTRYPOINT;
private String initialRoute = DEFAULT_INITIAL_ROUTE; private String initialRoute = DEFAULT_INITIAL_ROUTE;
private int whenEngineStart = ANY_ACTIVITY_CREATED; private int whenEngineStart = ANY_ACTIVITY_CREATED;
private int whenEngineDestory = APP_EXit;
private boolean isDebug = false; private boolean isDebug = false;
...@@ -207,7 +217,10 @@ public class NewFlutterBoost { ...@@ -207,7 +217,10 @@ public class NewFlutterBoost {
this.whenEngineStart = whenEngineStart; this.whenEngineStart = whenEngineStart;
return this; return this;
} }
public ConfigBuilder whenEngineDestory(@NonNull int whenEngineDestory) {
this.whenEngineDestory = whenEngineDestory;
return this;
}
public Platform build() { public Platform build() {
Platform platform = new Platform() { Platform platform = new Platform() {
...@@ -235,6 +248,11 @@ public class NewFlutterBoost { ...@@ -235,6 +248,11 @@ public class NewFlutterBoost {
return ConfigBuilder.this.whenEngineStart; return ConfigBuilder.this.whenEngineStart;
} }
@Override
public int whenEngineDestroy() {
return ConfigBuilder.this.whenEngineDestory;
}
public FlutterView.RenderMode renderMode() { public FlutterView.RenderMode renderMode() {
return ConfigBuilder.this.renderMode; return ConfigBuilder.this.renderMode;
} }
...@@ -246,11 +264,6 @@ public class NewFlutterBoost { ...@@ -246,11 +264,6 @@ public class NewFlutterBoost {
} }
public FlutterEngine engineProvider() {
return sInstance.mPlatform.engineProvider();
}
public IContainerManager containerManager() { public IContainerManager containerManager() {
return sInstance.mManager; return sInstance.mManager;
} }
...@@ -274,4 +287,35 @@ public class NewFlutterBoost { ...@@ -274,4 +287,35 @@ public class NewFlutterBoost {
public PluginRegistry getPluginRegistry(){ public PluginRegistry getPluginRegistry(){
return mRegistry; return mRegistry;
} }
public FlutterEngine engineProvider() {
if (mEngine == null) {
FlutterMain.startInitialization(mPlatform.getApplication());
FlutterShellArgs flutterShellArgs = new FlutterShellArgs(new String[0]);
FlutterMain.ensureInitializationComplete(
mPlatform.getApplication().getApplicationContext(), flutterShellArgs.toArray());
mEngine = new FlutterEngine(mPlatform.getApplication().getApplicationContext());
}
return mEngine;
}
public void boostDestroy(){
if(mEngine!=null){
mEngine.destroy();
}
mEngine=null;
mRegistry=null;
mCurrentActiveActivity=null;
}
} }
...@@ -8,21 +8,16 @@ import java.lang.reflect.Method; ...@@ -8,21 +8,16 @@ import java.lang.reflect.Method;
import java.util.Map; import java.util.Map;
import io.flutter.embedding.android.FlutterView; import io.flutter.embedding.android.FlutterView;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterShellArgs;
import io.flutter.plugin.common.PluginRegistry; import io.flutter.plugin.common.PluginRegistry;
import io.flutter.view.FlutterMain;
public abstract class Platform { public abstract class Platform {
private FlutterEngine mEngine;
public abstract Application getApplication(); public abstract Application getApplication();
public abstract void openContainer(Context context, String url, Map<String, Object> urlParams, int requestCode, Map<String, Object> exts); public abstract void openContainer(Context context, String url, Map<String, Object> urlParams, int requestCode, Map<String, Object> exts);
public abstract int whenEngineStart(); public abstract int whenEngineStart();
public abstract int whenEngineDestroy();
public abstract FlutterView.RenderMode renderMode(); public abstract FlutterView.RenderMode renderMode();
...@@ -37,26 +32,6 @@ public abstract class Platform { ...@@ -37,26 +32,6 @@ public abstract class Platform {
record.getContainer().finishContainer(result); record.getContainer().finishContainer(result);
} }
public FlutterEngine engineProvider() {
if (mEngine == null) {
FlutterMain.startInitialization(getApplication());
FlutterShellArgs flutterShellArgs = new FlutterShellArgs(new String[0]);
FlutterMain.ensureInitializationComplete(
getApplication().getApplicationContext(), flutterShellArgs.toArray());
mEngine = new FlutterEngine(getApplication().getApplicationContext());
}
return mEngine;
}
public FlutterEngine getEngine() {
return mEngine;
}
public void registerPlugins(PluginRegistry registry) { public void registerPlugins(PluginRegistry registry) {
try { try {
......
...@@ -77,8 +77,9 @@ public class FlutterActivityAndFragmentDelegate implements IFlutterViewContaine ...@@ -77,8 +77,9 @@ public class FlutterActivityAndFragmentDelegate implements IFlutterViewContaine
void onAttach(@NonNull Context context) { void onAttach(@NonNull Context context) {
ensureAlive(); ensureAlive();
if (NewFlutterBoost.instance().platform().whenEngineStart() == NewFlutterBoost.ConfigBuilder.FLUTTER_ACTIVITY_CREATED) {
initializeFlutter(context); NewFlutterBoost.instance().doInitialFlutter();
}
// When "retain instance" is true, the FlutterEngine will survive configuration // When "retain instance" is true, the FlutterEngine will survive configuration
// changes. Therefore, we create a new one only if one does not already exist. // changes. Therefore, we create a new one only if one does not already exist.
...@@ -116,27 +117,12 @@ public class FlutterActivityAndFragmentDelegate implements IFlutterViewContaine ...@@ -116,27 +117,12 @@ public class FlutterActivityAndFragmentDelegate implements IFlutterViewContaine
host.configureFlutterEngine(flutterEngine); host.configureFlutterEngine(flutterEngine);
} }
private void initializeFlutter(@NonNull Context context) {
FlutterMain.ensureInitializationComplete(
context.getApplicationContext(),
host.getFlutterShellArgs().toArray()
);
}
private void setupFlutterEngine() { private void setupFlutterEngine() {
Log.d(TAG, "Setting up FlutterEngine."); Log.d(TAG, "Setting up FlutterEngine.");
// First, check if the host wants to use a cached FlutterEngine.
// String cachedEngineId = host.getCachedEngineId();
// if (cachedEngineId != null) {
// flutterEngine = FlutterEngineCache.getInstance().get(cachedEngineId);
// isFlutterEngineFromHost = true;
// if (flutterEngine == null) {
// throw new IllegalStateException("The requested cached FlutterEngine did not exist in the FlutterEngineCache: '" + cachedEngineId + "'");
// }
// return;
// }
// Second, defer to subclasses for a custom FlutterEngine. // Second, defer to subclasses for a custom FlutterEngine.
flutterEngine = host.provideFlutterEngine(host.getContext()); flutterEngine = host.provideFlutterEngine(host.getContext());
...@@ -264,16 +250,6 @@ public class FlutterActivityAndFragmentDelegate implements IFlutterViewContaine ...@@ -264,16 +250,6 @@ public class FlutterActivityAndFragmentDelegate implements IFlutterViewContaine
Utils.fixInputMethodManagerLeak(host.getActivity()); Utils.fixInputMethodManagerLeak(host.getActivity());
// Destroy our FlutterEngine if we're not set to retain it.
// if (host.shouldDestroyEngineWithHost()) {
// flutterEngine.destroy();
//
// if (host.getCachedEngineId() != null) {
// FlutterEngineCache.getInstance().remove(host.getCachedEngineId());
// }
//
// flutterEngine = null;
// }
} }
...@@ -281,12 +257,6 @@ public class FlutterActivityAndFragmentDelegate implements IFlutterViewContaine ...@@ -281,12 +257,6 @@ public class FlutterActivityAndFragmentDelegate implements IFlutterViewContaine
mSyncer.onBackPressed(); mSyncer.onBackPressed();
ensureAlive(); ensureAlive();
// if (flutterEngine != null) {
// Log.v(TAG, "Forwarding onBackPressed() to FlutterEngine.");
// flutterEngine.getNavigationChannel().popRoute();
// } else {
// Log.w(TAG, "Invoked onBackPressed() before NewFlutterFragment was attached to an Activity.");
// }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment