sync method and clipdata
parent
88ba453230
commit
4cfdbafcbf
|
@ -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
|
||||
|
|
|
@ -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 + "}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -33,11 +33,13 @@ 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) {
|
||||
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");
|
||||
|
@ -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() {
|
||||
|
@ -87,6 +89,63 @@ public class ExtensionApiModule extends BaseApi {
|
|||
iCallback.onFail();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notImplemented() {
|
||||
iCallback.onFail();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
} 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();
|
||||
|
|
|
@ -12,6 +12,8 @@
|
|||
|
||||
- (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) {
|
||||
failure(@"config不能为空");
|
||||
return;
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#import "MOP_initialize.h"
|
||||
#import <FinApplet/FinApplet.h>
|
||||
#import <FinAppletExt/FinAppletExt.h>
|
||||
#import <FinAppletClipBoard/FinAppletClipBoard.h>
|
||||
#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(@{});
|
||||
|
||||
|
|
|
@ -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
|
|
@ -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
|
18
lib/mop.dart
18
lib/mop.dart
|
@ -10,6 +10,7 @@ typedef MopEventErrorCallback = void Function(dynamic event);
|
|||
|
||||
typedef ExtensionApiHandler = Future<Map<String, dynamic>> Function(
|
||||
dynamic params);
|
||||
typedef SyncExtensionApiHandler = Map<String, dynamic> 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) {
|
||||
|
|
Loading…
Reference in New Issue