Commit 0a4d2767 authored by Yacumima's avatar Yacumima

improve

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