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

engine初始化和Registry初始化优化

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