Commit 01ab95a8 authored by yangwu.jia's avatar yangwu.jia

engine初始化和Registry初始化优化

parent 76beb384
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.idlefish.flutterboost">
<application>
<activity android:name="com.idlefish.flutterboost.containers.BoostFlutterDefaultActivity" />
</application>
</manifest>
......@@ -11,6 +11,8 @@ import io.flutter.Log;
import io.flutter.embedding.android.FlutterView;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.embedding.engine.plugins.shim.ShimPluginRegistry;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.view.FlutterMain;
import java.util.HashMap;
......@@ -23,7 +25,7 @@ public class NewFlutterBoost {
private FlutterViewContainerManager mManager;
private Activity mCurrentActiveActivity;
private BoostPluginRegistry mRegistry;
private PluginRegistry mRegistry;
static NewFlutterBoost sInstance = null;
......@@ -41,19 +43,18 @@ public class NewFlutterBoost {
mManager = new FlutterViewContainerManager();
mRegistry = new BoostPluginRegistry(this.engineProvider(),
mPlatform.getApplication());
platform.getApplication().registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
Log.e("bbbb1", "xxxxx");
mCurrentActiveActivity=activity;
if (mPlatform.whenEngineStart() == ConfigBuilder.ANY_ACTIVITY_CREATED) {
Log.e("bbbb2", "xxxxx");
mRegistry.currentActivity(activity);
doInitialFlutterViewRun(mPlatform,mRegistry);
doInitialFlutter(mPlatform);
}
}
......@@ -117,18 +118,18 @@ public class NewFlutterBoost {
if (mPlatform.whenEngineStart() == ConfigBuilder.IMMEDIATELY) {
doInitialFlutterViewRun(mPlatform,mRegistry);
doInitialFlutter(mPlatform);
}
}
private void doInitialFlutterViewRun(Platform platform,BoostPluginRegistry registry) {
// Don't attempt to start a FlutterEngine if we're using a cached FlutterEngine.
// if (host.getCachedEngineId() != null) {
// return;
// }
private void doInitialFlutter(Platform platform ) {
if(platform.getEngine()!=null) return;
FlutterEngine flutterEngine = platform.engineProvider();
if (flutterEngine.getDartExecutor().isExecutingDart()) {
......@@ -137,7 +138,9 @@ public class NewFlutterBoost {
// So this is expected behavior in many cases.
return;
}
platform.registerPlugins(registry);
mRegistry = new ShimPluginRegistry(mPlatform.engineProvider());
// 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.
......@@ -268,7 +271,7 @@ public class NewFlutterBoost {
return mManager.findContainerById(id);
}
public BoostPluginRegistry getPluginRegistry(){
public PluginRegistry getPluginRegistry(){
return mRegistry;
}
}
......@@ -16,46 +16,54 @@ import io.flutter.view.FlutterMain;
public abstract class Platform {
public FlutterEngine mEngine ;
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 FlutterView.RenderMode renderMode();
public abstract FlutterView.RenderMode renderMode();
public abstract boolean isDebug() ;
public abstract boolean isDebug();
public abstract String initialRoute();
public abstract String initialRoute();
public void closeContainer(IContainerRecord record, Map<String, Object> result, Map<String, Object> exts) {
if(record == null) return;
if (record == null) return;
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());
mEngine = new FlutterEngine(getApplication().getApplicationContext());
}
return mEngine;
}
public void registerPlugins(PluginRegistry registry) {
public FlutterEngine getEngine() {
return mEngine;
}
public void registerPlugins(PluginRegistry registry) {
try {
Class clz = Class.forName("io.flutter.plugins.GeneratedPluginRegistrant");
Method method = clz.getDeclaredMethod("registerWith",PluginRegistry.class);
method.invoke(null,registry);
}catch (Throwable t){
Method method = clz.getDeclaredMethod("registerWith", PluginRegistry.class);
method.invoke(null, registry);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
......
......@@ -263,7 +263,7 @@ public class Utils {
return;
}
String [] arr = new String[]{"mServedView", "mNextServedView"};
String [] arr = new String[]{"mLastSrvView","mServedView", "mNextServedView"};
Field f = null;
Object obj_get = null;
for (int i = 0;i < arr.length;i ++) {
......@@ -283,7 +283,7 @@ public class Utils {
}
}
}catch(Throwable t){
t.printStackTrace();
// t.printStackTrace();
}
}
}
......
......@@ -109,6 +109,8 @@ public class FlutterActivityAndFragmentDelegate implements IFlutterViewContaine
host.getActivity(),
host.getLifecycle()
);
NewFlutterBoost.instance().platform().registerPlugins( NewFlutterBoost.instance().getPluginRegistry());
}
host.configureFlutterEngine(flutterEngine);
......@@ -147,7 +149,6 @@ public class FlutterActivityAndFragmentDelegate implements IFlutterViewContaine
// FlutterView.
Log.d(TAG, "No preferred FlutterEngine was provided. Creating a new FlutterEngine for"
+ " this NewFlutterFragment.");
flutterEngine = new FlutterEngine(host.getContext());
isFlutterEngineFromHost = false;
}
......
......@@ -485,7 +485,7 @@ public class NewFlutterFragment extends Fragment implements FlutterActivityAndFr
*/
@Override
public boolean shouldAttachEngineToActivity() {
return getArguments().getBoolean(ARG_SHOULD_ATTACH_ENGINE_TO_ACTIVITY);
return true;
}
......
......@@ -11,7 +11,7 @@ import java.util.Map;
import com.idlefish.flutterboost.interfaces.INativeRouter;
import io.flutter.app.FlutterApplication;
public class MyApplication extends FlutterApplication {
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
......
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