1010import android .graphics .drawable .StateListDrawable ;
1111import android .media .Image ;
1212import android .nfc .Tag ;
13+ import android .os .Handler ;
14+ import android .os .HandlerThread ;
15+ import android .os .Looper ;
1316import android .util .Log ;
1417import android .util .TypedValue ;
1518import android .view .Gravity ;
4346import io .flutter .plugin .common .MethodChannel .Result ;
4447import io .flutter .plugin .common .PluginRegistry .Registrar ;
4548
49+ class JVRequestItem {
50+ public MethodCall call ;
51+ Result result ;
52+ }
53+
4654/** JverifyPlugin */
4755public class JverifyPlugin implements MethodCallHandler {
4856
@@ -60,10 +68,13 @@ public class JverifyPlugin implements MethodCallHandler {
6068 private static String j_opr_key = "operator" ;
6169 // 默认超时时间
6270 private static int j_default_timeout = 5000 ;
71+ // 重复请求
72+ private static int j_error_code_repeat = -1 ;
6373
6474
6575 private Context context ;
6676 private MethodChannel channel ;
77+ private HashMap <String ,JVRequestItem > requestQueue = new HashMap ();
6778
6879 /** Plugin registration. */
6980 public static void registerWith (Registrar registrar ) {
@@ -81,6 +92,29 @@ private JverifyPlugin(Registrar registrar,MethodChannel channel){
8192 public void onMethodCall (MethodCall call , Result result ) {
8293 Log .d (TAG ,"onMethodCall:" + call .method );
8394
95+ // JVRequestItem item = requestQueue.get(call.method);
96+ // if (item == null) {
97+ // item = new JVRequestItem();
98+ // item.call = call;
99+ // item.result = result;
100+ //
101+ // requestQueue.put(call.method,item);
102+ //
103+ // processMethod(call, result);
104+ // }else {
105+ // String error_repeat_desc = call.method + " is requesting, please try again later.";
106+ //
107+ // Map<String,Object> map = new HashMap<>();
108+ // map.put(j_code_key,j_error_code_repeat);
109+ // map.put(j_msg_key,error_repeat_desc);
110+ //
111+ // result.success(map);
112+ // }
113+ processMethod (call ,result );
114+ }
115+
116+ private void processMethod (MethodCall call , Result result ) {
117+ Log .d (TAG ,"processMethod:" + call .method );
84118 if (call .method .equals ("setup" )) {
85119 setup (call ,result );
86120 }else if (call .method .equals ("setDebugMode" )) {
@@ -115,6 +149,34 @@ public void onMethodCall(MethodCall call, Result result) {
115149 }
116150 }
117151
152+ private void methodCallBack (Object object ,String method ) {
153+ Log .d (TAG ,"Action - methodCallBack:" + method );
154+
155+ JVRequestItem item = requestQueue .get (method );
156+ if (item != null ) {
157+ if (item .result != null ) {
158+ item .result .success (object );
159+ }
160+ }
161+ requestQueue .remove (method );
162+ }
163+
164+ // 主线程再返回数据
165+ private void runMainThread (final Map <String ,Object > map , final Result result , final String method ) {
166+ android .os .Handler handler = new Handler (Looper .getMainLooper ());
167+ handler .post (new Runnable () {
168+ @ Override
169+ public void run () {
170+ if (result == null && method != null ){
171+ channel .invokeMethod (method ,map );
172+ } else {
173+ result .success (map );
174+ }
175+ }
176+ });
177+ }
178+
179+
118180 /** SDK 初始换 */
119181 private void setup (MethodCall call ,Result result ){
120182 Log .d (TAG ,"Action - setup:" );
@@ -127,7 +189,8 @@ public void onResult(int code, String message) {
127189 map .put (j_code_key ,code );
128190 map .put (j_msg_key ,message );
129191 // 通过 channel 返回
130- channel .invokeMethod ("onReceiveSDKSetupCallBackEvent" ,map );
192+ //channel.invokeMethod("onReceiveSDKSetupCallBackEvent",map);
193+ runMainThread (map ,null ,"onReceiveSDKSetupCallBackEvent" );
131194 }
132195 });
133196 }
@@ -142,7 +205,7 @@ private void setDebugMode(MethodCall call,Result result){
142205
143206 Map <String ,Object > map = new HashMap <>();
144207 map .put (j_result_key ,enable );
145- result . success (map );
208+ runMainThread (map , result , null );
146209 }
147210
148211 /** 获取 SDK 初始化是否成功标识 */
@@ -155,7 +218,7 @@ private boolean isInitSuccess(MethodCall call, Result result) {
155218
156219 Map <String ,Object > map = new HashMap <>();
157220 map .put (j_result_key , isSuccess );
158- result . success (map );
221+ runMainThread (map , result , null );
159222
160223 return isSuccess ;
161224 }
@@ -171,13 +234,13 @@ private boolean checkVerifyEnable(MethodCall call,Result result){
171234
172235 Map <String ,Object > map = new HashMap <>();
173236 map .put (j_result_key , verifyEnable );
174- result . success (map );
237+ runMainThread (map , result , null );
175238
176239 return verifyEnable ;
177240 }
178241
179242 /** SDK获取号码认证token*/
180- private void getToken (MethodCall call , final Result result ) {
243+ private void getToken (final MethodCall call , final Result result ) {
181244 Log .d (TAG ,"Action - getToken:" );
182245
183246 int timeOut = j_default_timeout ;
@@ -206,7 +269,8 @@ public void onResult(int code, String content, String operator) {
206269 map .put (j_code_key ,code );
207270 map .put (j_msg_key ,content );
208271 map .put (j_opr_key ,operator );
209- result .success (map );
272+
273+ runMainThread (map ,result ,null );
210274 }
211275 });
212276 }
@@ -251,7 +315,7 @@ public void onResult(int code, String content, String operator) {
251315 }
252316
253317 /** SDK 一键登录预取号 */
254- private void preLogin (MethodCall call ,final Result result ) {
318+ private void preLogin (final MethodCall call , final Result result ) {
255319 Log .d (TAG ,"Action - preLogin:" + call .arguments );
256320
257321 int timeOut = j_default_timeout ;
@@ -265,19 +329,21 @@ private void preLogin(MethodCall call,final Result result) {
265329 public void onResult (int code , String message ) {
266330
267331 if (code == 7000 ){//code: 返回码,7000代表获取成功,其他为失败,详见错误码描述
268- Log .d (TAG , "verify consistent , message =" + message );
332+ Log .d (TAG , "verify success , message =" + message );
269333 }else {
270- Log .e (TAG , "code=" + code + ", message =" + message );
334+ Log .e (TAG , "verify fail, code=" + code + ", message =" + message );
271335 }
272-
273336 Map <String ,Object > map = new HashMap <>();
274337 map .put (j_code_key ,code );
275338 map .put (j_msg_key ,message );
276- result .success (map );
339+
340+ runMainThread (map ,result ,null );
277341 }
278342 });
279343 }
280344
345+
346+
281347 /** SDK清除预取号缓存 */
282348 private void clearPreLoginCache (MethodCall call ,final Result result ) {
283349 Log .d (TAG ,"Action - clearPreLoginCache:" );
@@ -295,7 +361,7 @@ private void loginAuthSyncApi(MethodCall call,final Result result) {
295361 Log .d (TAG ,"Action - loginAuthSyncApi:" );
296362 loginAuthInterface (true ,call ,result );
297363 }
298- private void loginAuthInterface (final Boolean isSync , MethodCall call , final Result result ) {
364+ private void loginAuthInterface (final Boolean isSync , final MethodCall call , final Result result ) {
299365 Log .d (TAG ,"Action - loginAuthInterface:" );
300366
301367 Object autoFinish = getValueByKey (call ,"autoDismiss" );
@@ -312,7 +378,8 @@ public void onEvent(int cmd, String msg) {
312378 final HashMap jsonMap = new HashMap ();
313379 jsonMap .put (j_code_key , cmd );
314380 jsonMap .put (j_msg_key , msg );
315- channel .invokeMethod ("onReceiveAuthPageEvent" , jsonMap );
381+
382+ runMainThread (jsonMap ,null ,"onReceiveAuthPageEvent" );
316383 }
317384 });
318385
@@ -330,10 +397,10 @@ public void onResult(int code, String content, String operator) {
330397 map .put (j_opr_key ,operator );
331398 if (isSync ) {
332399 // 通过 channel 返回
333- channel . invokeMethod ( "onReceiveLoginAuthCallBackEvent" , map );
400+ runMainThread ( map , null , "onReceiveLoginAuthCallBackEvent" );
334401 }else {
335402 // 通过回调返回
336- result . success (map );
403+ runMainThread (map , result , null );
337404 }
338405 }
339406 });
@@ -928,7 +995,7 @@ private void addCustomButtonWidgets(Map para, JVerifyUIConfig.Builder builder)
928995 @ Override
929996 public void onClicked (Context context , View view ) {
930997 Log .d (TAG ,"onClicked button widget." );
931- channel . invokeMethod ( "onReceiveClickWidgetEvent" , jsonMap );
998+ runMainThread ( jsonMap , null , "onReceiveClickWidgetEvent" );
932999 }
9331000 });
9341001 }
0 commit comments