DSBridge-Android icon indicating copy to clipboard operation
DSBridge-Android copied to clipboard

Some potencial ANR issue located in handling JSON

Open zengqiang24 opened this issue 4 years ago • 1 comments

Hi, Thank you for this awsome project. I just got a question for you. Does these json code call from Main thread? Maybe it causes performance issue.

` @Keep private void addInternalJavascriptObject() { addJavascriptObject(new Object() {

        @Keep
        @JavascriptInterface
        public boolean hasNativeMethod(Object args) throws JSONException {
            JSONObject jsonObject = (JSONObject) args;
            String methodName = jsonObject.getString("name").trim();
            String type = jsonObject.getString("type").trim();
            String[] nameStr = parseNamespace(methodName);
            Object jsb = javaScriptNamespaceInterfaces.get(nameStr[0]);
            if (jsb != null) {
                Class<?> cls = jsb.getClass();
                boolean asyn = false;
                Method method = null;
                try {
                    method = cls.getMethod(nameStr[1],
                            new Class[]{Object.class, CompletionHandler.class});
                    asyn = true;
                } catch (Exception e) {
                    try {
                        method = cls.getMethod(nameStr[1], new Class[]{Object.class});
                    } catch (Exception ex) {

                    }
                }
                if (method != null) {
                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
                        JavascriptInterface annotation = method.getAnnotation(JavascriptInterface.class);
                        if (annotation == null) {
                            return false;
                        }
                    }
                    if ("all".equals(type) || (asyn && "asyn".equals(type) || (!asyn && "syn".equals(type)))) {
                        return true;
                    }

                }
            }
            return false;
        }

`

zengqiang24 avatar Mar 25 '20 10:03 zengqiang24

@zengqiang24 This method is called from JavaBridge thread not main thread

shaobin0604 avatar Oct 27 '20 07:10 shaobin0604