net.niyonkuru.koodroid.webview.BlockingWebView.java Source code

Java tutorial

Introduction

Here is the source code for net.niyonkuru.koodroid.webview.BlockingWebView.java

Source

/*
 * Copyright 2012 Mike Niyonkuru
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.niyonkuru.koodroid.webview;

import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebView;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import net.niyonkuru.koodroid.js.JavaScriptInterface;

import org.apache.http.util.EncodingUtils;

import static net.niyonkuru.koodroid.BuildConfig.DEBUG;

public class BlockingWebView extends WebView {
    private static final String TAG = BlockingWebView.class.getSimpleName();

    private CountDownLatch mCountDownLatch;

    private IOException mPendingException;

    private final Handler mHandler;

    private BlockingWebView(Context context) {
        super(context);

        mHandler = new Handler();
    }

    public BlockingWebView init(final boolean loadFrame, final JavaScriptInterface js) {
        js.setWebView(this);

        mHandler.post(new Runnable() {
            @Override
            public void run() {
                setWebViewClient(new KoodoWebViewClient(loadFrame));

                addJavascriptInterface(js, "HTMLOUT");
            }
        });

        return this;
    }

    public void setDownloadListenerOnUiThread(final DownloadListener listener) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                setDownloadListener(listener);
            }
        });
    }

    public void loadOnUiThread(final String url) {
        if (DEBUG)
            Log.i(TAG, "GET: " + url);

        mHandler.post(new Runnable() {
            @Override
            public void run() {
                loadUrl(url);
            }
        });
    }

    public void load(final String url) throws IOException {
        loadOnUiThread(url);

        try {
            mCountDownLatch = new CountDownLatch(1);
            mCountDownLatch.await();
            throwPendingException();
        } catch (InterruptedException e) {
            super.stopLoading();
        }
    }

    public void postOnUiThread(final String url, final String postData) {
        if (DEBUG)
            Log.i(TAG, "POST: " + url + "?" + postData);

        mHandler.post(new Runnable() {
            @Override
            public void run() {
                postUrl(url, EncodingUtils.getBytes(postData, "BASE64"));
            }
        });
    }

    public void finish() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                stopLoading();
            }
        });

        mCountDownLatch.countDown();
    }

    public void abort(IOException e) {
        mPendingException = e;
        finish();
    }

    private void throwPendingException() throws IOException {
        if (mPendingException != null)
            throw mPendingException;
    }

    public static BlockingWebView createInstance(Context ctx) {
        BlockingWebView view = new BlockingWebView(ctx);

        WebSettings websettings = view.getSettings();

        websettings.setJavaScriptEnabled(true);
        websettings.setDatabaseEnabled(false);
        websettings.setDomStorageEnabled(false);
        websettings.setSupportZoom(false);
        websettings.setSavePassword(false);
        websettings.setSupportMultipleWindows(false);
        websettings.setAppCacheEnabled(false);
        websettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        websettings.setBlockNetworkImage(true);

        return view;
    }
}