Search in sources :

Example 6 with PageActivity

use of vip.kuaifan.weiui.PageActivity in project weiui by kuaifan.

the class weiuiPage method rewriteUrl.

public static String rewriteUrl(Context context, String url) {
    if (url == null || url.startsWith("http") || url.startsWith("ftp://")) {
        return url;
    }
    if (context instanceof PageActivity) {
        PageBean info = ((PageActivity) context).getPageInfo();
        if (info != null) {
            try {
                URL tmp = new URL(info.getUrl());
                String newUrl = tmp.getProtocol() + "://" + tmp.getHost();
                newUrl += (tmp.getPort() > -1 && tmp.getPort() != 80) ? (":" + tmp.getPort()) : "";
                if (url.startsWith("/")) {
                    return newUrl + url;
                } else {
                    String path = "/";
                    int lastIndex = tmp.getPath().lastIndexOf("/");
                    if (lastIndex > -1) {
                        path = tmp.getPath().substring(0, lastIndex + 1);
                    }
                    return newUrl + path + url;
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
    }
    return url;
}
Also used : PageActivity(vip.kuaifan.weiui.PageActivity) MalformedURLException(java.net.MalformedURLException) PageBean(vip.kuaifan.weiui.extend.bean.PageBean) URL(java.net.URL)

Example 7 with PageActivity

use of vip.kuaifan.weiui.PageActivity in project weiui by kuaifan.

the class weiuiAdDialog method create.

/**
 ************************************************************************************************
 */
/**
 ************************************************************************************************
 */
/**
 ************************************************************************************************
 */
public static void create(WXSDKInstance mWXSDKInstance, JSONObject json, JSCallback callback) {
    if (mWXSDKInstance == null || json == null) {
        return;
    }
    String imgUrl = weiuiParse.parseStr(json.getString("imgUrl"), json.getString("img"));
    if (imgUrl == null || imgUrl.isEmpty()) {
        return;
    }
    Context mContext = mWXSDKInstance.getContext();
    if (mContext == null) {
        return;
    }
    String dialogName = weiuiParse.parseStr(json.getString("dialogName"), weiuiCommon.randomString(8));
    int dialogWidth = weiuiParse.parseInt(json.getString("width"));
    int dialogHeight = weiuiParse.parseInt(json.getString("height"));
    boolean showClose = weiuiParse.parseBool(json.getString("showClose"), true);
    boolean backClose = weiuiParse.parseBool(json.getString("backClose"), true);
    // 
    if (callback != null) {
        Map<String, Object> newData = new HashMap<>();
        newData.put("status", "load");
        newData.put("dialogName", dialogName);
        newData.put("imgUrl", imgUrl);
        callback.invokeAndKeepAlive(newData);
    }
    Glide.with(mContext).asBitmap().load(imgUrl).apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL)).into(new SimpleTarget<Bitmap>() {

        @Override
        public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
            PageActivity.startTransparentPage(mContext, new JSCallback() {

                @Override
                public void invoke(Object data) {
                    if (callback != null) {
                        callback.invoke(data);
                    }
                }

                @Override
                public void invokeAndKeepAlive(Object data) {
                    Map<String, Object> retData = weiuiMap.objectToMap(data);
                    String pageName = weiuiParse.parseStr(retData.get("pageName"));
                    String status = weiuiParse.parseStr(retData.get("status"));
                    PageBean mBean = weiuiPage.getPageBean(pageName);
                    if (mBean == null) {
                        return;
                    }
                    if (status.equals("create")) {
                        Activity mActivity = (Activity) mBean.getContext();
                        Animation mAnimation = AnimationUtils.loadAnimation(mActivity, R.anim.in_to_down);
                        // 
                        View tmpView = LayoutInflater.from(mActivity).inflate(R.layout.dialog_ad, null);
                        ImageView tmpImage = tmpView.findViewById(R.id.v_activity_picture);
                        ImageView tmpClose = tmpView.findViewById(R.id.v_activity_close);
                        ViewGroup fullBox = tmpView.findViewById(R.id.float_full_box);
                        // 
                        if (callback != null) {
                            tmpImage.setOnClickListener(view -> {
                                Map<String, Object> newData = (Map<String, Object>) data;
                                newData.put("status", "click");
                                newData.put("dialogName", dialogName);
                                newData.put("imgUrl", imgUrl);
                                callback.invokeAndKeepAlive(newData);
                            });
                        }
                        if (showClose) {
                            tmpClose.setVisibility(View.VISIBLE);
                            tmpClose.setOnClickListener(view -> close(dialogName));
                        }
                        if (mActivity instanceof PageActivity) {
                            ((PageActivity) mActivity).setOnBackPressed(() -> !backClose || close(dialogName));
                        }
                        // 
                        DialogBean tmpBean = new DialogBean();
                        tmpBean.setDialogName(dialogName);
                        tmpBean.setImgUrl(imgUrl);
                        tmpBean.setPageName(mBean.getPageName());
                        tmpBean.setContext(mBean.getContext());
                        tmpBean.setView(tmpView);
                        mAdDialogList.add(tmpBean);
                        // 
                        ViewGroup.LayoutParams params = tmpImage.getLayoutParams();
                        if (dialogWidth > 0 && dialogHeight > 0) {
                            params.width = weiuiScreenUtils.weexPx2dp(mWXSDKInstance, dialogWidth);
                            params.height = weiuiScreenUtils.weexPx2dp(mWXSDKInstance, dialogHeight);
                        } else if (dialogWidth > 0) {
                            params.width = weiuiScreenUtils.weexPx2dp(mWXSDKInstance, dialogWidth);
                            params.height = (int) weiuiCommon.scaleHeight(params.width, resource.getWidth(), resource.getHeight());
                        } else if (dialogHeight > 0) {
                            params.height = weiuiScreenUtils.weexPx2dp(mWXSDKInstance, dialogHeight);
                            params.width = (int) weiuiCommon.scaleWidth(params.height, resource.getWidth(), resource.getHeight());
                        } else {
                            params.width = (int) (ScreenUtils.getScreenWidth() * 0.75);
                            params.height = (int) weiuiCommon.scaleHeight(params.width, resource.getWidth(), resource.getHeight());
                        }
                        tmpImage.setLayoutParams(params);
                        tmpImage.setImageBitmap(resource);
                        fullBox.startAnimation(mAnimation);
                        // 
                        if (callback != null) {
                            Map<String, Object> newData = (Map<String, Object>) data;
                            newData.put("status", "show");
                            newData.put("dialogName", dialogName);
                            newData.put("imgUrl", imgUrl);
                            callback.invokeAndKeepAlive(newData);
                        }
                        // 
                        ((FrameLayout) mActivity.findViewById(R.id.v_body)).addView(tmpView);
                    }
                    if (callback != null) {
                        if (data instanceof Map) {
                            Map<String, Object> newData = (Map<String, Object>) data;
                            newData.put("dialogName", dialogName);
                            newData.put("imgUrl", imgUrl);
                            callback.invokeAndKeepAlive(newData);
                        } else {
                            callback.invokeAndKeepAlive(data);
                        }
                    }
                }
            });
        }
    });
}
Also used : Context(android.content.Context) PageActivity(vip.kuaifan.weiui.PageActivity) HashMap(java.util.HashMap) RequestOptions(vip.kuaifan.weiui.extend.integration.glide.request.RequestOptions) ViewGroup(android.view.ViewGroup) PageActivity(vip.kuaifan.weiui.PageActivity) Activity(android.app.Activity) ImageView(android.widget.ImageView) View(android.view.View) JSCallback(com.taobao.weex.bridge.JSCallback) Bitmap(android.graphics.Bitmap) PageBean(vip.kuaifan.weiui.extend.bean.PageBean) FrameLayout(android.widget.FrameLayout) Animation(android.view.animation.Animation) JSONObject(com.alibaba.fastjson.JSONObject) ImageView(android.widget.ImageView) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with PageActivity

use of vip.kuaifan.weiui.PageActivity in project weiui by kuaifan.

the class weiuiAjax method ajax.

/**
 * 跨域异步请求 AJAX
 * @param context
 * @param json
 * @param callback
 */
public static void ajax(Context context, JSONObject json, JSCallback callback) {
    if (json == null || json.getString("url") == null) {
        return;
    }
    // 
    String url = weiuiJson.getString(json, "url", "");
    String name = weiuiJson.getString(json, "name", "");
    String method = weiuiJson.getString(json, "method", "get").toLowerCase();
    String dataType = weiuiJson.getString(json, "dataType", "json").toLowerCase();
    int timeout = weiuiJson.getInt(json, "timeout", 15000);
    long cache = weiuiJson.getLong(json, "cache", 0);
    // 
    JSONObject headers = weiuiJson.parseObject(json.getString("headers"));
    JSONObject data = weiuiJson.parseObject(json.getString("data"));
    JSONObject files = weiuiJson.parseObject(json.getString("files"));
    // 
    if (name.isEmpty()) {
        if (context instanceof PageActivity) {
            name = ((PageActivity) context).getPageInfo().getPageName();
        } else {
            name = weiuiCommon.randomString(8);
        }
    }
    // 
    Map<String, Object> mData = new HashMap<>();
    mData.put("setting:timeout", timeout);
    mData.put("setting:cache", cache);
    mData.put("setting:cacheLabel", "ajax");
    if (headers.size() > 0) {
        for (Map.Entry<String, Object> entry : headers.entrySet()) {
            mData.put("header:" + entry.getKey(), entry.getValue());
        }
    }
    if (data.size() > 0) {
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            mData.put(entry.getKey(), entry.getValue());
        }
    }
    if (files.size() > 0) {
        for (Map.Entry<String, Object> entry : headers.entrySet()) {
            mData.put("file:" + entry.getKey(), entry.getValue());
        }
    }
    // 
    String finalName = name;
    weiuiIhttp.ResultCallback mResultCall = new weiuiIhttp.ResultCallback() {

        @Override
        public void success(String data, boolean isCache) {
            if (callback != null) {
                Map<String, Object> ret = new HashMap<>();
                ret.put("status", "success");
                ret.put("name", finalName);
                ret.put("url", url);
                ret.put("cache", isCache);
                ret.put("result", dataType.equals("json") ? weiuiJson.parseObject(data) : data);
                callback.invokeAndKeepAlive(ret);
            }
        }

        @Override
        public void error(String error) {
            if (callback != null) {
                Map<String, Object> ret = new HashMap<>();
                ret.put("status", "error");
                ret.put("name", finalName);
                ret.put("url", url);
                ret.put("cache", false);
                ret.put("result", error);
                callback.invokeAndKeepAlive(ret);
            }
        }

        @Override
        public void complete() {
            if (callback != null) {
                Map<String, Object> ret = new HashMap<>();
                ret.put("status", "complete");
                ret.put("name", finalName);
                ret.put("url", url);
                ret.put("cache", false);
                ret.put("result", null);
                callback.invoke(ret);
            }
        }
    };
    // 
    if (callback != null) {
        Map<String, Object> ret = new HashMap<>();
        ret.put("status", "ready");
        ret.put("name", name);
        ret.put("url", url);
        ret.put("cache", false);
        ret.put("result", null);
        callback.invokeAndKeepAlive(ret);
    }
    // 
    if (method.equals("post")) {
        weiuiIhttp.post(name, url, mData, mResultCall);
    } else {
        weiuiIhttp.get(name, url, mData, mResultCall);
    }
}
Also used : PageActivity(vip.kuaifan.weiui.PageActivity) HashMap(java.util.HashMap) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

PageActivity (vip.kuaifan.weiui.PageActivity)8 JSONObject (com.alibaba.fastjson.JSONObject)5 PageBean (vip.kuaifan.weiui.extend.bean.PageBean)5 Activity (android.app.Activity)3 JSMethod (com.taobao.weex.annotation.JSMethod)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Context (android.content.Context)1 Bitmap (android.graphics.Bitmap)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 Animation (android.view.animation.Animation)1 FrameLayout (android.widget.FrameLayout)1 ImageView (android.widget.ImageView)1 JSCallback (com.taobao.weex.bridge.JSCallback)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 RequestOptions (vip.kuaifan.weiui.extend.integration.glide.request.RequestOptions)1