Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.MutableContextWrapper;

import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;

public class Main {
    /**
     * <p>Creates the shared webView used throughout the lifetime of the TurbolinksSession.</p>
     *
     * @param applicationContext An application context.
     * @return The shared WebView.
     */
    static WebView createWebView(Context applicationContext) {
        MutableContextWrapper contextWrapper = new MutableContextWrapper(applicationContext);
        WebView webView = new WebView(contextWrapper);
        configureWebViewDefaults(webView);
        setWebViewLayoutParams(webView);

        return webView;
    }

    /**
     * <p>Configures basic settings of the webView (Javascript enabled, DOM storage enabled,
     * database enabled).</p>
     *
     * @param webView The shared webView.
     */
    @SuppressLint("SetJavaScriptEnabled")
    private static void configureWebViewDefaults(WebView webView) {
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setDatabaseEnabled(true);

        webView.setWebChromeClient(new WebChromeClient());
    }

    /**
     * Sets the WebView's width/height layout params to MATCH_PARENT
     *
     * @param webView The shared webView.
     */
    private static void setWebViewLayoutParams(WebView webView) {
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        webView.setLayoutParams(params);
    }
}