实现flutter原生接口

master
kangxuyao 2022-01-05 15:24:31 +08:00
parent c17910963f
commit 425120e95c
6 changed files with 228 additions and 62 deletions

View File

@ -1,11 +1,20 @@
package com.finogeeks.mop.api.mop; package com.finogeeks.mop.api.mop;
import android.content.Context; import android.content.Context;
import android.util.Log;
import com.finogeeks.lib.applet.anim.FadeInAnim;
import com.finogeeks.lib.applet.anim.NoneAnim;
import com.finogeeks.lib.applet.anim.SlideFromBottomToTopAnim;
import com.finogeeks.lib.applet.anim.SlideFromLeftToRightAnim;
import com.finogeeks.lib.applet.anim.SlideFromRightToLeftAnim;
import com.finogeeks.lib.applet.anim.SlideFromTopToBottomAnim;
import com.finogeeks.lib.applet.client.FinAppClient; import com.finogeeks.lib.applet.client.FinAppClient;
import com.finogeeks.lib.applet.db.entity.FinApplet; import com.finogeeks.lib.applet.db.entity.FinApplet;
import com.finogeeks.lib.applet.interfaces.FinCallback;
import com.finogeeks.mop.api.BaseApi; import com.finogeeks.mop.api.BaseApi;
import com.finogeeks.mop.interfaces.ICallback; import com.finogeeks.mop.interfaces.ICallback;
import com.google.gson.Gson;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -13,13 +22,16 @@ import java.util.Map;
public class AppletManageModule extends BaseApi { public class AppletManageModule extends BaseApi {
private static final String TAG = "AppletManageModule";
public AppletManageModule(Context context) { public AppletManageModule(Context context) {
super(context); super(context);
} }
@Override @Override
public String[] apis() { public String[] apis() {
return new String[]{"currentApplet", "closeAllApplets", "clearApplets", "removeUsedApplet"}; return new String[]{"currentApplet", "closeAllApplets", "clearApplets", "removeUsedApplet",
"setActivityTransitionAnim", "sendCustomEvent", "callJS"};
} }
@Override @Override
@ -57,6 +69,70 @@ public class AppletManageModule extends BaseApi {
} else { } else {
callback.onFail(null); callback.onFail(null);
} }
} else if (event.equals("setActivityTransitionAnim")) {
String anim = (String) param.get("anim");
Log.d(TAG, "setActivityTransitionAnim:" + anim);
if ("SlideFromLeftToRightAnim".equals(anim)) {
FinAppClient.INSTANCE.getAppletApiManager().setActivityTransitionAnim(SlideFromLeftToRightAnim.INSTANCE);
} else if ("SlideFromRightToLeftAnim".equals(anim)) {
FinAppClient.INSTANCE.getAppletApiManager().setActivityTransitionAnim(SlideFromRightToLeftAnim.INSTANCE);
} else if ("SlideFromTopToBottomAnim".equals(anim)) {
FinAppClient.INSTANCE.getAppletApiManager().setActivityTransitionAnim(SlideFromTopToBottomAnim.INSTANCE);
} else if ("SlideFromBottomToTopAnim".equals(anim)) {
FinAppClient.INSTANCE.getAppletApiManager().setActivityTransitionAnim(SlideFromBottomToTopAnim.INSTANCE);
} else if ("FadeInAnim".equals(anim)) {
FinAppClient.INSTANCE.getAppletApiManager().setActivityTransitionAnim(FadeInAnim.INSTANCE);
} else if ("NoneAnim".equals(anim)) {
FinAppClient.INSTANCE.getAppletApiManager().setActivityTransitionAnim(NoneAnim.INSTANCE);
}
callback.onSuccess(null);
} else if (event.equals("sendCustomEvent")) {
String appId = (String) param.get("appId");
Map eventData = (Map) param.get("eventData");
Log.d(TAG, "sendCustomEvent:" + appId);
if (appId != null) {
FinAppClient.INSTANCE.getAppletApiManager().sendCustomEvent(appId, eventData == null ? "" : new Gson().toJson(eventData));
callback.onSuccess(null);
} else {
callback.onFail(null);
}
} else if (event.equals("callJS")) {
String appId = (String) param.get("appId");
String eventName = (String) param.get("eventName");
String nativeViewId = (String) param.get("nativeViewId");
int viewId = 0;
if (nativeViewId != null && !nativeViewId.equals("")) {
try {
viewId = Integer.parseInt(nativeViewId);
} catch (Exception e) {
e.printStackTrace();
}
}
Map eventData = (Map) param.get("eventData");
Log.d(TAG, "callJS:" + appId);
if (appId != null && eventName != null) {
FinAppClient.INSTANCE.getAppletApiManager().callJS(appId, eventName, eventData == null ? "" : new Gson().toJson(eventData),
viewId, new FinCallback<String>() {
@Override
public void onSuccess(String s) {
Map<String, Object> res = new HashMap<>();
res.put("data", s);
callback.onSuccess(res);
}
@Override
public void onError(int i, String s) {
callback.onFail(null);
}
@Override
public void onProgress(int i, String s) {
}
});
} else {
callback.onFail(null);
}
} }
} }
} }

View File

@ -5,6 +5,7 @@ import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.finogeeks.lib.applet.client.FinAppClient; import com.finogeeks.lib.applet.client.FinAppClient;
import com.finogeeks.lib.applet.client.FinAppInfo;
import com.finogeeks.lib.applet.sdk.model.StartAppletDecryptRequest; import com.finogeeks.lib.applet.sdk.model.StartAppletDecryptRequest;
import com.finogeeks.mop.api.BaseApi; import com.finogeeks.mop.api.BaseApi;
import com.finogeeks.mop.interfaces.ICallback; import com.finogeeks.mop.interfaces.ICallback;
@ -53,19 +54,20 @@ public class AppletModule extends BaseApi {
String appId = String.valueOf(param.get("appId")); String appId = String.valueOf(param.get("appId"));
Integer sequence = (Integer) param.get("sequence"); Integer sequence = (Integer) param.get("sequence");
Map<String, String> params = (Map) param.get("params"); Map<String, String> params = (Map) param.get("params");
String apiServer = (String) param.get("apiServer");
// mContextFlutterActivity // mContextFlutterActivity
// Android 6.07.0contextmContext // Android 6.07.0contextmContext
// 使Application Context // 使Application Context
Context context = mContext.getApplicationContext(); Context context = mContext.getApplicationContext();
if (params == null) { FinAppInfo.StartParams startParams = params == null ? null : new FinAppInfo.StartParams(params.get("path"), params.get("query"), params.get("scene"));
if (sequence == null) { Log.d(TAG, "openApplet:" + appId + "," + param + "," + sequence + "," + apiServer);
FinAppClient.INSTANCE.getAppletApiManager().startApplet(context, appId);
} else { if (apiServer != null) {
FinAppClient.INSTANCE.getAppletApiManager().startApplet(context, appId, sequence, null); FinAppClient.INSTANCE.getAppletApiManager().startApplet(context, apiServer, appId, sequence, startParams);
}
} else { } else {
FinAppClient.INSTANCE.getAppletApiManager().startApplet(context, appId, params); FinAppClient.INSTANCE.getAppletApiManager().startApplet(context, appId, sequence, startParams);
} }
// String apiServer = (String) param.get("apiServer"); // String apiServer = (String) param.get("apiServer");
// String apiPrefix = (String) param.get("apiPrefix"); // String apiPrefix = (String) param.get("apiPrefix");
// if (apiServer == null || apiServer.isEmpty() || apiPrefix == null || apiPrefix.isEmpty()) { // if (apiServer == null || apiServer.isEmpty() || apiPrefix == null || apiPrefix.isEmpty()) {

View File

@ -2,6 +2,7 @@ package com.finogeeks.mop.api.mop;
import android.app.Application; import android.app.Application;
import android.content.Context; import android.content.Context;
import android.util.Log;
import com.finogeeks.lib.applet.BuildConfig; import com.finogeeks.lib.applet.BuildConfig;
import com.finogeeks.lib.applet.client.FinAppClient; import com.finogeeks.lib.applet.client.FinAppClient;
@ -11,6 +12,8 @@ import com.finogeeks.lib.applet.interfaces.FinCallback;
import com.finogeeks.mop.api.BaseApi; import com.finogeeks.mop.api.BaseApi;
import com.finogeeks.mop.interfaces.ICallback; import com.finogeeks.mop.interfaces.ICallback;
import com.finogeeks.mop.service.MopPluginService; import com.finogeeks.mop.service.MopPluginService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -74,7 +77,22 @@ public class BaseModule extends BaseApi {
Boolean bindAppletWithMainProcess = (Boolean) param.get("bindAppletWithMainProcess"); Boolean bindAppletWithMainProcess = (Boolean) param.get("bindAppletWithMainProcess");
if (bindAppletWithMainProcess == null) bindAppletWithMainProcess = false; if (bindAppletWithMainProcess == null) bindAppletWithMainProcess = false;
FinAppConfig config = new FinAppConfig.Builder() String customWebViewUserAgent = (String) param.get("customWebViewUserAgent");
Integer appletIntervalUpdateLimit = (Integer) param.get("appletIntervalUpdateLimit");
Integer maxRunningApplet = (Integer) param.get("maxRunningApplet");
Gson gson = new Gson();
List<FinStoreConfig> finStoreConfigs = null;
if (param.get("finStoreConfigs") != null) {
finStoreConfigs = gson.fromJson(gson.toJson(param.get("finStoreConfigs")), new TypeToken<List<FinStoreConfig>>() {
}.getType());
}
FinAppConfig.UIConfig uiConfig = null;
if (param.get("uiConfig") != null) {
uiConfig = gson.fromJson(gson.toJson(param.get("uiConfig")), FinAppConfig.UIConfig.class);
}
FinAppConfig.Builder builder = new FinAppConfig.Builder()
.setSdkKey(appkey) .setSdkKey(appkey)
.setSdkSecret(secret) .setSdkSecret(secret)
.setApiUrl(apiServer) .setApiUrl(apiServer)
@ -84,8 +102,18 @@ public class BaseModule extends BaseApi {
.setUserId(userId) .setUserId(userId)
.setDebugMode(debug) .setDebugMode(debug)
.setDisableRequestPermissions(disablePermission) .setDisableRequestPermissions(disablePermission)
.setBindAppletWithMainProcess(bindAppletWithMainProcess) .setBindAppletWithMainProcess(bindAppletWithMainProcess);
.build();
if (customWebViewUserAgent != null)
builder.setCustomWebViewUserAgent(customWebViewUserAgent);
if (appletIntervalUpdateLimit != null)
builder.setAppletIntervalUpdateLimit(appletIntervalUpdateLimit);
if (maxRunningApplet != null) builder.setMaxRunningApplet(maxRunningApplet);
if (finStoreConfigs != null) builder.setFinStoreConfigs(finStoreConfigs);
if (uiConfig != null) builder.setUiConfig(uiConfig);
FinAppConfig config = builder.build();
Log.d(TAG, "config:" + gson.toJson(config));
final Application application = MopPluginService.getInstance().getActivity().getApplication(); final Application application = MopPluginService.getInstance().getActivity().getApplication();
// SDKSDK // SDKSDK

View File

@ -33,66 +33,126 @@ public class ExtensionApiModule extends BaseApi {
@Override @Override
public String[] apis() { public String[] apis() {
return new String[]{"registerExtensionApi"}; return new String[]{"registerExtensionApi","addWebExtentionApi"};
} }
@Override @Override
public void invoke(String s, Map param, ICallback iCallback) { public void invoke(String s, Map param, ICallback iCallback) {
MethodChannel channel = MopPluginService.getInstance().getMethodChannel(); if(s.equals("registerExtensionApi")) {
String name = (String) param.get("name"); MethodChannel channel = MopPluginService.getInstance().getMethodChannel();
FinAppClient.INSTANCE.getExtensionApiManager().registerApi(new com.finogeeks.lib.applet.api.BaseApi(getContext()) { String name = (String) param.get("name");
@Override Log.d(TAG, "registerExtensionApi:" + name);
public String[] apis() { FinAppClient.INSTANCE.getExtensionApiManager().registerApi(new com.finogeeks.lib.applet.api.BaseApi(getContext()) {
return new String[]{name}; @Override
} public String[] apis() {
return new String[]{name};
}
@Override @Override
public void invoke(String s, JSONObject jsonObject, com.finogeeks.lib.applet.interfaces.ICallback iCallback) { public void invoke(String s, JSONObject jsonObject, com.finogeeks.lib.applet.interfaces.ICallback iCallback) {
Log.d("MopPlugin", "invoke extensionApi:" + s + ",params:" + jsonObject); Log.d("MopPlugin", "invoke extensionApi:" + s + ",params:" + jsonObject);
Map params = GsonUtil.gson.fromJson(jsonObject.toString(), HashMap.class); Map params = GsonUtil.gson.fromJson(jsonObject.toString(), HashMap.class);
handler.post(() -> { handler.post(() -> {
channel.invokeMethod("extensionApi:" + name, params, new MethodChannel.Result() { channel.invokeMethod("extensionApi:" + name, params, new MethodChannel.Result() {
@Override @Override
public void success(Object result) { public void success(Object result) {
String json = GsonUtil.gson.toJson(result); String json = GsonUtil.gson.toJson(result);
FinAppTrace.d(ExtensionApiModule.TAG, "channel invokeMethod:" + name FinAppTrace.d(ExtensionApiModule.TAG, "channel invokeMethod:" + name
+ " success, result=" + result + ", json=" + json); + " success, result=" + result + ", json=" + json);
JSONObject ret = null; JSONObject ret = null;
if (json != null && !json.equals("null")) { if (json != null && !json.equals("null")) {
try { try {
ret = new JSONObject(json); ret = new JSONObject(json);
if (ret.has("errMsg")) { if (ret.has("errMsg")) {
String errMsg = ret.getString("errMsg"); String errMsg = ret.getString("errMsg");
if (errMsg.startsWith(name + ":fail")) { if (errMsg.startsWith(name + ":fail")) {
iCallback.onFail(ret); iCallback.onFail(ret);
return; return;
}
} }
} catch (JSONException e) {
e.printStackTrace();
} }
} catch (JSONException e) {
e.printStackTrace();
} }
iCallback.onSuccess(ret);
} }
iCallback.onSuccess(ret); @Override
} public void error(String errorCode, String errorMessage, Object errorDetails) {
FinAppTrace.e(ExtensionApiModule.TAG, "channel invokeMethod:" + name
+ " error, errorCode=" + errorCode
+ ", errorMessage=" + errorMessage
+ ", errorDetails=" + errorDetails);
iCallback.onFail();
}
@Override @Override
public void error(String errorCode, String errorMessage, Object errorDetails) { public void notImplemented() {
FinAppTrace.e(ExtensionApiModule.TAG, "channel invokeMethod:" + name iCallback.onFail();
+ " error, errorCode=" + errorCode }
+ ", errorMessage=" + errorMessage });
+ ", errorDetails=" + errorDetails);
iCallback.onFail();
}
@Override
public void notImplemented() {
iCallback.onFail();
}
}); });
}); }
} });
}); }else if(s.equals("addWebExtentionApi")){
MethodChannel channel = MopPluginService.getInstance().getMethodChannel();
String name = (String) param.get("name");
Log.d(TAG, "addWebExtentionApi:" + name);
FinAppClient.INSTANCE.getExtensionWebApiManager().registerApi(new com.finogeeks.lib.applet.api.BaseApi(getContext()) {
@Override
public String[] apis() {
return new String[]{name};
}
@Override
public void invoke(String s, JSONObject jsonObject, com.finogeeks.lib.applet.interfaces.ICallback iCallback) {
Log.d("MopPlugin", "invoke webextensionApi:" + s + ",params:" + jsonObject);
Map params = GsonUtil.gson.fromJson(jsonObject.toString(), HashMap.class);
handler.post(() -> {
channel.invokeMethod("webExtentionApi:" + name, params, new MethodChannel.Result() {
@Override
public void success(Object result) {
String json = GsonUtil.gson.toJson(result);
FinAppTrace.d(ExtensionApiModule.TAG, "channel invokeMethod:" + name
+ " success, result=" + result + ", json=" + json);
JSONObject ret = null;
if (json != null && !json.equals("null")) {
try {
ret = new JSONObject(json);
if (ret.has("errMsg")) {
String errMsg = ret.getString("errMsg");
if (errMsg.startsWith(name + ":fail")) {
iCallback.onFail(ret);
return;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
iCallback.onSuccess(ret);
}
@Override
public void error(String errorCode, String errorMessage, Object errorDetails) {
FinAppTrace.e(ExtensionApiModule.TAG, "channel invokeMethod:" + name
+ " error, errorCode=" + errorCode
+ ", errorMessage=" + errorMessage
+ ", errorDetails=" + errorDetails);
iCallback.onFail();
}
@Override
public void notImplemented() {
iCallback.onFail();
}
});
});
}
});
}
} }
} }

View File

@ -1 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"mop","path":"/Users/wangtao/Documents/fantai/code/finclip-flutter-sdk/","dependencies":[]}],"android":[{"name":"flutter_plugin_android_lifecycle","path":"/Users/wangtao/development/flutter/.pub-cache/hosted/pub.flutter-io.cn/flutter_plugin_android_lifecycle-2.0.5/","dependencies":[]},{"name":"mop","path":"/Users/wangtao/Documents/fantai/code/finclip-flutter-sdk/","dependencies":["flutter_plugin_android_lifecycle"]}],"macos":[],"linux":[],"windows":[],"web":[]},"dependencyGraph":[{"name":"flutter_plugin_android_lifecycle","dependencies":[]},{"name":"mop","dependencies":["flutter_plugin_android_lifecycle"]}],"date_created":"2021-12-28 10:57:42.321282","version":"2.8.1"} {"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"mop","path":"/Users/kangxuyao/StudioProjects/finclip-flutter-sdk/","dependencies":[]}],"android":[{"name":"flutter_plugin_android_lifecycle","path":"/Users/kangxuyao/.pub-cache/hosted/pub.flutter-io.cn/flutter_plugin_android_lifecycle-2.0.5/","dependencies":[]},{"name":"mop","path":"/Users/kangxuyao/StudioProjects/finclip-flutter-sdk/","dependencies":["flutter_plugin_android_lifecycle"]}],"macos":[],"linux":[],"windows":[],"web":[]},"dependencyGraph":[{"name":"flutter_plugin_android_lifecycle","dependencies":[]},{"name":"mop","dependencies":["flutter_plugin_android_lifecycle"]}],"date_created":"2022-01-04 13:47:58.706248","version":"2.6.0-12.0.pre.553"}

View File

@ -315,8 +315,8 @@ class Mop {
List<FinStoreConfig>? finStoreConfigs, List<FinStoreConfig>? finStoreConfigs,
UIConfig? uiConfig, UIConfig? uiConfig,
String? customWebViewUserAgent, String? customWebViewUserAgent,
int appletIntervalUpdateLimit = 0, int? appletIntervalUpdateLimit,
int maxRunningApplet = 5, int? maxRunningApplet,
}) async { }) async {
List<Map<String, dynamic>>? storeConfigs = List<Map<String, dynamic>>? storeConfigs =
finStoreConfigs?.map((e) => e.toMap()).toList(); finStoreConfigs?.map((e) => e.toMap()).toList();