Skip to content

Commit

Permalink
[双端一致性] Android端setup与push的参数形式与iOS对齐
Browse files Browse the repository at this point in the history
  • Loading branch information
ColdPaleLight committed May 25, 2021
1 parent d2d1fdc commit 4767623
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.os.Bundle;

import com.idlefish.flutterboost.containers.FlutterContainerManager;
Expand All @@ -13,7 +12,6 @@
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.view.FlutterMain;

public class FlutterBoost {
Expand Down Expand Up @@ -44,14 +42,14 @@ public interface Callback {
* @param callback Invoke the callback when the engine was started.
*/
public void setup(Application application, FlutterBoostDelegate delegate, Callback callback) {
setup(application, delegate, callback, FlutterBoostOptions.createDefault());
setup(application, delegate, callback, FlutterBoostSetupOptions.createDefault());
}

public void setup(Application application, FlutterBoostDelegate delegate, Callback callback, FlutterBoostOptions options) {
public void setup(Application application, FlutterBoostDelegate delegate, Callback callback, FlutterBoostSetupOptions options) {
// 1. initialize default engine
FlutterEngine engine = getEngine();
if (engine == null) {
if (options == null) options = FlutterBoostOptions.createDefault();
if (options == null) options = FlutterBoostSetupOptions.createDefault();
engine = new FlutterEngine(application, options.shellArgs());
engine.getNavigationChannel().setInitialRoute(options.initialRoute());
engine.getDartExecutor().executeDartEntrypoint(new DartExecutor.DartEntrypoint(
Expand Down Expand Up @@ -131,7 +129,11 @@ public FlutterViewContainer getTopContainer() {
* @param arguments The bussiness arguments.
*/
public void open(String name, Map<String, Object> arguments) {
this.getPlugin().getDelegate().pushFlutterRoute(name, null, arguments);
FlutterBoostRouteOptions options = new FlutterBoostRouteOptions.Builder()
.pageName(name)
.arguments(arguments)
.build();
this.getPlugin().getDelegate().pushFlutterRoute(options);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.idlefish.flutterboost;

import java.util.Map;

public interface FlutterBoostDelegate {
void pushNativeRoute(String pageName, Map<String, Object> arguments,int requestCode);
void pushFlutterRoute(String pageName, String uniqueId, Map<String, Object> arguments);
void pushNativeRoute(FlutterBoostRouteOptions options);
void pushFlutterRoute(FlutterBoostRouteOptions options);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ public void pushNativeRoute(Messages.CommonParams params) {
if(pageNames!=null){
pageNames.put(requestCode,params.getPageName());
}
delegate.pushNativeRoute(params.getPageName(), (Map<String, Object>) (Object)params.getArguments(),requestCode);
FlutterBoostRouteOptions options = new FlutterBoostRouteOptions.Builder()
.pageName(params.getPageName())
.arguments((Map<String, Object>) (Object) params.getArguments())
.requestCode(requestCode)
.build();
delegate.pushNativeRoute(options);
} else {
throw new RuntimeException("FlutterBoostPlugin might *NOT* set delegate!");
}
Expand All @@ -57,7 +62,12 @@ public void pushNativeRoute(Messages.CommonParams params) {
@Override
public void pushFlutterRoute(Messages.CommonParams params) {
if (delegate != null) {
delegate.pushFlutterRoute(params.getPageName(), params.getUniqueId(), (Map<String, Object>) (Object)params.getArguments());
FlutterBoostRouteOptions options = new FlutterBoostRouteOptions.Builder()
.pageName(params.getPageName())
.uniqueId(params.getUniqueId())
.arguments((Map<String, Object>) (Object)params.getArguments())
.build();
delegate.pushFlutterRoute(options);
} else {
throw new RuntimeException("FlutterBoostPlugin might *NOT* set delegate!");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.idlefish.flutterboost;

import java.util.Map;

public class FlutterBoostRouteOptions {
private final String pageName;
private final Map<String, Object> arguments;
private final int requestCode;
private final String uniqueId;

private FlutterBoostRouteOptions(FlutterBoostRouteOptions.Builder builder) {
this.pageName = builder.pageName;
this.arguments = builder.arguments;
this.requestCode = builder.requestCode;
this.uniqueId = builder.uniqueId;
}

public String pageName() {
return pageName;
}

public Map<String, Object> arguments() {
return arguments;
}

public int requestCode() {
return requestCode;
}

public String uniqueId() {
return uniqueId;
}

public static class Builder {
private String pageName;
private Map<String, Object> arguments;
private int requestCode;
private String uniqueId;

public Builder() {
}

public FlutterBoostRouteOptions.Builder pageName(String pageName) {
this.pageName = pageName;
return this;
}

public FlutterBoostRouteOptions.Builder arguments(Map<String, Object> arguments) {
this.arguments = arguments;
return this;
}

public FlutterBoostRouteOptions.Builder requestCode(int requestCode) {
this.requestCode = requestCode;
return this;
}

public FlutterBoostRouteOptions.Builder uniqueId(String uniqueId) {
this.uniqueId = uniqueId;
return this;
}

public FlutterBoostRouteOptions build() {
return new FlutterBoostRouteOptions(this);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.idlefish.flutterboost;

public class FlutterBoostOptions {
public class FlutterBoostSetupOptions {
private final String initialRoute;
private final String dartEntrypoint;
private final String[] shellArgs;

private FlutterBoostOptions(Builder builder) {
private FlutterBoostSetupOptions(Builder builder) {
this.initialRoute = builder.initialRoute;
this.dartEntrypoint = builder.dartEntrypoint;
this.shellArgs = builder.shellArgs;
}

public static FlutterBoostOptions createDefault() {
public static FlutterBoostSetupOptions createDefault() {
return new Builder().build();
}

Expand Down Expand Up @@ -71,8 +71,8 @@ public Builder shellArgs(String[] shellArgs){
return this;
}

public FlutterBoostOptions build() {
FlutterBoostOptions options = new FlutterBoostOptions(this);
public FlutterBoostSetupOptions build() {
FlutterBoostSetupOptions options = new FlutterBoostSetupOptions(this);
return options;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.idlefish.flutterboost.example;

import com.idlefish.flutterboost.FlutterBoost;
import com.idlefish.flutterboost.FlutterBoostOptions;
import com.idlefish.flutterboost.FlutterBoostSetupOptions;

import java.util.ArrayList;

Expand All @@ -14,7 +14,7 @@ public void onCreate() {
ArrayList<String> args = new ArrayList<>();
args.add("--trace-systrace");
args.add("--user-authorization-code=QZvoUptODA+KDgeFUluhheYns7X7CnDu9YRv8YmU0GXQcKLzs4C2WgjblrAIhtkqqGg==");
FlutterBoostOptions options = new FlutterBoostOptions.Builder().shellArgs(args.toArray(new String[0])).build();
FlutterBoostSetupOptions options = new FlutterBoostSetupOptions.Builder().shellArgs(args.toArray(new String[0])).build();
FlutterBoost.instance().setup(this, new MyFlutterBoostDelegate(), engine->{
engine.getPlugins();
}, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,30 @@

import com.idlefish.flutterboost.FlutterBoost;
import com.idlefish.flutterboost.FlutterBoostDelegate;
import com.idlefish.flutterboost.FlutterBoostRouteOptions;
import com.idlefish.flutterboost.containers.FlutterBoostActivity;

import java.util.Map;

import io.flutter.embedding.android.FlutterActivityLaunchConfigs;
import com.idlefish.flutterboost.FlutterBoostDelegate;

public class MyFlutterBoostDelegate implements FlutterBoostDelegate {

@Override
public void pushNativeRoute(String pageName, Map<String, Object> arguments,int requestCode) {
public void pushNativeRoute(FlutterBoostRouteOptions options) {
Intent intent = new Intent(FlutterBoost.instance().currentActivity(), NativePageActivity.class);
FlutterBoost.instance().currentActivity().startActivityForResult(intent, requestCode);
FlutterBoost.instance().currentActivity().startActivityForResult(intent, options.requestCode());
}

@Override
public void pushFlutterRoute(String pageName, String uniqueId, Map<String, Object> arguments) {
public void pushFlutterRoute(FlutterBoostRouteOptions options) {
Intent intent = new FlutterBoostActivity.CachedEngineIntentBuilder(FlutterBoostActivity.class)
.backgroundMode(FlutterActivityLaunchConfigs.BackgroundMode.transparent)
.destroyEngineWithActivity(false)
.uniqueId(uniqueId)
.url(pageName)
.urlParams(arguments)
.uniqueId(options.uniqueId())
.url(options.pageName())
.urlParams(options.arguments())
.build(FlutterBoost.instance().currentActivity());
FlutterBoost.instance().currentActivity().startActivity(intent);
}
Expand Down

0 comments on commit 4767623

Please sign in to comment.