Search in sources :

Example 51 with JSMethod

use of com.taobao.weex.annotation.JSMethod in project weex-example by KalicyZhou.

the class WXNavigatorModule method open.

@JSMethod(uiThread = true)
public void open(JSONObject options, JSCallback success, JSCallback failure) {
    if (options != null) {
        String url = options.getString(Constants.Value.URL);
        JSCallback callback = success;
        JSONObject result = new JSONObject();
        if (!TextUtils.isEmpty(url)) {
            Uri rawUri = Uri.parse(url);
            String scheme = rawUri.getScheme();
            if (TextUtils.isEmpty(scheme) || Constants.Scheme.HTTP.equalsIgnoreCase(scheme) || Constants.Scheme.HTTPS.equalsIgnoreCase(scheme)) {
                this.push(options.toJSONString(), success);
            } else {
                try {
                    Intent intent = new Intent(Intent.ACTION_VIEW, rawUri);
                    mWXSDKInstance.getContext().startActivity(intent);
                    result.put(CALLBACK_RESULT, MSG_SUCCESS);
                } catch (Throwable e) {
                    e.printStackTrace();
                    result.put(CALLBACK_RESULT, MSG_FAILED);
                    result.put(CALLBACK_MESSAGE, "Open page failed.");
                    callback = failure;
                }
            }
        } else {
            result.put(CALLBACK_RESULT, MSG_PARAM_ERR);
            result.put(CALLBACK_MESSAGE, "The URL parameter is empty.");
            callback = failure;
        }
        if (callback != null) {
            callback.invoke(result);
        }
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) Intent(android.content.Intent) Uri(android.net.Uri) JSCallback(com.taobao.weex.bridge.JSCallback) JSMethod(com.taobao.weex.annotation.JSMethod)

Example 52 with JSMethod

use of com.taobao.weex.annotation.JSMethod in project weex-example by KalicyZhou.

the class WXNavigatorModule method setNavBarHidden.

@JSMethod
public void setNavBarHidden(String param, final String callback) {
    String message = MSG_FAILED;
    try {
        JSONObject jsObj = JSON.parseObject(param);
        int visibility = jsObj.getInteger(Constants.Name.NAV_BAR_VISIBILITY);
        boolean success = changeVisibilityOfActionBar(mWXSDKInstance.getContext(), visibility);
        if (success) {
            message = MSG_SUCCESS;
        }
    } catch (JSONException e) {
        WXLogUtils.e(TAG, WXLogUtils.getStackTrace(e));
    }
    WXBridgeManager.getInstance().callback(mWXSDKInstance.getInstanceId(), callback, message);
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JSONException(com.alibaba.fastjson.JSONException) JSMethod(com.taobao.weex.annotation.JSMethod)

Example 53 with JSMethod

use of com.taobao.weex.annotation.JSMethod in project weex-example by KalicyZhou.

the class WXClipboardModule method setString.

@Override
@JSMethod
public void setString(String text) {
    if (null == text) {
        return;
    }
    Context context = mWXSDKInstance.getContext();
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(context.CLIPBOARD_SERVICE);
    ClipData clip = ClipData.newPlainText(CLIP_KEY, text);
    clipboard.setPrimaryClip(clip);
}
Also used : Context(android.content.Context) ClipboardManager(android.content.ClipboardManager) ClipData(android.content.ClipData) JSMethod(com.taobao.weex.annotation.JSMethod)

Example 54 with JSMethod

use of com.taobao.weex.annotation.JSMethod in project weex-example by KalicyZhou.

the class WXModalUIModule method prompt.

@JSMethod(uiThread = true)
public void prompt(String param, final JSCallback callback) {
    if (mWXSDKInstance.getContext() instanceof Activity) {
        String message = "";
        String defaultValue = "";
        String okTitle = OK;
        String cancelTitle = CANCEL;
        if (!TextUtils.isEmpty(param)) {
            try {
                param = URLDecoder.decode(param, "utf-8");
                JSONObject jsObj = JSON.parseObject(param);
                message = jsObj.getString(MESSAGE);
                okTitle = jsObj.getString(OK_TITLE);
                cancelTitle = jsObj.getString(CANCEL_TITLE);
                defaultValue = jsObj.getString(DEFAULT);
            } catch (Exception e) {
                WXLogUtils.e("[WXModalUIModule] confirm param parse error ", e);
            }
        }
        if (TextUtils.isEmpty(message)) {
            message = "";
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(mWXSDKInstance.getContext());
        builder.setMessage(message);
        final EditText editText = new EditText(mWXSDKInstance.getContext());
        editText.setText(defaultValue);
        builder.setView(editText);
        final String okTitleFinal = TextUtils.isEmpty(okTitle) ? OK : okTitle;
        final String cancelTitleFinal = TextUtils.isEmpty(cancelTitle) ? CANCEL : cancelTitle;
        builder.setPositiveButton(okTitleFinal, new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (callback != null) {
                    Map<String, Object> result = new HashMap<String, Object>();
                    result.put(RESULT, okTitleFinal);
                    result.put(DATA, editText.getText().toString());
                    callback.invoke(result);
                }
            }
        }).setNegativeButton(cancelTitleFinal, new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (callback != null) {
                    Map<String, Object> result = new HashMap<String, Object>();
                    result.put(RESULT, cancelTitleFinal);
                    result.put(DATA, editText.getText().toString());
                    callback.invoke(result);
                }
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.show();
        tracking(alertDialog);
    } else {
        WXLogUtils.e("when call prompt mWXSDKInstance.getContext() must instanceof Activity");
    }
}
Also used : AlertDialog(android.app.AlertDialog) EditText(android.widget.EditText) DialogInterface(android.content.DialogInterface) HashMap(java.util.HashMap) Activity(android.app.Activity) JSONObject(com.alibaba.fastjson.JSONObject) OnClickListener(android.content.DialogInterface.OnClickListener) JSONObject(com.alibaba.fastjson.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map) JSMethod(com.taobao.weex.annotation.JSMethod)

Example 55 with JSMethod

use of com.taobao.weex.annotation.JSMethod in project weex-example by KalicyZhou.

the class SimpleComponentHolder method getMethods.

static Pair<Map<String, Invoker>, Map<String, Invoker>> getMethods(Class clz) {
    Map<String, Invoker> methods = new HashMap<>();
    Map<String, Invoker> mInvokers = new HashMap<>();
    Annotation[] annotations;
    Annotation anno;
    try {
        for (Method method : clz.getMethods()) {
            try {
                annotations = method.getDeclaredAnnotations();
                for (int i = 0, annotationsCount = annotations.length; i < annotationsCount; ++i) {
                    anno = annotations[i];
                    if (anno == null) {
                        continue;
                    }
                    if (anno instanceof WXComponentProp) {
                        String name = ((WXComponentProp) anno).name();
                        methods.put(name, new MethodInvoker(method, true));
                        break;
                    } else if (anno instanceof JSMethod) {
                        JSMethod methodAnno = (JSMethod) anno;
                        String name = methodAnno.alias();
                        if (JSMethod.NOT_SET.equals(name)) {
                            name = method.getName();
                        }
                        mInvokers.put(name, new MethodInvoker(method, methodAnno.uiThread()));
                        break;
                    }
                }
            } catch (ArrayIndexOutOfBoundsException | IncompatibleClassChangeError e) {
            //ignore: getDeclaredAnnotations may throw this
            }
        }
    } catch (IndexOutOfBoundsException e) {
        e.printStackTrace();
    //ignore: getMethods may throw this
    }
    return new Pair<>(methods, mInvokers);
}
Also used : HashMap(java.util.HashMap) JSMethod(com.taobao.weex.annotation.JSMethod) Method(java.lang.reflect.Method) JSMethod(com.taobao.weex.annotation.JSMethod) Annotation(java.lang.annotation.Annotation) MethodInvoker(com.taobao.weex.bridge.MethodInvoker) Invoker(com.taobao.weex.bridge.Invoker) WXComponentProp(com.taobao.weex.ui.component.WXComponentProp) MethodInvoker(com.taobao.weex.bridge.MethodInvoker) Pair(android.util.Pair)

Aggregations

JSMethod (com.taobao.weex.annotation.JSMethod)85 WeexEventBean (com.benmu.framework.model.WeexEventBean)50 ArrayList (java.util.ArrayList)13 JSONObject (com.alibaba.fastjson.JSONObject)11 Intent (android.content.Intent)8 HashMap (java.util.HashMap)8 JSCallback (com.taobao.weex.bridge.JSCallback)7 Activity (android.app.Activity)4 EventCenter (com.benmu.framework.event.mediator.EventCenter)4 WXEditText (com.taobao.weex.ui.view.WXEditText)4 Map (java.util.Map)4 AlertDialog (android.app.AlertDialog)3 DialogInterface (android.content.DialogInterface)3 OnClickListener (android.content.DialogInterface.OnClickListener)3 Uri (android.net.Uri)3 EditText (android.widget.EditText)3 JSONException (com.alibaba.fastjson.JSONException)3 LightlyWebSocketAdapter (com.benmu.framework.extend.adapter.LightlyWebSocketAdapter)3 ClipData (android.content.ClipData)2 ClipboardManager (android.content.ClipboardManager)2