Code example for WebView: Displaying web pages

Methods: addJavascriptInterfacegetSettingsloadUrlsetWebChromeClientsetWebViewClient

0
import android.util.Log; 
import android.view.ViewGroup; 
import android.webkit.HttpAuthHandler; 
import android.webkit.JsPromptResult; 
import android.webkit.JsResult; 
import android.webkit.SslErrorHandler; 
import android.webkit.WebChromeClient; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.LinearLayout; 
 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.Vector; 
 
public class TestShellActivity extends Activity implements LayoutTestController {
 
    static enum DumpDataType {DUMP_AS_TEXT, EXT_REPR, NO_OP}
 
    public class AsyncHandler extends Handler {
        @Override 
        public void handleMessage(Message msg) {
            if (msg.what == MSG_TIMEOUT) {
                mTimedOut = true;
                if(mCallback != null)
                    mCallback.timedOut(mWebView.getUrl());
                requestWebKitData(); 
                return; 
            } else if (msg.what == MSG_WEBKIT_DATA) {
                TestShellActivity.this.dump(mTimedOut, (String)msg.obj);
                return; 
            } 
 
            super.handleMessage(msg);
        } 
    } 
 
    public void requestWebKitData() { 
        Message callback = mHandler.obtainMessage(MSG_WEBKIT_DATA);
 
        if (mRequestedWebKitData)
            throw new AssertionError("Requested webkit data twice: " + mWebView.getUrl());
 
        mRequestedWebKitData = true;
        switch (mDumpDataType) {
            case DUMP_AS_TEXT:
                mWebView.documentAsText(callback);
                break; 
            case EXT_REPR:
                mWebView.externalRepresentation(callback);
                break; 
            default: 
                finished(); 
                break; 
        } 
    } 
 
    public void clearCache() { 
      mWebView.clearCache(true);
    } 
 
    @Override 
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
 
        LinearLayout contentView = new LinearLayout(this);
        contentView.setOrientation(LinearLayout.VERTICAL);
        setContentView(contentView);
 
        mWebView = new WebView(this);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebChromeClient(mChromeClient);
        mWebView.setWebViewClient(new WebViewClient(){
 
            @Override 
            public void onPageFinished(WebView view, String url) {
                Log.v(LOGTAG, "onPageFinished, url=" + url);
                super.onPageFinished(view, url);
            } 
 
            @Override 
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                Log.v(LOGTAG, "onPageStarted, url=" + url);
                super.onPageStarted(view, url, favicon);
            } 
 
            @Override 
            public void onReceivedError(WebView view, int errorCode, String description,
                    String failingUrl) {
                Log.v(LOGTAG, "onReceivedError, errorCode=" + errorCode
                        + ", desc=" + description + ", url=" + failingUrl);
                super.onReceivedError(view, errorCode, description, failingUrl);
            } 
 
            @Override 
            public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler,
                    String host, String realm) {
                handler.cancel();
            } 
 
            @Override 
            public void onReceivedSslError(WebView view, SslErrorHandler handler,
                    SslError error) {
                handler.proceed();
            } 
 
        }); 
        mEventSender = new WebViewEventSender(mWebView);
        mCallbackProxy = new CallbackProxy(mEventSender, this);
 
        mWebView.addJavascriptInterface(mCallbackProxy, "layoutTestController");
        mWebView.addJavascriptInterface(mCallbackProxy, "eventSender");
        contentView.addView(mWebView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, 0.0f));
 
        mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
 
        mHandler = new AsyncHandler();
 
        Intent intent = getIntent();
        if (intent != null) {
            executeIntent(intent);
        } 
    } 
 
    @Override 
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        executeIntent(intent);
    } 
 
    private void executeIntent(Intent intent) {
        resetTestStatus(); 
        if (!Intent.ACTION_VIEW.equals(intent.getAction())) {
            return; 
        } 
 
        mTestUrl = intent.getStringExtra(TEST_URL);
        if (mTestUrl == null) {
            mUiAutoTestPath = intent.getStringExtra(UI_AUTO_TEST);
            if(mUiAutoTestPath != null) {
                beginUiAutoTest(); 
            } 
            return; 
        } 
 
        mResultFile = intent.getStringExtra(RESULT_FILE);
        mTimeoutInMillis = intent.getIntExtra(TIMEOUT_IN_MILLIS, 0);
 
        Log.v(LOGTAG, "  Loading " + mTestUrl);
        mWebView.loadUrl(mTestUrl);
 
        if (mTimeoutInMillis > 0) {
            // Create a timeout timer 
            Message m = mHandler.obtainMessage(MSG_TIMEOUT);
            mHandler.sendMessageDelayed(m, mTimeoutInMillis);