Commit 0a4d2767 authored by Yacumima's avatar Yacumima

improve

parent 8d103553
...@@ -35,12 +35,15 @@ public class BoostEngineProvider implements IFlutterEngineProvider { ...@@ -35,12 +35,15 @@ public class BoostEngineProvider implements IFlutterEngineProvider {
private BoostFlutterEngine mEngine = null; private BoostFlutterEngine mEngine = null;
BoostEngineProvider() { public BoostEngineProvider() {}
}
@Override @Override
public BoostFlutterEngine createEngine(Context context) { public BoostFlutterEngine createEngine(Context context) {
return new BoostFlutterEngine(context.getApplicationContext());
}
@Override
public BoostFlutterEngine provideEngine(Context context) {
Utils.assertCallOnMainThread(); Utils.assertCallOnMainThread();
if (mEngine == null) { if (mEngine == null) {
...@@ -48,7 +51,7 @@ public class BoostEngineProvider implements IFlutterEngineProvider { ...@@ -48,7 +51,7 @@ public class BoostEngineProvider implements IFlutterEngineProvider {
FlutterMain.ensureInitializationComplete( FlutterMain.ensureInitializationComplete(
context.getApplicationContext(), flutterShellArgs.toArray()); context.getApplicationContext(), flutterShellArgs.toArray());
mEngine = new BoostFlutterEngine(context.getApplicationContext()); mEngine = createEngine(context.getApplicationContext());
final IStateListener stateListener = FlutterBoost.sInstance.mStateListener; final IStateListener stateListener = FlutterBoost.sInstance.mStateListener;
if(stateListener != null) { if(stateListener != null) {
......
...@@ -23,13 +23,31 @@ import io.flutter.view.TextureRegistry; ...@@ -23,13 +23,31 @@ import io.flutter.view.TextureRegistry;
public class BoostFlutterEngine extends FlutterEngine { public class BoostFlutterEngine extends FlutterEngine {
protected final Context mContext; protected final Context mContext;
protected final BoostPluginRegistry mBoostPluginRegistry; protected final BoostPluginRegistry mBoostPluginRegistry;
protected final DartExecutor.DartEntrypoint mEntrypoint;
protected final String mInitRoute;
protected WeakReference<Activity> mCurrentActivityRef; protected WeakReference<Activity> mCurrentActivityRef;
public BoostFlutterEngine(@NonNull Context context) { public BoostFlutterEngine(@NonNull Context context) {
this(context,null,null);
}
public BoostFlutterEngine(@NonNull Context context,DartExecutor.DartEntrypoint entrypoint,String initRoute) {
super(context); super(context);
mContext = context.getApplicationContext(); mContext = context.getApplicationContext();
mBoostPluginRegistry = new BoostPluginRegistry(this,context); mBoostPluginRegistry = new BoostPluginRegistry(this,context);
if(entrypoint != null) {
mEntrypoint = entrypoint;
}else{
mEntrypoint = defaultDartEntrypoint(context);
}
if(initRoute != null) {
mInitRoute = initRoute;
}else{
mInitRoute = defaultInitialRoute(context);
}
} }
public void startRun(@Nullable Activity activity) { public void startRun(@Nullable Activity activity) {
...@@ -39,13 +57,8 @@ public class BoostFlutterEngine extends FlutterEngine { ...@@ -39,13 +57,8 @@ public class BoostFlutterEngine extends FlutterEngine {
Debuger.log("engine start running..."); Debuger.log("engine start running...");
getNavigationChannel().setInitialRoute("/"); getNavigationChannel().setInitialRoute(mInitRoute);
getDartExecutor().executeDartEntrypoint(mEntrypoint);
DartExecutor.DartEntrypoint entryPoint = new DartExecutor.DartEntrypoint(
mContext.getResources().getAssets(),
FlutterMain.findAppBundlePath(mContext),
"main");
getDartExecutor().executeDartEntrypoint(entryPoint);
final IStateListener stateListener = FlutterBoost.sInstance.mStateListener; final IStateListener stateListener = FlutterBoost.sInstance.mStateListener;
if(stateListener != null) { if(stateListener != null) {
...@@ -56,6 +69,17 @@ public class BoostFlutterEngine extends FlutterEngine { ...@@ -56,6 +69,17 @@ public class BoostFlutterEngine extends FlutterEngine {
} }
} }
protected DartExecutor.DartEntrypoint defaultDartEntrypoint(Context context){
return new DartExecutor.DartEntrypoint(
context.getResources().getAssets(),
FlutterMain.findAppBundlePath(context),
"main");
}
protected String defaultInitialRoute(Context context){
return "/";
}
public BoostPluginRegistry getBoostPluginRegistry(){ public BoostPluginRegistry getBoostPluginRegistry(){
return mBoostPluginRegistry; return mBoostPluginRegistry;
} }
......
...@@ -167,7 +167,7 @@ public class BoostFlutterView extends FrameLayout { ...@@ -167,7 +167,7 @@ public class BoostFlutterView extends FrameLayout {
} }
protected BoostFlutterEngine createFlutterEngine(Context context) { protected BoostFlutterEngine createFlutterEngine(Context context) {
return FlutterBoost.singleton().engineProvider().createEngine(context); return FlutterBoost.singleton().engineProvider().provideEngine(context);
} }
public void addFirstFrameRendered(OnFirstFrameRenderedListener listener) { public void addFirstFrameRendered(OnFirstFrameRenderedListener listener) {
......
...@@ -53,7 +53,7 @@ public class FlutterBoost { ...@@ -53,7 +53,7 @@ public class FlutterBoost {
if (platform.whenEngineStart() == IPlatform.IMMEDIATELY) { if (platform.whenEngineStart() == IPlatform.IMMEDIATELY) {
sInstance.mEngineProvider sInstance.mEngineProvider
.createEngine(platform.getApplication()) .provideEngine(platform.getApplication())
.startRun(null); .startRun(null);
} }
} }
...@@ -76,7 +76,12 @@ public class FlutterBoost { ...@@ -76,7 +76,12 @@ public class FlutterBoost {
private FlutterBoost(IPlatform platform) { private FlutterBoost(IPlatform platform) {
mPlatform = platform; mPlatform = platform;
mManager = new FlutterViewContainerManager(); mManager = new FlutterViewContainerManager();
mEngineProvider = new BoostEngineProvider();
IFlutterEngineProvider provider = platform.engineProvider();
if(provider == null) {
provider = new BoostEngineProvider();
}
mEngineProvider = provider;
platform.getApplication().registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks()); platform.getApplication().registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks());
BoostChannel.addActionAfterRegistered(new BoostChannel.ActionAfterRegistered() { BoostChannel.addActionAfterRegistered(new BoostChannel.ActionAfterRegistered() {
...@@ -120,7 +125,7 @@ public class FlutterBoost { ...@@ -120,7 +125,7 @@ public class FlutterBoost {
public void onActivityCreated(Activity activity, Bundle savedInstanceState) { public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
if (platform().whenEngineStart() == IPlatform.ANY_ACTIVITY_CREATED) { if (platform().whenEngineStart() == IPlatform.ANY_ACTIVITY_CREATED) {
sInstance.mEngineProvider sInstance.mEngineProvider
.createEngine(activity) .provideEngine(activity)
.startRun(activity); .startRun(activity);
} }
} }
......
package com.idlefish.flutterboost; package com.idlefish.flutterboost;
import com.idlefish.flutterboost.interfaces.IContainerRecord; import com.idlefish.flutterboost.interfaces.IContainerRecord;
import com.idlefish.flutterboost.interfaces.IFlutterEngineProvider;
import com.idlefish.flutterboost.interfaces.IPlatform; import com.idlefish.flutterboost.interfaces.IPlatform;
import java.lang.reflect.Method;
import java.util.Map; import java.util.Map;
import io.flutter.plugin.common.PluginRegistry;
public abstract class Platform implements IPlatform { public abstract class Platform implements IPlatform {
@Override
public boolean isDebug() {
return false;
}
@Override @Override
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;
...@@ -14,6 +23,22 @@ public abstract class Platform implements IPlatform { ...@@ -14,6 +23,22 @@ public abstract class Platform implements IPlatform {
record.getContainer().finishContainer(result); record.getContainer().finishContainer(result);
} }
@Override
public IFlutterEngineProvider engineProvider() {
return new BoostEngineProvider();
}
@Override
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){
throw new RuntimeException(t);
}
}
@Override @Override
public int whenEngineStart() { public int whenEngineStart() {
return ANY_ACTIVITY_CREATED; return ANY_ACTIVITY_CREATED;
......
...@@ -98,7 +98,7 @@ public abstract class BoostFlutterActivity extends Activity implements IFlutterV ...@@ -98,7 +98,7 @@ public abstract class BoostFlutterActivity extends Activity implements IFlutterV
} }
protected BoostFlutterEngine createFlutterEngine(){ protected BoostFlutterEngine createFlutterEngine(){
return FlutterBoost.singleton().engineProvider().createEngine(this); return FlutterBoost.singleton().engineProvider().provideEngine(this);
} }
protected BoostFlutterView createFlutterView(BoostFlutterEngine engine){ protected BoostFlutterView createFlutterView(BoostFlutterEngine engine){
......
...@@ -62,7 +62,7 @@ abstract public class BoostFlutterFragment extends Fragment implements IFlutterV ...@@ -62,7 +62,7 @@ abstract public class BoostFlutterFragment extends Fragment implements IFlutterV
} }
protected BoostFlutterEngine createFlutterEngine(){ protected BoostFlutterEngine createFlutterEngine(){
return FlutterBoost.singleton().engineProvider().createEngine(getContext()); return FlutterBoost.singleton().engineProvider().provideEngine(getContext());
} }
protected BoostFlutterView createFlutterView(BoostFlutterEngine engine){ protected BoostFlutterView createFlutterView(BoostFlutterEngine engine){
......
...@@ -38,6 +38,13 @@ public interface IFlutterEngineProvider { ...@@ -38,6 +38,13 @@ public interface IFlutterEngineProvider {
*/ */
BoostFlutterEngine createEngine(Context context); BoostFlutterEngine createEngine(Context context);
/**
* provide a flutter engine
* @param context
* @return
*/
BoostFlutterEngine provideEngine(Context context);
/** /**
* may return null * may return null
* @return * @return
......
...@@ -63,6 +63,8 @@ public interface IPlatform { ...@@ -63,6 +63,8 @@ public interface IPlatform {
void closeContainer(IContainerRecord record, Map<String,Object> result, Map<String,Object> exts); void closeContainer(IContainerRecord record, Map<String,Object> result, Map<String,Object> exts);
IFlutterEngineProvider engineProvider();
/** /**
* @return * @return
* *
......
...@@ -3,14 +3,19 @@ package com.taobao.idlefish.flutterboostexample; ...@@ -3,14 +3,19 @@ package com.taobao.idlefish.flutterboostexample;
import android.app.Application; import android.app.Application;
import android.content.Context; import android.content.Context;
import com.idlefish.flutterboost.BoostEngineProvider;
import com.idlefish.flutterboost.BoostFlutterEngine;
import com.idlefish.flutterboost.FlutterBoost; import com.idlefish.flutterboost.FlutterBoost;
import com.idlefish.flutterboost.Platform; import com.idlefish.flutterboost.Platform;
import com.idlefish.flutterboost.interfaces.IFlutterEngineProvider;
import java.util.Map; import java.util.Map;
import io.flutter.app.FlutterApplication; import io.flutter.app.FlutterApplication;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.plugin.common.PluginRegistry; import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.GeneratedPluginRegistrant; import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.view.FlutterMain;
public class MyApplication extends FlutterApplication { public class MyApplication extends FlutterApplication {
@Override @Override
...@@ -25,18 +30,21 @@ public class MyApplication extends FlutterApplication { ...@@ -25,18 +30,21 @@ public class MyApplication extends FlutterApplication {
} }
@Override @Override
public boolean isDebug() { public void openContainer(Context context, String url, Map<String, Object> urlParams, int requestCode, Map<String, Object> exts) {
return true; PageRouter.openPageByUrl(context,url,urlParams,requestCode);
}
@Override
public void registerPlugins(PluginRegistry registry) {
GeneratedPluginRegistrant.registerWith(registry);
} }
@Override @Override
public void openContainer(Context context, String url, Map<String, Object> urlParams, int requestCode, Map<String, Object> exts) { public IFlutterEngineProvider engineProvider() {
PageRouter.openPageByUrl(context,url,urlParams,requestCode); return new BoostEngineProvider(){
@Override
public BoostFlutterEngine createEngine(Context context) {
return new BoostFlutterEngine(context, new DartExecutor.DartEntrypoint(
context.getResources().getAssets(),
FlutterMain.findAppBundlePath(context),
"main2"),"/");
}
};
} }
@Override @Override
......
...@@ -4,13 +4,11 @@ ...@@ -4,13 +4,11 @@
#import "GeneratedPluginRegistrant.h" #import "GeneratedPluginRegistrant.h"
#import <flutter_boost/BoostChannel.h> #import <flutter_boost/BoostChannel.h>
#import <xservice_kit/XserviceKitPlugin.h>
@implementation GeneratedPluginRegistrant @implementation GeneratedPluginRegistrant
+ (void)registerWithRegistry:(NSObject<FlutterPluginRegistry>*)registry { + (void)registerWithRegistry:(NSObject<FlutterPluginRegistry>*)registry {
[BoostChannel registerWithRegistrar:[registry registrarForPlugin:@"BoostChannel"]]; [BoostChannel registerWithRegistrar:[registry registrarForPlugin:@"BoostChannel"]];
[XserviceKitPlugin registerWithRegistrar:[registry registrarForPlugin:@"XserviceKitPlugin"]];
} }
@end @end
...@@ -6,6 +6,11 @@ void main() { ...@@ -6,6 +6,11 @@ void main() {
runApp(MyApp()); runApp(MyApp());
} }
void main2() {
print("terry run main2");
runApp(MyApp());
}
class MyApp extends StatefulWidget { class MyApp extends StatefulWidget {
@override @override
_MyAppState createState() => _MyAppState(); _MyAppState createState() => _MyAppState();
......
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