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.annotation.SuppressLint; 20import android.webkit.WebSettings; 21 22import androidx.annotation.IntDef; 23import androidx.annotation.RequiresFeature; 24import androidx.annotation.RestrictTo; 25import androidx.webkit.internal.WebSettingsAdapter; 26import androidx.webkit.internal.WebViewFeatureInternal; 27import androidx.webkit.internal.WebViewGlueCommunicator; 28 29import java.lang.annotation.ElementType; 30import java.lang.annotation.Retention; 31import java.lang.annotation.RetentionPolicy; 32import java.lang.annotation.Target; 33 34/** 35 * Compatibility version of {@link android.webkit.WebSettings} 36 */ 37public class WebSettingsCompat { 38 private WebSettingsCompat() {} 39 40 /** 41 * Sets whether this WebView should raster tiles when it is 42 * offscreen but attached to a window. Turning this on can avoid 43 * rendering artifacts when animating an offscreen WebView on-screen. 44 * Offscreen WebViews in this mode use more memory. The default value is 45 * false.<br> 46 * Please follow these guidelines to limit memory usage: 47 * <ul> 48 * <li> WebView size should be not be larger than the device screen size. 49 * <li> Limit use of this mode to a small number of WebViews. Use it for 50 * visible WebViews and WebViews about to be animated to visible. 51 * </ul> 52 */ 53 @SuppressLint("NewApi") 54 @RequiresFeature(name = WebViewFeature.OFF_SCREEN_PRERASTER, 55 enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") 56 public static void setOffscreenPreRaster(WebSettings webSettings, boolean enabled) { 57 WebViewFeatureInternal webviewFeature = 58 WebViewFeatureInternal.getFeature(WebViewFeature.OFF_SCREEN_PRERASTER); 59 if (webviewFeature.isSupportedByFramework()) { 60 webSettings.setOffscreenPreRaster(enabled); 61 } else if (webviewFeature.isSupportedByWebView()) { 62 getAdapter(webSettings).setOffscreenPreRaster(enabled); 63 } else { 64 throw WebViewFeatureInternal.getUnsupportedOperationException(); 65 } 66 } 67 68 /** 69 * Gets whether this WebView should raster tiles when it is 70 * offscreen but attached to a window. 71 * @return {@code true} if this WebView will raster tiles when it is 72 * offscreen but attached to a window. 73 */ 74 @SuppressLint("NewApi") 75 @RequiresFeature(name = WebViewFeature.OFF_SCREEN_PRERASTER, 76 enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") 77 public static boolean getOffscreenPreRaster(WebSettings webSettings) { 78 WebViewFeatureInternal webviewFeature = 79 WebViewFeatureInternal.getFeature(WebViewFeature.OFF_SCREEN_PRERASTER); 80 if (webviewFeature.isSupportedByFramework()) { 81 return webSettings.getOffscreenPreRaster(); 82 } else if (webviewFeature.isSupportedByWebView()) { 83 return getAdapter(webSettings).getOffscreenPreRaster(); 84 } else { 85 throw WebViewFeatureInternal.getUnsupportedOperationException(); 86 } 87 } 88 89 /** 90 * Sets whether Safe Browsing is enabled. Safe Browsing allows WebView to 91 * protect against malware and phishing attacks by verifying the links. 92 * 93 * <p> 94 * Safe Browsing can be disabled for all WebViews using a manifest tag (read <a 95 * href="{@docRoot}reference/android/webkit/WebView.html">general Safe Browsing info</a>). The 96 * manifest tag has a lower precedence than this API. 97 * 98 * <p> 99 * Safe Browsing is enabled by default for devices which support it. 100 * 101 * @param enabled Whether Safe Browsing is enabled. 102 */ 103 @SuppressLint("NewApi") 104 @RequiresFeature(name = WebViewFeature.SAFE_BROWSING_ENABLE, 105 enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") 106 public static void setSafeBrowsingEnabled(WebSettings webSettings, boolean enabled) { 107 WebViewFeatureInternal webviewFeature = 108 WebViewFeatureInternal.getFeature(WebViewFeature.SAFE_BROWSING_ENABLE); 109 if (webviewFeature.isSupportedByFramework()) { 110 webSettings.setSafeBrowsingEnabled(enabled); 111 } else if (webviewFeature.isSupportedByWebView()) { 112 getAdapter(webSettings).setSafeBrowsingEnabled(enabled); 113 } else { 114 throw WebViewFeatureInternal.getUnsupportedOperationException(); 115 } 116 } 117 118 /** 119 * Gets whether Safe Browsing is enabled. 120 * See {@link #setSafeBrowsingEnabled}. 121 * 122 * @return {@code true} if Safe Browsing is enabled and {@code false} otherwise. 123 */ 124 @SuppressLint("NewApi") 125 @RequiresFeature(name = WebViewFeature.SAFE_BROWSING_ENABLE, 126 enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") 127 public static boolean getSafeBrowsingEnabled(WebSettings webSettings) { 128 WebViewFeatureInternal webviewFeature = 129 WebViewFeatureInternal.getFeature(WebViewFeature.SAFE_BROWSING_ENABLE); 130 if (webviewFeature.isSupportedByFramework()) { 131 return webSettings.getSafeBrowsingEnabled(); 132 } else if (webviewFeature.isSupportedByWebView()) { 133 return getAdapter(webSettings).getSafeBrowsingEnabled(); 134 } else { 135 throw WebViewFeatureInternal.getUnsupportedOperationException(); 136 } 137 } 138 139 /** 140 * @hide 141 */ 142 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) 143 @IntDef(flag = true, value = { 144 WebSettings.MENU_ITEM_NONE, 145 WebSettings.MENU_ITEM_SHARE, 146 WebSettings.MENU_ITEM_WEB_SEARCH, 147 WebSettings.MENU_ITEM_PROCESS_TEXT 148 }) 149 @Retention(RetentionPolicy.SOURCE) 150 @Target({ElementType.PARAMETER, ElementType.METHOD}) 151 public @interface MenuItemFlags {} 152 153 /** 154 * Disables the action mode menu items according to {@code menuItems} flag. 155 * @param menuItems an integer field flag for the menu items to be disabled. 156 */ 157 @SuppressLint("NewApi") 158 @RequiresFeature(name = WebViewFeature.DISABLED_ACTION_MODE_MENU_ITEMS, 159 enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") 160 public static void setDisabledActionModeMenuItems(WebSettings webSettings, 161 @MenuItemFlags int menuItems) { 162 WebViewFeatureInternal webviewFeature = 163 WebViewFeatureInternal.getFeature(WebViewFeature.DISABLED_ACTION_MODE_MENU_ITEMS); 164 if (webviewFeature.isSupportedByFramework()) { 165 webSettings.setDisabledActionModeMenuItems(menuItems); 166 } else if (webviewFeature.isSupportedByWebView()) { 167 getAdapter(webSettings).setDisabledActionModeMenuItems(menuItems); 168 } else { 169 throw WebViewFeatureInternal.getUnsupportedOperationException(); 170 } 171 } 172 173 /** 174 * Gets the action mode menu items that are disabled, expressed in an integer field flag. 175 * The default value is {@link WebSettings#MENU_ITEM_NONE} 176 * 177 * @return all the disabled menu item flags combined with bitwise OR. 178 */ 179 @SuppressLint("NewApi") 180 @RequiresFeature(name = WebViewFeature.DISABLED_ACTION_MODE_MENU_ITEMS, 181 enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported") 182 public static @MenuItemFlags int getDisabledActionModeMenuItems(WebSettings webSettings) { 183 WebViewFeatureInternal webviewFeature = 184 WebViewFeatureInternal.getFeature(WebViewFeature.DISABLED_ACTION_MODE_MENU_ITEMS); 185 if (webviewFeature.isSupportedByFramework()) { 186 return webSettings.getDisabledActionModeMenuItems(); 187 } else if (webviewFeature.isSupportedByWebView()) { 188 return getAdapter(webSettings).getDisabledActionModeMenuItems(); 189 } else { 190 throw WebViewFeatureInternal.getUnsupportedOperationException(); 191 } 192 } 193 194 private static WebSettingsAdapter getAdapter(WebSettings webSettings) { 195 return WebViewGlueCommunicator.getCompatConverter().convertSettings(webSettings); 196 } 197} 198 199