sync method and clipdata

phiz_2.41.5.1
simpleman1984@126.com 2023-08-02 17:31:33 +08:00
parent 88ba453230
commit 4cfdbafcbf
8 changed files with 181 additions and 7 deletions

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip

View File

@ -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 + "}";
}
}
}

View File

@ -33,12 +33,14 @@ public class ExtensionApiModule extends BaseApi {
@Override @Override
public String[] apis() { public String[] apis() {
return new String[]{"registerExtensionApi","addWebExtentionApi"}; return new String[]{"registerExtensionApi", "registerSyncExtensionApi", "addWebExtentionApi"};
} }
@Override @Override
public void invoke(String s, Map param, ICallback iCallback) { 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(); MethodChannel channel = MopPluginService.getInstance().getMethodChannel();
String name = (String) param.get("name"); String name = (String) param.get("name");
Log.d(TAG, "registerExtensionApi:" + name); Log.d(TAG, "registerExtensionApi:" + name);
@ -50,7 +52,7 @@ public class ExtensionApiModule extends BaseApi {
@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+" isFinAppProcess:"+isFinAppProcess);
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() {
@ -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(); MethodChannel channel = MopPluginService.getInstance().getMethodChannel();
String name = (String) param.get("name"); String name = (String) param.get("name");
Log.d(TAG, "addWebExtentionApi:" + name); Log.d(TAG, "addWebExtentionApi:" + name);

View File

@ -12,6 +12,8 @@
- (void)setupApiWithSuccess:(void (^)(NSDictionary<NSString *,id> * _Nonnull))success failure:(void (^)(id _Nullable))failure cancel:(void (^)(void))cancel - (void)setupApiWithSuccess:(void (^)(NSDictionary<NSString *,id> * _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) { if (!self.config) {
failure(@"config不能为空"); failure(@"config不能为空");
return; return;

View File

@ -8,6 +8,7 @@
#import "MOP_initialize.h" #import "MOP_initialize.h"
#import <FinApplet/FinApplet.h> #import <FinApplet/FinApplet.h>
#import <FinAppletExt/FinAppletExt.h> #import <FinAppletExt/FinAppletExt.h>
#import <FinAppletClipBoard/FinAppletClipBoard.h>
#import "MOPTools.h" #import "MOPTools.h"
@implementation MOP_initialize @implementation MOP_initialize
@ -126,8 +127,9 @@
// [[FATExtClient sharedClient] fat_prepareExtensionApis]; // [[FATExtClient sharedClient] fat_prepareExtensionApis];
[[FATClient sharedClient].logManager initLogWithLogDir:nil logLevel:FATLogLevelVerbose consoleLog:YES]; [[FATClient sharedClient].logManager initLogWithLogDir:nil logLevel:FATLogLevelVerbose consoleLog:YES];
[[FATClient sharedClient] setEnableLog:YES]; [[FATClient sharedClient] setEnableLog:YES];
// ClipBoard
[FATClipBoardComponent registerComponent];
success(@{}); success(@{});

View File

@ -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

View File

@ -0,0 +1,31 @@
//
// MOP_registerSyncExtensionApi.m
// mop
//
// Created by Stewen on 2023/6/30.
//
#import "MOP_registerSyncExtensionApi.h"
#import "MopPlugin.h"
#import <FinApplet/FinApplet.h>
@implementation MOP_registerSyncExtensionApi
- (void)setupApiWithSuccess:(void (^)(NSDictionary<NSString *,id> * _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

View File

@ -10,6 +10,7 @@ typedef MopEventErrorCallback = void Function(dynamic event);
typedef ExtensionApiHandler = Future<Map<String, dynamic>> Function( typedef ExtensionApiHandler = Future<Map<String, dynamic>> Function(
dynamic params); dynamic params);
typedef SyncExtensionApiHandler = Map<String, dynamic> Function(dynamic params);
typedef MopAppletHandler = Future Function(dynamic params); typedef MopAppletHandler = Future Function(dynamic params);
class FinStoreConfig { class FinStoreConfig {
@ -964,6 +965,14 @@ class Mop {
if (apiHandler != null) { if (apiHandler != null) {
return await apiHandler(call.arguments); 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:")) { } else if (call.method.startsWith("webExtentionApi:")) {
final name = call.method.substring("webExtentionApi:".length); final name = call.method.substring("webExtentionApi:".length);
final handler = _webExtensionApis[name]; final handler = _webExtensionApis[name];
@ -1063,7 +1072,7 @@ class Mop {
Map param = {}; Map param = {};
if (path != null) param["path"] = path; if (path != null) param["path"] = path;
if (query != null) param["query"] = query; 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 (sequence != null) params["sequence"] = sequence;
if (apiServer != null) params["apiServer"] = apiServer; if (apiServer != null) params["apiServer"] = apiServer;
if (scene != null) param["scene"] = scene; if (scene != null) param["scene"] = scene;
@ -1206,6 +1215,13 @@ class Mop {
_channel.invokeMethod("registerExtensionApi", {"name": name}); _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 /// register webview extension api
/// webviewapi /// webviewapi
void addWebExtentionApi(String name, ExtensionApiHandler handler) { void addWebExtentionApi(String name, ExtensionApiHandler handler) {