/*
 * Copyright (C) 2015 Leholady(乐活女人) Inc. All rights reserved.
 */
package net.soulwolf.wvjsbridge;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.webkit.WebView;

import java.util.List;

/**
 * author: EwenQin
 * since : 2016/10/24 下午4:14.
 */
public class WJBridgeWebView extends WebView implements WebViewJavascriptBridge,WJWebLoader{

    private static final String TAG = "WJBridgeWebView";

    private WJBridgeProvider mProvider;

    public WJBridgeWebView(Context context) {
        super(context);
        this._init();
    }

    public WJBridgeWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this._init();
    }

    public WJBridgeWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this._init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public WJBridgeWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this._init();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public WJBridgeWebView(Context context, AttributeSet attrs, int defStyleAttr, boolean privateBrowsing) {
        super(context, attrs, defStyleAttr, privateBrowsing);
        this._init();
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void _init(){
        this.mProvider = WJBridgeProvider.newInstance(this);
        this.setVerticalScrollBarEnabled(false);
        this.setHorizontalScrollBarEnabled(false);
        this.getSettings().setJavaScriptEnabled(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WebView.setWebContentsDebuggingEnabled(true);
        }
        this.setWebViewClient(onCreateWebViewClient(mProvider));
    }

    protected WJBridgeWebViewClient onCreateWebViewClient(WJBridgeProvider provider){
        return new WJBridgeWebViewClient(provider);
    }

    @Override
    public void send(String data) {
        this.mProvider.send(data);
    }

    @Override
    public void send(String data, WJCallbacks callbacks) {
        this.mProvider.send(data,callbacks);
    }

    @Override
    public void setStartupMessages(List<WJMessage> messages) {
        this.mProvider.setStartupMessages(messages);
    }

    @Override
    public void loadUrl(String jsUrl, WJCallbacks callbacks) {
        this.mProvider.loadUrl(jsUrl,callbacks);
    }

    @Override
    public void callHandler(String handlerName, String data, WJCallbacks callbacks) {
        this.mProvider.callHandler(handlerName,data,callbacks);
    }

    @Override
    public void registerHandler(String handlerName, WJBridgeHandler handler) {
        this.mProvider.registerHandler(handlerName,handler);
    }

    @Override
    public void setDefaultHandler(WJBridgeHandler handler) {
        this.mProvider.setDefaultHandler(handler);
    }

    @Override
    public void destroy() {
        this.mProvider.destroy();
        super.destroy();
    }
}

@@ -13,56 +13,666 @@
 ght 
-(C) 2015 Leholady(%E4%B9%90%E6%B4%BB%E5%A5%B3%E4%BA%BA) Inc. All rights reserved
+2015 Soulwolf Ching%0A * Copyright 2015 The Android Open Source Project for WebViewJavascriptBridge%0A * %0A * Licens ed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A * %0A *     http://www.apache.org/licenses/LICENSE-2.0%0A * %0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License
 .%0A *

Update WJBridgeWebView.java