Weex Android SDK源码分析之Module(webview)

Posted 王永迪

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Weex Android SDK源码分析之Module(webview)相关的知识,希望对你有一定的参考价值。

前言

我才知道weex module中还包括个webview,那就介绍下webview吧,是一个web操作相关的api~

代码解读

weex code

goBack(ref) : 加载前一个webview堆栈历史


Arguments

ref(string): webview 控件的引用.

Example

var webview = require('@weex-module/webview');
// 得到webview元素
var webElement = this.$el('webview');
webview.goBack(webElement.ref);
goForward(ref) :加载下一个webview堆栈历史

Arguments

ref(string) : webview 控件的引用.

Example

var webview = require('@weex-module/webview');
var webElement = this.$el('webview');
webview.goForward(webElement.ref);
reload(ref) : 重新加载当前webpage

Arguments

ref(string) : webview 控件的引用.

Example

var webview = require('@weex-module/webview');
var webElement = this.$el('webview');
webview.reload(webElement.ref);

android code

1、注册 :

    // 注册 webview module
    WXModuleManager.registerModule("webview", WXWebViewModule.class, true);
    // 注册 webview 组件
    registerComponent("web", WXWeb.class);

2、WXWebViewModule

public class WXWebViewModule extends WXModule 

    private enum Action 
        reload,
        goBack,
        goForward
    

    @WXModuleAnno
    public void goBack(String ref) 
        action(Action.goBack, ref);
    

    @WXModuleAnno
    public void goForward(String ref) 
        action(Action.goForward, ref);
    

    @WXModuleAnno
    public void reload(String ref) 
        action(Action.reload, ref);
    

    private void action(Action action, String ref) 
        WXWeb webComponent = (WXWeb) WXSDKManager.getInstance().getWXRenderManager()
                                                .getWXComponent(mWXSDKInstance.getInstanceId(), ref);
        webComponent.setAction(action.name());
    

3、WXWeb

public class WXWeb extends WXComponent 

    protected IWebView mWebView;
    private String mUrl;
    private boolean mUrlChanged;

    public WXWeb(WXSDKInstance instance, WXDomObject dom, WXVContainer parent,
     String instanceId, boolean isLazy) 
        super(instance, dom, parent, instanceId, isLazy);
        createView();
    

    // 创建
    protected void  createView()
        mWebView = new WXWebView(mContext);
    

    @Override
    protected void initView() 
        mWebView.setOnErrorListener(new IWebView.OnErrorListener() 
            @Override
            public void onError(String type, Object message) 
                fireEvent(type, message);
            
        );
        mWebView.setOnPageListener(new IWebView.OnPageListener() 
            @Override
            public void onReceivedTitle(String title) 
                if (mDomObj.event != null &&
                 mDomObj.event.contains(WXEventType.WEBVIEW_RECEIVEDTITLE)) 
                    Map<String, Object> params = new HashMap<>();
                    params.put("title", title);
                    WXSDKManager.getInstance()
                    .fireEvent(mInstanceId, getRef(), WXEventType.WEBVIEW_RECEIVEDTITLE, params);
                
            

            @Override
            public void onPageStart(String url) 
                if (mDomObj.event != null && mDomObj.event.contains(WXEventType.WEBVIEW_PAGESTART)) 
                    Map<String, Object> params = new HashMap<>();
                    params.put("url", url);
                    WXSDKManager.getInstance()
                    .fireEvent(mInstanceId, getRef(), WXEventType.WEBVIEW_PAGESTART, params);
                
            

            @Override
            public void onPageFinish(String url, boolean canGoBack, boolean canGoForward) 
                if (mDomObj.event != null && 
                    mDomObj.event.contains(WXEventType.WEBVIEW_PAGEFINISH)) 
                    Map<String, Object> params = new HashMap<>();
                    params.put("url", url);
                    params.put("canGoBack", canGoBack);
                    params.put("canGoForward", canGoForward);
                    WXSDKManager.getInstance()
                    .fireEvent(mInstanceId, getRef(), WXEventType.WEBVIEW_PAGEFINISH, params);
                
            
        );
        mHost = mWebView.getView();
    

    @Override
    public void flushView() 
        super.flushView();
        if (!TextUtils.isEmpty(mUrl) && mUrlChanged) 
            mUrlChanged = false;
            loadUrl(mUrl);
        
    

    @Override
    public void destroy() 
        super.destroy();
        getWebView().destroy();
    

    // is show loading
    @WXComponentProp(name = "show-loading")
    public void setShowLoading(boolean showLoading) 
        getWebView().setShowLoading(showLoading);
    

    // url
    @WXComponentProp(name = "src")
    public void setUrl(String url) 
        if (TextUtils.isEmpty(url) || mHost == null) 
            return;
        
        mUrl = url;
        mUrlChanged = true;
    

    // 意图处理
    public void setAction(String action) 
        if (!TextUtils.isEmpty(action)) 
            if (action.equals("goBack")) 
                goBack();
             else if (action.equals("goForward")) 
                goForward();
             else if (action.equals("reload")) 
                reload();
            
        
    

    private void fireEvent(String type, Object message) 
        if (mDomObj.event != null && mDomObj.event.contains(WXEventType.WEBVIEW_ERROR)) 
            Map<String, Object> params = new HashMap<>();
            params.put("type", type);
            params.put("errorMsg", message);
            WXSDKManager.getInstance()
            .fireEvent(mInstanceId, getRef(), WXEventType.WEBVIEW_ERROR, params);
        
    

    private void loadUrl(String url) 
        getWebView().loadUrl(url);
    

    private void reload() 
        getWebView().reload();
    

    private void goForward() 
        getWebView().goForward();
    

    private void goBack() 
        getWebView().goBack();
    

    private IWebView getWebView() 
        return mWebView;
    


4、WXWebView

public class WXWebView implements IWebView 

    private Context mContext;
    private WebView mWebView;
    private ProgressBar mProgressBar;
    private boolean mShowLoading = true;
    private OnErrorListener mOnErrorListener;
    private OnPageListener mOnPageListener;

    public WXWebView(Context context) 
        mContext = context;
    

    @Override
    public View getView() 
        FrameLayout root = new FrameLayout(mContext);
        root.setBackgroundColor(Color.WHITE);
        mWebView = new WebView(mContext);
        FrameLayout.LayoutParams wvLayoutParams =
                new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                                             FrameLayout.LayoutParams.MATCH_PARENT);
        wvLayoutParams.gravity = Gravity.CENTER;
        mWebView.setLayoutParams(wvLayoutParams);
        root.addView(mWebView);
        initWebView(mWebView);
        mProgressBar = new ProgressBar(mContext);
        showProgressBar(false);
        FrameLayout.LayoutParams pLayoutParams =
                new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                                             FrameLayout.LayoutParams.WRAP_CONTENT);
        mProgressBar.setLayoutParams(pLayoutParams);
        pLayoutParams.gravity = Gravity.CENTER;
        root.addView(mProgressBar);
        return root;
    

    @Override
    public void destroy() 
        if (getWebView() != null) 
            getWebView().removeAllViews();
            getWebView().destroy();
            mWebView = null;
        
    

    @Override
    public void loadUrl(String url) 
        getWebView().loadUrl(url);
    

    @Override
    public void reload() 
        getWebView().reload();
    

    @Override
    public void goBack() 
        getWebView().goBack();
    

    @Override
    public void goForward() 
        getWebView().goForward();
    

    @Override
    public void setShowLoading(boolean shown) 
        mShowLoading = shown;
    

    @Override
    public void setOnErrorListener(OnErrorListener listener) 
        mOnErrorListener = listener;
    

    @Override
    public void setOnPageListener(OnPageListener listener) 
        mOnPageListener = listener;
    

    private void showProgressBar(boolean shown) 
        if (mShowLoading) 
            mProgressBar.setVisibility(shown ? View.VISIBLE : View.GONE);
        
    

    private void showWebView(boolean shown) 
        mWebView.setVisibility(shown ? View.VISIBLE : View.INVISIBLE);
    

    private WebView getWebView() 
        return mWebView;
    

    private void initWebView(WebView wv) 
        // 初始化
        WebSettings settings = wv.getSettings();
        settings.setjavascriptEnabled(true);
        settings.setAppCacheEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setDomStorageEnabled(true);
        settings.setSupportZoom(false);
        settings.setBuiltInZoomControls(false);
        // 设置客户端
        wv.setWebViewClient(new WebViewClient() 
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) 
                view.loadUrl(url);
                WXLogUtils.v("tag", "onPageOverride " + url);
                return true;
            

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) 
                super.onPageStarted(view, url, favicon);
                WXLogUtils.v("tag", "onPageStarted " + url);
                if (mOnPageListener != null) 
                    mOnPageListener.onPageStart(url);
                
            

            @Override
            public void onPageFinished(WebView view, String url) 
                super.onPageFinished(view, url);
                WXLogUtils.v("tag", "onPageFinished " + url);
                if (mOnPageListener != null) 
                    mOnPageListener.onPageFinish(url, view.canGoBack(), view.canGoForward());
                
            

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request
            , WebResourceError error) 
                super.onReceivedError(view, request, error);
                if (mOnErrorListener != null) 
                    mOnErrorListener.onError("error", "page error");
                
            

            @Override
            public void onReceivedHttpError(WebView view, WebResourceRequest request,
             WebResourceResponse errorResponse) 
                super.onReceivedHttpError(view, request, errorResponse);
                if (mOnErrorListener != null) 
                    mOnErrorListener.onError("error", "http error");
                
            

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) 
                super.onReceivedSslError(view, handler, error);
                if (mOnErrorListener != null) 
                    mOnErrorListener.onError("error", "ssl error");
                
            

        );
        // 设置浏览器客户端
        wv.setWebChromeClient(new WebChromeClient() 
            @Override
            public void onProgressChanged(WebView view, int newProgress) 
                super.onProgressChanged(view, newProgress);
                showWebView(newProgress == 100);
                showProgressBar(newProgress != 100);
                WXLogUtils.v("tag", "onPageProgressChanged " + newProgress);
            

            @Override
            public void onReceivedTitle(WebView view, String title) 
                super.onReceivedTitle(view, title);
                if (mOnPageListener != null) 
                    mOnPageListener.onReceivedTitle(view.getTitle());
                
            

        );
    

1、WXWebViewModule 接收到意图指令,命令WXWeb执行指令;
2、WXWeb 通过IWebView 获取到WXWebView 对象,通知WXWebView 执行指令;
3、部分重要操作都已标明注释;

以上是关于Weex Android SDK源码分析之Module(webview)的主要内容,如果未能解决你的问题,请参考以下文章

Weex Android SDK源码分析之Module(modal)

Weex Android SDK源码分析之Module(webview)

Weex Android SDK源码分析之Module(animation)

Weex Android SDK源码分析之Module(animation)

Weex Android SDK源码分析之界面渲染(上)

Weex系列之Module源码解析