From 4cfdbafcbf3ac882eb493216c263f591c1739527 Mon Sep 17 00:00:00 2001 From: "simpleman1984@126.com" Date: Wed, 2 Aug 2023 17:31:33 +0800 Subject: [PATCH] sync method and clipdata --- .../gradle/wrapper/gradle-wrapper.properties | 2 +- .../java/com/finogeeks/mop/api/SyncApi.java | 46 +++++++++++++ .../mop/api/mop/ExtensionApiModule.java | 67 +++++++++++++++++-- ios/Classes/Api/MOP_initSDK.m | 2 + ios/Classes/Api/MOP_initialize.m | 4 +- .../Api/MOP_registerSyncExtensionApi.h | 18 +++++ .../Api/MOP_registerSyncExtensionApi.m | 31 +++++++++ lib/mop.dart | 18 ++++- 8 files changed, 181 insertions(+), 7 deletions(-) create mode 100644 android/src/main/java/com/finogeeks/mop/api/SyncApi.java create mode 100644 ios/Classes/Api/MOP_registerSyncExtensionApi.h create mode 100644 ios/Classes/Api/MOP_registerSyncExtensionApi.m diff --git a/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties index 3c9d085..ffed3a2 100644 --- a/android/gradle/wrapper/gradle-wrapper.properties +++ b/android/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip diff --git a/android/src/main/java/com/finogeeks/mop/api/SyncApi.java b/android/src/main/java/com/finogeeks/mop/api/SyncApi.java new file mode 100644 index 0000000..093d245 --- /dev/null +++ b/android/src/main/java/com/finogeeks/mop/api/SyncApi.java @@ -0,0 +1,46 @@ +// The code is already well written and doesn't need any improvement. +// However, I will add some comments to make it more readable. + +package com.finogeeks.mop.api; + +import android.content.Context; +import androidx.annotation.Nullable; +import org.json.JSONException; +import org.json.JSONObject; + +public abstract class SyncApi extends BaseApi { + public SyncApi(Context context) { + super(context); + } + + // This method is used to invoke the API. + @Nullable + public abstract String invoke(String url, JSONObject jsonObject); + + // This method is used to get the success response. + public JSONObject getSuccessRes(String message) { + JSONObject jsonObject = new JSONObject(); + String key = "errMsg"; + try { + jsonObject.put(key, message + ":ok"); + return jsonObject; + } catch (JSONException e) { + e.printStackTrace(); + return jsonObject; + } + } + + // This method is used to get the failure response. + public String getFailureRes(String message, String error) { + JSONObject jsonObject = new JSONObject(); + String key = "errMsg"; + try { + return jsonObject.put(key, message + ":fail " + error).toString(); + } catch (JSONException e) { + e.printStackTrace(); + return "{\"errMsg\":" + message + "\":fail \"" + error + "}"; + } + } +} + + diff --git a/android/src/main/java/com/finogeeks/mop/api/mop/ExtensionApiModule.java b/android/src/main/java/com/finogeeks/mop/api/mop/ExtensionApiModule.java index 6f8bac6..30cedd5 100644 --- a/android/src/main/java/com/finogeeks/mop/api/mop/ExtensionApiModule.java +++ b/android/src/main/java/com/finogeeks/mop/api/mop/ExtensionApiModule.java @@ -33,12 +33,14 @@ public class ExtensionApiModule extends BaseApi { @Override public String[] apis() { - return new String[]{"registerExtensionApi","addWebExtentionApi"}; + return new String[]{"registerExtensionApi", "registerSyncExtensionApi", "addWebExtentionApi"}; } @Override public void invoke(String s, Map param, ICallback iCallback) { - if(s.equals("registerExtensionApi")) { + boolean isFinAppProcess = FinAppClient.INSTANCE.isFinAppProcess(getContext()); + Log.d(TAG, "ExtensionApiModule invoke register api s:" + s + " param:" + param +" isFinAppProcess:"+isFinAppProcess); + if (s.equals("registerExtensionApi")) { MethodChannel channel = MopPluginService.getInstance().getMethodChannel(); String name = (String) param.get("name"); Log.d(TAG, "registerExtensionApi:" + name); @@ -50,7 +52,7 @@ public class ExtensionApiModule extends BaseApi { @Override 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+" isFinAppProcess:"+isFinAppProcess); Map params = GsonUtil.gson.fromJson(jsonObject.toString(), HashMap.class); handler.post(() -> { channel.invokeMethod("extensionApi:" + name, params, new MethodChannel.Result() { @@ -95,7 +97,64 @@ public class ExtensionApiModule extends BaseApi { }); } }); - }else if(s.equals("addWebExtentionApi")){ + } else if (s.equals("registerSyncExtensionApi")) { + MethodChannel channel = MopPluginService.getInstance().getMethodChannel(); + String name = (String) param.get("name"); + Log.d(TAG, "registerSyncExtensionApi:" + name); + FinAppClient.INSTANCE.getExtensionApiManager().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", "sync invoke extensionApi:" + s + ",params:" + jsonObject); + Map params = GsonUtil.gson.fromJson(jsonObject.toString(), HashMap.class); + handler.post(() -> { + channel.invokeMethod("syncextensionapi:" + name, params, new MethodChannel.Result() { + @Override + public void success(Object result) { + String json = GsonUtil.gson.toJson(result); + Log.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) { + FLog.e(ExtensionApiModule.TAG, "channel invokeMethod:" + name + + " 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); diff --git a/ios/Classes/Api/MOP_initSDK.m b/ios/Classes/Api/MOP_initSDK.m index a74edf4..435a228 100644 --- a/ios/Classes/Api/MOP_initSDK.m +++ b/ios/Classes/Api/MOP_initSDK.m @@ -12,6 +12,8 @@ - (void)setupApiWithSuccess:(void (^)(NSDictionary * _Nonnull))success failure:(void (^)(id _Nullable))failure cancel:(void (^)(void))cancel { + //google map key + [[FATExtClient sharedClient] registerGoogleMapService:@"AIzaSyC8gNBH-AwNLY9rxtr5-f178dBZhp0Ww3Y" placesKey:@"AIzaSyC8gNBH-AwNLY9rxtr5-f178dBZhp0Ww3Y"]; if (!self.config) { failure(@"config不能为空"); return; diff --git a/ios/Classes/Api/MOP_initialize.m b/ios/Classes/Api/MOP_initialize.m index ef11dc4..cc6fd5f 100644 --- a/ios/Classes/Api/MOP_initialize.m +++ b/ios/Classes/Api/MOP_initialize.m @@ -8,6 +8,7 @@ #import "MOP_initialize.h" #import #import +#import #import "MOPTools.h" @implementation MOP_initialize @@ -126,8 +127,9 @@ // [[FATExtClient sharedClient] fat_prepareExtensionApis]; [[FATClient sharedClient].logManager initLogWithLogDir:nil logLevel:FATLogLevelVerbose consoleLog:YES]; - [[FATClient sharedClient] setEnableLog:YES]; + // ClipBoard初始化 + [FATClipBoardComponent registerComponent]; success(@{}); diff --git a/ios/Classes/Api/MOP_registerSyncExtensionApi.h b/ios/Classes/Api/MOP_registerSyncExtensionApi.h new file mode 100644 index 0000000..741ec63 --- /dev/null +++ b/ios/Classes/Api/MOP_registerSyncExtensionApi.h @@ -0,0 +1,18 @@ +// +// MOP_registerSyncExtensionApi.h +// mop +// +// Created by Stewen on 2023/6/30. +// + +#import "MOPBaseApi.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface MOP_registerSyncExtensionApi : MOPBaseApi + +@property(nonatomic, copy) NSString* name; + +@end + +NS_ASSUME_NONNULL_END diff --git a/ios/Classes/Api/MOP_registerSyncExtensionApi.m b/ios/Classes/Api/MOP_registerSyncExtensionApi.m new file mode 100644 index 0000000..c928d25 --- /dev/null +++ b/ios/Classes/Api/MOP_registerSyncExtensionApi.m @@ -0,0 +1,31 @@ +// +// MOP_registerSyncExtensionApi.m +// mop +// +// Created by Stewen on 2023/6/30. +// + +#import "MOP_registerSyncExtensionApi.h" +#import "MopPlugin.h" +#import + +@implementation MOP_registerSyncExtensionApi + +- (void)setupApiWithSuccess:(void (^)(NSDictionary * _Nonnull))success failure:(void (^)(id _Nullable))failure cancel:(void (^)(void))cancel +{ + NSLog(@"MOP_registerSyncExtensionApi,name=%@",self.name); + FlutterMethodChannel *channel = [[MopPlugin instance] methodChannel]; + [[FATClient sharedClient] registerSyncExtensionApi:self.name handler:^NSDictionary *(FATAppletInfo *appletInfo, id param) { + if([self.name isEqualToString:@"getLanguageCodeSync"]){ + NSString *languageCode = [[NSLocale preferredLanguages] firstObject]; + NSString *shortCode = [[NSLocale componentsFromLocaleIdentifier:languageCode] objectForKey:NSLocaleLanguageCode]; + NSString *countryCode = [NSString stringWithFormat:@"%@", [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode]]; + NSDictionary *resultDict = @{@"languageCode":shortCode,@"countryCode":countryCode}; + return resultDict; + } + return @{}; + }]; + success(@{}); +} + +@end diff --git a/lib/mop.dart b/lib/mop.dart index 073ea19..eb42e08 100644 --- a/lib/mop.dart +++ b/lib/mop.dart @@ -10,6 +10,7 @@ typedef MopEventErrorCallback = void Function(dynamic event); typedef ExtensionApiHandler = Future> Function( dynamic params); +typedef SyncExtensionApiHandler = Map Function(dynamic params); typedef MopAppletHandler = Future Function(dynamic params); class FinStoreConfig { @@ -964,6 +965,14 @@ class Mop { if (apiHandler != null) { return await apiHandler(call.arguments); } + } else if (call.method.toLowerCase().startsWith("syncextensionapi:")) { + final name = + call.method.substring("syncextensionapi:".length); + final handler = _syncExtensionApis[name]; + debugPrint("syncExtensionApisName:$name,handler:$handler"); + if (handler != null) { + return handler(call.arguments); + } } else if (call.method.startsWith("webExtentionApi:")) { final name = call.method.substring("webExtentionApi:".length); final handler = _webExtensionApis[name]; @@ -1063,7 +1072,7 @@ class Mop { Map param = {}; if (path != null) param["path"] = path; if (query != null) param["query"] = query; - if (param.length > 0) params["params"] = param; + if (param.isNotEmpty) params["params"] = param; if (sequence != null) params["sequence"] = sequence; if (apiServer != null) params["apiServer"] = apiServer; if (scene != null) param["scene"] = scene; @@ -1206,6 +1215,13 @@ class Mop { _channel.invokeMethod("registerExtensionApi", {"name": name}); } + /// register sync extension api + /// 注册拓展api(同步) + void registerSyncExtensionApi(String name, SyncExtensionApiHandler handler) { + _syncExtensionApis[name] = handler; + _channel.invokeMethod("registerSyncExtensionApi", {"name": name}); + } + /// register webview extension api /// 注册webview拓展api void addWebExtentionApi(String name, ExtensionApiHandler handler) {