WebSettingsCompat.java revision f37dff2e70d322b7ac101d2da50551f5d543efe4
1/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.webkit;
18
19import android.os.Build;
20import android.webkit.WebSettings;
21
22import androidx.webkit.internal.WebSettingsAdapter;
23import androidx.webkit.internal.WebViewGlueCommunicator;
24
25/**
26 * Compatibility version of {@link android.webkit.WebSettings}
27 */
28public class WebSettingsCompat {
29    private WebSettingsCompat() {}
30
31    // TODO(gsennton): add feature detection
32
33    /**
34     * Sets whether this WebView should raster tiles when it is
35     * offscreen but attached to a window. Turning this on can avoid
36     * rendering artifacts when animating an offscreen WebView on-screen.
37     * Offscreen WebViews in this mode use more memory. The default value is
38     * false.<br>
39     * Please follow these guidelines to limit memory usage:
40     * <ul>
41     * <li> WebView size should be not be larger than the device screen size.
42     * <li> Limit use of this mode to a small number of WebViews. Use it for
43     *   visible WebViews and WebViews about to be animated to visible.
44     * </ul>
45     */
46    public static void setOffscreenPreRaster(WebSettings webSettings, boolean enabled) {
47        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
48            webSettings.setOffscreenPreRaster(enabled);
49        } else {
50            getAdapter(webSettings).setOffscreenPreRaster(enabled);
51        }
52    }
53
54    /**
55     * Gets whether this WebView should raster tiles when it is
56     * offscreen but attached to a window.
57     * @return {@code true} if this WebView will raster tiles when it is
58     * offscreen but attached to a window.
59     */
60    public static boolean getOffscreenPreRaster(WebSettings webSettings) {
61        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
62            return webSettings.getOffscreenPreRaster();
63        } else {
64            return getAdapter(webSettings).getOffscreenPreRaster();
65        }
66    }
67
68    /**
69     * Sets whether Safe Browsing is enabled. Safe Browsing allows WebView to
70     * protect against malware and phishing attacks by verifying the links.
71     *
72     * <p>
73     * Safe Browsing can be disabled for all WebViews using a manifest tag (read <a
74     * href="{@docRoot}reference/android/webkit/WebView.html">general Safe Browsing info</a>). The
75     * manifest tag has a lower precedence than this API.
76     *
77     * <p>
78     * Safe Browsing is enabled by default for devices which support it.
79     *
80     * @param enabled Whether Safe Browsing is enabled.
81     */
82    public static void setSafeBrowsingEnabled(WebSettings webSettings, boolean enabled) {
83        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
84            webSettings.setSafeBrowsingEnabled(enabled);
85        } else {
86            getAdapter(webSettings).setSafeBrowsingEnabled(enabled);
87        }
88    }
89
90    /**
91     * Gets whether Safe Browsing is enabled.
92     * See {@link #setSafeBrowsingEnabled}.
93     *
94     * @return {@code true} if Safe Browsing is enabled and {@code false} otherwise.
95     */
96    public static boolean getSafeBrowsingEnabled(WebSettings webSettings) {
97        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
98            return webSettings.getSafeBrowsingEnabled();
99        } else {
100            return getAdapter(webSettings).getSafeBrowsingEnabled();
101        }
102    }
103
104    /**
105     * Disables the action mode menu items according to {@code menuItems} flag.
106     * @param menuItems an integer field flag for the menu items to be disabled.
107     */
108    public static void setDisabledActionModeMenuItems(WebSettings webSettings, int menuItems) {
109        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
110            webSettings.setDisabledActionModeMenuItems(menuItems);
111        } else {
112            getAdapter(webSettings).setDisabledActionModeMenuItems(menuItems);
113        }
114    }
115
116    /**
117     * Gets the action mode menu items that are disabled, expressed in an integer field flag.
118     * The default value is {@link WebSettings#MENU_ITEM_NONE}
119     *
120     * @return all the disabled menu item flags combined with bitwise OR.
121     */
122    public static int getDisabledActionModeMenuItems(WebSettings webSettings) {
123        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
124            return webSettings.getDisabledActionModeMenuItems();
125        } else {
126            return getAdapter(webSettings).getDisabledActionModeMenuItems();
127        }
128    }
129
130    private static WebSettingsAdapter getAdapter(WebSettings webSettings) {
131        return WebViewGlueCommunicator.getCompatConverter().convertSettings(webSettings);
132    }
133}
134
135