实现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;
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.db.entity.FinApplet;
import com.finogeeks.lib.applet.interfaces.FinCallback;
import com.finogeeks.mop.api.BaseApi;
import com.finogeeks.mop.interfaces.ICallback;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.Map;
@ -13,13 +22,16 @@ import java.util.Map;
public class AppletManageModule extends BaseApi {
private static final String TAG = "AppletManageModule";
public AppletManageModule(Context context) {
super(context);
}
@Override
public String[] apis() {
return new String[]{"currentApplet", "closeAllApplets", "clearApplets", "removeUsedApplet"};
return new String[]{"currentApplet", "closeAllApplets", "clearApplets", "removeUsedApplet",
"setActivityTransitionAnim", "sendCustomEvent", "callJS"};
}
@Override
@ -57,6 +69,70 @@ public class AppletManageModule extends BaseApi {
} else {
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 com.finogeeks.lib.applet.client.FinAppClient;
import com.finogeeks.lib.applet.client.FinAppInfo;
import com.finogeeks.lib.applet.sdk.model.StartAppletDecryptRequest;
import com.finogeeks.mop.api.BaseApi;
import com.finogeeks.mop.interfaces.ICallback;
@ -53,19 +54,20 @@ public class AppletModule extends BaseApi {
String appId = String.valueOf(param.get("appId"));
Integer sequence = (Integer) param.get("sequence");
Map<String, String> params = (Map) param.get("params");
String apiServer = (String) param.get("apiServer");
// mContextFlutterActivity
// Android 6.07.0contextmContext
// 使Application Context
Context context = mContext.getApplicationContext();
if (params == null) {
if (sequence == null) {
FinAppClient.INSTANCE.getAppletApiManager().startApplet(context, appId);
FinAppInfo.StartParams startParams = params == null ? null : new FinAppInfo.StartParams(params.get("path"), params.get("query"), params.get("scene"));
Log.d(TAG, "openApplet:" + appId + "," + param + "," + sequence + "," + apiServer);
if (apiServer != null) {
FinAppClient.INSTANCE.getAppletApiManager().startApplet(context, apiServer, appId, sequence, startParams);
} else {
FinAppClient.INSTANCE.getAppletApiManager().startApplet(context, appId, sequence, null);
}
} else {
FinAppClient.INSTANCE.getAppletApiManager().startApplet(context, appId, params);
FinAppClient.INSTANCE.getAppletApiManager().startApplet(context, appId, sequence, startParams);
}
// String apiServer = (String) param.get("apiServer");
// String apiPrefix = (String) param.get("apiPrefix");
// 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.content.Context;
import android.util.Log;
import com.finogeeks.lib.applet.BuildConfig;
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.interfaces.ICallback;
import com.finogeeks.mop.service.MopPluginService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.ArrayList;
import java.util.List;
@ -74,7 +77,22 @@ public class BaseModule extends BaseApi {
Boolean bindAppletWithMainProcess = (Boolean) param.get("bindAppletWithMainProcess");
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)
.setSdkSecret(secret)
.setApiUrl(apiServer)
@ -84,8 +102,18 @@ public class BaseModule extends BaseApi {
.setUserId(userId)
.setDebugMode(debug)
.setDisableRequestPermissions(disablePermission)
.setBindAppletWithMainProcess(bindAppletWithMainProcess)
.build();
.setBindAppletWithMainProcess(bindAppletWithMainProcess);
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();
// SDKSDK

View File

@ -33,13 +33,15 @@ public class ExtensionApiModule extends BaseApi {
@Override
public String[] apis() {
return new String[]{"registerExtensionApi"};
return new String[]{"registerExtensionApi","addWebExtentionApi"};
}
@Override
public void invoke(String s, Map param, ICallback iCallback) {
if(s.equals("registerExtensionApi")) {
MethodChannel channel = MopPluginService.getInstance().getMethodChannel();
String name = (String) param.get("name");
Log.d(TAG, "registerExtensionApi:" + name);
FinAppClient.INSTANCE.getExtensionApiManager().registerApi(new com.finogeeks.lib.applet.api.BaseApi(getContext()) {
@Override
public String[] apis() {
@ -93,6 +95,64 @@ public class ExtensionApiModule extends BaseApi {
});
}
});
}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,
UIConfig? uiConfig,
String? customWebViewUserAgent,
int appletIntervalUpdateLimit = 0,
int maxRunningApplet = 5,
int? appletIntervalUpdateLimit,
int? maxRunningApplet,
}) async {
List<Map<String, dynamic>>? storeConfigs =
finStoreConfigs?.map((e) => e.toMap()).toList();