Search in sources :

Example 36 with WebSettings

use of android.webkit.WebSettings in project Lightning-Browser by anthonycr.

the class LightningView method initializeSettings.

/**
     * Initialize the settings of the WebView that are intrinsic to Lightning and cannot
     * be altered by the user. Distinguish between Incognito and Regular tabs here.
     */
@SuppressLint("NewApi")
private void initializeSettings() {
    if (mWebView == null) {
        return;
    }
    final WebSettings settings = mWebView.getSettings();
    if (API < Build.VERSION_CODES.JELLY_BEAN_MR2) {
        //noinspection deprecation
        settings.setAppCacheMaxSize(Long.MAX_VALUE);
    }
    if (API < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        //noinspection deprecation
        settings.setEnableSmoothTransition(true);
    }
    if (API > Build.VERSION_CODES.JELLY_BEAN) {
        settings.setMediaPlaybackRequiresUserGesture(true);
    }
    if (API >= Build.VERSION_CODES.LOLLIPOP && !mIsIncognitoTab) {
        settings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
    } else if (API >= Build.VERSION_CODES.LOLLIPOP) {
        // We're in Incognito mode, reject
        settings.setMixedContentMode(WebSettings.MIXED_CONTENT_NEVER_ALLOW);
    }
    if (!mIsIncognitoTab) {
        settings.setDomStorageEnabled(true);
        settings.setAppCacheEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        settings.setDatabaseEnabled(true);
    } else {
        settings.setDomStorageEnabled(false);
        settings.setAppCacheEnabled(false);
        settings.setDatabaseEnabled(false);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    }
    settings.setSupportZoom(true);
    settings.setBuiltInZoomControls(true);
    settings.setDisplayZoomControls(false);
    settings.setAllowContentAccess(true);
    settings.setAllowFileAccess(true);
    if (API >= Build.VERSION_CODES.JELLY_BEAN) {
        settings.setAllowFileAccessFromFileURLs(false);
        settings.setAllowUniversalAccessFromFileURLs(false);
    }
    getPathObservable("appcache").subscribeOn(Schedulers.io()).observeOn(Schedulers.main()).subscribe(new SingleOnSubscribe<File>() {

        @Override
        public void onItem(@Nullable File item) {
            Preconditions.checkNonNull(item);
            settings.setAppCachePath(item.getPath());
        }
    });
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
        getPathObservable("geolocation").subscribeOn(Schedulers.io()).observeOn(Schedulers.main()).subscribe(new SingleOnSubscribe<File>() {

            @Override
            public void onItem(@Nullable File item) {
                Preconditions.checkNonNull(item);
                //noinspection deprecation
                settings.setGeolocationDatabasePath(item.getPath());
            }
        });
    }
    getPathObservable("databases").subscribeOn(Schedulers.io()).observeOn(Schedulers.main()).subscribe(new SingleOnSubscribe<File>() {

        @Override
        public void onItem(@Nullable File item) {
            if (API < Build.VERSION_CODES.KITKAT) {
                Preconditions.checkNonNull(item);
                //noinspection deprecation
                settings.setDatabasePath(item.getPath());
            }
        }

        @Override
        public void onComplete() {
        }
    });
}
Also used : WebSettings(android.webkit.WebSettings) File(java.io.File) SuppressLint(android.annotation.SuppressLint)

Example 37 with WebSettings

use of android.webkit.WebSettings in project Lightning-Browser by anthonycr.

the class LightningView method initializePreferences.

/**
     * Initialize the preference driven settings of the WebView. This method
     * must be called whenever the preferences are changed within SharedPreferences.
     *
     * @param context the context in which the WebView was created, it is used
     *                to get the default UserAgent for the WebView.
     */
@SuppressLint({ "NewApi", "SetJavaScriptEnabled" })
public synchronized void initializePreferences(@NonNull Context context) {
    if (mWebView == null) {
        return;
    }
    WebSettings settings = mWebView.getSettings();
    if (mPreferences.getDoNotTrackEnabled()) {
        mRequestHeaders.put(HEADER_DNT, "1");
    } else {
        mRequestHeaders.remove(HEADER_DNT);
    }
    if (mPreferences.getRemoveIdentifyingHeadersEnabled()) {
        mRequestHeaders.put(HEADER_REQUESTED_WITH, "");
        mRequestHeaders.put(HEADER_WAP_PROFILE, "");
    } else {
        mRequestHeaders.remove(HEADER_REQUESTED_WITH);
        mRequestHeaders.remove(HEADER_WAP_PROFILE);
    }
    settings.setDefaultTextEncodingName(mPreferences.getTextEncoding());
    sHomepage = mPreferences.getHomepage();
    setColorMode(mPreferences.getRenderingMode());
    if (!mIsIncognitoTab) {
        settings.setGeolocationEnabled(mPreferences.getLocationEnabled());
    } else {
        settings.setGeolocationEnabled(false);
    }
    if (API < Build.VERSION_CODES.KITKAT) {
        switch(mPreferences.getFlashSupport()) {
            case 0:
                //noinspection deprecation
                settings.setPluginState(PluginState.OFF);
                break;
            case 1:
                //noinspection deprecation
                settings.setPluginState(PluginState.ON_DEMAND);
                break;
            case 2:
                //noinspection deprecation
                settings.setPluginState(PluginState.ON);
                break;
            default:
                break;
        }
    }
    setUserAgent(context, mPreferences.getUserAgentChoice());
    if (mPreferences.getSavePasswordsEnabled() && !mIsIncognitoTab) {
        if (API < Build.VERSION_CODES.JELLY_BEAN_MR2) {
            //noinspection deprecation
            settings.setSavePassword(true);
        }
        settings.setSaveFormData(true);
    } else {
        if (API < Build.VERSION_CODES.JELLY_BEAN_MR2) {
            //noinspection deprecation
            settings.setSavePassword(false);
        }
        settings.setSaveFormData(false);
    }
    if (mPreferences.getJavaScriptEnabled()) {
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
    } else {
        settings.setJavaScriptEnabled(false);
        settings.setJavaScriptCanOpenWindowsAutomatically(false);
    }
    if (mPreferences.getTextReflowEnabled()) {
        settings.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS);
        if (API >= android.os.Build.VERSION_CODES.KITKAT) {
            try {
                settings.setLayoutAlgorithm(LayoutAlgorithm.TEXT_AUTOSIZING);
            } catch (Exception e) {
                // This shouldn't be necessary, but there are a number
                // of KitKat devices that crash trying to set this
                Log.e(TAG, "Problem setting LayoutAlgorithm to TEXT_AUTOSIZING");
            }
        }
    } else {
        settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
    }
    settings.setBlockNetworkImage(mPreferences.getBlockImagesEnabled());
    if (!mIsIncognitoTab) {
        settings.setSupportMultipleWindows(mPreferences.getPopupsEnabled());
    } else {
        settings.setSupportMultipleWindows(false);
    }
    settings.setUseWideViewPort(mPreferences.getUseWideViewportEnabled());
    settings.setLoadWithOverviewMode(mPreferences.getOverviewModeEnabled());
    switch(mPreferences.getTextSize()) {
        case 0:
            settings.setTextZoom(200);
            break;
        case 1:
            settings.setTextZoom(150);
            break;
        case 2:
            settings.setTextZoom(125);
            break;
        case 3:
            settings.setTextZoom(100);
            break;
        case 4:
            settings.setTextZoom(75);
            break;
        case 5:
            settings.setTextZoom(50);
            break;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView, !mPreferences.getBlockThirdPartyCookiesEnabled());
    }
}
Also used : WebSettings(android.webkit.WebSettings) SuppressLint(android.annotation.SuppressLint)

Example 38 with WebSettings

use of android.webkit.WebSettings in project Lightning-Browser by anthonycr.

the class LightningView method setUserAgent.

/**
     * This method sets the user agent of the current tab.
     * There are four options, 1, 2, 3, 4.
     * <p/>
     * 1. use the default user agent
     * <p/>
     * 2. use the desktop user agent
     * <p/>
     * 3. use the mobile user agent
     * <p/>
     * 4. use a custom user agent, or the default user agent
     * if none was set.
     *
     * @param context the context needed to get the default user agent.
     * @param choice  the choice of user agent to use, see above comments.
     */
@SuppressLint("NewApi")
private void setUserAgent(Context context, int choice) {
    if (mWebView == null)
        return;
    WebSettings settings = mWebView.getSettings();
    switch(choice) {
        case 1:
            if (API >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                settings.setUserAgentString(WebSettings.getDefaultUserAgent(context));
            } else {
                settings.setUserAgentString(sDefaultUserAgent);
            }
            break;
        case 2:
            settings.setUserAgentString(Constants.DESKTOP_USER_AGENT);
            break;
        case 3:
            settings.setUserAgentString(Constants.MOBILE_USER_AGENT);
            break;
        case 4:
            String ua = mPreferences.getUserAgentString(sDefaultUserAgent);
            if (ua == null || ua.isEmpty()) {
                ua = " ";
            }
            settings.setUserAgentString(ua);
            break;
    }
}
Also used : WebSettings(android.webkit.WebSettings) SuppressLint(android.annotation.SuppressLint)

Example 39 with WebSettings

use of android.webkit.WebSettings in project diary by billthefarmer.

the class Diary method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    textView = (EditText) findViewById(R.id.text);
    scrollView = (ScrollView) findViewById(R.id.scroll);
    markdownView = (MarkdownView) findViewById(R.id.markdown);
    accept = findViewById(R.id.accept);
    edit = findViewById(R.id.edit);
    WebSettings settings = markdownView.getSettings();
    settings.setBuiltInZoomControls(true);
    settings.setDisplayZoomControls(false);
    setListeners();
    gestureDetector = new GestureDetector(this, new GestureListener());
    if (savedInstanceState == null)
        today();
    else {
        setDate(new GregorianCalendar((Integer) savedInstanceState.get(YEAR), (Integer) savedInstanceState.get(MONTH), (Integer) savedInstanceState.get(DAY)));
        shown = (Boolean) savedInstanceState.get(SHOWN);
    }
    // Copy help text to today's page if no entries
    if (prevEntry == null && nextEntry == null && textView.length() == 0)
        textView.setText(getHelp());
}
Also used : WebSettings(android.webkit.WebSettings) GregorianCalendar(java.util.GregorianCalendar) GestureDetector(android.view.GestureDetector)

Example 40 with WebSettings

use of android.webkit.WebSettings in project double-espresso by JakeWharton.

the class SimpleWebViewActivity method onCreate.

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    WebView mainWebView = new WebView(this);
    setContentView(mainWebView);
    mainWebView.loadData("<html>" + "<script>document.was_clicked = false</script>" + "<body> " + "<button style='height:1000px;width:1000px;' onclick='document.was_clicked = true'> " + "I'm a button</button>" + "</body> " + "</html>", "text/html", null);
    WebSettings settings = mainWebView.getSettings();
    settings.setJavaScriptEnabled(true);
}
Also used : WebSettings(android.webkit.WebSettings) WebView(android.webkit.WebView)

Aggregations

WebSettings (android.webkit.WebSettings)92 WebView (android.webkit.WebView)49 WebViewClient (android.webkit.WebViewClient)29 SuppressLint (android.annotation.SuppressLint)23 View (android.view.View)17 WebChromeClient (android.webkit.WebChromeClient)17 Intent (android.content.Intent)10 JsResult (android.webkit.JsResult)7 Bitmap (android.graphics.Bitmap)6 KeyEvent (android.view.KeyEvent)6 WebResourceRequest (android.webkit.WebResourceRequest)6 NetworkCallback (android.net.ConnectivityManager.NetworkCallback)5 Network (android.net.Network)5 NetworkCapabilities (android.net.NetworkCapabilities)5 NetworkRequest (android.net.NetworkRequest)5 Bundle (android.os.Bundle)5 RelativeLayout (android.widget.RelativeLayout)5 TextView (android.widget.TextView)5 WebResourceError (android.webkit.WebResourceError)4 Activity (android.app.Activity)3